Found out about sentinels recently, so I added a sentinel to Sxiv processes.
(require 'dired-x) ; to get `dired-mark-sexp'
(defun sxiv-sentinel (process event)
"In a corresponding Dired buffer, mark the files that were marked in
Sxiv (when run with the -o option)."
(let ((buffer (process-buffer process))
(directory nil)
(marked-files nil))
(when (equal event "finished\n")
(with-current-buffer buffer
(setq marked-files (string-split (buffer-string))
directory default-directory))
(kill-buffer buffer))
(when-let* ((files marked-files)
(dired-buffers (dired-buffers-for-dir directory)))
(with-current-buffer (car dired-buffers)
;; `dired-mark-sexp' doesn't seem to work w/o dynamic scoping.
;; Also, it needs the filenames to be sans directory, so it
;; will mark files with the same name in different subdirs.
(dlet ((filenames (mapcar #'file-name-nondirectory files))
(inhibit-message t))
(dired-mark-sexp '(member name filenames)))))))
;;; then run Sxiv using a command like this
(defun open-in-sxiv ()
(interactive)
(let* ((output-buffer (format " *Sxiv in %s (%s)*" default-directory
(floor (time-to-seconds))))
(process (start-process-shell-command "sxiv"
output-buffer
"sxiv -o ./")))
(set-process-sentinel process 'sxiv-sentinel)))