; Fix previous commit
[emacs.git] / lisp / image-dired.el
blob7978f07564550c49937a61f21db41b7bf20f7a31
1 ;;; image-dired.el --- use dired to browse and manipulate your images
2 ;;
3 ;; Copyright (C) 2005-2016 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 ;; * Investigate if it is possible to also write the tags to the image
108 ;; files.
110 ;; * From thumbs.el: Add an option for clean-up/max-size functionality
111 ;; for thumbnail directory.
113 ;; * From thumbs.el: Add setroot function.
115 ;; * From thumbs.el: Add image resizing, if useful (image-dired's automatic
116 ;; "image fit" might be enough)
118 ;; * From thumbs.el: Add the "modify" commands (emboss, negate,
119 ;; monochrome etc).
121 ;; * Asynchronous creation of thumbnails.
123 ;; * Add `image-dired-display-thumbs-ring' and functions to cycle that. Find
124 ;; out which is best, saving old batch just before inserting new, or
125 ;; saving the current batch in the ring when inserting it. Adding it
126 ;; probably needs rewriting `image-dired-display-thumbs' to be more general.
128 ;; * Find some way of toggling on and off really nice keybindings in
129 ;; dired (for example, using C-n or <down> instead of C-S-n). Richard
130 ;; suggested that we could keep C-t as prefix for image-dired commands
131 ;; as it is currently not used in dired. He also suggested that
132 ;; `dired-next-line' and `dired-previous-line' figure out if
133 ;; image-dired is enabled in the current buffer and, if it is, call
134 ;; `image-dired-dired-next-line' and
135 ;; `image-dired-dired-previous-line', respectively. Update: This is
136 ;; partly done; some bindings have now been added to dired.
138 ;; * Enhanced gallery creation with basic CSS-support and pagination
139 ;; of tag pages with many pictures.
141 ;; * Rewrite `image-dired-modify-mark-on-thumb-original-file' to be
142 ;; less ugly.
144 ;; * In some way keep track of buffers and windows and stuff so that
145 ;; it works as the user expects.
147 ;; * More/better documentation
150 ;;; Code:
152 (require 'dired)
153 (require 'format-spec)
154 (require 'widget)
156 (eval-when-compile
157 (require 'cl-lib)
158 (require 'wid-edit))
160 (defgroup image-dired nil
161 "Use dired to browse your images as thumbnails, and more."
162 :prefix "image-dired-"
163 :group 'multimedia)
165 (defcustom image-dired-dir (locate-user-emacs-file "image-dired/")
166 "Directory where thumbnail images are stored."
167 :type 'string
168 :group 'image-dired)
170 (defcustom image-dired-thumbnail-storage 'use-image-dired-dir
171 "How to store image-dired's thumbnail files.
172 Image-Dired can store thumbnail files in one of two ways and this is
173 controlled by this variable. \"Use image-dired dir\" means that the
174 thumbnails are stored in a central directory. \"Per directory\"
175 means that each thumbnail is stored in a subdirectory called
176 \".image-dired\" in the same directory where the image file is.
177 \"Thumbnail Managing Standard\" means that the thumbnails are
178 stored and generated according to the Thumbnail Managing Standard
179 that allows sharing of thumbnails across different programs."
180 :type '(choice :tag "How to store thumbnail files"
181 (const :tag "Thumbnail Managing Standard" standard)
182 (const :tag "Use image-dired-dir" use-image-dired-dir)
183 (const :tag "Per-directory" per-directory))
184 :group 'image-dired)
186 (defcustom image-dired-db-file
187 (expand-file-name ".image-dired_db" image-dired-dir)
188 "Database file where file names and their associated tags are stored."
189 :type 'string
190 :group 'image-dired)
192 (defcustom image-dired-temp-image-file
193 (expand-file-name ".image-dired_temp" image-dired-dir)
194 "Name of temporary image file used by various commands."
195 :type 'string
196 :group 'image-dired)
198 (defcustom image-dired-gallery-dir
199 (expand-file-name ".image-dired_gallery" image-dired-dir)
200 "Directory to store generated gallery html pages.
201 This path needs to be \"shared\" to the public so that it can access
202 the index.html page that image-dired creates."
203 :type 'string
204 :group 'image-dired)
206 (defcustom image-dired-gallery-image-root-url
207 "http://your.own.server/image-diredpics"
208 "URL where the full size images are to be found.
209 Note that this path has to be configured in your web server. Image-Dired
210 expects to find pictures in this directory."
211 :type 'string
212 :group 'image-dired)
214 (defcustom image-dired-gallery-thumb-image-root-url
215 "http://your.own.server/image-diredthumbs"
216 "URL where the thumbnail images are to be found.
217 Note that this path has to be configured in your web server. Image-Dired
218 expects to find pictures in this directory."
219 :type 'string
220 :group 'image-dired)
222 (defcustom image-dired-cmd-create-thumbnail-program
223 "convert"
224 "Executable used to create thumbnail.
225 Used together with `image-dired-cmd-create-thumbnail-options'."
226 :type 'string
227 :group 'image-dired)
229 (defcustom image-dired-cmd-create-thumbnail-options
230 "%p -size %wx%h \"%f\" -resize \"%wx%h>\" -strip jpeg:\"%t\""
231 "Format of command used to create thumbnail image.
232 Available options are %p which is replaced by
233 `image-dired-cmd-create-thumbnail-program', %w which is replaced by
234 `image-dired-thumb-width', %h which is replaced by `image-dired-thumb-height',
235 %f which is replaced by the file name of the original image and %t
236 which is replaced by the file name of the thumbnail file."
237 :type 'string
238 :group 'image-dired)
240 (defcustom image-dired-cmd-create-temp-image-program
241 "convert"
242 "Executable used to create temporary image.
243 Used together with `image-dired-cmd-create-temp-image-options'."
244 :type 'string
245 :group 'image-dired)
247 (defcustom image-dired-cmd-create-temp-image-options
248 "%p -size %wx%h \"%f\" -resize \"%wx%h>\" -strip jpeg:\"%t\""
249 "Format of command used to create temporary image for display window.
250 Available options are %p which is replaced by
251 `image-dired-cmd-create-temp-image-program', %w and %h which is replaced by
252 the calculated max size for width and height in the image display window,
253 %f which is replaced by the file name of the original image and %t which
254 is replaced by the file name of the temporary file."
255 :type 'string
256 :group 'image-dired)
258 (defcustom image-dired-cmd-pngnq-program (executable-find "pngnq")
259 "The file name of the `pngnq' program.
260 It quantizes colors of PNG images down to 256 colors."
261 :type '(choice (const :tag "Not Set" nil) string)
262 :group 'image-dired)
264 (defcustom image-dired-cmd-pngcrush-program (executable-find "pngcrush")
265 "The file name of the `pngcrush' program.
266 It optimizes the compression of PNG images. Also it adds PNG textual chunks
267 with the information required by the Thumbnail Managing Standard."
268 :type '(choice (const :tag "Not Set" nil) string)
269 :group 'image-dired)
271 (defcustom image-dired-cmd-create-standard-thumbnail-command
272 (concat
273 "%p -size %wx%h \"%f\" "
274 (unless (or image-dired-cmd-pngcrush-program image-dired-cmd-pngnq-program)
275 (concat
276 "-set \"Thumb::MTime\" \"%m\" "
277 "-set \"Thumb::URI\" \"file://%f\" "
278 "-set \"Description\" \"Thumbnail of file://%f\" "
279 "-set \"Software\" \"" (emacs-version) "\" "))
280 "-thumbnail \"%wx%h>\" png:\"%t\""
281 (if image-dired-cmd-pngnq-program
282 (concat
283 " ; " image-dired-cmd-pngnq-program " -f \"%t\""
284 (unless image-dired-cmd-pngcrush-program
285 " ; mv %q %t")))
286 (if image-dired-cmd-pngcrush-program
287 (concat
288 (unless image-dired-cmd-pngcrush-program
289 " ; cp %t %q")
290 " ; " image-dired-cmd-pngcrush-program " -q "
291 "-text b \"Description\" \"Thumbnail of file://%f\" "
292 "-text b \"Software\" \"" (emacs-version) "\" "
293 ;; "-text b \"Thumb::Image::Height\" \"%oh\" "
294 ;; "-text b \"Thumb::Image::Mimetype\" \"%mime\" "
295 ;; "-text b \"Thumb::Image::Width\" \"%ow\" "
296 "-text b \"Thumb::MTime\" \"%m\" "
297 ;; "-text b \"Thumb::Size\" \"%b\" "
298 "-text b \"Thumb::URI\" \"file://%f\" "
299 "%q %t"
300 " ; rm %q")))
301 "Command to create thumbnails according to the Thumbnail Managing Standard."
302 :version "26.1"
303 :type 'string
304 :group 'image-dired)
306 (defcustom image-dired-cmd-rotate-thumbnail-program
307 "mogrify"
308 "Executable used to rotate thumbnail.
309 Used together with `image-dired-cmd-rotate-thumbnail-options'."
310 :type 'string
311 :group 'image-dired)
313 (defcustom image-dired-cmd-rotate-thumbnail-options
314 "%p -rotate %d \"%t\""
315 "Format of command used to rotate thumbnail image.
316 Available options are %p which is replaced by
317 `image-dired-cmd-rotate-thumbnail-program', %d which is replaced by the
318 number of (positive) degrees to rotate the image, normally 90 or 270
319 \(for 90 degrees right and left), %t which is replaced by the file name
320 of the thumbnail file."
321 :type 'string
322 :group 'image-dired)
324 (defcustom image-dired-cmd-rotate-original-program
325 "jpegtran"
326 "Executable used to rotate original image.
327 Used together with `image-dired-cmd-rotate-original-options'."
328 :type 'string
329 :group 'image-dired)
331 (defcustom image-dired-cmd-rotate-original-options
332 "%p -rotate %d -copy all -outfile %t \"%o\""
333 "Format of command used to rotate original image.
334 Available options are %p which is replaced by
335 `image-dired-cmd-rotate-original-program', %d which is replaced by the
336 number of (positive) degrees to rotate the image, normally 90 or
337 270 \(for 90 degrees right and left), %o which is replaced by the
338 original image file name and %t which is replaced by
339 `image-dired-temp-image-file'."
340 :type 'string
341 :group 'image-dired)
343 (defcustom image-dired-temp-rotate-image-file
344 (expand-file-name ".image-dired_rotate_temp" image-dired-dir)
345 "Temporary file for rotate operations."
346 :type 'string
347 :group 'image-dired)
349 (defcustom image-dired-rotate-original-ask-before-overwrite t
350 "Confirm overwrite of original file after rotate operation.
351 If non-nil, ask user for confirmation before overwriting the
352 original file with `image-dired-temp-rotate-image-file'."
353 :type 'boolean
354 :group 'image-dired)
356 (defcustom image-dired-cmd-write-exif-data-program
357 "exiftool"
358 "Program used to write EXIF data to image.
359 Used together with `image-dired-cmd-write-exif-data-options'."
360 :type 'string
361 :group 'image-dired)
363 (defcustom image-dired-cmd-write-exif-data-options
364 "%p -%t=\"%v\" \"%f\""
365 "Format of command used to write EXIF data.
366 Available options are %p which is replaced by
367 `image-dired-cmd-write-exif-data-program', %f which is replaced by
368 the image file name, %t which is replaced by the tag name and %v
369 which is replaced by the tag value."
370 :type 'string
371 :group 'image-dired)
373 (defcustom image-dired-cmd-read-exif-data-program
374 "exiftool"
375 "Program used to read EXIF data to image.
376 Used together with `image-dired-cmd-read-exif-data-program-options'."
377 :type 'string
378 :group 'image-dired)
380 (defcustom image-dired-cmd-read-exif-data-options
381 "%p -s -s -s -%t \"%f\""
382 "Format of command used to read EXIF data.
383 Available options are %p which is replaced by
384 `image-dired-cmd-write-exif-data-program', %f which is replaced
385 by the image file name and %t which is replaced by the tag name."
386 :type 'string
387 :group 'image-dired)
389 (defcustom image-dired-gallery-hidden-tags
390 (list "private" "hidden" "pending")
391 "List of \"hidden\" tags.
392 Used by `image-dired-gallery-generate' to leave out \"hidden\" images."
393 :type '(repeat string)
394 :group 'image-dired)
396 (defcustom image-dired-thumb-size (if (eq 'standard image-dired-thumbnail-storage) 128 100)
397 "Size of thumbnails, in pixels.
398 This is the default size for both `image-dired-thumb-width'
399 and `image-dired-thumb-height'."
400 :type 'integer
401 :group 'image-dired)
403 (defcustom image-dired-thumb-width image-dired-thumb-size
404 "Width of thumbnails, in pixels."
405 :type 'integer
406 :group 'image-dired)
408 (defcustom image-dired-thumb-height image-dired-thumb-size
409 "Height of thumbnails, in pixels."
410 :type 'integer
411 :group 'image-dired)
413 (defcustom image-dired-thumb-relief 2
414 "Size of button-like border around thumbnails."
415 :type 'integer
416 :group 'image-dired)
418 (defcustom image-dired-thumb-margin 2
419 "Size of the margin around thumbnails.
420 This is where you see the cursor."
421 :type 'integer
422 :group 'image-dired)
424 (defcustom image-dired-line-up-method 'dynamic
425 "Default method for line-up of thumbnails in thumbnail buffer.
426 Used by `image-dired-display-thumbs' and other functions that needs
427 to line-up thumbnails. Dynamic means to use the available width of
428 the window containing the thumbnail buffer, Fixed means to use
429 `image-dired-thumbs-per-row', Interactive is for asking the user,
430 and No line-up means that no automatic line-up will be done."
431 :type '(choice :tag "Default line-up method"
432 (const :tag "Dynamic" dynamic)
433 (const :tag "Fixed" fixed)
434 (const :tag "Interactive" interactive)
435 (const :tag "No line-up" none))
436 :group 'image-dired)
438 (defcustom image-dired-thumbs-per-row 3
439 "Number of thumbnails to display per row in thumb buffer."
440 :type 'integer
441 :group 'image-dired)
443 (defcustom image-dired-display-window-width-correction 1
444 "Number to be used to correct image display window width.
445 Change if the default (1) does not work (i.e. if the image does not
446 completely fit)."
447 :type 'integer
448 :group 'image-dired)
450 (defcustom image-dired-display-window-height-correction 0
451 "Number to be used to correct image display window height.
452 Change if the default (0) does not work (i.e. if the image does not
453 completely fit)."
454 :type 'integer
455 :group 'image-dired)
457 (defcustom image-dired-track-movement t
458 "The current state of the tracking and mirroring.
459 For more information, see the documentation for
460 `image-dired-toggle-movement-tracking'."
461 :type 'boolean
462 :group 'image-dired)
464 (defcustom image-dired-append-when-browsing nil
465 "Append thumbnails in thumbnail buffer when browsing.
466 If non-nil, using `image-dired-next-line-and-display' and
467 `image-dired-previous-line-and-display' will leave a trail of thumbnail
468 images in the thumbnail buffer. If you enable this and want to clean
469 the thumbnail buffer because it is filled with too many thumbnails,
470 just call `image-dired-display-thumb' to display only the image at point.
471 This value can be toggled using `image-dired-toggle-append-browsing'."
472 :type 'boolean
473 :group 'image-dired)
475 (defcustom image-dired-dired-disp-props t
476 "If non-nil, display properties for dired file when browsing.
477 Used by `image-dired-next-line-and-display',
478 `image-dired-previous-line-and-display' and `image-dired-mark-and-display-next'.
479 If the database file is large, this can slow down image browsing in
480 dired and you might want to turn it off."
481 :type 'boolean
482 :group 'image-dired)
484 (defcustom image-dired-display-properties-format "%b: %f (%t): %c"
485 "Display format for thumbnail properties.
486 %b is replaced with associated dired buffer name, %f with file name
487 \(without path) of original image file, %t with the list of tags and %c
488 with the comment."
489 :type 'string
490 :group 'image-dired)
492 (defcustom image-dired-external-viewer
493 ;; TODO: Use mailcap, dired-guess-shell-alist-default,
494 ;; dired-view-command-alist.
495 (cond ((executable-find "display"))
496 ((executable-find "xli"))
497 ((executable-find "qiv") "qiv -t"))
498 "Name of external viewer.
499 Including parameters. Used when displaying original image from
500 `image-dired-thumbnail-mode'."
501 :type 'string
502 :group 'image-dired)
504 (defcustom image-dired-main-image-directory "~/pics/"
505 "Name of main image directory, if any.
506 Used by `image-dired-copy-with-exif-file-name'."
507 :type 'string
508 :group 'image-dired)
510 (defcustom image-dired-show-all-from-dir-max-files 50
511 "Maximum number of files to show using `image-dired-show-all-from-dir'
512 before warning the user."
513 :type 'integer
514 :group 'image-dired)
516 (defmacro image-dired--with-db-file (&rest body)
517 "Run BODY in a temp buffer containing `image-dired-db-file'.
518 Return the last form in BODY."
519 `(with-temp-buffer
520 (if (file-exists-p image-dired-db-file)
521 (insert-file-contents image-dired-db-file))
522 ,@body))
524 (defun image-dired-dir ()
525 "Return the current thumbnails directory (from variable `image-dired-dir').
526 Create the thumbnails directory if it does not exist."
527 (let ((image-dired-dir (file-name-as-directory
528 (expand-file-name image-dired-dir))))
529 (unless (file-directory-p image-dired-dir)
530 (make-directory image-dired-dir t)
531 (message "Creating thumbnails directory"))
532 image-dired-dir))
534 (defun image-dired-insert-image (file type relief margin)
535 "Insert image FILE of image TYPE, using RELIEF and MARGIN, at point."
537 (let ((i `(image :type ,type
538 :file ,file
539 :relief ,relief
540 :margin ,margin)))
541 (insert-image i)))
543 (defun image-dired-get-thumbnail-image (file)
544 "Return the image descriptor for a thumbnail of image file FILE."
545 (unless (string-match (image-file-name-regexp) file)
546 (error "%s is not a valid image file" file))
547 (let ((thumb-file (image-dired-thumb-name file)))
548 (unless (and (file-exists-p thumb-file)
549 (<= (float-time (nth 5 (file-attributes file)))
550 (float-time (nth 5 (file-attributes thumb-file)))))
551 (image-dired-create-thumb file thumb-file))
552 (create-image thumb-file)
553 ;; (list 'image :type 'jpeg
554 ;; :file thumb-file
555 ;; :relief image-dired-thumb-relief :margin image-dired-thumb-margin)
558 (defun image-dired-insert-thumbnail (file original-file-name
559 associated-dired-buffer)
560 "Insert thumbnail image FILE.
561 Add text properties ORIGINAL-FILE-NAME and ASSOCIATED-DIRED-BUFFER."
562 (let (beg end)
563 (setq beg (point))
564 (image-dired-insert-image file
565 ;; TODO: this should depend on the real file type
566 (if (eq 'standard image-dired-thumbnail-storage)
567 'png 'jpeg)
568 image-dired-thumb-relief
569 image-dired-thumb-margin)
570 (setq end (point))
571 (add-text-properties
572 beg end
573 (list 'image-dired-thumbnail t
574 'original-file-name original-file-name
575 'associated-dired-buffer associated-dired-buffer
576 'tags (image-dired-list-tags original-file-name)
577 'mouse-face 'highlight
578 'comment (image-dired-get-comment original-file-name)))))
580 (defun image-dired-thumb-name (file)
581 "Return thumbnail file name for FILE.
582 Depending on the value of `image-dired-thumbnail-storage', the file
583 name will vary. For central thumbnail file storage, make a
584 MD5-hash of the image file's directory name and add that to make
585 the thumbnail file name unique. For per-directory storage, just
586 add a subdirectory. For standard storage, produce the file name
587 according to the Thumbnail Managing Standard."
588 (cond ((eq 'standard image-dired-thumbnail-storage)
589 (expand-file-name
590 (concat "~/.thumbnails/normal/"
591 (md5 (concat "file://" (expand-file-name file))) ".png")))
592 ((eq 'use-image-dired-dir image-dired-thumbnail-storage)
593 (let* ((f (expand-file-name file))
594 (md5-hash
595 ;; Is MD5 hashes fast enough? The checksum of a
596 ;; thumbnail file name need not be that
597 ;; "cryptographically" good so a faster one could
598 ;; be used here.
599 (md5 (file-name-as-directory (file-name-directory f)))))
600 (format "%s%s%s.thumb.%s"
601 (file-name-as-directory (expand-file-name (image-dired-dir)))
602 (file-name-base f)
603 (if md5-hash (concat "_" md5-hash) "")
604 (file-name-extension f))))
605 ((eq 'per-directory image-dired-thumbnail-storage)
606 (let ((f (expand-file-name file)))
607 (format "%s.image-dired/%s.thumb.%s"
608 (file-name-directory f)
609 (file-name-base f)
610 (file-name-extension f))))))
612 (defun image-dired--check-executable-exists (executable)
613 (unless (executable-find (symbol-value executable))
614 (error "Executable %S not found" executable)))
616 (defun image-dired-create-thumb (original-file thumbnail-file)
617 "For ORIGINAL-FILE, create thumbnail image named THUMBNAIL-FILE."
618 (image-dired--check-executable-exists
619 'image-dired-cmd-create-thumbnail-program)
620 (let* ((width (int-to-string image-dired-thumb-width))
621 (height (int-to-string image-dired-thumb-height))
622 (modif-time (format "%.0f" (float-time (nth 5 (file-attributes
623 original-file)))))
624 (thumbnail-nq8-file (replace-regexp-in-string ".png\\'" "-nq8.png"
625 thumbnail-file))
626 (command
627 (format-spec
628 (if (eq 'standard image-dired-thumbnail-storage)
629 image-dired-cmd-create-standard-thumbnail-command
630 image-dired-cmd-create-thumbnail-options)
631 (list
632 (cons ?p image-dired-cmd-create-thumbnail-program)
633 (cons ?w width)
634 (cons ?h height)
635 (cons ?m modif-time)
636 (cons ?f original-file)
637 (cons ?q thumbnail-nq8-file)
638 (cons ?t thumbnail-file))))
639 thumbnail-dir)
640 (when (not (file-exists-p
641 (setq thumbnail-dir (file-name-directory thumbnail-file))))
642 (message "Creating thumbnail directory.")
643 (make-directory thumbnail-dir))
644 (call-process shell-file-name nil nil nil shell-command-switch command)))
646 ;;;###autoload
647 (defun image-dired-dired-toggle-marked-thumbs (&optional arg)
648 "Toggle thumbnails in front of file names in the dired buffer.
649 If no marked file could be found, insert or hide thumbnails on the
650 current line. ARG, if non-nil, specifies the files to use instead
651 of the marked files. If ARG is an integer, use the next ARG (or
652 previous -ARG, if ARG<0) files."
653 (interactive "P")
654 (dired-map-over-marks
655 (let ((image-pos (dired-move-to-filename))
656 (image-file (dired-get-filename nil t))
657 thumb-file
658 overlay)
659 (when (and image-file
660 (string-match-p (image-file-name-regexp) image-file))
661 (setq thumb-file (image-dired-get-thumbnail-image image-file))
662 ;; If image is not already added, then add it.
663 (let ((thumb-ov (cl-loop for ov in (overlays-in (point) (1+ (point)))
664 if (overlay-get ov 'thumb-file) return ov)))
665 (if thumb-ov
666 (delete-overlay thumb-ov)
667 (put-image thumb-file image-pos)
668 (setq overlay
669 (cl-loop for ov in (overlays-in (point) (1+ (point)))
670 if (overlay-get ov 'put-image) return ov))
671 (overlay-put overlay 'image-file image-file)
672 (overlay-put overlay 'thumb-file thumb-file)))))
673 arg ; Show or hide image on ARG next files.
674 'show-progress) ; Update dired display after each image is updated.
675 (add-hook 'dired-after-readin-hook
676 'image-dired-dired-after-readin-hook nil t))
678 (defun image-dired-dired-after-readin-hook ()
679 "Relocate existing thumbnail overlays in dired buffer after reverting.
680 Move them to their corresponding files if they still exist.
681 Otherwise, delete overlays."
682 (mapc (lambda (overlay)
683 (when (overlay-get overlay 'put-image)
684 (let* ((image-file (overlay-get overlay 'image-file))
685 (image-pos (dired-goto-file image-file)))
686 (if image-pos
687 (move-overlay overlay image-pos image-pos)
688 (delete-overlay overlay)))))
689 (overlays-in (point-min) (point-max))))
691 (defun image-dired-next-line-and-display ()
692 "Move to next dired line and display thumbnail image."
693 (interactive)
694 (dired-next-line 1)
695 (image-dired-display-thumbs
696 t (or image-dired-append-when-browsing nil) t)
697 (if image-dired-dired-disp-props
698 (image-dired-dired-display-properties)))
700 (defun image-dired-previous-line-and-display ()
701 "Move to previous dired line and display thumbnail image."
702 (interactive)
703 (dired-previous-line 1)
704 (image-dired-display-thumbs
705 t (or image-dired-append-when-browsing nil) t)
706 (if image-dired-dired-disp-props
707 (image-dired-dired-display-properties)))
709 (defun image-dired-toggle-append-browsing ()
710 "Toggle `image-dired-append-when-browsing'."
711 (interactive)
712 (setq image-dired-append-when-browsing
713 (not image-dired-append-when-browsing))
714 (message "Append browsing %s."
715 (if image-dired-append-when-browsing
716 "on"
717 "off")))
719 (defun image-dired-mark-and-display-next ()
720 "Mark current file in dired and display next thumbnail image."
721 (interactive)
722 (dired-mark 1)
723 (image-dired-display-thumbs
724 t (or image-dired-append-when-browsing nil) t)
725 (if image-dired-dired-disp-props
726 (image-dired-dired-display-properties)))
728 (defun image-dired-toggle-dired-display-properties ()
729 "Toggle `image-dired-dired-disp-props'."
730 (interactive)
731 (setq image-dired-dired-disp-props
732 (not image-dired-dired-disp-props))
733 (message "Dired display properties %s."
734 (if image-dired-dired-disp-props
735 "on"
736 "off")))
738 (defvar image-dired-thumbnail-buffer "*image-dired*"
739 "Image-Dired's thumbnail buffer.")
741 (defun image-dired-create-thumbnail-buffer ()
742 "Create thumb buffer and set `image-dired-thumbnail-mode'."
743 (let ((buf (get-buffer-create image-dired-thumbnail-buffer)))
744 (with-current-buffer buf
745 (setq buffer-read-only t)
746 (if (not (eq major-mode 'image-dired-thumbnail-mode))
747 (image-dired-thumbnail-mode)))
748 buf))
750 (defvar image-dired-display-image-buffer "*image-dired-display-image*"
751 "Where larger versions of the images are display.")
753 (defun image-dired-create-display-image-buffer ()
754 "Create image display buffer and set `image-dired-display-image-mode'."
755 (let ((buf (get-buffer-create image-dired-display-image-buffer)))
756 (with-current-buffer buf
757 (setq buffer-read-only t)
758 (if (not (eq major-mode 'image-dired-display-image-mode))
759 (image-dired-display-image-mode)))
760 buf))
762 (defvar image-dired-saved-window-configuration nil
763 "Saved window configuration.")
765 ;;;###autoload
766 (defun image-dired-dired-with-window-configuration (dir &optional arg)
767 "Open directory DIR and create a default window configuration.
769 Convenience command that:
771 - Opens dired in folder DIR
772 - Splits windows in most useful (?) way
773 - Set `truncate-lines' to t
775 After the command has finished, you would typically mark some
776 image files in dired and type
777 \\[image-dired-display-thumbs] (`image-dired-display-thumbs').
779 If called with prefix argument ARG, skip splitting of windows.
781 The current window configuration is saved and can be restored by
782 calling `image-dired-restore-window-configuration'."
783 (interactive "DDirectory: \nP")
784 (let ((buf (image-dired-create-thumbnail-buffer))
785 (buf2 (image-dired-create-display-image-buffer)))
786 (setq image-dired-saved-window-configuration
787 (current-window-configuration))
788 (dired dir)
789 (delete-other-windows)
790 (when (not arg)
791 (split-window-right)
792 (setq truncate-lines t)
793 (save-excursion
794 (other-window 1)
795 (pop-to-buffer-same-window buf)
796 (select-window (split-window-below))
797 (pop-to-buffer-same-window buf2)
798 (other-window -2)))))
800 (defun image-dired-restore-window-configuration ()
801 "Restore window configuration.
802 Restore any changes to the window configuration made by calling
803 `image-dired-dired-with-window-configuration'."
804 (interactive)
805 (if image-dired-saved-window-configuration
806 (set-window-configuration image-dired-saved-window-configuration)
807 (message "No saved window configuration")))
809 ;;;###autoload
810 (defun image-dired-display-thumbs (&optional arg append do-not-pop)
811 "Display thumbnails of all marked files, in `image-dired-thumbnail-buffer'.
812 If a thumbnail image does not exist for a file, it is created on the
813 fly. With prefix argument ARG, display only thumbnail for file at
814 point (this is useful if you have marked some files but want to show
815 another one).
817 Recommended usage is to split the current frame horizontally so that
818 you have the dired buffer in the left window and the
819 `image-dired-thumbnail-buffer' buffer in the right window.
821 With optional argument APPEND, append thumbnail to thumbnail buffer
822 instead of erasing it first.
824 Optional argument DO-NOT-POP controls if `pop-to-buffer' should be
825 used or not. If non-nil, use `display-buffer' instead of
826 `pop-to-buffer'. This is used from functions like
827 `image-dired-next-line-and-display' and
828 `image-dired-previous-line-and-display' where we do not want the
829 thumbnail buffer to be selected."
830 (interactive "P")
831 (let ((buf (image-dired-create-thumbnail-buffer))
832 thumb-name files dired-buf)
833 (if arg
834 (setq files (list (dired-get-filename)))
835 (setq files (dired-get-marked-files)))
836 (setq dired-buf (current-buffer))
837 (with-current-buffer buf
838 (let ((inhibit-read-only t))
839 (if (not append)
840 (erase-buffer)
841 (goto-char (point-max)))
842 (dolist (curr-file files)
843 (setq thumb-name (image-dired-thumb-name curr-file))
844 (if (and (not (file-exists-p thumb-name))
845 (not (= 0 (image-dired-create-thumb curr-file thumb-name))))
846 (message "Thumb could not be created for file %s" curr-file)
847 (image-dired-insert-thumbnail thumb-name curr-file dired-buf))))
848 (if do-not-pop
849 (display-buffer buf)
850 (pop-to-buffer buf))
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))))))
862 ;;;###autoload
863 (defun image-dired-show-all-from-dir (dir)
864 "Make a preview buffer for all images in DIR and display it.
865 If the number of files in DIR matching `image-file-name-regexp'
866 exceeds `image-dired-show-all-from-dir-max-files', a warning will be
867 displayed."
868 (interactive "DImage Dired: ")
869 (dired dir)
870 (dired-mark-files-regexp (image-file-name-regexp))
871 (let ((files (dired-get-marked-files)))
872 (if (or (<= (length files) image-dired-show-all-from-dir-max-files)
873 (and (> (length files) image-dired-show-all-from-dir-max-files)
874 (y-or-n-p
875 (format
876 "Directory contains more than %d image files. Proceed? "
877 image-dired-show-all-from-dir-max-files))))
878 (progn
879 (image-dired-display-thumbs)
880 (pop-to-buffer image-dired-thumbnail-buffer))
881 (message "Canceled."))))
883 ;;;###autoload
884 (defalias 'image-dired 'image-dired-show-all-from-dir)
886 ;;;###autoload
887 (define-obsolete-function-alias 'tumme 'image-dired "24.4")
889 (defun image-dired-sane-db-file ()
890 "Check if `image-dired-db-file' exists.
891 If not, try to create it (including any parent directories).
892 Signal error if there are problems creating it."
893 (or (file-exists-p image-dired-db-file)
894 (let (dir buf)
895 (unless (file-directory-p (setq dir (file-name-directory
896 image-dired-db-file)))
897 (make-directory dir t))
898 (with-current-buffer (setq buf (create-file-buffer
899 image-dired-db-file))
900 (write-file image-dired-db-file))
901 (kill-buffer buf)
902 (file-exists-p image-dired-db-file))
903 (error "Could not create %s" image-dired-db-file)))
905 (defun image-dired-write-tags (file-tags)
906 "Write file tags to database.
907 Write each file and tag in FILE-TAGS to the database.
908 FILE-TAGS is an alist in the following form:
909 ((FILE . TAG) ... )"
910 (image-dired-sane-db-file)
911 (let (end file tag)
912 (image-dired--with-db-file
913 (setq buffer-file-name image-dired-db-file)
914 (dolist (elt file-tags)
915 (setq file (car elt)
916 tag (cdr elt))
917 (goto-char (point-min))
918 (if (search-forward-regexp (format "^%s.*$" file) nil t)
919 (progn
920 (setq end (point))
921 (beginning-of-line)
922 (when (not (search-forward (format ";%s" tag) end t))
923 (end-of-line)
924 (insert (format ";%s" tag))))
925 (goto-char (point-max))
926 (insert (format "\n%s;%s" file tag))))
927 (save-buffer))))
929 (defun image-dired-remove-tag (files tag)
930 "For all FILES, remove TAG from the image database."
931 (image-dired-sane-db-file)
932 (image-dired--with-db-file
933 (setq buffer-file-name image-dired-db-file)
934 (let (end)
935 (unless (listp files)
936 (if (stringp files)
937 (setq files (list files))
938 (error "Files must be a string or a list of strings!")))
939 (dolist (file files)
940 (goto-char (point-min))
941 (when (search-forward-regexp (format "^%s" file) nil t)
942 (end-of-line)
943 (setq end (point))
944 (beginning-of-line)
945 (when (search-forward-regexp (format "\\(;%s\\)" tag) end t)
946 (delete-region (match-beginning 1) (match-end 1))
947 ;; Check if file should still be in the database. If
948 ;; it has no tags or comments, it will be removed.
949 (end-of-line)
950 (setq end (point))
951 (beginning-of-line)
952 (when (not (search-forward ";" end t))
953 (kill-line 1)
954 ;; If on empty line at end of buffer
955 (and (eobp)
956 (looking-at "^$")
957 (delete-char -1)))))))
958 (save-buffer)))
960 (defun image-dired-list-tags (file)
961 "Read all tags for image FILE from the image database."
962 (image-dired-sane-db-file)
963 (image-dired--with-db-file
964 (let (end (tags ""))
965 (when (search-forward-regexp (format "^%s" file) nil t)
966 (end-of-line)
967 (setq end (point))
968 (beginning-of-line)
969 (if (search-forward ";" end t)
970 (if (search-forward "comment:" end t)
971 (if (search-forward ";" end t)
972 (setq tags (buffer-substring (point) end)))
973 (setq tags (buffer-substring (point) end)))))
974 (split-string tags ";"))))
976 ;;;###autoload
977 (defun image-dired-tag-files (arg)
978 "Tag marked file(s) in dired. With prefix ARG, tag file at point."
979 (interactive "P")
980 (let ((tag (read-string "Tags to add (separate tags with a semicolon): "))
981 files)
982 (if arg
983 (setq files (list (dired-get-filename)))
984 (setq files (dired-get-marked-files)))
985 (image-dired-write-tags
986 (mapcar
987 (lambda (x)
988 (cons x tag))
989 files))))
991 (defun image-dired-tag-thumbnail ()
992 "Tag current thumbnail."
993 (interactive)
994 (let ((tag (read-string "Tags to add (separate tags with a semicolon): ")))
995 (image-dired-write-tags (list (cons (image-dired-original-file-name) tag))))
996 (image-dired-update-property
997 'tags (image-dired-list-tags (image-dired-original-file-name))))
999 ;;;###autoload
1000 (defun image-dired-delete-tag (arg)
1001 "Remove tag for selected file(s).
1002 With prefix argument ARG, remove tag from file at point."
1003 (interactive "P")
1004 (let ((tag (read-string "Tag to remove: "))
1005 files)
1006 (if arg
1007 (setq files (list (dired-get-filename)))
1008 (setq files (dired-get-marked-files)))
1009 (image-dired-remove-tag files tag)))
1011 (defun image-dired-tag-thumbnail-remove ()
1012 "Remove tag from thumbnail."
1013 (interactive)
1014 (let ((tag (read-string "Tag to remove: ")))
1015 (image-dired-remove-tag (image-dired-original-file-name) tag))
1016 (image-dired-update-property
1017 'tags (image-dired-list-tags (image-dired-original-file-name))))
1019 (defun image-dired-original-file-name ()
1020 "Get original file name for thumbnail or display image at point."
1021 (get-text-property (point) 'original-file-name))
1023 (defun image-dired-associated-dired-buffer ()
1024 "Get associated dired buffer at point."
1025 (get-text-property (point) 'associated-dired-buffer))
1027 (defun image-dired-get-buffer-window (buf)
1028 "Return window where buffer BUF is."
1029 (get-window-with-predicate
1030 (lambda (window)
1031 (equal (window-buffer window) buf))
1032 nil t))
1034 (defun image-dired-track-original-file ()
1035 "Track the original file in the associated dired buffer.
1036 See documentation for `image-dired-toggle-movement-tracking'.
1037 Interactive use only useful if `image-dired-track-movement' is nil."
1038 (interactive)
1039 (let* ((dired-buf (image-dired-associated-dired-buffer))
1040 (file-name (image-dired-original-file-name))
1041 (window (image-dired-get-buffer-window dired-buf)))
1042 (and (buffer-live-p dired-buf) file-name
1043 (with-current-buffer dired-buf
1044 (if (not (dired-goto-file file-name))
1045 (message "Could not track file")
1046 (if window (set-window-point window (point))))))))
1048 (defun image-dired-toggle-movement-tracking ()
1049 "Turn on and off `image-dired-track-movement'.
1050 Tracking of the movements between thumbnail and dired buffer so that
1051 they are \"mirrored\" in the dired buffer. When this is on, moving
1052 around in the thumbnail or dired buffer will find the matching
1053 position in the other buffer."
1054 (interactive)
1055 (setq image-dired-track-movement (not image-dired-track-movement))
1056 (message "Tracking %s" (if image-dired-track-movement "on" "off")))
1058 (defun image-dired-track-thumbnail ()
1059 "Track current dired file's thumb in `image-dired-thumbnail-buffer'.
1060 This is almost the same as what `image-dired-track-original-file' does,
1061 but the other way around."
1062 (let ((file (dired-get-filename))
1063 prop-val found window)
1064 (when (get-buffer image-dired-thumbnail-buffer)
1065 (with-current-buffer image-dired-thumbnail-buffer
1066 (goto-char (point-min))
1067 (while (and (not (eobp))
1068 (not found))
1069 (if (and (setq prop-val
1070 (get-text-property (point) 'original-file-name))
1071 (string= prop-val file))
1072 (setq found t))
1073 (if (not found)
1074 (forward-char 1)))
1075 (when found
1076 (if (setq window (image-dired-thumbnail-window))
1077 (set-window-point window (point)))
1078 (image-dired-display-thumb-properties))))))
1080 (defun image-dired-dired-next-line (&optional arg)
1081 "Call `dired-next-line', then track thumbnail.
1082 This can safely replace `dired-next-line'.
1083 With prefix argument, move ARG lines."
1084 (interactive "P")
1085 (dired-next-line (or arg 1))
1086 (if image-dired-track-movement
1087 (image-dired-track-thumbnail)))
1089 (defun image-dired-dired-previous-line (&optional arg)
1090 "Call `dired-previous-line', then track thumbnail.
1091 This can safely replace `dired-previous-line'.
1092 With prefix argument, move ARG lines."
1093 (interactive "P")
1094 (dired-previous-line (or arg 1))
1095 (if image-dired-track-movement
1096 (image-dired-track-thumbnail)))
1098 (defun image-dired-forward-image (&optional arg)
1099 "Move to next image and display properties.
1100 Optional prefix ARG says how many images to move; default is one
1101 image."
1102 (interactive "p")
1103 (let (pos (steps (or arg 1)))
1104 (dotimes (_ steps)
1105 (if (and (not (eobp))
1106 (save-excursion
1107 (forward-char)
1108 (while (and (not (eobp))
1109 (not (image-dired-image-at-point-p)))
1110 (forward-char))
1111 (setq pos (point))
1112 (image-dired-image-at-point-p)))
1113 (goto-char pos)
1114 (error "At last image"))))
1115 (when image-dired-track-movement
1116 (image-dired-track-original-file))
1117 (image-dired-display-thumb-properties))
1119 (defun image-dired-backward-image (&optional arg)
1120 "Move to previous image and display properties.
1121 Optional prefix ARG says how many images to move; default is one
1122 image."
1123 (interactive "p")
1124 (let (pos (steps (or arg 1)))
1125 (dotimes (_ steps)
1126 (if (and (not (bobp))
1127 (save-excursion
1128 (backward-char)
1129 (while (and (not (bobp))
1130 (not (image-dired-image-at-point-p)))
1131 (backward-char))
1132 (setq pos (point))
1133 (image-dired-image-at-point-p)))
1134 (goto-char pos)
1135 (error "At first image"))))
1136 (when image-dired-track-movement
1137 (image-dired-track-original-file))
1138 (image-dired-display-thumb-properties))
1140 (defun image-dired-next-line ()
1141 "Move to next line and display properties."
1142 (interactive)
1143 (let ((goal-column (current-column)))
1144 (forward-line 1)
1145 (move-to-column goal-column))
1146 ;; If we end up in an empty spot, back up to the next thumbnail.
1147 (if (not (image-dired-image-at-point-p))
1148 (image-dired-backward-image))
1149 (if image-dired-track-movement
1150 (image-dired-track-original-file))
1151 (image-dired-display-thumb-properties))
1154 (defun image-dired-previous-line ()
1155 "Move to previous line and display properties."
1156 (interactive)
1157 (let ((goal-column (current-column)))
1158 (forward-line -1)
1159 (move-to-column goal-column))
1160 ;; If we end up in an empty spot, back up to the next
1161 ;; thumbnail. This should only happen if the user deleted a
1162 ;; thumbnail and did not refresh, so it is not very common. But we
1163 ;; can handle it in a good manner, so why not?
1164 (if (not (image-dired-image-at-point-p))
1165 (image-dired-backward-image))
1166 (if image-dired-track-movement
1167 (image-dired-track-original-file))
1168 (image-dired-display-thumb-properties))
1170 (defun image-dired-format-properties-string (buf file props comment)
1171 "Format display properties.
1172 BUF is the associated dired buffer, FILE is the original image file
1173 name, PROPS is a list of tags and COMMENT is the image file's
1174 comment."
1175 (format-spec
1176 image-dired-display-properties-format
1177 (list
1178 (cons ?b (or buf ""))
1179 (cons ?f file)
1180 (cons ?t (or (princ props) ""))
1181 (cons ?c (or comment "")))))
1183 (defun image-dired-display-thumb-properties ()
1184 "Display thumbnail properties in the echo area."
1185 (if (not (eobp))
1186 (let ((file-name (file-name-nondirectory (image-dired-original-file-name)))
1187 (dired-buf (buffer-name (image-dired-associated-dired-buffer)))
1188 (props (mapconcat
1189 'princ
1190 (get-text-property (point) 'tags)
1191 ", "))
1192 (comment (get-text-property (point) 'comment)))
1193 (if file-name
1194 (message "%s"
1195 (image-dired-format-properties-string
1196 dired-buf
1197 file-name
1198 props
1199 comment))))))
1201 (defun image-dired-dired-file-marked-p ()
1202 "Check whether file on current line is marked or not."
1203 (save-excursion
1204 (beginning-of-line)
1205 (not (looking-at "^ .*$"))))
1207 (defun image-dired-modify-mark-on-thumb-original-file (command)
1208 "Modify mark in dired buffer.
1209 COMMAND is one of `mark' for marking file in dired, `unmark' for
1210 unmarking file in dired or `flag' for flagging file for delete in
1211 dired."
1212 (let ((file-name (image-dired-original-file-name))
1213 (dired-buf (image-dired-associated-dired-buffer)))
1214 (if (not (and dired-buf file-name))
1215 (message "No image, or image with correct properties, at point.")
1216 (with-current-buffer dired-buf
1217 (message "%s" file-name)
1218 (if (dired-goto-file file-name)
1219 (cond ((eq command 'mark) (dired-mark 1))
1220 ((eq command 'unmark) (dired-unmark 1))
1221 ((eq command 'toggle)
1222 (if (image-dired-dired-file-marked-p)
1223 (dired-unmark 1)
1224 (dired-mark 1)))
1225 ((eq command 'flag) (dired-flag-file-deletion 1))))))))
1227 (defun image-dired-mark-thumb-original-file ()
1228 "Mark original image file in associated dired buffer."
1229 (interactive)
1230 (image-dired-modify-mark-on-thumb-original-file 'mark)
1231 (image-dired-forward-image))
1233 (defun image-dired-unmark-thumb-original-file ()
1234 "Unmark original image file in associated dired buffer."
1235 (interactive)
1236 (image-dired-modify-mark-on-thumb-original-file 'unmark)
1237 (image-dired-forward-image))
1239 (defun image-dired-flag-thumb-original-file ()
1240 "Flag original image file for deletion in associated dired buffer."
1241 (interactive)
1242 (image-dired-modify-mark-on-thumb-original-file 'flag)
1243 (image-dired-forward-image))
1245 (defun image-dired-toggle-mark-thumb-original-file ()
1246 "Toggle mark on original image file in associated dired buffer."
1247 (interactive)
1248 (image-dired-modify-mark-on-thumb-original-file 'toggle))
1250 (defun image-dired-jump-original-dired-buffer ()
1251 "Jump to the dired buffer associated with the current image file.
1252 You probably want to use this together with
1253 `image-dired-track-original-file'."
1254 (interactive)
1255 (let ((buf (image-dired-associated-dired-buffer))
1256 window frame)
1257 (setq window (image-dired-get-buffer-window buf))
1258 (if window
1259 (progn
1260 (if (not (equal (selected-frame) (setq frame (window-frame window))))
1261 (select-frame-set-input-focus frame))
1262 (select-window window))
1263 (message "Associated dired buffer not visible"))))
1265 ;;;###autoload
1266 (defun image-dired-jump-thumbnail-buffer ()
1267 "Jump to thumbnail buffer."
1268 (interactive)
1269 (let ((window (image-dired-thumbnail-window))
1270 frame)
1271 (if window
1272 (progn
1273 (if (not (equal (selected-frame) (setq frame (window-frame window))))
1274 (select-frame-set-input-focus frame))
1275 (select-window window))
1276 (message "Thumbnail buffer not visible"))))
1278 (defvar image-dired-thumbnail-mode-line-up-map
1279 (let ((map (make-sparse-keymap)))
1280 ;; map it to "g" so that the user can press it more quickly
1281 (define-key map "g" 'image-dired-line-up-dynamic)
1282 ;; "f" for "fixed" number of thumbs per row
1283 (define-key map "f" 'image-dired-line-up)
1284 ;; "i" for "interactive"
1285 (define-key map "i" 'image-dired-line-up-interactive)
1286 map)
1287 "Keymap for line-up commands in `image-dired-thumbnail-mode'.")
1289 (defvar image-dired-thumbnail-mode-tag-map
1290 (let ((map (make-sparse-keymap)))
1291 ;; map it to "t" so that the user can press it more quickly
1292 (define-key map "t" 'image-dired-tag-thumbnail)
1293 ;; "r" for "remove"
1294 (define-key map "r" 'image-dired-tag-thumbnail-remove)
1295 map)
1296 "Keymap for tag commands in `image-dired-thumbnail-mode'.")
1298 (defvar image-dired-thumbnail-mode-map
1299 (let ((map (make-sparse-keymap)))
1300 (define-key map [right] 'image-dired-forward-image)
1301 (define-key map [left] 'image-dired-backward-image)
1302 (define-key map [up] 'image-dired-previous-line)
1303 (define-key map [down] 'image-dired-next-line)
1304 (define-key map "\C-f" 'image-dired-forward-image)
1305 (define-key map "\C-b" 'image-dired-backward-image)
1306 (define-key map "\C-p" 'image-dired-previous-line)
1307 (define-key map "\C-n" 'image-dired-next-line)
1309 (define-key map "d" 'image-dired-flag-thumb-original-file)
1310 (define-key map [delete] 'image-dired-flag-thumb-original-file)
1311 (define-key map "m" 'image-dired-mark-thumb-original-file)
1312 (define-key map "u" 'image-dired-unmark-thumb-original-file)
1313 (define-key map "." 'image-dired-track-original-file)
1314 (define-key map [tab] 'image-dired-jump-original-dired-buffer)
1316 ;; add line-up map
1317 (define-key map "g" image-dired-thumbnail-mode-line-up-map)
1318 ;; add tag map
1319 (define-key map "t" image-dired-thumbnail-mode-tag-map)
1321 (define-key map "\C-m" 'image-dired-display-thumbnail-original-image)
1322 (define-key map [C-return] 'image-dired-thumbnail-display-external)
1324 (define-key map "l" 'image-dired-rotate-thumbnail-left)
1325 (define-key map "r" 'image-dired-rotate-thumbnail-right)
1326 (define-key map "L" 'image-dired-rotate-original-left)
1327 (define-key map "R" 'image-dired-rotate-original-right)
1329 (define-key map "D" 'image-dired-thumbnail-set-image-description)
1330 (define-key map "\C-d" 'image-dired-delete-char)
1331 (define-key map " " 'image-dired-display-next-thumbnail-original)
1332 (define-key map (kbd "DEL") 'image-dired-display-previous-thumbnail-original)
1333 (define-key map "c" 'image-dired-comment-thumbnail)
1334 (define-key map "q" 'image-dired-kill-buffer-and-window)
1336 ;; Mouse
1337 (define-key map [mouse-2] 'image-dired-mouse-display-image)
1338 (define-key map [mouse-1] 'image-dired-mouse-select-thumbnail)
1339 ;; Seems I must first set C-down-mouse-1 to undefined, or else it
1340 ;; will trigger the buffer menu. If I try to instead bind
1341 ;; C-down-mouse-1 to `image-dired-mouse-toggle-mark', I get a message
1342 ;; about C-mouse-1 not being defined afterwards. Annoying, but I
1343 ;; probably do not completely understand mouse events.
1344 (define-key map [C-down-mouse-1] 'undefined)
1345 (define-key map [C-mouse-1] 'image-dired-mouse-toggle-mark)
1347 ;; Menu
1348 (easy-menu-define nil map
1349 "Menu for `image-dired-thumbnail-mode'."
1350 '("Image-Dired"
1351 ["Quit" image-dired-kill-buffer-and-window]
1352 ["Delete thumbnail from buffer" image-dired-delete-char]
1353 ["Remove tag from thumbnail" image-dired-tag-thumbnail-remove]
1354 ["Tag thumbnail" image-dired-tag-thumbnail]
1355 ["Comment thumbnail" image-dired-comment-thumbnail]
1356 ["Refresh thumb" image-dired-refresh-thumb]
1357 ["Dynamic line up" image-dired-line-up-dynamic]
1358 ["Line up thumbnails" image-dired-line-up]
1360 ["Rotate thumbnail left" image-dired-rotate-thumbnail-left]
1361 ["Rotate thumbnail right" image-dired-rotate-thumbnail-right]
1362 ["Rotate original left" image-dired-rotate-original-left]
1363 ["Rotate original right" image-dired-rotate-original-right]
1365 ["Toggle movement tracking on/off" image-dired-toggle-movement-tracking]
1367 ["Jump to dired buffer" image-dired-jump-original-dired-buffer]
1368 ["Track original" image-dired-track-original-file]
1370 ["Flag original for deletion" image-dired-flag-thumb-original-file]
1371 ["Unmark original" image-dired-unmark-thumb-original-file]
1372 ["Mark original" image-dired-mark-thumb-original-file]
1374 ["Display in external viewer" image-dired-thumbnail-display-external]
1375 ["Display image" image-dired-display-thumbnail-original-image]))
1376 map)
1377 "Keymap for `image-dired-thumbnail-mode'.")
1379 (defvar image-dired-display-image-mode-map
1380 (let ((map (make-sparse-keymap)))
1381 (define-key map "q" 'image-dired-kill-buffer-and-window)
1382 (define-key map "f" 'image-dired-display-current-image-full)
1383 (define-key map "s" 'image-dired-display-current-image-sized)
1385 (easy-menu-define nil map
1386 "Menu for `image-dired-display-image-mode-map'."
1387 '("Image-Dired"
1388 ["Quit" image-dired-kill-buffer-and-window]
1389 ["Display original, sized to fit" image-dired-display-current-image-sized]
1390 ["Display original, full size" image-dired-display-current-image-full]))
1391 map)
1392 "Keymap for `image-dired-display-image-mode'.")
1394 (defun image-dired-display-current-image-full ()
1395 "Display current image in full size."
1396 (interactive)
1397 (let ((file (image-dired-original-file-name)))
1398 (if file
1399 (progn
1400 (image-dired-display-image file t)
1401 (message "Full size image displayed"))
1402 (error "No original file name at point"))))
1404 (defun image-dired-display-current-image-sized ()
1405 "Display current image in sized to fit window dimensions."
1406 (interactive)
1407 (let ((file (image-dired-original-file-name)))
1408 (if file
1409 (progn
1410 (image-dired-display-image file)
1411 (message "Fitted image displayed"))
1412 (error "No original file name at point"))))
1414 (define-derived-mode image-dired-thumbnail-mode
1415 fundamental-mode "image-dired-thumbnail"
1416 "Browse and manipulate thumbnail images using dired.
1417 Use `image-dired-dired' and `image-dired-setup-dired-keybindings' to get a
1418 nice setup to start with."
1419 (message "image-dired-thumbnail-mode enabled"))
1421 (define-derived-mode image-dired-display-image-mode
1422 fundamental-mode "image-dired-image-display"
1423 "Mode for displaying and manipulating original image.
1424 Resized or in full-size."
1425 (message "image-dired-display-image-mode enabled"))
1427 ;;;###autoload
1428 (defun image-dired-setup-dired-keybindings ()
1429 "Setup easy-to-use keybindings for the commands to be used in dired mode.
1430 Note that n, p and <down> and <up> will be hijacked and bound to
1431 `image-dired-dired-x-line'."
1432 (interactive)
1434 ;; Hijack previous and next line movement. Let C-p and C-b be
1435 ;; though...
1437 (define-key dired-mode-map "p" 'image-dired-dired-previous-line)
1438 (define-key dired-mode-map "n" 'image-dired-dired-next-line)
1439 (define-key dired-mode-map [up] 'image-dired-dired-previous-line)
1440 (define-key dired-mode-map [down] 'image-dired-dired-next-line)
1442 (define-key dired-mode-map (kbd "C-S-n") 'image-dired-next-line-and-display)
1443 (define-key dired-mode-map (kbd "C-S-p") 'image-dired-previous-line-and-display)
1444 (define-key dired-mode-map (kbd "C-S-m") 'image-dired-mark-and-display-next)
1446 (define-key dired-mode-map "\C-td" 'image-dired-display-thumbs)
1447 (define-key dired-mode-map "\C-tt" 'image-dired-tag-files)
1448 (define-key dired-mode-map "\C-tr" 'image-dired-delete-tag)
1449 (define-key dired-mode-map [tab] 'image-dired-jump-thumbnail-buffer)
1450 (define-key dired-mode-map "\C-ti" 'image-dired-dired-display-image)
1451 (define-key dired-mode-map "\C-tx" 'image-dired-dired-display-external)
1452 (define-key dired-mode-map "\C-ta" 'image-dired-display-thumbs-append)
1453 (define-key dired-mode-map "\C-t." 'image-dired-display-thumb)
1454 (define-key dired-mode-map "\C-tc" 'image-dired-dired-comment-files)
1455 (define-key dired-mode-map "\C-tf" 'image-dired-mark-tagged-files)
1457 ;; Menu for dired
1458 (define-key dired-mode-map [menu-bar image-dired]
1459 (cons "Image-Dired" (make-sparse-keymap "Image-Dired")))
1461 (define-key dired-mode-map [menu-bar image-dired image-dired-copy-with-exif-file-name]
1462 '("Copy with EXIF file name" . image-dired-copy-with-exif-file-name))
1464 (define-key dired-mode-map [menu-bar image-dired image-dired-dired-comment-files]
1465 '("Comment files" . image-dired-dired-comment-files))
1467 (define-key dired-mode-map [menu-bar image-dired image-dired-mark-tagged-files]
1468 '("Mark tagged files" . image-dired-mark-tagged-files))
1470 (define-key dired-mode-map [menu-bar image-dired image-dired-delete-tag]
1471 '("Remove tag from files" . image-dired-delete-tag))
1473 (define-key dired-mode-map [menu-bar image-dired image-dired-tag-files]
1474 '("Tag files" . image-dired-tag-files))
1476 (define-key dired-mode-map [menu-bar image-dired image-dired-jump-thumbnail-buffer]
1477 '("Jump to thumbnail buffer" . image-dired-jump-thumbnail-buffer))
1479 (define-key dired-mode-map [menu-bar image-dired image-dired-toggle-movement-tracking]
1480 '("Toggle movement tracking" . image-dired-toggle-movement-tracking))
1482 (define-key dired-mode-map
1483 [menu-bar image-dired image-dired-toggle-append-browsing]
1484 '("Toggle append browsing" . image-dired-toggle-append-browsing))
1486 (define-key dired-mode-map
1487 [menu-bar image-dired image-dired-toggle-disp-props]
1488 '("Toggle display properties" . image-dired-toggle-dired-display-properties))
1490 (define-key dired-mode-map
1491 [menu-bar image-dired image-dired-dired-display-external]
1492 '("Display in external viewer" . image-dired-dired-display-external))
1493 (define-key dired-mode-map
1494 [menu-bar image-dired image-dired-dired-display-image]
1495 '("Display image" . image-dired-dired-display-image))
1496 (define-key dired-mode-map
1497 [menu-bar image-dired image-dired-display-thumb]
1498 '("Display this thumbnail" . image-dired-display-thumb))
1499 (define-key dired-mode-map
1500 [menu-bar image-dired image-dired-display-thumbs-append]
1501 '("Display thumbnails append" . image-dired-display-thumbs-append))
1502 (define-key dired-mode-map
1503 [menu-bar image-dired image-dired-display-thumbs]
1504 '("Display thumbnails" . image-dired-display-thumbs))
1506 (define-key dired-mode-map
1507 [menu-bar image-dired image-dired-create-thumbs]
1508 '("Create thumbnails for marked files" . image-dired-create-thumbs))
1510 (define-key dired-mode-map
1511 [menu-bar image-dired image-dired-mark-and-display-next]
1512 '("Mark and display next" . image-dired-mark-and-display-next))
1513 (define-key dired-mode-map
1514 [menu-bar image-dired image-dired-previous-line-and-display]
1515 '("Display thumb for previous file" . image-dired-previous-line-and-display))
1516 (define-key dired-mode-map
1517 [menu-bar image-dired image-dired-next-line-and-display]
1518 '("Display thumb for next file" . image-dired-next-line-and-display)))
1520 (declare-function clear-image-cache "image.c" (&optional filter))
1522 (defun image-dired-create-thumbs (&optional arg)
1523 "Create thumbnail images for all marked files in dired.
1524 With prefix argument ARG, create thumbnails even if they already exist
1525 \(i.e. use this to refresh your thumbnails)."
1526 (interactive "P")
1527 (let (thumb-name)
1528 (dolist (curr-file (dired-get-marked-files))
1529 (setq thumb-name (image-dired-thumb-name curr-file))
1530 ;; If the user overrides the exist check, we must clear the
1531 ;; image cache so that if the user wants to display the
1532 ;; thumbnail, it is not fetched from cache.
1533 (if arg
1534 (clear-image-cache))
1535 (when (or (not (file-exists-p thumb-name))
1536 arg)
1537 (when (not (= 0 (image-dired-create-thumb curr-file thumb-name)))
1538 (error "Thumb could not be created"))))))
1540 (defvar image-dired-slideshow-timer nil
1541 "Slideshow timer.")
1543 (defvar image-dired-slideshow-count 0
1544 "Keeping track on number of images in slideshow.")
1546 (defvar image-dired-slideshow-times 0
1547 "Number of pictures to display in slideshow.")
1549 (defun image-dired-slideshow-step ()
1550 "Step to next file, if `image-dired-slideshow-times' has not been reached."
1551 (if (< image-dired-slideshow-count image-dired-slideshow-times)
1552 (progn
1553 (message "%s" (1+ image-dired-slideshow-count))
1554 (setq image-dired-slideshow-count (1+ image-dired-slideshow-count))
1555 (image-dired-next-line-and-display))
1556 (image-dired-slideshow-stop)))
1558 (defun image-dired-slideshow-start ()
1559 "Start slideshow.
1560 Ask user for number of images to show and the delay in between."
1561 (interactive)
1562 (setq image-dired-slideshow-count 0)
1563 (setq image-dired-slideshow-times (string-to-number (read-string "How many: ")))
1564 (let ((repeat (string-to-number
1565 (read-string
1566 "Delay, in seconds. Decimals are accepted : " "1"))))
1567 (setq image-dired-slideshow-timer
1568 (run-with-timer
1569 0 repeat
1570 'image-dired-slideshow-step))))
1572 (defun image-dired-slideshow-stop ()
1573 "Cancel slideshow."
1574 (interactive)
1575 (cancel-timer image-dired-slideshow-timer))
1577 (defun image-dired-delete-char ()
1578 "Remove current thumbnail from thumbnail buffer and line up."
1579 (interactive)
1580 (let ((inhibit-read-only t))
1581 (delete-char 1)
1582 (if (looking-at " ")
1583 (delete-char 1))))
1585 ;;;###autoload
1586 (defun image-dired-display-thumbs-append ()
1587 "Append thumbnails to `image-dired-thumbnail-buffer'."
1588 (interactive)
1589 (image-dired-display-thumbs nil t t))
1591 ;;;###autoload
1592 (defun image-dired-display-thumb ()
1593 "Shorthand for `image-dired-display-thumbs' with prefix argument."
1594 (interactive)
1595 (image-dired-display-thumbs t nil t))
1597 (defun image-dired-line-up ()
1598 "Line up thumbnails according to `image-dired-thumbs-per-row'.
1599 See also `image-dired-line-up-dynamic'."
1600 (interactive)
1601 (let ((inhibit-read-only t))
1602 (goto-char (point-min))
1603 (while (and (not (image-dired-image-at-point-p))
1604 (not (eobp)))
1605 (delete-char 1))
1606 (while (not (eobp))
1607 (forward-char)
1608 (while (and (not (image-dired-image-at-point-p))
1609 (not (eobp)))
1610 (delete-char 1)))
1611 (goto-char (point-min))
1612 (let ((count 0))
1613 (while (not (eobp))
1614 (forward-char)
1615 (if (= image-dired-thumbs-per-row 1)
1616 (insert "\n")
1617 (insert " ")
1618 (setq count (1+ count))
1619 (when (and (= count (- image-dired-thumbs-per-row 1))
1620 (not (eobp)))
1621 (forward-char)
1622 (insert "\n")
1623 (setq count 0)))))
1624 (goto-char (point-min))))
1626 (defun image-dired-line-up-dynamic ()
1627 "Line up thumbnails images dynamically.
1628 Calculate how many thumbnails fit."
1629 (interactive)
1630 (let* ((char-width (frame-char-width))
1631 (width (image-dired-window-width-pixels (image-dired-thumbnail-window)))
1632 (image-dired-thumbs-per-row
1633 (/ width
1634 (+ (* 2 image-dired-thumb-relief)
1635 (* 2 image-dired-thumb-margin)
1636 image-dired-thumb-width char-width))))
1637 (image-dired-line-up)))
1639 (defun image-dired-line-up-interactive ()
1640 "Line up thumbnails interactively.
1641 Ask user how many thumbnails should be displayed per row."
1642 (interactive)
1643 (let ((image-dired-thumbs-per-row
1644 (string-to-number (read-string "How many thumbs per row: "))))
1645 (if (not (> image-dired-thumbs-per-row 0))
1646 (message "Number must be greater than 0")
1647 (image-dired-line-up))))
1649 (defun image-dired-thumbnail-display-external ()
1650 "Display original image for thumbnail at point using external viewer."
1651 (interactive)
1652 (let ((file (image-dired-original-file-name)))
1653 (if (not (image-dired-image-at-point-p))
1654 (message "No thumbnail at point")
1655 (if (not file)
1656 (message "No original file name found")
1657 (call-process shell-file-name nil nil nil shell-command-switch
1658 (format "%s \"%s\"" image-dired-external-viewer file))))))
1660 ;;;###autoload
1661 (defun image-dired-dired-display-external ()
1662 "Display file at point using an external viewer."
1663 (interactive)
1664 (let ((file (dired-get-filename)))
1665 (call-process shell-file-name nil nil nil shell-command-switch
1666 (format "%s \"%s\"" image-dired-external-viewer file))))
1668 (defun image-dired-window-width-pixels (window)
1669 "Calculate WINDOW width in pixels."
1670 (* (window-width window) (frame-char-width)))
1672 (defun image-dired-window-height-pixels (window)
1673 "Calculate WINDOW height in pixels."
1674 ;; Note: The mode-line consumes one line
1675 (* (- (window-height window) 1) (frame-char-height)))
1677 (defun image-dired-display-window ()
1678 "Return window where `image-dired-display-image-buffer' is visible."
1679 (get-window-with-predicate
1680 (lambda (window)
1681 (equal (buffer-name (window-buffer window)) image-dired-display-image-buffer))
1682 nil t))
1684 (defun image-dired-thumbnail-window ()
1685 "Return window where `image-dired-thumbnail-buffer' is visible."
1686 (get-window-with-predicate
1687 (lambda (window)
1688 (equal (buffer-name (window-buffer window)) image-dired-thumbnail-buffer))
1689 nil t))
1691 (defun image-dired-associated-dired-buffer-window ()
1692 "Return window where associated dired buffer is visible."
1693 (let (buf)
1694 (if (image-dired-image-at-point-p)
1695 (progn
1696 (setq buf (image-dired-associated-dired-buffer))
1697 (get-window-with-predicate
1698 (lambda (window)
1699 (equal (window-buffer window) buf))))
1700 (error "No thumbnail image at point"))))
1702 (defun image-dired-display-window-width ()
1703 "Return width, in pixels, of image-dired's image display window."
1704 (- (image-dired-window-width-pixels (image-dired-display-window))
1705 image-dired-display-window-width-correction))
1707 (defun image-dired-display-window-height ()
1708 "Return height, in pixels, of image-dired's image display window."
1709 (- (image-dired-window-height-pixels (image-dired-display-window))
1710 image-dired-display-window-height-correction))
1712 (defun image-dired-display-image (file &optional original-size)
1713 "Display image FILE in image buffer.
1714 Use this when you want to display the image, semi sized, in a new
1715 window. The image is sized to fit the display window (using a
1716 temporary file, don't worry). Because of this, it will not be as
1717 quick as opening it directly, but on most modern systems it
1718 should feel snappy enough.
1720 If optional argument ORIGINAL-SIZE is non-nil, display image in its
1721 original size."
1722 (image-dired--check-executable-exists
1723 'image-dired-cmd-create-temp-image-program)
1724 (let ((new-file (expand-file-name image-dired-temp-image-file))
1725 width height command ret
1726 (image-type 'jpeg))
1727 (setq file (expand-file-name file))
1728 (if (not original-size)
1729 (progn
1730 (setq width (image-dired-display-window-width))
1731 (setq height (image-dired-display-window-height))
1732 (setq command
1733 (format-spec
1734 image-dired-cmd-create-temp-image-options
1735 (list
1736 (cons ?p image-dired-cmd-create-temp-image-program)
1737 (cons ?w width)
1738 (cons ?h height)
1739 (cons ?f file)
1740 (cons ?t new-file))))
1741 (setq ret (call-process shell-file-name nil nil nil
1742 shell-command-switch command))
1743 (if (not (= 0 ret))
1744 (error "Could not resize image")))
1745 (setq image-type (image-type-from-file-name file))
1746 (copy-file file new-file t))
1747 (with-current-buffer (image-dired-create-display-image-buffer)
1748 (let ((inhibit-read-only t))
1749 (erase-buffer)
1750 (clear-image-cache)
1751 (image-dired-insert-image image-dired-temp-image-file image-type 0 0)
1752 (goto-char (point-min))
1753 (image-dired-update-property 'original-file-name file)))))
1755 (defun image-dired-display-thumbnail-original-image (&optional arg)
1756 "Display current thumbnail's original image in display buffer.
1757 See documentation for `image-dired-display-image' for more information.
1758 With prefix argument ARG, display image in its original size."
1759 (interactive "P")
1760 (let ((file (image-dired-original-file-name)))
1761 (if (not (string-equal major-mode "image-dired-thumbnail-mode"))
1762 (message "Not in image-dired-thumbnail-mode")
1763 (if (not (image-dired-image-at-point-p))
1764 (message "No thumbnail at point")
1765 (if (not file)
1766 (message "No original file name found")
1767 (image-dired-create-display-image-buffer)
1768 (display-buffer image-dired-display-image-buffer)
1769 (image-dired-display-image file arg))))))
1772 ;;;###autoload
1773 (defun image-dired-dired-display-image (&optional arg)
1774 "Display current image file.
1775 See documentation for `image-dired-display-image' for more information.
1776 With prefix argument ARG, display image in its original size."
1777 (interactive "P")
1778 (image-dired-create-display-image-buffer)
1779 (display-buffer image-dired-display-image-buffer)
1780 (image-dired-display-image (dired-get-filename) arg))
1782 (defun image-dired-image-at-point-p ()
1783 "Return true if there is an image-dired thumbnail at point."
1784 (get-text-property (point) 'image-dired-thumbnail))
1786 (defun image-dired-rotate-thumbnail (degrees)
1787 "Rotate thumbnail DEGREES degrees."
1788 (image-dired--check-executable-exists
1789 'image-dired-cmd-rotate-thumbnail-program)
1790 (if (not (image-dired-image-at-point-p))
1791 (message "No thumbnail at point")
1792 (let ((file (image-dired-thumb-name (image-dired-original-file-name)))
1793 command)
1794 (setq command (format-spec
1795 image-dired-cmd-rotate-thumbnail-options
1796 (list
1797 (cons ?p image-dired-cmd-rotate-thumbnail-program)
1798 (cons ?d degrees)
1799 (cons ?t (expand-file-name file)))))
1800 (call-process shell-file-name nil nil nil shell-command-switch command)
1801 ;; Clear the cache to refresh image. I wish I could just refresh
1802 ;; the current file but I do not know how to do that. Yet...
1803 (clear-image-cache))))
1805 (defun image-dired-rotate-thumbnail-left ()
1806 "Rotate thumbnail left (counter clockwise) 90 degrees.
1807 The result of the rotation is displayed in the image display area
1808 and a confirmation is needed before the original image files is
1809 overwritten. This confirmation can be turned off using
1810 `image-dired-rotate-original-ask-before-overwrite'."
1811 (interactive)
1812 (image-dired-rotate-thumbnail "270"))
1814 (defun image-dired-rotate-thumbnail-right ()
1815 "Rotate thumbnail counter right (clockwise) 90 degrees.
1816 The result of the rotation is displayed in the image display area
1817 and a confirmation is needed before the original image files is
1818 overwritten. This confirmation can be turned off using
1819 `image-dired-rotate-original-ask-before-overwrite'."
1820 (interactive)
1821 (image-dired-rotate-thumbnail "90"))
1823 (defun image-dired-refresh-thumb ()
1824 "Force creation of new image for current thumbnail."
1825 (interactive)
1826 (let ((file (image-dired-original-file-name)))
1827 (clear-image-cache)
1828 (image-dired-create-thumb file (image-dired-thumb-name file))))
1830 (defun image-dired-rotate-original (degrees)
1831 "Rotate original image DEGREES degrees."
1832 (image-dired--check-executable-exists
1833 'image-dired-cmd-rotate-original-program)
1834 (if (not (image-dired-image-at-point-p))
1835 (message "No image at point")
1836 (let ((file (image-dired-original-file-name))
1837 command)
1838 (unless (eq 'jpeg (image-type file))
1839 (error "Only JPEG images can be rotated!"))
1840 (setq command (format-spec
1841 image-dired-cmd-rotate-original-options
1842 (list
1843 (cons ?p image-dired-cmd-rotate-original-program)
1844 (cons ?d degrees)
1845 (cons ?o (expand-file-name file))
1846 (cons ?t image-dired-temp-rotate-image-file))))
1847 (if (not (= 0 (call-process shell-file-name nil nil nil
1848 shell-command-switch command)))
1849 (error "Could not rotate image")
1850 (image-dired-display-image image-dired-temp-rotate-image-file)
1851 (if (or (and image-dired-rotate-original-ask-before-overwrite
1852 (y-or-n-p
1853 "Rotate to temp file OK. Overwrite original image? "))
1854 (not image-dired-rotate-original-ask-before-overwrite))
1855 (progn
1856 (copy-file image-dired-temp-rotate-image-file file t)
1857 (image-dired-refresh-thumb))
1858 (image-dired-display-image file))))))
1860 (defun image-dired-rotate-original-left ()
1861 "Rotate original image left (counter clockwise) 90 degrees."
1862 (interactive)
1863 (image-dired-rotate-original "270"))
1865 (defun image-dired-rotate-original-right ()
1866 "Rotate original image right (clockwise) 90 degrees."
1867 (interactive)
1868 (image-dired-rotate-original "90"))
1870 (defun image-dired-get-exif-file-name (file)
1871 "Use the image's EXIF information to return a unique file name.
1872 The file name should be unique as long as you do not take more than
1873 one picture per second. The original file name is suffixed at the end
1874 for traceability. The format of the returned file name is
1875 YYYY_MM_DD_HH_MM_DD_ORIG_FILE_NAME.jpg. Used from
1876 `image-dired-copy-with-exif-file-name'."
1877 (let (data no-exif-data-found)
1878 (if (not (eq 'jpeg (image-type (expand-file-name file))))
1879 (setq no-exif-data-found t
1880 data (format-time-string
1881 "%Y:%m:%d %H:%M:%S"
1882 (file-attribute-modification-time
1883 (file-attributes (expand-file-name file)))))
1884 (setq data (image-dired-get-exif-data (expand-file-name file)
1885 "DateTimeOriginal")))
1886 (while (string-match "[ :]" data)
1887 (setq data (replace-match "_" nil nil data)))
1888 (format "%s%s%s" data
1889 (if no-exif-data-found
1890 "_noexif_"
1891 "_")
1892 (file-name-nondirectory file))))
1894 (defun image-dired-thumbnail-set-image-description ()
1895 "Set the ImageDescription EXIF tag for the original image.
1896 If the image already has a value for this tag, it is used as the
1897 default value at the prompt."
1898 (interactive)
1899 (if (not (image-dired-image-at-point-p))
1900 (message "No thumbnail at point")
1901 (let* ((file (image-dired-original-file-name))
1902 (old-value (image-dired-get-exif-data file "ImageDescription")))
1903 (if (eq 0
1904 (image-dired-set-exif-data file "ImageDescription"
1905 (read-string "Value of ImageDescription: "
1906 old-value)))
1907 (message "Successfully wrote ImageDescription tag.")
1908 (error "Could not write ImageDescription tag")))))
1910 (defun image-dired-set-exif-data (file tag-name tag-value)
1911 "In FILE, set EXIF tag TAG-NAME to value TAG-VALUE."
1912 (image-dired--check-executable-exists
1913 'image-dired-cmd-write-exif-data-program)
1914 (let (command)
1915 (setq command (format-spec
1916 image-dired-cmd-write-exif-data-options
1917 (list
1918 (cons ?p image-dired-cmd-write-exif-data-program)
1919 (cons ?f (expand-file-name file))
1920 (cons ?t tag-name)
1921 (cons ?v tag-value))))
1922 (call-process shell-file-name nil nil nil shell-command-switch command)))
1924 (defun image-dired-get-exif-data (file tag-name)
1925 "From FILE, return EXIF tag TAG-NAME."
1926 (image-dired--check-executable-exists
1927 'image-dired-cmd-read-exif-data-program)
1928 (let ((buf (get-buffer-create "*image-dired-get-exif-data*"))
1929 command tag-value)
1930 (setq command (format-spec
1931 image-dired-cmd-read-exif-data-options
1932 (list
1933 (cons ?p image-dired-cmd-read-exif-data-program)
1934 (cons ?f file)
1935 (cons ?t tag-name))))
1936 (with-current-buffer buf
1937 (delete-region (point-min) (point-max))
1938 (if (not (eq (call-process shell-file-name nil t nil
1939 shell-command-switch command) 0))
1940 (error "Could not get EXIF tag")
1941 (goto-char (point-min))
1942 ;; Clean buffer from newlines and carriage returns before
1943 ;; getting final info
1944 (while (search-forward-regexp "[\n\r]" nil t)
1945 (replace-match "" nil t))
1946 (setq tag-value (buffer-substring (point-min) (point-max)))))
1947 tag-value))
1949 (defun image-dired-copy-with-exif-file-name ()
1950 "Copy file with unique name to main image directory.
1951 Copy current or all marked files in dired to a new file in your
1952 main image directory, using a file name generated by
1953 `image-dired-get-exif-file-name'. A typical usage for this if when
1954 copying images from a digital camera into the image directory.
1956 Typically, you would open up the folder with the incoming
1957 digital images, mark the files to be copied, and execute this
1958 function. The result is a couple of new files in
1959 `image-dired-main-image-directory' called
1960 2005_05_08_12_52_00_dscn0319.jpg,
1961 2005_05_08_14_27_45_dscn0320.jpg etc."
1962 (interactive)
1963 (let (new-name
1964 (files (dired-get-marked-files)))
1965 (mapcar
1966 (lambda (curr-file)
1967 (setq new-name
1968 (format "%s/%s"
1969 (file-name-as-directory
1970 (expand-file-name image-dired-main-image-directory))
1971 (image-dired-get-exif-file-name curr-file)))
1972 (message "Copying %s to %s" curr-file new-name)
1973 (copy-file curr-file new-name))
1974 files)))
1976 (defun image-dired-display-next-thumbnail-original ()
1977 "In thumbnail buffer, move to next thumbnail and display the image."
1978 (interactive)
1979 (image-dired-forward-image)
1980 (image-dired-display-thumbnail-original-image))
1982 (defun image-dired-display-previous-thumbnail-original ()
1983 "Move to previous thumbnail and display image."
1984 (interactive)
1985 (image-dired-backward-image)
1986 (image-dired-display-thumbnail-original-image))
1988 (defun image-dired-write-comments (file-comments)
1989 "Write file comments to database.
1990 Write file comments to one or more files.
1991 FILE-COMMENTS is an alist on the following form:
1992 ((FILE . COMMENT) ... )"
1993 (image-dired-sane-db-file)
1994 (let (end comment-beg-pos comment-end-pos file comment)
1995 (image-dired--with-db-file
1996 (setq buffer-file-name image-dired-db-file)
1997 (dolist (elt file-comments)
1998 (setq file (car elt)
1999 comment (cdr elt))
2000 (goto-char (point-min))
2001 (if (search-forward-regexp (format "^%s.*$" file) nil t)
2002 (progn
2003 (setq end (point))
2004 (beginning-of-line)
2005 ;; Delete old comment, if any
2006 (when (search-forward ";comment:" end t)
2007 (setq comment-beg-pos (match-beginning 0))
2008 ;; Any tags after the comment?
2009 (if (search-forward ";" end t)
2010 (setq comment-end-pos (- (point) 1))
2011 (setq comment-end-pos end))
2012 ;; Delete comment tag and comment
2013 (delete-region comment-beg-pos comment-end-pos))
2014 ;; Insert new comment
2015 (beginning-of-line)
2016 (unless (search-forward ";" end t)
2017 (end-of-line)
2018 (insert ";"))
2019 (insert (format "comment:%s;" comment)))
2020 ;; File does not exist in database - add it.
2021 (goto-char (point-max))
2022 (insert (format "\n%s;comment:%s" file comment))))
2023 (save-buffer))))
2025 (defun image-dired-update-property (prop value)
2026 "Update text property PROP with value VALUE at point."
2027 (let ((inhibit-read-only t))
2028 (put-text-property
2029 (point) (1+ (point))
2030 prop
2031 value)))
2033 ;;;###autoload
2034 (defun image-dired-dired-comment-files ()
2035 "Add comment to current or marked files in dired."
2036 (interactive)
2037 (let ((comment (image-dired-read-comment)))
2038 (image-dired-write-comments
2039 (mapcar
2040 (lambda (curr-file)
2041 (cons curr-file comment))
2042 (dired-get-marked-files)))))
2044 (defun image-dired-comment-thumbnail ()
2045 "Add comment to current thumbnail in thumbnail buffer."
2046 (interactive)
2047 (let* ((file (image-dired-original-file-name))
2048 (comment (image-dired-read-comment file)))
2049 (image-dired-write-comments (list (cons file comment)))
2050 (image-dired-update-property 'comment comment))
2051 (image-dired-display-thumb-properties))
2053 (defun image-dired-read-comment (&optional file)
2054 "Read comment for an image.
2055 Optionally use old comment from FILE as initial value."
2056 (let ((comment
2057 (read-string
2058 "Comment: "
2059 (if file (image-dired-get-comment file)))))
2060 comment))
2062 (defun image-dired-get-comment (file)
2063 "Get comment for file FILE."
2064 (image-dired-sane-db-file)
2065 (image-dired--with-db-file
2066 (let (end comment-beg-pos comment-end-pos comment)
2067 (when (search-forward-regexp (format "^%s" file) nil t)
2068 (end-of-line)
2069 (setq end (point))
2070 (beginning-of-line)
2071 (when (search-forward ";comment:" end t)
2072 (setq comment-beg-pos (point))
2073 (if (search-forward ";" end t)
2074 (setq comment-end-pos (- (point) 1))
2075 (setq comment-end-pos end))
2076 (setq comment (buffer-substring
2077 comment-beg-pos comment-end-pos))))
2078 comment)))
2080 ;;;###autoload
2081 (defun image-dired-mark-tagged-files ()
2082 "Use regexp to mark files with matching tag.
2083 A `tag' is a keyword, a piece of meta data, associated with an
2084 image file and stored in image-dired's database file. This command
2085 lets you input a regexp and this will be matched against all tags
2086 on all image files in the database file. The files that have a
2087 matching tag will be marked in the dired buffer."
2088 (interactive)
2089 (image-dired-sane-db-file)
2090 (let ((tag (read-string "Mark tagged files (regexp): "))
2091 (hits 0)
2092 files)
2093 (image-dired--with-db-file
2094 ;; Collect matches
2095 (while (search-forward-regexp
2096 (concat "\\(^[^;\n]+\\);.*" tag ".*$") nil t)
2097 (push (match-string 1) files)))
2098 ;; Mark files
2099 (dolist (curr-file files)
2100 ;; I tried using `dired-mark-files-regexp' but it was waaaay to
2101 ;; slow. Don't bother about hits found in other directories
2102 ;; than the current one.
2103 (when (string= (file-name-as-directory
2104 (expand-file-name default-directory))
2105 (file-name-as-directory
2106 (file-name-directory curr-file)))
2107 (setq curr-file (file-name-nondirectory curr-file))
2108 (goto-char (point-min))
2109 (when (search-forward-regexp (format "\\s %s$" curr-file) nil t)
2110 (setq hits (+ hits 1))
2111 (dired-mark 1))))
2112 (message "%d files with matching tag marked." hits)))
2114 (defun image-dired-mouse-display-image (event)
2115 "Use mouse EVENT, call `image-dired-display-image' to display image.
2116 Track this in associated dired buffer if `image-dired-track-movement' is
2117 non-nil."
2118 (interactive "e")
2119 (mouse-set-point event)
2120 (goto-char (posn-point (event-end event)))
2121 (let ((file (image-dired-original-file-name)))
2122 (when file
2123 (if image-dired-track-movement
2124 (image-dired-track-original-file))
2125 (image-dired-create-display-image-buffer)
2126 (display-buffer image-dired-display-image-buffer)
2127 (image-dired-display-image file))))
2129 (defun image-dired-mouse-select-thumbnail (event)
2130 "Use mouse EVENT to select thumbnail image.
2131 Track this in associated dired buffer if `image-dired-track-movement' is
2132 non-nil."
2133 (interactive "e")
2134 (mouse-set-point event)
2135 (goto-char (posn-point (event-end event)))
2136 (if image-dired-track-movement
2137 (image-dired-track-original-file))
2138 (image-dired-display-thumb-properties))
2140 (defun image-dired-mouse-toggle-mark (event)
2141 "Use mouse EVENT to toggle dired mark for thumbnail.
2142 Track this in associated dired buffer if `image-dired-track-movement' is
2143 non-nil."
2144 (interactive "e")
2145 (mouse-set-point event)
2146 (goto-char (posn-point (event-end event)))
2147 (if image-dired-track-movement
2148 (image-dired-track-original-file))
2149 (image-dired-toggle-mark-thumb-original-file))
2151 (defun image-dired-dired-display-properties ()
2152 "Display properties for dired file in the echo area."
2153 (interactive)
2154 (let* ((file (dired-get-filename))
2155 (file-name (file-name-nondirectory file))
2156 (dired-buf (buffer-name (current-buffer)))
2157 (props (mapconcat
2158 'princ
2159 (image-dired-list-tags file)
2160 ", "))
2161 (comment (image-dired-get-comment file)))
2162 (if file-name
2163 (message "%s"
2164 (image-dired-format-properties-string
2165 dired-buf
2166 file-name
2167 props
2168 comment)))))
2170 (defvar image-dired-tag-file-list nil
2171 "List to store tag-file structure.")
2173 (defvar image-dired-file-tag-list nil
2174 "List to store file-tag structure.")
2176 (defvar image-dired-file-comment-list nil
2177 "List to store file comments.")
2179 (defun image-dired-add-to-tag-file-list (tag file)
2180 "Add relation between TAG and FILE."
2181 (let (curr)
2182 (if image-dired-tag-file-list
2183 (if (setq curr (assoc tag image-dired-tag-file-list))
2184 (if (not (member file curr))
2185 (setcdr curr (cons file (cdr curr))))
2186 (setcdr image-dired-tag-file-list
2187 (cons (list tag file) (cdr image-dired-tag-file-list))))
2188 (setq image-dired-tag-file-list (list (list tag file))))))
2190 (defun image-dired-add-to-tag-file-lists (tag file)
2191 "Helper function used from `image-dired-create-gallery-lists'.
2193 Add TAG to FILE in one list and FILE to TAG in the other.
2195 Lisp structures look like the following:
2197 image-dired-file-tag-list:
2199 ((\"filename1\" \"tag1\" \"tag2\" \"tag3\" ...)
2200 (\"filename2\" \"tag1\" \"tag2\" \"tag3\" ...)
2201 ...)
2203 image-dired-tag-file-list:
2205 ((\"tag1\" \"filename1\" \"filename2\" \"filename3\" ...)
2206 (\"tag2\" \"filename1\" \"filename2\" \"filename3\" ...)
2207 ...)"
2208 ;; Add tag to file list
2209 (let (curr)
2210 (if image-dired-file-tag-list
2211 (if (setq curr (assoc file image-dired-file-tag-list))
2212 (setcdr curr (cons tag (cdr curr)))
2213 (setcdr image-dired-file-tag-list
2214 (cons (list file tag) (cdr image-dired-file-tag-list))))
2215 (setq image-dired-file-tag-list (list (list file tag))))
2216 ;; Add file to tag list
2217 (if image-dired-tag-file-list
2218 (if (setq curr (assoc tag image-dired-tag-file-list))
2219 (if (not (member file curr))
2220 (setcdr curr (cons file (cdr curr))))
2221 (setcdr image-dired-tag-file-list
2222 (cons (list tag file) (cdr image-dired-tag-file-list))))
2223 (setq image-dired-tag-file-list (list (list tag file))))))
2225 (defun image-dired-add-to-file-comment-list (file comment)
2226 "Helper function used from `image-dired-create-gallery-lists'.
2228 For FILE, add COMMENT to list.
2230 Lisp structure looks like the following:
2232 image-dired-file-comment-list:
2234 ((\"filename1\" . \"comment1\")
2235 (\"filename2\" . \"comment2\")
2236 ...)"
2237 (if image-dired-file-comment-list
2238 (if (not (assoc file image-dired-file-comment-list))
2239 (setcdr image-dired-file-comment-list
2240 (cons (cons file comment)
2241 (cdr image-dired-file-comment-list))))
2242 (setq image-dired-file-comment-list (list (cons file comment)))))
2244 (defun image-dired-create-gallery-lists ()
2245 "Create temporary lists used by `image-dired-gallery-generate'."
2246 (image-dired-sane-db-file)
2247 (image-dired--with-db-file
2248 (let (end beg file row-tags)
2249 (setq image-dired-tag-file-list nil)
2250 (setq image-dired-file-tag-list nil)
2251 (setq image-dired-file-comment-list nil)
2252 (goto-char (point-min))
2253 (while (search-forward-regexp "^." nil t)
2254 (end-of-line)
2255 (setq end (point))
2256 (beginning-of-line)
2257 (setq beg (point))
2258 (unless (search-forward ";" end nil)
2259 (error "Something is really wrong, check format of database"))
2260 (setq row-tags (split-string
2261 (buffer-substring beg end) ";"))
2262 (setq file (car row-tags))
2263 (dolist (x (cdr row-tags))
2264 (if (not (string-match "^comment:\\(.*\\)" x))
2265 (image-dired-add-to-tag-file-lists x file)
2266 (image-dired-add-to-file-comment-list file (match-string 1 x)))))))
2267 ;; Sort tag-file list
2268 (setq image-dired-tag-file-list
2269 (sort image-dired-tag-file-list
2270 (lambda (x y)
2271 (string< (car x) (car y))))))
2273 (defun image-dired-hidden-p (file)
2274 "Return t if image FILE has a \"hidden\" tag."
2275 (let (hidden)
2276 (mapc
2277 (lambda (tag)
2278 (if (member tag image-dired-gallery-hidden-tags)
2279 (setq hidden t)))
2280 (cdr (assoc file image-dired-file-tag-list)))
2281 hidden))
2283 (defun image-dired-gallery-generate ()
2284 "Generate gallery pages.
2285 First we create a couple of Lisp structures from the database to make
2286 it easier to generate, then HTML-files are created in
2287 `image-dired-gallery-dir'."
2288 (interactive)
2289 (if (eq 'per-directory image-dired-thumbnail-storage)
2290 (error "Currently, gallery generation is not supported \
2291 when using per-directory thumbnail file storage"))
2292 (image-dired-create-gallery-lists)
2293 (let ((tags image-dired-tag-file-list)
2294 (index-file (format "%s/index.html" image-dired-gallery-dir))
2295 count tag tag-file
2296 comment file-tags tag-link tag-link-list)
2297 ;; Make sure gallery root exist
2298 (if (file-exists-p image-dired-gallery-dir)
2299 (if (not (file-directory-p image-dired-gallery-dir))
2300 (error "Variable image-dired-gallery-dir is not a directory"))
2301 (make-directory image-dired-gallery-dir))
2302 ;; Open index file
2303 (with-temp-file index-file
2304 (if (file-exists-p index-file)
2305 (insert-file-contents index-file))
2306 (insert "<html>\n")
2307 (insert " <body>\n")
2308 (insert " <h2>Image-Dired Gallery</h2>\n")
2309 (insert (format "<p>\n Gallery generated %s\n <p>\n"
2310 (current-time-string)))
2311 (insert " <h3>Tag index</h3>\n")
2312 (setq count 1)
2313 ;; Pre-generate list of all tag links
2314 (dolist (curr tags)
2315 (setq tag (car curr))
2316 (when (not (member tag image-dired-gallery-hidden-tags))
2317 (setq tag-link (format "<a href=\"%d.html\">%s</a>" count tag))
2318 (if tag-link-list
2319 (setq tag-link-list
2320 (append tag-link-list (list (cons tag tag-link))))
2321 (setq tag-link-list (list (cons tag tag-link))))
2322 (setq count (1+ count))))
2323 (setq count 1)
2324 ;; Main loop where we generated thumbnail pages per tag
2325 (dolist (curr tags)
2326 (setq tag (car curr))
2327 ;; Don't display hidden tags
2328 (when (not (member tag image-dired-gallery-hidden-tags))
2329 ;; Insert link to tag page in index
2330 (insert (format " %s<br>\n" (cdr (assoc tag tag-link-list))))
2331 ;; Open per-tag file
2332 (setq tag-file (format "%s/%s.html" image-dired-gallery-dir count))
2333 (with-temp-file tag-file
2334 (if (file-exists-p tag-file)
2335 (insert-file-contents tag-file))
2336 (erase-buffer)
2337 (insert "<html>\n")
2338 (insert " <body>\n")
2339 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2340 (insert (format " <h2>Images with tag &quot;%s&quot;</h2>" tag))
2341 ;; Main loop for files per tag page
2342 (dolist (file (cdr curr))
2343 (unless (image-dired-hidden-p file)
2344 ;; Insert thumbnail with link to full image
2345 (insert
2346 (format "<a href=\"%s/%s\"><img src=\"%s/%s\"%s></a>\n"
2347 image-dired-gallery-image-root-url
2348 (file-name-nondirectory file)
2349 image-dired-gallery-thumb-image-root-url
2350 (file-name-nondirectory (image-dired-thumb-name file)) file))
2351 ;; Insert comment, if any
2352 (if (setq comment (cdr (assoc file image-dired-file-comment-list)))
2353 (insert (format "<br>\n%s<br>\n" comment))
2354 (insert "<br>\n"))
2355 ;; Insert links to other tags, if any
2356 (when (> (length
2357 (setq file-tags (assoc file image-dired-file-tag-list))) 2)
2358 (insert "[ ")
2359 (dolist (extra-tag file-tags)
2360 ;; Only insert if not file name or the main tag
2361 (if (and (not (equal extra-tag tag))
2362 (not (equal extra-tag file)))
2363 (insert
2364 (format "%s " (cdr (assoc extra-tag tag-link-list))))))
2365 (insert "]<br>\n"))))
2366 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2367 (insert " </body>\n")
2368 (insert "</html>\n"))
2369 (setq count (1+ count))))
2370 (insert " </body>\n")
2371 (insert "</html>"))))
2373 (defun image-dired-kill-buffer-and-window ()
2374 "Kill the current buffer and, if possible, also the window."
2375 (interactive)
2376 (let ((buffer (current-buffer)))
2377 (condition-case nil
2378 (delete-window (selected-window))
2379 (error nil))
2380 (kill-buffer buffer)))
2382 (defvar image-dired-widget-list nil
2383 "List to keep track of meta data in edit buffer.")
2385 (declare-function widget-forward "wid-edit" (arg))
2387 ;;;###autoload
2388 (defun image-dired-dired-edit-comment-and-tags ()
2389 "Edit comment and tags of current or marked image files.
2390 Edit comment and tags for all marked image files in an
2391 easy-to-use form."
2392 (interactive)
2393 (setq image-dired-widget-list nil)
2394 ;; Setup buffer.
2395 (let ((files (dired-get-marked-files)))
2396 (pop-to-buffer-same-window "*Image-Dired Edit Meta Data*")
2397 (kill-all-local-variables)
2398 (make-local-variable 'widget-example-repeat)
2399 (let ((inhibit-read-only t))
2400 (erase-buffer))
2401 (remove-overlays)
2402 ;; Some help for the user.
2403 (widget-insert
2404 "\nEdit comments and tags for each image. Separate multiple tags
2405 with a comma. Move forward between fields using TAB or RET.
2406 Move to the previous field using backtab (S-TAB). Save by
2407 activating the Save button at the bottom of the form or cancel
2408 the operation by activating the Cancel button.\n\n")
2409 ;; Here comes all images and a comment and tag field for each
2410 ;; image.
2411 (let (thumb-file img comment-widget tag-widget)
2413 (dolist (file files)
2415 (setq thumb-file (image-dired-thumb-name file)
2416 img (create-image thumb-file))
2418 (insert-image img)
2419 (widget-insert "\n\nComment: ")
2420 (setq comment-widget
2421 (widget-create 'editable-field
2422 :size 60
2423 :format "%v "
2424 :value (or (image-dired-get-comment file) "")))
2425 (widget-insert "\nTags: ")
2426 (setq tag-widget
2427 (widget-create 'editable-field
2428 :size 60
2429 :format "%v "
2430 :value (or (mapconcat
2431 (lambda (tag)
2432 tag)
2433 (image-dired-list-tags file)
2434 ",") "")))
2435 ;; Save information in all widgets so that we can use it when
2436 ;; the user saves the form.
2437 (setq image-dired-widget-list
2438 (append image-dired-widget-list
2439 (list (list file comment-widget tag-widget))))
2440 (widget-insert "\n\n")))
2442 ;; Footer with Save and Cancel button.
2443 (widget-insert "\n")
2444 (widget-create 'push-button
2445 :notify
2446 (lambda (&rest _ignore)
2447 (image-dired-save-information-from-widgets)
2448 (bury-buffer)
2449 (message "Done."))
2450 "Save")
2451 (widget-insert " ")
2452 (widget-create 'push-button
2453 :notify
2454 (lambda (&rest _ignore)
2455 (bury-buffer)
2456 (message "Operation canceled."))
2457 "Cancel")
2458 (widget-insert "\n")
2459 (use-local-map widget-keymap)
2460 (widget-setup)
2461 ;; Jump to the first widget.
2462 (widget-forward 1)))
2464 (defun image-dired-save-information-from-widgets ()
2465 "Save information found in `image-dired-widget-list'.
2466 Use the information in `image-dired-widget-list' to save comments and
2467 tags to their respective image file. Internal function used by
2468 `image-dired-dired-edit-comment-and-tags'."
2469 (let (file comment tag-string tag-list lst)
2470 (image-dired-write-comments
2471 (mapcar
2472 (lambda (widget)
2473 (setq file (car widget)
2474 comment (widget-value (cadr widget)))
2475 (cons file comment))
2476 image-dired-widget-list))
2477 (image-dired-write-tags
2478 (dolist (widget image-dired-widget-list lst)
2479 (setq file (car widget)
2480 tag-string (widget-value (car (cddr widget)))
2481 tag-list (split-string tag-string ","))
2482 (dolist (tag tag-list)
2483 (push (cons file tag) lst))))))
2485 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2486 ;;;;;;;;; TEST-SECTION ;;;;;;;;;;;
2487 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2489 ;; (defvar image-dired-dir-max-size 12300000)
2491 ;; (defun image-dired-test-clean-old-files ()
2492 ;; "Clean `image-dired-dir' from old thumbnail files.
2493 ;; \"Oldness\" measured using last access time. If the total size of all
2494 ;; thumbnail files in `image-dired-dir' is larger than 'image-dired-dir-max-size',
2495 ;; old files are deleted until the max size is reached."
2496 ;; (let* ((files
2497 ;; (sort
2498 ;; (mapcar
2499 ;; (lambda (f)
2500 ;; (let ((fattribs (file-attributes f)))
2501 ;; ;; Get last access time and file size
2502 ;; `(,(nth 4 fattribs) ,(nth 7 fattribs) ,f)))
2503 ;; (directory-files (image-dired-dir) t ".+\\.thumb\\..+$"))
2504 ;; ;; Sort function. Compare time between two files.
2505 ;; (lambda (l1 l2)
2506 ;; (time-less-p (car l1) (car l2)))))
2507 ;; (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) files))))
2508 ;; (while (> dirsize image-dired-dir-max-size)
2509 ;; (y-or-n-p
2510 ;; (format "Size of thumbnail directory: %d, delete old file %s? "
2511 ;; dirsize (cadr (cdar files))))
2512 ;; (delete-file (cadr (cdar files)))
2513 ;; (setq dirsize (- dirsize (car (cdar files))))
2514 ;; (setq files (cdr files)))))
2516 ;;;;;;;;;;;;;;;;;;;;;;,
2518 ;; (defun dired-speedbar-buttons (dired-buffer)
2519 ;; (when (and (boundp 'image-dired-use-speedbar)
2520 ;; image-dired-use-speedbar)
2521 ;; (let ((filename (with-current-buffer dired-buffer
2522 ;; (dired-get-filename))))
2523 ;; (when (and (not (string-equal filename (buffer-string)))
2524 ;; (string-match (image-file-name-regexp) filename))
2525 ;; (erase-buffer)
2526 ;; (insert (propertize
2527 ;; filename
2528 ;; 'display
2529 ;; (image-dired-get-thumbnail-image filename)))))))
2531 ;; (setq image-dired-use-speedbar t)
2533 (provide 'image-dired)
2535 ;;; image-dired.el ends here