1 ;;; image-dired.el --- use dired to browse and manipulate your images
3 ;; Copyright (C) 2005, 2006, 2007 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 2, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
31 ;; I needed a program to browse, organize and tag my pictures. I got
32 ;; tired of the old gallery program I used as it did not allow
33 ;; multi-file operations easily. Also, it put things out of my
34 ;; control. Image viewing programs I tested did not allow multi-file
35 ;; operations or did not do what I wanted it to.
37 ;; So, I got the idea to use the wonderful functionality of Emacs and
38 ;; `dired' to do it. It would allow me to do almost anything I wanted,
39 ;; which is basically just to browse all my pictures in an easy way,
40 ;; letting me manipulate and tag them in various ways. `dired' already
41 ;; provide all the file handling and navigation facilities; I only
42 ;; needed to add some functions to display the images.
44 ;; I briefly tried out thumbs.el, and although it seemed more
45 ;; powerful than this package, it did not work the way I wanted to. It
46 ;; was too slow to created thumbnails of all files in a directory (I
47 ;; currently keep all my 2000+ images in the same directory) and
48 ;; browsing the thumbnail buffer was slow too. image-dired.el will not
49 ;; create thumbnails until they are needed and the browsing is done
50 ;; quickly and easily in dired. I copied a great deal of ideas and
51 ;; code from there though... :)
53 ;; `image-dired' stores the thumbnail files in `image-dired-dir'
54 ;; using the file name format ORIGNAME.thumb.ORIGEXT. For example
55 ;; ~/.emacs.d/image-dired/myimage01.thumb.jpg. The "database" is for
56 ;; now just a plain text file with the following format:
58 ;; file-name-non-directory;comment:comment-text;tag1;tag2;tag3;...;tagN
64 ;; * The ImageMagick package. Currently, `convert' and `mogrify' are
65 ;; used. Find it here: http://www.imagemagick.org.
67 ;; * For non-lossy rotation of JPEG images, the JpegTRAN program is
70 ;; * For `image-dired-get-exif-data' and `image-dired-write-exif-data' to work,
71 ;; the command line tool `exiftool' is needed. It can be found here:
72 ;; http://www.sno.phy.queensu.ca/~phil/exiftool/. These two functions
73 ;; are, among other things, used for writing comments to image files
74 ;; using `image-dired-thumbnail-set-image-description' and to create
75 ;; "unique" file names using `image-dired-get-exif-file-name' (used by
76 ;; `image-dired-copy-with-exif-file-name').
82 ;; This information has been moved to the manual. Type `C-h r' to open
83 ;; the Emacs manual and go to the node Thumbnails by typing `g
86 ;; Quickstart: M-x image-dired RET DIRNAME RET
88 ;; where DIRNAME is a directory containing image files.
93 ;; * Supports all image formats that Emacs and convert supports, but
94 ;; the thumbnails are hard-coded to JPEG format.
96 ;; * WARNING: The "database" format used might be changed so keep a
97 ;; backup of `image-dired-db-file' when testing new versions.
103 ;; * Support gallery creation when using per-directory thumbnail
106 ;; * Some sort of auto-rotate function based on rotate info in the
109 ;; * Check if exiftool exist before trying to call it to give a better
112 ;; * Investigate if it is possible to also write the tags to the image
115 ;; * From thumbs.el: Add an option for clean-up/max-size functionality
116 ;; for thumbnail directory.
118 ;; * From thumbs.el: Add setroot function.
120 ;; * From thumbs.el: Add image resizing, if useful (image-dired's automatic
121 ;; "image fit" might be enough)
123 ;; * From thumbs.el: Add the "modify" commands (emboss, negate,
126 ;; * Asynchronous creation of thumbnails.
128 ;; * Add `image-dired-display-thumbs-ring' and functions to cycle that. Find
129 ;; out which is best, saving old batch just before inserting new, or
130 ;; saving the current batch in the ring when inserting it. Adding it
131 ;; probably needs rewriting `image-dired-display-thumbs' to be more general.
133 ;; * Find some way of toggling on and off really nice keybindings in
134 ;; dired (for example, using C-n or <down> instead of C-S-n). Richard
135 ;; suggested that we could keep C-t as prefix for image-dired commands
136 ;; as it is currently not used in dired. He also suggested that
137 ;; `dired-next-line' and `dired-previous-line' figure out if
138 ;; image-dired is enabled in the current buffer and, if it is, call
139 ;; `image-dired-dired-next-line' and
140 ;; `image-dired-dired-previous-line', respectively. Update: This is
141 ;; partly done; some bindings have now been added to dired.
143 ;; * Enhanced gallery creation with basic CSS-support and pagination
144 ;; of tag pages with many pictures.
146 ;; * Rewrite `image-dired-modify-mark-on-thumb-original-file' to be
149 ;; * In some way keep track of buffers and windows and stuff so that
150 ;; it works as the user expects.
152 ;; * More/better documentation
158 (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
"~/.emacs.d/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
"~/.emacs.d/image-dired/.image-dired_db"
191 "Database file where file names and their associated tags are stored."
195 (defcustom image-dired-temp-image-file
"~/.emacs.d/image-dired/.image-dired_temp"
196 "Name of temporary image file used by various commands."
200 (defcustom image-dired-gallery-dir
"~/.emacs.d/image-dired/.image-dired_gallery"
201 "Directory to store generated gallery html pages.
202 This path needs to be \"shared\" to the public so that it can access
203 the index.html page that image-dired creates."
207 (defcustom image-dired-gallery-image-root-url
208 "http://your.own.server/image-diredpics"
209 "URL where the full size images are to be found.
210 Note that this path has to be configured in your web server. Image-Dired
211 expects to find pictures in this directory."
215 (defcustom image-dired-gallery-thumb-image-root-url
216 "http://your.own.server/image-diredthumbs"
217 "URL where the thumbnail images are to be found.
218 Note that this path has to be configured in your web server. Image-Dired
219 expects to find pictures in this directory."
223 (defcustom image-dired-cmd-create-thumbnail-program
225 "Executable used to create thumbnail.
226 Used together with `image-dired-cmd-create-thumbnail-options'."
230 (defcustom image-dired-cmd-create-thumbnail-options
231 "%p -size %wx%h \"%f\" -resize %wx%h -strip jpeg:\"%t\""
232 "Format of command used to create thumbnail image.
233 Available options are %p which is replaced by
234 `image-dired-cmd-create-thumbnail-program', %w which is replaced by
235 `image-dired-thumb-width', %h which is replaced by `image-dired-thumb-height',
236 %f which is replaced by the file name of the original image and %t
237 which is replaced by the file name of the thumbnail file."
241 (defcustom image-dired-cmd-create-temp-image-program
243 "Executable used to create temporary image.
244 Used together with `image-dired-cmd-create-temp-image-options'."
248 (defcustom image-dired-cmd-create-temp-image-options
249 "%p -size %wx%h \"%f\" -resize %wx%h -strip jpeg:\"%t\""
250 "Format of command used to create temporary image for display window.
251 Available options are %p which is replaced by
252 `image-dired-cmd-create-temp-image-program', %w and %h which is replaced by
253 the calculated max size for width and height in the image display window,
254 %f which is replaced by the file name of the original image and %t which
255 is replaced by the file name of the temporary file."
259 (defcustom image-dired-cmd-pngnq-program
(executable-find "pngnq")
260 "The file name of the `pngnq' program.
261 It quantizes colors of PNG images down to 256 colors."
262 :type
'(choice (const :tag
"Not Set" nil
) string
)
265 (defcustom image-dired-cmd-pngcrush-program
(executable-find "pngcrush")
266 "The file name of the `pngcrush' program.
267 It optimizes the compression of PNG images. Also it adds PNG textual chunks
268 with the information required by the Thumbnail Managing Standard."
269 :type
'(choice (const :tag
"Not Set" nil
) string
)
272 (defcustom image-dired-cmd-create-standard-thumbnail-command
274 image-dired-cmd-create-thumbnail-program
" "
275 "-size %wx%h \"%f\" "
276 (unless (or image-dired-cmd-pngcrush-program image-dired-cmd-pngnq-program
)
278 "-set \"Thumb::MTime\" \"%m\" "
279 "-set \"Thumb::URI\" \"file://%f\" "
280 "-set \"Description\" \"Thumbnail of file://%f\" "
281 "-set \"Software\" \"" (emacs-version) "\" "))
282 "-thumbnail %wx%h png:\"%t\""
283 (if image-dired-cmd-pngnq-program
285 " ; " image-dired-cmd-pngnq-program
" -f \"%t\""
286 (unless image-dired-cmd-pngcrush-program
288 (if image-dired-cmd-pngcrush-program
290 (unless image-dired-cmd-pngcrush-program
292 " ; " image-dired-cmd-pngcrush-program
" -q "
293 "-text b \"Description\" \"Thumbnail of file://%f\" "
294 "-text b \"Software\" \"" (emacs-version) "\" "
295 ;; "-text b \"Thumb::Image::Height\" \"%oh\" "
296 ;; "-text b \"Thumb::Image::Mimetype\" \"%mime\" "
297 ;; "-text b \"Thumb::Image::Width\" \"%ow\" "
298 "-text b \"Thumb::MTime\" \"%m\" "
299 ;; "-text b \"Thumb::Size\" \"%b\" "
300 "-text b \"Thumb::URI\" \"file://%f\" "
303 "Command to create thumbnails according to the Thumbnail Managing Standard."
307 (defcustom image-dired-cmd-rotate-thumbnail-program
309 "Executable used to rotate thumbnail.
310 Used together with `image-dired-cmd-rotate-thumbnail-options'."
314 (defcustom image-dired-cmd-rotate-thumbnail-options
315 "%p -rotate %d \"%t\""
316 "Format of command used to rotate thumbnail image.
317 Available options are %p which is replaced by
318 `image-dired-cmd-rotate-thumbnail-program', %d which is replaced by the
319 number of (positive) degrees to rotate the image, normally 90 or 270
320 \(for 90 degrees right and left), %t which is replaced by the file name
321 of the thumbnail file."
325 (defcustom image-dired-cmd-rotate-original-program
327 "Executable used to rotate original image.
328 Used together with `image-dired-cmd-rotate-original-options'."
332 (defcustom image-dired-cmd-rotate-original-options
333 "%p -rotate %d -copy all -outfile %t \"%o\""
334 "Format of command used to rotate original image.
335 Available options are %p which is replaced by
336 `image-dired-cmd-rotate-original-program', %d which is replaced by the
337 number of (positive) degrees to rotate the image, normally 90 or
338 270 \(for 90 degrees right and left), %o which is replaced by the
339 original image file name and %t which is replaced by
340 `image-dired-temp-image-file'."
344 (defcustom image-dired-temp-rotate-image-file
345 "~/.emacs.d/image-dired/.image-dired_rotate_temp"
346 "Temporary file for rotate operations."
350 (defcustom image-dired-rotate-original-ask-before-overwrite t
351 "Confirm overwrite of original file after rotate operation.
352 If non-nil, ask user for confirmation before overwriting the
353 original file with `image-dired-temp-rotate-image-file'."
357 (defcustom image-dired-cmd-write-exif-data-program
359 "Program used to write EXIF data to image.
360 Used together with `image-dired-cmd-write-exif-data-options'."
364 (defcustom image-dired-cmd-write-exif-data-options
365 "%p -%t=\"%v\" \"%f\""
366 "Format of command used to write EXIF data.
367 Available options are %p which is replaced by
368 `image-dired-cmd-write-exif-data-program', %f which is replaced by the
369 image file name, %t which is replaced by the tag name and %v
370 which is replaced by the tag value."
374 (defcustom image-dired-cmd-read-exif-data-program
376 "Program used to read EXIF data to image.
377 Used together with `image-dired-cmd-read-exif-data-program-options'."
381 (defcustom image-dired-cmd-read-exif-data-options
382 "%p -s -s -s -%t \"%f\""
383 "Format of command used to read EXIF data.
384 Available options are %p which is replaced by
385 `image-dired-cmd-write-exif-data-options', %f which is replaced
386 by the image file name and %t which is replaced by the tag name."
390 (defcustom image-dired-gallery-hidden-tags
391 (list "private" "hidden" "pending")
392 "List of \"hidden\" tags.
393 Used by `image-dired-gallery-generate' to leave out \"hidden\" images."
394 :type
'(repeat string
)
397 (defcustom image-dired-thumb-size
(if (eq 'standard image-dired-thumbnail-storage
) 128 100)
398 "Size of thumbnails, in pixels.
399 This is the default size for both `image-dired-thumb-width' and `image-dired-thumb-height'."
403 (defcustom image-dired-thumb-width image-dired-thumb-size
404 "Width of thumbnails, in pixels."
408 (defcustom image-dired-thumb-height image-dired-thumb-size
409 "Height of thumbnails, in pixels."
413 (defcustom image-dired-thumb-relief
2
414 "Size of button-like border around thumbnails."
418 (defcustom image-dired-thumb-margin
2
419 "Size of the margin around thumbnails.
420 This is where you see the cursor."
424 (defcustom image-dired-line-up-method
'dynamic
425 "Default method for line-up of thumbnails in thumbnail buffer.
426 Used by `image-dired-display-thumbs' and other functions that needs to
427 line-up thumbnails. Dynamic means to use the available width of the
428 window containing the thumbnail buffer, Fixed means to use
429 `image-dired-thumbs-per-row', Interactive is for asking the user, and No
430 line-up means that no automatic line-up will be done."
431 :type
'(choice :tag
"Default line-up method"
432 (const :tag
"Dynamic" dynamic
)
433 (const :tag
"Fixed" fixed
)
434 (const :tag
"Interactive" interactive
)
435 (const :tag
"No line-up" none
))
438 (defcustom image-dired-thumbs-per-row
3
439 "Number of thumbnails to display per row in thumb buffer."
443 (defcustom image-dired-display-window-width-correction
1
444 "Number to be used to correct image display window width.
445 Change if the default (1) does not work (i.e. if the image does not
450 (defcustom image-dired-display-window-height-correction
0
451 "Number to be used to correct image display window height.
452 Change if the default (0) does not work (i.e. if the image does not
457 (defcustom image-dired-track-movement t
458 "The current state of the tracking and mirroring.
459 For more information, see the documentation for
460 `image-dired-toggle-movement-tracking'."
464 (defcustom image-dired-append-when-browsing nil
465 "Append thumbnails in thumbnail buffer when browsing.
466 If non-nil, using `image-dired-next-line-and-display' and
467 `image-dired-previous-line-and-display' will leave a trail of thumbnail
468 images in the thumbnail buffer. If you enable this and want to clean
469 the thumbnail buffer because it is filled with too many thumbmnails,
470 just call `image-dired-display-thumb' to display only the image at point.
471 This value can be toggled using `image-dired-toggle-append-browsing'."
475 (defcustom image-dired-dired-disp-props t
476 "If non-nil, display properties for dired file when browsing.
477 Used by `image-dired-next-line-and-display',
478 `image-dired-previous-line-and-display' and `image-dired-mark-and-display-next'.
479 If the database file is large, this can slow down image browsing in
480 dired and you might want to turn it off."
484 (defcustom image-dired-display-properties-format
"%b: %f (%t): %c"
485 "Display format for thumbnail properties.
486 %b is replaced with associated dired buffer name, %f with file name
487 \(without path) of original image file, %t with the list of tags and %c
492 (defcustom image-dired-external-viewer
493 ;; TODO: Use mailcap, dired-guess-shell-alist-default,
494 ;; dired-view-command-alist.
495 (cond ((executable-find "display"))
496 ((executable-find "xli"))
497 ((executable-find "qiv") "qiv -t"))
498 "Name of external viewer.
499 Including parameters. Used when displaying original image from
500 `image-dired-thumbnail-mode'."
504 (defcustom image-dired-main-image-directory
"~/pics/"
505 "Name of main image directory, if any.
506 Used by `image-dired-copy-with-exif-file-name'."
510 (defcustom image-dired-show-all-from-dir-max-files
50
511 "Maximum number of files to show using `image-dired-show-all-from-dir'.
512 before warning the user."
516 (defun image-dired-dir ()
517 "Return the current thumbnails directory (from variable `image-dired-dir').
518 Create the thumbnails directory if it does not exist."
519 (let ((image-dired-dir (file-name-as-directory
520 (expand-file-name image-dired-dir
))))
521 (unless (file-directory-p image-dired-dir
)
522 (make-directory image-dired-dir t
)
523 (message "Creating thumbnails directory"))
526 (defun image-dired-insert-image (file type relief margin
)
527 "Insert image FILE of image TYPE, using RELIEF and MARGIN, at point."
529 (let ((i `(image :type
,type
535 (defun image-dired-get-thumbnail-image (file)
536 "Return the image descriptor for a thumbnail of image file FILE."
537 (unless (string-match (image-file-name-regexp) file
)
538 (error "%s is not a valid image file" file
))
539 (let ((thumb-file (image-dired-thumb-name file
)))
540 (unless (and (file-exists-p thumb-file
)
541 (<= (float-time (nth 5 (file-attributes file
)))
542 (float-time (nth 5 (file-attributes thumb-file
)))))
543 (image-dired-create-thumb file thumb-file
))
544 (create-image thumb-file
)
545 ;; (list 'image :type 'jpeg
547 ;; :relief image-dired-thumb-relief :margin image-dired-thumb-margin)
550 (defun image-dired-insert-thumbnail (file original-file-name
551 associated-dired-buffer
)
552 "Insert thumbnail image FILE.
553 Add text properties ORIGINAL-FILE-NAME and ASSOCIATED-DIRED-BUFFER."
556 (image-dired-insert-image file
557 ;; TODO: this should depend on the real file type
558 (if (eq 'standard image-dired-thumbnail-storage
)
560 image-dired-thumb-relief
561 image-dired-thumb-margin
)
565 (list 'image-dired-thumbnail t
566 'original-file-name original-file-name
567 'associated-dired-buffer associated-dired-buffer
568 'tags
(image-dired-list-tags original-file-name
)
569 'mouse-face
'highlight
570 'comment
(image-dired-get-comment original-file-name
)))))
572 (defun image-dired-thumb-name (file)
573 "Return thumbnail file name for FILE.
574 Depending on the value of `image-dired-thumbnail-storage', the file
575 name will vary. For central thumbnail file storage, make a
576 MD5-hash of the image file's directory name and add that to make
577 the thumbnail file name unique. For per-directory storage, just
578 add a subdirectory. For standard storage, produce the file name
579 according to the Thumbnail Managing Standard."
580 (cond ((eq 'standard image-dired-thumbnail-storage
)
582 (concat "~/.thumbnails/normal/"
583 (md5 (concat "file://" (expand-file-name file
))) ".png")))
584 ((eq 'use-image-dired-dir image-dired-thumbnail-storage
)
585 (let* ((f (expand-file-name file
))
587 ;; Is MD5 hashes fast enough? The checksum of a
588 ;; thumbnail file name need not be that
589 ;; "cryptographically" good so a faster one could
591 (md5 (file-name-as-directory (file-name-directory f
)))))
592 (format "%s%s%s.thumb.%s"
593 (file-name-as-directory (expand-file-name (image-dired-dir)))
594 (file-name-sans-extension (file-name-nondirectory f
))
595 (if md5-hash
(concat "_" md5-hash
) "")
596 (file-name-extension f
))))
597 ((eq 'per-directory image-dired-thumbnail-storage
)
598 (let ((f (expand-file-name file
)))
599 (format "%s.image-dired/%s.thumb.%s"
600 (file-name-directory f
)
601 (file-name-sans-extension (file-name-nondirectory f
))
602 (file-name-extension f
))))))
604 (defun image-dired-create-thumb (original-file thumbnail-file
)
605 "For ORIGINAL-FILE, create thumbnail image named THUMBNAIL-FILE."
606 (let* ((width (int-to-string image-dired-thumb-width
))
607 (height (int-to-string image-dired-thumb-height
))
608 (modif-time (format "%.0f" (float-time (nth 5 (file-attributes
610 (thumbnail-nq8-file (replace-regexp-in-string ".png\\'" "-nq8.png"
614 (if (eq 'standard image-dired-thumbnail-storage
)
615 image-dired-cmd-create-standard-thumbnail-command
616 image-dired-cmd-create-thumbnail-options
)
618 (cons ?p image-dired-cmd-create-thumbnail-program
)
622 (cons ?f original-file
)
623 (cons ?q thumbnail-nq8-file
)
624 (cons ?t thumbnail-file
))))
626 (when (not (file-exists-p
627 (setq thumbnail-dir
(file-name-directory thumbnail-file
))))
628 (message "Creating thumbnail directory.")
629 (make-directory thumbnail-dir
))
630 (call-process shell-file-name nil nil nil shell-command-switch command
)))
633 (defun image-dired-dired-insert-marked-thumbs ()
634 "Insert thumbnails before file names of marked files in the dired buffer."
636 (dired-map-over-marks
637 (let* ((image-pos (dired-move-to-filename))
638 (image-file (dired-get-filename))
639 (thumb-file (image-dired-get-thumbnail-image image-file
))
641 ;; If image is not already added, then add it.
642 (unless (delq nil
(mapcar (lambda (o) (overlay-get o
'put-image
))
643 ;; Can't use (overlays-at (point)), BUG?
644 (overlays-in (point) (1+ (point)))))
645 (put-image thumb-file image-pos
)
648 (car (delq nil
(mapcar (lambda (o) (and (overlay-get o
'put-image
) o
))
649 (overlays-in (point) (1+ (point)))))))
650 (overlay-put overlay
'image-file image-file
)
651 (overlay-put overlay
'thumb-file thumb-file
)))
653 (add-hook 'dired-after-readin-hook
'image-dired-dired-after-readin-hook nil t
))
655 (defun image-dired-dired-after-readin-hook ()
656 "Relocate existing thumbnail overlays in dired buffer after reverting.
657 Move them to their corresponding files if they are still exist.
658 Otherwise, delete overlays."
659 (mapc (lambda (overlay)
660 (when (overlay-get overlay
'put-image
)
661 (let* ((image-file (overlay-get overlay
'image-file
))
662 (image-pos (dired-goto-file image-file
)))
664 (move-overlay overlay image-pos image-pos
)
665 (delete-overlay overlay
)))))
666 (overlays-in (point-min) (point-max))))
668 (defun image-dired-next-line-and-display ()
669 "Move to next dired line and display thumbnail image."
672 (image-dired-display-thumbs
673 t
(or image-dired-append-when-browsing nil
) t
)
674 (if image-dired-dired-disp-props
675 (image-dired-dired-display-properties)))
677 (defun image-dired-previous-line-and-display ()
678 "Move to previous dired line and display thumbnail image."
680 (dired-previous-line 1)
681 (image-dired-display-thumbs
682 t
(or image-dired-append-when-browsing nil
) t
)
683 (if image-dired-dired-disp-props
684 (image-dired-dired-display-properties)))
686 (defun image-dired-toggle-append-browsing ()
687 "Toggle `image-dired-append-when-browsing'."
689 (setq image-dired-append-when-browsing
690 (not image-dired-append-when-browsing
))
691 (message "Append browsing %s."
692 (if image-dired-append-when-browsing
696 (defun image-dired-mark-and-display-next ()
697 "Mark current file in dired and display next thumbnail image."
700 (image-dired-display-thumbs
701 t
(or image-dired-append-when-browsing nil
) t
)
702 (if image-dired-dired-disp-props
703 (image-dired-dired-display-properties)))
705 (defun image-dired-toggle-dired-display-properties ()
706 "Toggle `image-dired-dired-disp-props'."
708 (setq image-dired-dired-disp-props
709 (not image-dired-dired-disp-props
))
710 (message "Dired display properties %s."
711 (if image-dired-dired-disp-props
715 (defvar image-dired-thumbnail-buffer
"*image-dired*"
716 "Image-Dired's thumbnail buffer.")
718 (defun image-dired-create-thumbnail-buffer ()
719 "Create thumb buffer and set `image-dired-thumbnail-mode'."
720 (let ((buf (get-buffer-create image-dired-thumbnail-buffer
)))
721 (with-current-buffer buf
722 (setq buffer-read-only t
)
723 (if (not (eq major-mode
'image-dired-thumbnail-mode
))
724 (image-dired-thumbnail-mode)))
727 (defvar image-dired-display-image-buffer
"*image-dired-display-image*"
728 "Where larger versions of the images are display.")
730 (defun image-dired-create-display-image-buffer ()
731 "Create image display buffer and set `image-dired-display-image-mode'."
732 (let ((buf (get-buffer-create image-dired-display-image-buffer
)))
733 (with-current-buffer buf
734 (setq buffer-read-only t
)
735 (if (not (eq major-mode
'image-dired-display-image-mode
))
736 (image-dired-display-image-mode)))
739 (defvar image-dired-saved-window-configuration nil
740 "Saved window configuration.")
743 (defun image-dired-dired-with-window-configuration (dir &optional arg
)
744 "Open directory DIR and create a default window configuration.
746 Convenience command that:
748 - Opens dired in folder DIR
749 - Splits windows in most useful (?) way
750 - Set `truncate-lines' to t
752 After the command has finished, you would typically mark some
753 image files in dired and type
754 \\[image-dired-display-thumbs] (`image-dired-display-thumbs').
756 If called with prefix argument ARG, skip splitting of windows.
758 The current window configuration is saved and can be restored by
759 calling `image-dired-restore-window-configuration'."
760 (interactive "DDirectory: \nP")
761 (let ((buf (image-dired-create-thumbnail-buffer))
762 (buf2 (image-dired-create-display-image-buffer)))
763 (setq image-dired-saved-window-configuration
764 (current-window-configuration))
766 (delete-other-windows)
768 (split-window-horizontally)
769 (setq truncate-lines t
)
772 (switch-to-buffer buf
)
773 (split-window-vertically)
775 (switch-to-buffer buf2
)
776 (other-window -
2)))))
778 (defun image-dired-restore-window-configuration ()
779 "Restore window configuration.
780 Restore any changes to the window configuration made by calling
781 `image-dired-dired-with-window-configuration'."
783 (if image-dired-saved-window-configuration
784 (set-window-configuration image-dired-saved-window-configuration
)
785 (message "No saved window configuration")))
788 (defun image-dired-display-thumbs (&optional arg append do-not-pop
)
789 "Display thumbnails of all marked files, in `image-dired-thumbnail-buffer'.
790 If a thumbnail image does not exist for a file, it is created on the
791 fly. With prefix argument ARG, display only thumbnail for file at
792 point (this is useful if you have marked some files but want to show
795 Recommended usage is to split the current frame horizontally so that
796 you have the dired buffer in the left window and the
797 `image-dired-thumbnail-buffer' buffer in the right window.
799 With optional argument APPEND, append thumbnail to thumbnail buffer
800 instead of erasing it first.
802 Option argument DO-NOT-POP controls if `pop-to-buffer' should be
803 used or not. If non-nil, use `display-buffer' instead of
804 `pop-to-buffer'. This is used from functions like
805 `image-dired-next-line-and-display' and
806 `image-dired-previous-line-and-display' where we do not want the
807 thumbnail buffer to be selected."
809 (let ((buf (image-dired-create-thumbnail-buffer))
810 curr-file thumb-name files count dired-buf beg
)
812 (setq files
(list (dired-get-filename)))
813 (setq files
(dired-get-marked-files)))
814 (setq dired-buf
(current-buffer))
815 (with-current-buffer buf
816 (let ((inhibit-read-only t
))
819 (goto-char (point-max)))
822 (setq thumb-name
(image-dired-thumb-name curr-file
))
823 (if (and (not (file-exists-p thumb-name
))
824 (not (= 0 (image-dired-create-thumb curr-file thumb-name
))))
825 (message "Thumb could not be created for file %s" curr-file
)
826 (image-dired-insert-thumbnail thumb-name curr-file dired-buf
)))
828 (cond ((eq 'dynamic image-dired-line-up-method
)
829 (image-dired-line-up-dynamic))
830 ((eq 'fixed image-dired-line-up-method
)
831 (image-dired-line-up))
832 ((eq 'interactive image-dired-line-up-method
)
833 (image-dired-line-up-interactive))
834 ((eq 'none image-dired-line-up-method
)
837 (image-dired-line-up-dynamic))))
839 (display-buffer image-dired-thumbnail-buffer
)
840 (pop-to-buffer image-dired-thumbnail-buffer
))))
843 (defun image-dired-show-all-from-dir (dir)
844 "Make a preview buffer for all images in DIR and display it.
845 If the number of files in DIR matching `image-file-name-regexp'
846 exceeds `image-dired-show-all-from-dir-max-files', a warning will be
848 (interactive "DDir: ")
850 (dired-mark-files-regexp (image-file-name-regexp))
851 (let ((files (dired-get-marked-files)))
852 (if (or (<= (length files
) image-dired-show-all-from-dir-max-files
)
853 (and (> (length files
) image-dired-show-all-from-dir-max-files
)
856 "Directory contains more than %d image files. Proceed? "
857 image-dired-show-all-from-dir-max-files
))))
859 (image-dired-display-thumbs)
860 (pop-to-buffer image-dired-thumbnail-buffer
))
861 (message "Cancelled."))))
864 (defalias 'image-dired
'image-dired-show-all-from-dir
)
867 (defalias 'tumme
'image-dired-show-all-from-dir
)
869 (defun image-dired-write-tags (file-tags)
870 "Write file tags to database.
871 Write each file and tag in FILE-TAGS to the database. FILE-TAGS
872 is an alist in the following form:
875 (with-temp-file image-dired-db-file
876 (insert-file-contents image-dired-db-file
)
877 (dolist (elt file-tags
)
880 (goto-char (point-min))
881 (if (search-forward-regexp (format "^%s.*$" file
) nil t
)
885 (when (not (search-forward (format ";%s" tag
) end t
))
887 (insert (format ";%s" tag
))))
888 (goto-char (point-max))
889 (insert (format "\n%s;%s" file tag
)))))))
891 (defun image-dired-remove-tag (files tag
)
892 "For all FILES, remove TAG from the image database."
895 (setq buf
(find-file image-dired-db-file
))
896 (if (not (listp files
))
898 (setq files
(list files
))
899 (error "Files must be a string or a list of strings!")))
902 (goto-char (point-min))
903 (when (search-forward-regexp
904 (format "^%s" file
) nil t
)
908 (when (search-forward-regexp (format "\\(;%s\\)" tag
) end t
)
909 (delete-region (match-beginning 1) (match-end 1))
910 ;; Check if file should still be in the database. If
911 ;; it has no tags or comments, it will be removed.
915 (when (not (search-forward ";" end t
))
917 ;; If on empty line at end of buffer
920 (delete-backward-char 1))))))
925 (defun image-dired-list-tags (file)
926 "Read all tags for image FILE from the image database."
928 (let (end buf
(tags ""))
929 (setq buf
(find-file image-dired-db-file
))
930 (goto-char (point-min))
931 (when (search-forward-regexp
932 (format "^%s" file
) nil t
)
936 (if (search-forward ";" end t
)
937 (if (search-forward "comment:" end t
)
938 (if (search-forward ";" end t
)
939 (setq tags
(buffer-substring (point) end
)))
940 (setq tags
(buffer-substring (point) end
)))))
942 (split-string tags
";"))))
945 (defun image-dired-tag-files (arg)
946 "Tag marked file(s) in dired. With prefix ARG, tag file at point."
948 (let ((tag (read-string "Tags to add (separate tags with a semicolon): "))
951 (setq files
(list (dired-get-filename)))
952 (setq files
(dired-get-marked-files)))
953 (image-dired-write-tags
959 (defun image-dired-tag-thumbnail ()
960 "Tag current thumbnail."
962 (let ((tag (read-string "Tags to add (separate tags with a semicolon): ")))
963 (image-dired-write-tags (list (cons (image-dired-original-file-name) tag
))))
964 (image-dired-update-property
965 'tags
(image-dired-list-tags (image-dired-original-file-name))))
968 (defun image-dired-delete-tag (arg)
969 "Remove tag for selected file(s).
970 With prefix argument ARG, remove tag from file at point."
972 (let ((tag (read-string "Tag to remove: "))
975 (setq files
(list (dired-get-filename)))
976 (setq files
(dired-get-marked-files)))
977 (image-dired-remove-tag files tag
)))
979 (defun image-dired-tag-thumbnail-remove ()
980 "Remove tag from thumbnail."
982 (let ((tag (read-string "Tag to remove: ")))
983 (image-dired-remove-tag (image-dired-original-file-name) tag
))
984 (image-dired-update-property
985 'tags
(image-dired-list-tags (image-dired-original-file-name))))
987 (defun image-dired-original-file-name ()
988 "Get original file name for thumbnail or display image at point."
989 (get-text-property (point) 'original-file-name
))
991 (defun image-dired-associated-dired-buffer ()
992 "Get associated dired buffer at point."
993 (get-text-property (point) 'associated-dired-buffer
))
995 (defun image-dired-get-buffer-window (buf)
996 "Return window where buffer BUF is."
997 (get-window-with-predicate
999 (equal (window-buffer window
) buf
))
1002 (defun image-dired-track-original-file ()
1003 "Track the original file in the associated dired buffer.
1004 See documentation for `image-dired-toggle-movement-tracking'. Interactive
1005 use only useful if `image-dired-track-movement' is nil."
1007 (let ((old-buf (current-buffer))
1008 (dired-buf (image-dired-associated-dired-buffer))
1009 (file-name (image-dired-original-file-name)))
1010 (when (and (buffer-live-p dired-buf
) file-name
)
1011 (setq file-name
(file-name-nondirectory file-name
))
1012 (set-buffer dired-buf
)
1013 (goto-char (point-min))
1014 (if (not (search-forward file-name nil t
))
1015 (message "Could not track file")
1016 (dired-move-to-filename)
1018 (image-dired-get-buffer-window dired-buf
) (point)))
1019 (set-buffer old-buf
))))
1021 (defun image-dired-toggle-movement-tracking ()
1022 "Turn on and off `image-dired-track-movement'.
1023 Tracking of the movements between thumbnail and dired buffer so that
1024 they are \"mirrored\" in the dired buffer. When this is on, moving
1025 around in the thumbnail or dired buffer will find the matching
1026 position in the other buffer."
1028 (setq image-dired-track-movement
(not image-dired-track-movement
))
1029 (message "Tracking %s" (if image-dired-track-movement
"on" "off")))
1031 (defun image-dired-track-thumbnail ()
1032 "Track current dired file's thumb in `image-dired-thumbnail-buffer'.
1033 This is almost the same as what `image-dired-track-original-file' does, but
1034 the other way around."
1035 (let ((file (dired-get-filename))
1036 (old-buf (current-buffer))
1038 (when (get-buffer image-dired-thumbnail-buffer
)
1039 (set-buffer image-dired-thumbnail-buffer
)
1040 (goto-char (point-min))
1041 (while (and (not (eobp))
1043 (if (and (setq prop-val
1044 (get-text-property (point) 'original-file-name
))
1045 (string= prop-val file
))
1051 (image-dired-thumbnail-window) (point))
1052 (image-dired-display-thumb-properties))
1053 (set-buffer old-buf
))))
1055 (defun image-dired-dired-next-line (&optional arg
)
1056 "Call `dired-next-line', then track thumbnail.
1057 This can safely replace `dired-next-line'. With prefix argument, move
1060 (dired-next-line (or arg
1))
1061 (if image-dired-track-movement
1062 (image-dired-track-thumbnail)))
1064 (defun image-dired-dired-previous-line (&optional arg
)
1065 "Call `dired-previous-line', then track thumbnail.
1066 This can safely replace `dired-previous-line'. With prefix argument,
1069 (dired-previous-line (or arg
1))
1070 (if image-dired-track-movement
1071 (image-dired-track-thumbnail)))
1073 (defun image-dired-forward-image (&optional arg
)
1074 "Move to next image and display properties.
1075 Optional prefix ARG says how many images to move; default is one
1078 (let (pos (steps (or arg
1)))
1080 (if (and (not (eobp))
1083 (while (and (not (eobp))
1084 (not (image-dired-image-at-point-p)))
1087 (image-dired-image-at-point-p)))
1089 (error "At last image"))))
1090 (when image-dired-track-movement
1091 (image-dired-track-original-file))
1092 (image-dired-display-thumb-properties))
1094 (defun image-dired-backward-image (&optional arg
)
1095 "Move to previous image and display properties.
1096 Optional prefix ARG says how many images to move; default is one
1099 (let (pos (steps (or arg
1)))
1101 (if (and (not (bobp))
1104 (while (and (not (bobp))
1105 (not (image-dired-image-at-point-p)))
1108 (image-dired-image-at-point-p)))
1110 (error "At first image"))))
1111 (when image-dired-track-movement
1112 (image-dired-track-original-file))
1113 (image-dired-display-thumb-properties))
1115 (defun image-dired-next-line ()
1116 "Move to next line and display properties."
1119 ;; If we end up in an empty spot, back up to the next thumbnail.
1120 (if (not (image-dired-image-at-point-p))
1121 (image-dired-backward-image))
1122 (if image-dired-track-movement
1123 (image-dired-track-original-file))
1124 (image-dired-display-thumb-properties))
1127 (defun image-dired-previous-line ()
1128 "Move to previous line and display properties."
1131 ;; If we end up in an empty spot, back up to the next
1132 ;; thumbnail. This should only happen if the user deleted a
1133 ;; thumbnail and did not refresh, so it is not very common. But we
1134 ;; can handle it in a good manner, so why not?
1135 (if (not (image-dired-image-at-point-p))
1136 (image-dired-backward-image))
1137 (if image-dired-track-movement
1138 (image-dired-track-original-file))
1139 (image-dired-display-thumb-properties))
1141 (defun image-dired-format-properties-string (buf file props comment
)
1142 "Format display properties.
1143 BUF is the associated dired buffer, FILE is the original image file
1144 name, PROPS is a list of tags and COMMENT is the image files's
1147 image-dired-display-properties-format
1149 (cons ?b
(or buf
""))
1151 (cons ?t
(or (princ props
) ""))
1152 (cons ?c
(or comment
"")))))
1154 (defun image-dired-display-thumb-properties ()
1155 "Display thumbnail properties in the echo area."
1157 (let ((file-name (file-name-nondirectory (image-dired-original-file-name)))
1158 (dired-buf (buffer-name (image-dired-associated-dired-buffer)))
1161 (get-text-property (point) 'tags
)
1163 (comment (get-text-property (point) 'comment
)))
1166 (image-dired-format-properties-string
1172 (defun image-dired-dired-file-marked-p ()
1173 "Check whether file on current line is marked or not."
1176 (not (looking-at "^ .*$"))))
1178 (defun image-dired-modify-mark-on-thumb-original-file (command)
1179 "Modify mark in dired buffer.
1180 This is quite ugly but I don't know how to implemented in a better
1181 way. COMMAND is one of 'mark for marking file in dired, 'unmark for
1182 unmarking file in dired or 'flag for flagging file for delete in
1184 (let ((file-name (image-dired-original-file-name))
1185 (dired-buf (image-dired-associated-dired-buffer)))
1186 (if (not (and dired-buf file-name
))
1187 (message "No image, or image with correct properties, at point.")
1188 (with-current-buffer dired-buf
1190 (setq file-name
(file-name-nondirectory file-name
))
1191 (goto-char (point-min))
1192 (if (search-forward file-name nil t
)
1193 (cond ((eq command
'mark
) (dired-mark 1))
1194 ((eq command
'unmark
) (dired-unmark 1))
1195 ((eq command
'toggle
)
1196 (if (image-dired-dired-file-marked-p)
1199 ((eq command
'flag
) (dired-flag-file-deletion 1))))))))
1201 (defun image-dired-mark-thumb-original-file ()
1202 "Mark original image file in associated dired buffer."
1204 (image-dired-modify-mark-on-thumb-original-file 'mark
)
1205 (image-dired-forward-image))
1207 (defun image-dired-unmark-thumb-original-file ()
1208 "Unmark original image file in associated dired buffer."
1210 (image-dired-modify-mark-on-thumb-original-file 'unmark
)
1211 (image-dired-forward-image))
1213 (defun image-dired-flag-thumb-original-file ()
1214 "Flag original image file for deletion in associated dired buffer."
1216 (image-dired-modify-mark-on-thumb-original-file 'flag
)
1217 (image-dired-forward-image))
1219 (defun image-dired-toggle-mark-thumb-original-file ()
1220 "Toggle mark on original image file in associated dired buffer."
1222 (image-dired-modify-mark-on-thumb-original-file 'toggle
))
1224 (defun image-dired-jump-original-dired-buffer ()
1225 "Jump to the dired buffer associated with the current image file.
1226 You probably want to use this together with
1227 `image-dired-track-original-file'."
1229 (let ((buf (image-dired-associated-dired-buffer))
1231 (setq window
(image-dired-get-buffer-window buf
))
1234 (if (not (equal (selected-frame) (setq frame
(window-frame window
))))
1235 (select-frame-set-input-focus frame
))
1236 (select-window window
))
1237 (message "Associated dired buffer not visible"))))
1240 (defun image-dired-jump-thumbnail-buffer ()
1241 "Jump to thumbnail buffer."
1243 (let ((window (image-dired-thumbnail-window))
1247 (if (not (equal (selected-frame) (setq frame
(window-frame window
))))
1248 (select-frame-set-input-focus frame
))
1249 (select-window window
))
1250 (message "Thumbnail buffer not visible"))))
1252 (defvar image-dired-thumbnail-mode-map
(make-sparse-keymap)
1253 "Keymap for `image-dired-thumbnail-mode'.")
1255 (defvar image-dired-thumbnail-mode-line-up-map
(make-sparse-keymap)
1256 "Keymap for line-up commands in `image-dired-thumbnail-mode'.")
1258 (defvar image-dired-thumbnail-mode-tag-map
(make-sparse-keymap)
1259 "Keymap for tag commands in `image-dired-thumbnail-mode'.")
1261 (defun image-dired-define-thumbnail-mode-keymap ()
1262 "Define keymap for `image-dired-thumbnail-mode'."
1265 (define-key image-dired-thumbnail-mode-map
[right] 'image-dired-forward-image)
1266 (define-key image-dired-thumbnail-mode-map [left] 'image-dired-backward-image)
1267 (define-key image-dired-thumbnail-mode-map [up] 'image-dired-previous-line)
1268 (define-key image-dired-thumbnail-mode-map [down] 'image-dired-next-line)
1269 (define-key image-dired-thumbnail-mode-map "\C-f" 'image-dired-forward-image)
1270 (define-key image-dired-thumbnail-mode-map "\C-b" 'image-dired-backward-image)
1271 (define-key image-dired-thumbnail-mode-map "\C-p" 'image-dired-previous-line)
1272 (define-key image-dired-thumbnail-mode-map "\C-n" 'image-dired-next-line)
1274 (define-key image-dired-thumbnail-mode-map "d" 'image-dired-flag-thumb-original-file)
1275 (define-key image-dired-thumbnail-mode-map [delete]
1276 'image-dired-flag-thumb-original-file)
1277 (define-key image-dired-thumbnail-mode-map "m" 'image-dired-mark-thumb-original-file)
1278 (define-key image-dired-thumbnail-mode-map "u" 'image-dired-unmark-thumb-original-file)
1279 (define-key image-dired-thumbnail-mode-map "." 'image-dired-track-original-file)
1280 (define-key image-dired-thumbnail-mode-map [tab] 'image-dired-jump-original-dired-buffer)
1283 (define-key image-dired-thumbnail-mode-map "g" image-dired-thumbnail-mode-line-up-map)
1285 ;; map it to "g" so that the user can press it more quickly
1286 (define-key image-dired-thumbnail-mode-line-up-map "g" 'image-dired-line-up-dynamic)
1287 ;; "f" for "fixed" number of thumbs per row
1288 (define-key image-dired-thumbnail-mode-line-up-map "f" 'image-dired-line-up)
1289 ;; "i" for "interactive"
1290 (define-key image-dired-thumbnail-mode-line-up-map "i" 'image-dired-line-up-interactive)
1293 (define-key image-dired-thumbnail-mode-map "t" image-dired-thumbnail-mode-tag-map)
1295 ;; map it to "t" so that the user can press it more quickly
1296 (define-key image-dired-thumbnail-mode-tag-map "t" 'image-dired-tag-thumbnail)
1298 (define-key image-dired-thumbnail-mode-tag-map "r" 'image-dired-tag-thumbnail-remove)
1300 (define-key image-dired-thumbnail-mode-map "\C-m"
1301 'image-dired-display-thumbnail-original-image)
1302 (define-key image-dired-thumbnail-mode-map [C-return]
1303 'image-dired-thumbnail-display-external)
1305 (define-key image-dired-thumbnail-mode-map "l" 'image-dired-rotate-thumbnail-left)
1306 (define-key image-dired-thumbnail-mode-map "r" 'image-dired-rotate-thumbnail-right)
1308 (define-key image-dired-thumbnail-mode-map "L" 'image-dired-rotate-original-left)
1309 (define-key image-dired-thumbnail-mode-map "R" 'image-dired-rotate-original-right)
1311 (define-key image-dired-thumbnail-mode-map "D"
1312 'image-dired-thumbnail-set-image-description)
1314 (define-key image-dired-thumbnail-mode-map "\C-d" 'image-dired-delete-char)
1315 (define-key image-dired-thumbnail-mode-map " "
1316 'image-dired-display-next-thumbnail-original)
1317 (define-key image-dired-thumbnail-mode-map
1318 (kbd "DEL") 'image-dired-display-previous-thumbnail-original)
1319 (define-key image-dired-thumbnail-mode-map "c" 'image-dired-comment-thumbnail)
1320 (define-key image-dired-thumbnail-mode-map "q" 'image-dired-kill-buffer-and-window)
1323 (define-key image-dired-thumbnail-mode-map [mouse-2] 'image-dired-mouse-display-image)
1324 (define-key image-dired-thumbnail-mode-map [mouse-1] 'image-dired-mouse-select-thumbnail)
1326 ;; Seems I must first set C-down-mouse-1 to undefined, or else it
1327 ;; will trigger the buffer menu. If I try to instead bind
1328 ;; C-down-mouse-1 to `image-dired-mouse-toggle-mark', I get a message
1329 ;; about C-mouse-1 not being defined afterwards. Annoying, but I
1330 ;; probably do not completely understand mouse events.
1332 (define-key image-dired-thumbnail-mode-map [C-down-mouse-1] 'undefined)
1333 (define-key image-dired-thumbnail-mode-map [C-mouse-1] 'image-dired-mouse-toggle-mark)
1336 (define-key image-dired-thumbnail-mode-map [menu-bar image-dired]
1337 (cons "Image-Dired" (make-sparse-keymap "Image-Dired")))
1339 (define-key image-dired-thumbnail-mode-map
1340 [menu-bar image-dired image-dired-kill-buffer-and-window]
1341 '("Quit" . image-dired-kill-buffer-and-window))
1343 (define-key image-dired-thumbnail-mode-map
1344 [menu-bar image-dired image-dired-delete-char]
1345 '("Delete thumbnail from buffer" . image-dired-delete-char))
1347 (define-key image-dired-thumbnail-mode-map
1348 [menu-bar image-dired image-dired-tag-thumbnail-remove]
1349 '("Remove tag from thumbnail" . image-dired-tag-thumbnail-remove))
1351 (define-key image-dired-thumbnail-mode-map
1352 [menu-bar image-dired image-dired-tag-thumbnail]
1353 '("Tag thumbnail" . image-dired-tag-thumbnail))
1355 (define-key image-dired-thumbnail-mode-map
1356 [menu-bar image-dired image-dired-comment-thumbnail]
1357 '("Comment thumbnail" . image-dired-comment-thumbnail))
1359 (define-key image-dired-thumbnail-mode-map
1360 [menu-bar image-dired image-dired-refresh-thumb]
1361 '("Refresh thumb" . image-dired-refresh-thumb))
1362 (define-key image-dired-thumbnail-mode-map
1363 [menu-bar image-dired image-dired-line-up-dynamic]
1364 '("Dynamic line up" . image-dired-line-up-dynamic))
1365 (define-key image-dired-thumbnail-mode-map
1366 [menu-bar image-dired image-dired-line-up]
1367 '("Line up thumbnails" . image-dired-line-up))
1369 (define-key image-dired-thumbnail-mode-map
1370 [menu-bar image-dired image-dired-rotate-thumbnail-left]
1371 '("Rotate thumbnail left" . image-dired-rotate-thumbnail-left))
1372 (define-key image-dired-thumbnail-mode-map
1373 [menu-bar image-dired image-dired-rotate-thumbnail-right]
1374 '("Rotate thumbnail right" . image-dired-rotate-thumbnail-right))
1376 (define-key image-dired-thumbnail-mode-map
1377 [menu-bar image-dired image-dired-rotate-original-left]
1378 '("Rotate original left" . image-dired-rotate-original-left))
1379 (define-key image-dired-thumbnail-mode-map
1380 [menu-bar image-dired image-dired-rotate-original-right]
1381 '("Rotate original right" . image-dired-rotate-original-right))
1383 (define-key image-dired-thumbnail-mode-map
1384 [menu-bar image-dired image-dired-toggle-movement-tracking]
1385 '("Toggle movement tracking on/off" . image-dired-toggle-movement-tracking))
1387 (define-key image-dired-thumbnail-mode-map
1388 [menu-bar image-dired image-dired-jump-original-dired-buffer]
1389 '("Jump to dired buffer" . image-dired-jump-original-dired-buffer))
1390 (define-key image-dired-thumbnail-mode-map
1391 [menu-bar image-dired image-dired-track-original-file]
1392 '("Track original" . image-dired-track-original-file))
1394 (define-key image-dired-thumbnail-mode-map
1395 [menu-bar image-dired image-dired-flag-thumb-original-file]
1396 '("Flag original for deletion" . image-dired-flag-thumb-original-file))
1397 (define-key image-dired-thumbnail-mode-map
1398 [menu-bar image-dired image-dired-unmark-thumb-original-file]
1399 '("Unmark original" . image-dired-unmark-thumb-original-file))
1400 (define-key image-dired-thumbnail-mode-map
1401 [menu-bar image-dired image-dired-mark-thumb-original-file]
1402 '("Mark original" . image-dired-mark-thumb-original-file))
1404 (define-key image-dired-thumbnail-mode-map
1405 [menu-bar image-dired image-dired-thumbnail-display-external]
1406 '("Display in external viewer" . image-dired-thumbnail-display-external))
1407 (define-key image-dired-thumbnail-mode-map
1408 [menu-bar image-dired image-dired-display-thumbnail-original-image]
1409 '("Display image" . image-dired-display-thumbnail-original-image)))
1411 (defvar image-dired-display-image-mode-map (make-sparse-keymap)
1412 "Keymap for `image-dired-display-image-mode'.")
1414 (defun image-dired-define-display-image-mode-keymap ()
1415 "Define keymap for `image-dired-display-image-mode'."
1418 (define-key image-dired-display-image-mode-map "q" 'image-dired-kill-buffer-and-window)
1420 (define-key image-dired-display-image-mode-map "f"
1421 'image-dired-display-current-image-full)
1423 (define-key image-dired-display-image-mode-map "s"
1424 'image-dired-display-current-image-sized)
1427 (define-key image-dired-display-image-mode-map [menu-bar image-dired]
1428 (cons "Image-Dired" (make-sparse-keymap "Image-Dired")))
1430 (define-key image-dired-display-image-mode-map
1431 [menu-bar image-dired image-dired-kill-buffer-and-window]
1432 '("Quit" . image-dired-kill-buffer-and-window))
1434 (define-key image-dired-display-image-mode-map
1435 [menu-bar image-dired image-dired-display-current-image-sized]
1436 '("Display original, sized to fit" . image-dired-display-current-image-sized))
1438 (define-key image-dired-display-image-mode-map
1439 [menu-bar image-dired image-dired-display-current-image-full]
1440 '("Display original, full size" . image-dired-display-current-image-full))
1444 (defun image-dired-display-current-image-full ()
1445 "Display current image in full size."
1447 (let ((file (image-dired-original-file-name)))
1450 (image-dired-display-image file t)
1451 (message "Full size image displayed"))
1452 (error "No original file name at point"))))
1454 (defun image-dired-display-current-image-sized ()
1455 "Display current image in sized to fit window dimensions."
1457 (let ((file (image-dired-original-file-name)))
1460 (image-dired-display-image file)
1461 (message "Full size image displayed"))
1462 (error "No original file name at point"))))
1464 (define-derived-mode image-dired-thumbnail-mode
1465 fundamental-mode "image-dired-thumbnail"
1466 "Browse and manipulate thumbnail images using dired.
1467 Use `image-dired-dired' and `image-dired-setup-dired-keybindings' to get a
1468 nice setup to start with."
1469 (image-dired-define-thumbnail-mode-keymap)
1470 (message "image-dired-thumbnail-mode enabled"))
1472 (define-derived-mode image-dired-display-image-mode
1473 fundamental-mode "image-dired-image-display"
1474 "Mode for displaying and manipulating original image.
1475 Resized or in full-size."
1476 (image-dired-define-display-image-mode-keymap)
1477 (message "image-dired-display-image-mode enabled"))
1480 (defun image-dired-setup-dired-keybindings ()
1481 "Setup easy-to-use keybindings for the commands to be used in dired mode.
1482 Note that n, p and <down> and <up> will be hijacked and bound to
1483 `image-dired-dired-x-line'."
1486 ;; Hijack previous and next line movement. Let C-p and C-b be
1489 (define-key dired-mode-map "p" 'image-dired-dired-previous-line)
1490 (define-key dired-mode-map "n" 'image-dired-dired-next-line)
1491 (define-key dired-mode-map [up] 'image-dired-dired-previous-line)
1492 (define-key dired-mode-map [down] 'image-dired-dired-next-line)
1494 (define-key dired-mode-map (kbd "C-S-n") 'image-dired-next-line-and-display)
1495 (define-key dired-mode-map (kbd "C-S-p") 'image-dired-previous-line-and-display)
1496 (define-key dired-mode-map (kbd "C-S-m") 'image-dired-mark-and-display-next)
1498 (define-key dired-mode-map "\C-td" 'image-dired-display-thumbs)
1499 (define-key dired-mode-map "\C-tt" 'image-dired-tag-files)
1500 (define-key dired-mode-map "\C-tr" 'image-dired-delete-tag)
1501 (define-key dired-mode-map [tab] 'image-dired-jump-thumbnail-buffer)
1502 (define-key dired-mode-map "\C-ti" 'image-dired-dired-display-image)
1503 (define-key dired-mode-map "\C-tx" 'image-dired-dired-display-external)
1504 (define-key dired-mode-map "\C-ta" 'image-dired-display-thumbs-append)
1505 (define-key dired-mode-map "\C-t." 'image-dired-display-thumb)
1506 (define-key dired-mode-map "\C-tc" 'image-dired-dired-comment-files)
1507 (define-key dired-mode-map "\C-tf" 'image-dired-mark-tagged-files)
1510 (define-key dired-mode-map [menu-bar image-dired]
1511 (cons "Image-Dired" (make-sparse-keymap "Image-Dired")))
1513 (define-key dired-mode-map [menu-bar image-dired image-dired-copy-with-exif-file-name]
1514 '("Copy with EXIF file name" . image-dired-copy-with-exif-file-name))
1516 (define-key dired-mode-map [menu-bar image-dired image-dired-dired-comment-files]
1517 '("Comment files" . image-dired-dired-comment-files))
1519 (define-key dired-mode-map [menu-bar image-dired image-dired-mark-tagged-files]
1520 '("Mark tagged files" . image-dired-mark-tagged-files))
1522 (define-key dired-mode-map [menu-bar image-dired image-dired-delete-tag]
1523 '("Remove tag from files" . image-dired-delete-tag))
1525 (define-key dired-mode-map [menu-bar image-dired image-dired-tag-files]
1526 '("Tag files" . image-dired-tag-files))
1528 (define-key dired-mode-map [menu-bar image-dired image-dired-jump-thumbnail-buffer]
1529 '("Jump to thumbnail buffer" . image-dired-jump-thumbnail-buffer))
1531 (define-key dired-mode-map [menu-bar image-dired image-dired-toggle-movement-tracking]
1532 '("Toggle movement tracking" . image-dired-toggle-movement-tracking))
1534 (define-key dired-mode-map
1535 [menu-bar image-dired image-dired-toggle-append-browsing]
1536 '("Toggle append browsing" . image-dired-toggle-append-browsing))
1538 (define-key dired-mode-map
1539 [menu-bar image-dired image-dired-toggle-disp-props]
1540 '("Toggle display properties" . image-dired-toggle-dired-display-properties))
1542 (define-key dired-mode-map
1543 [menu-bar image-dired image-dired-dired-display-external]
1544 '("Display in external viewer" . image-dired-dired-display-external))
1545 (define-key dired-mode-map
1546 [menu-bar image-dired image-dired-dired-display-image]
1547 '("Display image" . image-dired-dired-display-image))
1548 (define-key dired-mode-map
1549 [menu-bar image-dired image-dired-display-thumb]
1550 '("Display this thumbnail" . image-dired-display-thumb))
1551 (define-key dired-mode-map
1552 [menu-bar image-dired image-dired-display-thumbs-append]
1553 '("Display thumbnails append" . image-dired-display-thumbs-append))
1554 (define-key dired-mode-map
1555 [menu-bar image-dired image-dired-display-thumbs]
1556 '("Display thumbnails" . image-dired-display-thumbs))
1558 (define-key dired-mode-map
1559 [menu-bar image-dired image-dired-create-thumbs]
1560 '("Create thumbnails for marked files" . image-dired-create-thumbs))
1562 (define-key dired-mode-map
1563 [menu-bar image-dired image-dired-mark-and-display-next]
1564 '("Mark and display next" . image-dired-mark-and-display-next))
1565 (define-key dired-mode-map
1566 [menu-bar image-dired image-dired-previous-line-and-display]
1567 '("Display thumb for previous file" . image-dired-previous-line-and-display))
1568 (define-key dired-mode-map
1569 [menu-bar image-dired image-dired-next-line-and-display]
1570 '("Display thumb for next file" . image-dired-next-line-and-display)))
1572 (defun image-dired-create-thumbs (&optional arg)
1573 "Create thumbnail images for all marked files in dired.
1574 With prefix argument ARG, create thumbnails even if they already exist
1575 \(i.e. use this to refresh your thumbnails)."
1577 (let (curr-file thumb-name files count)
1578 (setq files (dired-get-marked-files))
1581 (setq thumb-name (image-dired-thumb-name curr-file))
1582 ;; If the user overrides the exist check, we must clear the
1583 ;; image cache so that if the user wants to display the
1584 ;; thumnail, it is not fetched from cache.
1586 (clear-image-cache))
1587 (if (or (not (file-exists-p thumb-name))
1589 (if (not (= 0 (image-dired-create-thumb curr-file
1590 (image-dired-thumb-name curr-file))))
1591 (error "Thumb could not be created"))))
1594 (defvar image-dired-slideshow-timer nil
1597 (defvar image-dired-slideshow-count 0
1598 "Keeping track on number of images in slideshow.")
1600 (defvar image-dired-slideshow-times 0
1601 "Number of pictures to display in slideshow.")
1603 (defun image-dired-slideshow-step ()
1604 "Step to next file, if `image-dired-slideshow-times' has not been reached."
1605 (if (< image-dired-slideshow-count image-dired-slideshow-times)
1607 (message "%s" (1+ image-dired-slideshow-count))
1608 (setq image-dired-slideshow-count (1+ image-dired-slideshow-count))
1609 (image-dired-next-line-and-display))
1610 (image-dired-slideshow-stop)))
1612 (defun image-dired-slideshow-start ()
1614 Ask user for number of images to show and the delay in between."
1616 (setq image-dired-slideshow-count 0)
1617 (setq image-dired-slideshow-times (string-to-number (read-string "How many: ")))
1618 (let ((repeat (string-to-number
1620 "Delay, in seconds. Decimals are accepted : " "1"))))
1621 (setq image-dired-slideshow-timer
1624 'image-dired-slideshow-step))))
1626 (defun image-dired-slideshow-stop ()
1629 (cancel-timer image-dired-slideshow-timer))
1631 (defun image-dired-delete-char ()
1632 "Remove current thumbnail from thumbnail buffer and line up."
1634 (let ((inhibit-read-only t))
1636 (if (looking-at " ")
1640 (defun image-dired-display-thumbs-append ()
1641 "Append thumbnails to `image-dired-thumbnail-buffer'."
1643 (image-dired-display-thumbs nil t t))
1646 (defun image-dired-display-thumb ()
1647 "Shorthand for `image-dired-display-thumbs' with prefix argument."
1649 (image-dired-display-thumbs t nil t))
1651 (defun image-dired-line-up ()
1652 "Line up thumbnails according to `image-dired-thumbs-per-row'.
1653 See also `image-dired-line-up-dynamic'."
1655 (let ((inhibit-read-only t))
1656 (goto-char (point-min))
1657 (while (and (not (image-dired-image-at-point-p))
1662 (while (and (not (image-dired-image-at-point-p))
1665 (goto-char (point-min))
1669 (if (= image-dired-thumbs-per-row 1)
1672 (setq count (1+ count))
1673 (when (and (= count (- image-dired-thumbs-per-row 1))
1678 (goto-char (point-min))))
1680 (defun image-dired-line-up-dynamic ()
1681 "Line up thumbnails images dynamically.
1682 Calculate how many thumbnails fit."
1684 (let* ((char-width (frame-char-width))
1685 (width (image-dired-window-width-pixels (image-dired-thumbnail-window)))
1686 (image-dired-thumbs-per-row
1688 (+ (* 2 image-dired-thumb-relief)
1689 (* 2 image-dired-thumb-margin)
1690 image-dired-thumb-width char-width))))
1691 (image-dired-line-up)))
1693 (defun image-dired-line-up-interactive ()
1694 "Line up thumbnails interactively.
1695 Ask user how many thumbnails should be displayed per row."
1697 (let ((image-dired-thumbs-per-row
1698 (string-to-number (read-string "How many thumbs per row: "))))
1699 (if (not (> image-dired-thumbs-per-row 0))
1700 (message "Number must be greater than 0")
1701 (image-dired-line-up))))
1703 (defun image-dired-thumbnail-display-external ()
1704 "Display original image for thumbnail at point using external viewer."
1706 (let ((file (image-dired-original-file-name)))
1707 (if (not (image-dired-image-at-point-p))
1708 (message "No thumbnail at point")
1710 (message "No original file name found")
1711 (call-process shell-file-name nil nil nil shell-command-switch
1712 (format "%s \"%s\"" image-dired-external-viewer file))))))
1715 (defun image-dired-dired-display-external ()
1716 "Display file at point using an external viewer."
1718 (let ((file (dired-get-filename)))
1719 (call-process shell-file-name nil nil nil shell-command-switch
1720 (format "%s \"%s\"" image-dired-external-viewer file))))
1722 (defun image-dired-window-width-pixels (window)
1723 "Calculate WINDOW width in pixels."
1724 (* (window-width window) (frame-char-width)))
1726 (defun image-dired-window-height-pixels (window)
1727 "Calculate WINDOW height in pixels."
1728 ;; Note: The mode-line consumes one line
1729 (* (- (window-height window) 1) (frame-char-height)))
1731 (defun image-dired-display-window ()
1732 "Return window where `image-dired-display-image-buffer' is visible."
1733 (get-window-with-predicate
1735 (equal (buffer-name (window-buffer window)) image-dired-display-image-buffer))
1738 (defun image-dired-thumbnail-window ()
1739 "Return window where `image-dired-thumbnail-buffer' is visible."
1740 (get-window-with-predicate
1742 (equal (buffer-name (window-buffer window)) image-dired-thumbnail-buffer))
1745 (defun image-dired-associated-dired-buffer-window ()
1746 "Return window where associated dired buffer is visible."
1748 (if (image-dired-image-at-point-p)
1750 (setq buf (image-dired-associated-dired-buffer))
1751 (get-window-with-predicate
1753 (equal (window-buffer window) buf))))
1754 (error "No thumbnail image at point"))))
1756 (defun image-dired-display-window-width ()
1757 "Return width, in pixels, of image-dired's image display window."
1758 (- (image-dired-window-width-pixels (image-dired-display-window))
1759 image-dired-display-window-width-correction))
1761 (defun image-dired-display-window-height ()
1762 "Return height, in pixels, of image-dired's image display window."
1763 (- (image-dired-window-height-pixels (image-dired-display-window))
1764 image-dired-display-window-height-correction))
1766 (defun image-dired-display-image (file &optional original-size)
1767 "Display image FILE in image buffer.
1768 Use this when you want to display the image, semi sized, in a new
1769 window. The image is sized to fit the display window (using a
1770 temporary file, don't worry). Because of this, it will not be as
1771 quick as opening it directly, but on most modern systems it
1772 should feel snappy enough.
1774 If optional argument ORIGINAL-SIZE is non-nil, display image in its
1776 (let ((new-file (expand-file-name image-dired-temp-image-file))
1777 width height command ret
1779 (setq file (expand-file-name file))
1780 (if (not original-size)
1782 (setq width (image-dired-display-window-width))
1783 (setq height (image-dired-display-window-height))
1786 image-dired-cmd-create-temp-image-options
1788 (cons ?p image-dired-cmd-create-temp-image-program)
1792 (cons ?t new-file))))
1793 (setq ret (call-process shell-file-name nil nil nil
1794 shell-command-switch command))
1796 (error "Could not resize image")))
1797 (setq image-type (image-type-from-file-name file))
1798 (copy-file file new-file t))
1799 (with-current-buffer (image-dired-create-display-image-buffer)
1800 (let ((inhibit-read-only t))
1803 (image-dired-insert-image image-dired-temp-image-file image-type 0 0)
1804 (goto-char (point-min))
1805 (image-dired-update-property 'original-file-name file)))))
1807 (defun image-dired-display-thumbnail-original-image (&optional arg)
1808 "Display current thumbnail's original image in display buffer.
1809 See documentation for `image-dired-display-image' for more information.
1810 With prefix argument ARG, display image in its original size."
1812 (let ((file (image-dired-original-file-name)))
1813 (if (not (string-equal major-mode "image-dired-thumbnail-mode"))
1814 (message "Not in image-dired-thumbnail-mode")
1815 (if (not (image-dired-image-at-point-p))
1816 (message "No thumbnail at point")
1818 (message "No original file name found")
1819 (image-dired-create-display-image-buffer)
1820 (display-buffer image-dired-display-image-buffer)
1821 (image-dired-display-image file arg))))))
1825 (defun image-dired-dired-display-image (&optional arg)
1826 "Display current image file.
1827 See documentation for `image-dired-display-image' for more information.
1828 With prefix argument ARG, display image in its original size."
1830 (image-dired-create-display-image-buffer)
1831 (display-buffer image-dired-display-image-buffer)
1832 (image-dired-display-image (dired-get-filename) arg))
1834 (defun image-dired-image-at-point-p ()
1835 "Return true if there is a image-dired thumbnail at point."
1836 (get-text-property (point) 'image-dired-thumbnail))
1838 (defun image-dired-rotate-thumbnail (degrees)
1839 "Rotate thumbnail DEGREES degrees."
1840 (if (not (image-dired-image-at-point-p))
1841 (message "No thumbnail at point")
1842 (let ((file (image-dired-thumb-name (image-dired-original-file-name)))
1844 (setq command (format-spec
1845 image-dired-cmd-rotate-thumbnail-options
1847 (cons ?p image-dired-cmd-rotate-thumbnail-program)
1849 (cons ?t (expand-file-name file)))))
1850 (call-process shell-file-name nil nil nil shell-command-switch command)
1851 ;; Clear the cache to refresh image. I wish I could just refresh
1852 ;; the current file but I do not know how to do that. Yet...
1853 (clear-image-cache))))
1855 (defun image-dired-rotate-thumbnail-left ()
1856 "Rotate thumbnail left (counter clockwise) 90 degrees.
1857 The result of the rotation is displayed in the image display area
1858 and a confirmation is needed before the original image files is
1859 overwritten. This confirmation can be turned off using
1860 `image-dired-rotate-original-ask-before-overwrite'."
1862 (image-dired-rotate-thumbnail "270"))
1864 (defun image-dired-rotate-thumbnail-right ()
1865 "Rotate thumbnail counter right (clockwise) 90 degrees.
1866 The result of the rotation is displayed in the image display area
1867 and a confirmation is needed before the original image files is
1868 overwritten. This confirmation can be turned off using
1869 `image-dired-rotate-original-ask-before-overwrite'."
1871 (image-dired-rotate-thumbnail "90"))
1873 (defun image-dired-refresh-thumb ()
1874 "Force creation of new image for current thumbnail."
1876 (let ((file (image-dired-original-file-name)))
1878 (image-dired-create-thumb file (image-dired-thumb-name file))))
1880 (defun image-dired-rotate-original (degrees)
1881 "Rotate original image DEGREES degrees."
1882 (if (not (image-dired-image-at-point-p))
1883 (message "No image at point")
1884 (let ((file (image-dired-original-file-name))
1886 (if (not (string-match "\.[jJ][pP[eE]?[gG]$" file))
1887 (error "Only JPEG images can be rotated!"))
1888 (setq command (format-spec
1889 image-dired-cmd-rotate-original-options
1891 (cons ?p image-dired-cmd-rotate-original-program)
1893 (cons ?o (expand-file-name file))
1894 (cons ?t image-dired-temp-rotate-image-file))))
1895 (if (not (= 0 (call-process shell-file-name nil nil nil
1896 shell-command-switch command)))
1897 (error "Could not rotate image")
1898 (image-dired-display-image image-dired-temp-rotate-image-file)
1899 (if (or (and image-dired-rotate-original-ask-before-overwrite
1901 "Rotate to temp file OK. Overwrite original image? "))
1902 (not image-dired-rotate-original-ask-before-overwrite))
1904 (copy-file image-dired-temp-rotate-image-file file t)
1905 (image-dired-refresh-thumb))
1906 (image-dired-display-image file))))))
1908 (defun image-dired-rotate-original-left ()
1909 "Rotate original image left (counter clockwise) 90 degrees."
1911 (image-dired-rotate-original "270"))
1913 (defun image-dired-rotate-original-right ()
1914 "Rotate original image right (clockwise) 90 degrees."
1916 (image-dired-rotate-original "90"))
1918 (defun image-dired-get-exif-file-name (file)
1919 "Use the image's EXIF information to return a unique file name.
1920 The file name should be unique as long as you do not take more than
1921 one picture per second. The original file name is suffixed at the end
1922 for traceability. The format of the returned file name is
1923 YYYY_MM_DD_HH_MM_DD_ORIG_FILE_NAME.jpg. Used from
1924 `image-dired-copy-with-exif-file-name'."
1925 (let (data no-exif-data-found)
1926 (if (not (string-match "\.[Jj][Pp][Ee]?[Gg]$" (expand-file-name file)))
1928 (setq no-exif-data-found t)
1932 (nth 5 (file-attributes (expand-file-name file))))))
1933 (setq data (image-dired-get-exif-data (expand-file-name file)
1934 "DateTimeOriginal")))
1935 (while (string-match "[ :]" data)
1936 (setq data (replace-match "_" nil nil data)))
1937 (format "%s%s%s" data
1938 (if no-exif-data-found
1941 (file-name-nondirectory file))))
1943 (defun image-dired-thumbnail-set-image-description ()
1944 "Set the ImageDescription EXIF tag for the original image.
1945 If the image already has a value for this tag, it is used as the
1946 default value at the prompt."
1948 (if (not (image-dired-image-at-point-p))
1949 (message "No thumbnail at point")
1950 (let* ((file (image-dired-original-file-name))
1951 (old-value (image-dired-get-exif-data file "ImageDescription")))
1953 (image-dired-set-exif-data file "ImageDescription"
1954 (read-string "Value of ImageDescription: "
1956 (message "Successfully wrote ImageDescription tag.")
1957 (error "Could not write ImageDescription tag")))))
1959 (defun image-dired-set-exif-data (file tag-name tag-value)
1960 "In FILE, set EXIF tag TAG-NAME to value TAG-VALUE."
1962 (setq command (format-spec
1963 image-dired-cmd-write-exif-data-options
1965 (cons ?p image-dired-cmd-write-exif-data-program)
1966 (cons ?f (expand-file-name file))
1968 (cons ?v tag-value))))
1969 (call-process shell-file-name nil nil nil shell-command-switch command)))
1971 (defun image-dired-get-exif-data (file tag-name)
1972 "From FILE, return EXIF tag TAG-NAME."
1973 (let ((buf (get-buffer-create "*image-dired-get-exif-data*"))
1975 (setq command (format-spec
1976 image-dired-cmd-read-exif-data-options
1978 (cons ?p image-dired-cmd-read-exif-data-program)
1980 (cons ?t tag-name))))
1981 (with-current-buffer buf
1982 (delete-region (point-min) (point-max))
1983 (if (not (eq (call-process shell-file-name nil t nil
1984 shell-command-switch command) 0))
1985 (error "Could not get EXIF tag")
1986 (goto-char (point-min))
1987 ;; Clean buffer from newlines and carriage returns before
1988 ;; getting final info
1989 (while (search-forward-regexp "[\n\r]" nil t)
1990 (replace-match "" nil t))
1991 (setq tag-value (buffer-substring (point-min) (point-max)))))
1994 (defun image-dired-copy-with-exif-file-name ()
1995 "Copy file with unique name to main image directory.
1996 Copy current or all marked files in dired to a new file in your
1997 main image directory, using a file name generated by
1998 `image-dired-get-exif-file-name'. A typical usage for this if when
1999 copying images from a digital camera into the image directory.
2001 Typically, you would open up the folder with the incoming
2002 digital images, mark the files to be copied, and execute this
2003 function. The result is a couple of new files in
2004 `image-dired-main-image-directory' called
2005 2005_05_08_12_52_00_dscn0319.jpg,
2006 2005_05_08_14_27_45_dscn0320.jpg etc."
2009 (files (dired-get-marked-files)))
2014 (file-name-as-directory
2015 (expand-file-name image-dired-main-image-directory))
2016 (image-dired-get-exif-file-name curr-file)))
2017 (message "Copying %s to %s" curr-file new-name)
2018 (copy-file curr-file new-name))
2021 (defun image-dired-display-next-thumbnail-original ()
2022 "In thubnail buffer, move to next thumbnail and display the image."
2024 (image-dired-forward-image)
2025 (image-dired-display-thumbnail-original-image))
2027 (defun image-dired-display-previous-thumbnail-original ()
2028 "Move to previous thumbnail and display image."
2030 (image-dired-backward-image)
2031 (image-dired-display-thumbnail-original-image))
2033 (defun image-dired-write-comments (file-comments)
2034 "Write file comments to database.
2035 Write file comments to one or more files. FILE-COMMENTS is an alist on
2037 ((FILE . COMMENT) ... )"
2038 (let (end comment-beg-pos comment-end-pos file comment)
2039 (with-temp-file image-dired-db-file
2040 (insert-file-contents image-dired-db-file)
2041 (dolist (elt file-comments)
2042 (setq file (car elt)
2044 (goto-char (point-min))
2045 (if (search-forward-regexp (format "^%s.*$" file) nil t)
2049 ;; Delete old comment, if any
2050 (when (search-forward ";comment:" end t)
2051 (setq comment-beg-pos (match-beginning 0))
2052 ;; Any tags after the comment?
2053 (if (search-forward ";" end t)
2054 (setq comment-end-pos (- (point) 1))
2055 (setq comment-end-pos end))
2056 ;; Delete comment tag and comment
2057 (delete-region comment-beg-pos comment-end-pos))
2058 ;; Insert new comment
2060 (unless (search-forward ";" end t)
2063 (insert (format "comment:%s;" comment)))
2064 ;; File does not exist in database - add it.
2065 (goto-char (point-max))
2066 (insert (format "\n%s;comment:%s" file comment)))))))
2068 (defun image-dired-update-property (prop value)
2069 "Update text property PROP with value VALUE at point."
2070 (let ((inhibit-read-only t))
2072 (point) (1+ (point))
2077 (defun image-dired-dired-comment-files ()
2078 "Add comment to current or marked files in dired."
2080 (let ((comment (image-dired-read-comment)))
2081 (image-dired-write-comments
2084 (cons curr-file comment))
2085 (dired-get-marked-files)))))
2087 (defun image-dired-comment-thumbnail ()
2088 "Add comment to current thumbnail in thumbnail buffer."
2090 (let* ((file (image-dired-original-file-name))
2091 (comment (image-dired-read-comment file)))
2092 (image-dired-write-comments (list (cons file comment)))
2093 (image-dired-update-property 'comment comment))
2094 (image-dired-display-thumb-properties))
2096 (defun image-dired-read-comment (&optional file)
2097 "Read comment for an image.
2098 Read comment for an image, optionally using old comment from FILE
2103 (if file (image-dired-get-comment file)))))
2106 (defun image-dired-get-comment (file)
2107 "Get comment for file FILE."
2109 (let (end buf comment-beg-pos comment-end-pos comment)
2110 (setq buf (find-file image-dired-db-file))
2111 (goto-char (point-min))
2112 (when (search-forward-regexp
2113 (format "^%s" file) nil t)
2117 (cond ((search-forward ";comment:" end t)
2118 (setq comment-beg-pos (point))
2119 (if (search-forward ";" end t)
2120 (setq comment-end-pos (- (point) 1))
2121 (setq comment-end-pos end))
2122 (setq comment (buffer-substring
2123 comment-beg-pos comment-end-pos)))))
2128 (defun image-dired-mark-tagged-files ()
2129 "Use regexp to mark files with matching tag.
2130 A `tag' is a keyword, a piece of meta data, associated with an
2131 image file and stored in image-dired's database file. This command
2132 lets you input a regexp and this will be matched against all tags
2133 on all image files in the database file. The files that have a
2134 matching tags will be marked in the dired buffer."
2136 (let ((tag (read-string "Mark tagged files (regexp): "))
2140 (setq buf (find-file image-dired-db-file))
2141 (goto-char (point-min))
2143 (while (search-forward-regexp
2144 (concat "\\(^[^;\n]+\\);.*" tag ".*$") nil t)
2145 (setq files (append (list (match-string 1)) files)))
2149 ;; I tried using `dired-mark-files-regexp' but it was
2152 ;; Don't bother about hits found in other directories than
2154 (when (string= (file-name-as-directory
2155 (expand-file-name default-directory))
2156 (file-name-as-directory
2157 (file-name-directory curr-file)))
2158 (setq curr-file (file-name-nondirectory curr-file))
2159 (goto-char (point-min))
2160 (when (search-forward-regexp (format "\\s %s$" curr-file) nil t)
2161 (setq hits (+ hits 1))
2164 (message "%d files with matching tag marked." hits)))
2166 (defun image-dired-mouse-display-image (event)
2167 "Use mouse EVENT, call `image-dired-display-image' to display image.
2168 Track this in associated dired buffer if `image-dired-track-movement' is
2172 (mouse-set-point event)
2173 (goto-char (posn-point (event-end event)))
2174 (setq file (image-dired-original-file-name))
2175 (if image-dired-track-movement
2176 (image-dired-track-original-file))
2177 (image-dired-create-display-image-buffer)
2178 (display-buffer image-dired-display-image-buffer)
2179 (image-dired-display-image file)))
2181 (defun image-dired-mouse-select-thumbnail (event)
2182 "Use mouse EVENT to select thumbnail image.
2183 Track this in associated dired buffer if `image-dired-track-movement' is
2187 (mouse-set-point event)
2188 (goto-char (posn-point (event-end event)))
2189 (if image-dired-track-movement
2190 (image-dired-track-original-file)))
2191 (image-dired-display-thumb-properties))
2193 (defun image-dired-mouse-toggle-mark (event)
2194 "Use mouse EVENT to toggle dired mark for thumbnail.
2195 Track this in associated dired buffer if `image-dired-track-movement' is
2199 (mouse-set-point event)
2200 (goto-char (posn-point (event-end event)))
2201 (if image-dired-track-movement
2202 (image-dired-track-original-file)))
2203 (image-dired-toggle-mark-thumb-original-file))
2205 (defun image-dired-dired-display-properties ()
2206 "Display properties for dired file in the echo area."
2208 (let* ((file (dired-get-filename))
2209 (file-name (file-name-nondirectory file))
2210 (dired-buf (buffer-name (current-buffer)))
2213 (image-dired-list-tags file)
2215 (comment (image-dired-get-comment file)))
2218 (image-dired-format-properties-string
2224 (defvar image-dired-tag-file-list nil
2225 "List to store tag-file structure.")
2227 (defvar image-dired-file-tag-list nil
2228 "List to store file-tag structure.")
2230 (defvar image-dired-file-comment-list nil
2231 "List to store file comments.")
2233 (defun image-dired-add-to-tag-file-list (tag file)
2234 "Add relation between TAG and FILE."
2236 (if image-dired-tag-file-list
2237 (if (setq curr (assoc tag image-dired-tag-file-list))
2238 (if (not (member file curr))
2239 (setcdr curr (cons file (cdr curr))))
2240 (setcdr image-dired-tag-file-list
2241 (cons (list tag file) (cdr image-dired-tag-file-list))))
2242 (setq image-dired-tag-file-list (list (list tag file))))))
2244 (defun image-dired-add-to-tag-file-lists (tag file)
2245 "Helper function used from `image-dired-create-gallery-lists'.
2247 Add TAG to FILE in one list and FILE to TAG in the other.
2249 Lisp structures look like the following:
2251 image-dired-file-tag-list:
2253 ((\"filename1\" \"tag1\" \"tag2\" \"tag3\" ...)
2254 (\"filename2\" \"tag1\" \"tag2\" \"tag3\" ...)
2257 image-dired-tag-file-list:
2259 ((\"tag1\" \"filename1\" \"filename2\" \"filename3\" ...)
2260 (\"tag2\" \"filename1\" \"filename2\" \"filename3\" ...)
2262 ;; Add tag to file list
2264 (if image-dired-file-tag-list
2265 (if (setq curr (assoc file image-dired-file-tag-list))
2266 (setcdr curr (cons tag (cdr curr)))
2267 (setcdr image-dired-file-tag-list
2268 (cons (list file tag) (cdr image-dired-file-tag-list))))
2269 (setq image-dired-file-tag-list (list (list file tag))))
2270 ;; Add file to tag list
2271 (if image-dired-tag-file-list
2272 (if (setq curr (assoc tag image-dired-tag-file-list))
2273 (if (not (member file curr))
2274 (setcdr curr (cons file (cdr curr))))
2275 (setcdr image-dired-tag-file-list
2276 (cons (list tag file) (cdr image-dired-tag-file-list))))
2277 (setq image-dired-tag-file-list (list (list tag file))))))
2279 (defun image-dired-add-to-file-comment-list (file comment)
2280 "Helper function used from `image-dired-create-gallery-lists'.
2282 For FILE, add COMMENT to list.
2284 Lisp structure looks like the following:
2286 image-dired-file-comment-list:
2288 ((\"filename1\" . \"comment1\")
2289 (\"filename2\" . \"comment2\")
2291 (if image-dired-file-comment-list
2292 (if (not (assoc file image-dired-file-comment-list))
2293 (setcdr image-dired-file-comment-list
2294 (cons (cons file comment)
2295 (cdr image-dired-file-comment-list))))
2296 (setq image-dired-file-comment-list (list (cons file comment)))))
2298 (defun image-dired-create-gallery-lists ()
2299 "Create temporary lists used by `image-dired-gallery-generate'."
2300 (let ((buf (find-file image-dired-db-file))
2301 end beg file row-tags)
2302 (setq image-dired-tag-file-list nil)
2303 (setq image-dired-file-tag-list nil)
2304 (setq image-dired-file-comment-list nil)
2305 (goto-char (point-min))
2306 (while (search-forward-regexp "^." nil t)
2311 (if (not (search-forward ";" end nil))
2312 (error "Something is really wrong, check format of database"))
2313 (setq row-tags (split-string
2314 (buffer-substring beg end) ";"))
2315 (setq file (car row-tags))
2318 (if (not (string-match "^comment:\\(.*\\)" x))
2319 (image-dired-add-to-tag-file-lists x file)
2320 (image-dired-add-to-file-comment-list file (match-string 1 x))))
2323 ;; Sort tag-file list
2324 (setq image-dired-tag-file-list
2325 (sort image-dired-tag-file-list
2327 (string< (car x) (car y))))))
2329 (defun image-dired-hidden-p (file)
2330 "Return t if image FILE has a \"hidden\" tag."
2334 (if (member tag image-dired-gallery-hidden-tags)
2336 (cdr (assoc file image-dired-file-tag-list)))
2339 (defun image-dired-gallery-generate ()
2340 "Generate gallery pages.
2341 First we create a couple of Lisp structures from the database to make
2342 it easier to generate, then HTML-files are created in
2343 `image-dired-gallery-dir'"
2345 (if (eq 'per-directory image-dired-thumbnail-storage)
2346 (error "Currently, gallery generation is not supported \
2347 when using per-directory thumbnail file storage"))
2348 (image-dired-create-gallery-lists)
2349 (let ((tags image-dired-tag-file-list)
2350 count curr tag index-buf tag-buf
2351 comment file-tags tag-link tag-link-list)
2352 ;; Make sure gallery root exist
2353 (if (file-exists-p image-dired-gallery-dir)
2354 (if (not (file-directory-p image-dired-gallery-dir))
2355 (error "Variable image-dired-gallery-dir is not a directory"))
2356 (make-directory image-dired-gallery-dir))
2358 (setq index-buf (find-file
2359 (format "%s/index.html" image-dired-gallery-dir)))
2362 (insert " <body>\n")
2363 (insert " <h2>Image-Dired Gallery</h2>\n")
2364 (insert (format "<p>\n Gallery generated %s\n <p>\n"
2365 (current-time-string)))
2366 (insert " <h3>Tag index</h3>\n")
2368 ;; Pre-generate list of all tag links
2371 (setq tag (car curr))
2372 (when (not (member tag image-dired-gallery-hidden-tags))
2373 (setq tag-link (format "<a href=\"%d.html\">%s</a>" count tag))
2376 (append tag-link-list (list (cons tag tag-link))))
2377 (setq tag-link-list (list (cons tag tag-link))))
2378 (setq count (1+ count))))
2381 ;; Main loop where we generated thumbnail pages per tag
2384 (setq tag (car curr))
2385 ;; Don't display hidden tags
2386 (when (not (member tag image-dired-gallery-hidden-tags))
2387 ;; Insert link to tag page in index
2388 (insert (format " %s<br>\n" (cdr (assoc tag tag-link-list))))
2389 ;; Open per-tag file
2390 (setq tag-buf (find-file
2391 (format "%s/%s.html" image-dired-gallery-dir count)))
2394 (insert " <body>\n")
2395 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2396 (insert (format " <h2>Images with tag "%s"</h2>" tag))
2397 ;; Main loop for files per tag page
2400 (when (not (image-dired-hidden-p file))
2401 ;; Insert thumbnail with link to full image
2403 (format "<a href=\"%s/%s\"><img src=\"%s/%s\"%s></a>\n"
2404 image-dired-gallery-image-root-url
2405 (file-name-nondirectory file)
2406 image-dired-gallery-thumb-image-root-url
2407 (file-name-nondirectory (image-dired-thumb-name file)) file))
2408 ;; Insert comment, if any
2409 (if (setq comment (cdr (assoc file image-dired-file-comment-list)))
2410 (insert (format "<br>\n%s<br>\n" comment))
2412 ;; Insert links to other tags, if any
2414 (setq file-tags (assoc file image-dired-file-tag-list))) 2)
2418 ;; Only insert if not file name or the main tag
2419 (if (and (not (equal extra-tag tag))
2420 (not (equal extra-tag file)))
2422 (format "%s " (cdr (assoc extra-tag tag-link-list))))))
2424 (insert "]<br>\n"))))
2426 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2427 (insert " </body>\n")
2428 (insert "</html>\n")
2430 (kill-buffer tag-buf)
2431 (setq count (1+ count))))
2433 (insert " </body>\n")
2436 (kill-buffer index-buf)))
2438 (defun image-dired-kill-buffer-and-window ()
2439 "Kill the current buffer and, if possible, also the window."
2441 (let ((buffer (current-buffer)))
2443 (delete-window (selected-window))
2445 (kill-buffer buffer)))
2447 (defvar image-dired-widget-list nil
2448 "List to keep track of meta data in edit buffer.")
2451 (defun image-dired-dired-edit-comment-and-tags ()
2452 "Edit comment and tags of current or marked image files.
2453 Edit comment and tags for all marked image files in an
2456 (setq image-dired-widget-list nil)
2458 (let ((files (dired-get-marked-files)))
2459 (switch-to-buffer "*Image-Dired Edit Meta Data*")
2460 (kill-all-local-variables)
2461 (make-local-variable 'widget-example-repeat)
2462 (let ((inhibit-read-only t))
2465 ;; Some help for the user.
2467 "\nEdit comments and tags for each image. Separate multiple tags
2468 with a comma. Move forward between fields using TAB or RET.
2469 Move to the previous field using backtab (S-TAB). Save by
2470 activating the Save button at the bottom of the form or cancel
2471 the operation by activating the Cancel button.\n\n")
2472 ;; Here comes all images and a comment and tag field for each
2474 (let (thumb-file img comment-widget tag-widget)
2476 (dolist (file files)
2478 (setq thumb-file (image-dired-thumb-name file)
2479 img (create-image thumb-file))
2482 (widget-insert "\n\nComment: ")
2483 (setq comment-widget
2484 (widget-create 'editable-field
2487 :value (or (image-dired-get-comment file) "")))
2488 (widget-insert "\nTags: ")
2490 (widget-create 'editable-field
2493 :value (or (mapconcat
2496 (image-dired-list-tags file)
2498 ;; Save information in all widgets so that we can use it when
2499 ;; the user saves the form.
2500 (setq image-dired-widget-list
2501 (append image-dired-widget-list
2502 (list (list file comment-widget tag-widget))))
2503 (widget-insert "\n\n")))
2505 ;; Footer with Save and Cancel button.
2506 (widget-insert "\n")
2507 (widget-create 'push-button
2509 (lambda (&rest ignore)
2510 (image-dired-save-information-from-widgets)
2515 (widget-create 'push-button
2517 (lambda (&rest ignore)
2519 (message "Operation canceled."))
2521 (widget-insert "\n")
2522 (use-local-map widget-keymap)
2524 ;; Jump to the first widget.
2525 (widget-forward 1)))
2527 (defun image-dired-save-information-from-widgets ()
2528 "Save information found in `image-dired-widget-list'.
2529 Use the information in `image-dired-widget-list' to save comments and
2530 tags to their respective image file. Internal function used by
2531 `image-dired-dired-edit-comment-and-tags'."
2532 (let (file comment tag-string tag-list lst)
2533 (image-dired-write-comments
2536 (setq file (car widget)
2537 comment (widget-value (cadr widget)))
2538 (cons file comment))
2539 image-dired-widget-list))
2540 (image-dired-write-tags
2541 (dolist (widget image-dired-widget-list lst)
2542 (setq file (car widget)
2543 tag-string (widget-value (car (cddr widget)))
2544 tag-list (split-string tag-string ","))
2545 (dolist (tag tag-list)
2546 (push (cons file tag) lst))))))
2548 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2549 ;;;;;;;;; TEST-SECTION ;;;;;;;;;;;
2550 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2552 ;; (defvar image-dired-dir-max-size 12300000)
2554 ;; (defun image-dired-test-clean-old-files ()
2555 ;; "Clean `image-dired-dir' from old thumbnail files.
2556 ;; \"Oldness\" measured using last access time. If the total size of all
2557 ;; thumbnail files in `image-dired-dir' is larger than 'image-dired-dir-max-size',
2558 ;; old files are deleted until the max size is reached."
2563 ;; (let ((fattribs (file-attributes f)))
2564 ;; ;; Get last access time and file size
2565 ;; `(,(nth 4 fattribs) ,(nth 7 fattribs) ,f)))
2566 ;; (directory-files (image-dired-dir) t ".+\.thumb\..+$"))
2567 ;; ;; Sort function. Compare time between two files.
2569 ;; (time-less-p (car l1) (car l2)))))
2570 ;; (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) files))))
2571 ;; (while (> dirsize image-dired-dir-max-size)
2573 ;; (format "Size of thumbnail directory: %d, delete old file %s? "
2574 ;; dirsize (cadr (cdar files))))
2575 ;; (delete-file (cadr (cdar files)))
2576 ;; (setq dirsize (- dirsize (car (cdar files))))
2577 ;; (setq files (cdr files)))))
2579 ;;;;;;;;;;;;;;;;;;;;;;,
2581 ;; (defun dired-speedbar-buttons (dired-buffer)
2582 ;; (when (and (boundp 'image-dired-use-speedbar)
2583 ;; image-dired-use-speedbar)
2584 ;; (let ((filename (with-current-buffer dired-buffer
2585 ;; (dired-get-filename))))
2586 ;; (when (and (not (string-equal filename (buffer-string)))
2587 ;; (string-match (image-file-name-regexp) filename))
2589 ;; (insert (propertize
2592 ;; (image-dired-get-thumbnail-image filename)))))))
2594 ;; (setq image-dired-use-speedbar t)
2596 (provide 'image-dired)
2598 ;; arch-tag: 9d11411d-331f-4380-8b44-8adfe3a0343e
2599 ;;; image-dired.el ends here