Compare commits

...

3 Commits

Author SHA1 Message Date
contrapunctus 3544c6b3a4 Open file at point from text files too 2021-04-06 19:10:10 +05:30
contrapunctus b8becd786c Extend TODO 2021-04-06 17:59:08 +05:30
contrapunctus ded8989e82 Add workaround for possible Emacs issue (see #9) 2020-09-03 10:03:27 +05:30
3 changed files with 72 additions and 49 deletions

View File

@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [0.4.0] - 2021-04-06
### Added
* When launching from a text file, open sxiv with the image at point (using `-n`)
## [0.3.3] - 2020-08-03
### Fixed
* Error with selected files in a subdirectory (issue #1)

View File

@ -9,6 +9,11 @@
* Other options like running recursively, modifying arguments, etc
6. [ ] 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.
7. [ ] `sxiv-exclude-strings` does not work recursively, because only the directories are passed to the process. Adding all files to the path might cause it to fail (bash length limit), or take a long time.
* Use find(1) to pass the files?
8. [ ] Make it work in find-dired buffers too
9. [ ] Bug - sometimes, if a lot of files (usually over 50) are marked in sxiv, the input received by `sxiv-insert-subdirs` is incomplete - the first file name is a trailing segment of an actual existing filename, e.g. if a file is called `"foo/bar_baz.jpg"`, I might get something like `"r_baz.jpg"` as the first element.
* I have no idea what's causing this or how to fix it. Help needed! :(
10. [ ] Optimize startup speed, especially with a large number of files.
11. [ ] When using a text file, mark files marked in sxiv.
* Maybe using multiple-cursors or iedit?
12. [x] When launching from a text file, open sxiv with the image at point (using `-n`).

112
sxiv.el
View File

@ -41,6 +41,12 @@ It must contain \"-o\" for marking in Dired buffers to function."
"Exclude files whose paths match these strings."
:type '(repeat string))
(defcustom sxiv-after-exit-functions '(sxiv-dired-mark-files)
"Functions run after the sxiv process exits.
Each function must accept two arguments, the associated process
and a string, which is output just received from it."
:type '(repeat function))
(defvar sxiv--directory nil
"Directory `sxiv' was called from.
Used by `sxiv-filter' to know where to mark files.")
@ -58,7 +64,9 @@ With no marked files, or if not in a Dired buffer, return nil."
Return PATHS unchanged."
(cl-loop for path in paths
;; If the file does not exist in the current directory...
unless (and (file-exists-p (file-name-nondirectory path))
when (and (not (file-exists-p (file-name-nondirectory path)))
;; Workaround for what looks like an Emacs issue (see TODO #9)
(file-exists-p path)
;; ;; ...I don't understand why this is here!
;; ;; Why would there be a directory in the selected
;; ;; files, seeing as one can't mark directories in
@ -68,25 +76,48 @@ Return PATHS unchanged."
do (dired-insert-subdir (file-name-directory path))
finally return paths))
(defun sxiv-dired-mark-files (files)
"Mark FILES in the current (dired) buffer."
(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-filter (_process output)
"Open a `dired' buffer and mark any files marked by the user in `sxiv'.
Used as process filter for `sxiv'.
OUTPUT is the output of the sxiv process as a string."
(defun sxiv-dired-mark-files (_process output)
"Open a `dired' buffer and mark any files marked by the user in `sxiv'."
(find-file sxiv--directory)
(--> (split-string output "\n")
(-drop-last 1 it)
(sxiv-insert-subdirs it)
(sxiv-dired-mark-files it)))
(let ((files (--> (split-string output "\n")
(-drop-last 1 it)
(sxiv-insert-subdirs it))))
(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-filter (process output)
"Used as process filter for `sxiv'.
OUTPUT is the output of the sxiv process as a string."
(run-hook-with-args 'sxiv-after-exit-functions process output))
(defun sxiv-paths-raw ()
(cond ((sxiv-dired-marked-files-p)
(dired-get-marked-files))
((derived-mode-p 'text-mode)
(split-string
(buffer-substring-no-properties (point-min) (point-max))
"\n"))
(t (directory-files default-directory))))
(defun sxiv-file-at-point-index (&optional paths)
"Return index of file at point.
PATHS should be a list of relative file names as strings, and is
required for dired-mode buffers."
(cond ((derived-mode-p 'dired-mode)
(let* ((path-at-point (dired-file-name-at-point))
(image-at-point (and path-at-point
;; REVIEW - also check if file is an image?
(file-regular-p path-at-point)
(file-relative-name path-at-point)))
(index (when image-at-point
(--find-index (equal image-at-point it) paths))))
(when index (1+ index))))
((derived-mode-p 'text-mode)
(line-number-at-pos))))
(defun sxiv (&optional prefix)
"Run sxiv(1), the Simple X Image Viewer.
@ -102,37 +133,21 @@ run recursively (-r).
If run from a text file containing one file name per line, open
the files listed."
(interactive "P")
(let* ((path-at-point (dired-file-name-at-point))
(fn-at-point (when (and path-at-point
;; REVIEW - also check if file is an image?
(file-regular-p path-at-point))
(file-relative-name path-at-point)))
(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")))
(t (directory-files default-directory))))
(paths (--remove (or (equal it ".")
(equal it "..")
;; Currently, this takes effect even
;; when running from a text
;; file...should that be the case?
(-find (lambda (exclude)
(string-match-p exclude it))
sxiv-exclude-strings))
paths))
(let* ((paths (--remove (or (equal it ".") (equal it "..")
;; Currently, this takes effect even
;; when running from a text
;; file...should that be the case?
(-find (lambda (exclude)
(string-match-p exclude it))
sxiv-exclude-strings))
(sxiv-paths-raw)))
;; recurse with prefix arg, or if every path is a directory
(recurse (or prefix (-every? #'file-directory-p paths)))
;; remove directories if not running recursively
(paths (if recurse paths (seq-remove #'file-directory-p paths)))
(fn-at-point-index (when fn-at-point
(--find-index (equal fn-at-point it)
paths)))
(fn-at-point-index (when fn-at-point-index
(-> (1+ fn-at-point-index)
(number-to-string))))
(recurse (if recurse "-r" "")))
(paths (if recurse paths (seq-remove #'file-directory-p paths)))
(index (sxiv-file-at-point-index paths))
(index (when index (number-to-string index)))
(recurse (if recurse "-r" "")))
(setq sxiv--directory default-directory)
(message "Running sxiv...")
(make-process :name "sxiv"
@ -140,8 +155,7 @@ the files listed."
:command
(append '("sxiv")
sxiv-arguments
(when fn-at-point-index
(list "-n" fn-at-point-index))
(when index (list "-n" index))
(list recurse "--")
paths)
:connection-type 'pipe