From 20de6ab6a5567351df9f28d74052bad04877ef08 Mon Sep 17 00:00:00 2001 From: Christian Wittern Date: Sat, 19 Jan 2013 23:22:38 +0800 Subject: [PATCH] * image-mode.el (image-next-file, image-previous-file): New commands. (image-mode-map): Bind them to n and p. (image-mode--images-in-directory): New helper function. Fixes: debbugs:8453 --- etc/NEWS | 5 +++++ lisp/ChangeLog | 8 ++++++++ lisp/image-mode.el | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/etc/NEWS b/etc/NEWS index 4cba17573b3..a2e0f41305a 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -110,6 +110,11 @@ amounts of data into the ERC input. *** Removed icomplete-show-key-bindings. ** Image mode + +*** New commands `n' (`image-next-file') and `p' (`image-previous-file') +visit the next image file and the previous image file in the same +directory, respectively. + --- *** The command `image-mode-fit-frame' deletes other windows. When toggling, it restores the frame's previous window configuration. diff --git a/lisp/ChangeLog b/lisp/ChangeLog index f63e9ecafe8..8d0a61525d9 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,11 @@ +2013-01-19 Christian Wittern (tiny change) + Chong Yidong + + * image-mode.el (image-next-file, image-previous-file): New + commands (Bug#8453). + (image-mode-map): Bind them to n and p. + (image-mode--images-in-directory): New helper function. + 2013-01-19 Chong Yidong * image-mode.el (image-mode-fit-frame): Add a frame argument. diff --git a/lisp/image-mode.el b/lisp/image-mode.el index bbb72335aa3..6a13d528037 100644 --- a/lisp/image-mode.el +++ b/lisp/image-mode.el @@ -339,6 +339,8 @@ call." (define-key map (kbd "SPC") 'image-scroll-up) (define-key map (kbd "DEL") 'image-scroll-down) (define-key map (kbd "RET") 'image-toggle-animation) + (define-key map "n" 'image-next-file) + (define-key map "p" 'image-previous-file) (define-key map [remap forward-char] 'image-forward-hscroll) (define-key map [remap backward-char] 'image-backward-hscroll) (define-key map [remap right-char] 'image-forward-hscroll) @@ -617,6 +619,52 @@ Otherwise it plays once, then stops." (image-animate image index (if image-animate-loop t))))))))) + +;;; Switching to the next/previous image + +(defun image-next-file (&optional n) + "Visit the next image in the same directory as the current image file. +With optional argument N, visit the Nth image file after the +current one, in cyclic alphabetical order. + +This command visits the specified file via `find-alternate-file', +replacing the current Image mode buffer." + (interactive "p") + (unless (derived-mode-p 'image-mode) + (error "The buffer is not in Image mode")) + (unless buffer-file-name + (error "The current image is not associated with a file")) + (let* ((file (file-name-nondirectory buffer-file-name)) + (images (image-mode--images-in-directory file)) + (idx 0)) + (catch 'image-visit-next-file + (dolist (f images) + (if (string= f file) + (throw 'image-visit-next-file (1+ idx))) + (setq idx (1+ idx)))) + (setq idx (mod (+ idx (or n 1)) (length images))) + (find-alternate-file (nth idx images)))) + +(defun image-previous-file (&optional n) + "Visit the preceding image in the same directory as the current file. +With optional argument N, visit the Nth image file preceding the +current one, in cyclic alphabetical order. + +This command visits the specified file via `find-alternate-file', +replacing the current Image mode buffer." + (interactive "p") + (image-next-file (- n))) + +(defun image-mode--images-in-directory (file) + (let* ((dir (file-name-directory buffer-file-name)) + (files (directory-files dir nil + (image-file-name-regexp) t))) + ;; Add the current file to the list of images if necessary, in + ;; case it does not match `image-file-name-regexp'. + (unless (member file files) + (push file files)) + (sort files 'string-lessp))) + ;;; Support for bookmark.el (declare-function bookmark-make-record-default -- 2.39.5