lisp/term/x-win.el (emacs-session-filename): Use `locate-user-emacs-file'.
[emacs.git] / lisp / image-dired.el
blobafb940fe33748d3bba4b66bb71886dcd71dbef30
1 ;;; image-dired.el --- use dired to browse and manipulate your images
2 ;;
3 ;; Copyright (C) 2005-2013 Free Software Foundation, Inc.
4 ;;
5 ;; Version: 0.4.11
6 ;; Keywords: multimedia
7 ;; Author: Mathias Dahl <mathias.rem0veth1s.dahl@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; BACKGROUND
27 ;; ==========
29 ;; I needed a program to browse, organize and tag my pictures. I got
30 ;; tired of the old gallery program I used as it did not allow
31 ;; multi-file operations easily. Also, it put things out of my
32 ;; control. Image viewing programs I tested did not allow multi-file
33 ;; operations or did not do what I wanted it to.
35 ;; So, I got the idea to use the wonderful functionality of Emacs and
36 ;; `dired' to do it. It would allow me to do almost anything I wanted,
37 ;; which is basically just to browse all my pictures in an easy way,
38 ;; letting me manipulate and tag them in various ways. `dired' already
39 ;; provide all the file handling and navigation facilities; I only
40 ;; needed to add some functions to display the images.
42 ;; I briefly tried out thumbs.el, and although it seemed more
43 ;; powerful than this package, it did not work the way I wanted to. It
44 ;; was too slow to created thumbnails of all files in a directory (I
45 ;; currently keep all my 2000+ images in the same directory) and
46 ;; browsing the thumbnail buffer was slow too. image-dired.el will not
47 ;; create thumbnails until they are needed and the browsing is done
48 ;; quickly and easily in dired. I copied a great deal of ideas and
49 ;; code from there though... :)
51 ;; `image-dired' stores the thumbnail files in `image-dired-dir'
52 ;; using the file name format ORIGNAME.thumb.ORIGEXT. For example
53 ;; ~/.emacs.d/image-dired/myimage01.thumb.jpg. The "database" is for
54 ;; now just a plain text file with the following format:
56 ;; file-name-non-directory;comment:comment-text;tag1;tag2;tag3;...;tagN
59 ;; PREREQUISITES
60 ;; =============
62 ;; * The ImageMagick package. Currently, `convert' and `mogrify' are
63 ;; used. Find it here: http://www.imagemagick.org.
65 ;; * For non-lossy rotation of JPEG images, the JpegTRAN program is
66 ;; needed.
68 ;; * For `image-dired-get-exif-data' and `image-dired-write-exif-data' to work,
69 ;; the command line tool `exiftool' is needed. It can be found here:
70 ;; http://www.sno.phy.queensu.ca/~phil/exiftool/. These two functions
71 ;; are, among other things, used for writing comments to image files
72 ;; using `image-dired-thumbnail-set-image-description' and to create
73 ;; "unique" file names using `image-dired-get-exif-file-name' (used by
74 ;; `image-dired-copy-with-exif-file-name').
77 ;; USAGE
78 ;; =====
80 ;; This information has been moved to the manual. Type `C-h r' to open
81 ;; the Emacs manual and go to the node Thumbnails by typing `g
82 ;; Thumbnails RET'.
84 ;; Quickstart: M-x image-dired RET DIRNAME RET
86 ;; where DIRNAME is a directory containing image files.
88 ;; LIMITATIONS
89 ;; ===========
91 ;; * Supports all image formats that Emacs and convert supports, but
92 ;; the thumbnails are hard-coded to JPEG format.
94 ;; * WARNING: The "database" format used might be changed so keep a
95 ;; backup of `image-dired-db-file' when testing new versions.
98 ;; TODO
99 ;; ====
101 ;; * Support gallery creation when using per-directory thumbnail
102 ;; storage.
104 ;; * Some sort of auto-rotate function based on rotate info in the
105 ;; EXIF data.
107 ;; * Check if exiftool exist before trying to call it to give a better
108 ;; error message.
110 ;; * Investigate if it is possible to also write the tags to the image
111 ;; files.
113 ;; * From thumbs.el: Add an option for clean-up/max-size functionality
114 ;; for thumbnail directory.
116 ;; * From thumbs.el: Add setroot function.
118 ;; * From thumbs.el: Add image resizing, if useful (image-dired's automatic
119 ;; "image fit" might be enough)
121 ;; * From thumbs.el: Add the "modify" commands (emboss, negate,
122 ;; monochrome etc).
124 ;; * Asynchronous creation of thumbnails.
126 ;; * Add `image-dired-display-thumbs-ring' and functions to cycle that. Find
127 ;; out which is best, saving old batch just before inserting new, or
128 ;; saving the current batch in the ring when inserting it. Adding it
129 ;; probably needs rewriting `image-dired-display-thumbs' to be more general.
131 ;; * Find some way of toggling on and off really nice keybindings in
132 ;; dired (for example, using C-n or <down> instead of C-S-n). Richard
133 ;; suggested that we could keep C-t as prefix for image-dired commands
134 ;; as it is currently not used in dired. He also suggested that
135 ;; `dired-next-line' and `dired-previous-line' figure out if
136 ;; image-dired is enabled in the current buffer and, if it is, call
137 ;; `image-dired-dired-next-line' and
138 ;; `image-dired-dired-previous-line', respectively. Update: This is
139 ;; partly done; some bindings have now been added to dired.
141 ;; * Enhanced gallery creation with basic CSS-support and pagination
142 ;; of tag pages with many pictures.
144 ;; * Rewrite `image-dired-modify-mark-on-thumb-original-file' to be
145 ;; less ugly.
147 ;; * In some way keep track of buffers and windows and stuff so that
148 ;; it works as the user expects.
150 ;; * More/better documentation
153 ;;; Code:
155 (require 'dired)
156 (require 'format-spec)
157 (require 'widget)
159 (require 'cl-lib)
161 (eval-when-compile
162 (require 'wid-edit))
164 (defgroup image-dired nil
165 "Use dired to browse your images as thumbnails, and more."
166 :prefix "image-dired-"
167 :group 'multimedia)
169 (defcustom image-dired-dir (locate-user-emacs-file "image-dired/")
170 "Directory where thumbnail images are stored."
171 :type 'string
172 :group 'image-dired)
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))
188 :group 'image-dired)
190 (defcustom image-dired-db-file
191 (expand-file-name ".image-dired_db" image-dired-dir)
192 "Database file where file names and their associated tags are stored."
193 :type 'string
194 :group 'image-dired)
196 (defcustom image-dired-temp-image-file
197 (expand-file-name ".image-dired_temp" image-dired-dir)
198 "Name of temporary image file used by various commands."
199 :type 'string
200 :group 'image-dired)
202 (defcustom image-dired-gallery-dir
203 (expand-file-name ".image-dired_gallery" image-dired-dir)
204 "Directory to store generated gallery html pages.
205 This path needs to be \"shared\" to the public so that it can access
206 the index.html page that image-dired creates."
207 :type 'string
208 :group 'image-dired)
210 (defcustom image-dired-gallery-image-root-url
211 "http://your.own.server/image-diredpics"
212 "URL where the full size images are to be found.
213 Note that this path has to be configured in your web server. Image-Dired
214 expects to find pictures in this directory."
215 :type 'string
216 :group 'image-dired)
218 (defcustom image-dired-gallery-thumb-image-root-url
219 "http://your.own.server/image-diredthumbs"
220 "URL where the thumbnail images are to be found.
221 Note that this path has to be configured in your web server. Image-Dired
222 expects to find pictures in this directory."
223 :type 'string
224 :group 'image-dired)
226 (defcustom image-dired-cmd-create-thumbnail-program
227 "convert"
228 "Executable used to create thumbnail.
229 Used together with `image-dired-cmd-create-thumbnail-options'."
230 :type 'string
231 :group 'image-dired)
233 (defcustom image-dired-cmd-create-thumbnail-options
234 "%p -size %wx%h \"%f\" -resize \"%wx%h>\" -strip jpeg:\"%t\""
235 "Format of command used to create thumbnail image.
236 Available options are %p which is replaced by
237 `image-dired-cmd-create-thumbnail-program', %w which is replaced by
238 `image-dired-thumb-width', %h which is replaced by `image-dired-thumb-height',
239 %f which is replaced by the file name of the original image and %t
240 which is replaced by the file name of the thumbnail file."
241 :type 'string
242 :group 'image-dired)
244 (defcustom image-dired-cmd-create-temp-image-program
245 "convert"
246 "Executable used to create temporary image.
247 Used together with `image-dired-cmd-create-temp-image-options'."
248 :type 'string
249 :group 'image-dired)
251 (defcustom image-dired-cmd-create-temp-image-options
252 "%p -size %wx%h \"%f\" -resize \"%wx%h>\" -strip jpeg:\"%t\""
253 "Format of command used to create temporary image for display window.
254 Available options are %p which is replaced by
255 `image-dired-cmd-create-temp-image-program', %w and %h which is replaced by
256 the calculated max size for width and height in the image display window,
257 %f which is replaced by the file name of the original image and %t which
258 is replaced by the file name of the temporary file."
259 :type 'string
260 :group 'image-dired)
262 (defcustom image-dired-cmd-pngnq-program (executable-find "pngnq")
263 "The file name of the `pngnq' program.
264 It quantizes colors of PNG images down to 256 colors."
265 :type '(choice (const :tag "Not Set" nil) string)
266 :group 'image-dired)
268 (defcustom image-dired-cmd-pngcrush-program (executable-find "pngcrush")
269 "The file name of the `pngcrush' program.
270 It optimizes the compression of PNG images. Also it adds PNG textual chunks
271 with the information required by the Thumbnail Managing Standard."
272 :type '(choice (const :tag "Not Set" nil) string)
273 :group 'image-dired)
275 (defcustom image-dired-cmd-create-standard-thumbnail-command
276 (concat
277 image-dired-cmd-create-thumbnail-program " "
278 "-size %wx%h \"%f\" "
279 (unless (or image-dired-cmd-pngcrush-program image-dired-cmd-pngnq-program)
280 (concat
281 "-set \"Thumb::MTime\" \"%m\" "
282 "-set \"Thumb::URI\" \"file://%f\" "
283 "-set \"Description\" \"Thumbnail of file://%f\" "
284 "-set \"Software\" \"" (emacs-version) "\" "))
285 "-thumbnail \"%wx%h>\" png:\"%t\""
286 (if image-dired-cmd-pngnq-program
287 (concat
288 " ; " image-dired-cmd-pngnq-program " -f \"%t\""
289 (unless image-dired-cmd-pngcrush-program
290 " ; mv %q %t")))
291 (if image-dired-cmd-pngcrush-program
292 (concat
293 (unless image-dired-cmd-pngcrush-program
294 " ; cp %t %q")
295 " ; " image-dired-cmd-pngcrush-program " -q "
296 "-text b \"Description\" \"Thumbnail of file://%f\" "
297 "-text b \"Software\" \"" (emacs-version) "\" "
298 ;; "-text b \"Thumb::Image::Height\" \"%oh\" "
299 ;; "-text b \"Thumb::Image::Mimetype\" \"%mime\" "
300 ;; "-text b \"Thumb::Image::Width\" \"%ow\" "
301 "-text b \"Thumb::MTime\" \"%m\" "
302 ;; "-text b \"Thumb::Size\" \"%b\" "
303 "-text b \"Thumb::URI\" \"file://%f\" "
304 "%q %t"
305 " ; rm %q")))
306 "Command to create thumbnails according to the Thumbnail Managing Standard."
307 :type 'string
308 :group 'image-dired)
310 (defcustom image-dired-cmd-rotate-thumbnail-program
311 "mogrify"
312 "Executable used to rotate thumbnail.
313 Used together with `image-dired-cmd-rotate-thumbnail-options'."
314 :type 'string
315 :group 'image-dired)
317 (defcustom image-dired-cmd-rotate-thumbnail-options
318 "%p -rotate %d \"%t\""
319 "Format of command used to rotate thumbnail image.
320 Available options are %p which is replaced by
321 `image-dired-cmd-rotate-thumbnail-program', %d which is replaced by the
322 number of (positive) degrees to rotate the image, normally 90 or 270
323 \(for 90 degrees right and left), %t which is replaced by the file name
324 of the thumbnail file."
325 :type 'string
326 :group 'image-dired)
328 (defcustom image-dired-cmd-rotate-original-program
329 "jpegtran"
330 "Executable used to rotate original image.
331 Used together with `image-dired-cmd-rotate-original-options'."
332 :type 'string
333 :group 'image-dired)
335 (defcustom image-dired-cmd-rotate-original-options
336 "%p -rotate %d -copy all -outfile %t \"%o\""
337 "Format of command used to rotate original image.
338 Available options are %p which is replaced by
339 `image-dired-cmd-rotate-original-program', %d which is replaced by the
340 number of (positive) degrees to rotate the image, normally 90 or
341 270 \(for 90 degrees right and left), %o which is replaced by the
342 original image file name and %t which is replaced by
343 `image-dired-temp-image-file'."
344 :type 'string
345 :group 'image-dired)
347 (defcustom image-dired-temp-rotate-image-file
348 (expand-file-name ".image-dired_rotate_temp" image-dired-dir)
349 "Temporary file for rotate operations."
350 :type 'string
351 :group 'image-dired)
353 (defcustom image-dired-rotate-original-ask-before-overwrite t
354 "Confirm overwrite of original file after rotate operation.
355 If non-nil, ask user for confirmation before overwriting the
356 original file with `image-dired-temp-rotate-image-file'."
357 :type 'boolean
358 :group 'image-dired)
360 (defcustom image-dired-cmd-write-exif-data-program
361 "exiftool"
362 "Program used to write EXIF data to image.
363 Used together with `image-dired-cmd-write-exif-data-options'."
364 :type 'string
365 :group 'image-dired)
367 (defcustom image-dired-cmd-write-exif-data-options
368 "%p -%t=\"%v\" \"%f\""
369 "Format of command used to write EXIF data.
370 Available options are %p which is replaced by
371 `image-dired-cmd-write-exif-data-program', %f which is replaced by
372 the image file name, %t which is replaced by the tag name and %v
373 which is replaced by the tag value."
374 :type 'string
375 :group 'image-dired)
377 (defcustom image-dired-cmd-read-exif-data-program
378 "exiftool"
379 "Program used to read EXIF data to image.
380 Used together with `image-dired-cmd-read-exif-data-program-options'."
381 :type 'string
382 :group 'image-dired)
384 (defcustom image-dired-cmd-read-exif-data-options
385 "%p -s -s -s -%t \"%f\""
386 "Format of command used to read EXIF data.
387 Available options are %p which is replaced by
388 `image-dired-cmd-write-exif-data-program', %f which is replaced
389 by the image file name and %t which is replaced by the tag name."
390 :type 'string
391 :group 'image-dired)
393 (defcustom image-dired-gallery-hidden-tags
394 (list "private" "hidden" "pending")
395 "List of \"hidden\" tags.
396 Used by `image-dired-gallery-generate' to leave out \"hidden\" images."
397 :type '(repeat string)
398 :group 'image-dired)
400 (defcustom image-dired-thumb-size (if (eq 'standard image-dired-thumbnail-storage) 128 100)
401 "Size of thumbnails, in pixels.
402 This is the default size for both `image-dired-thumb-width'
403 and `image-dired-thumb-height'."
404 :type 'integer
405 :group 'image-dired)
407 (defcustom image-dired-thumb-width image-dired-thumb-size
408 "Width of thumbnails, in pixels."
409 :type 'integer
410 :group 'image-dired)
412 (defcustom image-dired-thumb-height image-dired-thumb-size
413 "Height of thumbnails, in pixels."
414 :type 'integer
415 :group 'image-dired)
417 (defcustom image-dired-thumb-relief 2
418 "Size of button-like border around thumbnails."
419 :type 'integer
420 :group 'image-dired)
422 (defcustom image-dired-thumb-margin 2
423 "Size of the margin around thumbnails.
424 This is where you see the cursor."
425 :type 'integer
426 :group 'image-dired)
428 (defcustom image-dired-line-up-method 'dynamic
429 "Default method for line-up of thumbnails in thumbnail buffer.
430 Used by `image-dired-display-thumbs' and other functions that needs
431 to line-up thumbnails. Dynamic means to use the available width of
432 the window containing the thumbnail buffer, Fixed means to use
433 `image-dired-thumbs-per-row', Interactive is for asking the user,
434 and No line-up means that no automatic line-up will be done."
435 :type '(choice :tag "Default line-up method"
436 (const :tag "Dynamic" dynamic)
437 (const :tag "Fixed" fixed)
438 (const :tag "Interactive" interactive)
439 (const :tag "No line-up" none))
440 :group 'image-dired)
442 (defcustom image-dired-thumbs-per-row 3
443 "Number of thumbnails to display per row in thumb buffer."
444 :type 'integer
445 :group 'image-dired)
447 (defcustom image-dired-display-window-width-correction 1
448 "Number to be used to correct image display window width.
449 Change if the default (1) does not work (i.e. if the image does not
450 completely fit)."
451 :type 'integer
452 :group 'image-dired)
454 (defcustom image-dired-display-window-height-correction 0
455 "Number to be used to correct image display window height.
456 Change if the default (0) does not work (i.e. if the image does not
457 completely fit)."
458 :type 'integer
459 :group 'image-dired)
461 (defcustom image-dired-track-movement t
462 "The current state of the tracking and mirroring.
463 For more information, see the documentation for
464 `image-dired-toggle-movement-tracking'."
465 :type 'boolean
466 :group 'image-dired)
468 (defcustom image-dired-append-when-browsing nil
469 "Append thumbnails in thumbnail buffer when browsing.
470 If non-nil, using `image-dired-next-line-and-display' and
471 `image-dired-previous-line-and-display' will leave a trail of thumbnail
472 images in the thumbnail buffer. If you enable this and want to clean
473 the thumbnail buffer because it is filled with too many thumbnails,
474 just call `image-dired-display-thumb' to display only the image at point.
475 This value can be toggled using `image-dired-toggle-append-browsing'."
476 :type 'boolean
477 :group 'image-dired)
479 (defcustom image-dired-dired-disp-props t
480 "If non-nil, display properties for dired file when browsing.
481 Used by `image-dired-next-line-and-display',
482 `image-dired-previous-line-and-display' and `image-dired-mark-and-display-next'.
483 If the database file is large, this can slow down image browsing in
484 dired and you might want to turn it off."
485 :type 'boolean
486 :group 'image-dired)
488 (defcustom image-dired-display-properties-format "%b: %f (%t): %c"
489 "Display format for thumbnail properties.
490 %b is replaced with associated dired buffer name, %f with file name
491 \(without path) of original image file, %t with the list of tags and %c
492 with the comment."
493 :type 'string
494 :group 'image-dired)
496 (defcustom image-dired-external-viewer
497 ;; TODO: Use mailcap, dired-guess-shell-alist-default,
498 ;; dired-view-command-alist.
499 (cond ((executable-find "display"))
500 ((executable-find "xli"))
501 ((executable-find "qiv") "qiv -t"))
502 "Name of external viewer.
503 Including parameters. Used when displaying original image from
504 `image-dired-thumbnail-mode'."
505 :type 'string
506 :group 'image-dired)
508 (defcustom image-dired-main-image-directory "~/pics/"
509 "Name of main image directory, if any.
510 Used by `image-dired-copy-with-exif-file-name'."
511 :type 'string
512 :group 'image-dired)
514 (defcustom image-dired-show-all-from-dir-max-files 50
515 "Maximum number of files to show using `image-dired-show-all-from-dir'
516 before warning the user."
517 :type 'integer
518 :group 'image-dired)
520 (defmacro image-dired--with-db-file (&rest body)
521 "Run BODY in a temp buffer containing `image-dired-db-file'.
522 Return the last form in BODY."
523 `(with-temp-buffer
524 (if (file-exists-p image-dired-db-file)
525 (insert-file-contents image-dired-db-file))
526 ,@body))
528 (defun image-dired-dir ()
529 "Return the current thumbnails directory (from variable `image-dired-dir').
530 Create the thumbnails directory if it does not exist."
531 (let ((image-dired-dir (file-name-as-directory
532 (expand-file-name image-dired-dir))))
533 (unless (file-directory-p image-dired-dir)
534 (make-directory image-dired-dir t)
535 (message "Creating thumbnails directory"))
536 image-dired-dir))
538 (defun image-dired-insert-image (file type relief margin)
539 "Insert image FILE of image TYPE, using RELIEF and MARGIN, at point."
541 (let ((i `(image :type ,type
542 :file ,file
543 :relief ,relief
544 :margin ,margin)))
545 (insert-image i)))
547 (defun image-dired-get-thumbnail-image (file)
548 "Return the image descriptor for a thumbnail of image file FILE."
549 (unless (string-match (image-file-name-regexp) file)
550 (error "%s is not a valid image file" file))
551 (let ((thumb-file (image-dired-thumb-name file)))
552 (unless (and (file-exists-p thumb-file)
553 (<= (float-time (nth 5 (file-attributes file)))
554 (float-time (nth 5 (file-attributes thumb-file)))))
555 (image-dired-create-thumb file thumb-file))
556 (create-image thumb-file)
557 ;; (list 'image :type 'jpeg
558 ;; :file thumb-file
559 ;; :relief image-dired-thumb-relief :margin image-dired-thumb-margin)
562 (defun image-dired-insert-thumbnail (file original-file-name
563 associated-dired-buffer)
564 "Insert thumbnail image FILE.
565 Add text properties ORIGINAL-FILE-NAME and ASSOCIATED-DIRED-BUFFER."
566 (let (beg end)
567 (setq beg (point))
568 (image-dired-insert-image file
569 ;; TODO: this should depend on the real file type
570 (if (eq 'standard image-dired-thumbnail-storage)
571 'png 'jpeg)
572 image-dired-thumb-relief
573 image-dired-thumb-margin)
574 (setq end (point))
575 (add-text-properties
576 beg end
577 (list 'image-dired-thumbnail t
578 'original-file-name original-file-name
579 'associated-dired-buffer associated-dired-buffer
580 'tags (image-dired-list-tags original-file-name)
581 'mouse-face 'highlight
582 'comment (image-dired-get-comment original-file-name)))))
584 (defun image-dired-thumb-name (file)
585 "Return thumbnail file name for FILE.
586 Depending on the value of `image-dired-thumbnail-storage', the file
587 name will vary. For central thumbnail file storage, make a
588 MD5-hash of the image file's directory name and add that to make
589 the thumbnail file name unique. For per-directory storage, just
590 add a subdirectory. For standard storage, produce the file name
591 according to the Thumbnail Managing Standard."
592 (cond ((eq 'standard image-dired-thumbnail-storage)
593 (expand-file-name
594 (concat "~/.thumbnails/normal/"
595 (md5 (concat "file://" (expand-file-name file))) ".png")))
596 ((eq 'use-image-dired-dir image-dired-thumbnail-storage)
597 (let* ((f (expand-file-name file))
598 (md5-hash
599 ;; Is MD5 hashes fast enough? The checksum of a
600 ;; thumbnail file name need not be that
601 ;; "cryptographically" good so a faster one could
602 ;; be used here.
603 (md5 (file-name-as-directory (file-name-directory f)))))
604 (format "%s%s%s.thumb.%s"
605 (file-name-as-directory (expand-file-name (image-dired-dir)))
606 (file-name-base f)
607 (if md5-hash (concat "_" md5-hash) "")
608 (file-name-extension f))))
609 ((eq 'per-directory image-dired-thumbnail-storage)
610 (let ((f (expand-file-name file)))
611 (format "%s.image-dired/%s.thumb.%s"
612 (file-name-directory f)
613 (file-name-base f)
614 (file-name-extension f))))))
616 (defun image-dired-create-thumb (original-file thumbnail-file)
617 "For ORIGINAL-FILE, create thumbnail image named THUMBNAIL-FILE."
618 (let* ((width (int-to-string image-dired-thumb-width))
619 (height (int-to-string image-dired-thumb-height))
620 (modif-time (format "%.0f" (float-time (nth 5 (file-attributes
621 original-file)))))
622 (thumbnail-nq8-file (replace-regexp-in-string ".png\\'" "-nq8.png"
623 thumbnail-file))
624 (command
625 (format-spec
626 (if (eq 'standard image-dired-thumbnail-storage)
627 image-dired-cmd-create-standard-thumbnail-command
628 image-dired-cmd-create-thumbnail-options)
629 (list
630 (cons ?p image-dired-cmd-create-thumbnail-program)
631 (cons ?w width)
632 (cons ?h height)
633 (cons ?m modif-time)
634 (cons ?f original-file)
635 (cons ?q thumbnail-nq8-file)
636 (cons ?t thumbnail-file))))
637 thumbnail-dir)
638 (when (not (file-exists-p
639 (setq thumbnail-dir (file-name-directory thumbnail-file))))
640 (message "Creating thumbnail directory.")
641 (make-directory thumbnail-dir))
642 (call-process shell-file-name nil nil nil shell-command-switch command)))
644 ;;;###autoload
645 (defun image-dired-dired-toggle-marked-thumbs (&optional arg)
646 "Toggle thumbnails in front of file names in the dired buffer.
647 If no marked file could be found, insert or hide thumbnails on the
648 current line. ARG, if non-nil, specifies the files to use instead
649 of the marked files. If ARG is an integer, use the next ARG (or
650 previous -ARG, if ARG<0) files."
651 (interactive "P")
652 (dired-map-over-marks
653 (let* ((image-pos (dired-move-to-filename))
654 (image-file (dired-get-filename nil t))
655 thumb-file
656 overlay)
657 (when (and image-file
658 (string-match-p (image-file-name-regexp) image-file))
659 (setq thumb-file (image-dired-get-thumbnail-image image-file))
660 ;; If image is not already added, then add it.
661 (let* ((cur-ovs (overlays-in (point) (1+ (point))))
662 (thumb-ov (car (cl-remove-if-not
663 (lambda (ov) (overlay-get ov 'thumb-file))
664 cur-ovs))))
665 (if thumb-ov
666 (delete-overlay thumb-ov)
667 (put-image thumb-file image-pos)
668 (setq overlay
669 (cl-loop for o in (overlays-in (point) (1+ (point)))
670 when (overlay-get o 'put-image) collect o into ov
671 finally return (car ov)))
672 (overlay-put overlay 'image-file image-file)
673 (overlay-put overlay 'thumb-file thumb-file)))))
674 arg ; Show or hide image on ARG next files.
675 'show-progress) ; Update dired display after each image is updated.
676 (add-hook 'dired-after-readin-hook
677 'image-dired-dired-after-readin-hook nil t))
679 (defun image-dired-dired-after-readin-hook ()
680 "Relocate existing thumbnail overlays in dired buffer after reverting.
681 Move them to their corresponding files if they still exist.
682 Otherwise, delete overlays."
683 (mapc (lambda (overlay)
684 (when (overlay-get overlay 'put-image)
685 (let* ((image-file (overlay-get overlay 'image-file))
686 (image-pos (dired-goto-file image-file)))
687 (if image-pos
688 (move-overlay overlay image-pos image-pos)
689 (delete-overlay overlay)))))
690 (overlays-in (point-min) (point-max))))
692 (defun image-dired-next-line-and-display ()
693 "Move to next dired line and display thumbnail image."
694 (interactive)
695 (dired-next-line 1)
696 (image-dired-display-thumbs
697 t (or image-dired-append-when-browsing nil) t)
698 (if image-dired-dired-disp-props
699 (image-dired-dired-display-properties)))
701 (defun image-dired-previous-line-and-display ()
702 "Move to previous dired line and display thumbnail image."
703 (interactive)
704 (dired-previous-line 1)
705 (image-dired-display-thumbs
706 t (or image-dired-append-when-browsing nil) t)
707 (if image-dired-dired-disp-props
708 (image-dired-dired-display-properties)))
710 (defun image-dired-toggle-append-browsing ()
711 "Toggle `image-dired-append-when-browsing'."
712 (interactive)
713 (setq image-dired-append-when-browsing
714 (not image-dired-append-when-browsing))
715 (message "Append browsing %s."
716 (if image-dired-append-when-browsing
717 "on"
718 "off")))
720 (defun image-dired-mark-and-display-next ()
721 "Mark current file in dired and display next thumbnail image."
722 (interactive)
723 (dired-mark 1)
724 (image-dired-display-thumbs
725 t (or image-dired-append-when-browsing nil) t)
726 (if image-dired-dired-disp-props
727 (image-dired-dired-display-properties)))
729 (defun image-dired-toggle-dired-display-properties ()
730 "Toggle `image-dired-dired-disp-props'."
731 (interactive)
732 (setq image-dired-dired-disp-props
733 (not image-dired-dired-disp-props))
734 (message "Dired display properties %s."
735 (if image-dired-dired-disp-props
736 "on"
737 "off")))
739 (defvar image-dired-thumbnail-buffer "*image-dired*"
740 "Image-Dired's thumbnail buffer.")
742 (defun image-dired-create-thumbnail-buffer ()
743 "Create thumb buffer and set `image-dired-thumbnail-mode'."
744 (let ((buf (get-buffer-create image-dired-thumbnail-buffer)))
745 (with-current-buffer buf
746 (setq buffer-read-only t)
747 (if (not (eq major-mode 'image-dired-thumbnail-mode))
748 (image-dired-thumbnail-mode)))
749 buf))
751 (defvar image-dired-display-image-buffer "*image-dired-display-image*"
752 "Where larger versions of the images are display.")
754 (defun image-dired-create-display-image-buffer ()
755 "Create image display buffer and set `image-dired-display-image-mode'."
756 (let ((buf (get-buffer-create image-dired-display-image-buffer)))
757 (with-current-buffer buf
758 (setq buffer-read-only t)
759 (if (not (eq major-mode 'image-dired-display-image-mode))
760 (image-dired-display-image-mode)))
761 buf))
763 (defvar image-dired-saved-window-configuration nil
764 "Saved window configuration.")
766 ;;;###autoload
767 (defun image-dired-dired-with-window-configuration (dir &optional arg)
768 "Open directory DIR and create a default window configuration.
770 Convenience command that:
772 - Opens dired in folder DIR
773 - Splits windows in most useful (?) way
774 - Set `truncate-lines' to t
776 After the command has finished, you would typically mark some
777 image files in dired and type
778 \\[image-dired-display-thumbs] (`image-dired-display-thumbs').
780 If called with prefix argument ARG, skip splitting of windows.
782 The current window configuration is saved and can be restored by
783 calling `image-dired-restore-window-configuration'."
784 (interactive "DDirectory: \nP")
785 (let ((buf (image-dired-create-thumbnail-buffer))
786 (buf2 (image-dired-create-display-image-buffer)))
787 (setq image-dired-saved-window-configuration
788 (current-window-configuration))
789 (dired dir)
790 (delete-other-windows)
791 (when (not arg)
792 (split-window-right)
793 (setq truncate-lines t)
794 (save-excursion
795 (other-window 1)
796 (switch-to-buffer buf)
797 (select-window (split-window-below))
798 (switch-to-buffer buf2)
799 (other-window -2)))))
801 (defun image-dired-restore-window-configuration ()
802 "Restore window configuration.
803 Restore any changes to the window configuration made by calling
804 `image-dired-dired-with-window-configuration'."
805 (interactive)
806 (if image-dired-saved-window-configuration
807 (set-window-configuration image-dired-saved-window-configuration)
808 (message "No saved window configuration")))
810 ;;;###autoload
811 (defun image-dired-display-thumbs (&optional arg append do-not-pop)
812 "Display thumbnails of all marked files, in `image-dired-thumbnail-buffer'.
813 If a thumbnail image does not exist for a file, it is created on the
814 fly. With prefix argument ARG, display only thumbnail for file at
815 point (this is useful if you have marked some files but want to show
816 another one).
818 Recommended usage is to split the current frame horizontally so that
819 you have the dired buffer in the left window and the
820 `image-dired-thumbnail-buffer' buffer in the right window.
822 With optional argument APPEND, append thumbnail to thumbnail buffer
823 instead of erasing it first.
825 Optional argument DO-NOT-POP controls if `pop-to-buffer' should be
826 used or not. If non-nil, use `display-buffer' instead of
827 `pop-to-buffer'. This is used from functions like
828 `image-dired-next-line-and-display' and
829 `image-dired-previous-line-and-display' where we do not want the
830 thumbnail buffer to be selected."
831 (interactive "P")
832 (let ((buf (image-dired-create-thumbnail-buffer))
833 thumb-name files dired-buf)
834 (if arg
835 (setq files (list (dired-get-filename)))
836 (setq files (dired-get-marked-files)))
837 (setq dired-buf (current-buffer))
838 (with-current-buffer buf
839 (let ((inhibit-read-only t))
840 (if (not append)
841 (erase-buffer)
842 (goto-char (point-max)))
843 (mapc
844 (lambda (curr-file)
845 (setq thumb-name (image-dired-thumb-name curr-file))
846 (if (and (not (file-exists-p thumb-name))
847 (not (= 0 (image-dired-create-thumb curr-file thumb-name))))
848 (message "Thumb could not be created for file %s" curr-file)
849 (image-dired-insert-thumbnail thumb-name curr-file dired-buf)))
850 files))
851 (cond ((eq 'dynamic image-dired-line-up-method)
852 (image-dired-line-up-dynamic))
853 ((eq 'fixed image-dired-line-up-method)
854 (image-dired-line-up))
855 ((eq 'interactive image-dired-line-up-method)
856 (image-dired-line-up-interactive))
857 ((eq 'none image-dired-line-up-method)
858 nil)
860 (image-dired-line-up-dynamic))))
861 (if do-not-pop
862 (display-buffer image-dired-thumbnail-buffer)
863 (pop-to-buffer image-dired-thumbnail-buffer))))
865 ;;;###autoload
866 (defun image-dired-show-all-from-dir (dir)
867 "Make a preview buffer for all images in DIR and display it.
868 If the number of files in DIR matching `image-file-name-regexp'
869 exceeds `image-dired-show-all-from-dir-max-files', a warning will be
870 displayed."
871 (interactive "DDir: ")
872 (dired dir)
873 (dired-mark-files-regexp (image-file-name-regexp))
874 (let ((files (dired-get-marked-files)))
875 (if (or (<= (length files) image-dired-show-all-from-dir-max-files)
876 (and (> (length files) image-dired-show-all-from-dir-max-files)
877 (y-or-n-p
878 (format
879 "Directory contains more than %d image files. Proceed? "
880 image-dired-show-all-from-dir-max-files))))
881 (progn
882 (image-dired-display-thumbs)
883 (pop-to-buffer image-dired-thumbnail-buffer))
884 (message "Cancelled."))))
886 ;;;###autoload
887 (defalias 'image-dired 'image-dired-show-all-from-dir)
889 ;;;###autoload
890 (define-obsolete-function-alias 'tumme 'image-dired "24.4")
892 (defun image-dired-sane-db-file ()
893 "Check if `image-dired-db-file' exists.
894 If not, try to create it (including any parent directories).
895 Signal error if there are problems creating it."
896 (or (file-exists-p image-dired-db-file)
897 (let (dir buf)
898 (unless (file-directory-p (setq dir (file-name-directory
899 image-dired-db-file)))
900 (make-directory dir t))
901 (with-current-buffer (setq buf (create-file-buffer
902 image-dired-db-file))
903 (write-file image-dired-db-file))
904 (kill-buffer buf)
905 (file-exists-p image-dired-db-file))
906 (error "Could not create %s" image-dired-db-file)))
908 (defun image-dired-write-tags (file-tags)
909 "Write file tags to database.
910 Write each file and tag in FILE-TAGS to the database.
911 FILE-TAGS is an alist in the following form:
912 ((FILE . TAG) ... )"
913 (image-dired-sane-db-file)
914 (let (end file tag)
915 (image-dired--with-db-file
916 (setq buffer-file-name image-dired-db-file)
917 (dolist (elt file-tags)
918 (setq file (car elt)
919 tag (cdr elt))
920 (goto-char (point-min))
921 (if (search-forward-regexp (format "^%s.*$" file) nil t)
922 (progn
923 (setq end (point))
924 (beginning-of-line)
925 (when (not (search-forward (format ";%s" tag) end t))
926 (end-of-line)
927 (insert (format ";%s" tag))))
928 (goto-char (point-max))
929 (insert (format "\n%s;%s" file tag))))
930 (save-buffer))))
932 (defun image-dired-remove-tag (files tag)
933 "For all FILES, remove TAG from the image database."
934 (image-dired-sane-db-file)
935 (image-dired--with-db-file
936 (setq buffer-file-name image-dired-db-file)
937 (let (end)
938 (unless (listp files)
939 (if (stringp files)
940 (setq files (list files))
941 (error "Files must be a string or a list of strings!")))
942 (dolist (file files)
943 (goto-char (point-min))
944 (when (search-forward-regexp (format "^%s" file) nil t)
945 (end-of-line)
946 (setq end (point))
947 (beginning-of-line)
948 (when (search-forward-regexp (format "\\(;%s\\)" tag) end t)
949 (delete-region (match-beginning 1) (match-end 1))
950 ;; Check if file should still be in the database. If
951 ;; it has no tags or comments, it will be removed.
952 (end-of-line)
953 (setq end (point))
954 (beginning-of-line)
955 (when (not (search-forward ";" end t))
956 (kill-line 1)
957 ;; If on empty line at end of buffer
958 (and (eobp)
959 (looking-at "^$")
960 (delete-char -1)))))))
961 (save-buffer)))
963 (defun image-dired-list-tags (file)
964 "Read all tags for image FILE from the image database."
965 (image-dired-sane-db-file)
966 (image-dired--with-db-file
967 (let (end (tags ""))
968 (when (search-forward-regexp (format "^%s" file) nil t)
969 (end-of-line)
970 (setq end (point))
971 (beginning-of-line)
972 (if (search-forward ";" end t)
973 (if (search-forward "comment:" end t)
974 (if (search-forward ";" end t)
975 (setq tags (buffer-substring (point) end)))
976 (setq tags (buffer-substring (point) end)))))
977 (split-string tags ";"))))
979 ;;;###autoload
980 (defun image-dired-tag-files (arg)
981 "Tag marked file(s) in dired. With prefix ARG, tag file at point."
982 (interactive "P")
983 (let ((tag (read-string "Tags to add (separate tags with a semicolon): "))
984 files)
985 (if arg
986 (setq files (list (dired-get-filename)))
987 (setq files (dired-get-marked-files)))
988 (image-dired-write-tags
989 (mapcar
990 (lambda (x)
991 (cons x tag))
992 files))))
994 (defun image-dired-tag-thumbnail ()
995 "Tag current thumbnail."
996 (interactive)
997 (let ((tag (read-string "Tags to add (separate tags with a semicolon): ")))
998 (image-dired-write-tags (list (cons (image-dired-original-file-name) tag))))
999 (image-dired-update-property
1000 'tags (image-dired-list-tags (image-dired-original-file-name))))
1002 ;;;###autoload
1003 (defun image-dired-delete-tag (arg)
1004 "Remove tag for selected file(s).
1005 With prefix argument ARG, remove tag from file at point."
1006 (interactive "P")
1007 (let ((tag (read-string "Tag to remove: "))
1008 files)
1009 (if arg
1010 (setq files (list (dired-get-filename)))
1011 (setq files (dired-get-marked-files)))
1012 (image-dired-remove-tag files tag)))
1014 (defun image-dired-tag-thumbnail-remove ()
1015 "Remove tag from thumbnail."
1016 (interactive)
1017 (let ((tag (read-string "Tag to remove: ")))
1018 (image-dired-remove-tag (image-dired-original-file-name) tag))
1019 (image-dired-update-property
1020 'tags (image-dired-list-tags (image-dired-original-file-name))))
1022 (defun image-dired-original-file-name ()
1023 "Get original file name for thumbnail or display image at point."
1024 (get-text-property (point) 'original-file-name))
1026 (defun image-dired-associated-dired-buffer ()
1027 "Get associated dired buffer at point."
1028 (get-text-property (point) 'associated-dired-buffer))
1030 (defun image-dired-get-buffer-window (buf)
1031 "Return window where buffer BUF is."
1032 (get-window-with-predicate
1033 (lambda (window)
1034 (equal (window-buffer window) buf))
1035 nil t))
1037 (defun image-dired-track-original-file ()
1038 "Track the original file in the associated dired buffer.
1039 See documentation for `image-dired-toggle-movement-tracking'.
1040 Interactive use only useful if `image-dired-track-movement' is nil."
1041 (interactive)
1042 (let ((old-buf (current-buffer))
1043 (dired-buf (image-dired-associated-dired-buffer))
1044 (file-name (image-dired-original-file-name)))
1045 (when (and (buffer-live-p dired-buf) file-name)
1046 (set-buffer dired-buf)
1047 (if (not (dired-goto-file file-name))
1048 (message "Could not track file")
1049 (set-window-point
1050 (image-dired-get-buffer-window dired-buf) (point)))
1051 (set-buffer old-buf))))
1053 (defun image-dired-toggle-movement-tracking ()
1054 "Turn on and off `image-dired-track-movement'.
1055 Tracking of the movements between thumbnail and dired buffer so that
1056 they are \"mirrored\" in the dired buffer. When this is on, moving
1057 around in the thumbnail or dired buffer will find the matching
1058 position in the other buffer."
1059 (interactive)
1060 (setq image-dired-track-movement (not image-dired-track-movement))
1061 (message "Tracking %s" (if image-dired-track-movement "on" "off")))
1063 (defun image-dired-track-thumbnail ()
1064 "Track current dired file's thumb in `image-dired-thumbnail-buffer'.
1065 This is almost the same as what `image-dired-track-original-file' does,
1066 but the other way around."
1067 (let ((file (dired-get-filename))
1068 (old-buf (current-buffer))
1069 prop-val found)
1070 (when (get-buffer image-dired-thumbnail-buffer)
1071 (set-buffer image-dired-thumbnail-buffer)
1072 (goto-char (point-min))
1073 (while (and (not (eobp))
1074 (not found))
1075 (if (and (setq prop-val
1076 (get-text-property (point) 'original-file-name))
1077 (string= prop-val file))
1078 (setq found t))
1079 (if (not found)
1080 (forward-char 1)))
1081 (when found
1082 (set-window-point
1083 (image-dired-thumbnail-window) (point))
1084 (image-dired-display-thumb-properties))
1085 (set-buffer old-buf))))
1087 (defun image-dired-dired-next-line (&optional arg)
1088 "Call `dired-next-line', then track thumbnail.
1089 This can safely replace `dired-next-line'.
1090 With prefix argument, move ARG lines."
1091 (interactive "P")
1092 (dired-next-line (or arg 1))
1093 (if image-dired-track-movement
1094 (image-dired-track-thumbnail)))
1096 (defun image-dired-dired-previous-line (&optional arg)
1097 "Call `dired-previous-line', then track thumbnail.
1098 This can safely replace `dired-previous-line'.
1099 With prefix argument, move ARG lines."
1100 (interactive "P")
1101 (dired-previous-line (or arg 1))
1102 (if image-dired-track-movement
1103 (image-dired-track-thumbnail)))
1105 (defun image-dired-forward-image (&optional arg)
1106 "Move to next image and display properties.
1107 Optional prefix ARG says how many images to move; default is one
1108 image."
1109 (interactive "p")
1110 (let (pos (steps (or arg 1)))
1111 (dotimes (i steps)
1112 (if (and (not (eobp))
1113 (save-excursion
1114 (forward-char)
1115 (while (and (not (eobp))
1116 (not (image-dired-image-at-point-p)))
1117 (forward-char))
1118 (setq pos (point))
1119 (image-dired-image-at-point-p)))
1120 (goto-char pos)
1121 (error "At last image"))))
1122 (when image-dired-track-movement
1123 (image-dired-track-original-file))
1124 (image-dired-display-thumb-properties))
1126 (defun image-dired-backward-image (&optional arg)
1127 "Move to previous image and display properties.
1128 Optional prefix ARG says how many images to move; default is one
1129 image."
1130 (interactive "p")
1131 (let (pos (steps (or arg 1)))
1132 (dotimes (i steps)
1133 (if (and (not (bobp))
1134 (save-excursion
1135 (backward-char)
1136 (while (and (not (bobp))
1137 (not (image-dired-image-at-point-p)))
1138 (backward-char))
1139 (setq pos (point))
1140 (image-dired-image-at-point-p)))
1141 (goto-char pos)
1142 (error "At first image"))))
1143 (when image-dired-track-movement
1144 (image-dired-track-original-file))
1145 (image-dired-display-thumb-properties))
1147 (defun image-dired-next-line ()
1148 "Move to next line and display properties."
1149 (interactive)
1150 (forward-line 1)
1151 ;; If we end up in an empty spot, back up to the next thumbnail.
1152 (if (not (image-dired-image-at-point-p))
1153 (image-dired-backward-image))
1154 (if image-dired-track-movement
1155 (image-dired-track-original-file))
1156 (image-dired-display-thumb-properties))
1159 (defun image-dired-previous-line ()
1160 "Move to previous line and display properties."
1161 (interactive)
1162 (forward-line -1)
1163 ;; If we end up in an empty spot, back up to the next
1164 ;; thumbnail. This should only happen if the user deleted a
1165 ;; thumbnail and did not refresh, so it is not very common. But we
1166 ;; can handle it in a good manner, so why not?
1167 (if (not (image-dired-image-at-point-p))
1168 (image-dired-backward-image))
1169 (if image-dired-track-movement
1170 (image-dired-track-original-file))
1171 (image-dired-display-thumb-properties))
1173 (defun image-dired-format-properties-string (buf file props comment)
1174 "Format display properties.
1175 BUF is the associated dired buffer, FILE is the original image file
1176 name, PROPS is a list of tags and COMMENT is the image file's
1177 comment."
1178 (format-spec
1179 image-dired-display-properties-format
1180 (list
1181 (cons ?b (or buf ""))
1182 (cons ?f file)
1183 (cons ?t (or (princ props) ""))
1184 (cons ?c (or comment "")))))
1186 (defun image-dired-display-thumb-properties ()
1187 "Display thumbnail properties in the echo area."
1188 (if (not (eobp))
1189 (let ((file-name (file-name-nondirectory (image-dired-original-file-name)))
1190 (dired-buf (buffer-name (image-dired-associated-dired-buffer)))
1191 (props (mapconcat
1192 'princ
1193 (get-text-property (point) 'tags)
1194 ", "))
1195 (comment (get-text-property (point) 'comment)))
1196 (if file-name
1197 (message "%s"
1198 (image-dired-format-properties-string
1199 dired-buf
1200 file-name
1201 props
1202 comment))))))
1204 (defun image-dired-dired-file-marked-p ()
1205 "Check whether file on current line is marked or not."
1206 (save-excursion
1207 (beginning-of-line)
1208 (not (looking-at "^ .*$"))))
1210 (defun image-dired-modify-mark-on-thumb-original-file (command)
1211 "Modify mark in dired buffer.
1212 COMMAND is one of 'mark for marking file in dired, 'unmark for
1213 unmarking file in dired or 'flag for flagging file for delete in
1214 dired."
1215 (let ((file-name (image-dired-original-file-name))
1216 (dired-buf (image-dired-associated-dired-buffer)))
1217 (if (not (and dired-buf file-name))
1218 (message "No image, or image with correct properties, at point.")
1219 (with-current-buffer dired-buf
1220 (message "%s" file-name)
1221 (if (dired-goto-file file-name)
1222 (cond ((eq command 'mark) (dired-mark 1))
1223 ((eq command 'unmark) (dired-unmark 1))
1224 ((eq command 'toggle)
1225 (if (image-dired-dired-file-marked-p)
1226 (dired-unmark 1)
1227 (dired-mark 1)))
1228 ((eq command 'flag) (dired-flag-file-deletion 1))))))))
1230 (defun image-dired-mark-thumb-original-file ()
1231 "Mark original image file in associated dired buffer."
1232 (interactive)
1233 (image-dired-modify-mark-on-thumb-original-file 'mark)
1234 (image-dired-forward-image))
1236 (defun image-dired-unmark-thumb-original-file ()
1237 "Unmark original image file in associated dired buffer."
1238 (interactive)
1239 (image-dired-modify-mark-on-thumb-original-file 'unmark)
1240 (image-dired-forward-image))
1242 (defun image-dired-flag-thumb-original-file ()
1243 "Flag original image file for deletion in associated dired buffer."
1244 (interactive)
1245 (image-dired-modify-mark-on-thumb-original-file 'flag)
1246 (image-dired-forward-image))
1248 (defun image-dired-toggle-mark-thumb-original-file ()
1249 "Toggle mark on original image file in associated dired buffer."
1250 (interactive)
1251 (image-dired-modify-mark-on-thumb-original-file 'toggle))
1253 (defun image-dired-jump-original-dired-buffer ()
1254 "Jump to the dired buffer associated with the current image file.
1255 You probably want to use this together with
1256 `image-dired-track-original-file'."
1257 (interactive)
1258 (let ((buf (image-dired-associated-dired-buffer))
1259 window frame)
1260 (setq window (image-dired-get-buffer-window buf))
1261 (if window
1262 (progn
1263 (if (not (equal (selected-frame) (setq frame (window-frame window))))
1264 (select-frame-set-input-focus frame))
1265 (select-window window))
1266 (message "Associated dired buffer not visible"))))
1268 ;;;###autoload
1269 (defun image-dired-jump-thumbnail-buffer ()
1270 "Jump to thumbnail buffer."
1271 (interactive)
1272 (let ((window (image-dired-thumbnail-window))
1273 frame)
1274 (if window
1275 (progn
1276 (if (not (equal (selected-frame) (setq frame (window-frame window))))
1277 (select-frame-set-input-focus frame))
1278 (select-window window))
1279 (message "Thumbnail buffer not visible"))))
1281 (defvar image-dired-thumbnail-mode-map (make-sparse-keymap)
1282 "Keymap for `image-dired-thumbnail-mode'.")
1284 (defvar image-dired-thumbnail-mode-line-up-map (make-sparse-keymap)
1285 "Keymap for line-up commands in `image-dired-thumbnail-mode'.")
1287 (defvar image-dired-thumbnail-mode-tag-map (make-sparse-keymap)
1288 "Keymap for tag commands in `image-dired-thumbnail-mode'.")
1290 (defun image-dired-define-thumbnail-mode-keymap ()
1291 "Define keymap for `image-dired-thumbnail-mode'."
1293 ;; Keys
1294 (define-key image-dired-thumbnail-mode-map [right] 'image-dired-forward-image)
1295 (define-key image-dired-thumbnail-mode-map [left] 'image-dired-backward-image)
1296 (define-key image-dired-thumbnail-mode-map [up] 'image-dired-previous-line)
1297 (define-key image-dired-thumbnail-mode-map [down] 'image-dired-next-line)
1298 (define-key image-dired-thumbnail-mode-map "\C-f" 'image-dired-forward-image)
1299 (define-key image-dired-thumbnail-mode-map "\C-b" 'image-dired-backward-image)
1300 (define-key image-dired-thumbnail-mode-map "\C-p" 'image-dired-previous-line)
1301 (define-key image-dired-thumbnail-mode-map "\C-n" 'image-dired-next-line)
1303 (define-key image-dired-thumbnail-mode-map "d" 'image-dired-flag-thumb-original-file)
1304 (define-key image-dired-thumbnail-mode-map [delete]
1305 'image-dired-flag-thumb-original-file)
1306 (define-key image-dired-thumbnail-mode-map "m" 'image-dired-mark-thumb-original-file)
1307 (define-key image-dired-thumbnail-mode-map "u" 'image-dired-unmark-thumb-original-file)
1308 (define-key image-dired-thumbnail-mode-map "." 'image-dired-track-original-file)
1309 (define-key image-dired-thumbnail-mode-map [tab] 'image-dired-jump-original-dired-buffer)
1311 ;; add line-up map
1312 (define-key image-dired-thumbnail-mode-map "g" image-dired-thumbnail-mode-line-up-map)
1314 ;; map it to "g" so that the user can press it more quickly
1315 (define-key image-dired-thumbnail-mode-line-up-map "g" 'image-dired-line-up-dynamic)
1316 ;; "f" for "fixed" number of thumbs per row
1317 (define-key image-dired-thumbnail-mode-line-up-map "f" 'image-dired-line-up)
1318 ;; "i" for "interactive"
1319 (define-key image-dired-thumbnail-mode-line-up-map "i" 'image-dired-line-up-interactive)
1321 ;; add tag map
1322 (define-key image-dired-thumbnail-mode-map "t" image-dired-thumbnail-mode-tag-map)
1324 ;; map it to "t" so that the user can press it more quickly
1325 (define-key image-dired-thumbnail-mode-tag-map "t" 'image-dired-tag-thumbnail)
1326 ;; "r" for "remove"
1327 (define-key image-dired-thumbnail-mode-tag-map "r" 'image-dired-tag-thumbnail-remove)
1329 (define-key image-dired-thumbnail-mode-map "\C-m"
1330 'image-dired-display-thumbnail-original-image)
1331 (define-key image-dired-thumbnail-mode-map [C-return]
1332 'image-dired-thumbnail-display-external)
1334 (define-key image-dired-thumbnail-mode-map "l" 'image-dired-rotate-thumbnail-left)
1335 (define-key image-dired-thumbnail-mode-map "r" 'image-dired-rotate-thumbnail-right)
1337 (define-key image-dired-thumbnail-mode-map "L" 'image-dired-rotate-original-left)
1338 (define-key image-dired-thumbnail-mode-map "R" 'image-dired-rotate-original-right)
1340 (define-key image-dired-thumbnail-mode-map "D"
1341 'image-dired-thumbnail-set-image-description)
1343 (define-key image-dired-thumbnail-mode-map "\C-d" 'image-dired-delete-char)
1344 (define-key image-dired-thumbnail-mode-map " "
1345 'image-dired-display-next-thumbnail-original)
1346 (define-key image-dired-thumbnail-mode-map
1347 (kbd "DEL") 'image-dired-display-previous-thumbnail-original)
1348 (define-key image-dired-thumbnail-mode-map "c" 'image-dired-comment-thumbnail)
1349 (define-key image-dired-thumbnail-mode-map "q" 'image-dired-kill-buffer-and-window)
1351 ;; Mouse
1352 (define-key image-dired-thumbnail-mode-map [mouse-2] 'image-dired-mouse-display-image)
1353 (define-key image-dired-thumbnail-mode-map [mouse-1] 'image-dired-mouse-select-thumbnail)
1355 ;; Seems I must first set C-down-mouse-1 to undefined, or else it
1356 ;; will trigger the buffer menu. If I try to instead bind
1357 ;; C-down-mouse-1 to `image-dired-mouse-toggle-mark', I get a message
1358 ;; about C-mouse-1 not being defined afterwards. Annoying, but I
1359 ;; probably do not completely understand mouse events.
1361 (define-key image-dired-thumbnail-mode-map [C-down-mouse-1] 'undefined)
1362 (define-key image-dired-thumbnail-mode-map [C-mouse-1] 'image-dired-mouse-toggle-mark)
1364 ;; Menu
1365 (define-key image-dired-thumbnail-mode-map [menu-bar image-dired]
1366 (cons "Image-Dired" (make-sparse-keymap "Image-Dired")))
1368 (define-key image-dired-thumbnail-mode-map
1369 [menu-bar image-dired image-dired-kill-buffer-and-window]
1370 '("Quit" . image-dired-kill-buffer-and-window))
1372 (define-key image-dired-thumbnail-mode-map
1373 [menu-bar image-dired image-dired-delete-char]
1374 '("Delete thumbnail from buffer" . image-dired-delete-char))
1376 (define-key image-dired-thumbnail-mode-map
1377 [menu-bar image-dired image-dired-tag-thumbnail-remove]
1378 '("Remove tag from thumbnail" . image-dired-tag-thumbnail-remove))
1380 (define-key image-dired-thumbnail-mode-map
1381 [menu-bar image-dired image-dired-tag-thumbnail]
1382 '("Tag thumbnail" . image-dired-tag-thumbnail))
1384 (define-key image-dired-thumbnail-mode-map
1385 [menu-bar image-dired image-dired-comment-thumbnail]
1386 '("Comment thumbnail" . image-dired-comment-thumbnail))
1388 (define-key image-dired-thumbnail-mode-map
1389 [menu-bar image-dired image-dired-refresh-thumb]
1390 '("Refresh thumb" . image-dired-refresh-thumb))
1391 (define-key image-dired-thumbnail-mode-map
1392 [menu-bar image-dired image-dired-line-up-dynamic]
1393 '("Dynamic line up" . image-dired-line-up-dynamic))
1394 (define-key image-dired-thumbnail-mode-map
1395 [menu-bar image-dired image-dired-line-up]
1396 '("Line up thumbnails" . image-dired-line-up))
1398 (define-key image-dired-thumbnail-mode-map
1399 [menu-bar image-dired image-dired-rotate-thumbnail-left]
1400 '("Rotate thumbnail left" . image-dired-rotate-thumbnail-left))
1401 (define-key image-dired-thumbnail-mode-map
1402 [menu-bar image-dired image-dired-rotate-thumbnail-right]
1403 '("Rotate thumbnail right" . image-dired-rotate-thumbnail-right))
1405 (define-key image-dired-thumbnail-mode-map
1406 [menu-bar image-dired image-dired-rotate-original-left]
1407 '("Rotate original left" . image-dired-rotate-original-left))
1408 (define-key image-dired-thumbnail-mode-map
1409 [menu-bar image-dired image-dired-rotate-original-right]
1410 '("Rotate original right" . image-dired-rotate-original-right))
1412 (define-key image-dired-thumbnail-mode-map
1413 [menu-bar image-dired image-dired-toggle-movement-tracking]
1414 '("Toggle movement tracking on/off" . image-dired-toggle-movement-tracking))
1416 (define-key image-dired-thumbnail-mode-map
1417 [menu-bar image-dired image-dired-jump-original-dired-buffer]
1418 '("Jump to dired buffer" . image-dired-jump-original-dired-buffer))
1419 (define-key image-dired-thumbnail-mode-map
1420 [menu-bar image-dired image-dired-track-original-file]
1421 '("Track original" . image-dired-track-original-file))
1423 (define-key image-dired-thumbnail-mode-map
1424 [menu-bar image-dired image-dired-flag-thumb-original-file]
1425 '("Flag original for deletion" . image-dired-flag-thumb-original-file))
1426 (define-key image-dired-thumbnail-mode-map
1427 [menu-bar image-dired image-dired-unmark-thumb-original-file]
1428 '("Unmark original" . image-dired-unmark-thumb-original-file))
1429 (define-key image-dired-thumbnail-mode-map
1430 [menu-bar image-dired image-dired-mark-thumb-original-file]
1431 '("Mark original" . image-dired-mark-thumb-original-file))
1433 (define-key image-dired-thumbnail-mode-map
1434 [menu-bar image-dired image-dired-thumbnail-display-external]
1435 '("Display in external viewer" . image-dired-thumbnail-display-external))
1436 (define-key image-dired-thumbnail-mode-map
1437 [menu-bar image-dired image-dired-display-thumbnail-original-image]
1438 '("Display image" . image-dired-display-thumbnail-original-image)))
1440 (defvar image-dired-display-image-mode-map (make-sparse-keymap)
1441 "Keymap for `image-dired-display-image-mode'.")
1443 (defun image-dired-define-display-image-mode-keymap ()
1444 "Define keymap for `image-dired-display-image-mode'."
1446 ;; Keys
1447 (define-key image-dired-display-image-mode-map "q" 'image-dired-kill-buffer-and-window)
1449 (define-key image-dired-display-image-mode-map "f"
1450 'image-dired-display-current-image-full)
1452 (define-key image-dired-display-image-mode-map "s"
1453 'image-dired-display-current-image-sized)
1455 ;; Menu
1456 (define-key image-dired-display-image-mode-map [menu-bar image-dired]
1457 (cons "Image-Dired" (make-sparse-keymap "Image-Dired")))
1459 (define-key image-dired-display-image-mode-map
1460 [menu-bar image-dired image-dired-kill-buffer-and-window]
1461 '("Quit" . image-dired-kill-buffer-and-window))
1463 (define-key image-dired-display-image-mode-map
1464 [menu-bar image-dired image-dired-display-current-image-sized]
1465 '("Display original, sized to fit" . image-dired-display-current-image-sized))
1467 (define-key image-dired-display-image-mode-map
1468 [menu-bar image-dired image-dired-display-current-image-full]
1469 '("Display original, full size" . image-dired-display-current-image-full))
1473 (defun image-dired-display-current-image-full ()
1474 "Display current image in full size."
1475 (interactive)
1476 (let ((file (image-dired-original-file-name)))
1477 (if file
1478 (progn
1479 (image-dired-display-image file t)
1480 (message "Full size image displayed"))
1481 (error "No original file name at point"))))
1483 (defun image-dired-display-current-image-sized ()
1484 "Display current image in sized to fit window dimensions."
1485 (interactive)
1486 (let ((file (image-dired-original-file-name)))
1487 (if file
1488 (progn
1489 (image-dired-display-image file)
1490 (message "Full size image displayed"))
1491 (error "No original file name at point"))))
1493 (define-derived-mode image-dired-thumbnail-mode
1494 fundamental-mode "image-dired-thumbnail"
1495 "Browse and manipulate thumbnail images using dired.
1496 Use `image-dired-dired' and `image-dired-setup-dired-keybindings' to get a
1497 nice setup to start with."
1498 (image-dired-define-thumbnail-mode-keymap)
1499 (message "image-dired-thumbnail-mode enabled"))
1501 (define-derived-mode image-dired-display-image-mode
1502 fundamental-mode "image-dired-image-display"
1503 "Mode for displaying and manipulating original image.
1504 Resized or in full-size."
1505 (image-dired-define-display-image-mode-keymap)
1506 (message "image-dired-display-image-mode enabled"))
1508 ;;;###autoload
1509 (defun image-dired-setup-dired-keybindings ()
1510 "Setup easy-to-use keybindings for the commands to be used in dired mode.
1511 Note that n, p and <down> and <up> will be hijacked and bound to
1512 `image-dired-dired-x-line'."
1513 (interactive)
1515 ;; Hijack previous and next line movement. Let C-p and C-b be
1516 ;; though...
1518 (define-key dired-mode-map "p" 'image-dired-dired-previous-line)
1519 (define-key dired-mode-map "n" 'image-dired-dired-next-line)
1520 (define-key dired-mode-map [up] 'image-dired-dired-previous-line)
1521 (define-key dired-mode-map [down] 'image-dired-dired-next-line)
1523 (define-key dired-mode-map (kbd "C-S-n") 'image-dired-next-line-and-display)
1524 (define-key dired-mode-map (kbd "C-S-p") 'image-dired-previous-line-and-display)
1525 (define-key dired-mode-map (kbd "C-S-m") 'image-dired-mark-and-display-next)
1527 (define-key dired-mode-map "\C-td" 'image-dired-display-thumbs)
1528 (define-key dired-mode-map "\C-tt" 'image-dired-tag-files)
1529 (define-key dired-mode-map "\C-tr" 'image-dired-delete-tag)
1530 (define-key dired-mode-map [tab] 'image-dired-jump-thumbnail-buffer)
1531 (define-key dired-mode-map "\C-ti" 'image-dired-dired-display-image)
1532 (define-key dired-mode-map "\C-tx" 'image-dired-dired-display-external)
1533 (define-key dired-mode-map "\C-ta" 'image-dired-display-thumbs-append)
1534 (define-key dired-mode-map "\C-t." 'image-dired-display-thumb)
1535 (define-key dired-mode-map "\C-tc" 'image-dired-dired-comment-files)
1536 (define-key dired-mode-map "\C-tf" 'image-dired-mark-tagged-files)
1538 ;; Menu for dired
1539 (define-key dired-mode-map [menu-bar image-dired]
1540 (cons "Image-Dired" (make-sparse-keymap "Image-Dired")))
1542 (define-key dired-mode-map [menu-bar image-dired image-dired-copy-with-exif-file-name]
1543 '("Copy with EXIF file name" . image-dired-copy-with-exif-file-name))
1545 (define-key dired-mode-map [menu-bar image-dired image-dired-dired-comment-files]
1546 '("Comment files" . image-dired-dired-comment-files))
1548 (define-key dired-mode-map [menu-bar image-dired image-dired-mark-tagged-files]
1549 '("Mark tagged files" . image-dired-mark-tagged-files))
1551 (define-key dired-mode-map [menu-bar image-dired image-dired-delete-tag]
1552 '("Remove tag from files" . image-dired-delete-tag))
1554 (define-key dired-mode-map [menu-bar image-dired image-dired-tag-files]
1555 '("Tag files" . image-dired-tag-files))
1557 (define-key dired-mode-map [menu-bar image-dired image-dired-jump-thumbnail-buffer]
1558 '("Jump to thumbnail buffer" . image-dired-jump-thumbnail-buffer))
1560 (define-key dired-mode-map [menu-bar image-dired image-dired-toggle-movement-tracking]
1561 '("Toggle movement tracking" . image-dired-toggle-movement-tracking))
1563 (define-key dired-mode-map
1564 [menu-bar image-dired image-dired-toggle-append-browsing]
1565 '("Toggle append browsing" . image-dired-toggle-append-browsing))
1567 (define-key dired-mode-map
1568 [menu-bar image-dired image-dired-toggle-disp-props]
1569 '("Toggle display properties" . image-dired-toggle-dired-display-properties))
1571 (define-key dired-mode-map
1572 [menu-bar image-dired image-dired-dired-display-external]
1573 '("Display in external viewer" . image-dired-dired-display-external))
1574 (define-key dired-mode-map
1575 [menu-bar image-dired image-dired-dired-display-image]
1576 '("Display image" . image-dired-dired-display-image))
1577 (define-key dired-mode-map
1578 [menu-bar image-dired image-dired-display-thumb]
1579 '("Display this thumbnail" . image-dired-display-thumb))
1580 (define-key dired-mode-map
1581 [menu-bar image-dired image-dired-display-thumbs-append]
1582 '("Display thumbnails append" . image-dired-display-thumbs-append))
1583 (define-key dired-mode-map
1584 [menu-bar image-dired image-dired-display-thumbs]
1585 '("Display thumbnails" . image-dired-display-thumbs))
1587 (define-key dired-mode-map
1588 [menu-bar image-dired image-dired-create-thumbs]
1589 '("Create thumbnails for marked files" . image-dired-create-thumbs))
1591 (define-key dired-mode-map
1592 [menu-bar image-dired image-dired-mark-and-display-next]
1593 '("Mark and display next" . image-dired-mark-and-display-next))
1594 (define-key dired-mode-map
1595 [menu-bar image-dired image-dired-previous-line-and-display]
1596 '("Display thumb for previous file" . image-dired-previous-line-and-display))
1597 (define-key dired-mode-map
1598 [menu-bar image-dired image-dired-next-line-and-display]
1599 '("Display thumb for next file" . image-dired-next-line-and-display)))
1601 (declare-function clear-image-cache "image.c" (&optional filter))
1603 (defun image-dired-create-thumbs (&optional arg)
1604 "Create thumbnail images for all marked files in dired.
1605 With prefix argument ARG, create thumbnails even if they already exist
1606 \(i.e. use this to refresh your thumbnails)."
1607 (interactive "P")
1608 (let (thumb-name files)
1609 (setq files (dired-get-marked-files))
1610 (mapcar
1611 (lambda (curr-file)
1612 (setq thumb-name (image-dired-thumb-name curr-file))
1613 ;; If the user overrides the exist check, we must clear the
1614 ;; image cache so that if the user wants to display the
1615 ;; thumbnail, it is not fetched from cache.
1616 (if arg
1617 (clear-image-cache))
1618 (if (or (not (file-exists-p thumb-name))
1619 arg)
1620 (if (not (= 0 (image-dired-create-thumb curr-file
1621 (image-dired-thumb-name curr-file))))
1622 (error "Thumb could not be created"))))
1623 files)))
1625 (defvar image-dired-slideshow-timer nil
1626 "Slideshow timer.")
1628 (defvar image-dired-slideshow-count 0
1629 "Keeping track on number of images in slideshow.")
1631 (defvar image-dired-slideshow-times 0
1632 "Number of pictures to display in slideshow.")
1634 (defun image-dired-slideshow-step ()
1635 "Step to next file, if `image-dired-slideshow-times' has not been reached."
1636 (if (< image-dired-slideshow-count image-dired-slideshow-times)
1637 (progn
1638 (message "%s" (1+ image-dired-slideshow-count))
1639 (setq image-dired-slideshow-count (1+ image-dired-slideshow-count))
1640 (image-dired-next-line-and-display))
1641 (image-dired-slideshow-stop)))
1643 (defun image-dired-slideshow-start ()
1644 "Start slideshow.
1645 Ask user for number of images to show and the delay in between."
1646 (interactive)
1647 (setq image-dired-slideshow-count 0)
1648 (setq image-dired-slideshow-times (string-to-number (read-string "How many: ")))
1649 (let ((repeat (string-to-number
1650 (read-string
1651 "Delay, in seconds. Decimals are accepted : " "1"))))
1652 (setq image-dired-slideshow-timer
1653 (run-with-timer
1654 0 repeat
1655 'image-dired-slideshow-step))))
1657 (defun image-dired-slideshow-stop ()
1658 "Cancel slideshow."
1659 (interactive)
1660 (cancel-timer image-dired-slideshow-timer))
1662 (defun image-dired-delete-char ()
1663 "Remove current thumbnail from thumbnail buffer and line up."
1664 (interactive)
1665 (let ((inhibit-read-only t))
1666 (delete-char 1)
1667 (if (looking-at " ")
1668 (delete-char 1))))
1670 ;;;###autoload
1671 (defun image-dired-display-thumbs-append ()
1672 "Append thumbnails to `image-dired-thumbnail-buffer'."
1673 (interactive)
1674 (image-dired-display-thumbs nil t t))
1676 ;;;###autoload
1677 (defun image-dired-display-thumb ()
1678 "Shorthand for `image-dired-display-thumbs' with prefix argument."
1679 (interactive)
1680 (image-dired-display-thumbs t nil t))
1682 (defun image-dired-line-up ()
1683 "Line up thumbnails according to `image-dired-thumbs-per-row'.
1684 See also `image-dired-line-up-dynamic'."
1685 (interactive)
1686 (let ((inhibit-read-only t))
1687 (goto-char (point-min))
1688 (while (and (not (image-dired-image-at-point-p))
1689 (not (eobp)))
1690 (delete-char 1))
1691 (while (not (eobp))
1692 (forward-char)
1693 (while (and (not (image-dired-image-at-point-p))
1694 (not (eobp)))
1695 (delete-char 1)))
1696 (goto-char (point-min))
1697 (let ((count 0))
1698 (while (not (eobp))
1699 (forward-char)
1700 (if (= image-dired-thumbs-per-row 1)
1701 (insert "\n")
1702 (insert " ")
1703 (setq count (1+ count))
1704 (when (and (= count (- image-dired-thumbs-per-row 1))
1705 (not (eobp)))
1706 (forward-char)
1707 (insert "\n")
1708 (setq count 0)))))
1709 (goto-char (point-min))))
1711 (defun image-dired-line-up-dynamic ()
1712 "Line up thumbnails images dynamically.
1713 Calculate how many thumbnails fit."
1714 (interactive)
1715 (let* ((char-width (frame-char-width))
1716 (width (image-dired-window-width-pixels (image-dired-thumbnail-window)))
1717 (image-dired-thumbs-per-row
1718 (/ width
1719 (+ (* 2 image-dired-thumb-relief)
1720 (* 2 image-dired-thumb-margin)
1721 image-dired-thumb-width char-width))))
1722 (image-dired-line-up)))
1724 (defun image-dired-line-up-interactive ()
1725 "Line up thumbnails interactively.
1726 Ask user how many thumbnails should be displayed per row."
1727 (interactive)
1728 (let ((image-dired-thumbs-per-row
1729 (string-to-number (read-string "How many thumbs per row: "))))
1730 (if (not (> image-dired-thumbs-per-row 0))
1731 (message "Number must be greater than 0")
1732 (image-dired-line-up))))
1734 (defun image-dired-thumbnail-display-external ()
1735 "Display original image for thumbnail at point using external viewer."
1736 (interactive)
1737 (let ((file (image-dired-original-file-name)))
1738 (if (not (image-dired-image-at-point-p))
1739 (message "No thumbnail at point")
1740 (if (not file)
1741 (message "No original file name found")
1742 (call-process shell-file-name nil nil nil shell-command-switch
1743 (format "%s \"%s\"" image-dired-external-viewer file))))))
1745 ;;;###autoload
1746 (defun image-dired-dired-display-external ()
1747 "Display file at point using an external viewer."
1748 (interactive)
1749 (let ((file (dired-get-filename)))
1750 (call-process shell-file-name nil nil nil shell-command-switch
1751 (format "%s \"%s\"" image-dired-external-viewer file))))
1753 (defun image-dired-window-width-pixels (window)
1754 "Calculate WINDOW width in pixels."
1755 (* (window-width window) (frame-char-width)))
1757 (defun image-dired-window-height-pixels (window)
1758 "Calculate WINDOW height in pixels."
1759 ;; Note: The mode-line consumes one line
1760 (* (- (window-height window) 1) (frame-char-height)))
1762 (defun image-dired-display-window ()
1763 "Return window where `image-dired-display-image-buffer' is visible."
1764 (get-window-with-predicate
1765 (lambda (window)
1766 (equal (buffer-name (window-buffer window)) image-dired-display-image-buffer))
1767 nil t))
1769 (defun image-dired-thumbnail-window ()
1770 "Return window where `image-dired-thumbnail-buffer' is visible."
1771 (get-window-with-predicate
1772 (lambda (window)
1773 (equal (buffer-name (window-buffer window)) image-dired-thumbnail-buffer))
1774 nil t))
1776 (defun image-dired-associated-dired-buffer-window ()
1777 "Return window where associated dired buffer is visible."
1778 (let (buf)
1779 (if (image-dired-image-at-point-p)
1780 (progn
1781 (setq buf (image-dired-associated-dired-buffer))
1782 (get-window-with-predicate
1783 (lambda (window)
1784 (equal (window-buffer window) buf))))
1785 (error "No thumbnail image at point"))))
1787 (defun image-dired-display-window-width ()
1788 "Return width, in pixels, of image-dired's image display window."
1789 (- (image-dired-window-width-pixels (image-dired-display-window))
1790 image-dired-display-window-width-correction))
1792 (defun image-dired-display-window-height ()
1793 "Return height, in pixels, of image-dired's image display window."
1794 (- (image-dired-window-height-pixels (image-dired-display-window))
1795 image-dired-display-window-height-correction))
1797 (defun image-dired-display-image (file &optional original-size)
1798 "Display image FILE in image buffer.
1799 Use this when you want to display the image, semi sized, in a new
1800 window. The image is sized to fit the display window (using a
1801 temporary file, don't worry). Because of this, it will not be as
1802 quick as opening it directly, but on most modern systems it
1803 should feel snappy enough.
1805 If optional argument ORIGINAL-SIZE is non-nil, display image in its
1806 original size."
1807 (let ((new-file (expand-file-name image-dired-temp-image-file))
1808 width height command ret
1809 (image-type 'jpeg))
1810 (setq file (expand-file-name file))
1811 (if (not original-size)
1812 (progn
1813 (setq width (image-dired-display-window-width))
1814 (setq height (image-dired-display-window-height))
1815 (setq command
1816 (format-spec
1817 image-dired-cmd-create-temp-image-options
1818 (list
1819 (cons ?p image-dired-cmd-create-temp-image-program)
1820 (cons ?w width)
1821 (cons ?h height)
1822 (cons ?f file)
1823 (cons ?t new-file))))
1824 (setq ret (call-process shell-file-name nil nil nil
1825 shell-command-switch command))
1826 (if (not (= 0 ret))
1827 (error "Could not resize image")))
1828 (setq image-type (image-type-from-file-name file))
1829 (copy-file file new-file t))
1830 (with-current-buffer (image-dired-create-display-image-buffer)
1831 (let ((inhibit-read-only t))
1832 (erase-buffer)
1833 (clear-image-cache)
1834 (image-dired-insert-image image-dired-temp-image-file image-type 0 0)
1835 (goto-char (point-min))
1836 (image-dired-update-property 'original-file-name file)))))
1838 (defun image-dired-display-thumbnail-original-image (&optional arg)
1839 "Display current thumbnail's original image in display buffer.
1840 See documentation for `image-dired-display-image' for more information.
1841 With prefix argument ARG, display image in its original size."
1842 (interactive "P")
1843 (let ((file (image-dired-original-file-name)))
1844 (if (not (string-equal major-mode "image-dired-thumbnail-mode"))
1845 (message "Not in image-dired-thumbnail-mode")
1846 (if (not (image-dired-image-at-point-p))
1847 (message "No thumbnail at point")
1848 (if (not file)
1849 (message "No original file name found")
1850 (image-dired-create-display-image-buffer)
1851 (display-buffer image-dired-display-image-buffer)
1852 (image-dired-display-image file arg))))))
1855 ;;;###autoload
1856 (defun image-dired-dired-display-image (&optional arg)
1857 "Display current image file.
1858 See documentation for `image-dired-display-image' for more information.
1859 With prefix argument ARG, display image in its original size."
1860 (interactive "P")
1861 (image-dired-create-display-image-buffer)
1862 (display-buffer image-dired-display-image-buffer)
1863 (image-dired-display-image (dired-get-filename) arg))
1865 (defun image-dired-image-at-point-p ()
1866 "Return true if there is an image-dired thumbnail at point."
1867 (get-text-property (point) 'image-dired-thumbnail))
1869 (defun image-dired-rotate-thumbnail (degrees)
1870 "Rotate thumbnail DEGREES degrees."
1871 (if (not (image-dired-image-at-point-p))
1872 (message "No thumbnail at point")
1873 (let ((file (image-dired-thumb-name (image-dired-original-file-name)))
1874 command)
1875 (setq command (format-spec
1876 image-dired-cmd-rotate-thumbnail-options
1877 (list
1878 (cons ?p image-dired-cmd-rotate-thumbnail-program)
1879 (cons ?d degrees)
1880 (cons ?t (expand-file-name file)))))
1881 (call-process shell-file-name nil nil nil shell-command-switch command)
1882 ;; Clear the cache to refresh image. I wish I could just refresh
1883 ;; the current file but I do not know how to do that. Yet...
1884 (clear-image-cache))))
1886 (defun image-dired-rotate-thumbnail-left ()
1887 "Rotate thumbnail left (counter clockwise) 90 degrees.
1888 The result of the rotation is displayed in the image display area
1889 and a confirmation is needed before the original image files is
1890 overwritten. This confirmation can be turned off using
1891 `image-dired-rotate-original-ask-before-overwrite'."
1892 (interactive)
1893 (image-dired-rotate-thumbnail "270"))
1895 (defun image-dired-rotate-thumbnail-right ()
1896 "Rotate thumbnail counter right (clockwise) 90 degrees.
1897 The result of the rotation is displayed in the image display area
1898 and a confirmation is needed before the original image files is
1899 overwritten. This confirmation can be turned off using
1900 `image-dired-rotate-original-ask-before-overwrite'."
1901 (interactive)
1902 (image-dired-rotate-thumbnail "90"))
1904 (defun image-dired-refresh-thumb ()
1905 "Force creation of new image for current thumbnail."
1906 (interactive)
1907 (let ((file (image-dired-original-file-name)))
1908 (clear-image-cache)
1909 (image-dired-create-thumb file (image-dired-thumb-name file))))
1911 (defun image-dired-rotate-original (degrees)
1912 "Rotate original image DEGREES degrees."
1913 (if (not (image-dired-image-at-point-p))
1914 (message "No image at point")
1915 (let ((file (image-dired-original-file-name))
1916 command)
1917 (if (not (string-match "\.[jJ][pP[eE]?[gG]$" file))
1918 (error "Only JPEG images can be rotated!"))
1919 (setq command (format-spec
1920 image-dired-cmd-rotate-original-options
1921 (list
1922 (cons ?p image-dired-cmd-rotate-original-program)
1923 (cons ?d degrees)
1924 (cons ?o (expand-file-name file))
1925 (cons ?t image-dired-temp-rotate-image-file))))
1926 (if (not (= 0 (call-process shell-file-name nil nil nil
1927 shell-command-switch command)))
1928 (error "Could not rotate image")
1929 (image-dired-display-image image-dired-temp-rotate-image-file)
1930 (if (or (and image-dired-rotate-original-ask-before-overwrite
1931 (y-or-n-p
1932 "Rotate to temp file OK. Overwrite original image? "))
1933 (not image-dired-rotate-original-ask-before-overwrite))
1934 (progn
1935 (copy-file image-dired-temp-rotate-image-file file t)
1936 (image-dired-refresh-thumb))
1937 (image-dired-display-image file))))))
1939 (defun image-dired-rotate-original-left ()
1940 "Rotate original image left (counter clockwise) 90 degrees."
1941 (interactive)
1942 (image-dired-rotate-original "270"))
1944 (defun image-dired-rotate-original-right ()
1945 "Rotate original image right (clockwise) 90 degrees."
1946 (interactive)
1947 (image-dired-rotate-original "90"))
1949 (defun image-dired-get-exif-file-name (file)
1950 "Use the image's EXIF information to return a unique file name.
1951 The file name should be unique as long as you do not take more than
1952 one picture per second. The original file name is suffixed at the end
1953 for traceability. The format of the returned file name is
1954 YYYY_MM_DD_HH_MM_DD_ORIG_FILE_NAME.jpg. Used from
1955 `image-dired-copy-with-exif-file-name'."
1956 (let (data no-exif-data-found)
1957 (if (not (string-match "\.[Jj][Pp][Ee]?[Gg]$" (expand-file-name file)))
1958 (progn
1959 (setq no-exif-data-found t)
1960 (setq data
1961 (format-time-string
1962 "%Y:%m:%d %H:%M:%S"
1963 (nth 5 (file-attributes (expand-file-name file))))))
1964 (setq data (image-dired-get-exif-data (expand-file-name file)
1965 "DateTimeOriginal")))
1966 (while (string-match "[ :]" data)
1967 (setq data (replace-match "_" nil nil data)))
1968 (format "%s%s%s" data
1969 (if no-exif-data-found
1970 "_noexif_"
1971 "_")
1972 (file-name-nondirectory file))))
1974 (defun image-dired-thumbnail-set-image-description ()
1975 "Set the ImageDescription EXIF tag for the original image.
1976 If the image already has a value for this tag, it is used as the
1977 default value at the prompt."
1978 (interactive)
1979 (if (not (image-dired-image-at-point-p))
1980 (message "No thumbnail at point")
1981 (let* ((file (image-dired-original-file-name))
1982 (old-value (image-dired-get-exif-data file "ImageDescription")))
1983 (if (eq 0
1984 (image-dired-set-exif-data file "ImageDescription"
1985 (read-string "Value of ImageDescription: "
1986 old-value)))
1987 (message "Successfully wrote ImageDescription tag.")
1988 (error "Could not write ImageDescription tag")))))
1990 (defun image-dired-set-exif-data (file tag-name tag-value)
1991 "In FILE, set EXIF tag TAG-NAME to value TAG-VALUE."
1992 (let (command)
1993 (setq command (format-spec
1994 image-dired-cmd-write-exif-data-options
1995 (list
1996 (cons ?p image-dired-cmd-write-exif-data-program)
1997 (cons ?f (expand-file-name file))
1998 (cons ?t tag-name)
1999 (cons ?v tag-value))))
2000 (call-process shell-file-name nil nil nil shell-command-switch command)))
2002 (defun image-dired-get-exif-data (file tag-name)
2003 "From FILE, return EXIF tag TAG-NAME."
2004 (let ((buf (get-buffer-create "*image-dired-get-exif-data*"))
2005 command tag-value)
2006 (setq command (format-spec
2007 image-dired-cmd-read-exif-data-options
2008 (list
2009 (cons ?p image-dired-cmd-read-exif-data-program)
2010 (cons ?f file)
2011 (cons ?t tag-name))))
2012 (with-current-buffer buf
2013 (delete-region (point-min) (point-max))
2014 (if (not (eq (call-process shell-file-name nil t nil
2015 shell-command-switch command) 0))
2016 (error "Could not get EXIF tag")
2017 (goto-char (point-min))
2018 ;; Clean buffer from newlines and carriage returns before
2019 ;; getting final info
2020 (while (search-forward-regexp "[\n\r]" nil t)
2021 (replace-match "" nil t))
2022 (setq tag-value (buffer-substring (point-min) (point-max)))))
2023 tag-value))
2025 (defun image-dired-copy-with-exif-file-name ()
2026 "Copy file with unique name to main image directory.
2027 Copy current or all marked files in dired to a new file in your
2028 main image directory, using a file name generated by
2029 `image-dired-get-exif-file-name'. A typical usage for this if when
2030 copying images from a digital camera into the image directory.
2032 Typically, you would open up the folder with the incoming
2033 digital images, mark the files to be copied, and execute this
2034 function. The result is a couple of new files in
2035 `image-dired-main-image-directory' called
2036 2005_05_08_12_52_00_dscn0319.jpg,
2037 2005_05_08_14_27_45_dscn0320.jpg etc."
2038 (interactive)
2039 (let (new-name
2040 (files (dired-get-marked-files)))
2041 (mapcar
2042 (lambda (curr-file)
2043 (setq new-name
2044 (format "%s/%s"
2045 (file-name-as-directory
2046 (expand-file-name image-dired-main-image-directory))
2047 (image-dired-get-exif-file-name curr-file)))
2048 (message "Copying %s to %s" curr-file new-name)
2049 (copy-file curr-file new-name))
2050 files)))
2052 (defun image-dired-display-next-thumbnail-original ()
2053 "In thumbnail buffer, move to next thumbnail and display the image."
2054 (interactive)
2055 (image-dired-forward-image)
2056 (image-dired-display-thumbnail-original-image))
2058 (defun image-dired-display-previous-thumbnail-original ()
2059 "Move to previous thumbnail and display image."
2060 (interactive)
2061 (image-dired-backward-image)
2062 (image-dired-display-thumbnail-original-image))
2064 (defun image-dired-write-comments (file-comments)
2065 "Write file comments to database.
2066 Write file comments to one or more files.
2067 FILE-COMMENTS is an alist on the following form:
2068 ((FILE . COMMENT) ... )"
2069 (image-dired-sane-db-file)
2070 (let (end comment-beg-pos comment-end-pos file comment)
2071 (image-dired--with-db-file
2072 (setq buffer-file-name image-dired-db-file)
2073 (dolist (elt file-comments)
2074 (setq file (car elt)
2075 comment (cdr elt))
2076 (goto-char (point-min))
2077 (if (search-forward-regexp (format "^%s.*$" file) nil t)
2078 (progn
2079 (setq end (point))
2080 (beginning-of-line)
2081 ;; Delete old comment, if any
2082 (when (search-forward ";comment:" end t)
2083 (setq comment-beg-pos (match-beginning 0))
2084 ;; Any tags after the comment?
2085 (if (search-forward ";" end t)
2086 (setq comment-end-pos (- (point) 1))
2087 (setq comment-end-pos end))
2088 ;; Delete comment tag and comment
2089 (delete-region comment-beg-pos comment-end-pos))
2090 ;; Insert new comment
2091 (beginning-of-line)
2092 (unless (search-forward ";" end t)
2093 (end-of-line)
2094 (insert ";"))
2095 (insert (format "comment:%s;" comment)))
2096 ;; File does not exist in database - add it.
2097 (goto-char (point-max))
2098 (insert (format "\n%s;comment:%s" file comment))))
2099 (save-buffer))))
2101 (defun image-dired-update-property (prop value)
2102 "Update text property PROP with value VALUE at point."
2103 (let ((inhibit-read-only t))
2104 (put-text-property
2105 (point) (1+ (point))
2106 prop
2107 value)))
2109 ;;;###autoload
2110 (defun image-dired-dired-comment-files ()
2111 "Add comment to current or marked files in dired."
2112 (interactive)
2113 (let ((comment (image-dired-read-comment)))
2114 (image-dired-write-comments
2115 (mapcar
2116 (lambda (curr-file)
2117 (cons curr-file comment))
2118 (dired-get-marked-files)))))
2120 (defun image-dired-comment-thumbnail ()
2121 "Add comment to current thumbnail in thumbnail buffer."
2122 (interactive)
2123 (let* ((file (image-dired-original-file-name))
2124 (comment (image-dired-read-comment file)))
2125 (image-dired-write-comments (list (cons file comment)))
2126 (image-dired-update-property 'comment comment))
2127 (image-dired-display-thumb-properties))
2129 (defun image-dired-read-comment (&optional file)
2130 "Read comment for an image.
2131 Optionally use old comment from FILE as initial value."
2132 (let ((comment
2133 (read-string
2134 "Comment: "
2135 (if file (image-dired-get-comment file)))))
2136 comment))
2138 (defun image-dired-get-comment (file)
2139 "Get comment for file FILE."
2140 (image-dired-sane-db-file)
2141 (image-dired--with-db-file
2142 (let (end comment-beg-pos comment-end-pos comment)
2143 (when (search-forward-regexp (format "^%s" file) nil t)
2144 (end-of-line)
2145 (setq end (point))
2146 (beginning-of-line)
2147 (when (search-forward ";comment:" end t)
2148 (setq comment-beg-pos (point))
2149 (if (search-forward ";" end t)
2150 (setq comment-end-pos (- (point) 1))
2151 (setq comment-end-pos end))
2152 (setq comment (buffer-substring
2153 comment-beg-pos comment-end-pos))))
2154 comment)))
2156 ;;;###autoload
2157 (defun image-dired-mark-tagged-files ()
2158 "Use regexp to mark files with matching tag.
2159 A `tag' is a keyword, a piece of meta data, associated with an
2160 image file and stored in image-dired's database file. This command
2161 lets you input a regexp and this will be matched against all tags
2162 on all image files in the database file. The files that have a
2163 matching tag will be marked in the dired buffer."
2164 (interactive)
2165 (image-dired-sane-db-file)
2166 (let ((tag (read-string "Mark tagged files (regexp): "))
2167 (hits 0)
2168 files)
2169 (image-dired--with-db-file
2170 ;; Collect matches
2171 (while (search-forward-regexp
2172 (concat "\\(^[^;\n]+\\);.*" tag ".*$") nil t)
2173 (push (match-string 1) files)))
2174 ;; Mark files
2175 (dolist (curr-file files)
2176 ;; I tried using `dired-mark-files-regexp' but it was waaaay to
2177 ;; slow. Don't bother about hits found in other directories
2178 ;; than the current one.
2179 (when (string= (file-name-as-directory
2180 (expand-file-name default-directory))
2181 (file-name-as-directory
2182 (file-name-directory curr-file)))
2183 (setq curr-file (file-name-nondirectory curr-file))
2184 (goto-char (point-min))
2185 (when (search-forward-regexp (format "\\s %s$" curr-file) nil t)
2186 (setq hits (+ hits 1))
2187 (dired-mark 1))))
2188 (message "%d files with matching tag marked." hits)))
2190 (defun image-dired-mouse-display-image (event)
2191 "Use mouse EVENT, call `image-dired-display-image' to display image.
2192 Track this in associated dired buffer if `image-dired-track-movement' is
2193 non-nil."
2194 (interactive "e")
2195 (mouse-set-point event)
2196 (goto-char (posn-point (event-end event)))
2197 (let ((file (image-dired-original-file-name)))
2198 (when file
2199 (if image-dired-track-movement
2200 (image-dired-track-original-file))
2201 (image-dired-create-display-image-buffer)
2202 (display-buffer image-dired-display-image-buffer)
2203 (image-dired-display-image file))))
2205 (defun image-dired-mouse-select-thumbnail (event)
2206 "Use mouse EVENT to select thumbnail image.
2207 Track this in associated dired buffer if `image-dired-track-movement' is
2208 non-nil."
2209 (interactive "e")
2210 (mouse-set-point event)
2211 (goto-char (posn-point (event-end event)))
2212 (if image-dired-track-movement
2213 (image-dired-track-original-file))
2214 (image-dired-display-thumb-properties))
2216 (defun image-dired-mouse-toggle-mark (event)
2217 "Use mouse EVENT to toggle dired mark for thumbnail.
2218 Track this in associated dired buffer if `image-dired-track-movement' is
2219 non-nil."
2220 (interactive "e")
2221 (mouse-set-point event)
2222 (goto-char (posn-point (event-end event)))
2223 (if image-dired-track-movement
2224 (image-dired-track-original-file))
2225 (image-dired-toggle-mark-thumb-original-file))
2227 (defun image-dired-dired-display-properties ()
2228 "Display properties for dired file in the echo area."
2229 (interactive)
2230 (let* ((file (dired-get-filename))
2231 (file-name (file-name-nondirectory file))
2232 (dired-buf (buffer-name (current-buffer)))
2233 (props (mapconcat
2234 'princ
2235 (image-dired-list-tags file)
2236 ", "))
2237 (comment (image-dired-get-comment file)))
2238 (if file-name
2239 (message "%s"
2240 (image-dired-format-properties-string
2241 dired-buf
2242 file-name
2243 props
2244 comment)))))
2246 (defvar image-dired-tag-file-list nil
2247 "List to store tag-file structure.")
2249 (defvar image-dired-file-tag-list nil
2250 "List to store file-tag structure.")
2252 (defvar image-dired-file-comment-list nil
2253 "List to store file comments.")
2255 (defun image-dired-add-to-tag-file-list (tag file)
2256 "Add relation between TAG and FILE."
2257 (let (curr)
2258 (if image-dired-tag-file-list
2259 (if (setq curr (assoc tag image-dired-tag-file-list))
2260 (if (not (member file curr))
2261 (setcdr curr (cons file (cdr curr))))
2262 (setcdr image-dired-tag-file-list
2263 (cons (list tag file) (cdr image-dired-tag-file-list))))
2264 (setq image-dired-tag-file-list (list (list tag file))))))
2266 (defun image-dired-add-to-tag-file-lists (tag file)
2267 "Helper function used from `image-dired-create-gallery-lists'.
2269 Add TAG to FILE in one list and FILE to TAG in the other.
2271 Lisp structures look like the following:
2273 image-dired-file-tag-list:
2275 ((\"filename1\" \"tag1\" \"tag2\" \"tag3\" ...)
2276 (\"filename2\" \"tag1\" \"tag2\" \"tag3\" ...)
2277 ...)
2279 image-dired-tag-file-list:
2281 ((\"tag1\" \"filename1\" \"filename2\" \"filename3\" ...)
2282 (\"tag2\" \"filename1\" \"filename2\" \"filename3\" ...)
2283 ...)"
2284 ;; Add tag to file list
2285 (let (curr)
2286 (if image-dired-file-tag-list
2287 (if (setq curr (assoc file image-dired-file-tag-list))
2288 (setcdr curr (cons tag (cdr curr)))
2289 (setcdr image-dired-file-tag-list
2290 (cons (list file tag) (cdr image-dired-file-tag-list))))
2291 (setq image-dired-file-tag-list (list (list file tag))))
2292 ;; Add file to tag list
2293 (if image-dired-tag-file-list
2294 (if (setq curr (assoc tag image-dired-tag-file-list))
2295 (if (not (member file curr))
2296 (setcdr curr (cons file (cdr curr))))
2297 (setcdr image-dired-tag-file-list
2298 (cons (list tag file) (cdr image-dired-tag-file-list))))
2299 (setq image-dired-tag-file-list (list (list tag file))))))
2301 (defun image-dired-add-to-file-comment-list (file comment)
2302 "Helper function used from `image-dired-create-gallery-lists'.
2304 For FILE, add COMMENT to list.
2306 Lisp structure looks like the following:
2308 image-dired-file-comment-list:
2310 ((\"filename1\" . \"comment1\")
2311 (\"filename2\" . \"comment2\")
2312 ...)"
2313 (if image-dired-file-comment-list
2314 (if (not (assoc file image-dired-file-comment-list))
2315 (setcdr image-dired-file-comment-list
2316 (cons (cons file comment)
2317 (cdr image-dired-file-comment-list))))
2318 (setq image-dired-file-comment-list (list (cons file comment)))))
2320 (defun image-dired-create-gallery-lists ()
2321 "Create temporary lists used by `image-dired-gallery-generate'."
2322 (image-dired-sane-db-file)
2323 (image-dired--with-db-file
2324 (let (end beg file row-tags)
2325 (setq image-dired-tag-file-list nil)
2326 (setq image-dired-file-tag-list nil)
2327 (setq image-dired-file-comment-list nil)
2328 (goto-char (point-min))
2329 (while (search-forward-regexp "^." nil t)
2330 (end-of-line)
2331 (setq end (point))
2332 (beginning-of-line)
2333 (setq beg (point))
2334 (unless (search-forward ";" end nil)
2335 (error "Something is really wrong, check format of database"))
2336 (setq row-tags (split-string
2337 (buffer-substring beg end) ";"))
2338 (setq file (car row-tags))
2339 (dolist (x (cdr row-tags))
2340 (if (not (string-match "^comment:\\(.*\\)" x))
2341 (image-dired-add-to-tag-file-lists x file)
2342 (image-dired-add-to-file-comment-list file (match-string 1 x)))))))
2343 ;; Sort tag-file list
2344 (setq image-dired-tag-file-list
2345 (sort image-dired-tag-file-list
2346 (lambda (x y)
2347 (string< (car x) (car y))))))
2349 (defun image-dired-hidden-p (file)
2350 "Return t if image FILE has a \"hidden\" tag."
2351 (let (hidden)
2352 (mapc
2353 (lambda (tag)
2354 (if (member tag image-dired-gallery-hidden-tags)
2355 (setq hidden t)))
2356 (cdr (assoc file image-dired-file-tag-list)))
2357 hidden))
2359 (defun image-dired-gallery-generate ()
2360 "Generate gallery pages.
2361 First we create a couple of Lisp structures from the database to make
2362 it easier to generate, then HTML-files are created in
2363 `image-dired-gallery-dir'."
2364 (interactive)
2365 (if (eq 'per-directory image-dired-thumbnail-storage)
2366 (error "Currently, gallery generation is not supported \
2367 when using per-directory thumbnail file storage"))
2368 (image-dired-create-gallery-lists)
2369 (let ((tags image-dired-tag-file-list)
2370 (index-file (format "%s/index.html" image-dired-gallery-dir))
2371 count tag tag-file
2372 comment file-tags tag-link tag-link-list)
2373 ;; Make sure gallery root exist
2374 (if (file-exists-p image-dired-gallery-dir)
2375 (if (not (file-directory-p image-dired-gallery-dir))
2376 (error "Variable image-dired-gallery-dir is not a directory"))
2377 (make-directory image-dired-gallery-dir))
2378 ;; Open index file
2379 (with-temp-file index-file
2380 (if (file-exists-p index-file)
2381 (insert-file-contents index-file))
2382 (insert "<html>\n")
2383 (insert " <body>\n")
2384 (insert " <h2>Image-Dired Gallery</h2>\n")
2385 (insert (format "<p>\n Gallery generated %s\n <p>\n"
2386 (current-time-string)))
2387 (insert " <h3>Tag index</h3>\n")
2388 (setq count 1)
2389 ;; Pre-generate list of all tag links
2390 (dolist (curr tags)
2391 (setq tag (car curr))
2392 (when (not (member tag image-dired-gallery-hidden-tags))
2393 (setq tag-link (format "<a href=\"%d.html\">%s</a>" count tag))
2394 (if tag-link-list
2395 (setq tag-link-list
2396 (append tag-link-list (list (cons tag tag-link))))
2397 (setq tag-link-list (list (cons tag tag-link))))
2398 (setq count (1+ count))))
2399 (setq count 1)
2400 ;; Main loop where we generated thumbnail pages per tag
2401 (dolist (curr tags)
2402 (setq tag (car curr))
2403 ;; Don't display hidden tags
2404 (when (not (member tag image-dired-gallery-hidden-tags))
2405 ;; Insert link to tag page in index
2406 (insert (format " %s<br>\n" (cdr (assoc tag tag-link-list))))
2407 ;; Open per-tag file
2408 (setq tag-file (format "%s/%s.html" image-dired-gallery-dir count))
2409 (with-temp-file tag-file
2410 (if (file-exists-p tag-file)
2411 (insert-file-contents tag-file))
2412 (erase-buffer)
2413 (insert "<html>\n")
2414 (insert " <body>\n")
2415 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2416 (insert (format " <h2>Images with tag &quot;%s&quot;</h2>" tag))
2417 ;; Main loop for files per tag page
2418 (dolist (file (cdr curr))
2419 (unless (image-dired-hidden-p file)
2420 ;; Insert thumbnail with link to full image
2421 (insert
2422 (format "<a href=\"%s/%s\"><img src=\"%s/%s\"%s></a>\n"
2423 image-dired-gallery-image-root-url
2424 (file-name-nondirectory file)
2425 image-dired-gallery-thumb-image-root-url
2426 (file-name-nondirectory (image-dired-thumb-name file)) file))
2427 ;; Insert comment, if any
2428 (if (setq comment (cdr (assoc file image-dired-file-comment-list)))
2429 (insert (format "<br>\n%s<br>\n" comment))
2430 (insert "<br>\n"))
2431 ;; Insert links to other tags, if any
2432 (when (> (length
2433 (setq file-tags (assoc file image-dired-file-tag-list))) 2)
2434 (insert "[ ")
2435 (dolist (extra-tag file-tags)
2436 ;; Only insert if not file name or the main tag
2437 (if (and (not (equal extra-tag tag))
2438 (not (equal extra-tag file)))
2439 (insert
2440 (format "%s " (cdr (assoc extra-tag tag-link-list))))))
2441 (insert "]<br>\n"))))
2442 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2443 (insert " </body>\n")
2444 (insert "</html>\n"))
2445 (setq count (1+ count))))
2446 (insert " </body>\n")
2447 (insert "</html>"))))
2449 (defun image-dired-kill-buffer-and-window ()
2450 "Kill the current buffer and, if possible, also the window."
2451 (interactive)
2452 (let ((buffer (current-buffer)))
2453 (condition-case nil
2454 (delete-window (selected-window))
2455 (error nil))
2456 (kill-buffer buffer)))
2458 (defvar image-dired-widget-list nil
2459 "List to keep track of meta data in edit buffer.")
2461 (declare-function widget-forward "wid-edit" (arg))
2463 ;;;###autoload
2464 (defun image-dired-dired-edit-comment-and-tags ()
2465 "Edit comment and tags of current or marked image files.
2466 Edit comment and tags for all marked image files in an
2467 easy-to-use form."
2468 (interactive)
2469 (setq image-dired-widget-list nil)
2470 ;; Setup buffer.
2471 (let ((files (dired-get-marked-files)))
2472 (switch-to-buffer "*Image-Dired Edit Meta Data*")
2473 (kill-all-local-variables)
2474 (make-local-variable 'widget-example-repeat)
2475 (let ((inhibit-read-only t))
2476 (erase-buffer))
2477 (remove-overlays)
2478 ;; Some help for the user.
2479 (widget-insert
2480 "\nEdit comments and tags for each image. Separate multiple tags
2481 with a comma. Move forward between fields using TAB or RET.
2482 Move to the previous field using backtab (S-TAB). Save by
2483 activating the Save button at the bottom of the form or cancel
2484 the operation by activating the Cancel button.\n\n")
2485 ;; Here comes all images and a comment and tag field for each
2486 ;; image.
2487 (let (thumb-file img comment-widget tag-widget)
2489 (dolist (file files)
2491 (setq thumb-file (image-dired-thumb-name file)
2492 img (create-image thumb-file))
2494 (insert-image img)
2495 (widget-insert "\n\nComment: ")
2496 (setq comment-widget
2497 (widget-create 'editable-field
2498 :size 60
2499 :format "%v "
2500 :value (or (image-dired-get-comment file) "")))
2501 (widget-insert "\nTags: ")
2502 (setq tag-widget
2503 (widget-create 'editable-field
2504 :size 60
2505 :format "%v "
2506 :value (or (mapconcat
2507 (lambda (tag)
2508 tag)
2509 (image-dired-list-tags file)
2510 ",") "")))
2511 ;; Save information in all widgets so that we can use it when
2512 ;; the user saves the form.
2513 (setq image-dired-widget-list
2514 (append image-dired-widget-list
2515 (list (list file comment-widget tag-widget))))
2516 (widget-insert "\n\n")))
2518 ;; Footer with Save and Cancel button.
2519 (widget-insert "\n")
2520 (widget-create 'push-button
2521 :notify
2522 (lambda (&rest _ignore)
2523 (image-dired-save-information-from-widgets)
2524 (bury-buffer)
2525 (message "Done."))
2526 "Save")
2527 (widget-insert " ")
2528 (widget-create 'push-button
2529 :notify
2530 (lambda (&rest _ignore)
2531 (bury-buffer)
2532 (message "Operation canceled."))
2533 "Cancel")
2534 (widget-insert "\n")
2535 (use-local-map widget-keymap)
2536 (widget-setup)
2537 ;; Jump to the first widget.
2538 (widget-forward 1)))
2540 (defun image-dired-save-information-from-widgets ()
2541 "Save information found in `image-dired-widget-list'.
2542 Use the information in `image-dired-widget-list' to save comments and
2543 tags to their respective image file. Internal function used by
2544 `image-dired-dired-edit-comment-and-tags'."
2545 (let (file comment tag-string tag-list lst)
2546 (image-dired-write-comments
2547 (mapcar
2548 (lambda (widget)
2549 (setq file (car widget)
2550 comment (widget-value (cadr widget)))
2551 (cons file comment))
2552 image-dired-widget-list))
2553 (image-dired-write-tags
2554 (dolist (widget image-dired-widget-list lst)
2555 (setq file (car widget)
2556 tag-string (widget-value (car (cddr widget)))
2557 tag-list (split-string tag-string ","))
2558 (dolist (tag tag-list)
2559 (push (cons file tag) lst))))))
2561 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2562 ;;;;;;;;; TEST-SECTION ;;;;;;;;;;;
2563 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2565 ;; (defvar image-dired-dir-max-size 12300000)
2567 ;; (defun image-dired-test-clean-old-files ()
2568 ;; "Clean `image-dired-dir' from old thumbnail files.
2569 ;; \"Oldness\" measured using last access time. If the total size of all
2570 ;; thumbnail files in `image-dired-dir' is larger than 'image-dired-dir-max-size',
2571 ;; old files are deleted until the max size is reached."
2572 ;; (let* ((files
2573 ;; (sort
2574 ;; (mapcar
2575 ;; (lambda (f)
2576 ;; (let ((fattribs (file-attributes f)))
2577 ;; ;; Get last access time and file size
2578 ;; `(,(nth 4 fattribs) ,(nth 7 fattribs) ,f)))
2579 ;; (directory-files (image-dired-dir) t ".+\.thumb\..+$"))
2580 ;; ;; Sort function. Compare time between two files.
2581 ;; (lambda (l1 l2)
2582 ;; (time-less-p (car l1) (car l2)))))
2583 ;; (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) files))))
2584 ;; (while (> dirsize image-dired-dir-max-size)
2585 ;; (y-or-n-p
2586 ;; (format "Size of thumbnail directory: %d, delete old file %s? "
2587 ;; dirsize (cadr (cdar files))))
2588 ;; (delete-file (cadr (cdar files)))
2589 ;; (setq dirsize (- dirsize (car (cdar files))))
2590 ;; (setq files (cdr files)))))
2592 ;;;;;;;;;;;;;;;;;;;;;;,
2594 ;; (defun dired-speedbar-buttons (dired-buffer)
2595 ;; (when (and (boundp 'image-dired-use-speedbar)
2596 ;; image-dired-use-speedbar)
2597 ;; (let ((filename (with-current-buffer dired-buffer
2598 ;; (dired-get-filename))))
2599 ;; (when (and (not (string-equal filename (buffer-string)))
2600 ;; (string-match (image-file-name-regexp) filename))
2601 ;; (erase-buffer)
2602 ;; (insert (propertize
2603 ;; filename
2604 ;; 'display
2605 ;; (image-dired-get-thumbnail-image filename)))))))
2607 ;; (setq image-dired-use-speedbar t)
2609 (provide 'image-dired)
2611 ;;; image-dired.el ends here