Move from personal init to separate repository, create README

This commit is contained in:
contrapunctus 2020-01-12 23:03:34 +05:30
commit eaea4cd35f
2 changed files with 114 additions and 0 deletions

26
README.md Normal file
View File

@ -0,0 +1,26 @@
# sxiv.el
Launch sxiv (Simple X Image Viewer) from Emacs, with Dired integration.
## Installation
You can get `sxiv.el` from
`sxiv.el` requires [dash.el](https://github.com/magnars/dash.el)
## Usage
`M-x sxiv` - entry point. Run it in a Dired buffer containing images. Files marked in sxiv will be marked in Dired.
If the Dired buffer has marked files, open only those files.
With prefix argument, or when only provided directories, run recursively.
Run it from a text file containing one file name per line to open the listed files.
## TODO
* Create user-customizable variable to hold default arguments
* Mark files in subdirectories if run recursively (by inserting the subdirectory into the current buffer)
* Let user edit options (ideally with transient.el) when called with null argument/two prefix arguments
* When running with a lot of files, sxiv may take some time to start. Signal to the user that it is starting, and let them kill it if they want.
* What should be the behavior when we open Dired-marked files, then mark files within sxiv?
## Bugs
* `sxiv-dired-marked-files-p` doesn't work as intended with non '*' markers (e.g. C or D)

88
sxiv.el Normal file
View File

@ -0,0 +1,88 @@
(require 'dash)
(defvar sxiv--directory nil
"Directory `sxiv' was called from.
Used by `sxiv-filter' to know where to mark files.")
(defun sxiv-dired-marked-files-p ()
"Return t if there are marked files in the current Dired
buffer. With no marked files, or if not in a Dired buffer, return
nil."
(interactive)
(if (equal major-mode 'dired-mode)
(if (save-excursion
(goto-char (point-min))
(re-search-forward dired-re-mark nil t))
t
nil)
nil))
(defun sxiv-filter (process output)
"Open a dired buffer and mark any files marked by the user in `sxiv'.
Used as process filter for `sxiv'."
(find-file sxiv--directory)
(--> output
(split-string it "\n")
(-drop-last 1 it)
(sxiv-dired-mark-files it)))
(defun sxiv-dired-mark-files (files)
(interactive)
(dired-mark-if
(and (not (looking-at-p dired-re-dot))
(not (eolp))
(let ((fn (dired-get-filename t t)))
(and fn (--find (equal fn it) files))))
"file"))
(defun sxiv (&optional prefix)
"Run sxiv(1), the Simple X Image Viewer.
By default, when run in a Dired buffer, open all files in the
current directory. Files marked in sxiv will be marked in Dired.
If run from a Dired buffer with marked files, open only those
files.
With prefix argument, or when only provided directories, run
recursively (-r).
If run from a text file containing one file name per line, open
the files listed."
(interactive "P")
(let* ((paths (cond ((sxiv-dired-marked-files-p)
(dired-get-marked-files))
((derived-mode-p 'text-mode)
(--> (buffer-substring-no-properties (point-min)
(point-max))
(split-string it "\n")
(-drop-last 1 it))) ;; why?
(t (directory-files default-directory))))
(paths (--remove (or (equal it ".")
(equal it ".."))
paths))
(recurse (or prefix
(-every? #'file-directory-p paths)))
;; remove directories if not running recursively
(paths (->> (if recurse
paths
(seq-remove #'file-directory-p paths))
(mapcar #'shell-quote-argument)
(--reduce (concat acc " " it))))
(recurse (if recurse "-r" ""))
(proc (make-process :name "sxiv"
:buffer "sxiv"
:command
(list shell-file-name
shell-command-switch
(concat "sxiv -afo "
recurse
" -- "
paths))
:connection-type 'pipe
:stderr "sxiv-errors")))
(setq sxiv--directory default-directory)
(set-process-filter proc #'sxiv-filter)))
;; Local Variables:
;; nameless-current-name: "sxiv"
;; End: