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 +profile \"*\" 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 +profile \"*\" 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)
1778 (setq file (expand-file-name file))
1779 (if (not original-size)
1781 (setq width (image-dired-display-window-width))
1782 (setq height (image-dired-display-window-height))
1785 image-dired-cmd-create-temp-image-options
1787 (cons ?p image-dired-cmd-create-temp-image-program)
1791 (cons ?t new-file))))
1792 (setq ret (call-process shell-file-name nil nil nil
1793 shell-command-switch command))
1795 (error "Could not resize image")))
1796 (copy-file file new-file t))
1797 (with-current-buffer (image-dired-create-display-image-buffer)
1798 (let ((inhibit-read-only t))
1801 (image-dired-insert-image image-dired-temp-image-file 'jpeg 0 0)
1802 (goto-char (point-min))
1803 (image-dired-update-property 'original-file-name file)))))
1805 (defun image-dired-display-thumbnail-original-image (&optional arg)
1806 "Display current thumbnail's original image in display buffer.
1807 See documentation for `image-dired-display-image' for more information.
1808 With prefix argument ARG, display image in its original size."
1810 (let ((file (image-dired-original-file-name)))
1811 (if (not (string-equal major-mode "image-dired-thumbnail-mode"))
1812 (message "Not in image-dired-thumbnail-mode")
1813 (if (not (image-dired-image-at-point-p))
1814 (message "No thumbnail at point")
1816 (message "No original file name found")
1817 (image-dired-create-display-image-buffer)
1818 (display-buffer image-dired-display-image-buffer)
1819 (image-dired-display-image file arg))))))
1823 (defun image-dired-dired-display-image (&optional arg)
1824 "Display current image file.
1825 See documentation for `image-dired-display-image' for more information.
1826 With prefix argument ARG, display image in its original size."
1828 (image-dired-create-display-image-buffer)
1829 (display-buffer image-dired-display-image-buffer)
1830 (image-dired-display-image (dired-get-filename) arg))
1832 (defun image-dired-image-at-point-p ()
1833 "Return true if there is a image-dired thumbnail at point."
1834 (get-text-property (point) 'image-dired-thumbnail))
1836 (defun image-dired-rotate-thumbnail (degrees)
1837 "Rotate thumbnail DEGREES degrees."
1838 (if (not (image-dired-image-at-point-p))
1839 (message "No thumbnail at point")
1840 (let ((file (image-dired-thumb-name (image-dired-original-file-name)))
1842 (setq command (format-spec
1843 image-dired-cmd-rotate-thumbnail-options
1845 (cons ?p image-dired-cmd-rotate-thumbnail-program)
1847 (cons ?t (expand-file-name file)))))
1848 (call-process shell-file-name nil nil nil shell-command-switch command)
1849 ;; Clear the cache to refresh image. I wish I could just refresh
1850 ;; the current file but I do not know how to do that. Yet...
1851 (clear-image-cache))))
1853 (defun image-dired-rotate-thumbnail-left ()
1854 "Rotate thumbnail left (counter clockwise) 90 degrees.
1855 The result of the rotation is displayed in the image display area
1856 and a confirmation is needed before the original image files is
1857 overwritten. This confirmation can be turned off using
1858 `image-dired-rotate-original-ask-before-overwrite'."
1860 (image-dired-rotate-thumbnail "270"))
1862 (defun image-dired-rotate-thumbnail-right ()
1863 "Rotate thumbnail counter right (clockwise) 90 degrees.
1864 The result of the rotation is displayed in the image display area
1865 and a confirmation is needed before the original image files is
1866 overwritten. This confirmation can be turned off using
1867 `image-dired-rotate-original-ask-before-overwrite'."
1869 (image-dired-rotate-thumbnail "90"))
1871 (defun image-dired-refresh-thumb ()
1872 "Force creation of new image for current thumbnail."
1874 (let ((file (image-dired-original-file-name)))
1876 (image-dired-create-thumb file (image-dired-thumb-name file))))
1878 (defun image-dired-rotate-original (degrees)
1879 "Rotate original image DEGREES degrees."
1880 (if (not (image-dired-image-at-point-p))
1881 (message "No image at point")
1882 (let ((file (image-dired-original-file-name))
1884 (if (not (string-match "\.[jJ][pP[eE]?[gG]$" file))
1885 (error "Only JPEG images can be rotated!"))
1886 (setq command (format-spec
1887 image-dired-cmd-rotate-original-options
1889 (cons ?p image-dired-cmd-rotate-original-program)
1891 (cons ?o (expand-file-name file))
1892 (cons ?t image-dired-temp-rotate-image-file))))
1893 (if (not (= 0 (call-process shell-file-name nil nil nil
1894 shell-command-switch command)))
1895 (error "Could not rotate image")
1896 (image-dired-display-image image-dired-temp-rotate-image-file)
1897 (if (or (and image-dired-rotate-original-ask-before-overwrite
1899 "Rotate to temp file OK. Overwrite original image? "))
1900 (not image-dired-rotate-original-ask-before-overwrite))
1902 (copy-file image-dired-temp-rotate-image-file file t)
1903 (image-dired-refresh-thumb))
1904 (image-dired-display-image file))))))
1906 (defun image-dired-rotate-original-left ()
1907 "Rotate original image left (counter clockwise) 90 degrees."
1909 (image-dired-rotate-original "270"))
1911 (defun image-dired-rotate-original-right ()
1912 "Rotate original image right (clockwise) 90 degrees."
1914 (image-dired-rotate-original "90"))
1916 (defun image-dired-get-exif-file-name (file)
1917 "Use the image's EXIF information to return a unique file name.
1918 The file name should be unique as long as you do not take more than
1919 one picture per second. The original file name is suffixed at the end
1920 for traceability. The format of the returned file name is
1921 YYYY_MM_DD_HH_MM_DD_ORIG_FILE_NAME.jpg. Used from
1922 `image-dired-copy-with-exif-file-name'."
1923 (let (data no-exif-data-found)
1924 (if (not (string-match "\.[Jj][Pp][Ee]?[Gg]$" (expand-file-name file)))
1926 (setq no-exif-data-found t)
1930 (nth 5 (file-attributes (expand-file-name file))))))
1931 (setq data (image-dired-get-exif-data (expand-file-name file)
1932 "DateTimeOriginal")))
1933 (while (string-match "[ :]" data)
1934 (setq data (replace-match "_" nil nil data)))
1935 (format "%s%s%s" data
1936 (if no-exif-data-found
1939 (file-name-nondirectory file))))
1941 (defun image-dired-thumbnail-set-image-description ()
1942 "Set the ImageDescription EXIF tag for the original image.
1943 If the image already has a value for this tag, it is used as the
1944 default value at the prompt."
1946 (if (not (image-dired-image-at-point-p))
1947 (message "No thumbnail at point")
1948 (let* ((file (image-dired-original-file-name))
1949 (old-value (image-dired-get-exif-data file "ImageDescription")))
1951 (image-dired-set-exif-data file "ImageDescription"
1952 (read-string "Value of ImageDescription: "
1954 (message "Successfully wrote ImageDescription tag.")
1955 (error "Could not write ImageDescription tag")))))
1957 (defun image-dired-set-exif-data (file tag-name tag-value)
1958 "In FILE, set EXIF tag TAG-NAME to value TAG-VALUE."
1960 (setq command (format-spec
1961 image-dired-cmd-write-exif-data-options
1963 (cons ?p image-dired-cmd-write-exif-data-program)
1964 (cons ?f (expand-file-name file))
1966 (cons ?v tag-value))))
1967 (call-process shell-file-name nil nil nil shell-command-switch command)))
1969 (defun image-dired-get-exif-data (file tag-name)
1970 "From FILE, return EXIF tag TAG-NAME."
1971 (let ((buf (get-buffer-create "*image-dired-get-exif-data*"))
1973 (setq command (format-spec
1974 image-dired-cmd-read-exif-data-options
1976 (cons ?p image-dired-cmd-read-exif-data-program)
1978 (cons ?t tag-name))))
1979 (with-current-buffer buf
1980 (delete-region (point-min) (point-max))
1981 (if (not (eq (call-process shell-file-name nil t nil
1982 shell-command-switch command) 0))
1983 (error "Could not get EXIF tag")
1984 (goto-char (point-min))
1985 ;; Clean buffer from newlines and carriage returns before
1986 ;; getting final info
1987 (while (search-forward-regexp "[\n\r]" nil t)
1988 (replace-match "" nil t))
1989 (setq tag-value (buffer-substring (point-min) (point-max)))))
1992 (defun image-dired-copy-with-exif-file-name ()
1993 "Copy file with unique name to main image directory.
1994 Copy current or all marked files in dired to a new file in your
1995 main image directory, using a file name generated by
1996 `image-dired-get-exif-file-name'. A typical usage for this if when
1997 copying images from a digital camera into the image directory.
1999 Typically, you would open up the folder with the incoming
2000 digital images, mark the files to be copied, and execute this
2001 function. The result is a couple of new files in
2002 `image-dired-main-image-directory' called
2003 2005_05_08_12_52_00_dscn0319.jpg,
2004 2005_05_08_14_27_45_dscn0320.jpg etc."
2007 (files (dired-get-marked-files)))
2012 (file-name-as-directory
2013 (expand-file-name image-dired-main-image-directory))
2014 (image-dired-get-exif-file-name curr-file)))
2015 (message "Copying %s to %s" curr-file new-name)
2016 (copy-file curr-file new-name))
2019 (defun image-dired-display-next-thumbnail-original ()
2020 "In thubnail buffer, move to next thumbnail and display the image."
2022 (image-dired-forward-image)
2023 (image-dired-display-thumbnail-original-image))
2025 (defun image-dired-display-previous-thumbnail-original ()
2026 "Move to previous thumbnail and display image."
2028 (image-dired-backward-image)
2029 (image-dired-display-thumbnail-original-image))
2031 (defun image-dired-write-comments (file-comments)
2032 "Write file comments to database.
2033 Write file comments to one or more files. FILE-COMMENTS is an alist on
2035 ((FILE . COMMENT) ... )"
2036 (let (end comment-beg-pos comment-end-pos file comment)
2037 (with-temp-file image-dired-db-file
2038 (insert-file-contents image-dired-db-file)
2039 (dolist (elt file-comments)
2040 (setq file (car elt)
2042 (goto-char (point-min))
2043 (if (search-forward-regexp (format "^%s.*$" file) nil t)
2047 ;; Delete old comment, if any
2048 (when (search-forward ";comment:" end t)
2049 (setq comment-beg-pos (match-beginning 0))
2050 ;; Any tags after the comment?
2051 (if (search-forward ";" end t)
2052 (setq comment-end-pos (- (point) 1))
2053 (setq comment-end-pos end))
2054 ;; Delete comment tag and comment
2055 (delete-region comment-beg-pos comment-end-pos))
2056 ;; Insert new comment
2058 (unless (search-forward ";" end t)
2061 (insert (format "comment:%s;" comment)))
2062 ;; File does not exist in database - add it.
2063 (goto-char (point-max))
2064 (insert (format "\n%s;comment:%s" file comment)))))))
2066 (defun image-dired-update-property (prop value)
2067 "Update text property PROP with value VALUE at point."
2068 (let ((inhibit-read-only t))
2070 (point) (1+ (point))
2075 (defun image-dired-dired-comment-files ()
2076 "Add comment to current or marked files in dired."
2078 (let ((comment (image-dired-read-comment)))
2079 (image-dired-write-comments
2082 (cons curr-file comment))
2083 (dired-get-marked-files)))))
2085 (defun image-dired-comment-thumbnail ()
2086 "Add comment to current thumbnail in thumbnail buffer."
2088 (let* ((file (image-dired-original-file-name))
2089 (comment (image-dired-read-comment file)))
2090 (image-dired-write-comments (list (cons file comment)))
2091 (image-dired-update-property 'comment comment))
2092 (image-dired-display-thumb-properties))
2094 (defun image-dired-read-comment (&optional file)
2095 "Read comment for an image.
2096 Read comment for an image, optionally using old comment from FILE
2101 (if file (image-dired-get-comment file)))))
2104 (defun image-dired-get-comment (file)
2105 "Get comment for file FILE."
2107 (let (end buf comment-beg-pos comment-end-pos comment)
2108 (setq buf (find-file image-dired-db-file))
2109 (goto-char (point-min))
2110 (when (search-forward-regexp
2111 (format "^%s" file) nil t)
2115 (cond ((search-forward ";comment:" end t)
2116 (setq comment-beg-pos (point))
2117 (if (search-forward ";" end t)
2118 (setq comment-end-pos (- (point) 1))
2119 (setq comment-end-pos end))
2120 (setq comment (buffer-substring
2121 comment-beg-pos comment-end-pos)))))
2126 (defun image-dired-mark-tagged-files ()
2127 "Use regexp to mark files with matching tag.
2128 A `tag' is a keyword, a piece of meta data, associated with an
2129 image file and stored in image-dired's database file. This command
2130 lets you input a regexp and this will be matched against all tags
2131 on all image files in the database file. The files that have a
2132 matching tags will be marked in the dired buffer."
2134 (let ((tag (read-string "Mark tagged files (regexp): "))
2138 (setq buf (find-file image-dired-db-file))
2139 (goto-char (point-min))
2141 (while (search-forward-regexp
2142 (concat "\\(^[^;\n]+\\);.*" tag ".*$") nil t)
2143 (setq files (append (list (match-string 1)) files)))
2147 ;; I tried using `dired-mark-files-regexp' but it was
2150 ;; Don't bother about hits found in other directories than
2152 (when (string= (file-name-as-directory
2153 (expand-file-name default-directory))
2154 (file-name-as-directory
2155 (file-name-directory curr-file)))
2156 (setq curr-file (file-name-nondirectory curr-file))
2157 (goto-char (point-min))
2158 (when (search-forward-regexp (format "\\s %s$" curr-file) nil t)
2159 (setq hits (+ hits 1))
2162 (message "%d files with matching tag marked." hits)))
2164 (defun image-dired-mouse-display-image (event)
2165 "Use mouse EVENT, call `image-dired-display-image' to display image.
2166 Track this in associated dired buffer if `image-dired-track-movement' is
2170 (mouse-set-point event)
2171 (goto-char (posn-point (event-end event)))
2172 (setq file (image-dired-original-file-name))
2173 (if image-dired-track-movement
2174 (image-dired-track-original-file))
2175 (image-dired-create-display-image-buffer)
2176 (display-buffer image-dired-display-image-buffer)
2177 (image-dired-display-image file)))
2179 (defun image-dired-mouse-select-thumbnail (event)
2180 "Use mouse EVENT to select thumbnail image.
2181 Track this in associated dired buffer if `image-dired-track-movement' is
2185 (mouse-set-point event)
2186 (goto-char (posn-point (event-end event)))
2187 (if image-dired-track-movement
2188 (image-dired-track-original-file)))
2189 (image-dired-display-thumb-properties))
2191 (defun image-dired-mouse-toggle-mark (event)
2192 "Use mouse EVENT to toggle dired mark for thumbnail.
2193 Track this in associated dired buffer if `image-dired-track-movement' is
2197 (mouse-set-point event)
2198 (goto-char (posn-point (event-end event)))
2199 (if image-dired-track-movement
2200 (image-dired-track-original-file)))
2201 (image-dired-toggle-mark-thumb-original-file))
2203 (defun image-dired-dired-display-properties ()
2204 "Display properties for dired file in the echo area."
2206 (let* ((file (dired-get-filename))
2207 (file-name (file-name-nondirectory file))
2208 (dired-buf (buffer-name (current-buffer)))
2211 (image-dired-list-tags file)
2213 (comment (image-dired-get-comment file)))
2216 (image-dired-format-properties-string
2222 (defvar image-dired-tag-file-list nil
2223 "List to store tag-file structure.")
2225 (defvar image-dired-file-tag-list nil
2226 "List to store file-tag structure.")
2228 (defvar image-dired-file-comment-list nil
2229 "List to store file comments.")
2231 (defun image-dired-add-to-tag-file-list (tag file)
2232 "Add relation between TAG and FILE."
2234 (if image-dired-tag-file-list
2235 (if (setq curr (assoc tag image-dired-tag-file-list))
2236 (if (not (member file curr))
2237 (setcdr curr (cons file (cdr curr))))
2238 (setcdr image-dired-tag-file-list
2239 (cons (list tag file) (cdr image-dired-tag-file-list))))
2240 (setq image-dired-tag-file-list (list (list tag file))))))
2242 (defun image-dired-add-to-tag-file-lists (tag file)
2243 "Helper function used from `image-dired-create-gallery-lists'.
2245 Add TAG to FILE in one list and FILE to TAG in the other.
2247 Lisp structures look like the following:
2249 image-dired-file-tag-list:
2251 ((\"filename1\" \"tag1\" \"tag2\" \"tag3\" ...)
2252 (\"filename2\" \"tag1\" \"tag2\" \"tag3\" ...)
2255 image-dired-tag-file-list:
2257 ((\"tag1\" \"filename1\" \"filename2\" \"filename3\" ...)
2258 (\"tag2\" \"filename1\" \"filename2\" \"filename3\" ...)
2260 ;; Add tag to file list
2262 (if image-dired-file-tag-list
2263 (if (setq curr (assoc file image-dired-file-tag-list))
2264 (setcdr curr (cons tag (cdr curr)))
2265 (setcdr image-dired-file-tag-list
2266 (cons (list file tag) (cdr image-dired-file-tag-list))))
2267 (setq image-dired-file-tag-list (list (list file tag))))
2268 ;; Add file to tag list
2269 (if image-dired-tag-file-list
2270 (if (setq curr (assoc tag image-dired-tag-file-list))
2271 (if (not (member file curr))
2272 (setcdr curr (cons file (cdr curr))))
2273 (setcdr image-dired-tag-file-list
2274 (cons (list tag file) (cdr image-dired-tag-file-list))))
2275 (setq image-dired-tag-file-list (list (list tag file))))))
2277 (defun image-dired-add-to-file-comment-list (file comment)
2278 "Helper function used from `image-dired-create-gallery-lists'.
2280 For FILE, add COMMENT to list.
2282 Lisp structure looks like the following:
2284 image-dired-file-comment-list:
2286 ((\"filename1\" . \"comment1\")
2287 (\"filename2\" . \"comment2\")
2289 (if image-dired-file-comment-list
2290 (if (not (assoc file image-dired-file-comment-list))
2291 (setcdr image-dired-file-comment-list
2292 (cons (cons file comment)
2293 (cdr image-dired-file-comment-list))))
2294 (setq image-dired-file-comment-list (list (cons file comment)))))
2296 (defun image-dired-create-gallery-lists ()
2297 "Create temporary lists used by `image-dired-gallery-generate'."
2298 (let ((buf (find-file image-dired-db-file))
2299 end beg file row-tags)
2300 (setq image-dired-tag-file-list nil)
2301 (setq image-dired-file-tag-list nil)
2302 (setq image-dired-file-comment-list nil)
2303 (goto-char (point-min))
2304 (while (search-forward-regexp "^." nil t)
2309 (if (not (search-forward ";" end nil))
2310 (error "Something is really wrong, check format of database"))
2311 (setq row-tags (split-string
2312 (buffer-substring beg end) ";"))
2313 (setq file (car row-tags))
2316 (if (not (string-match "^comment:\\(.*\\)" x))
2317 (image-dired-add-to-tag-file-lists x file)
2318 (image-dired-add-to-file-comment-list file (match-string 1 x))))
2321 ;; Sort tag-file list
2322 (setq image-dired-tag-file-list
2323 (sort image-dired-tag-file-list
2325 (string< (car x) (car y))))))
2327 (defun image-dired-hidden-p (file)
2328 "Return t if image FILE has a \"hidden\" tag."
2332 (if (member tag image-dired-gallery-hidden-tags)
2334 (cdr (assoc file image-dired-file-tag-list)))
2337 (defun image-dired-gallery-generate ()
2338 "Generate gallery pages.
2339 First we create a couple of Lisp structures from the database to make
2340 it easier to generate, then HTML-files are created in
2341 `image-dired-gallery-dir'"
2343 (if (eq 'per-directory image-dired-thumbnail-storage)
2344 (error "Currently, gallery generation is not supported \
2345 when using per-directory thumbnail file storage"))
2346 (image-dired-create-gallery-lists)
2347 (let ((tags image-dired-tag-file-list)
2348 count curr tag index-buf tag-buf
2349 comment file-tags tag-link tag-link-list)
2350 ;; Make sure gallery root exist
2351 (if (file-exists-p image-dired-gallery-dir)
2352 (if (not (file-directory-p image-dired-gallery-dir))
2353 (error "Variable image-dired-gallery-dir is not a directory"))
2354 (make-directory image-dired-gallery-dir))
2356 (setq index-buf (find-file
2357 (format "%s/index.html" image-dired-gallery-dir)))
2360 (insert " <body>\n")
2361 (insert " <h2>Image-Dired Gallery</h2>\n")
2362 (insert (format "<p>\n Gallery generated %s\n <p>\n"
2363 (current-time-string)))
2364 (insert " <h3>Tag index</h3>\n")
2366 ;; Pre-generate list of all tag links
2369 (setq tag (car curr))
2370 (when (not (member tag image-dired-gallery-hidden-tags))
2371 (setq tag-link (format "<a href=\"%d.html\">%s</a>" count tag))
2374 (append tag-link-list (list (cons tag tag-link))))
2375 (setq tag-link-list (list (cons tag tag-link))))
2376 (setq count (1+ count))))
2379 ;; Main loop where we generated thumbnail pages per tag
2382 (setq tag (car curr))
2383 ;; Don't display hidden tags
2384 (when (not (member tag image-dired-gallery-hidden-tags))
2385 ;; Insert link to tag page in index
2386 (insert (format " %s<br>\n" (cdr (assoc tag tag-link-list))))
2387 ;; Open per-tag file
2388 (setq tag-buf (find-file
2389 (format "%s/%s.html" image-dired-gallery-dir count)))
2392 (insert " <body>\n")
2393 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2394 (insert (format " <h2>Images with tag "%s"</h2>" tag))
2395 ;; Main loop for files per tag page
2398 (when (not (image-dired-hidden-p file))
2399 ;; Insert thumbnail with link to full image
2401 (format "<a href=\"%s/%s\"><img src=\"%s/%s\"%s></a>\n"
2402 image-dired-gallery-image-root-url
2403 (file-name-nondirectory file)
2404 image-dired-gallery-thumb-image-root-url
2405 (file-name-nondirectory (image-dired-thumb-name file)) file))
2406 ;; Insert comment, if any
2407 (if (setq comment (cdr (assoc file image-dired-file-comment-list)))
2408 (insert (format "<br>\n%s<br>\n" comment))
2410 ;; Insert links to other tags, if any
2412 (setq file-tags (assoc file image-dired-file-tag-list))) 2)
2416 ;; Only insert if not file name or the main tag
2417 (if (and (not (equal extra-tag tag))
2418 (not (equal extra-tag file)))
2420 (format "%s " (cdr (assoc extra-tag tag-link-list))))))
2422 (insert "]<br>\n"))))
2424 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2425 (insert " </body>\n")
2426 (insert "</html>\n")
2428 (kill-buffer tag-buf)
2429 (setq count (1+ count))))
2431 (insert " </body>\n")
2434 (kill-buffer index-buf)))
2436 (defun image-dired-kill-buffer-and-window ()
2437 "Kill the current buffer and, if possible, also the window."
2439 (let ((buffer (current-buffer)))
2441 (delete-window (selected-window))
2443 (kill-buffer buffer)))
2445 (defvar image-dired-widget-list nil
2446 "List to keep track of meta data in edit buffer.")
2449 (defun image-dired-dired-edit-comment-and-tags ()
2450 "Edit comment and tags of current or marked image files.
2451 Edit comment and tags for all marked image files in an
2454 (setq image-dired-widget-list nil)
2456 (let ((files (dired-get-marked-files)))
2457 (switch-to-buffer "*Image-Dired Edit Meta Data*")
2458 (kill-all-local-variables)
2459 (make-local-variable 'widget-example-repeat)
2460 (let ((inhibit-read-only t))
2463 ;; Some help for the user.
2465 "\nEdit comments and tags for each image. Separate multiple tags
2466 with a comma. Move forward between fields using TAB or RET.
2467 Move to the previous field using backtab (S-TAB). Save by
2468 activating the Save button at the bottom of the form or cancel
2469 the operation by activating the Cancel button.\n\n")
2470 ;; Here comes all images and a comment and tag field for each
2472 (let (thumb-file img comment-widget tag-widget)
2474 (dolist (file files)
2476 (setq thumb-file (image-dired-thumb-name file)
2477 img (create-image thumb-file))
2480 (widget-insert "\n\nComment: ")
2481 (setq comment-widget
2482 (widget-create 'editable-field
2485 :value (or (image-dired-get-comment file) "")))
2486 (widget-insert "\nTags: ")
2488 (widget-create 'editable-field
2491 :value (or (mapconcat
2494 (image-dired-list-tags file)
2496 ;; Save information in all widgets so that we can use it when
2497 ;; the user saves the form.
2498 (setq image-dired-widget-list
2499 (append image-dired-widget-list
2500 (list (list file comment-widget tag-widget))))
2501 (widget-insert "\n\n")))
2503 ;; Footer with Save and Cancel button.
2504 (widget-insert "\n")
2505 (widget-create 'push-button
2507 (lambda (&rest ignore)
2508 (image-dired-save-information-from-widgets)
2513 (widget-create 'push-button
2515 (lambda (&rest ignore)
2517 (message "Operation canceled."))
2519 (widget-insert "\n")
2520 (use-local-map widget-keymap)
2522 ;; Jump to the first widget.
2523 (widget-forward 1)))
2525 (defun image-dired-save-information-from-widgets ()
2526 "Save information found in `image-dired-widget-list'.
2527 Use the information in `image-dired-widget-list' to save comments and
2528 tags to their respective image file. Internal function used by
2529 `image-dired-dired-edit-comment-and-tags'."
2530 (let (file comment tag-string tag-list lst)
2531 (image-dired-write-comments
2534 (setq file (car widget)
2535 comment (widget-value (cadr widget)))
2536 (cons file comment))
2537 image-dired-widget-list))
2538 (image-dired-write-tags
2539 (dolist (widget image-dired-widget-list lst)
2540 (setq file (car widget)
2541 tag-string (widget-value (car (cddr widget)))
2542 tag-list (split-string tag-string ","))
2543 (dolist (tag tag-list)
2544 (push (cons file tag) lst))))))
2546 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2547 ;;;;;;;;; TEST-SECTION ;;;;;;;;;;;
2548 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2550 ;; (defvar image-dired-dir-max-size 12300000)
2552 ;; (defun image-dired-test-clean-old-files ()
2553 ;; "Clean `image-dired-dir' from old thumbnail files.
2554 ;; \"Oldness\" measured using last access time. If the total size of all
2555 ;; thumbnail files in `image-dired-dir' is larger than 'image-dired-dir-max-size',
2556 ;; old files are deleted until the max size is reached."
2561 ;; (let ((fattribs (file-attributes f)))
2562 ;; ;; Get last access time and file size
2563 ;; `(,(nth 4 fattribs) ,(nth 7 fattribs) ,f)))
2564 ;; (directory-files (image-dired-dir) t ".+\.thumb\..+$"))
2565 ;; ;; Sort function. Compare time between two files.
2567 ;; (time-less-p (car l1) (car l2)))))
2568 ;; (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) files))))
2569 ;; (while (> dirsize image-dired-dir-max-size)
2571 ;; (format "Size of thumbnail directory: %d, delete old file %s? "
2572 ;; dirsize (cadr (cdar files))))
2573 ;; (delete-file (cadr (cdar files)))
2574 ;; (setq dirsize (- dirsize (car (cdar files))))
2575 ;; (setq files (cdr files)))))
2577 ;;;;;;;;;;;;;;;;;;;;;;,
2579 ;; (defun dired-speedbar-buttons (dired-buffer)
2580 ;; (when (and (boundp 'image-dired-use-speedbar)
2581 ;; image-dired-use-speedbar)
2582 ;; (let ((filename (with-current-buffer dired-buffer
2583 ;; (dired-get-filename))))
2584 ;; (when (and (not (string-equal filename (buffer-string)))
2585 ;; (string-match (image-file-name-regexp) filename))
2587 ;; (insert (propertize
2590 ;; (image-dired-get-thumbnail-image filename)))))))
2592 ;; (setq image-dired-use-speedbar t)
2594 (provide 'image-dired)
2596 ;; arch-tag: 9d11411d-331f-4380-8b44-8adfe3a0343e
2597 ;;; image-dired.el ends here