Remove compat code in Tramp
[emacs.git] / lisp / image-dired.el
blobee5678f95895860ba6fb5010b739fdaeecb6ded4
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 (when (not (file-exists-p thumb-name))
845 (if (and (not (file-exists-p thumb-name))
846 (not (= 0 (image-dired-create-thumb curr-file thumb-name))))
847 (message "Thumb could not be created for file %s" curr-file)
848 (image-dired-insert-thumbnail thumb-name curr-file dired-buf)))))
849 (if do-not-pop
850 (display-buffer buf)
851 (pop-to-buffer buf))
852 (cond ((eq 'dynamic image-dired-line-up-method)
853 (image-dired-line-up-dynamic))
854 ((eq 'fixed image-dired-line-up-method)
855 (image-dired-line-up))
856 ((eq 'interactive image-dired-line-up-method)
857 (image-dired-line-up-interactive))
858 ((eq 'none image-dired-line-up-method)
859 nil)
861 (image-dired-line-up-dynamic))))))
863 ;;;###autoload
864 (defun image-dired-show-all-from-dir (dir)
865 "Make a preview buffer for all images in DIR and display it.
866 If the number of files in DIR matching `image-file-name-regexp'
867 exceeds `image-dired-show-all-from-dir-max-files', a warning will be
868 displayed."
869 (interactive "DImage Dired: ")
870 (dired dir)
871 (dired-mark-files-regexp (image-file-name-regexp))
872 (let ((files (dired-get-marked-files)))
873 (if (or (<= (length files) image-dired-show-all-from-dir-max-files)
874 (and (> (length files) image-dired-show-all-from-dir-max-files)
875 (y-or-n-p
876 (format
877 "Directory contains more than %d image files. Proceed? "
878 image-dired-show-all-from-dir-max-files))))
879 (progn
880 (image-dired-display-thumbs)
881 (pop-to-buffer image-dired-thumbnail-buffer))
882 (message "Canceled."))))
884 ;;;###autoload
885 (defalias 'image-dired 'image-dired-show-all-from-dir)
887 ;;;###autoload
888 (define-obsolete-function-alias 'tumme 'image-dired "24.4")
890 (defun image-dired-sane-db-file ()
891 "Check if `image-dired-db-file' exists.
892 If not, try to create it (including any parent directories).
893 Signal error if there are problems creating it."
894 (or (file-exists-p image-dired-db-file)
895 (let (dir buf)
896 (unless (file-directory-p (setq dir (file-name-directory
897 image-dired-db-file)))
898 (make-directory dir t))
899 (with-current-buffer (setq buf (create-file-buffer
900 image-dired-db-file))
901 (write-file image-dired-db-file))
902 (kill-buffer buf)
903 (file-exists-p image-dired-db-file))
904 (error "Could not create %s" image-dired-db-file)))
906 (defun image-dired-write-tags (file-tags)
907 "Write file tags to database.
908 Write each file and tag in FILE-TAGS to the database.
909 FILE-TAGS is an alist in the following form:
910 ((FILE . TAG) ... )"
911 (image-dired-sane-db-file)
912 (let (end file tag)
913 (image-dired--with-db-file
914 (setq buffer-file-name image-dired-db-file)
915 (dolist (elt file-tags)
916 (setq file (car elt)
917 tag (cdr elt))
918 (goto-char (point-min))
919 (if (search-forward-regexp (format "^%s.*$" file) nil t)
920 (progn
921 (setq end (point))
922 (beginning-of-line)
923 (when (not (search-forward (format ";%s" tag) end t))
924 (end-of-line)
925 (insert (format ";%s" tag))))
926 (goto-char (point-max))
927 (insert (format "\n%s;%s" file tag))))
928 (save-buffer))))
930 (defun image-dired-remove-tag (files tag)
931 "For all FILES, remove TAG from the image database."
932 (image-dired-sane-db-file)
933 (image-dired--with-db-file
934 (setq buffer-file-name image-dired-db-file)
935 (let (end)
936 (unless (listp files)
937 (if (stringp files)
938 (setq files (list files))
939 (error "Files must be a string or a list of strings!")))
940 (dolist (file files)
941 (goto-char (point-min))
942 (when (search-forward-regexp (format "^%s" file) nil t)
943 (end-of-line)
944 (setq end (point))
945 (beginning-of-line)
946 (when (search-forward-regexp (format "\\(;%s\\)" tag) end t)
947 (delete-region (match-beginning 1) (match-end 1))
948 ;; Check if file should still be in the database. If
949 ;; it has no tags or comments, it will be removed.
950 (end-of-line)
951 (setq end (point))
952 (beginning-of-line)
953 (when (not (search-forward ";" end t))
954 (kill-line 1)
955 ;; If on empty line at end of buffer
956 (and (eobp)
957 (looking-at "^$")
958 (delete-char -1)))))))
959 (save-buffer)))
961 (defun image-dired-list-tags (file)
962 "Read all tags for image FILE from the image database."
963 (image-dired-sane-db-file)
964 (image-dired--with-db-file
965 (let (end (tags ""))
966 (when (search-forward-regexp (format "^%s" file) nil t)
967 (end-of-line)
968 (setq end (point))
969 (beginning-of-line)
970 (if (search-forward ";" end t)
971 (if (search-forward "comment:" end t)
972 (if (search-forward ";" end t)
973 (setq tags (buffer-substring (point) end)))
974 (setq tags (buffer-substring (point) end)))))
975 (split-string tags ";"))))
977 ;;;###autoload
978 (defun image-dired-tag-files (arg)
979 "Tag marked file(s) in dired. With prefix ARG, tag file at point."
980 (interactive "P")
981 (let ((tag (read-string "Tags to add (separate tags with a semicolon): "))
982 files)
983 (if arg
984 (setq files (list (dired-get-filename)))
985 (setq files (dired-get-marked-files)))
986 (image-dired-write-tags
987 (mapcar
988 (lambda (x)
989 (cons x tag))
990 files))))
992 (defun image-dired-tag-thumbnail ()
993 "Tag current thumbnail."
994 (interactive)
995 (let ((tag (read-string "Tags to add (separate tags with a semicolon): ")))
996 (image-dired-write-tags (list (cons (image-dired-original-file-name) tag))))
997 (image-dired-update-property
998 'tags (image-dired-list-tags (image-dired-original-file-name))))
1000 ;;;###autoload
1001 (defun image-dired-delete-tag (arg)
1002 "Remove tag for selected file(s).
1003 With prefix argument ARG, remove tag from file at point."
1004 (interactive "P")
1005 (let ((tag (read-string "Tag to remove: "))
1006 files)
1007 (if arg
1008 (setq files (list (dired-get-filename)))
1009 (setq files (dired-get-marked-files)))
1010 (image-dired-remove-tag files tag)))
1012 (defun image-dired-tag-thumbnail-remove ()
1013 "Remove tag from thumbnail."
1014 (interactive)
1015 (let ((tag (read-string "Tag to remove: ")))
1016 (image-dired-remove-tag (image-dired-original-file-name) tag))
1017 (image-dired-update-property
1018 'tags (image-dired-list-tags (image-dired-original-file-name))))
1020 (defun image-dired-original-file-name ()
1021 "Get original file name for thumbnail or display image at point."
1022 (get-text-property (point) 'original-file-name))
1024 (defun image-dired-associated-dired-buffer ()
1025 "Get associated dired buffer at point."
1026 (get-text-property (point) 'associated-dired-buffer))
1028 (defun image-dired-get-buffer-window (buf)
1029 "Return window where buffer BUF is."
1030 (get-window-with-predicate
1031 (lambda (window)
1032 (equal (window-buffer window) buf))
1033 nil t))
1035 (defun image-dired-track-original-file ()
1036 "Track the original file in the associated dired buffer.
1037 See documentation for `image-dired-toggle-movement-tracking'.
1038 Interactive use only useful if `image-dired-track-movement' is nil."
1039 (interactive)
1040 (let* ((dired-buf (image-dired-associated-dired-buffer))
1041 (file-name (image-dired-original-file-name))
1042 (window (image-dired-get-buffer-window dired-buf)))
1043 (and (buffer-live-p dired-buf) file-name
1044 (with-current-buffer dired-buf
1045 (if (not (dired-goto-file file-name))
1046 (message "Could not track file")
1047 (if window (set-window-point window (point))))))))
1049 (defun image-dired-toggle-movement-tracking ()
1050 "Turn on and off `image-dired-track-movement'.
1051 Tracking of the movements between thumbnail and dired buffer so that
1052 they are \"mirrored\" in the dired buffer. When this is on, moving
1053 around in the thumbnail or dired buffer will find the matching
1054 position in the other buffer."
1055 (interactive)
1056 (setq image-dired-track-movement (not image-dired-track-movement))
1057 (message "Tracking %s" (if image-dired-track-movement "on" "off")))
1059 (defun image-dired-track-thumbnail ()
1060 "Track current dired file's thumb in `image-dired-thumbnail-buffer'.
1061 This is almost the same as what `image-dired-track-original-file' does,
1062 but the other way around."
1063 (let ((file (dired-get-filename))
1064 prop-val found window)
1065 (when (get-buffer image-dired-thumbnail-buffer)
1066 (with-current-buffer image-dired-thumbnail-buffer
1067 (goto-char (point-min))
1068 (while (and (not (eobp))
1069 (not found))
1070 (if (and (setq prop-val
1071 (get-text-property (point) 'original-file-name))
1072 (string= prop-val file))
1073 (setq found t))
1074 (if (not found)
1075 (forward-char 1)))
1076 (when found
1077 (if (setq window (image-dired-thumbnail-window))
1078 (set-window-point window (point)))
1079 (image-dired-display-thumb-properties))))))
1081 (defun image-dired-dired-next-line (&optional arg)
1082 "Call `dired-next-line', then track thumbnail.
1083 This can safely replace `dired-next-line'.
1084 With prefix argument, move ARG lines."
1085 (interactive "P")
1086 (dired-next-line (or arg 1))
1087 (if image-dired-track-movement
1088 (image-dired-track-thumbnail)))
1090 (defun image-dired-dired-previous-line (&optional arg)
1091 "Call `dired-previous-line', then track thumbnail.
1092 This can safely replace `dired-previous-line'.
1093 With prefix argument, move ARG lines."
1094 (interactive "P")
1095 (dired-previous-line (or arg 1))
1096 (if image-dired-track-movement
1097 (image-dired-track-thumbnail)))
1099 (defun image-dired-forward-image (&optional arg)
1100 "Move to next image and display properties.
1101 Optional prefix ARG says how many images to move; default is one
1102 image."
1103 (interactive "p")
1104 (let (pos (steps (or arg 1)))
1105 (dotimes (_ steps)
1106 (if (and (not (eobp))
1107 (save-excursion
1108 (forward-char)
1109 (while (and (not (eobp))
1110 (not (image-dired-image-at-point-p)))
1111 (forward-char))
1112 (setq pos (point))
1113 (image-dired-image-at-point-p)))
1114 (goto-char pos)
1115 (error "At last image"))))
1116 (when image-dired-track-movement
1117 (image-dired-track-original-file))
1118 (image-dired-display-thumb-properties))
1120 (defun image-dired-backward-image (&optional arg)
1121 "Move to previous image and display properties.
1122 Optional prefix ARG says how many images to move; default is one
1123 image."
1124 (interactive "p")
1125 (let (pos (steps (or arg 1)))
1126 (dotimes (_ steps)
1127 (if (and (not (bobp))
1128 (save-excursion
1129 (backward-char)
1130 (while (and (not (bobp))
1131 (not (image-dired-image-at-point-p)))
1132 (backward-char))
1133 (setq pos (point))
1134 (image-dired-image-at-point-p)))
1135 (goto-char pos)
1136 (error "At first image"))))
1137 (when image-dired-track-movement
1138 (image-dired-track-original-file))
1139 (image-dired-display-thumb-properties))
1141 (defun image-dired-next-line ()
1142 "Move to next line and display properties."
1143 (interactive)
1144 (let ((goal-column (current-column)))
1145 (forward-line 1)
1146 (move-to-column goal-column))
1147 ;; If we end up in an empty spot, back up to the next thumbnail.
1148 (if (not (image-dired-image-at-point-p))
1149 (image-dired-backward-image))
1150 (if image-dired-track-movement
1151 (image-dired-track-original-file))
1152 (image-dired-display-thumb-properties))
1155 (defun image-dired-previous-line ()
1156 "Move to previous line and display properties."
1157 (interactive)
1158 (let ((goal-column (current-column)))
1159 (forward-line -1)
1160 (move-to-column goal-column))
1161 ;; If we end up in an empty spot, back up to the next
1162 ;; thumbnail. This should only happen if the user deleted a
1163 ;; thumbnail and did not refresh, so it is not very common. But we
1164 ;; can handle it in a good manner, so why not?
1165 (if (not (image-dired-image-at-point-p))
1166 (image-dired-backward-image))
1167 (if image-dired-track-movement
1168 (image-dired-track-original-file))
1169 (image-dired-display-thumb-properties))
1171 (defun image-dired-format-properties-string (buf file props comment)
1172 "Format display properties.
1173 BUF is the associated dired buffer, FILE is the original image file
1174 name, PROPS is a list of tags and COMMENT is the image file's
1175 comment."
1176 (format-spec
1177 image-dired-display-properties-format
1178 (list
1179 (cons ?b (or buf ""))
1180 (cons ?f file)
1181 (cons ?t (or (princ props) ""))
1182 (cons ?c (or comment "")))))
1184 (defun image-dired-display-thumb-properties ()
1185 "Display thumbnail properties in the echo area."
1186 (if (not (eobp))
1187 (let ((file-name (file-name-nondirectory (image-dired-original-file-name)))
1188 (dired-buf (buffer-name (image-dired-associated-dired-buffer)))
1189 (props (mapconcat
1190 'princ
1191 (get-text-property (point) 'tags)
1192 ", "))
1193 (comment (get-text-property (point) 'comment)))
1194 (if file-name
1195 (message "%s"
1196 (image-dired-format-properties-string
1197 dired-buf
1198 file-name
1199 props
1200 comment))))))
1202 (defun image-dired-dired-file-marked-p ()
1203 "Check whether file on current line is marked or not."
1204 (save-excursion
1205 (beginning-of-line)
1206 (not (looking-at "^ .*$"))))
1208 (defun image-dired-modify-mark-on-thumb-original-file (command)
1209 "Modify mark in dired buffer.
1210 COMMAND is one of `mark' for marking file in dired, `unmark' for
1211 unmarking file in dired or `flag' for flagging file for delete in
1212 dired."
1213 (let ((file-name (image-dired-original-file-name))
1214 (dired-buf (image-dired-associated-dired-buffer)))
1215 (if (not (and dired-buf file-name))
1216 (message "No image, or image with correct properties, at point.")
1217 (with-current-buffer dired-buf
1218 (message "%s" file-name)
1219 (if (dired-goto-file file-name)
1220 (cond ((eq command 'mark) (dired-mark 1))
1221 ((eq command 'unmark) (dired-unmark 1))
1222 ((eq command 'toggle)
1223 (if (image-dired-dired-file-marked-p)
1224 (dired-unmark 1)
1225 (dired-mark 1)))
1226 ((eq command 'flag) (dired-flag-file-deletion 1))))))))
1228 (defun image-dired-mark-thumb-original-file ()
1229 "Mark original image file in associated dired buffer."
1230 (interactive)
1231 (image-dired-modify-mark-on-thumb-original-file 'mark)
1232 (image-dired-forward-image))
1234 (defun image-dired-unmark-thumb-original-file ()
1235 "Unmark original image file in associated dired buffer."
1236 (interactive)
1237 (image-dired-modify-mark-on-thumb-original-file 'unmark)
1238 (image-dired-forward-image))
1240 (defun image-dired-flag-thumb-original-file ()
1241 "Flag original image file for deletion in associated dired buffer."
1242 (interactive)
1243 (image-dired-modify-mark-on-thumb-original-file 'flag)
1244 (image-dired-forward-image))
1246 (defun image-dired-toggle-mark-thumb-original-file ()
1247 "Toggle mark on original image file in associated dired buffer."
1248 (interactive)
1249 (image-dired-modify-mark-on-thumb-original-file 'toggle))
1251 (defun image-dired-jump-original-dired-buffer ()
1252 "Jump to the dired buffer associated with the current image file.
1253 You probably want to use this together with
1254 `image-dired-track-original-file'."
1255 (interactive)
1256 (let ((buf (image-dired-associated-dired-buffer))
1257 window frame)
1258 (setq window (image-dired-get-buffer-window buf))
1259 (if window
1260 (progn
1261 (if (not (equal (selected-frame) (setq frame (window-frame window))))
1262 (select-frame-set-input-focus frame))
1263 (select-window window))
1264 (message "Associated dired buffer not visible"))))
1266 ;;;###autoload
1267 (defun image-dired-jump-thumbnail-buffer ()
1268 "Jump to thumbnail buffer."
1269 (interactive)
1270 (let ((window (image-dired-thumbnail-window))
1271 frame)
1272 (if window
1273 (progn
1274 (if (not (equal (selected-frame) (setq frame (window-frame window))))
1275 (select-frame-set-input-focus frame))
1276 (select-window window))
1277 (message "Thumbnail buffer not visible"))))
1279 (defvar image-dired-thumbnail-mode-line-up-map
1280 (let ((map (make-sparse-keymap)))
1281 ;; map it to "g" so that the user can press it more quickly
1282 (define-key map "g" 'image-dired-line-up-dynamic)
1283 ;; "f" for "fixed" number of thumbs per row
1284 (define-key map "f" 'image-dired-line-up)
1285 ;; "i" for "interactive"
1286 (define-key map "i" 'image-dired-line-up-interactive)
1287 map)
1288 "Keymap for line-up commands in `image-dired-thumbnail-mode'.")
1290 (defvar image-dired-thumbnail-mode-tag-map
1291 (let ((map (make-sparse-keymap)))
1292 ;; map it to "t" so that the user can press it more quickly
1293 (define-key map "t" 'image-dired-tag-thumbnail)
1294 ;; "r" for "remove"
1295 (define-key map "r" 'image-dired-tag-thumbnail-remove)
1296 map)
1297 "Keymap for tag commands in `image-dired-thumbnail-mode'.")
1299 (defvar image-dired-thumbnail-mode-map
1300 (let ((map (make-sparse-keymap)))
1301 (define-key map [right] 'image-dired-forward-image)
1302 (define-key map [left] 'image-dired-backward-image)
1303 (define-key map [up] 'image-dired-previous-line)
1304 (define-key map [down] 'image-dired-next-line)
1305 (define-key map "\C-f" 'image-dired-forward-image)
1306 (define-key map "\C-b" 'image-dired-backward-image)
1307 (define-key map "\C-p" 'image-dired-previous-line)
1308 (define-key map "\C-n" 'image-dired-next-line)
1310 (define-key map "d" 'image-dired-flag-thumb-original-file)
1311 (define-key map [delete] 'image-dired-flag-thumb-original-file)
1312 (define-key map "m" 'image-dired-mark-thumb-original-file)
1313 (define-key map "u" 'image-dired-unmark-thumb-original-file)
1314 (define-key map "." 'image-dired-track-original-file)
1315 (define-key map [tab] 'image-dired-jump-original-dired-buffer)
1317 ;; add line-up map
1318 (define-key map "g" image-dired-thumbnail-mode-line-up-map)
1319 ;; add tag map
1320 (define-key map "t" image-dired-thumbnail-mode-tag-map)
1322 (define-key map "\C-m" 'image-dired-display-thumbnail-original-image)
1323 (define-key map [C-return] 'image-dired-thumbnail-display-external)
1325 (define-key map "l" 'image-dired-rotate-thumbnail-left)
1326 (define-key map "r" 'image-dired-rotate-thumbnail-right)
1327 (define-key map "L" 'image-dired-rotate-original-left)
1328 (define-key map "R" 'image-dired-rotate-original-right)
1330 (define-key map "D" 'image-dired-thumbnail-set-image-description)
1331 (define-key map "\C-d" 'image-dired-delete-char)
1332 (define-key map " " 'image-dired-display-next-thumbnail-original)
1333 (define-key map (kbd "DEL") 'image-dired-display-previous-thumbnail-original)
1334 (define-key map "c" 'image-dired-comment-thumbnail)
1335 (define-key map "q" 'image-dired-kill-buffer-and-window)
1337 ;; Mouse
1338 (define-key map [mouse-2] 'image-dired-mouse-display-image)
1339 (define-key map [mouse-1] 'image-dired-mouse-select-thumbnail)
1340 ;; Seems I must first set C-down-mouse-1 to undefined, or else it
1341 ;; will trigger the buffer menu. If I try to instead bind
1342 ;; C-down-mouse-1 to `image-dired-mouse-toggle-mark', I get a message
1343 ;; about C-mouse-1 not being defined afterwards. Annoying, but I
1344 ;; probably do not completely understand mouse events.
1345 (define-key map [C-down-mouse-1] 'undefined)
1346 (define-key map [C-mouse-1] 'image-dired-mouse-toggle-mark)
1348 ;; Menu
1349 (easy-menu-define nil map
1350 "Menu for `image-dired-thumbnail-mode'."
1351 '("Image-Dired"
1352 ["Quit" image-dired-kill-buffer-and-window]
1353 ["Delete thumbnail from buffer" image-dired-delete-char]
1354 ["Remove tag from thumbnail" image-dired-tag-thumbnail-remove]
1355 ["Tag thumbnail" image-dired-tag-thumbnail]
1356 ["Comment thumbnail" image-dired-comment-thumbnail]
1357 ["Refresh thumb" image-dired-refresh-thumb]
1358 ["Dynamic line up" image-dired-line-up-dynamic]
1359 ["Line up thumbnails" image-dired-line-up]
1361 ["Rotate thumbnail left" image-dired-rotate-thumbnail-left]
1362 ["Rotate thumbnail right" image-dired-rotate-thumbnail-right]
1363 ["Rotate original left" image-dired-rotate-original-left]
1364 ["Rotate original right" image-dired-rotate-original-right]
1366 ["Toggle movement tracking on/off" image-dired-toggle-movement-tracking]
1368 ["Jump to dired buffer" image-dired-jump-original-dired-buffer]
1369 ["Track original" image-dired-track-original-file]
1371 ["Flag original for deletion" image-dired-flag-thumb-original-file]
1372 ["Unmark original" image-dired-unmark-thumb-original-file]
1373 ["Mark original" image-dired-mark-thumb-original-file]
1375 ["Display in external viewer" image-dired-thumbnail-display-external]
1376 ["Display image" image-dired-display-thumbnail-original-image]))
1377 map)
1378 "Keymap for `image-dired-thumbnail-mode'.")
1380 (defvar image-dired-display-image-mode-map
1381 (let ((map (make-sparse-keymap)))
1382 (define-key map "q" 'image-dired-kill-buffer-and-window)
1383 (define-key map "f" 'image-dired-display-current-image-full)
1384 (define-key map "s" 'image-dired-display-current-image-sized)
1386 (easy-menu-define nil map
1387 "Menu for `image-dired-display-image-mode-map'."
1388 '("Image-Dired"
1389 ["Quit" image-dired-kill-buffer-and-window]
1390 ["Display original, sized to fit" image-dired-display-current-image-sized]
1391 ["Display original, full size" image-dired-display-current-image-full]))
1392 map)
1393 "Keymap for `image-dired-display-image-mode'.")
1395 (defun image-dired-display-current-image-full ()
1396 "Display current image in full size."
1397 (interactive)
1398 (let ((file (image-dired-original-file-name)))
1399 (if file
1400 (progn
1401 (image-dired-display-image file t)
1402 (message "Full size image displayed"))
1403 (error "No original file name at point"))))
1405 (defun image-dired-display-current-image-sized ()
1406 "Display current image in sized to fit window dimensions."
1407 (interactive)
1408 (let ((file (image-dired-original-file-name)))
1409 (if file
1410 (progn
1411 (image-dired-display-image file)
1412 (message "Fitted image displayed"))
1413 (error "No original file name at point"))))
1415 (define-derived-mode image-dired-thumbnail-mode
1416 fundamental-mode "image-dired-thumbnail"
1417 "Browse and manipulate thumbnail images using dired.
1418 Use `image-dired-dired' and `image-dired-setup-dired-keybindings' to get a
1419 nice setup to start with."
1420 (message "image-dired-thumbnail-mode enabled"))
1422 (define-derived-mode image-dired-display-image-mode
1423 fundamental-mode "image-dired-image-display"
1424 "Mode for displaying and manipulating original image.
1425 Resized or in full-size."
1426 (message "image-dired-display-image-mode enabled"))
1428 ;;;###autoload
1429 (defun image-dired-setup-dired-keybindings ()
1430 "Setup easy-to-use keybindings for the commands to be used in dired mode.
1431 Note that n, p and <down> and <up> will be hijacked and bound to
1432 `image-dired-dired-x-line'."
1433 (interactive)
1435 ;; Hijack previous and next line movement. Let C-p and C-b be
1436 ;; though...
1438 (define-key dired-mode-map "p" 'image-dired-dired-previous-line)
1439 (define-key dired-mode-map "n" 'image-dired-dired-next-line)
1440 (define-key dired-mode-map [up] 'image-dired-dired-previous-line)
1441 (define-key dired-mode-map [down] 'image-dired-dired-next-line)
1443 (define-key dired-mode-map (kbd "C-S-n") 'image-dired-next-line-and-display)
1444 (define-key dired-mode-map (kbd "C-S-p") 'image-dired-previous-line-and-display)
1445 (define-key dired-mode-map (kbd "C-S-m") 'image-dired-mark-and-display-next)
1447 (define-key dired-mode-map "\C-td" 'image-dired-display-thumbs)
1448 (define-key dired-mode-map "\C-tt" 'image-dired-tag-files)
1449 (define-key dired-mode-map "\C-tr" 'image-dired-delete-tag)
1450 (define-key dired-mode-map [tab] 'image-dired-jump-thumbnail-buffer)
1451 (define-key dired-mode-map "\C-ti" 'image-dired-dired-display-image)
1452 (define-key dired-mode-map "\C-tx" 'image-dired-dired-display-external)
1453 (define-key dired-mode-map "\C-ta" 'image-dired-display-thumbs-append)
1454 (define-key dired-mode-map "\C-t." 'image-dired-display-thumb)
1455 (define-key dired-mode-map "\C-tc" 'image-dired-dired-comment-files)
1456 (define-key dired-mode-map "\C-tf" 'image-dired-mark-tagged-files)
1458 ;; Menu for dired
1459 (define-key dired-mode-map [menu-bar image-dired]
1460 (cons "Image-Dired" (make-sparse-keymap "Image-Dired")))
1462 (define-key dired-mode-map [menu-bar image-dired image-dired-copy-with-exif-file-name]
1463 '("Copy with EXIF file name" . image-dired-copy-with-exif-file-name))
1465 (define-key dired-mode-map [menu-bar image-dired image-dired-dired-comment-files]
1466 '("Comment files" . image-dired-dired-comment-files))
1468 (define-key dired-mode-map [menu-bar image-dired image-dired-mark-tagged-files]
1469 '("Mark tagged files" . image-dired-mark-tagged-files))
1471 (define-key dired-mode-map [menu-bar image-dired image-dired-delete-tag]
1472 '("Remove tag from files" . image-dired-delete-tag))
1474 (define-key dired-mode-map [menu-bar image-dired image-dired-tag-files]
1475 '("Tag files" . image-dired-tag-files))
1477 (define-key dired-mode-map [menu-bar image-dired image-dired-jump-thumbnail-buffer]
1478 '("Jump to thumbnail buffer" . image-dired-jump-thumbnail-buffer))
1480 (define-key dired-mode-map [menu-bar image-dired image-dired-toggle-movement-tracking]
1481 '("Toggle movement tracking" . image-dired-toggle-movement-tracking))
1483 (define-key dired-mode-map
1484 [menu-bar image-dired image-dired-toggle-append-browsing]
1485 '("Toggle append browsing" . image-dired-toggle-append-browsing))
1487 (define-key dired-mode-map
1488 [menu-bar image-dired image-dired-toggle-disp-props]
1489 '("Toggle display properties" . image-dired-toggle-dired-display-properties))
1491 (define-key dired-mode-map
1492 [menu-bar image-dired image-dired-dired-display-external]
1493 '("Display in external viewer" . image-dired-dired-display-external))
1494 (define-key dired-mode-map
1495 [menu-bar image-dired image-dired-dired-display-image]
1496 '("Display image" . image-dired-dired-display-image))
1497 (define-key dired-mode-map
1498 [menu-bar image-dired image-dired-display-thumb]
1499 '("Display this thumbnail" . image-dired-display-thumb))
1500 (define-key dired-mode-map
1501 [menu-bar image-dired image-dired-display-thumbs-append]
1502 '("Display thumbnails append" . image-dired-display-thumbs-append))
1503 (define-key dired-mode-map
1504 [menu-bar image-dired image-dired-display-thumbs]
1505 '("Display thumbnails" . image-dired-display-thumbs))
1507 (define-key dired-mode-map
1508 [menu-bar image-dired image-dired-create-thumbs]
1509 '("Create thumbnails for marked files" . image-dired-create-thumbs))
1511 (define-key dired-mode-map
1512 [menu-bar image-dired image-dired-mark-and-display-next]
1513 '("Mark and display next" . image-dired-mark-and-display-next))
1514 (define-key dired-mode-map
1515 [menu-bar image-dired image-dired-previous-line-and-display]
1516 '("Display thumb for previous file" . image-dired-previous-line-and-display))
1517 (define-key dired-mode-map
1518 [menu-bar image-dired image-dired-next-line-and-display]
1519 '("Display thumb for next file" . image-dired-next-line-and-display)))
1521 (declare-function clear-image-cache "image.c" (&optional filter))
1523 (defun image-dired-create-thumbs (&optional arg)
1524 "Create thumbnail images for all marked files in dired.
1525 With prefix argument ARG, create thumbnails even if they already exist
1526 \(i.e. use this to refresh your thumbnails)."
1527 (interactive "P")
1528 (let (thumb-name)
1529 (dolist (curr-file (dired-get-marked-files))
1530 (setq thumb-name (image-dired-thumb-name curr-file))
1531 ;; If the user overrides the exist check, we must clear the
1532 ;; image cache so that if the user wants to display the
1533 ;; thumbnail, it is not fetched from cache.
1534 (if arg
1535 (clear-image-cache))
1536 (when (or (not (file-exists-p thumb-name))
1537 arg)
1538 (when (not (= 0 (image-dired-create-thumb curr-file thumb-name)))
1539 (error "Thumb could not be created"))))))
1541 (defvar image-dired-slideshow-timer nil
1542 "Slideshow timer.")
1544 (defvar image-dired-slideshow-count 0
1545 "Keeping track on number of images in slideshow.")
1547 (defvar image-dired-slideshow-times 0
1548 "Number of pictures to display in slideshow.")
1550 (defun image-dired-slideshow-step ()
1551 "Step to next file, if `image-dired-slideshow-times' has not been reached."
1552 (if (< image-dired-slideshow-count image-dired-slideshow-times)
1553 (progn
1554 (message "%s" (1+ image-dired-slideshow-count))
1555 (setq image-dired-slideshow-count (1+ image-dired-slideshow-count))
1556 (image-dired-next-line-and-display))
1557 (image-dired-slideshow-stop)))
1559 (defun image-dired-slideshow-start ()
1560 "Start slideshow.
1561 Ask user for number of images to show and the delay in between."
1562 (interactive)
1563 (setq image-dired-slideshow-count 0)
1564 (setq image-dired-slideshow-times (string-to-number (read-string "How many: ")))
1565 (let ((repeat (string-to-number
1566 (read-string
1567 "Delay, in seconds. Decimals are accepted : " "1"))))
1568 (setq image-dired-slideshow-timer
1569 (run-with-timer
1570 0 repeat
1571 'image-dired-slideshow-step))))
1573 (defun image-dired-slideshow-stop ()
1574 "Cancel slideshow."
1575 (interactive)
1576 (cancel-timer image-dired-slideshow-timer))
1578 (defun image-dired-delete-char ()
1579 "Remove current thumbnail from thumbnail buffer and line up."
1580 (interactive)
1581 (let ((inhibit-read-only t))
1582 (delete-char 1)
1583 (if (looking-at " ")
1584 (delete-char 1))))
1586 ;;;###autoload
1587 (defun image-dired-display-thumbs-append ()
1588 "Append thumbnails to `image-dired-thumbnail-buffer'."
1589 (interactive)
1590 (image-dired-display-thumbs nil t t))
1592 ;;;###autoload
1593 (defun image-dired-display-thumb ()
1594 "Shorthand for `image-dired-display-thumbs' with prefix argument."
1595 (interactive)
1596 (image-dired-display-thumbs t nil t))
1598 (defun image-dired-line-up ()
1599 "Line up thumbnails according to `image-dired-thumbs-per-row'.
1600 See also `image-dired-line-up-dynamic'."
1601 (interactive)
1602 (let ((inhibit-read-only t))
1603 (goto-char (point-min))
1604 (while (and (not (image-dired-image-at-point-p))
1605 (not (eobp)))
1606 (delete-char 1))
1607 (while (not (eobp))
1608 (forward-char)
1609 (while (and (not (image-dired-image-at-point-p))
1610 (not (eobp)))
1611 (delete-char 1)))
1612 (goto-char (point-min))
1613 (let ((count 0))
1614 (while (not (eobp))
1615 (forward-char)
1616 (if (= image-dired-thumbs-per-row 1)
1617 (insert "\n")
1618 (insert " ")
1619 (setq count (1+ count))
1620 (when (and (= count (- image-dired-thumbs-per-row 1))
1621 (not (eobp)))
1622 (forward-char)
1623 (insert "\n")
1624 (setq count 0)))))
1625 (goto-char (point-min))))
1627 (defun image-dired-line-up-dynamic ()
1628 "Line up thumbnails images dynamically.
1629 Calculate how many thumbnails fit."
1630 (interactive)
1631 (let* ((char-width (frame-char-width))
1632 (width (image-dired-window-width-pixels (image-dired-thumbnail-window)))
1633 (image-dired-thumbs-per-row
1634 (/ width
1635 (+ (* 2 image-dired-thumb-relief)
1636 (* 2 image-dired-thumb-margin)
1637 image-dired-thumb-width char-width))))
1638 (image-dired-line-up)))
1640 (defun image-dired-line-up-interactive ()
1641 "Line up thumbnails interactively.
1642 Ask user how many thumbnails should be displayed per row."
1643 (interactive)
1644 (let ((image-dired-thumbs-per-row
1645 (string-to-number (read-string "How many thumbs per row: "))))
1646 (if (not (> image-dired-thumbs-per-row 0))
1647 (message "Number must be greater than 0")
1648 (image-dired-line-up))))
1650 (defun image-dired-thumbnail-display-external ()
1651 "Display original image for thumbnail at point using external viewer."
1652 (interactive)
1653 (let ((file (image-dired-original-file-name)))
1654 (if (not (image-dired-image-at-point-p))
1655 (message "No thumbnail at point")
1656 (if (not file)
1657 (message "No original file name found")
1658 (call-process shell-file-name nil nil nil shell-command-switch
1659 (format "%s \"%s\"" image-dired-external-viewer file))))))
1661 ;;;###autoload
1662 (defun image-dired-dired-display-external ()
1663 "Display file at point using an external viewer."
1664 (interactive)
1665 (let ((file (dired-get-filename)))
1666 (call-process shell-file-name nil nil nil shell-command-switch
1667 (format "%s \"%s\"" image-dired-external-viewer file))))
1669 (defun image-dired-window-width-pixels (window)
1670 "Calculate WINDOW width in pixels."
1671 (* (window-width window) (frame-char-width)))
1673 (defun image-dired-window-height-pixels (window)
1674 "Calculate WINDOW height in pixels."
1675 ;; Note: The mode-line consumes one line
1676 (* (- (window-height window) 1) (frame-char-height)))
1678 (defun image-dired-display-window ()
1679 "Return window where `image-dired-display-image-buffer' is visible."
1680 (get-window-with-predicate
1681 (lambda (window)
1682 (equal (buffer-name (window-buffer window)) image-dired-display-image-buffer))
1683 nil t))
1685 (defun image-dired-thumbnail-window ()
1686 "Return window where `image-dired-thumbnail-buffer' is visible."
1687 (get-window-with-predicate
1688 (lambda (window)
1689 (equal (buffer-name (window-buffer window)) image-dired-thumbnail-buffer))
1690 nil t))
1692 (defun image-dired-associated-dired-buffer-window ()
1693 "Return window where associated dired buffer is visible."
1694 (let (buf)
1695 (if (image-dired-image-at-point-p)
1696 (progn
1697 (setq buf (image-dired-associated-dired-buffer))
1698 (get-window-with-predicate
1699 (lambda (window)
1700 (equal (window-buffer window) buf))))
1701 (error "No thumbnail image at point"))))
1703 (defun image-dired-display-window-width ()
1704 "Return width, in pixels, of image-dired's image display window."
1705 (- (image-dired-window-width-pixels (image-dired-display-window))
1706 image-dired-display-window-width-correction))
1708 (defun image-dired-display-window-height ()
1709 "Return height, in pixels, of image-dired's image display window."
1710 (- (image-dired-window-height-pixels (image-dired-display-window))
1711 image-dired-display-window-height-correction))
1713 (defun image-dired-display-image (file &optional original-size)
1714 "Display image FILE in image buffer.
1715 Use this when you want to display the image, semi sized, in a new
1716 window. The image is sized to fit the display window (using a
1717 temporary file, don't worry). Because of this, it will not be as
1718 quick as opening it directly, but on most modern systems it
1719 should feel snappy enough.
1721 If optional argument ORIGINAL-SIZE is non-nil, display image in its
1722 original size."
1723 (image-dired--check-executable-exists
1724 'image-dired-cmd-create-temp-image-program)
1725 (let ((new-file (expand-file-name image-dired-temp-image-file))
1726 width height command ret
1727 (image-type 'jpeg))
1728 (setq file (expand-file-name file))
1729 (if (not original-size)
1730 (progn
1731 (setq width (image-dired-display-window-width))
1732 (setq height (image-dired-display-window-height))
1733 (setq command
1734 (format-spec
1735 image-dired-cmd-create-temp-image-options
1736 (list
1737 (cons ?p image-dired-cmd-create-temp-image-program)
1738 (cons ?w width)
1739 (cons ?h height)
1740 (cons ?f file)
1741 (cons ?t new-file))))
1742 (setq ret (call-process shell-file-name nil nil nil
1743 shell-command-switch command))
1744 (if (not (= 0 ret))
1745 (error "Could not resize image")))
1746 (setq image-type (image-type-from-file-name file))
1747 (copy-file file new-file t))
1748 (with-current-buffer (image-dired-create-display-image-buffer)
1749 (let ((inhibit-read-only t))
1750 (erase-buffer)
1751 (clear-image-cache)
1752 (image-dired-insert-image image-dired-temp-image-file image-type 0 0)
1753 (goto-char (point-min))
1754 (image-dired-update-property 'original-file-name file)))))
1756 (defun image-dired-display-thumbnail-original-image (&optional arg)
1757 "Display current thumbnail's original image in display buffer.
1758 See documentation for `image-dired-display-image' for more information.
1759 With prefix argument ARG, display image in its original size."
1760 (interactive "P")
1761 (let ((file (image-dired-original-file-name)))
1762 (if (not (string-equal major-mode "image-dired-thumbnail-mode"))
1763 (message "Not in image-dired-thumbnail-mode")
1764 (if (not (image-dired-image-at-point-p))
1765 (message "No thumbnail at point")
1766 (if (not file)
1767 (message "No original file name found")
1768 (image-dired-create-display-image-buffer)
1769 (display-buffer image-dired-display-image-buffer)
1770 (image-dired-display-image file arg))))))
1773 ;;;###autoload
1774 (defun image-dired-dired-display-image (&optional arg)
1775 "Display current image file.
1776 See documentation for `image-dired-display-image' for more information.
1777 With prefix argument ARG, display image in its original size."
1778 (interactive "P")
1779 (image-dired-create-display-image-buffer)
1780 (display-buffer image-dired-display-image-buffer)
1781 (image-dired-display-image (dired-get-filename) arg))
1783 (defun image-dired-image-at-point-p ()
1784 "Return true if there is an image-dired thumbnail at point."
1785 (get-text-property (point) 'image-dired-thumbnail))
1787 (defun image-dired-rotate-thumbnail (degrees)
1788 "Rotate thumbnail DEGREES degrees."
1789 (image-dired--check-executable-exists
1790 'image-dired-cmd-rotate-thumbnail-program)
1791 (if (not (image-dired-image-at-point-p))
1792 (message "No thumbnail at point")
1793 (let ((file (image-dired-thumb-name (image-dired-original-file-name)))
1794 command)
1795 (setq command (format-spec
1796 image-dired-cmd-rotate-thumbnail-options
1797 (list
1798 (cons ?p image-dired-cmd-rotate-thumbnail-program)
1799 (cons ?d degrees)
1800 (cons ?t (expand-file-name file)))))
1801 (call-process shell-file-name nil nil nil shell-command-switch command)
1802 ;; Clear the cache to refresh image. I wish I could just refresh
1803 ;; the current file but I do not know how to do that. Yet...
1804 (clear-image-cache))))
1806 (defun image-dired-rotate-thumbnail-left ()
1807 "Rotate thumbnail left (counter clockwise) 90 degrees.
1808 The result of the rotation is displayed in the image display area
1809 and a confirmation is needed before the original image files is
1810 overwritten. This confirmation can be turned off using
1811 `image-dired-rotate-original-ask-before-overwrite'."
1812 (interactive)
1813 (image-dired-rotate-thumbnail "270"))
1815 (defun image-dired-rotate-thumbnail-right ()
1816 "Rotate thumbnail counter right (clockwise) 90 degrees.
1817 The result of the rotation is displayed in the image display area
1818 and a confirmation is needed before the original image files is
1819 overwritten. This confirmation can be turned off using
1820 `image-dired-rotate-original-ask-before-overwrite'."
1821 (interactive)
1822 (image-dired-rotate-thumbnail "90"))
1824 (defun image-dired-refresh-thumb ()
1825 "Force creation of new image for current thumbnail."
1826 (interactive)
1827 (let ((file (image-dired-original-file-name)))
1828 (clear-image-cache)
1829 (image-dired-create-thumb file (image-dired-thumb-name file))))
1831 (defun image-dired-rotate-original (degrees)
1832 "Rotate original image DEGREES degrees."
1833 (image-dired--check-executable-exists
1834 'image-dired-cmd-rotate-original-program)
1835 (if (not (image-dired-image-at-point-p))
1836 (message "No image at point")
1837 (let ((file (image-dired-original-file-name))
1838 command)
1839 (unless (eq 'jpeg (image-type file))
1840 (error "Only JPEG images can be rotated!"))
1841 (setq command (format-spec
1842 image-dired-cmd-rotate-original-options
1843 (list
1844 (cons ?p image-dired-cmd-rotate-original-program)
1845 (cons ?d degrees)
1846 (cons ?o (expand-file-name file))
1847 (cons ?t image-dired-temp-rotate-image-file))))
1848 (if (not (= 0 (call-process shell-file-name nil nil nil
1849 shell-command-switch command)))
1850 (error "Could not rotate image")
1851 (image-dired-display-image image-dired-temp-rotate-image-file)
1852 (if (or (and image-dired-rotate-original-ask-before-overwrite
1853 (y-or-n-p
1854 "Rotate to temp file OK. Overwrite original image? "))
1855 (not image-dired-rotate-original-ask-before-overwrite))
1856 (progn
1857 (copy-file image-dired-temp-rotate-image-file file t)
1858 (image-dired-refresh-thumb))
1859 (image-dired-display-image file))))))
1861 (defun image-dired-rotate-original-left ()
1862 "Rotate original image left (counter clockwise) 90 degrees."
1863 (interactive)
1864 (image-dired-rotate-original "270"))
1866 (defun image-dired-rotate-original-right ()
1867 "Rotate original image right (clockwise) 90 degrees."
1868 (interactive)
1869 (image-dired-rotate-original "90"))
1871 (defun image-dired-get-exif-file-name (file)
1872 "Use the image's EXIF information to return a unique file name.
1873 The file name should be unique as long as you do not take more than
1874 one picture per second. The original file name is suffixed at the end
1875 for traceability. The format of the returned file name is
1876 YYYY_MM_DD_HH_MM_DD_ORIG_FILE_NAME.jpg. Used from
1877 `image-dired-copy-with-exif-file-name'."
1878 (let (data no-exif-data-found)
1879 (if (not (eq 'jpeg (image-type (expand-file-name file))))
1880 (setq no-exif-data-found t
1881 data (format-time-string
1882 "%Y:%m:%d %H:%M:%S"
1883 (file-attribute-modification-time
1884 (file-attributes (expand-file-name file)))))
1885 (setq data (image-dired-get-exif-data (expand-file-name file)
1886 "DateTimeOriginal")))
1887 (while (string-match "[ :]" data)
1888 (setq data (replace-match "_" nil nil data)))
1889 (format "%s%s%s" data
1890 (if no-exif-data-found
1891 "_noexif_"
1892 "_")
1893 (file-name-nondirectory file))))
1895 (defun image-dired-thumbnail-set-image-description ()
1896 "Set the ImageDescription EXIF tag for the original image.
1897 If the image already has a value for this tag, it is used as the
1898 default value at the prompt."
1899 (interactive)
1900 (if (not (image-dired-image-at-point-p))
1901 (message "No thumbnail at point")
1902 (let* ((file (image-dired-original-file-name))
1903 (old-value (image-dired-get-exif-data file "ImageDescription")))
1904 (if (eq 0
1905 (image-dired-set-exif-data file "ImageDescription"
1906 (read-string "Value of ImageDescription: "
1907 old-value)))
1908 (message "Successfully wrote ImageDescription tag.")
1909 (error "Could not write ImageDescription tag")))))
1911 (defun image-dired-set-exif-data (file tag-name tag-value)
1912 "In FILE, set EXIF tag TAG-NAME to value TAG-VALUE."
1913 (image-dired--check-executable-exists
1914 'image-dired-cmd-write-exif-data-program)
1915 (let (command)
1916 (setq command (format-spec
1917 image-dired-cmd-write-exif-data-options
1918 (list
1919 (cons ?p image-dired-cmd-write-exif-data-program)
1920 (cons ?f (expand-file-name file))
1921 (cons ?t tag-name)
1922 (cons ?v tag-value))))
1923 (call-process shell-file-name nil nil nil shell-command-switch command)))
1925 (defun image-dired-get-exif-data (file tag-name)
1926 "From FILE, return EXIF tag TAG-NAME."
1927 (image-dired--check-executable-exists
1928 'image-dired-cmd-read-exif-data-program)
1929 (let ((buf (get-buffer-create "*image-dired-get-exif-data*"))
1930 command tag-value)
1931 (setq command (format-spec
1932 image-dired-cmd-read-exif-data-options
1933 (list
1934 (cons ?p image-dired-cmd-read-exif-data-program)
1935 (cons ?f file)
1936 (cons ?t tag-name))))
1937 (with-current-buffer buf
1938 (delete-region (point-min) (point-max))
1939 (if (not (eq (call-process shell-file-name nil t nil
1940 shell-command-switch command) 0))
1941 (error "Could not get EXIF tag")
1942 (goto-char (point-min))
1943 ;; Clean buffer from newlines and carriage returns before
1944 ;; getting final info
1945 (while (search-forward-regexp "[\n\r]" nil t)
1946 (replace-match "" nil t))
1947 (setq tag-value (buffer-substring (point-min) (point-max)))))
1948 tag-value))
1950 (defun image-dired-copy-with-exif-file-name ()
1951 "Copy file with unique name to main image directory.
1952 Copy current or all marked files in dired to a new file in your
1953 main image directory, using a file name generated by
1954 `image-dired-get-exif-file-name'. A typical usage for this if when
1955 copying images from a digital camera into the image directory.
1957 Typically, you would open up the folder with the incoming
1958 digital images, mark the files to be copied, and execute this
1959 function. The result is a couple of new files in
1960 `image-dired-main-image-directory' called
1961 2005_05_08_12_52_00_dscn0319.jpg,
1962 2005_05_08_14_27_45_dscn0320.jpg etc."
1963 (interactive)
1964 (let (new-name
1965 (files (dired-get-marked-files)))
1966 (mapcar
1967 (lambda (curr-file)
1968 (setq new-name
1969 (format "%s/%s"
1970 (file-name-as-directory
1971 (expand-file-name image-dired-main-image-directory))
1972 (image-dired-get-exif-file-name curr-file)))
1973 (message "Copying %s to %s" curr-file new-name)
1974 (copy-file curr-file new-name))
1975 files)))
1977 (defun image-dired-display-next-thumbnail-original ()
1978 "In thumbnail buffer, move to next thumbnail and display the image."
1979 (interactive)
1980 (image-dired-forward-image)
1981 (image-dired-display-thumbnail-original-image))
1983 (defun image-dired-display-previous-thumbnail-original ()
1984 "Move to previous thumbnail and display image."
1985 (interactive)
1986 (image-dired-backward-image)
1987 (image-dired-display-thumbnail-original-image))
1989 (defun image-dired-write-comments (file-comments)
1990 "Write file comments to database.
1991 Write file comments to one or more files.
1992 FILE-COMMENTS is an alist on the following form:
1993 ((FILE . COMMENT) ... )"
1994 (image-dired-sane-db-file)
1995 (let (end comment-beg-pos comment-end-pos file comment)
1996 (image-dired--with-db-file
1997 (setq buffer-file-name image-dired-db-file)
1998 (dolist (elt file-comments)
1999 (setq file (car elt)
2000 comment (cdr elt))
2001 (goto-char (point-min))
2002 (if (search-forward-regexp (format "^%s.*$" file) nil t)
2003 (progn
2004 (setq end (point))
2005 (beginning-of-line)
2006 ;; Delete old comment, if any
2007 (when (search-forward ";comment:" end t)
2008 (setq comment-beg-pos (match-beginning 0))
2009 ;; Any tags after the comment?
2010 (if (search-forward ";" end t)
2011 (setq comment-end-pos (- (point) 1))
2012 (setq comment-end-pos end))
2013 ;; Delete comment tag and comment
2014 (delete-region comment-beg-pos comment-end-pos))
2015 ;; Insert new comment
2016 (beginning-of-line)
2017 (unless (search-forward ";" end t)
2018 (end-of-line)
2019 (insert ";"))
2020 (insert (format "comment:%s;" comment)))
2021 ;; File does not exist in database - add it.
2022 (goto-char (point-max))
2023 (insert (format "\n%s;comment:%s" file comment))))
2024 (save-buffer))))
2026 (defun image-dired-update-property (prop value)
2027 "Update text property PROP with value VALUE at point."
2028 (let ((inhibit-read-only t))
2029 (put-text-property
2030 (point) (1+ (point))
2031 prop
2032 value)))
2034 ;;;###autoload
2035 (defun image-dired-dired-comment-files ()
2036 "Add comment to current or marked files in dired."
2037 (interactive)
2038 (let ((comment (image-dired-read-comment)))
2039 (image-dired-write-comments
2040 (mapcar
2041 (lambda (curr-file)
2042 (cons curr-file comment))
2043 (dired-get-marked-files)))))
2045 (defun image-dired-comment-thumbnail ()
2046 "Add comment to current thumbnail in thumbnail buffer."
2047 (interactive)
2048 (let* ((file (image-dired-original-file-name))
2049 (comment (image-dired-read-comment file)))
2050 (image-dired-write-comments (list (cons file comment)))
2051 (image-dired-update-property 'comment comment))
2052 (image-dired-display-thumb-properties))
2054 (defun image-dired-read-comment (&optional file)
2055 "Read comment for an image.
2056 Optionally use old comment from FILE as initial value."
2057 (let ((comment
2058 (read-string
2059 "Comment: "
2060 (if file (image-dired-get-comment file)))))
2061 comment))
2063 (defun image-dired-get-comment (file)
2064 "Get comment for file FILE."
2065 (image-dired-sane-db-file)
2066 (image-dired--with-db-file
2067 (let (end comment-beg-pos comment-end-pos comment)
2068 (when (search-forward-regexp (format "^%s" file) nil t)
2069 (end-of-line)
2070 (setq end (point))
2071 (beginning-of-line)
2072 (when (search-forward ";comment:" end t)
2073 (setq comment-beg-pos (point))
2074 (if (search-forward ";" end t)
2075 (setq comment-end-pos (- (point) 1))
2076 (setq comment-end-pos end))
2077 (setq comment (buffer-substring
2078 comment-beg-pos comment-end-pos))))
2079 comment)))
2081 ;;;###autoload
2082 (defun image-dired-mark-tagged-files ()
2083 "Use regexp to mark files with matching tag.
2084 A `tag' is a keyword, a piece of meta data, associated with an
2085 image file and stored in image-dired's database file. This command
2086 lets you input a regexp and this will be matched against all tags
2087 on all image files in the database file. The files that have a
2088 matching tag will be marked in the dired buffer."
2089 (interactive)
2090 (image-dired-sane-db-file)
2091 (let ((tag (read-string "Mark tagged files (regexp): "))
2092 (hits 0)
2093 files)
2094 (image-dired--with-db-file
2095 ;; Collect matches
2096 (while (search-forward-regexp
2097 (concat "\\(^[^;\n]+\\);.*" tag ".*$") nil t)
2098 (push (match-string 1) files)))
2099 ;; Mark files
2100 (dolist (curr-file files)
2101 ;; I tried using `dired-mark-files-regexp' but it was waaaay to
2102 ;; slow. Don't bother about hits found in other directories
2103 ;; than the current one.
2104 (when (string= (file-name-as-directory
2105 (expand-file-name default-directory))
2106 (file-name-as-directory
2107 (file-name-directory curr-file)))
2108 (setq curr-file (file-name-nondirectory curr-file))
2109 (goto-char (point-min))
2110 (when (search-forward-regexp (format "\\s %s$" curr-file) nil t)
2111 (setq hits (+ hits 1))
2112 (dired-mark 1))))
2113 (message "%d files with matching tag marked." hits)))
2115 (defun image-dired-mouse-display-image (event)
2116 "Use mouse EVENT, call `image-dired-display-image' to display image.
2117 Track this in associated dired buffer if `image-dired-track-movement' is
2118 non-nil."
2119 (interactive "e")
2120 (mouse-set-point event)
2121 (goto-char (posn-point (event-end event)))
2122 (let ((file (image-dired-original-file-name)))
2123 (when file
2124 (if image-dired-track-movement
2125 (image-dired-track-original-file))
2126 (image-dired-create-display-image-buffer)
2127 (display-buffer image-dired-display-image-buffer)
2128 (image-dired-display-image file))))
2130 (defun image-dired-mouse-select-thumbnail (event)
2131 "Use mouse EVENT to select thumbnail image.
2132 Track this in associated dired buffer if `image-dired-track-movement' is
2133 non-nil."
2134 (interactive "e")
2135 (mouse-set-point event)
2136 (goto-char (posn-point (event-end event)))
2137 (if image-dired-track-movement
2138 (image-dired-track-original-file))
2139 (image-dired-display-thumb-properties))
2141 (defun image-dired-mouse-toggle-mark (event)
2142 "Use mouse EVENT to toggle dired mark for thumbnail.
2143 Track this in associated dired buffer if `image-dired-track-movement' is
2144 non-nil."
2145 (interactive "e")
2146 (mouse-set-point event)
2147 (goto-char (posn-point (event-end event)))
2148 (if image-dired-track-movement
2149 (image-dired-track-original-file))
2150 (image-dired-toggle-mark-thumb-original-file))
2152 (defun image-dired-dired-display-properties ()
2153 "Display properties for dired file in the echo area."
2154 (interactive)
2155 (let* ((file (dired-get-filename))
2156 (file-name (file-name-nondirectory file))
2157 (dired-buf (buffer-name (current-buffer)))
2158 (props (mapconcat
2159 'princ
2160 (image-dired-list-tags file)
2161 ", "))
2162 (comment (image-dired-get-comment file)))
2163 (if file-name
2164 (message "%s"
2165 (image-dired-format-properties-string
2166 dired-buf
2167 file-name
2168 props
2169 comment)))))
2171 (defvar image-dired-tag-file-list nil
2172 "List to store tag-file structure.")
2174 (defvar image-dired-file-tag-list nil
2175 "List to store file-tag structure.")
2177 (defvar image-dired-file-comment-list nil
2178 "List to store file comments.")
2180 (defun image-dired-add-to-tag-file-list (tag file)
2181 "Add relation between TAG and FILE."
2182 (let (curr)
2183 (if image-dired-tag-file-list
2184 (if (setq curr (assoc tag image-dired-tag-file-list))
2185 (if (not (member file curr))
2186 (setcdr curr (cons file (cdr curr))))
2187 (setcdr image-dired-tag-file-list
2188 (cons (list tag file) (cdr image-dired-tag-file-list))))
2189 (setq image-dired-tag-file-list (list (list tag file))))))
2191 (defun image-dired-add-to-tag-file-lists (tag file)
2192 "Helper function used from `image-dired-create-gallery-lists'.
2194 Add TAG to FILE in one list and FILE to TAG in the other.
2196 Lisp structures look like the following:
2198 image-dired-file-tag-list:
2200 ((\"filename1\" \"tag1\" \"tag2\" \"tag3\" ...)
2201 (\"filename2\" \"tag1\" \"tag2\" \"tag3\" ...)
2202 ...)
2204 image-dired-tag-file-list:
2206 ((\"tag1\" \"filename1\" \"filename2\" \"filename3\" ...)
2207 (\"tag2\" \"filename1\" \"filename2\" \"filename3\" ...)
2208 ...)"
2209 ;; Add tag to file list
2210 (let (curr)
2211 (if image-dired-file-tag-list
2212 (if (setq curr (assoc file image-dired-file-tag-list))
2213 (setcdr curr (cons tag (cdr curr)))
2214 (setcdr image-dired-file-tag-list
2215 (cons (list file tag) (cdr image-dired-file-tag-list))))
2216 (setq image-dired-file-tag-list (list (list file tag))))
2217 ;; Add file to tag list
2218 (if image-dired-tag-file-list
2219 (if (setq curr (assoc tag image-dired-tag-file-list))
2220 (if (not (member file curr))
2221 (setcdr curr (cons file (cdr curr))))
2222 (setcdr image-dired-tag-file-list
2223 (cons (list tag file) (cdr image-dired-tag-file-list))))
2224 (setq image-dired-tag-file-list (list (list tag file))))))
2226 (defun image-dired-add-to-file-comment-list (file comment)
2227 "Helper function used from `image-dired-create-gallery-lists'.
2229 For FILE, add COMMENT to list.
2231 Lisp structure looks like the following:
2233 image-dired-file-comment-list:
2235 ((\"filename1\" . \"comment1\")
2236 (\"filename2\" . \"comment2\")
2237 ...)"
2238 (if image-dired-file-comment-list
2239 (if (not (assoc file image-dired-file-comment-list))
2240 (setcdr image-dired-file-comment-list
2241 (cons (cons file comment)
2242 (cdr image-dired-file-comment-list))))
2243 (setq image-dired-file-comment-list (list (cons file comment)))))
2245 (defun image-dired-create-gallery-lists ()
2246 "Create temporary lists used by `image-dired-gallery-generate'."
2247 (image-dired-sane-db-file)
2248 (image-dired--with-db-file
2249 (let (end beg file row-tags)
2250 (setq image-dired-tag-file-list nil)
2251 (setq image-dired-file-tag-list nil)
2252 (setq image-dired-file-comment-list nil)
2253 (goto-char (point-min))
2254 (while (search-forward-regexp "^." nil t)
2255 (end-of-line)
2256 (setq end (point))
2257 (beginning-of-line)
2258 (setq beg (point))
2259 (unless (search-forward ";" end nil)
2260 (error "Something is really wrong, check format of database"))
2261 (setq row-tags (split-string
2262 (buffer-substring beg end) ";"))
2263 (setq file (car row-tags))
2264 (dolist (x (cdr row-tags))
2265 (if (not (string-match "^comment:\\(.*\\)" x))
2266 (image-dired-add-to-tag-file-lists x file)
2267 (image-dired-add-to-file-comment-list file (match-string 1 x)))))))
2268 ;; Sort tag-file list
2269 (setq image-dired-tag-file-list
2270 (sort image-dired-tag-file-list
2271 (lambda (x y)
2272 (string< (car x) (car y))))))
2274 (defun image-dired-hidden-p (file)
2275 "Return t if image FILE has a \"hidden\" tag."
2276 (let (hidden)
2277 (mapc
2278 (lambda (tag)
2279 (if (member tag image-dired-gallery-hidden-tags)
2280 (setq hidden t)))
2281 (cdr (assoc file image-dired-file-tag-list)))
2282 hidden))
2284 (defun image-dired-gallery-generate ()
2285 "Generate gallery pages.
2286 First we create a couple of Lisp structures from the database to make
2287 it easier to generate, then HTML-files are created in
2288 `image-dired-gallery-dir'."
2289 (interactive)
2290 (if (eq 'per-directory image-dired-thumbnail-storage)
2291 (error "Currently, gallery generation is not supported \
2292 when using per-directory thumbnail file storage"))
2293 (image-dired-create-gallery-lists)
2294 (let ((tags image-dired-tag-file-list)
2295 (index-file (format "%s/index.html" image-dired-gallery-dir))
2296 count tag tag-file
2297 comment file-tags tag-link tag-link-list)
2298 ;; Make sure gallery root exist
2299 (if (file-exists-p image-dired-gallery-dir)
2300 (if (not (file-directory-p image-dired-gallery-dir))
2301 (error "Variable image-dired-gallery-dir is not a directory"))
2302 (make-directory image-dired-gallery-dir))
2303 ;; Open index file
2304 (with-temp-file index-file
2305 (if (file-exists-p index-file)
2306 (insert-file-contents index-file))
2307 (insert "<html>\n")
2308 (insert " <body>\n")
2309 (insert " <h2>Image-Dired Gallery</h2>\n")
2310 (insert (format "<p>\n Gallery generated %s\n <p>\n"
2311 (current-time-string)))
2312 (insert " <h3>Tag index</h3>\n")
2313 (setq count 1)
2314 ;; Pre-generate list of all tag links
2315 (dolist (curr tags)
2316 (setq tag (car curr))
2317 (when (not (member tag image-dired-gallery-hidden-tags))
2318 (setq tag-link (format "<a href=\"%d.html\">%s</a>" count tag))
2319 (if tag-link-list
2320 (setq tag-link-list
2321 (append tag-link-list (list (cons tag tag-link))))
2322 (setq tag-link-list (list (cons tag tag-link))))
2323 (setq count (1+ count))))
2324 (setq count 1)
2325 ;; Main loop where we generated thumbnail pages per tag
2326 (dolist (curr tags)
2327 (setq tag (car curr))
2328 ;; Don't display hidden tags
2329 (when (not (member tag image-dired-gallery-hidden-tags))
2330 ;; Insert link to tag page in index
2331 (insert (format " %s<br>\n" (cdr (assoc tag tag-link-list))))
2332 ;; Open per-tag file
2333 (setq tag-file (format "%s/%s.html" image-dired-gallery-dir count))
2334 (with-temp-file tag-file
2335 (if (file-exists-p tag-file)
2336 (insert-file-contents tag-file))
2337 (erase-buffer)
2338 (insert "<html>\n")
2339 (insert " <body>\n")
2340 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2341 (insert (format " <h2>Images with tag &quot;%s&quot;</h2>" tag))
2342 ;; Main loop for files per tag page
2343 (dolist (file (cdr curr))
2344 (unless (image-dired-hidden-p file)
2345 ;; Insert thumbnail with link to full image
2346 (insert
2347 (format "<a href=\"%s/%s\"><img src=\"%s/%s\"%s></a>\n"
2348 image-dired-gallery-image-root-url
2349 (file-name-nondirectory file)
2350 image-dired-gallery-thumb-image-root-url
2351 (file-name-nondirectory (image-dired-thumb-name file)) file))
2352 ;; Insert comment, if any
2353 (if (setq comment (cdr (assoc file image-dired-file-comment-list)))
2354 (insert (format "<br>\n%s<br>\n" comment))
2355 (insert "<br>\n"))
2356 ;; Insert links to other tags, if any
2357 (when (> (length
2358 (setq file-tags (assoc file image-dired-file-tag-list))) 2)
2359 (insert "[ ")
2360 (dolist (extra-tag file-tags)
2361 ;; Only insert if not file name or the main tag
2362 (if (and (not (equal extra-tag tag))
2363 (not (equal extra-tag file)))
2364 (insert
2365 (format "%s " (cdr (assoc extra-tag tag-link-list))))))
2366 (insert "]<br>\n"))))
2367 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2368 (insert " </body>\n")
2369 (insert "</html>\n"))
2370 (setq count (1+ count))))
2371 (insert " </body>\n")
2372 (insert "</html>"))))
2374 (defun image-dired-kill-buffer-and-window ()
2375 "Kill the current buffer and, if possible, also the window."
2376 (interactive)
2377 (let ((buffer (current-buffer)))
2378 (condition-case nil
2379 (delete-window (selected-window))
2380 (error nil))
2381 (kill-buffer buffer)))
2383 (defvar image-dired-widget-list nil
2384 "List to keep track of meta data in edit buffer.")
2386 (declare-function widget-forward "wid-edit" (arg))
2388 ;;;###autoload
2389 (defun image-dired-dired-edit-comment-and-tags ()
2390 "Edit comment and tags of current or marked image files.
2391 Edit comment and tags for all marked image files in an
2392 easy-to-use form."
2393 (interactive)
2394 (setq image-dired-widget-list nil)
2395 ;; Setup buffer.
2396 (let ((files (dired-get-marked-files)))
2397 (pop-to-buffer-same-window "*Image-Dired Edit Meta Data*")
2398 (kill-all-local-variables)
2399 (make-local-variable 'widget-example-repeat)
2400 (let ((inhibit-read-only t))
2401 (erase-buffer))
2402 (remove-overlays)
2403 ;; Some help for the user.
2404 (widget-insert
2405 "\nEdit comments and tags for each image. Separate multiple tags
2406 with a comma. Move forward between fields using TAB or RET.
2407 Move to the previous field using backtab (S-TAB). Save by
2408 activating the Save button at the bottom of the form or cancel
2409 the operation by activating the Cancel button.\n\n")
2410 ;; Here comes all images and a comment and tag field for each
2411 ;; image.
2412 (let (thumb-file img comment-widget tag-widget)
2414 (dolist (file files)
2416 (setq thumb-file (image-dired-thumb-name file)
2417 img (create-image thumb-file))
2419 (insert-image img)
2420 (widget-insert "\n\nComment: ")
2421 (setq comment-widget
2422 (widget-create 'editable-field
2423 :size 60
2424 :format "%v "
2425 :value (or (image-dired-get-comment file) "")))
2426 (widget-insert "\nTags: ")
2427 (setq tag-widget
2428 (widget-create 'editable-field
2429 :size 60
2430 :format "%v "
2431 :value (or (mapconcat
2432 (lambda (tag)
2433 tag)
2434 (image-dired-list-tags file)
2435 ",") "")))
2436 ;; Save information in all widgets so that we can use it when
2437 ;; the user saves the form.
2438 (setq image-dired-widget-list
2439 (append image-dired-widget-list
2440 (list (list file comment-widget tag-widget))))
2441 (widget-insert "\n\n")))
2443 ;; Footer with Save and Cancel button.
2444 (widget-insert "\n")
2445 (widget-create 'push-button
2446 :notify
2447 (lambda (&rest _ignore)
2448 (image-dired-save-information-from-widgets)
2449 (bury-buffer)
2450 (message "Done."))
2451 "Save")
2452 (widget-insert " ")
2453 (widget-create 'push-button
2454 :notify
2455 (lambda (&rest _ignore)
2456 (bury-buffer)
2457 (message "Operation canceled."))
2458 "Cancel")
2459 (widget-insert "\n")
2460 (use-local-map widget-keymap)
2461 (widget-setup)
2462 ;; Jump to the first widget.
2463 (widget-forward 1)))
2465 (defun image-dired-save-information-from-widgets ()
2466 "Save information found in `image-dired-widget-list'.
2467 Use the information in `image-dired-widget-list' to save comments and
2468 tags to their respective image file. Internal function used by
2469 `image-dired-dired-edit-comment-and-tags'."
2470 (let (file comment tag-string tag-list lst)
2471 (image-dired-write-comments
2472 (mapcar
2473 (lambda (widget)
2474 (setq file (car widget)
2475 comment (widget-value (cadr widget)))
2476 (cons file comment))
2477 image-dired-widget-list))
2478 (image-dired-write-tags
2479 (dolist (widget image-dired-widget-list lst)
2480 (setq file (car widget)
2481 tag-string (widget-value (car (cddr widget)))
2482 tag-list (split-string tag-string ","))
2483 (dolist (tag tag-list)
2484 (push (cons file tag) lst))))))
2486 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2487 ;;;;;;;;; TEST-SECTION ;;;;;;;;;;;
2488 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2490 ;; (defvar image-dired-dir-max-size 12300000)
2492 ;; (defun image-dired-test-clean-old-files ()
2493 ;; "Clean `image-dired-dir' from old thumbnail files.
2494 ;; \"Oldness\" measured using last access time. If the total size of all
2495 ;; thumbnail files in `image-dired-dir' is larger than 'image-dired-dir-max-size',
2496 ;; old files are deleted until the max size is reached."
2497 ;; (let* ((files
2498 ;; (sort
2499 ;; (mapcar
2500 ;; (lambda (f)
2501 ;; (let ((fattribs (file-attributes f)))
2502 ;; ;; Get last access time and file size
2503 ;; `(,(nth 4 fattribs) ,(nth 7 fattribs) ,f)))
2504 ;; (directory-files (image-dired-dir) t ".+\\.thumb\\..+$"))
2505 ;; ;; Sort function. Compare time between two files.
2506 ;; (lambda (l1 l2)
2507 ;; (time-less-p (car l1) (car l2)))))
2508 ;; (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) files))))
2509 ;; (while (> dirsize image-dired-dir-max-size)
2510 ;; (y-or-n-p
2511 ;; (format "Size of thumbnail directory: %d, delete old file %s? "
2512 ;; dirsize (cadr (cdar files))))
2513 ;; (delete-file (cadr (cdar files)))
2514 ;; (setq dirsize (- dirsize (car (cdar files))))
2515 ;; (setq files (cdr files)))))
2517 ;;;;;;;;;;;;;;;;;;;;;;,
2519 ;; (defun dired-speedbar-buttons (dired-buffer)
2520 ;; (when (and (boundp 'image-dired-use-speedbar)
2521 ;; image-dired-use-speedbar)
2522 ;; (let ((filename (with-current-buffer dired-buffer
2523 ;; (dired-get-filename))))
2524 ;; (when (and (not (string-equal filename (buffer-string)))
2525 ;; (string-match (image-file-name-regexp) filename))
2526 ;; (erase-buffer)
2527 ;; (insert (propertize
2528 ;; filename
2529 ;; 'display
2530 ;; (image-dired-get-thumbnail-image filename)))))))
2532 ;; (setq image-dired-use-speedbar t)
2534 (provide 'image-dired)
2536 ;;; image-dired.el ends here