* image-mode.el (image-next-file, image-previous-file): New commands.
authorChristian Wittern <cwittern@gmail.com>
Sat, 19 Jan 2013 15:22:38 +0000 (23:22 +0800)
committerChong Yidong <cyd@gnu.org>
Sat, 19 Jan 2013 15:22:38 +0000 (23:22 +0800)
(image-mode-map): Bind them to n and p.
(image-mode--images-in-directory): New helper function.

Fixes: debbugs:8453

etc/NEWS
lisp/ChangeLog
lisp/image-mode.el

index 4cba175..a2e0f41 100644 (file)
--- 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.
index f63e9ec..8d0a615 100644 (file)
@@ -1,3 +1,11 @@
+2013-01-19  Christian Wittern  <cwittern@gmail.com>  (tiny change)
+           Chong Yidong  <cyd@gnu.org>
+
+       * 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  <cyd@gnu.org>
 
        * image-mode.el (image-mode-fit-frame): Add a frame argument.
index bbb7233..6a13d52 100644 (file)
@@ -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)
@@ -618,6 +620,52 @@ Otherwise it plays once, then stops."
                           (if image-animate-loop t)))))))))
 
 \f
+;;; 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)))
+
+\f
 ;;; Support for bookmark.el
 (declare-function bookmark-make-record-default
                   "bookmark" (&optional no-file no-context posn))