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