1 ;;; image-dired.el --- use dired to browse and manipulate your images
3 ;; Copyright (C) 2005-2016 Free Software Foundation, Inc.
6 ;; Keywords: multimedia
7 ;; Author: Mathias Dahl <mathias.rem0veth1s.dahl@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; I needed a program to browse, organize and tag my pictures. I got
30 ;; tired of the old gallery program I used as it did not allow
31 ;; multi-file operations easily. Also, it put things out of my
32 ;; control. Image viewing programs I tested did not allow multi-file
33 ;; operations or did not do what I wanted it to.
35 ;; So, I got the idea to use the wonderful functionality of Emacs and
36 ;; `dired' to do it. It would allow me to do almost anything I wanted,
37 ;; which is basically just to browse all my pictures in an easy way,
38 ;; letting me manipulate and tag them in various ways. `dired' already
39 ;; provide all the file handling and navigation facilities; I only
40 ;; needed to add some functions to display the images.
42 ;; I briefly tried out thumbs.el, and although it seemed more
43 ;; powerful than this package, it did not work the way I wanted to. It
44 ;; was too slow to created thumbnails of all files in a directory (I
45 ;; currently keep all my 2000+ images in the same directory) and
46 ;; browsing the thumbnail buffer was slow too. image-dired.el will not
47 ;; create thumbnails until they are needed and the browsing is done
48 ;; quickly and easily in dired. I copied a great deal of ideas and
49 ;; code from there though... :)
51 ;; `image-dired' stores the thumbnail files in `image-dired-dir'
52 ;; using the file name format ORIGNAME.thumb.ORIGEXT. For example
53 ;; ~/.emacs.d/image-dired/myimage01.thumb.jpg. The "database" is for
54 ;; now just a plain text file with the following format:
56 ;; file-name-non-directory;comment:comment-text;tag1;tag2;tag3;...;tagN
62 ;; * The ImageMagick package. Currently, `convert' and `mogrify' are
63 ;; used. Find it here: http://www.imagemagick.org.
65 ;; * For non-lossy rotation of JPEG images, the JpegTRAN program is
68 ;; * For `image-dired-get-exif-data' and `image-dired-write-exif-data' to work,
69 ;; the command line tool `exiftool' is needed. It can be found here:
70 ;; http://www.sno.phy.queensu.ca/~phil/exiftool/. These two functions
71 ;; are, among other things, used for writing comments to image files
72 ;; using `image-dired-thumbnail-set-image-description' and to create
73 ;; "unique" file names using `image-dired-get-exif-file-name' (used by
74 ;; `image-dired-copy-with-exif-file-name').
80 ;; This information has been moved to the manual. Type `C-h r' to open
81 ;; the Emacs manual and go to the node Thumbnails by typing `g
84 ;; Quickstart: M-x image-dired RET DIRNAME RET
86 ;; where DIRNAME is a directory containing image files.
91 ;; * Supports all image formats that Emacs and convert supports, but
92 ;; the thumbnails are hard-coded to JPEG format.
94 ;; * WARNING: The "database" format used might be changed so keep a
95 ;; backup of `image-dired-db-file' when testing new versions.
101 ;; * Support gallery creation when using per-directory thumbnail
104 ;; * Some sort of auto-rotate function based on rotate info in the
107 ;; * Check if exiftool exist before trying to call it to give a better
110 ;; * Investigate if it is possible to also write the tags to the image
113 ;; * From thumbs.el: Add an option for clean-up/max-size functionality
114 ;; for thumbnail directory.
116 ;; * From thumbs.el: Add setroot function.
118 ;; * From thumbs.el: Add image resizing, if useful (image-dired's automatic
119 ;; "image fit" might be enough)
121 ;; * From thumbs.el: Add the "modify" commands (emboss, negate,
124 ;; * Asynchronous creation of thumbnails.
126 ;; * Add `image-dired-display-thumbs-ring' and functions to cycle that. Find
127 ;; out which is best, saving old batch just before inserting new, or
128 ;; saving the current batch in the ring when inserting it. Adding it
129 ;; probably needs rewriting `image-dired-display-thumbs' to be more general.
131 ;; * Find some way of toggling on and off really nice keybindings in
132 ;; dired (for example, using C-n or <down> instead of C-S-n). Richard
133 ;; suggested that we could keep C-t as prefix for image-dired commands
134 ;; as it is currently not used in dired. He also suggested that
135 ;; `dired-next-line' and `dired-previous-line' figure out if
136 ;; image-dired is enabled in the current buffer and, if it is, call
137 ;; `image-dired-dired-next-line' and
138 ;; `image-dired-dired-previous-line', respectively. Update: This is
139 ;; partly done; some bindings have now been added to dired.
141 ;; * Enhanced gallery creation with basic CSS-support and pagination
142 ;; of tag pages with many pictures.
144 ;; * Rewrite `image-dired-modify-mark-on-thumb-original-file' to be
147 ;; * In some way keep track of buffers and windows and stuff so that
148 ;; it works as the user expects.
150 ;; * More/better documentation
156 (require 'format-spec
)
164 (defgroup image-dired nil
165 "Use dired to browse your images as thumbnails, and more."
166 :prefix
"image-dired-"
169 (defcustom image-dired-dir
(locate-user-emacs-file "image-dired/")
170 "Directory where thumbnail images are stored."
174 (defcustom image-dired-thumbnail-storage
'use-image-dired-dir
175 "How to store image-dired's thumbnail files.
176 Image-Dired can store thumbnail files in one of two ways and this is
177 controlled by this variable. \"Use image-dired dir\" means that the
178 thumbnails are stored in a central directory. \"Per directory\"
179 means that each thumbnail is stored in a subdirectory called
180 \".image-dired\" in the same directory where the image file is.
181 \"Thumbnail Managing Standard\" means that the thumbnails are
182 stored and generated according to the Thumbnail Managing Standard
183 that allows sharing of thumbnails across different programs."
184 :type
'(choice :tag
"How to store thumbnail files"
185 (const :tag
"Thumbnail Managing Standard" standard
)
186 (const :tag
"Use image-dired-dir" use-image-dired-dir
)
187 (const :tag
"Per-directory" per-directory
))
190 (defcustom image-dired-db-file
191 (expand-file-name ".image-dired_db" image-dired-dir
)
192 "Database file where file names and their associated tags are stored."
196 (defcustom image-dired-temp-image-file
197 (expand-file-name ".image-dired_temp" image-dired-dir
)
198 "Name of temporary image file used by various commands."
202 (defcustom image-dired-gallery-dir
203 (expand-file-name ".image-dired_gallery" image-dired-dir
)
204 "Directory to store generated gallery html pages.
205 This path needs to be \"shared\" to the public so that it can access
206 the index.html page that image-dired creates."
210 (defcustom image-dired-gallery-image-root-url
211 "http://your.own.server/image-diredpics"
212 "URL where the full size images are to be found.
213 Note that this path has to be configured in your web server. Image-Dired
214 expects to find pictures in this directory."
218 (defcustom image-dired-gallery-thumb-image-root-url
219 "http://your.own.server/image-diredthumbs"
220 "URL where the thumbnail images are to be found.
221 Note that this path has to be configured in your web server. Image-Dired
222 expects to find pictures in this directory."
226 (defcustom image-dired-cmd-create-thumbnail-program
227 (executable-find "convert")
228 "Executable used to create thumbnail.
229 Used together with `image-dired-cmd-create-thumbnail-options'."
233 (defcustom image-dired-cmd-create-thumbnail-options
234 "%p -size %wx%h \"%f\" -resize \"%wx%h>\" -strip jpeg:\"%t\""
235 "Format of command used to create thumbnail image.
236 Available options are %p which is replaced by
237 `image-dired-cmd-create-thumbnail-program', %w which is replaced by
238 `image-dired-thumb-width', %h which is replaced by `image-dired-thumb-height',
239 %f which is replaced by the file name of the original image and %t
240 which is replaced by the file name of the thumbnail file."
244 (defcustom image-dired-cmd-create-temp-image-program
245 (executable-find "convert")
246 "Executable used to create temporary image.
247 Used together with `image-dired-cmd-create-temp-image-options'."
251 (defcustom image-dired-cmd-create-temp-image-options
252 "%p -size %wx%h \"%f\" -resize \"%wx%h>\" -strip jpeg:\"%t\""
253 "Format of command used to create temporary image for display window.
254 Available options are %p which is replaced by
255 `image-dired-cmd-create-temp-image-program', %w and %h which is replaced by
256 the calculated max size for width and height in the image display window,
257 %f which is replaced by the file name of the original image and %t which
258 is replaced by the file name of the temporary file."
262 (defcustom image-dired-cmd-pngnq-program
(executable-find "pngnq")
263 "The file name of the `pngnq' program.
264 It quantizes colors of PNG images down to 256 colors."
265 :type
'(choice (const :tag
"Not Set" nil
) string
)
268 (defcustom image-dired-cmd-pngcrush-program
(executable-find "pngcrush")
269 "The file name of the `pngcrush' program.
270 It optimizes the compression of PNG images. Also it adds PNG textual chunks
271 with the information required by the Thumbnail Managing Standard."
272 :type
'(choice (const :tag
"Not Set" nil
) string
)
275 (defcustom image-dired-cmd-create-standard-thumbnail-command
277 image-dired-cmd-create-thumbnail-program
" "
278 "-size %wx%h \"%f\" "
279 (unless (or image-dired-cmd-pngcrush-program image-dired-cmd-pngnq-program
)
281 "-set \"Thumb::MTime\" \"%m\" "
282 "-set \"Thumb::URI\" \"file://%f\" "
283 "-set \"Description\" \"Thumbnail of file://%f\" "
284 "-set \"Software\" \"" (emacs-version) "\" "))
285 "-thumbnail \"%wx%h>\" png:\"%t\""
286 (if image-dired-cmd-pngnq-program
288 " ; " image-dired-cmd-pngnq-program
" -f \"%t\""
289 (unless image-dired-cmd-pngcrush-program
291 (if image-dired-cmd-pngcrush-program
293 (unless image-dired-cmd-pngcrush-program
295 " ; " image-dired-cmd-pngcrush-program
" -q "
296 "-text b \"Description\" \"Thumbnail of file://%f\" "
297 "-text b \"Software\" \"" (emacs-version) "\" "
298 ;; "-text b \"Thumb::Image::Height\" \"%oh\" "
299 ;; "-text b \"Thumb::Image::Mimetype\" \"%mime\" "
300 ;; "-text b \"Thumb::Image::Width\" \"%ow\" "
301 "-text b \"Thumb::MTime\" \"%m\" "
302 ;; "-text b \"Thumb::Size\" \"%b\" "
303 "-text b \"Thumb::URI\" \"file://%f\" "
306 "Command to create thumbnails according to the Thumbnail Managing Standard."
310 (defcustom image-dired-cmd-rotate-thumbnail-program
311 (executable-find "mogrify")
312 "Executable used to rotate thumbnail.
313 Used together with `image-dired-cmd-rotate-thumbnail-options'."
317 (defcustom image-dired-cmd-rotate-thumbnail-options
318 "%p -rotate %d \"%t\""
319 "Format of command used to rotate thumbnail image.
320 Available options are %p which is replaced by
321 `image-dired-cmd-rotate-thumbnail-program', %d which is replaced by the
322 number of (positive) degrees to rotate the image, normally 90 or 270
323 \(for 90 degrees right and left), %t which is replaced by the file name
324 of the thumbnail file."
328 (defcustom image-dired-cmd-rotate-original-program
329 (cond ((executable-find "jpegtran"))
330 ((executable-find "convert")))
331 "Executable used to rotate original image.
332 Used together with `image-dired-cmd-rotate-original-options'."
336 (defcustom image-dired-cmd-rotate-original-options
337 (when image-dired-cmd-rotate-original-program
338 (pcase image-dired-cmd-rotate-original-program
339 ((pred (lambda (x) (string-match-p "jpegtran" x
)))
340 "%p -rotate %d -copy all -outfile %t \"%o\"")
341 ((pred (lambda (x) (string-match-p "convert" x
)))
342 "%p -rotate %d \"%o\" %t")))
343 "Format of command used to rotate original image.
344 Available options are %p which is replaced by
345 `image-dired-cmd-rotate-original-program', %d which is replaced by the
346 number of (positive) degrees to rotate the image, normally 90 or
347 270 \(for 90 degrees right and left), %o which is replaced by the
348 original image file name and %t which is replaced by
349 `image-dired-temp-image-file'."
353 (defcustom image-dired-temp-rotate-image-file
354 (expand-file-name ".image-dired_rotate_temp" image-dired-dir
)
355 "Temporary file for rotate operations."
359 (defcustom image-dired-rotate-original-ask-before-overwrite t
360 "Confirm overwrite of original file after rotate operation.
361 If non-nil, ask user for confirmation before overwriting the
362 original file with `image-dired-temp-rotate-image-file'."
366 (defcustom image-dired-cmd-write-exif-data-program
367 (executable-find "exiftool")
368 "Program used to write EXIF data to image.
369 Used together with `image-dired-cmd-write-exif-data-options'."
373 (defcustom image-dired-cmd-write-exif-data-options
374 "%p -%t=\"%v\" \"%f\""
375 "Format of command used to write EXIF data.
376 Available options are %p which is replaced by
377 `image-dired-cmd-write-exif-data-program', %f which is replaced by
378 the image file name, %t which is replaced by the tag name and %v
379 which is replaced by the tag value."
383 (defcustom image-dired-cmd-read-exif-data-program
384 (executable-find "exiftool")
385 "Program used to read EXIF data to image.
386 Used together with `image-dired-cmd-read-exif-data-program-options'."
390 (defcustom image-dired-cmd-read-exif-data-options
391 "%p -s -s -s -%t \"%f\""
392 "Format of command used to read EXIF data.
393 Available options are %p which is replaced by
394 `image-dired-cmd-write-exif-data-program', %f which is replaced
395 by the image file name and %t which is replaced by the tag name."
399 (defcustom image-dired-gallery-hidden-tags
400 (list "private" "hidden" "pending")
401 "List of \"hidden\" tags.
402 Used by `image-dired-gallery-generate' to leave out \"hidden\" images."
403 :type
'(repeat string
)
406 (defcustom image-dired-thumb-size
(if (eq 'standard image-dired-thumbnail-storage
) 128 100)
407 "Size of thumbnails, in pixels.
408 This is the default size for both `image-dired-thumb-width'
409 and `image-dired-thumb-height'."
413 (defcustom image-dired-thumb-width image-dired-thumb-size
414 "Width of thumbnails, in pixels."
418 (defcustom image-dired-thumb-height image-dired-thumb-size
419 "Height of thumbnails, in pixels."
423 (defcustom image-dired-thumb-relief
2
424 "Size of button-like border around thumbnails."
428 (defcustom image-dired-thumb-margin
2
429 "Size of the margin around thumbnails.
430 This is where you see the cursor."
434 (defcustom image-dired-line-up-method
'dynamic
435 "Default method for line-up of thumbnails in thumbnail buffer.
436 Used by `image-dired-display-thumbs' and other functions that needs
437 to line-up thumbnails. Dynamic means to use the available width of
438 the window containing the thumbnail buffer, Fixed means to use
439 `image-dired-thumbs-per-row', Interactive is for asking the user,
440 and No line-up means that no automatic line-up will be done."
441 :type
'(choice :tag
"Default line-up method"
442 (const :tag
"Dynamic" dynamic
)
443 (const :tag
"Fixed" fixed
)
444 (const :tag
"Interactive" interactive
)
445 (const :tag
"No line-up" none
))
448 (defcustom image-dired-thumbs-per-row
3
449 "Number of thumbnails to display per row in thumb buffer."
453 (defcustom image-dired-display-window-width-correction
1
454 "Number to be used to correct image display window width.
455 Change if the default (1) does not work (i.e. if the image does not
460 (defcustom image-dired-display-window-height-correction
0
461 "Number to be used to correct image display window height.
462 Change if the default (0) does not work (i.e. if the image does not
467 (defcustom image-dired-track-movement t
468 "The current state of the tracking and mirroring.
469 For more information, see the documentation for
470 `image-dired-toggle-movement-tracking'."
474 (defcustom image-dired-append-when-browsing nil
475 "Append thumbnails in thumbnail buffer when browsing.
476 If non-nil, using `image-dired-next-line-and-display' and
477 `image-dired-previous-line-and-display' will leave a trail of thumbnail
478 images in the thumbnail buffer. If you enable this and want to clean
479 the thumbnail buffer because it is filled with too many thumbnails,
480 just call `image-dired-display-thumb' to display only the image at point.
481 This value can be toggled using `image-dired-toggle-append-browsing'."
485 (defcustom image-dired-dired-disp-props t
486 "If non-nil, display properties for dired file when browsing.
487 Used by `image-dired-next-line-and-display',
488 `image-dired-previous-line-and-display' and `image-dired-mark-and-display-next'.
489 If the database file is large, this can slow down image browsing in
490 dired and you might want to turn it off."
494 (defcustom image-dired-display-properties-format
"%b: %f (%t): %c"
495 "Display format for thumbnail properties.
496 %b is replaced with associated dired buffer name, %f with file name
497 \(without path) of original image file, %t with the list of tags and %c
502 (defcustom image-dired-external-viewer
503 ;; TODO: Use mailcap, dired-guess-shell-alist-default,
504 ;; dired-view-command-alist.
505 (cond ((executable-find "display"))
506 ((executable-find "xli"))
507 ((executable-find "qiv") "qiv -t"))
508 "Name of external viewer.
509 Including parameters. Used when displaying original image from
510 `image-dired-thumbnail-mode'."
514 (defcustom image-dired-main-image-directory
"~/pics/"
515 "Name of main image directory, if any.
516 Used by `image-dired-copy-with-exif-file-name'."
520 (defcustom image-dired-show-all-from-dir-max-files
50
521 "Maximum number of files to show using `image-dired-show-all-from-dir'
522 before warning the user."
526 (defmacro image-dired--with-db-file
(&rest body
)
527 "Run BODY in a temp buffer containing `image-dired-db-file'.
528 Return the last form in BODY."
530 (if (file-exists-p image-dired-db-file
)
531 (insert-file-contents image-dired-db-file
))
534 (defun image-dired-dir ()
535 "Return the current thumbnails directory (from variable `image-dired-dir').
536 Create the thumbnails directory if it does not exist."
537 (let ((image-dired-dir (file-name-as-directory
538 (expand-file-name image-dired-dir
))))
539 (unless (file-directory-p image-dired-dir
)
540 (make-directory image-dired-dir t
)
541 (message "Creating thumbnails directory"))
544 (defun image-dired-insert-image (file type relief margin
)
545 "Insert image FILE of image TYPE, using RELIEF and MARGIN, at point."
547 (let ((i `(image :type
,type
553 (defun image-dired-get-thumbnail-image (file)
554 "Return the image descriptor for a thumbnail of image file FILE."
555 (unless (string-match (image-file-name-regexp) file
)
556 (error "%s is not a valid image file" file
))
557 (let ((thumb-file (image-dired-thumb-name file
)))
558 (unless (and (file-exists-p thumb-file
)
559 (<= (float-time (nth 5 (file-attributes file
)))
560 (float-time (nth 5 (file-attributes thumb-file
)))))
561 (image-dired-create-thumb file thumb-file
))
562 (create-image thumb-file
)
563 ;; (list 'image :type 'jpeg
565 ;; :relief image-dired-thumb-relief :margin image-dired-thumb-margin)
568 (defun image-dired-insert-thumbnail (file original-file-name
569 associated-dired-buffer
)
570 "Insert thumbnail image FILE.
571 Add text properties ORIGINAL-FILE-NAME and ASSOCIATED-DIRED-BUFFER."
574 (image-dired-insert-image file
575 ;; TODO: this should depend on the real file type
576 (if (eq 'standard image-dired-thumbnail-storage
)
578 image-dired-thumb-relief
579 image-dired-thumb-margin
)
583 (list 'image-dired-thumbnail t
584 'original-file-name original-file-name
585 'associated-dired-buffer associated-dired-buffer
586 'tags
(image-dired-list-tags original-file-name
)
587 'mouse-face
'highlight
588 'comment
(image-dired-get-comment original-file-name
)))))
590 (defun image-dired-thumb-name (file)
591 "Return thumbnail file name for FILE.
592 Depending on the value of `image-dired-thumbnail-storage', the file
593 name will vary. For central thumbnail file storage, make a
594 MD5-hash of the image file's directory name and add that to make
595 the thumbnail file name unique. For per-directory storage, just
596 add a subdirectory. For standard storage, produce the file name
597 according to the Thumbnail Managing Standard."
598 (cond ((eq 'standard image-dired-thumbnail-storage
)
600 (concat "~/.thumbnails/normal/"
601 (md5 (concat "file://" (expand-file-name file
))) ".png")))
602 ((eq 'use-image-dired-dir image-dired-thumbnail-storage
)
603 (let* ((f (expand-file-name file
))
605 ;; Is MD5 hashes fast enough? The checksum of a
606 ;; thumbnail file name need not be that
607 ;; "cryptographically" good so a faster one could
609 (md5 (file-name-as-directory (file-name-directory f
)))))
610 (format "%s%s%s.thumb.%s"
611 (file-name-as-directory (expand-file-name (image-dired-dir)))
613 (if md5-hash
(concat "_" md5-hash
) "")
614 (file-name-extension f
))))
615 ((eq 'per-directory image-dired-thumbnail-storage
)
616 (let ((f (expand-file-name file
)))
617 (format "%s.image-dired/%s.thumb.%s"
618 (file-name-directory f
)
620 (file-name-extension f
))))))
622 (defun image-dired-create-thumb (original-file thumbnail-file
)
623 "For ORIGINAL-FILE, create thumbnail image named THUMBNAIL-FILE."
624 (unless image-dired-cmd-create-thumbnail-program
625 (error "image-dired-cmd-create-thumbnail-program is nil"))
626 (let* ((width (int-to-string image-dired-thumb-width
))
627 (height (int-to-string image-dired-thumb-height
))
628 (modif-time (format "%.0f" (float-time (nth 5 (file-attributes
630 (thumbnail-nq8-file (replace-regexp-in-string ".png\\'" "-nq8.png"
634 (if (eq 'standard image-dired-thumbnail-storage
)
635 image-dired-cmd-create-standard-thumbnail-command
636 image-dired-cmd-create-thumbnail-options
)
638 (cons ?p image-dired-cmd-create-thumbnail-program
)
642 (cons ?f original-file
)
643 (cons ?q thumbnail-nq8-file
)
644 (cons ?t thumbnail-file
))))
646 (when (not (file-exists-p
647 (setq thumbnail-dir
(file-name-directory thumbnail-file
))))
648 (message "Creating thumbnail directory.")
649 (make-directory thumbnail-dir
))
650 (call-process shell-file-name nil nil nil shell-command-switch command
)))
653 (defun image-dired-dired-toggle-marked-thumbs (&optional arg
)
654 "Toggle thumbnails in front of file names in the dired buffer.
655 If no marked file could be found, insert or hide thumbnails on the
656 current line. ARG, if non-nil, specifies the files to use instead
657 of the marked files. If ARG is an integer, use the next ARG (or
658 previous -ARG, if ARG<0) files."
660 (dired-map-over-marks
661 (let* ((image-pos (dired-move-to-filename))
662 (image-file (dired-get-filename nil t
))
665 (when (and image-file
666 (string-match-p (image-file-name-regexp) image-file
))
667 (setq thumb-file
(image-dired-get-thumbnail-image image-file
))
668 ;; If image is not already added, then add it.
669 (let* ((cur-ovs (overlays-in (point) (1+ (point))))
670 (thumb-ov (car (cl-remove-if-not
671 (lambda (ov) (overlay-get ov
'thumb-file
))
674 (delete-overlay thumb-ov
)
675 (put-image thumb-file image-pos
)
677 (cl-loop for o in
(overlays-in (point) (1+ (point)))
678 when
(overlay-get o
'put-image
) collect o into ov
679 finally return
(car ov
)))
680 (overlay-put overlay
'image-file image-file
)
681 (overlay-put overlay
'thumb-file thumb-file
)))))
682 arg
; Show or hide image on ARG next files.
683 'show-progress
) ; Update dired display after each image is updated.
684 (add-hook 'dired-after-readin-hook
685 'image-dired-dired-after-readin-hook nil t
))
687 (defun image-dired-dired-after-readin-hook ()
688 "Relocate existing thumbnail overlays in dired buffer after reverting.
689 Move them to their corresponding files if they still exist.
690 Otherwise, delete overlays."
691 (mapc (lambda (overlay)
692 (when (overlay-get overlay
'put-image
)
693 (let* ((image-file (overlay-get overlay
'image-file
))
694 (image-pos (dired-goto-file image-file
)))
696 (move-overlay overlay image-pos image-pos
)
697 (delete-overlay overlay
)))))
698 (overlays-in (point-min) (point-max))))
700 (defun image-dired-next-line-and-display ()
701 "Move to next dired line and display thumbnail image."
704 (image-dired-display-thumbs
705 t
(or image-dired-append-when-browsing nil
) t
)
706 (if image-dired-dired-disp-props
707 (image-dired-dired-display-properties)))
709 (defun image-dired-previous-line-and-display ()
710 "Move to previous dired line and display thumbnail image."
712 (dired-previous-line 1)
713 (image-dired-display-thumbs
714 t
(or image-dired-append-when-browsing nil
) t
)
715 (if image-dired-dired-disp-props
716 (image-dired-dired-display-properties)))
718 (defun image-dired-toggle-append-browsing ()
719 "Toggle `image-dired-append-when-browsing'."
721 (setq image-dired-append-when-browsing
722 (not image-dired-append-when-browsing
))
723 (message "Append browsing %s."
724 (if image-dired-append-when-browsing
728 (defun image-dired-mark-and-display-next ()
729 "Mark current file in dired and display next thumbnail image."
732 (image-dired-display-thumbs
733 t
(or image-dired-append-when-browsing nil
) t
)
734 (if image-dired-dired-disp-props
735 (image-dired-dired-display-properties)))
737 (defun image-dired-toggle-dired-display-properties ()
738 "Toggle `image-dired-dired-disp-props'."
740 (setq image-dired-dired-disp-props
741 (not image-dired-dired-disp-props
))
742 (message "Dired display properties %s."
743 (if image-dired-dired-disp-props
747 (defvar image-dired-thumbnail-buffer
"*image-dired*"
748 "Image-Dired's thumbnail buffer.")
750 (defun image-dired-create-thumbnail-buffer ()
751 "Create thumb buffer and set `image-dired-thumbnail-mode'."
752 (let ((buf (get-buffer-create image-dired-thumbnail-buffer
)))
753 (with-current-buffer buf
754 (setq buffer-read-only t
)
755 (if (not (eq major-mode
'image-dired-thumbnail-mode
))
756 (image-dired-thumbnail-mode)))
759 (defvar image-dired-display-image-buffer
"*image-dired-display-image*"
760 "Where larger versions of the images are display.")
762 (defun image-dired-create-display-image-buffer ()
763 "Create image display buffer and set `image-dired-display-image-mode'."
764 (let ((buf (get-buffer-create image-dired-display-image-buffer
)))
765 (with-current-buffer buf
766 (setq buffer-read-only t
)
767 (if (not (eq major-mode
'image-dired-display-image-mode
))
768 (image-dired-display-image-mode)))
771 (defvar image-dired-saved-window-configuration nil
772 "Saved window configuration.")
775 (defun image-dired-dired-with-window-configuration (dir &optional arg
)
776 "Open directory DIR and create a default window configuration.
778 Convenience command that:
780 - Opens dired in folder DIR
781 - Splits windows in most useful (?) way
782 - Set `truncate-lines' to t
784 After the command has finished, you would typically mark some
785 image files in dired and type
786 \\[image-dired-display-thumbs] (`image-dired-display-thumbs').
788 If called with prefix argument ARG, skip splitting of windows.
790 The current window configuration is saved and can be restored by
791 calling `image-dired-restore-window-configuration'."
792 (interactive "DDirectory: \nP")
793 (let ((buf (image-dired-create-thumbnail-buffer))
794 (buf2 (image-dired-create-display-image-buffer)))
795 (setq image-dired-saved-window-configuration
796 (current-window-configuration))
798 (delete-other-windows)
801 (setq truncate-lines t
)
804 (switch-to-buffer buf
)
805 (select-window (split-window-below))
806 (switch-to-buffer buf2
)
807 (other-window -
2)))))
809 (defun image-dired-restore-window-configuration ()
810 "Restore window configuration.
811 Restore any changes to the window configuration made by calling
812 `image-dired-dired-with-window-configuration'."
814 (if image-dired-saved-window-configuration
815 (set-window-configuration image-dired-saved-window-configuration
)
816 (message "No saved window configuration")))
819 (defun image-dired-display-thumbs (&optional arg append do-not-pop
)
820 "Display thumbnails of all marked files, in `image-dired-thumbnail-buffer'.
821 If a thumbnail image does not exist for a file, it is created on the
822 fly. With prefix argument ARG, display only thumbnail for file at
823 point (this is useful if you have marked some files but want to show
826 Recommended usage is to split the current frame horizontally so that
827 you have the dired buffer in the left window and the
828 `image-dired-thumbnail-buffer' buffer in the right window.
830 With optional argument APPEND, append thumbnail to thumbnail buffer
831 instead of erasing it first.
833 Optional argument DO-NOT-POP controls if `pop-to-buffer' should be
834 used or not. If non-nil, use `display-buffer' instead of
835 `pop-to-buffer'. This is used from functions like
836 `image-dired-next-line-and-display' and
837 `image-dired-previous-line-and-display' where we do not want the
838 thumbnail buffer to be selected."
840 (let ((buf (image-dired-create-thumbnail-buffer))
841 thumb-name files dired-buf
)
843 (setq files
(list (dired-get-filename)))
844 (setq files
(dired-get-marked-files)))
845 (setq dired-buf
(current-buffer))
846 (with-current-buffer buf
847 (let ((inhibit-read-only t
))
850 (goto-char (point-max)))
853 (setq thumb-name
(image-dired-thumb-name curr-file
))
854 (if (and (not (file-exists-p thumb-name
))
855 (not (= 0 (image-dired-create-thumb curr-file thumb-name
))))
856 (message "Thumb could not be created for file %s" curr-file
)
857 (image-dired-insert-thumbnail thumb-name curr-file dired-buf
)))
859 (cond ((eq 'dynamic image-dired-line-up-method
)
860 (image-dired-line-up-dynamic))
861 ((eq 'fixed image-dired-line-up-method
)
862 (image-dired-line-up))
863 ((eq 'interactive image-dired-line-up-method
)
864 (image-dired-line-up-interactive))
865 ((eq 'none image-dired-line-up-method
)
868 (image-dired-line-up-dynamic))))
870 (display-buffer image-dired-thumbnail-buffer
)
871 (pop-to-buffer image-dired-thumbnail-buffer
))))
874 (defun image-dired-show-all-from-dir (dir)
875 "Make a preview buffer for all images in DIR and display it.
876 If the number of files in DIR matching `image-file-name-regexp'
877 exceeds `image-dired-show-all-from-dir-max-files', a warning will be
879 (interactive "DDir: ")
881 (dired-mark-files-regexp (image-file-name-regexp))
882 (let ((files (dired-get-marked-files)))
883 (if (or (<= (length files
) image-dired-show-all-from-dir-max-files
)
884 (and (> (length files
) image-dired-show-all-from-dir-max-files
)
887 "Directory contains more than %d image files. Proceed? "
888 image-dired-show-all-from-dir-max-files
))))
890 (image-dired-display-thumbs)
891 (pop-to-buffer image-dired-thumbnail-buffer
))
892 (message "Canceled."))))
895 (defalias 'image-dired
'image-dired-show-all-from-dir
)
898 (define-obsolete-function-alias 'tumme
'image-dired
"24.4")
900 (defun image-dired-sane-db-file ()
901 "Check if `image-dired-db-file' exists.
902 If not, try to create it (including any parent directories).
903 Signal error if there are problems creating it."
904 (or (file-exists-p image-dired-db-file
)
906 (unless (file-directory-p (setq dir
(file-name-directory
907 image-dired-db-file
)))
908 (make-directory dir t
))
909 (with-current-buffer (setq buf
(create-file-buffer
910 image-dired-db-file
))
911 (write-file image-dired-db-file
))
913 (file-exists-p image-dired-db-file
))
914 (error "Could not create %s" image-dired-db-file
)))
916 (defun image-dired-write-tags (file-tags)
917 "Write file tags to database.
918 Write each file and tag in FILE-TAGS to the database.
919 FILE-TAGS is an alist in the following form:
921 (image-dired-sane-db-file)
923 (image-dired--with-db-file
924 (setq buffer-file-name image-dired-db-file
)
925 (dolist (elt file-tags
)
928 (goto-char (point-min))
929 (if (search-forward-regexp (format "^%s.*$" file
) nil t
)
933 (when (not (search-forward (format ";%s" tag
) end t
))
935 (insert (format ";%s" tag
))))
936 (goto-char (point-max))
937 (insert (format "\n%s;%s" file tag
))))
940 (defun image-dired-remove-tag (files tag
)
941 "For all FILES, remove TAG from the image database."
942 (image-dired-sane-db-file)
943 (image-dired--with-db-file
944 (setq buffer-file-name image-dired-db-file
)
946 (unless (listp files
)
948 (setq files
(list files
))
949 (error "Files must be a string or a list of strings!")))
951 (goto-char (point-min))
952 (when (search-forward-regexp (format "^%s" file
) nil t
)
956 (when (search-forward-regexp (format "\\(;%s\\)" tag
) end t
)
957 (delete-region (match-beginning 1) (match-end 1))
958 ;; Check if file should still be in the database. If
959 ;; it has no tags or comments, it will be removed.
963 (when (not (search-forward ";" end t
))
965 ;; If on empty line at end of buffer
968 (delete-char -
1)))))))
971 (defun image-dired-list-tags (file)
972 "Read all tags for image FILE from the image database."
973 (image-dired-sane-db-file)
974 (image-dired--with-db-file
976 (when (search-forward-regexp (format "^%s" file
) nil t
)
980 (if (search-forward ";" end t
)
981 (if (search-forward "comment:" end t
)
982 (if (search-forward ";" end t
)
983 (setq tags
(buffer-substring (point) end
)))
984 (setq tags
(buffer-substring (point) end
)))))
985 (split-string tags
";"))))
988 (defun image-dired-tag-files (arg)
989 "Tag marked file(s) in dired. With prefix ARG, tag file at point."
991 (let ((tag (read-string "Tags to add (separate tags with a semicolon): "))
994 (setq files
(list (dired-get-filename)))
995 (setq files
(dired-get-marked-files)))
996 (image-dired-write-tags
1002 (defun image-dired-tag-thumbnail ()
1003 "Tag current thumbnail."
1005 (let ((tag (read-string "Tags to add (separate tags with a semicolon): ")))
1006 (image-dired-write-tags (list (cons (image-dired-original-file-name) tag
))))
1007 (image-dired-update-property
1008 'tags
(image-dired-list-tags (image-dired-original-file-name))))
1011 (defun image-dired-delete-tag (arg)
1012 "Remove tag for selected file(s).
1013 With prefix argument ARG, remove tag from file at point."
1015 (let ((tag (read-string "Tag to remove: "))
1018 (setq files
(list (dired-get-filename)))
1019 (setq files
(dired-get-marked-files)))
1020 (image-dired-remove-tag files tag
)))
1022 (defun image-dired-tag-thumbnail-remove ()
1023 "Remove tag from thumbnail."
1025 (let ((tag (read-string "Tag to remove: ")))
1026 (image-dired-remove-tag (image-dired-original-file-name) tag
))
1027 (image-dired-update-property
1028 'tags
(image-dired-list-tags (image-dired-original-file-name))))
1030 (defun image-dired-original-file-name ()
1031 "Get original file name for thumbnail or display image at point."
1032 (get-text-property (point) 'original-file-name
))
1034 (defun image-dired-associated-dired-buffer ()
1035 "Get associated dired buffer at point."
1036 (get-text-property (point) 'associated-dired-buffer
))
1038 (defun image-dired-get-buffer-window (buf)
1039 "Return window where buffer BUF is."
1040 (get-window-with-predicate
1042 (equal (window-buffer window
) buf
))
1045 (defun image-dired-track-original-file ()
1046 "Track the original file in the associated dired buffer.
1047 See documentation for `image-dired-toggle-movement-tracking'.
1048 Interactive use only useful if `image-dired-track-movement' is nil."
1050 (let* ((dired-buf (image-dired-associated-dired-buffer))
1051 (file-name (image-dired-original-file-name))
1052 (window (image-dired-get-buffer-window dired-buf
)))
1053 (and (buffer-live-p dired-buf
) file-name
1054 (with-current-buffer dired-buf
1055 (if (not (dired-goto-file file-name
))
1056 (message "Could not track file")
1057 (if window
(set-window-point window
(point))))))))
1059 (defun image-dired-toggle-movement-tracking ()
1060 "Turn on and off `image-dired-track-movement'.
1061 Tracking of the movements between thumbnail and dired buffer so that
1062 they are \"mirrored\" in the dired buffer. When this is on, moving
1063 around in the thumbnail or dired buffer will find the matching
1064 position in the other buffer."
1066 (setq image-dired-track-movement
(not image-dired-track-movement
))
1067 (message "Tracking %s" (if image-dired-track-movement
"on" "off")))
1069 (defun image-dired-track-thumbnail ()
1070 "Track current dired file's thumb in `image-dired-thumbnail-buffer'.
1071 This is almost the same as what `image-dired-track-original-file' does,
1072 but the other way around."
1073 (let ((file (dired-get-filename))
1074 prop-val found window
)
1075 (when (get-buffer image-dired-thumbnail-buffer
)
1076 (with-current-buffer image-dired-thumbnail-buffer
1077 (goto-char (point-min))
1078 (while (and (not (eobp))
1080 (if (and (setq prop-val
1081 (get-text-property (point) 'original-file-name
))
1082 (string= prop-val file
))
1087 (if (setq window
(image-dired-thumbnail-window))
1088 (set-window-point window
(point)))
1089 (image-dired-display-thumb-properties))))))
1091 (defun image-dired-dired-next-line (&optional arg
)
1092 "Call `dired-next-line', then track thumbnail.
1093 This can safely replace `dired-next-line'.
1094 With prefix argument, move ARG lines."
1096 (dired-next-line (or arg
1))
1097 (if image-dired-track-movement
1098 (image-dired-track-thumbnail)))
1100 (defun image-dired-dired-previous-line (&optional arg
)
1101 "Call `dired-previous-line', then track thumbnail.
1102 This can safely replace `dired-previous-line'.
1103 With prefix argument, move ARG lines."
1105 (dired-previous-line (or arg
1))
1106 (if image-dired-track-movement
1107 (image-dired-track-thumbnail)))
1109 (defun image-dired-forward-image (&optional arg
)
1110 "Move to next image and display properties.
1111 Optional prefix ARG says how many images to move; default is one
1114 (let (pos (steps (or arg
1)))
1116 (if (and (not (eobp))
1119 (while (and (not (eobp))
1120 (not (image-dired-image-at-point-p)))
1123 (image-dired-image-at-point-p)))
1125 (error "At last image"))))
1126 (when image-dired-track-movement
1127 (image-dired-track-original-file))
1128 (image-dired-display-thumb-properties))
1130 (defun image-dired-backward-image (&optional arg
)
1131 "Move to previous image and display properties.
1132 Optional prefix ARG says how many images to move; default is one
1135 (let (pos (steps (or arg
1)))
1137 (if (and (not (bobp))
1140 (while (and (not (bobp))
1141 (not (image-dired-image-at-point-p)))
1144 (image-dired-image-at-point-p)))
1146 (error "At first image"))))
1147 (when image-dired-track-movement
1148 (image-dired-track-original-file))
1149 (image-dired-display-thumb-properties))
1151 (defun image-dired-next-line ()
1152 "Move to next line and display properties."
1154 (let ((goal-column (current-column)))
1156 (move-to-column goal-column
))
1157 ;; If we end up in an empty spot, back up to the next thumbnail.
1158 (if (not (image-dired-image-at-point-p))
1159 (image-dired-backward-image))
1160 (if image-dired-track-movement
1161 (image-dired-track-original-file))
1162 (image-dired-display-thumb-properties))
1165 (defun image-dired-previous-line ()
1166 "Move to previous line and display properties."
1168 (let ((goal-column (current-column)))
1170 (move-to-column goal-column
))
1171 ;; If we end up in an empty spot, back up to the next
1172 ;; thumbnail. This should only happen if the user deleted a
1173 ;; thumbnail and did not refresh, so it is not very common. But we
1174 ;; can handle it in a good manner, so why not?
1175 (if (not (image-dired-image-at-point-p))
1176 (image-dired-backward-image))
1177 (if image-dired-track-movement
1178 (image-dired-track-original-file))
1179 (image-dired-display-thumb-properties))
1181 (defun image-dired-format-properties-string (buf file props comment
)
1182 "Format display properties.
1183 BUF is the associated dired buffer, FILE is the original image file
1184 name, PROPS is a list of tags and COMMENT is the image file's
1187 image-dired-display-properties-format
1189 (cons ?b
(or buf
""))
1191 (cons ?t
(or (princ props
) ""))
1192 (cons ?c
(or comment
"")))))
1194 (defun image-dired-display-thumb-properties ()
1195 "Display thumbnail properties in the echo area."
1197 (let ((file-name (file-name-nondirectory (image-dired-original-file-name)))
1198 (dired-buf (buffer-name (image-dired-associated-dired-buffer)))
1201 (get-text-property (point) 'tags
)
1203 (comment (get-text-property (point) 'comment
)))
1206 (image-dired-format-properties-string
1212 (defun image-dired-dired-file-marked-p ()
1213 "Check whether file on current line is marked or not."
1216 (not (looking-at "^ .*$"))))
1218 (defun image-dired-modify-mark-on-thumb-original-file (command)
1219 "Modify mark in dired buffer.
1220 COMMAND is one of `mark' for marking file in dired, `unmark' for
1221 unmarking file in dired or `flag' for flagging file for delete in
1223 (let ((file-name (image-dired-original-file-name))
1224 (dired-buf (image-dired-associated-dired-buffer)))
1225 (if (not (and dired-buf file-name
))
1226 (message "No image, or image with correct properties, at point.")
1227 (with-current-buffer dired-buf
1228 (message "%s" file-name
)
1229 (if (dired-goto-file file-name
)
1230 (cond ((eq command
'mark
) (dired-mark 1))
1231 ((eq command
'unmark
) (dired-unmark 1))
1232 ((eq command
'toggle
)
1233 (if (image-dired-dired-file-marked-p)
1236 ((eq command
'flag
) (dired-flag-file-deletion 1))))))))
1238 (defun image-dired-mark-thumb-original-file ()
1239 "Mark original image file in associated dired buffer."
1241 (image-dired-modify-mark-on-thumb-original-file 'mark
)
1242 (image-dired-forward-image))
1244 (defun image-dired-unmark-thumb-original-file ()
1245 "Unmark original image file in associated dired buffer."
1247 (image-dired-modify-mark-on-thumb-original-file 'unmark
)
1248 (image-dired-forward-image))
1250 (defun image-dired-flag-thumb-original-file ()
1251 "Flag original image file for deletion in associated dired buffer."
1253 (image-dired-modify-mark-on-thumb-original-file 'flag
)
1254 (image-dired-forward-image))
1256 (defun image-dired-toggle-mark-thumb-original-file ()
1257 "Toggle mark on original image file in associated dired buffer."
1259 (image-dired-modify-mark-on-thumb-original-file 'toggle
))
1261 (defun image-dired-jump-original-dired-buffer ()
1262 "Jump to the dired buffer associated with the current image file.
1263 You probably want to use this together with
1264 `image-dired-track-original-file'."
1266 (let ((buf (image-dired-associated-dired-buffer))
1268 (setq window
(image-dired-get-buffer-window buf
))
1271 (if (not (equal (selected-frame) (setq frame
(window-frame window
))))
1272 (select-frame-set-input-focus frame
))
1273 (select-window window
))
1274 (message "Associated dired buffer not visible"))))
1277 (defun image-dired-jump-thumbnail-buffer ()
1278 "Jump to thumbnail buffer."
1280 (let ((window (image-dired-thumbnail-window))
1284 (if (not (equal (selected-frame) (setq frame
(window-frame window
))))
1285 (select-frame-set-input-focus frame
))
1286 (select-window window
))
1287 (message "Thumbnail buffer not visible"))))
1289 (defvar image-dired-thumbnail-mode-map
(make-sparse-keymap)
1290 "Keymap for `image-dired-thumbnail-mode'.")
1292 (defvar image-dired-thumbnail-mode-line-up-map
(make-sparse-keymap)
1293 "Keymap for line-up commands in `image-dired-thumbnail-mode'.")
1295 (defvar image-dired-thumbnail-mode-tag-map
(make-sparse-keymap)
1296 "Keymap for tag commands in `image-dired-thumbnail-mode'.")
1298 (defun image-dired-define-thumbnail-mode-keymap ()
1299 "Define keymap for `image-dired-thumbnail-mode'."
1302 (define-key image-dired-thumbnail-mode-map
[right] 'image-dired-forward-image)
1303 (define-key image-dired-thumbnail-mode-map [left] 'image-dired-backward-image)
1304 (define-key image-dired-thumbnail-mode-map [up] 'image-dired-previous-line)
1305 (define-key image-dired-thumbnail-mode-map [down] 'image-dired-next-line)
1306 (define-key image-dired-thumbnail-mode-map "\C-f" 'image-dired-forward-image)
1307 (define-key image-dired-thumbnail-mode-map "\C-b" 'image-dired-backward-image)
1308 (define-key image-dired-thumbnail-mode-map "\C-p" 'image-dired-previous-line)
1309 (define-key image-dired-thumbnail-mode-map "\C-n" 'image-dired-next-line)
1311 (define-key image-dired-thumbnail-mode-map "d" 'image-dired-flag-thumb-original-file)
1312 (define-key image-dired-thumbnail-mode-map [delete]
1313 'image-dired-flag-thumb-original-file)
1314 (define-key image-dired-thumbnail-mode-map "m" 'image-dired-mark-thumb-original-file)
1315 (define-key image-dired-thumbnail-mode-map "u" 'image-dired-unmark-thumb-original-file)
1316 (define-key image-dired-thumbnail-mode-map "." 'image-dired-track-original-file)
1317 (define-key image-dired-thumbnail-mode-map [tab] 'image-dired-jump-original-dired-buffer)
1320 (define-key image-dired-thumbnail-mode-map "g" image-dired-thumbnail-mode-line-up-map)
1322 ;; map it to "g" so that the user can press it more quickly
1323 (define-key image-dired-thumbnail-mode-line-up-map "g" 'image-dired-line-up-dynamic)
1324 ;; "f" for "fixed" number of thumbs per row
1325 (define-key image-dired-thumbnail-mode-line-up-map "f" 'image-dired-line-up)
1326 ;; "i" for "interactive"
1327 (define-key image-dired-thumbnail-mode-line-up-map "i" 'image-dired-line-up-interactive)
1330 (define-key image-dired-thumbnail-mode-map "t" image-dired-thumbnail-mode-tag-map)
1332 ;; map it to "t" so that the user can press it more quickly
1333 (define-key image-dired-thumbnail-mode-tag-map "t" 'image-dired-tag-thumbnail)
1335 (define-key image-dired-thumbnail-mode-tag-map "r" 'image-dired-tag-thumbnail-remove)
1337 (define-key image-dired-thumbnail-mode-map "\C-m"
1338 'image-dired-display-thumbnail-original-image)
1339 (define-key image-dired-thumbnail-mode-map [C-return]
1340 'image-dired-thumbnail-display-external)
1342 (define-key image-dired-thumbnail-mode-map "l" 'image-dired-rotate-thumbnail-left)
1343 (define-key image-dired-thumbnail-mode-map "r" 'image-dired-rotate-thumbnail-right)
1345 (define-key image-dired-thumbnail-mode-map "L" 'image-dired-rotate-original-left)
1346 (define-key image-dired-thumbnail-mode-map "R" 'image-dired-rotate-original-right)
1348 (define-key image-dired-thumbnail-mode-map "D"
1349 'image-dired-thumbnail-set-image-description)
1351 (define-key image-dired-thumbnail-mode-map "\C-d" 'image-dired-delete-char)
1352 (define-key image-dired-thumbnail-mode-map " "
1353 'image-dired-display-next-thumbnail-original)
1354 (define-key image-dired-thumbnail-mode-map
1355 (kbd "DEL") 'image-dired-display-previous-thumbnail-original)
1356 (define-key image-dired-thumbnail-mode-map "c" 'image-dired-comment-thumbnail)
1357 (define-key image-dired-thumbnail-mode-map "q" 'image-dired-kill-buffer-and-window)
1360 (define-key image-dired-thumbnail-mode-map [mouse-2] 'image-dired-mouse-display-image)
1361 (define-key image-dired-thumbnail-mode-map [mouse-1] 'image-dired-mouse-select-thumbnail)
1363 ;; Seems I must first set C-down-mouse-1 to undefined, or else it
1364 ;; will trigger the buffer menu. If I try to instead bind
1365 ;; C-down-mouse-1 to `image-dired-mouse-toggle-mark', I get a message
1366 ;; about C-mouse-1 not being defined afterwards. Annoying, but I
1367 ;; probably do not completely understand mouse events.
1369 (define-key image-dired-thumbnail-mode-map [C-down-mouse-1] 'undefined)
1370 (define-key image-dired-thumbnail-mode-map [C-mouse-1] 'image-dired-mouse-toggle-mark)
1373 (define-key image-dired-thumbnail-mode-map [menu-bar image-dired]
1374 (cons "Image-Dired" (make-sparse-keymap "Image-Dired")))
1376 (define-key image-dired-thumbnail-mode-map
1377 [menu-bar image-dired image-dired-kill-buffer-and-window]
1378 '("Quit" . image-dired-kill-buffer-and-window))
1380 (define-key image-dired-thumbnail-mode-map
1381 [menu-bar image-dired image-dired-delete-char]
1382 '("Delete thumbnail from buffer" . image-dired-delete-char))
1384 (define-key image-dired-thumbnail-mode-map
1385 [menu-bar image-dired image-dired-tag-thumbnail-remove]
1386 '("Remove tag from thumbnail" . image-dired-tag-thumbnail-remove))
1388 (define-key image-dired-thumbnail-mode-map
1389 [menu-bar image-dired image-dired-tag-thumbnail]
1390 '("Tag thumbnail" . image-dired-tag-thumbnail))
1392 (define-key image-dired-thumbnail-mode-map
1393 [menu-bar image-dired image-dired-comment-thumbnail]
1394 '("Comment thumbnail" . image-dired-comment-thumbnail))
1396 (define-key image-dired-thumbnail-mode-map
1397 [menu-bar image-dired image-dired-refresh-thumb]
1398 '("Refresh thumb" . image-dired-refresh-thumb))
1399 (define-key image-dired-thumbnail-mode-map
1400 [menu-bar image-dired image-dired-line-up-dynamic]
1401 '("Dynamic line up" . image-dired-line-up-dynamic))
1402 (define-key image-dired-thumbnail-mode-map
1403 [menu-bar image-dired image-dired-line-up]
1404 '("Line up thumbnails" . image-dired-line-up))
1406 (define-key image-dired-thumbnail-mode-map
1407 [menu-bar image-dired image-dired-rotate-thumbnail-left]
1408 '("Rotate thumbnail left" . image-dired-rotate-thumbnail-left))
1409 (define-key image-dired-thumbnail-mode-map
1410 [menu-bar image-dired image-dired-rotate-thumbnail-right]
1411 '("Rotate thumbnail right" . image-dired-rotate-thumbnail-right))
1413 (define-key image-dired-thumbnail-mode-map
1414 [menu-bar image-dired image-dired-rotate-original-left]
1415 '("Rotate original left" . image-dired-rotate-original-left))
1416 (define-key image-dired-thumbnail-mode-map
1417 [menu-bar image-dired image-dired-rotate-original-right]
1418 '("Rotate original right" . image-dired-rotate-original-right))
1420 (define-key image-dired-thumbnail-mode-map
1421 [menu-bar image-dired image-dired-toggle-movement-tracking]
1422 '("Toggle movement tracking on/off" . image-dired-toggle-movement-tracking))
1424 (define-key image-dired-thumbnail-mode-map
1425 [menu-bar image-dired image-dired-jump-original-dired-buffer]
1426 '("Jump to dired buffer" . image-dired-jump-original-dired-buffer))
1427 (define-key image-dired-thumbnail-mode-map
1428 [menu-bar image-dired image-dired-track-original-file]
1429 '("Track original" . image-dired-track-original-file))
1431 (define-key image-dired-thumbnail-mode-map
1432 [menu-bar image-dired image-dired-flag-thumb-original-file]
1433 '("Flag original for deletion" . image-dired-flag-thumb-original-file))
1434 (define-key image-dired-thumbnail-mode-map
1435 [menu-bar image-dired image-dired-unmark-thumb-original-file]
1436 '("Unmark original" . image-dired-unmark-thumb-original-file))
1437 (define-key image-dired-thumbnail-mode-map
1438 [menu-bar image-dired image-dired-mark-thumb-original-file]
1439 '("Mark original" . image-dired-mark-thumb-original-file))
1441 (define-key image-dired-thumbnail-mode-map
1442 [menu-bar image-dired image-dired-thumbnail-display-external]
1443 '("Display in external viewer" . image-dired-thumbnail-display-external))
1444 (define-key image-dired-thumbnail-mode-map
1445 [menu-bar image-dired image-dired-display-thumbnail-original-image]
1446 '("Display image" . image-dired-display-thumbnail-original-image)))
1448 (defvar image-dired-display-image-mode-map (make-sparse-keymap)
1449 "Keymap for `image-dired-display-image-mode'.")
1451 (defun image-dired-define-display-image-mode-keymap ()
1452 "Define keymap for `image-dired-display-image-mode'."
1455 (define-key image-dired-display-image-mode-map "q" 'image-dired-kill-buffer-and-window)
1457 (define-key image-dired-display-image-mode-map "f"
1458 'image-dired-display-current-image-full)
1460 (define-key image-dired-display-image-mode-map "s"
1461 'image-dired-display-current-image-sized)
1464 (define-key image-dired-display-image-mode-map [menu-bar image-dired]
1465 (cons "Image-Dired" (make-sparse-keymap "Image-Dired")))
1467 (define-key image-dired-display-image-mode-map
1468 [menu-bar image-dired image-dired-kill-buffer-and-window]
1469 '("Quit" . image-dired-kill-buffer-and-window))
1471 (define-key image-dired-display-image-mode-map
1472 [menu-bar image-dired image-dired-display-current-image-sized]
1473 '("Display original, sized to fit" . image-dired-display-current-image-sized))
1475 (define-key image-dired-display-image-mode-map
1476 [menu-bar image-dired image-dired-display-current-image-full]
1477 '("Display original, full size" . image-dired-display-current-image-full))
1481 (defun image-dired-display-current-image-full ()
1482 "Display current image in full size."
1484 (let ((file (image-dired-original-file-name)))
1487 (image-dired-display-image file t)
1488 (message "Full size image displayed"))
1489 (error "No original file name at point"))))
1491 (defun image-dired-display-current-image-sized ()
1492 "Display current image in sized to fit window dimensions."
1494 (let ((file (image-dired-original-file-name)))
1497 (image-dired-display-image file)
1498 (message "Full size image displayed"))
1499 (error "No original file name at point"))))
1501 (define-derived-mode image-dired-thumbnail-mode
1502 fundamental-mode "image-dired-thumbnail"
1503 "Browse and manipulate thumbnail images using dired.
1504 Use `image-dired-dired' and `image-dired-setup-dired-keybindings' to get a
1505 nice setup to start with."
1506 (image-dired-define-thumbnail-mode-keymap)
1507 (message "image-dired-thumbnail-mode enabled"))
1509 (define-derived-mode image-dired-display-image-mode
1510 fundamental-mode "image-dired-image-display"
1511 "Mode for displaying and manipulating original image.
1512 Resized or in full-size."
1513 (image-dired-define-display-image-mode-keymap)
1514 (message "image-dired-display-image-mode enabled"))
1517 (defun image-dired-setup-dired-keybindings ()
1518 "Setup easy-to-use keybindings for the commands to be used in dired mode.
1519 Note that n, p and <down> and <up> will be hijacked and bound to
1520 `image-dired-dired-x-line'."
1523 ;; Hijack previous and next line movement. Let C-p and C-b be
1526 (define-key dired-mode-map "p" 'image-dired-dired-previous-line)
1527 (define-key dired-mode-map "n" 'image-dired-dired-next-line)
1528 (define-key dired-mode-map [up] 'image-dired-dired-previous-line)
1529 (define-key dired-mode-map [down] 'image-dired-dired-next-line)
1531 (define-key dired-mode-map (kbd "C-S-n") 'image-dired-next-line-and-display)
1532 (define-key dired-mode-map (kbd "C-S-p") 'image-dired-previous-line-and-display)
1533 (define-key dired-mode-map (kbd "C-S-m") 'image-dired-mark-and-display-next)
1535 (define-key dired-mode-map "\C-td" 'image-dired-display-thumbs)
1536 (define-key dired-mode-map "\C-tt" 'image-dired-tag-files)
1537 (define-key dired-mode-map "\C-tr" 'image-dired-delete-tag)
1538 (define-key dired-mode-map [tab] 'image-dired-jump-thumbnail-buffer)
1539 (define-key dired-mode-map "\C-ti" 'image-dired-dired-display-image)
1540 (define-key dired-mode-map "\C-tx" 'image-dired-dired-display-external)
1541 (define-key dired-mode-map "\C-ta" 'image-dired-display-thumbs-append)
1542 (define-key dired-mode-map "\C-t." 'image-dired-display-thumb)
1543 (define-key dired-mode-map "\C-tc" 'image-dired-dired-comment-files)
1544 (define-key dired-mode-map "\C-tf" 'image-dired-mark-tagged-files)
1547 (define-key dired-mode-map [menu-bar image-dired]
1548 (cons "Image-Dired" (make-sparse-keymap "Image-Dired")))
1550 (define-key dired-mode-map [menu-bar image-dired image-dired-copy-with-exif-file-name]
1551 '("Copy with EXIF file name" . image-dired-copy-with-exif-file-name))
1553 (define-key dired-mode-map [menu-bar image-dired image-dired-dired-comment-files]
1554 '("Comment files" . image-dired-dired-comment-files))
1556 (define-key dired-mode-map [menu-bar image-dired image-dired-mark-tagged-files]
1557 '("Mark tagged files" . image-dired-mark-tagged-files))
1559 (define-key dired-mode-map [menu-bar image-dired image-dired-delete-tag]
1560 '("Remove tag from files" . image-dired-delete-tag))
1562 (define-key dired-mode-map [menu-bar image-dired image-dired-tag-files]
1563 '("Tag files" . image-dired-tag-files))
1565 (define-key dired-mode-map [menu-bar image-dired image-dired-jump-thumbnail-buffer]
1566 '("Jump to thumbnail buffer" . image-dired-jump-thumbnail-buffer))
1568 (define-key dired-mode-map [menu-bar image-dired image-dired-toggle-movement-tracking]
1569 '("Toggle movement tracking" . image-dired-toggle-movement-tracking))
1571 (define-key dired-mode-map
1572 [menu-bar image-dired image-dired-toggle-append-browsing]
1573 '("Toggle append browsing" . image-dired-toggle-append-browsing))
1575 (define-key dired-mode-map
1576 [menu-bar image-dired image-dired-toggle-disp-props]
1577 '("Toggle display properties" . image-dired-toggle-dired-display-properties))
1579 (define-key dired-mode-map
1580 [menu-bar image-dired image-dired-dired-display-external]
1581 '("Display in external viewer" . image-dired-dired-display-external))
1582 (define-key dired-mode-map
1583 [menu-bar image-dired image-dired-dired-display-image]
1584 '("Display image" . image-dired-dired-display-image))
1585 (define-key dired-mode-map
1586 [menu-bar image-dired image-dired-display-thumb]
1587 '("Display this thumbnail" . image-dired-display-thumb))
1588 (define-key dired-mode-map
1589 [menu-bar image-dired image-dired-display-thumbs-append]
1590 '("Display thumbnails append" . image-dired-display-thumbs-append))
1591 (define-key dired-mode-map
1592 [menu-bar image-dired image-dired-display-thumbs]
1593 '("Display thumbnails" . image-dired-display-thumbs))
1595 (define-key dired-mode-map
1596 [menu-bar image-dired image-dired-create-thumbs]
1597 '("Create thumbnails for marked files" . image-dired-create-thumbs))
1599 (define-key dired-mode-map
1600 [menu-bar image-dired image-dired-mark-and-display-next]
1601 '("Mark and display next" . image-dired-mark-and-display-next))
1602 (define-key dired-mode-map
1603 [menu-bar image-dired image-dired-previous-line-and-display]
1604 '("Display thumb for previous file" . image-dired-previous-line-and-display))
1605 (define-key dired-mode-map
1606 [menu-bar image-dired image-dired-next-line-and-display]
1607 '("Display thumb for next file" . image-dired-next-line-and-display)))
1609 (declare-function clear-image-cache "image.c" (&optional filter))
1611 (defun image-dired-create-thumbs (&optional arg)
1612 "Create thumbnail images for all marked files in dired.
1613 With prefix argument ARG, create thumbnails even if they already exist
1614 \(i.e. use this to refresh your thumbnails)."
1616 (let (thumb-name files)
1617 (setq files (dired-get-marked-files))
1620 (setq thumb-name (image-dired-thumb-name curr-file))
1621 ;; If the user overrides the exist check, we must clear the
1622 ;; image cache so that if the user wants to display the
1623 ;; thumbnail, it is not fetched from cache.
1625 (clear-image-cache))
1626 (if (or (not (file-exists-p thumb-name))
1628 (if (not (= 0 (image-dired-create-thumb curr-file
1629 (image-dired-thumb-name curr-file))))
1630 (error "Thumb could not be created"))))
1633 (defvar image-dired-slideshow-timer nil
1636 (defvar image-dired-slideshow-count 0
1637 "Keeping track on number of images in slideshow.")
1639 (defvar image-dired-slideshow-times 0
1640 "Number of pictures to display in slideshow.")
1642 (defun image-dired-slideshow-step ()
1643 "Step to next file, if `image-dired-slideshow-times' has not been reached."
1644 (if (< image-dired-slideshow-count image-dired-slideshow-times)
1646 (message "%s" (1+ image-dired-slideshow-count))
1647 (setq image-dired-slideshow-count (1+ image-dired-slideshow-count))
1648 (image-dired-next-line-and-display))
1649 (image-dired-slideshow-stop)))
1651 (defun image-dired-slideshow-start ()
1653 Ask user for number of images to show and the delay in between."
1655 (setq image-dired-slideshow-count 0)
1656 (setq image-dired-slideshow-times (string-to-number (read-string "How many: ")))
1657 (let ((repeat (string-to-number
1659 "Delay, in seconds. Decimals are accepted : " "1"))))
1660 (setq image-dired-slideshow-timer
1663 'image-dired-slideshow-step))))
1665 (defun image-dired-slideshow-stop ()
1668 (cancel-timer image-dired-slideshow-timer))
1670 (defun image-dired-delete-char ()
1671 "Remove current thumbnail from thumbnail buffer and line up."
1673 (let ((inhibit-read-only t))
1675 (if (looking-at " ")
1679 (defun image-dired-display-thumbs-append ()
1680 "Append thumbnails to `image-dired-thumbnail-buffer'."
1682 (image-dired-display-thumbs nil t t))
1685 (defun image-dired-display-thumb ()
1686 "Shorthand for `image-dired-display-thumbs' with prefix argument."
1688 (image-dired-display-thumbs t nil t))
1690 (defun image-dired-line-up ()
1691 "Line up thumbnails according to `image-dired-thumbs-per-row'.
1692 See also `image-dired-line-up-dynamic'."
1694 (let ((inhibit-read-only t))
1695 (goto-char (point-min))
1696 (while (and (not (image-dired-image-at-point-p))
1701 (while (and (not (image-dired-image-at-point-p))
1704 (goto-char (point-min))
1708 (if (= image-dired-thumbs-per-row 1)
1711 (setq count (1+ count))
1712 (when (and (= count (- image-dired-thumbs-per-row 1))
1717 (goto-char (point-min))))
1719 (defun image-dired-line-up-dynamic ()
1720 "Line up thumbnails images dynamically.
1721 Calculate how many thumbnails fit."
1723 (let* ((char-width (frame-char-width))
1724 (width (image-dired-window-width-pixels (image-dired-thumbnail-window)))
1725 (image-dired-thumbs-per-row
1727 (+ (* 2 image-dired-thumb-relief)
1728 (* 2 image-dired-thumb-margin)
1729 image-dired-thumb-width char-width))))
1730 (image-dired-line-up)))
1732 (defun image-dired-line-up-interactive ()
1733 "Line up thumbnails interactively.
1734 Ask user how many thumbnails should be displayed per row."
1736 (let ((image-dired-thumbs-per-row
1737 (string-to-number (read-string "How many thumbs per row: "))))
1738 (if (not (> image-dired-thumbs-per-row 0))
1739 (message "Number must be greater than 0")
1740 (image-dired-line-up))))
1742 (defun image-dired-thumbnail-display-external ()
1743 "Display original image for thumbnail at point using external viewer."
1745 (let ((file (image-dired-original-file-name)))
1746 (if (not (image-dired-image-at-point-p))
1747 (message "No thumbnail at point")
1749 (message "No original file name found")
1750 (call-process shell-file-name nil nil nil shell-command-switch
1751 (format "%s \"%s\"" image-dired-external-viewer file))))))
1754 (defun image-dired-dired-display-external ()
1755 "Display file at point using an external viewer."
1757 (let ((file (dired-get-filename)))
1758 (call-process shell-file-name nil nil nil shell-command-switch
1759 (format "%s \"%s\"" image-dired-external-viewer file))))
1761 (defun image-dired-window-width-pixels (window)
1762 "Calculate WINDOW width in pixels."
1763 (* (window-width window) (frame-char-width)))
1765 (defun image-dired-window-height-pixels (window)
1766 "Calculate WINDOW height in pixels."
1767 ;; Note: The mode-line consumes one line
1768 (* (- (window-height window) 1) (frame-char-height)))
1770 (defun image-dired-display-window ()
1771 "Return window where `image-dired-display-image-buffer' is visible."
1772 (get-window-with-predicate
1774 (equal (buffer-name (window-buffer window)) image-dired-display-image-buffer))
1777 (defun image-dired-thumbnail-window ()
1778 "Return window where `image-dired-thumbnail-buffer' is visible."
1779 (get-window-with-predicate
1781 (equal (buffer-name (window-buffer window)) image-dired-thumbnail-buffer))
1784 (defun image-dired-associated-dired-buffer-window ()
1785 "Return window where associated dired buffer is visible."
1787 (if (image-dired-image-at-point-p)
1789 (setq buf (image-dired-associated-dired-buffer))
1790 (get-window-with-predicate
1792 (equal (window-buffer window) buf))))
1793 (error "No thumbnail image at point"))))
1795 (defun image-dired-display-window-width ()
1796 "Return width, in pixels, of image-dired's image display window."
1797 (- (image-dired-window-width-pixels (image-dired-display-window))
1798 image-dired-display-window-width-correction))
1800 (defun image-dired-display-window-height ()
1801 "Return height, in pixels, of image-dired's image display window."
1802 (- (image-dired-window-height-pixels (image-dired-display-window))
1803 image-dired-display-window-height-correction))
1805 (defun image-dired-display-image (file &optional original-size)
1806 "Display image FILE in image buffer.
1807 Use this when you want to display the image, semi sized, in a new
1808 window. The image is sized to fit the display window (using a
1809 temporary file, don't worry). Because of this, it will not be as
1810 quick as opening it directly, but on most modern systems it
1811 should feel snappy enough.
1813 If optional argument ORIGINAL-SIZE is non-nil, display image in its
1815 (let ((new-file (expand-file-name image-dired-temp-image-file))
1816 width height command ret
1818 (setq file (expand-file-name file))
1819 (if (not original-size)
1821 (setq width (image-dired-display-window-width))
1822 (setq height (image-dired-display-window-height))
1823 (unless image-dired-cmd-create-temp-image-program
1824 (error "image-dired-cmd-create-temp-image-program is nil"))
1827 image-dired-cmd-create-temp-image-options
1829 (cons ?p image-dired-cmd-create-temp-image-program)
1833 (cons ?t new-file))))
1834 (setq ret (call-process shell-file-name nil nil nil
1835 shell-command-switch command))
1837 (error "Could not resize image")))
1838 (setq image-type (image-type-from-file-name file))
1839 (copy-file file new-file t))
1840 (with-current-buffer (image-dired-create-display-image-buffer)
1841 (let ((inhibit-read-only t))
1844 (image-dired-insert-image image-dired-temp-image-file image-type 0 0)
1845 (goto-char (point-min))
1846 (image-dired-update-property 'original-file-name file)))))
1848 (defun image-dired-display-thumbnail-original-image (&optional arg)
1849 "Display current thumbnail's original image in display buffer.
1850 See documentation for `image-dired-display-image' for more information.
1851 With prefix argument ARG, display image in its original size."
1853 (let ((file (image-dired-original-file-name)))
1854 (if (not (string-equal major-mode "image-dired-thumbnail-mode"))
1855 (message "Not in image-dired-thumbnail-mode")
1856 (if (not (image-dired-image-at-point-p))
1857 (message "No thumbnail at point")
1859 (message "No original file name found")
1860 (image-dired-create-display-image-buffer)
1861 (display-buffer image-dired-display-image-buffer)
1862 (image-dired-display-image file arg))))))
1866 (defun image-dired-dired-display-image (&optional arg)
1867 "Display current image file.
1868 See documentation for `image-dired-display-image' for more information.
1869 With prefix argument ARG, display image in its original size."
1871 (image-dired-create-display-image-buffer)
1872 (display-buffer image-dired-display-image-buffer)
1873 (image-dired-display-image (dired-get-filename) arg))
1875 (defun image-dired-image-at-point-p ()
1876 "Return true if there is an image-dired thumbnail at point."
1877 (get-text-property (point) 'image-dired-thumbnail))
1879 (defun image-dired-rotate-thumbnail (degrees)
1880 "Rotate thumbnail DEGREES degrees."
1881 (unless image-dired-cmd-rotate-thumbnail-program
1882 (error "image-dired-cmd-rotate-thumbnail-program is nil"))
1883 (if (not (image-dired-image-at-point-p))
1884 (message "No thumbnail at point")
1885 (let ((file (image-dired-thumb-name (image-dired-original-file-name)))
1887 (setq command (format-spec
1888 image-dired-cmd-rotate-thumbnail-options
1890 (cons ?p image-dired-cmd-rotate-thumbnail-program)
1892 (cons ?t (expand-file-name file)))))
1893 (call-process shell-file-name nil nil nil shell-command-switch command)
1894 ;; Clear the cache to refresh image. I wish I could just refresh
1895 ;; the current file but I do not know how to do that. Yet...
1896 (clear-image-cache))))
1898 (defun image-dired-rotate-thumbnail-left ()
1899 "Rotate thumbnail left (counter clockwise) 90 degrees.
1900 The result of the rotation is displayed in the image display area
1901 and a confirmation is needed before the original image files is
1902 overwritten. This confirmation can be turned off using
1903 `image-dired-rotate-original-ask-before-overwrite'."
1905 (image-dired-rotate-thumbnail "270"))
1907 (defun image-dired-rotate-thumbnail-right ()
1908 "Rotate thumbnail counter right (clockwise) 90 degrees.
1909 The result of the rotation is displayed in the image display area
1910 and a confirmation is needed before the original image files is
1911 overwritten. This confirmation can be turned off using
1912 `image-dired-rotate-original-ask-before-overwrite'."
1914 (image-dired-rotate-thumbnail "90"))
1916 (defun image-dired-refresh-thumb ()
1917 "Force creation of new image for current thumbnail."
1919 (let ((file (image-dired-original-file-name)))
1921 (image-dired-create-thumb file (image-dired-thumb-name file))))
1923 (defun image-dired-rotate-original (degrees)
1924 "Rotate original image DEGREES degrees."
1925 (unless (image-dired-image-at-point-p)
1926 (message "No image at point"))
1927 (unless image-dired-cmd-rotate-original-program
1928 (error "image-dired-cmd-rotate-original-program is nil"))
1929 (let ((file (image-dired-original-file-name))
1931 (unless (eq 'jpeg (image-type file))
1932 (error "Only JPEG images can be rotated!"))
1933 (setq command (format-spec
1934 image-dired-cmd-rotate-original-options
1936 (cons ?p image-dired-cmd-rotate-original-program)
1938 (cons ?o (expand-file-name file))
1939 (cons ?t image-dired-temp-rotate-image-file))))
1940 (if (not (= 0 (call-process shell-file-name nil nil nil
1941 shell-command-switch command)))
1942 (error "Could not rotate image")
1943 (image-dired-display-image image-dired-temp-rotate-image-file)
1944 (if (or (and image-dired-rotate-original-ask-before-overwrite
1946 "Rotate to temp file OK. Overwrite original image? "))
1947 (not image-dired-rotate-original-ask-before-overwrite))
1949 (copy-file image-dired-temp-rotate-image-file file t)
1950 (image-dired-refresh-thumb))
1951 (image-dired-display-image file)))))
1953 (defun image-dired-rotate-original-left ()
1954 "Rotate original image left (counter clockwise) 90 degrees."
1956 (image-dired-rotate-original "270"))
1958 (defun image-dired-rotate-original-right ()
1959 "Rotate original image right (clockwise) 90 degrees."
1961 (image-dired-rotate-original "90"))
1963 (defun image-dired-get-exif-file-name (file)
1964 "Use the image's EXIF information to return a unique file name.
1965 The file name should be unique as long as you do not take more than
1966 one picture per second. The original file name is suffixed at the end
1967 for traceability. The format of the returned file name is
1968 YYYY_MM_DD_HH_MM_DD_ORIG_FILE_NAME.jpg. Used from
1969 `image-dired-copy-with-exif-file-name'."
1970 (let (data no-exif-data-found)
1971 (if (not (eq 'jpeg (image-type (expand-file-name file))))
1972 (setq no-exif-data-found t
1973 data (format-time-string
1975 (file-attribute-modification-time
1976 (file-attributes (expand-file-name file)))))
1977 (setq data (image-dired-get-exif-data (expand-file-name file)
1978 "DateTimeOriginal")))
1979 (while (string-match "[ :]" data)
1980 (setq data (replace-match "_" nil nil data)))
1981 (format "%s%s%s" data
1982 (if no-exif-data-found
1985 (file-name-nondirectory file))))
1987 (defun image-dired-thumbnail-set-image-description ()
1988 "Set the ImageDescription EXIF tag for the original image.
1989 If the image already has a value for this tag, it is used as the
1990 default value at the prompt."
1992 (if (not (image-dired-image-at-point-p))
1993 (message "No thumbnail at point")
1994 (let* ((file (image-dired-original-file-name))
1995 (old-value (image-dired-get-exif-data file "ImageDescription")))
1997 (image-dired-set-exif-data file "ImageDescription"
1998 (read-string "Value of ImageDescription: "
2000 (message "Successfully wrote ImageDescription tag.")
2001 (error "Could not write ImageDescription tag")))))
2003 (defun image-dired-set-exif-data (file tag-name tag-value)
2004 "In FILE, set EXIF tag TAG-NAME to value TAG-VALUE."
2005 (unless image-dired-cmd-write-exif-data-program
2006 (error "image-dired-cmd-write-exif-data-program is nil"))
2008 (setq command (format-spec
2009 image-dired-cmd-write-exif-data-options
2011 (cons ?p image-dired-cmd-write-exif-data-program)
2012 (cons ?f (expand-file-name file))
2014 (cons ?v tag-value))))
2015 (call-process shell-file-name nil nil nil shell-command-switch command)))
2017 (defun image-dired-get-exif-data (file tag-name)
2018 "From FILE, return EXIF tag TAG-NAME."
2019 (unless image-dired-cmd-read-exif-data-program
2020 (error "image-dired-cmd-read-exif-data-program is nil"))
2021 (let ((buf (get-buffer-create "*image-dired-get-exif-data*"))
2023 (setq command (format-spec
2024 image-dired-cmd-read-exif-data-options
2026 (cons ?p image-dired-cmd-read-exif-data-program)
2028 (cons ?t tag-name))))
2029 (with-current-buffer buf
2030 (delete-region (point-min) (point-max))
2031 (if (not (eq (call-process shell-file-name nil t nil
2032 shell-command-switch command) 0))
2033 (error "Could not get EXIF tag")
2034 (goto-char (point-min))
2035 ;; Clean buffer from newlines and carriage returns before
2036 ;; getting final info
2037 (while (search-forward-regexp "[\n\r]" nil t)
2038 (replace-match "" nil t))
2039 (setq tag-value (buffer-substring (point-min) (point-max)))))
2042 (defun image-dired-copy-with-exif-file-name ()
2043 "Copy file with unique name to main image directory.
2044 Copy current or all marked files in dired to a new file in your
2045 main image directory, using a file name generated by
2046 `image-dired-get-exif-file-name'. A typical usage for this if when
2047 copying images from a digital camera into the image directory.
2049 Typically, you would open up the folder with the incoming
2050 digital images, mark the files to be copied, and execute this
2051 function. The result is a couple of new files in
2052 `image-dired-main-image-directory' called
2053 2005_05_08_12_52_00_dscn0319.jpg,
2054 2005_05_08_14_27_45_dscn0320.jpg etc."
2057 (files (dired-get-marked-files)))
2062 (file-name-as-directory
2063 (expand-file-name image-dired-main-image-directory))
2064 (image-dired-get-exif-file-name curr-file)))
2065 (message "Copying %s to %s" curr-file new-name)
2066 (copy-file curr-file new-name))
2069 (defun image-dired-display-next-thumbnail-original ()
2070 "In thumbnail buffer, move to next thumbnail and display the image."
2072 (image-dired-forward-image)
2073 (image-dired-display-thumbnail-original-image))
2075 (defun image-dired-display-previous-thumbnail-original ()
2076 "Move to previous thumbnail and display image."
2078 (image-dired-backward-image)
2079 (image-dired-display-thumbnail-original-image))
2081 (defun image-dired-write-comments (file-comments)
2082 "Write file comments to database.
2083 Write file comments to one or more files.
2084 FILE-COMMENTS is an alist on the following form:
2085 ((FILE . COMMENT) ... )"
2086 (image-dired-sane-db-file)
2087 (let (end comment-beg-pos comment-end-pos file comment)
2088 (image-dired--with-db-file
2089 (setq buffer-file-name image-dired-db-file)
2090 (dolist (elt file-comments)
2091 (setq file (car elt)
2093 (goto-char (point-min))
2094 (if (search-forward-regexp (format "^%s.*$" file) nil t)
2098 ;; Delete old comment, if any
2099 (when (search-forward ";comment:" end t)
2100 (setq comment-beg-pos (match-beginning 0))
2101 ;; Any tags after the comment?
2102 (if (search-forward ";" end t)
2103 (setq comment-end-pos (- (point) 1))
2104 (setq comment-end-pos end))
2105 ;; Delete comment tag and comment
2106 (delete-region comment-beg-pos comment-end-pos))
2107 ;; Insert new comment
2109 (unless (search-forward ";" end t)
2112 (insert (format "comment:%s;" comment)))
2113 ;; File does not exist in database - add it.
2114 (goto-char (point-max))
2115 (insert (format "\n%s;comment:%s" file comment))))
2118 (defun image-dired-update-property (prop value)
2119 "Update text property PROP with value VALUE at point."
2120 (let ((inhibit-read-only t))
2122 (point) (1+ (point))
2127 (defun image-dired-dired-comment-files ()
2128 "Add comment to current or marked files in dired."
2130 (let ((comment (image-dired-read-comment)))
2131 (image-dired-write-comments
2134 (cons curr-file comment))
2135 (dired-get-marked-files)))))
2137 (defun image-dired-comment-thumbnail ()
2138 "Add comment to current thumbnail in thumbnail buffer."
2140 (let* ((file (image-dired-original-file-name))
2141 (comment (image-dired-read-comment file)))
2142 (image-dired-write-comments (list (cons file comment)))
2143 (image-dired-update-property 'comment comment))
2144 (image-dired-display-thumb-properties))
2146 (defun image-dired-read-comment (&optional file)
2147 "Read comment for an image.
2148 Optionally use old comment from FILE as initial value."
2152 (if file (image-dired-get-comment file)))))
2155 (defun image-dired-get-comment (file)
2156 "Get comment for file FILE."
2157 (image-dired-sane-db-file)
2158 (image-dired--with-db-file
2159 (let (end comment-beg-pos comment-end-pos comment)
2160 (when (search-forward-regexp (format "^%s" file) nil t)
2164 (when (search-forward ";comment:" end t)
2165 (setq comment-beg-pos (point))
2166 (if (search-forward ";" end t)
2167 (setq comment-end-pos (- (point) 1))
2168 (setq comment-end-pos end))
2169 (setq comment (buffer-substring
2170 comment-beg-pos comment-end-pos))))
2174 (defun image-dired-mark-tagged-files ()
2175 "Use regexp to mark files with matching tag.
2176 A `tag' is a keyword, a piece of meta data, associated with an
2177 image file and stored in image-dired's database file. This command
2178 lets you input a regexp and this will be matched against all tags
2179 on all image files in the database file. The files that have a
2180 matching tag will be marked in the dired buffer."
2182 (image-dired-sane-db-file)
2183 (let ((tag (read-string "Mark tagged files (regexp): "))
2186 (image-dired--with-db-file
2188 (while (search-forward-regexp
2189 (concat "\\(^[^;\n]+\\);.*" tag ".*$") nil t)
2190 (push (match-string 1) files)))
2192 (dolist (curr-file files)
2193 ;; I tried using `dired-mark-files-regexp' but it was waaaay to
2194 ;; slow. Don't bother about hits found in other directories
2195 ;; than the current one.
2196 (when (string= (file-name-as-directory
2197 (expand-file-name default-directory))
2198 (file-name-as-directory
2199 (file-name-directory curr-file)))
2200 (setq curr-file (file-name-nondirectory curr-file))
2201 (goto-char (point-min))
2202 (when (search-forward-regexp (format "\\s %s$" curr-file) nil t)
2203 (setq hits (+ hits 1))
2205 (message "%d files with matching tag marked." hits)))
2207 (defun image-dired-mouse-display-image (event)
2208 "Use mouse EVENT, call `image-dired-display-image' to display image.
2209 Track this in associated dired buffer if `image-dired-track-movement' is
2212 (mouse-set-point event)
2213 (goto-char (posn-point (event-end event)))
2214 (let ((file (image-dired-original-file-name)))
2216 (if image-dired-track-movement
2217 (image-dired-track-original-file))
2218 (image-dired-create-display-image-buffer)
2219 (display-buffer image-dired-display-image-buffer)
2220 (image-dired-display-image file))))
2222 (defun image-dired-mouse-select-thumbnail (event)
2223 "Use mouse EVENT to select thumbnail image.
2224 Track this in associated dired buffer if `image-dired-track-movement' is
2227 (mouse-set-point event)
2228 (goto-char (posn-point (event-end event)))
2229 (if image-dired-track-movement
2230 (image-dired-track-original-file))
2231 (image-dired-display-thumb-properties))
2233 (defun image-dired-mouse-toggle-mark (event)
2234 "Use mouse EVENT to toggle dired mark for thumbnail.
2235 Track this in associated dired buffer if `image-dired-track-movement' is
2238 (mouse-set-point event)
2239 (goto-char (posn-point (event-end event)))
2240 (if image-dired-track-movement
2241 (image-dired-track-original-file))
2242 (image-dired-toggle-mark-thumb-original-file))
2244 (defun image-dired-dired-display-properties ()
2245 "Display properties for dired file in the echo area."
2247 (let* ((file (dired-get-filename))
2248 (file-name (file-name-nondirectory file))
2249 (dired-buf (buffer-name (current-buffer)))
2252 (image-dired-list-tags file)
2254 (comment (image-dired-get-comment file)))
2257 (image-dired-format-properties-string
2263 (defvar image-dired-tag-file-list nil
2264 "List to store tag-file structure.")
2266 (defvar image-dired-file-tag-list nil
2267 "List to store file-tag structure.")
2269 (defvar image-dired-file-comment-list nil
2270 "List to store file comments.")
2272 (defun image-dired-add-to-tag-file-list (tag file)
2273 "Add relation between TAG and FILE."
2275 (if image-dired-tag-file-list
2276 (if (setq curr (assoc tag image-dired-tag-file-list))
2277 (if (not (member file curr))
2278 (setcdr curr (cons file (cdr curr))))
2279 (setcdr image-dired-tag-file-list
2280 (cons (list tag file) (cdr image-dired-tag-file-list))))
2281 (setq image-dired-tag-file-list (list (list tag file))))))
2283 (defun image-dired-add-to-tag-file-lists (tag file)
2284 "Helper function used from `image-dired-create-gallery-lists'.
2286 Add TAG to FILE in one list and FILE to TAG in the other.
2288 Lisp structures look like the following:
2290 image-dired-file-tag-list:
2292 ((\"filename1\" \"tag1\" \"tag2\" \"tag3\" ...)
2293 (\"filename2\" \"tag1\" \"tag2\" \"tag3\" ...)
2296 image-dired-tag-file-list:
2298 ((\"tag1\" \"filename1\" \"filename2\" \"filename3\" ...)
2299 (\"tag2\" \"filename1\" \"filename2\" \"filename3\" ...)
2301 ;; Add tag to file list
2303 (if image-dired-file-tag-list
2304 (if (setq curr (assoc file image-dired-file-tag-list))
2305 (setcdr curr (cons tag (cdr curr)))
2306 (setcdr image-dired-file-tag-list
2307 (cons (list file tag) (cdr image-dired-file-tag-list))))
2308 (setq image-dired-file-tag-list (list (list file tag))))
2309 ;; Add file to tag list
2310 (if image-dired-tag-file-list
2311 (if (setq curr (assoc tag image-dired-tag-file-list))
2312 (if (not (member file curr))
2313 (setcdr curr (cons file (cdr curr))))
2314 (setcdr image-dired-tag-file-list
2315 (cons (list tag file) (cdr image-dired-tag-file-list))))
2316 (setq image-dired-tag-file-list (list (list tag file))))))
2318 (defun image-dired-add-to-file-comment-list (file comment)
2319 "Helper function used from `image-dired-create-gallery-lists'.
2321 For FILE, add COMMENT to list.
2323 Lisp structure looks like the following:
2325 image-dired-file-comment-list:
2327 ((\"filename1\" . \"comment1\")
2328 (\"filename2\" . \"comment2\")
2330 (if image-dired-file-comment-list
2331 (if (not (assoc file image-dired-file-comment-list))
2332 (setcdr image-dired-file-comment-list
2333 (cons (cons file comment)
2334 (cdr image-dired-file-comment-list))))
2335 (setq image-dired-file-comment-list (list (cons file comment)))))
2337 (defun image-dired-create-gallery-lists ()
2338 "Create temporary lists used by `image-dired-gallery-generate'."
2339 (image-dired-sane-db-file)
2340 (image-dired--with-db-file
2341 (let (end beg file row-tags)
2342 (setq image-dired-tag-file-list nil)
2343 (setq image-dired-file-tag-list nil)
2344 (setq image-dired-file-comment-list nil)
2345 (goto-char (point-min))
2346 (while (search-forward-regexp "^." nil t)
2351 (unless (search-forward ";" end nil)
2352 (error "Something is really wrong, check format of database"))
2353 (setq row-tags (split-string
2354 (buffer-substring beg end) ";"))
2355 (setq file (car row-tags))
2356 (dolist (x (cdr row-tags))
2357 (if (not (string-match "^comment:\\(.*\\)" x))
2358 (image-dired-add-to-tag-file-lists x file)
2359 (image-dired-add-to-file-comment-list file (match-string 1 x)))))))
2360 ;; Sort tag-file list
2361 (setq image-dired-tag-file-list
2362 (sort image-dired-tag-file-list
2364 (string< (car x) (car y))))))
2366 (defun image-dired-hidden-p (file)
2367 "Return t if image FILE has a \"hidden\" tag."
2371 (if (member tag image-dired-gallery-hidden-tags)
2373 (cdr (assoc file image-dired-file-tag-list)))
2376 (defun image-dired-gallery-generate ()
2377 "Generate gallery pages.
2378 First we create a couple of Lisp structures from the database to make
2379 it easier to generate, then HTML-files are created in
2380 `image-dired-gallery-dir'."
2382 (if (eq 'per-directory image-dired-thumbnail-storage)
2383 (error "Currently, gallery generation is not supported \
2384 when using per-directory thumbnail file storage"))
2385 (image-dired-create-gallery-lists)
2386 (let ((tags image-dired-tag-file-list)
2387 (index-file (format "%s/index.html" image-dired-gallery-dir))
2389 comment file-tags tag-link tag-link-list)
2390 ;; Make sure gallery root exist
2391 (if (file-exists-p image-dired-gallery-dir)
2392 (if (not (file-directory-p image-dired-gallery-dir))
2393 (error "Variable image-dired-gallery-dir is not a directory"))
2394 (make-directory image-dired-gallery-dir))
2396 (with-temp-file index-file
2397 (if (file-exists-p index-file)
2398 (insert-file-contents index-file))
2400 (insert " <body>\n")
2401 (insert " <h2>Image-Dired Gallery</h2>\n")
2402 (insert (format "<p>\n Gallery generated %s\n <p>\n"
2403 (current-time-string)))
2404 (insert " <h3>Tag index</h3>\n")
2406 ;; Pre-generate list of all tag links
2408 (setq tag (car curr))
2409 (when (not (member tag image-dired-gallery-hidden-tags))
2410 (setq tag-link (format "<a href=\"%d.html\">%s</a>" count tag))
2413 (append tag-link-list (list (cons tag tag-link))))
2414 (setq tag-link-list (list (cons tag tag-link))))
2415 (setq count (1+ count))))
2417 ;; Main loop where we generated thumbnail pages per tag
2419 (setq tag (car curr))
2420 ;; Don't display hidden tags
2421 (when (not (member tag image-dired-gallery-hidden-tags))
2422 ;; Insert link to tag page in index
2423 (insert (format " %s<br>\n" (cdr (assoc tag tag-link-list))))
2424 ;; Open per-tag file
2425 (setq tag-file (format "%s/%s.html" image-dired-gallery-dir count))
2426 (with-temp-file tag-file
2427 (if (file-exists-p tag-file)
2428 (insert-file-contents tag-file))
2431 (insert " <body>\n")
2432 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2433 (insert (format " <h2>Images with tag "%s"</h2>" tag))
2434 ;; Main loop for files per tag page
2435 (dolist (file (cdr curr))
2436 (unless (image-dired-hidden-p file)
2437 ;; Insert thumbnail with link to full image
2439 (format "<a href=\"%s/%s\"><img src=\"%s/%s\"%s></a>\n"
2440 image-dired-gallery-image-root-url
2441 (file-name-nondirectory file)
2442 image-dired-gallery-thumb-image-root-url
2443 (file-name-nondirectory (image-dired-thumb-name file)) file))
2444 ;; Insert comment, if any
2445 (if (setq comment (cdr (assoc file image-dired-file-comment-list)))
2446 (insert (format "<br>\n%s<br>\n" comment))
2448 ;; Insert links to other tags, if any
2450 (setq file-tags (assoc file image-dired-file-tag-list))) 2)
2452 (dolist (extra-tag file-tags)
2453 ;; Only insert if not file name or the main tag
2454 (if (and (not (equal extra-tag tag))
2455 (not (equal extra-tag file)))
2457 (format "%s " (cdr (assoc extra-tag tag-link-list))))))
2458 (insert "]<br>\n"))))
2459 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2460 (insert " </body>\n")
2461 (insert "</html>\n"))
2462 (setq count (1+ count))))
2463 (insert " </body>\n")
2464 (insert "</html>"))))
2466 (defun image-dired-kill-buffer-and-window ()
2467 "Kill the current buffer and, if possible, also the window."
2469 (let ((buffer (current-buffer)))
2471 (delete-window (selected-window))
2473 (kill-buffer buffer)))
2475 (defvar image-dired-widget-list nil
2476 "List to keep track of meta data in edit buffer.")
2478 (declare-function widget-forward "wid-edit" (arg))
2481 (defun image-dired-dired-edit-comment-and-tags ()
2482 "Edit comment and tags of current or marked image files.
2483 Edit comment and tags for all marked image files in an
2486 (setq image-dired-widget-list nil)
2488 (let ((files (dired-get-marked-files)))
2489 (switch-to-buffer "*Image-Dired Edit Meta Data*")
2490 (kill-all-local-variables)
2491 (make-local-variable 'widget-example-repeat)
2492 (let ((inhibit-read-only t))
2495 ;; Some help for the user.
2497 "\nEdit comments and tags for each image. Separate multiple tags
2498 with a comma. Move forward between fields using TAB or RET.
2499 Move to the previous field using backtab (S-TAB). Save by
2500 activating the Save button at the bottom of the form or cancel
2501 the operation by activating the Cancel button.\n\n")
2502 ;; Here comes all images and a comment and tag field for each
2504 (let (thumb-file img comment-widget tag-widget)
2506 (dolist (file files)
2508 (setq thumb-file (image-dired-thumb-name file)
2509 img (create-image thumb-file))
2512 (widget-insert "\n\nComment: ")
2513 (setq comment-widget
2514 (widget-create 'editable-field
2517 :value (or (image-dired-get-comment file) "")))
2518 (widget-insert "\nTags: ")
2520 (widget-create 'editable-field
2523 :value (or (mapconcat
2526 (image-dired-list-tags file)
2528 ;; Save information in all widgets so that we can use it when
2529 ;; the user saves the form.
2530 (setq image-dired-widget-list
2531 (append image-dired-widget-list
2532 (list (list file comment-widget tag-widget))))
2533 (widget-insert "\n\n")))
2535 ;; Footer with Save and Cancel button.
2536 (widget-insert "\n")
2537 (widget-create 'push-button
2539 (lambda (&rest _ignore)
2540 (image-dired-save-information-from-widgets)
2545 (widget-create 'push-button
2547 (lambda (&rest _ignore)
2549 (message "Operation canceled."))
2551 (widget-insert "\n")
2552 (use-local-map widget-keymap)
2554 ;; Jump to the first widget.
2555 (widget-forward 1)))
2557 (defun image-dired-save-information-from-widgets ()
2558 "Save information found in `image-dired-widget-list'.
2559 Use the information in `image-dired-widget-list' to save comments and
2560 tags to their respective image file. Internal function used by
2561 `image-dired-dired-edit-comment-and-tags'."
2562 (let (file comment tag-string tag-list lst)
2563 (image-dired-write-comments
2566 (setq file (car widget)
2567 comment (widget-value (cadr widget)))
2568 (cons file comment))
2569 image-dired-widget-list))
2570 (image-dired-write-tags
2571 (dolist (widget image-dired-widget-list lst)
2572 (setq file (car widget)
2573 tag-string (widget-value (car (cddr widget)))
2574 tag-list (split-string tag-string ","))
2575 (dolist (tag tag-list)
2576 (push (cons file tag) lst))))))
2578 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2579 ;;;;;;;;; TEST-SECTION ;;;;;;;;;;;
2580 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2582 ;; (defvar image-dired-dir-max-size 12300000)
2584 ;; (defun image-dired-test-clean-old-files ()
2585 ;; "Clean `image-dired-dir' from old thumbnail files.
2586 ;; \"Oldness\" measured using last access time. If the total size of all
2587 ;; thumbnail files in `image-dired-dir' is larger than 'image-dired-dir-max-size',
2588 ;; old files are deleted until the max size is reached."
2593 ;; (let ((fattribs (file-attributes f)))
2594 ;; ;; Get last access time and file size
2595 ;; `(,(nth 4 fattribs) ,(nth 7 fattribs) ,f)))
2596 ;; (directory-files (image-dired-dir) t ".+\\.thumb\\..+$"))
2597 ;; ;; Sort function. Compare time between two files.
2599 ;; (time-less-p (car l1) (car l2)))))
2600 ;; (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) files))))
2601 ;; (while (> dirsize image-dired-dir-max-size)
2603 ;; (format "Size of thumbnail directory: %d, delete old file %s? "
2604 ;; dirsize (cadr (cdar files))))
2605 ;; (delete-file (cadr (cdar files)))
2606 ;; (setq dirsize (- dirsize (car (cdar files))))
2607 ;; (setq files (cdr files)))))
2609 ;;;;;;;;;;;;;;;;;;;;;;,
2611 ;; (defun dired-speedbar-buttons (dired-buffer)
2612 ;; (when (and (boundp 'image-dired-use-speedbar)
2613 ;; image-dired-use-speedbar)
2614 ;; (let ((filename (with-current-buffer dired-buffer
2615 ;; (dired-get-filename))))
2616 ;; (when (and (not (string-equal filename (buffer-string)))
2617 ;; (string-match (image-file-name-regexp) filename))
2619 ;; (insert (propertize
2622 ;; (image-dired-get-thumbnail-image filename)))))))
2624 ;; (setq image-dired-use-speedbar t)
2626 (provide 'image-dired)
2628 ;;; image-dired.el ends here