Unify CNS11643-15
[emacs.git] / lisp / image-dired.el
blob49dba52c88430fcdedc76703cd5e6b13418c4399
1 ;;; image-dired.el --- use dired to browse and manipulate your images -*- lexical-binding: t -*-
2 ;;
3 ;; Copyright (C) 2005-2017 Free Software Foundation, Inc.
4 ;;
5 ;; Version: 0.4.11
6 ;; Keywords: multimedia
7 ;; Author: Mathias Dahl <mathias.rem0veth1s.dahl@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; BACKGROUND
27 ;; ==========
29 ;; I needed a program to browse, organize and tag my pictures. I got
30 ;; tired of the old gallery program I used as it did not allow
31 ;; multi-file operations easily. Also, it put things out of my
32 ;; control. Image viewing programs I tested did not allow multi-file
33 ;; operations or did not do what I wanted it to.
35 ;; So, I got the idea to use the wonderful functionality of Emacs and
36 ;; `dired' to do it. It would allow me to do almost anything I wanted,
37 ;; which is basically just to browse all my pictures in an easy way,
38 ;; letting me manipulate and tag them in various ways. `dired' already
39 ;; provide all the file handling and navigation facilities; I only
40 ;; needed to add some functions to display the images.
42 ;; I briefly tried out thumbs.el, and although it seemed more
43 ;; powerful than this package, it did not work the way I wanted to. It
44 ;; was too slow to created thumbnails of all files in a directory (I
45 ;; currently keep all my 2000+ images in the same directory) and
46 ;; browsing the thumbnail buffer was slow too. image-dired.el will not
47 ;; create thumbnails until they are needed and the browsing is done
48 ;; quickly and easily in dired. I copied a great deal of ideas and
49 ;; code from there though... :)
51 ;; `image-dired' stores the thumbnail files in `image-dired-dir'
52 ;; using the file name format ORIGNAME.thumb.ORIGEXT. For example
53 ;; ~/.emacs.d/image-dired/myimage01.thumb.jpg. The "database" is for
54 ;; now just a plain text file with the following format:
56 ;; file-name-non-directory;comment:comment-text;tag1;tag2;tag3;...;tagN
59 ;; PREREQUISITES
60 ;; =============
62 ;; * The ImageMagick package. Currently, `convert' and `mogrify' are
63 ;; used. Find it here: http://www.imagemagick.org.
65 ;; * For non-lossy rotation of JPEG images, the JpegTRAN program is
66 ;; needed.
68 ;; * For `image-dired-get-exif-data' and `image-dired-write-exif-data' to work,
69 ;; the command line tool `exiftool' is needed. It can be found here:
70 ;; http://www.sno.phy.queensu.ca/~phil/exiftool/. These two functions
71 ;; are, among other things, used for writing comments to image files
72 ;; using `image-dired-thumbnail-set-image-description' and to create
73 ;; "unique" file names using `image-dired-get-exif-file-name' (used by
74 ;; `image-dired-copy-with-exif-file-name').
77 ;; USAGE
78 ;; =====
80 ;; This information has been moved to the manual. Type `C-h r' to open
81 ;; the Emacs manual and go to the node Thumbnails by typing `g
82 ;; 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 :type '(choice (const :tag "Not Set" nil) file)
309 :group 'image-dired)
311 (defcustom image-dired-cmd-optipng-options '("-o5" "%t")
312 "Arguments passed to `image-dired-optipng-program'.
313 Available format specifiers are described in
314 `image-dired-cmd-create-thumbnail-options'."
315 :type '(repeat (string :tag "Argument"))
316 :link '(url-link "man:optipng(1)")
317 :group 'image-dired)
319 (defcustom image-dired-cmd-create-standard-thumbnail-options
320 (append '("-size" "%wx%h" "%f[0]")
321 (unless (or image-dired-cmd-pngcrush-program
322 image-dired-cmd-pngnq-program)
323 (list
324 "-set" "Thumb::MTime" "%m"
325 "-set" "Thumb::URI" "file://%f"
326 "-set" "Description" "Thumbnail of file://%f"
327 "-set" "Software" (emacs-version)))
328 '("-thumbnail" "%wx%h>" "png:%t"))
329 "Options for creating thumbnails according to the Thumbnail Managing Standard.
330 Available format specifiers are the same as in
331 `image-dired-cmd-create-thumbnail-options', with %m for file modification time."
332 :version "26.1"
333 :type '(repeat (string :tag "Argument"))
334 :group 'image-dired)
336 (defcustom image-dired-cmd-rotate-thumbnail-program
337 "mogrify"
338 "Executable used to rotate thumbnail.
339 Used together with `image-dired-cmd-rotate-thumbnail-options'."
340 :type 'file
341 :group 'image-dired)
343 (defcustom image-dired-cmd-rotate-thumbnail-options
344 '("-rotate" "%d" "%t")
345 "Arguments of command used to rotate thumbnail image.
346 Used with `image-dired-cmd-rotate-thumbnail-program'.
347 Available format specifiers are: %d which is replaced by the
348 number of (positive) degrees to rotate the image, normally 90 or 270
349 \(for 90 degrees right and left), %t which is replaced by the file name
350 of the thumbnail file."
351 :version "26.1"
352 :type '(repeat (string :tag "Argument"))
353 :group 'image-dired)
355 (defcustom image-dired-cmd-rotate-original-program
356 "jpegtran"
357 "Executable used to rotate original image.
358 Used together with `image-dired-cmd-rotate-original-options'."
359 :type 'file
360 :group 'image-dired)
362 (defcustom image-dired-cmd-rotate-original-options
363 '("-rotate" "%d" "-copy" "all" "-outfile" "%t" "%o")
364 "Arguments of command used to rotate original image.
365 Used with `image-dired-cmd-rotate-original-program'.
366 Available format specifiers are: %d which is replaced by the
367 number of (positive) degrees to rotate the image, normally 90 or
368 270 \(for 90 degrees right and left), %o which is replaced by the
369 original image file name and %t which is replaced by
370 `image-dired-temp-image-file'."
371 :version "26.1"
372 :type '(repeat (string :tag "Argument"))
373 :group 'image-dired)
375 (defcustom image-dired-temp-rotate-image-file
376 (expand-file-name ".image-dired_rotate_temp" image-dired-dir)
377 "Temporary file for rotate operations."
378 :type 'file
379 :group 'image-dired)
381 (defcustom image-dired-rotate-original-ask-before-overwrite t
382 "Confirm overwrite of original file after rotate operation.
383 If non-nil, ask user for confirmation before overwriting the
384 original file with `image-dired-temp-rotate-image-file'."
385 :type 'boolean
386 :group 'image-dired)
388 (defcustom image-dired-cmd-write-exif-data-program
389 "exiftool"
390 "Program used to write EXIF data to image.
391 Used together with `image-dired-cmd-write-exif-data-options'."
392 :type 'file
393 :group 'image-dired)
395 (defcustom image-dired-cmd-write-exif-data-options
396 '("-%t=%v" "%f")
397 "Arguments of command used to write EXIF data.
398 Used with `image-dired-cmd-write-exif-data-program'.
399 Available format specifiers are: %f which is replaced by
400 the image file name, %t which is replaced by the tag name and %v
401 which is replaced by the tag value."
402 :version "26.1"
403 :type '(repeat (string :tag "Argument"))
404 :group 'image-dired)
406 (defcustom image-dired-cmd-read-exif-data-program
407 "exiftool"
408 "Program used to read EXIF data to image.
409 Used together with `image-dired-cmd-read-exif-data-options'."
410 :type 'file
411 :group 'image-dired)
413 (defcustom image-dired-cmd-read-exif-data-options
414 '("-s" "-s" "-s" "-%t" "%f")
415 "Arguments of command used to read EXIF data.
416 Used with `image-dired-cmd-read-exif-data-program'.
417 Available format specifiers are: %f which is replaced
418 by the image file name and %t which is replaced by the tag name."
419 :version "26.1"
420 :type '(repeat (string :tag "Argument"))
421 :group 'image-dired)
423 (defcustom image-dired-gallery-hidden-tags
424 (list "private" "hidden" "pending")
425 "List of \"hidden\" tags.
426 Used by `image-dired-gallery-generate' to leave out \"hidden\" images."
427 :type '(repeat string)
428 :group 'image-dired)
430 (defcustom image-dired-thumb-size
431 (cond
432 ((eq 'standard image-dired-thumbnail-storage) 128)
433 ((eq 'standard-large image-dired-thumbnail-storage) 256)
434 (t 100))
435 "Size of thumbnails, in pixels.
436 This is the default size for both `image-dired-thumb-width'
437 and `image-dired-thumb-height'."
438 :type 'integer
439 :group 'image-dired)
441 (defcustom image-dired-thumb-width image-dired-thumb-size
442 "Width of thumbnails, in pixels."
443 :type 'integer
444 :group 'image-dired)
446 (defcustom image-dired-thumb-height image-dired-thumb-size
447 "Height of thumbnails, in pixels."
448 :type 'integer
449 :group 'image-dired)
451 (defcustom image-dired-thumb-relief 2
452 "Size of button-like border around thumbnails."
453 :type 'integer
454 :group 'image-dired)
456 (defcustom image-dired-thumb-margin 2
457 "Size of the margin around thumbnails.
458 This is where you see the cursor."
459 :type 'integer
460 :group 'image-dired)
462 (defcustom image-dired-line-up-method 'dynamic
463 "Default method for line-up of thumbnails in thumbnail buffer.
464 Used by `image-dired-display-thumbs' and other functions that needs
465 to line-up thumbnails. Dynamic means to use the available width of
466 the window containing the thumbnail buffer, Fixed means to use
467 `image-dired-thumbs-per-row', Interactive is for asking the user,
468 and No line-up means that no automatic line-up will be done."
469 :type '(choice :tag "Default line-up method"
470 (const :tag "Dynamic" dynamic)
471 (const :tag "Fixed" fixed)
472 (const :tag "Interactive" interactive)
473 (const :tag "No line-up" none))
474 :group 'image-dired)
476 (defcustom image-dired-thumbs-per-row 3
477 "Number of thumbnails to display per row in thumb buffer."
478 :type 'integer
479 :group 'image-dired)
481 (defcustom image-dired-display-window-width-correction 1
482 "Number to be used to correct image display window width.
483 Change if the default (1) does not work (i.e. if the image does not
484 completely fit)."
485 :type 'integer
486 :group 'image-dired)
488 (defcustom image-dired-display-window-height-correction 0
489 "Number to be used to correct image display window height.
490 Change if the default (0) does not work (i.e. if the image does not
491 completely fit)."
492 :type 'integer
493 :group 'image-dired)
495 (defcustom image-dired-track-movement t
496 "The current state of the tracking and mirroring.
497 For more information, see the documentation for
498 `image-dired-toggle-movement-tracking'."
499 :type 'boolean
500 :group 'image-dired)
502 (defcustom image-dired-append-when-browsing nil
503 "Append thumbnails in thumbnail buffer when browsing.
504 If non-nil, using `image-dired-next-line-and-display' and
505 `image-dired-previous-line-and-display' will leave a trail of thumbnail
506 images in the thumbnail buffer. If you enable this and want to clean
507 the thumbnail buffer because it is filled with too many thumbnails,
508 just call `image-dired-display-thumb' to display only the image at point.
509 This value can be toggled using `image-dired-toggle-append-browsing'."
510 :type 'boolean
511 :group 'image-dired)
513 (defcustom image-dired-dired-disp-props t
514 "If non-nil, display properties for dired file when browsing.
515 Used by `image-dired-next-line-and-display',
516 `image-dired-previous-line-and-display' and `image-dired-mark-and-display-next'.
517 If the database file is large, this can slow down image browsing in
518 dired and you might want to turn it off."
519 :type 'boolean
520 :group 'image-dired)
522 (defcustom image-dired-display-properties-format "%b: %f (%t): %c"
523 "Display format for thumbnail properties.
524 %b is replaced with associated dired buffer name, %f with file name
525 \(without path) of original image file, %t with the list of tags and %c
526 with the comment."
527 :type 'string
528 :group 'image-dired)
530 (defcustom image-dired-external-viewer
531 ;; TODO: Use mailcap, dired-guess-shell-alist-default,
532 ;; dired-view-command-alist.
533 (cond ((executable-find "display"))
534 ((executable-find "xli"))
535 ((executable-find "qiv") "qiv -t"))
536 "Name of external viewer.
537 Including parameters. Used when displaying original image from
538 `image-dired-thumbnail-mode'."
539 :type 'string
540 :group 'image-dired)
542 (defcustom image-dired-main-image-directory "~/pics/"
543 "Name of main image directory, if any.
544 Used by `image-dired-copy-with-exif-file-name'."
545 :type 'string
546 :group 'image-dired)
548 (defcustom image-dired-show-all-from-dir-max-files 50
549 "Maximum number of files to show using `image-dired-show-all-from-dir'
550 before warning the user."
551 :type 'integer
552 :group 'image-dired)
554 (defmacro image-dired--with-db-file (&rest body)
555 "Run BODY in a temp buffer containing `image-dired-db-file'.
556 Return the last form in BODY."
557 (declare (indent 0) (debug t))
558 `(with-temp-buffer
559 (if (file-exists-p image-dired-db-file)
560 (insert-file-contents image-dired-db-file))
561 ,@body))
563 (defun image-dired-dir ()
564 "Return the current thumbnails directory (from variable `image-dired-dir').
565 Create the thumbnails directory if it does not exist."
566 (let ((image-dired-dir (file-name-as-directory
567 (expand-file-name image-dired-dir))))
568 (unless (file-directory-p image-dired-dir)
569 (make-directory image-dired-dir t)
570 (message "Creating thumbnails directory"))
571 image-dired-dir))
573 (defun image-dired-insert-image (file type relief margin)
574 "Insert image FILE of image TYPE, using RELIEF and MARGIN, at point."
575 (let ((i `(image :type ,type
576 :file ,file
577 :relief ,relief
578 :margin ,margin)))
579 (insert-image i)))
581 (defun image-dired-get-thumbnail-image (file)
582 "Return the image descriptor for a thumbnail of image file FILE."
583 (unless (string-match (image-file-name-regexp) file)
584 (error "%s is not a valid image file" file))
585 (let ((thumb-file (image-dired-thumb-name file)))
586 (unless (and (file-exists-p thumb-file)
587 (<= (float-time (nth 5 (file-attributes file)))
588 (float-time (nth 5 (file-attributes thumb-file)))))
589 (image-dired-create-thumb file thumb-file))
590 (create-image thumb-file)
591 ;; (list 'image :type 'jpeg
592 ;; :file thumb-file
593 ;; :relief image-dired-thumb-relief :margin image-dired-thumb-margin)
596 (defun image-dired-insert-thumbnail (file original-file-name
597 associated-dired-buffer)
598 "Insert thumbnail image FILE.
599 Add text properties ORIGINAL-FILE-NAME and ASSOCIATED-DIRED-BUFFER."
600 (let (beg end)
601 (setq beg (point))
602 (image-dired-insert-image file
603 ;; TODO: this should depend on the real file type
604 (if (memq image-dired-thumbnail-storage
605 '(standard standard-large))
606 'png 'jpeg)
607 image-dired-thumb-relief
608 image-dired-thumb-margin)
609 (setq end (point))
610 (add-text-properties
611 beg end
612 (list 'image-dired-thumbnail t
613 'original-file-name original-file-name
614 'associated-dired-buffer associated-dired-buffer
615 'tags (image-dired-list-tags original-file-name)
616 'mouse-face 'highlight
617 'comment (image-dired-get-comment original-file-name)))))
619 (defun image-dired-thumb-name (file)
620 "Return thumbnail file name for FILE.
621 Depending on the value of `image-dired-thumbnail-storage', the file
622 name will vary. For central thumbnail file storage, make a
623 MD5-hash of the image file's directory name and add that to make
624 the thumbnail file name unique. For per-directory storage, just
625 add a subdirectory. For standard storage, produce the file name
626 according to the Thumbnail Managing Standard."
627 (cond ((memq image-dired-thumbnail-storage '(standard standard-large))
628 (let* ((xdg (getenv "XDG_CACHE_HOME"))
629 (dir (if (and xdg (file-name-absolute-p xdg))
630 xdg "~/.cache"))
631 (thumbdir (cl-case image-dired-thumbnail-storage
632 (standard "thumbnails/normal")
633 (standard-large "thumbnails/large"))))
634 (expand-file-name
635 (concat (md5 (concat "file://" (expand-file-name file))) ".png")
636 (expand-file-name thumbdir dir))))
637 ((eq 'use-image-dired-dir image-dired-thumbnail-storage)
638 (let* ((f (expand-file-name file))
639 (md5-hash
640 ;; Is MD5 hashes fast enough? The checksum of a
641 ;; thumbnail file name need not be that
642 ;; "cryptographically" good so a faster one could
643 ;; be used here.
644 (md5 (file-name-as-directory (file-name-directory f)))))
645 (format "%s%s%s.thumb.%s"
646 (file-name-as-directory (expand-file-name (image-dired-dir)))
647 (file-name-base f)
648 (if md5-hash (concat "_" md5-hash) "")
649 (file-name-extension f))))
650 ((eq 'per-directory image-dired-thumbnail-storage)
651 (let ((f (expand-file-name file)))
652 (format "%s.image-dired/%s.thumb.%s"
653 (file-name-directory f)
654 (file-name-base f)
655 (file-name-extension f))))))
657 (defun image-dired--check-executable-exists (executable)
658 (unless (executable-find (symbol-value executable))
659 (error "Executable %S not found" executable)))
661 (defun image-dired-thumb-size (dimension)
662 "Return thumb size depending on `image-dired-thumbnail-storage'.
663 DIMENSION should be either the symbol 'width or 'height."
664 (cond
665 ((eq 'standard image-dired-thumbnail-storage) 128)
666 ((eq 'standard-large image-dired-thumbnail-storage) 256)
667 (t (cl-ecase dimension
668 (width image-dired-thumb-width)
669 (height image-dired-thumb-height)))))
671 (defvar image-dired-queue nil
672 "List of items in the queue.
673 Each item has the form (ORIGINAL-FILE TARGET-FILE).")
675 (defvar image-dired-queue-active-jobs 0
676 "Number of active jobs in `image-dired-queue'.")
678 (defvar image-dired-queue-active-limit 2
679 "Maximum number of concurrent jobs permitted for generating images.
680 Increase at own risk.")
682 (defun image-dired-pngnq-thumb (spec)
683 "Quantize thumbnail described by format SPEC with pngnq(1)."
684 (let ((process
685 (apply #'start-process "image-dired-pngnq" nil
686 image-dired-cmd-pngnq-program
687 (mapcar (lambda (arg) (format-spec arg spec))
688 image-dired-cmd-pngnq-options))))
689 (setf (process-sentinel process)
690 (lambda (process status)
691 (if (and (eq (process-status process) 'exit)
692 (zerop (process-exit-status process)))
693 ;; Pass off to pngcrush, or just rename the
694 ;; THUMB-nq8.png file back to THUMB.png
695 (if (and image-dired-cmd-pngcrush-program
696 (executable-find image-dired-cmd-pngcrush-program))
697 (image-dired-pngcrush-thumb spec)
698 (let ((nq8 (cdr (assq ?q spec)))
699 (thumb (cdr (assq ?t spec))))
700 (rename-file nq8 thumb t)))
701 (message "command %S %s" (process-command process)
702 (replace-regexp-in-string "\n" "" status)))))
703 process))
705 (defun image-dired-pngcrush-thumb (spec)
706 "Optimize thumbnail described by format SPEC with pngcrush(1)."
707 ;; If pngnq wasn't run, then the THUMB-nq8.png file does not exist.
708 ;; pngcrush needs an infile and outfile, so we just copy THUMB to
709 ;; THUMB-nq8.png and use the latter as a temp file.
710 (when (not image-dired-cmd-pngnq-program)
711 (let ((temp (cdr (assq ?q spec)))
712 (thumb (cdr (assq ?t spec))))
713 (copy-file thumb temp)))
714 (let ((process
715 (apply #'start-process "image-dired-pngcrush" nil
716 image-dired-cmd-pngcrush-program
717 (mapcar (lambda (arg) (format-spec arg spec))
718 image-dired-cmd-pngcrush-options))))
719 (setf (process-sentinel process)
720 (lambda (process status)
721 (unless (and (eq (process-status process) 'exit)
722 (zerop (process-exit-status process)))
723 (message "command %S %s" (process-command process)
724 (replace-regexp-in-string "\n" "" status)))
725 (when (memq (process-status process) '(exit signal))
726 (let ((temp (cdr (assq ?q spec))))
727 (delete-file temp)))))
728 process))
730 (defun image-dired-optipng-thumb (spec)
731 "Optimize thumbnail described by format SPEC with optipng(1)."
732 (let ((process
733 (apply #'start-process "image-dired-optipng" nil
734 image-dired-cmd-optipng-program
735 (mapcar (lambda (arg) (format-spec arg spec))
736 image-dired-cmd-optipng-options))))
737 (setf (process-sentinel process)
738 (lambda (process status)
739 (unless (and (eq (process-status process) 'exit)
740 (zerop (process-exit-status process)))
741 (message "command %S %s" (process-command process)
742 (replace-regexp-in-string "\n" "" status)))))
743 process))
745 (defun image-dired-create-thumb-1 (original-file thumbnail-file)
746 "For ORIGINAL-FILE, create thumbnail image named THUMBNAIL-FILE."
747 (image-dired--check-executable-exists
748 'image-dired-cmd-create-thumbnail-program)
749 (let* ((width (int-to-string (image-dired-thumb-size 'width)))
750 (height (int-to-string (image-dired-thumb-size 'height)))
751 (modif-time (floor (float-time (nth 5 (file-attributes original-file)))))
752 (thumbnail-nq8-file (replace-regexp-in-string ".png\\'" "-nq8.png"
753 thumbnail-file))
754 (spec
755 (list
756 (cons ?w width)
757 (cons ?h height)
758 (cons ?m modif-time)
759 (cons ?f original-file)
760 (cons ?q thumbnail-nq8-file)
761 (cons ?t thumbnail-file)))
762 (thumbnail-dir (file-name-directory thumbnail-file))
763 process)
764 (when (not (file-exists-p thumbnail-dir))
765 (message "Creating thumbnail directory")
766 (make-directory thumbnail-dir t)
767 (set-file-modes thumbnail-dir #o700))
769 ;; Thumbnail file creation processes begin here and are marshaled
770 ;; in a queue by `image-dired-create-thumb'.
771 (setq process
772 (apply #'start-process "image-dired-create-thumbnail" nil
773 image-dired-cmd-create-thumbnail-program
774 (mapcar
775 (lambda (arg) (format-spec arg spec))
776 (if (memq image-dired-thumbnail-storage
777 '(standard standard-large))
778 image-dired-cmd-create-standard-thumbnail-options
779 image-dired-cmd-create-thumbnail-options))))
781 (setf (process-sentinel process)
782 (lambda (process status)
783 ;; Trigger next in queue once a thumbnail has been created
784 (cl-decf image-dired-queue-active-jobs)
785 (image-dired-thumb-queue-run)
786 (if (not (and (eq (process-status process) 'exit)
787 (zerop (process-exit-status process))))
788 (message "Thumb could not be created for %s: %s"
789 (abbreviate-file-name original-file)
790 (replace-regexp-in-string "\n" "" status))
791 (set-file-modes thumbnail-file #o600)
792 (clear-image-cache thumbnail-file)
793 ;; PNG thumbnail has been created since we are
794 ;; following the XDG thumbnail spec, so try to optimize
795 (when (memq image-dired-thumbnail-storage
796 '(standard standard-large))
797 (cond
798 ((and image-dired-cmd-pngnq-program
799 (executable-find image-dired-cmd-pngnq-program))
800 (image-dired-pngnq-thumb spec))
801 ((and image-dired-cmd-pngcrush-program
802 (executable-find image-dired-cmd-pngcrush-program))
803 (image-dired-pngcrush-thumb spec))
804 ((and image-dired-cmd-optipng-program
805 (executable-find image-dired-cmd-optipng-program))
806 (image-dired-optipng-thumb spec)))))))
807 process))
809 (defun image-dired-thumb-queue-run ()
810 "Run a queued job if one exists and not too many jobs are running.
811 Queued items live in `image-dired-queue'."
812 (while (and image-dired-queue
813 (< image-dired-queue-active-jobs
814 image-dired-queue-active-limit))
815 (cl-incf image-dired-queue-active-jobs)
816 (apply #'image-dired-create-thumb-1 (pop image-dired-queue))))
818 (defun image-dired-create-thumb (original-file thumbnail-file)
819 "Add a job for generating thumbnail to `image-dired-queue'."
820 (setq image-dired-queue
821 (nconc image-dired-queue
822 (list (list original-file thumbnail-file))))
823 (run-at-time 0 nil #'image-dired-thumb-queue-run))
825 ;;;###autoload
826 (defun image-dired-dired-toggle-marked-thumbs (&optional arg)
827 "Toggle thumbnails in front of file names in the dired buffer.
828 If no marked file could be found, insert or hide thumbnails on the
829 current line. ARG, if non-nil, specifies the files to use instead
830 of the marked files. If ARG is an integer, use the next ARG (or
831 previous -ARG, if ARG<0) files."
832 (interactive "P")
833 (dired-map-over-marks
834 (let ((image-pos (dired-move-to-filename))
835 (image-file (dired-get-filename nil t))
836 thumb-file
837 overlay)
838 (when (and image-file
839 (string-match-p (image-file-name-regexp) image-file))
840 (setq thumb-file (image-dired-get-thumbnail-image image-file))
841 ;; If image is not already added, then add it.
842 (let ((thumb-ov (cl-loop for ov in (overlays-in (point) (1+ (point)))
843 if (overlay-get ov 'thumb-file) return ov)))
844 (if thumb-ov
845 (delete-overlay thumb-ov)
846 (put-image thumb-file image-pos)
847 (setq overlay
848 (cl-loop for ov in (overlays-in (point) (1+ (point)))
849 if (overlay-get ov 'put-image) return ov))
850 (overlay-put overlay 'image-file image-file)
851 (overlay-put overlay 'thumb-file thumb-file)))))
852 arg ; Show or hide image on ARG next files.
853 'show-progress) ; Update dired display after each image is updated.
854 (add-hook 'dired-after-readin-hook
855 'image-dired-dired-after-readin-hook nil t))
857 (defun image-dired-dired-after-readin-hook ()
858 "Relocate existing thumbnail overlays in dired buffer after reverting.
859 Move them to their corresponding files if they still exist.
860 Otherwise, delete overlays."
861 (mapc (lambda (overlay)
862 (when (overlay-get overlay 'put-image)
863 (let* ((image-file (overlay-get overlay 'image-file))
864 (image-pos (dired-goto-file image-file)))
865 (if image-pos
866 (move-overlay overlay image-pos image-pos)
867 (delete-overlay overlay)))))
868 (overlays-in (point-min) (point-max))))
870 (defun image-dired-next-line-and-display ()
871 "Move to next dired line and display thumbnail image."
872 (interactive)
873 (dired-next-line 1)
874 (image-dired-display-thumbs
875 t (or image-dired-append-when-browsing nil) t)
876 (if image-dired-dired-disp-props
877 (image-dired-dired-display-properties)))
879 (defun image-dired-previous-line-and-display ()
880 "Move to previous dired line and display thumbnail image."
881 (interactive)
882 (dired-previous-line 1)
883 (image-dired-display-thumbs
884 t (or image-dired-append-when-browsing nil) t)
885 (if image-dired-dired-disp-props
886 (image-dired-dired-display-properties)))
888 (defun image-dired-toggle-append-browsing ()
889 "Toggle `image-dired-append-when-browsing'."
890 (interactive)
891 (setq image-dired-append-when-browsing
892 (not image-dired-append-when-browsing))
893 (message "Append browsing %s."
894 (if image-dired-append-when-browsing
895 "on"
896 "off")))
898 (defun image-dired-mark-and-display-next ()
899 "Mark current file in dired and display next thumbnail image."
900 (interactive)
901 (dired-mark 1)
902 (image-dired-display-thumbs
903 t (or image-dired-append-when-browsing nil) t)
904 (if image-dired-dired-disp-props
905 (image-dired-dired-display-properties)))
907 (defun image-dired-toggle-dired-display-properties ()
908 "Toggle `image-dired-dired-disp-props'."
909 (interactive)
910 (setq image-dired-dired-disp-props
911 (not image-dired-dired-disp-props))
912 (message "Dired display properties %s."
913 (if image-dired-dired-disp-props
914 "on"
915 "off")))
917 (defvar image-dired-thumbnail-buffer "*image-dired*"
918 "Image-Dired's thumbnail buffer.")
920 (defun image-dired-create-thumbnail-buffer ()
921 "Create thumb buffer and set `image-dired-thumbnail-mode'."
922 (let ((buf (get-buffer-create image-dired-thumbnail-buffer)))
923 (with-current-buffer buf
924 (setq buffer-read-only t)
925 (if (not (eq major-mode 'image-dired-thumbnail-mode))
926 (image-dired-thumbnail-mode)))
927 buf))
929 (defvar image-dired-display-image-buffer "*image-dired-display-image*"
930 "Where larger versions of the images are display.")
932 (defun image-dired-create-display-image-buffer ()
933 "Create image display buffer and set `image-dired-display-image-mode'."
934 (let ((buf (get-buffer-create image-dired-display-image-buffer)))
935 (with-current-buffer buf
936 (setq buffer-read-only t)
937 (if (not (eq major-mode 'image-dired-display-image-mode))
938 (image-dired-display-image-mode)))
939 buf))
941 (defvar image-dired-saved-window-configuration nil
942 "Saved window configuration.")
944 ;;;###autoload
945 (defun image-dired-dired-with-window-configuration (dir &optional arg)
946 "Open directory DIR and create a default window configuration.
948 Convenience command that:
950 - Opens dired in folder DIR
951 - Splits windows in most useful (?) way
952 - Set `truncate-lines' to t
954 After the command has finished, you would typically mark some
955 image files in dired and type
956 \\[image-dired-display-thumbs] (`image-dired-display-thumbs').
958 If called with prefix argument ARG, skip splitting of windows.
960 The current window configuration is saved and can be restored by
961 calling `image-dired-restore-window-configuration'."
962 (interactive "DDirectory: \nP")
963 (let ((buf (image-dired-create-thumbnail-buffer))
964 (buf2 (image-dired-create-display-image-buffer)))
965 (setq image-dired-saved-window-configuration
966 (current-window-configuration))
967 (dired dir)
968 (delete-other-windows)
969 (when (not arg)
970 (split-window-right)
971 (setq truncate-lines t)
972 (save-excursion
973 (other-window 1)
974 (pop-to-buffer-same-window buf)
975 (select-window (split-window-below))
976 (pop-to-buffer-same-window buf2)
977 (other-window -2)))))
979 (defun image-dired-restore-window-configuration ()
980 "Restore window configuration.
981 Restore any changes to the window configuration made by calling
982 `image-dired-dired-with-window-configuration'."
983 (interactive)
984 (if image-dired-saved-window-configuration
985 (set-window-configuration image-dired-saved-window-configuration)
986 (message "No saved window configuration")))
988 ;;;###autoload
989 (defun image-dired-display-thumbs (&optional arg append do-not-pop)
990 "Display thumbnails of all marked files, in `image-dired-thumbnail-buffer'.
991 If a thumbnail image does not exist for a file, it is created on the
992 fly. With prefix argument ARG, display only thumbnail for file at
993 point (this is useful if you have marked some files but want to show
994 another one).
996 Recommended usage is to split the current frame horizontally so that
997 you have the dired buffer in the left window and the
998 `image-dired-thumbnail-buffer' buffer in the right window.
1000 With optional argument APPEND, append thumbnail to thumbnail buffer
1001 instead of erasing it first.
1003 Optional argument DO-NOT-POP controls if `pop-to-buffer' should be
1004 used or not. If non-nil, use `display-buffer' instead of
1005 `pop-to-buffer'. This is used from functions like
1006 `image-dired-next-line-and-display' and
1007 `image-dired-previous-line-and-display' where we do not want the
1008 thumbnail buffer to be selected."
1009 (interactive "P")
1010 (let ((buf (image-dired-create-thumbnail-buffer))
1011 thumb-name files dired-buf)
1012 (if arg
1013 (setq files (list (dired-get-filename)))
1014 (setq files (dired-get-marked-files)))
1015 (setq dired-buf (current-buffer))
1016 (with-current-buffer buf
1017 (let ((inhibit-read-only t))
1018 (if (not append)
1019 (erase-buffer)
1020 (goto-char (point-max)))
1021 (dolist (curr-file files)
1022 (setq thumb-name (image-dired-thumb-name curr-file))
1023 (when (not (file-exists-p thumb-name))
1024 (image-dired-create-thumb curr-file thumb-name))
1025 (image-dired-insert-thumbnail thumb-name curr-file dired-buf)))
1026 (if do-not-pop
1027 (display-buffer buf)
1028 (pop-to-buffer buf))
1029 (cond ((eq 'dynamic image-dired-line-up-method)
1030 (image-dired-line-up-dynamic))
1031 ((eq 'fixed image-dired-line-up-method)
1032 (image-dired-line-up))
1033 ((eq 'interactive image-dired-line-up-method)
1034 (image-dired-line-up-interactive))
1035 ((eq 'none image-dired-line-up-method)
1036 nil)
1038 (image-dired-line-up-dynamic))))))
1040 ;;;###autoload
1041 (defun image-dired-show-all-from-dir (dir)
1042 "Make a preview buffer for all images in DIR and display it.
1043 If the number of files in DIR matching `image-file-name-regexp'
1044 exceeds `image-dired-show-all-from-dir-max-files', a warning will be
1045 displayed."
1046 (interactive "DImage Dired: ")
1047 (dired dir)
1048 (dired-mark-files-regexp (image-file-name-regexp))
1049 (let ((files (dired-get-marked-files)))
1050 (if (or (<= (length files) image-dired-show-all-from-dir-max-files)
1051 (and (> (length files) image-dired-show-all-from-dir-max-files)
1052 (y-or-n-p
1053 (format
1054 "Directory contains more than %d image files. Proceed? "
1055 image-dired-show-all-from-dir-max-files))))
1056 (progn
1057 (image-dired-display-thumbs)
1058 (pop-to-buffer image-dired-thumbnail-buffer))
1059 (message "Canceled."))))
1061 ;;;###autoload
1062 (defalias 'image-dired 'image-dired-show-all-from-dir)
1064 ;;;###autoload
1065 (define-obsolete-function-alias 'tumme 'image-dired "24.4")
1067 (defun image-dired-sane-db-file ()
1068 "Check if `image-dired-db-file' exists.
1069 If not, try to create it (including any parent directories).
1070 Signal error if there are problems creating it."
1071 (or (file-exists-p image-dired-db-file)
1072 (let (dir buf)
1073 (unless (file-directory-p (setq dir (file-name-directory
1074 image-dired-db-file)))
1075 (make-directory dir t))
1076 (with-current-buffer (setq buf (create-file-buffer
1077 image-dired-db-file))
1078 (write-file image-dired-db-file))
1079 (kill-buffer buf)
1080 (file-exists-p image-dired-db-file))
1081 (error "Could not create %s" image-dired-db-file)))
1083 (defun image-dired-write-tags (file-tags)
1084 "Write file tags to database.
1085 Write each file and tag in FILE-TAGS to the database.
1086 FILE-TAGS is an alist in the following form:
1087 ((FILE . TAG) ... )"
1088 (image-dired-sane-db-file)
1089 (let (end file tag)
1090 (image-dired--with-db-file
1091 (setq buffer-file-name image-dired-db-file)
1092 (dolist (elt file-tags)
1093 (setq file (car elt)
1094 tag (cdr elt))
1095 (goto-char (point-min))
1096 (if (search-forward-regexp (format "^%s.*$" file) nil t)
1097 (progn
1098 (setq end (point))
1099 (beginning-of-line)
1100 (when (not (search-forward (format ";%s" tag) end t))
1101 (end-of-line)
1102 (insert (format ";%s" tag))))
1103 (goto-char (point-max))
1104 (insert (format "\n%s;%s" file tag))))
1105 (save-buffer))))
1107 (defun image-dired-remove-tag (files tag)
1108 "For all FILES, remove TAG from the image database."
1109 (image-dired-sane-db-file)
1110 (image-dired--with-db-file
1111 (setq buffer-file-name image-dired-db-file)
1112 (let (end)
1113 (unless (listp files)
1114 (if (stringp files)
1115 (setq files (list files))
1116 (error "Files must be a string or a list of strings!")))
1117 (dolist (file files)
1118 (goto-char (point-min))
1119 (when (search-forward-regexp (format "^%s" file) nil t)
1120 (end-of-line)
1121 (setq end (point))
1122 (beginning-of-line)
1123 (when (search-forward-regexp (format "\\(;%s\\)" tag) end t)
1124 (delete-region (match-beginning 1) (match-end 1))
1125 ;; Check if file should still be in the database. If
1126 ;; it has no tags or comments, it will be removed.
1127 (end-of-line)
1128 (setq end (point))
1129 (beginning-of-line)
1130 (when (not (search-forward ";" end t))
1131 (kill-line 1)
1132 ;; If on empty line at end of buffer
1133 (and (eobp)
1134 (looking-at "^$")
1135 (delete-char -1)))))))
1136 (save-buffer)))
1138 (defun image-dired-list-tags (file)
1139 "Read all tags for image FILE from the image database."
1140 (image-dired-sane-db-file)
1141 (image-dired--with-db-file
1142 (let (end (tags ""))
1143 (when (search-forward-regexp (format "^%s" file) nil t)
1144 (end-of-line)
1145 (setq end (point))
1146 (beginning-of-line)
1147 (if (search-forward ";" end t)
1148 (if (search-forward "comment:" end t)
1149 (if (search-forward ";" end t)
1150 (setq tags (buffer-substring (point) end)))
1151 (setq tags (buffer-substring (point) end)))))
1152 (split-string tags ";"))))
1154 ;;;###autoload
1155 (defun image-dired-tag-files (arg)
1156 "Tag marked file(s) in dired. With prefix ARG, tag file at point."
1157 (interactive "P")
1158 (let ((tag (read-string "Tags to add (separate tags with a semicolon): "))
1159 files)
1160 (if arg
1161 (setq files (list (dired-get-filename)))
1162 (setq files (dired-get-marked-files)))
1163 (image-dired-write-tags
1164 (mapcar
1165 (lambda (x)
1166 (cons x tag))
1167 files))))
1169 (defun image-dired-tag-thumbnail ()
1170 "Tag current thumbnail."
1171 (interactive)
1172 (let ((tag (read-string "Tags to add (separate tags with a semicolon): ")))
1173 (image-dired-write-tags (list (cons (image-dired-original-file-name) tag))))
1174 (image-dired-update-property
1175 'tags (image-dired-list-tags (image-dired-original-file-name))))
1177 ;;;###autoload
1178 (defun image-dired-delete-tag (arg)
1179 "Remove tag for selected file(s).
1180 With prefix argument ARG, remove tag from file at point."
1181 (interactive "P")
1182 (let ((tag (read-string "Tag to remove: "))
1183 files)
1184 (if arg
1185 (setq files (list (dired-get-filename)))
1186 (setq files (dired-get-marked-files)))
1187 (image-dired-remove-tag files tag)))
1189 (defun image-dired-tag-thumbnail-remove ()
1190 "Remove tag from thumbnail."
1191 (interactive)
1192 (let ((tag (read-string "Tag to remove: ")))
1193 (image-dired-remove-tag (image-dired-original-file-name) tag))
1194 (image-dired-update-property
1195 'tags (image-dired-list-tags (image-dired-original-file-name))))
1197 (defun image-dired-original-file-name ()
1198 "Get original file name for thumbnail or display image at point."
1199 (get-text-property (point) 'original-file-name))
1201 (defun image-dired-file-name-at-point ()
1202 "Get abbreviated file name for thumbnail or display image at point."
1203 (let ((f (image-dired-original-file-name)))
1204 (when f
1205 (abbreviate-file-name f))))
1207 (defun image-dired-associated-dired-buffer ()
1208 "Get associated dired buffer at point."
1209 (get-text-property (point) 'associated-dired-buffer))
1211 (defun image-dired-get-buffer-window (buf)
1212 "Return window where buffer BUF is."
1213 (get-window-with-predicate
1214 (lambda (window)
1215 (equal (window-buffer window) buf))
1216 nil t))
1218 (defun image-dired-track-original-file ()
1219 "Track the original file in the associated dired buffer.
1220 See documentation for `image-dired-toggle-movement-tracking'.
1221 Interactive use only useful if `image-dired-track-movement' is nil."
1222 (interactive)
1223 (let* ((dired-buf (image-dired-associated-dired-buffer))
1224 (file-name (image-dired-original-file-name))
1225 (window (image-dired-get-buffer-window dired-buf)))
1226 (and (buffer-live-p dired-buf) file-name
1227 (with-current-buffer dired-buf
1228 (if (not (dired-goto-file file-name))
1229 (message "Could not track file")
1230 (if window (set-window-point window (point))))))))
1232 (defun image-dired-toggle-movement-tracking ()
1233 "Turn on and off `image-dired-track-movement'.
1234 Tracking of the movements between thumbnail and dired buffer so that
1235 they are \"mirrored\" in the dired buffer. When this is on, moving
1236 around in the thumbnail or dired buffer will find the matching
1237 position in the other buffer."
1238 (interactive)
1239 (setq image-dired-track-movement (not image-dired-track-movement))
1240 (message "Tracking %s" (if image-dired-track-movement "on" "off")))
1242 (defun image-dired-track-thumbnail ()
1243 "Track current dired file's thumb in `image-dired-thumbnail-buffer'.
1244 This is almost the same as what `image-dired-track-original-file' does,
1245 but the other way around."
1246 (let ((file (dired-get-filename))
1247 prop-val found window)
1248 (when (get-buffer image-dired-thumbnail-buffer)
1249 (with-current-buffer image-dired-thumbnail-buffer
1250 (goto-char (point-min))
1251 (while (and (not (eobp))
1252 (not found))
1253 (if (and (setq prop-val
1254 (get-text-property (point) 'original-file-name))
1255 (string= prop-val file))
1256 (setq found t))
1257 (if (not found)
1258 (forward-char 1)))
1259 (when found
1260 (if (setq window (image-dired-thumbnail-window))
1261 (set-window-point window (point)))
1262 (image-dired-display-thumb-properties))))))
1264 (defun image-dired-dired-next-line (&optional arg)
1265 "Call `dired-next-line', then track thumbnail.
1266 This can safely replace `dired-next-line'.
1267 With prefix argument, move ARG lines."
1268 (interactive "P")
1269 (dired-next-line (or arg 1))
1270 (if image-dired-track-movement
1271 (image-dired-track-thumbnail)))
1273 (defun image-dired-dired-previous-line (&optional arg)
1274 "Call `dired-previous-line', then track thumbnail.
1275 This can safely replace `dired-previous-line'.
1276 With prefix argument, move ARG lines."
1277 (interactive "P")
1278 (dired-previous-line (or arg 1))
1279 (if image-dired-track-movement
1280 (image-dired-track-thumbnail)))
1282 (defun image-dired-forward-image (&optional arg)
1283 "Move to next image and display properties.
1284 Optional prefix ARG says how many images to move; default is one
1285 image."
1286 (interactive "p")
1287 (let (pos (steps (or arg 1)))
1288 (dotimes (_ steps)
1289 (if (and (not (eobp))
1290 (save-excursion
1291 (forward-char)
1292 (while (and (not (eobp))
1293 (not (image-dired-image-at-point-p)))
1294 (forward-char))
1295 (setq pos (point))
1296 (image-dired-image-at-point-p)))
1297 (goto-char pos)
1298 (error "At last image"))))
1299 (when image-dired-track-movement
1300 (image-dired-track-original-file))
1301 (image-dired-display-thumb-properties))
1303 (defun image-dired-backward-image (&optional arg)
1304 "Move to previous image and display properties.
1305 Optional prefix ARG says how many images to move; default is one
1306 image."
1307 (interactive "p")
1308 (let (pos (steps (or arg 1)))
1309 (dotimes (_ steps)
1310 (if (and (not (bobp))
1311 (save-excursion
1312 (backward-char)
1313 (while (and (not (bobp))
1314 (not (image-dired-image-at-point-p)))
1315 (backward-char))
1316 (setq pos (point))
1317 (image-dired-image-at-point-p)))
1318 (goto-char pos)
1319 (error "At first image"))))
1320 (when image-dired-track-movement
1321 (image-dired-track-original-file))
1322 (image-dired-display-thumb-properties))
1324 (defun image-dired-next-line ()
1325 "Move to next line and display properties."
1326 (interactive)
1327 (let ((goal-column (current-column)))
1328 (forward-line 1)
1329 (move-to-column goal-column))
1330 ;; If we end up in an empty spot, back up to the next thumbnail.
1331 (if (not (image-dired-image-at-point-p))
1332 (image-dired-backward-image))
1333 (if image-dired-track-movement
1334 (image-dired-track-original-file))
1335 (image-dired-display-thumb-properties))
1338 (defun image-dired-previous-line ()
1339 "Move to previous line and display properties."
1340 (interactive)
1341 (let ((goal-column (current-column)))
1342 (forward-line -1)
1343 (move-to-column goal-column))
1344 ;; If we end up in an empty spot, back up to the next
1345 ;; thumbnail. This should only happen if the user deleted a
1346 ;; thumbnail and did not refresh, so it is not very common. But we
1347 ;; can handle it in a good manner, so why not?
1348 (if (not (image-dired-image-at-point-p))
1349 (image-dired-backward-image))
1350 (if image-dired-track-movement
1351 (image-dired-track-original-file))
1352 (image-dired-display-thumb-properties))
1354 (defun image-dired-format-properties-string (buf file props comment)
1355 "Format display properties.
1356 BUF is the associated dired buffer, FILE is the original image file
1357 name, PROPS is a stringified list of tags and COMMENT is the image file's
1358 comment."
1359 (format-spec
1360 image-dired-display-properties-format
1361 (list
1362 (cons ?b (or buf ""))
1363 (cons ?f file)
1364 (cons ?t (or props ""))
1365 (cons ?c (or comment "")))))
1367 (defun image-dired-display-thumb-properties ()
1368 "Display thumbnail properties in the echo area."
1369 (if (not (eobp))
1370 (let ((file-name (file-name-nondirectory (image-dired-original-file-name)))
1371 (dired-buf (buffer-name (image-dired-associated-dired-buffer)))
1372 (props (mapconcat #'identity (get-text-property (point) 'tags) ", "))
1373 (comment (get-text-property (point) 'comment))
1374 (message-log-max nil))
1375 (if file-name
1376 (message "%s"
1377 (image-dired-format-properties-string
1378 dired-buf
1379 file-name
1380 props
1381 comment))))))
1383 (defun image-dired-dired-file-marked-p ()
1384 "Check whether file on current line is marked or not."
1385 (save-excursion
1386 (beginning-of-line)
1387 (not (looking-at "^ .*$"))))
1389 (defun image-dired-modify-mark-on-thumb-original-file (command)
1390 "Modify mark in dired buffer.
1391 COMMAND is one of `mark' for marking file in dired, `unmark' for
1392 unmarking file in dired or `flag' for flagging file for delete in
1393 dired."
1394 (let ((file-name (image-dired-original-file-name))
1395 (dired-buf (image-dired-associated-dired-buffer)))
1396 (if (not (and dired-buf file-name))
1397 (message "No image, or image with correct properties, at point.")
1398 (with-current-buffer dired-buf
1399 (message "%s" file-name)
1400 (if (dired-goto-file file-name)
1401 (cond ((eq command 'mark) (dired-mark 1))
1402 ((eq command 'unmark) (dired-unmark 1))
1403 ((eq command 'toggle)
1404 (if (image-dired-dired-file-marked-p)
1405 (dired-unmark 1)
1406 (dired-mark 1)))
1407 ((eq command 'flag) (dired-flag-file-deletion 1))))))))
1409 (defun image-dired-mark-thumb-original-file ()
1410 "Mark original image file in associated dired buffer."
1411 (interactive)
1412 (image-dired-modify-mark-on-thumb-original-file 'mark)
1413 (image-dired-forward-image))
1415 (defun image-dired-unmark-thumb-original-file ()
1416 "Unmark original image file in associated dired buffer."
1417 (interactive)
1418 (image-dired-modify-mark-on-thumb-original-file 'unmark)
1419 (image-dired-forward-image))
1421 (defun image-dired-flag-thumb-original-file ()
1422 "Flag original image file for deletion in associated dired buffer."
1423 (interactive)
1424 (image-dired-modify-mark-on-thumb-original-file 'flag)
1425 (image-dired-forward-image))
1427 (defun image-dired-toggle-mark-thumb-original-file ()
1428 "Toggle mark on original image file in associated dired buffer."
1429 (interactive)
1430 (image-dired-modify-mark-on-thumb-original-file 'toggle))
1432 (defun image-dired-jump-original-dired-buffer ()
1433 "Jump to the dired buffer associated with the current image file.
1434 You probably want to use this together with
1435 `image-dired-track-original-file'."
1436 (interactive)
1437 (let ((buf (image-dired-associated-dired-buffer))
1438 window frame)
1439 (setq window (image-dired-get-buffer-window buf))
1440 (if window
1441 (progn
1442 (if (not (equal (selected-frame) (setq frame (window-frame window))))
1443 (select-frame-set-input-focus frame))
1444 (select-window window))
1445 (message "Associated dired buffer not visible"))))
1447 ;;;###autoload
1448 (defun image-dired-jump-thumbnail-buffer ()
1449 "Jump to thumbnail buffer."
1450 (interactive)
1451 (let ((window (image-dired-thumbnail-window))
1452 frame)
1453 (if window
1454 (progn
1455 (if (not (equal (selected-frame) (setq frame (window-frame window))))
1456 (select-frame-set-input-focus frame))
1457 (select-window window))
1458 (message "Thumbnail buffer not visible"))))
1460 (defvar image-dired-thumbnail-mode-line-up-map
1461 (let ((map (make-sparse-keymap)))
1462 ;; map it to "g" so that the user can press it more quickly
1463 (define-key map "g" 'image-dired-line-up-dynamic)
1464 ;; "f" for "fixed" number of thumbs per row
1465 (define-key map "f" 'image-dired-line-up)
1466 ;; "i" for "interactive"
1467 (define-key map "i" 'image-dired-line-up-interactive)
1468 map)
1469 "Keymap for line-up commands in `image-dired-thumbnail-mode'.")
1471 (defvar image-dired-thumbnail-mode-tag-map
1472 (let ((map (make-sparse-keymap)))
1473 ;; map it to "t" so that the user can press it more quickly
1474 (define-key map "t" 'image-dired-tag-thumbnail)
1475 ;; "r" for "remove"
1476 (define-key map "r" 'image-dired-tag-thumbnail-remove)
1477 map)
1478 "Keymap for tag commands in `image-dired-thumbnail-mode'.")
1480 (defvar image-dired-thumbnail-mode-map
1481 (let ((map (make-sparse-keymap)))
1482 (define-key map [right] 'image-dired-forward-image)
1483 (define-key map [left] 'image-dired-backward-image)
1484 (define-key map [up] 'image-dired-previous-line)
1485 (define-key map [down] 'image-dired-next-line)
1486 (define-key map "\C-f" 'image-dired-forward-image)
1487 (define-key map "\C-b" 'image-dired-backward-image)
1488 (define-key map "\C-p" 'image-dired-previous-line)
1489 (define-key map "\C-n" 'image-dired-next-line)
1491 (define-key map "d" 'image-dired-flag-thumb-original-file)
1492 (define-key map [delete] 'image-dired-flag-thumb-original-file)
1493 (define-key map "m" 'image-dired-mark-thumb-original-file)
1494 (define-key map "u" 'image-dired-unmark-thumb-original-file)
1495 (define-key map "." 'image-dired-track-original-file)
1496 (define-key map [tab] 'image-dired-jump-original-dired-buffer)
1498 ;; add line-up map
1499 (define-key map "g" image-dired-thumbnail-mode-line-up-map)
1500 ;; add tag map
1501 (define-key map "t" image-dired-thumbnail-mode-tag-map)
1503 (define-key map "\C-m" 'image-dired-display-thumbnail-original-image)
1504 (define-key map [C-return] 'image-dired-thumbnail-display-external)
1506 (define-key map "l" 'image-dired-rotate-thumbnail-left)
1507 (define-key map "r" 'image-dired-rotate-thumbnail-right)
1508 (define-key map "L" 'image-dired-rotate-original-left)
1509 (define-key map "R" 'image-dired-rotate-original-right)
1511 (define-key map "D" 'image-dired-thumbnail-set-image-description)
1512 (define-key map "\C-d" 'image-dired-delete-char)
1513 (define-key map " " 'image-dired-display-next-thumbnail-original)
1514 (define-key map (kbd "DEL") 'image-dired-display-previous-thumbnail-original)
1515 (define-key map "c" 'image-dired-comment-thumbnail)
1517 ;; Mouse
1518 (define-key map [mouse-2] 'image-dired-mouse-display-image)
1519 (define-key map [mouse-1] 'image-dired-mouse-select-thumbnail)
1520 ;; Seems I must first set C-down-mouse-1 to undefined, or else it
1521 ;; will trigger the buffer menu. If I try to instead bind
1522 ;; C-down-mouse-1 to `image-dired-mouse-toggle-mark', I get a message
1523 ;; about C-mouse-1 not being defined afterwards. Annoying, but I
1524 ;; probably do not completely understand mouse events.
1525 (define-key map [C-down-mouse-1] 'undefined)
1526 (define-key map [C-mouse-1] 'image-dired-mouse-toggle-mark)
1528 ;; Menu
1529 (easy-menu-define nil map
1530 "Menu for `image-dired-thumbnail-mode'."
1531 '("Image-Dired"
1532 ["Quit" quit-window]
1533 ["Delete thumbnail from buffer" image-dired-delete-char]
1534 ["Remove tag from thumbnail" image-dired-tag-thumbnail-remove]
1535 ["Tag thumbnail" image-dired-tag-thumbnail]
1536 ["Comment thumbnail" image-dired-comment-thumbnail]
1537 ["Refresh thumb" image-dired-refresh-thumb]
1538 ["Dynamic line up" image-dired-line-up-dynamic]
1539 ["Line up thumbnails" image-dired-line-up]
1541 ["Rotate thumbnail left" image-dired-rotate-thumbnail-left]
1542 ["Rotate thumbnail right" image-dired-rotate-thumbnail-right]
1543 ["Rotate original left" image-dired-rotate-original-left]
1544 ["Rotate original right" image-dired-rotate-original-right]
1546 ["Toggle movement tracking on/off" image-dired-toggle-movement-tracking]
1548 ["Jump to dired buffer" image-dired-jump-original-dired-buffer]
1549 ["Track original" image-dired-track-original-file]
1551 ["Flag original for deletion" image-dired-flag-thumb-original-file]
1552 ["Unmark original" image-dired-unmark-thumb-original-file]
1553 ["Mark original" image-dired-mark-thumb-original-file]
1555 ["Display in external viewer" image-dired-thumbnail-display-external]
1556 ["Display image" image-dired-display-thumbnail-original-image]))
1557 map)
1558 "Keymap for `image-dired-thumbnail-mode'.")
1560 (defvar image-dired-display-image-mode-map
1561 (let ((map (make-sparse-keymap)))
1562 ;; `image-mode-map' has bindings that do not make sense in image-dired
1563 ;; (set-keymap-parent map image-mode-map)
1564 (define-key map "f" 'image-dired-display-current-image-full)
1565 (define-key map "s" 'image-dired-display-current-image-sized)
1566 (define-key map "g" nil)
1568 ;; Useful bindings from `image-mode-map'
1569 (define-key map [remap forward-char] 'image-forward-hscroll)
1570 (define-key map [remap backward-char] 'image-backward-hscroll)
1571 (define-key map [remap right-char] 'image-forward-hscroll)
1572 (define-key map [remap left-char] 'image-backward-hscroll)
1573 (define-key map [remap previous-line] 'image-previous-line)
1574 (define-key map [remap next-line] 'image-next-line)
1575 (define-key map [remap scroll-up] 'image-scroll-up)
1576 (define-key map [remap scroll-down] 'image-scroll-down)
1577 (define-key map [remap scroll-up-command] 'image-scroll-up)
1578 (define-key map [remap scroll-down-command] 'image-scroll-down)
1579 (define-key map [remap scroll-left] 'image-scroll-left)
1580 (define-key map [remap scroll-right] 'image-scroll-right)
1581 (define-key map [remap move-beginning-of-line] 'image-bol)
1582 (define-key map [remap move-end-of-line] 'image-eol)
1583 (define-key map [remap beginning-of-buffer] 'image-bob)
1584 (define-key map [remap end-of-buffer] 'image-eob)
1586 (easy-menu-define nil map
1587 "Menu for `image-dired-display-image-mode-map'."
1588 '("Image-Dired"
1589 ["Quit" quit-window]
1590 ["Display original, sized to fit" image-dired-display-current-image-sized]
1591 ["Display original, full size" image-dired-display-current-image-full]))
1592 map)
1593 "Keymap for `image-dired-display-image-mode'.")
1595 (defun image-dired-display-current-image-full ()
1596 "Display current image in full size."
1597 (interactive)
1598 (let ((file (image-dired-original-file-name)))
1599 (if file
1600 (progn
1601 (image-dired-display-image file t)
1602 (message "Full size image displayed"))
1603 (error "No original file name at point"))))
1605 (defun image-dired-display-current-image-sized ()
1606 "Display current image in sized to fit window dimensions."
1607 (interactive)
1608 (let ((file (image-dired-original-file-name)))
1609 (if file
1610 (progn
1611 (image-dired-display-image file)
1612 (message "Fitted image displayed"))
1613 (error "No original file name at point"))))
1615 (define-derived-mode image-dired-thumbnail-mode
1616 special-mode "image-dired-thumbnail"
1617 "Browse and manipulate thumbnail images using dired.
1618 Use `image-dired-minor-mode' to get a nice setup."
1619 :group 'image-dired
1620 (buffer-disable-undo)
1621 (add-hook 'file-name-at-point-functions 'image-dired-file-name-at-point nil t))
1623 (define-derived-mode image-dired-display-image-mode
1624 special-mode "image-dired-image-display"
1625 "Mode for displaying and manipulating original image.
1626 Resized or in full-size."
1627 :group 'image-dired
1628 (buffer-disable-undo)
1629 (image-mode-setup-winprops)
1630 (setq cursor-type nil)
1631 (add-hook 'file-name-at-point-functions 'image-dired-file-name-at-point nil t))
1633 (defvar image-dired-minor-mode-map
1634 (let ((map (make-sparse-keymap)))
1635 ;; (set-keymap-parent map dired-mode-map)
1636 ;; Hijack previous and next line movement. Let C-p and C-b be
1637 ;; though...
1638 (define-key map "p" 'image-dired-dired-previous-line)
1639 (define-key map "n" 'image-dired-dired-next-line)
1640 (define-key map [up] 'image-dired-dired-previous-line)
1641 (define-key map [down] 'image-dired-dired-next-line)
1643 (define-key map (kbd "C-S-n") 'image-dired-next-line-and-display)
1644 (define-key map (kbd "C-S-p") 'image-dired-previous-line-and-display)
1645 (define-key map (kbd "C-S-m") 'image-dired-mark-and-display-next)
1647 (define-key map "\C-td" 'image-dired-display-thumbs)
1648 (define-key map [tab] 'image-dired-jump-thumbnail-buffer)
1649 (define-key map "\C-ti" 'image-dired-dired-display-image)
1650 (define-key map "\C-tx" 'image-dired-dired-display-external)
1651 (define-key map "\C-ta" 'image-dired-display-thumbs-append)
1652 (define-key map "\C-t." 'image-dired-display-thumb)
1653 (define-key map "\C-tc" 'image-dired-dired-comment-files)
1654 (define-key map "\C-tf" 'image-dired-mark-tagged-files)
1656 ;; Menu for dired
1657 (easy-menu-define nil map
1658 "Menu for `image-dired-minor-mode'."
1659 '("Image-dired"
1660 ["Copy with EXIF file name" image-dired-copy-with-exif-file-name]
1661 ["Comment files" image-dired-dired-comment-files]
1662 ["Mark tagged files" image-dired-mark-tagged-files]
1663 ["Jump to thumbnail buffer" image-dired-jump-thumbnail-buffer]
1665 ["Toggle movement tracking" image-dired-toggle-movement-tracking]
1666 ["Toggle append browsing" image-dired-toggle-append-browsing]
1667 ["Toggle display properties" image-dired-toggle-dired-display-properties]
1669 ["Display in external viewer" image-dired-dired-display-external]
1670 ["Display image" image-dired-dired-display-image]
1671 ["Display this thumbnail" image-dired-display-thumb]
1672 ["Display thumbnails append" image-dired-display-thumbs-append]
1674 ["Create thumbnails for marked files" image-dired-create-thumbs]
1676 ["Mark and display next" image-dired-mark-and-display-next]
1677 ["Display thumb for previous file" image-dired-previous-line-and-display]
1678 ["Display thumb for next file" image-dired-next-line-and-display]))
1679 map)
1680 "Keymap for `image-dired-minor-mode'.")
1682 ;;;###autoload
1683 (define-minor-mode image-dired-minor-mode
1684 "Setup easy-to-use keybindings for the commands to be used in dired mode.
1685 Note that n, p and <down> and <up> will be hijacked and bound to
1686 `image-dired-dired-x-line'."
1687 :keymap image-dired-minor-mode-map)
1689 ;;;###autoload
1690 (define-obsolete-function-alias 'image-dired-setup-dired-keybindings 'image-dired-minor-mode
1691 "26.1")
1693 (declare-function clear-image-cache "image.c" (&optional filter))
1695 (defun image-dired-create-thumbs (&optional arg)
1696 "Create thumbnail images for all marked files in dired.
1697 With prefix argument ARG, create thumbnails even if they already exist
1698 \(i.e. use this to refresh your thumbnails)."
1699 (interactive "P")
1700 (let (thumb-name)
1701 (dolist (curr-file (dired-get-marked-files))
1702 (setq thumb-name (image-dired-thumb-name curr-file))
1703 ;; If the user overrides the exist check, we must clear the
1704 ;; image cache so that if the user wants to display the
1705 ;; thumbnail, it is not fetched from cache.
1706 (when arg
1707 (clear-image-cache (expand-file-name thumb-name)))
1708 (when (or (not (file-exists-p thumb-name))
1709 arg)
1710 (image-dired-create-thumb curr-file thumb-name)))))
1712 (defvar image-dired-slideshow-timer nil
1713 "Slideshow timer.")
1715 (defvar image-dired-slideshow-count 0
1716 "Keeping track on number of images in slideshow.")
1718 (defvar image-dired-slideshow-times 0
1719 "Number of pictures to display in slideshow.")
1721 (defun image-dired-slideshow-step ()
1722 "Step to next file, if `image-dired-slideshow-times' has not been reached."
1723 (if (< image-dired-slideshow-count image-dired-slideshow-times)
1724 (progn
1725 (message "%s" (1+ image-dired-slideshow-count))
1726 (setq image-dired-slideshow-count (1+ image-dired-slideshow-count))
1727 (image-dired-next-line-and-display))
1728 (image-dired-slideshow-stop)))
1730 (defun image-dired-slideshow-start ()
1731 "Start slideshow.
1732 Ask user for number of images to show and the delay in between."
1733 (interactive)
1734 (setq image-dired-slideshow-count 0)
1735 (setq image-dired-slideshow-times (string-to-number (read-string "How many: ")))
1736 (let ((repeat (string-to-number
1737 (read-string
1738 "Delay, in seconds. Decimals are accepted : " "1"))))
1739 (setq image-dired-slideshow-timer
1740 (run-with-timer
1741 0 repeat
1742 'image-dired-slideshow-step))))
1744 (defun image-dired-slideshow-stop ()
1745 "Cancel slideshow."
1746 (interactive)
1747 (cancel-timer image-dired-slideshow-timer))
1749 (defun image-dired-delete-char ()
1750 "Remove current thumbnail from thumbnail buffer and line up."
1751 (interactive)
1752 (let ((inhibit-read-only t))
1753 (delete-char 1)
1754 (when (= (following-char) ?\s)
1755 (delete-char 1))))
1757 ;;;###autoload
1758 (defun image-dired-display-thumbs-append ()
1759 "Append thumbnails to `image-dired-thumbnail-buffer'."
1760 (interactive)
1761 (image-dired-display-thumbs nil t t))
1763 ;;;###autoload
1764 (defun image-dired-display-thumb ()
1765 "Shorthand for `image-dired-display-thumbs' with prefix argument."
1766 (interactive)
1767 (image-dired-display-thumbs t nil t))
1769 (defun image-dired-line-up ()
1770 "Line up thumbnails according to `image-dired-thumbs-per-row'.
1771 See also `image-dired-line-up-dynamic'."
1772 (interactive)
1773 (let ((inhibit-read-only t))
1774 (goto-char (point-min))
1775 (while (and (not (image-dired-image-at-point-p))
1776 (not (eobp)))
1777 (delete-char 1))
1778 (while (not (eobp))
1779 (forward-char)
1780 (while (and (not (image-dired-image-at-point-p))
1781 (not (eobp)))
1782 (delete-char 1)))
1783 (goto-char (point-min))
1784 (let ((count 0))
1785 (while (not (eobp))
1786 (forward-char)
1787 (if (= image-dired-thumbs-per-row 1)
1788 (insert "\n")
1789 (insert " ")
1790 (setq count (1+ count))
1791 (when (and (= count (- image-dired-thumbs-per-row 1))
1792 (not (eobp)))
1793 (forward-char)
1794 (insert "\n")
1795 (setq count 0)))))
1796 (goto-char (point-min))))
1798 (defun image-dired-line-up-dynamic ()
1799 "Line up thumbnails images dynamically.
1800 Calculate how many thumbnails fit."
1801 (interactive)
1802 (let* ((char-width (frame-char-width))
1803 (width (image-dired-window-width-pixels (image-dired-thumbnail-window)))
1804 (image-dired-thumbs-per-row
1805 (/ width
1806 (+ (* 2 image-dired-thumb-relief)
1807 (* 2 image-dired-thumb-margin)
1808 (image-dired-thumb-size 'width)
1809 char-width))))
1810 (image-dired-line-up)))
1812 (defun image-dired-line-up-interactive ()
1813 "Line up thumbnails interactively.
1814 Ask user how many thumbnails should be displayed per row."
1815 (interactive)
1816 (let ((image-dired-thumbs-per-row
1817 (string-to-number (read-string "How many thumbs per row: "))))
1818 (if (not (> image-dired-thumbs-per-row 0))
1819 (message "Number must be greater than 0")
1820 (image-dired-line-up))))
1822 (defun image-dired-thumbnail-display-external ()
1823 "Display original image for thumbnail at point using external viewer."
1824 (interactive)
1825 (let ((file (image-dired-original-file-name)))
1826 (if (not (image-dired-image-at-point-p))
1827 (message "No thumbnail at point")
1828 (if (not file)
1829 (message "No original file name found")
1830 (start-process "image-dired-thumb-external" nil
1831 image-dired-external-viewer file)))))
1833 ;;;###autoload
1834 (defun image-dired-dired-display-external ()
1835 "Display file at point using an external viewer."
1836 (interactive)
1837 (let ((file (dired-get-filename)))
1838 (start-process "image-dired-external" nil
1839 image-dired-external-viewer file)))
1841 (defun image-dired-window-width-pixels (window)
1842 "Calculate WINDOW width in pixels."
1843 (* (window-width window) (frame-char-width)))
1845 (defun image-dired-window-height-pixels (window)
1846 "Calculate WINDOW height in pixels."
1847 ;; Note: The mode-line consumes one line
1848 (* (- (window-height window) 1) (frame-char-height)))
1850 (defun image-dired-display-window ()
1851 "Return window where `image-dired-display-image-buffer' is visible."
1852 (get-window-with-predicate
1853 (lambda (window)
1854 (equal (buffer-name (window-buffer window)) image-dired-display-image-buffer))
1855 nil t))
1857 (defun image-dired-thumbnail-window ()
1858 "Return window where `image-dired-thumbnail-buffer' is visible."
1859 (get-window-with-predicate
1860 (lambda (window)
1861 (equal (buffer-name (window-buffer window)) image-dired-thumbnail-buffer))
1862 nil t))
1864 (defun image-dired-associated-dired-buffer-window ()
1865 "Return window where associated dired buffer is visible."
1866 (let (buf)
1867 (if (image-dired-image-at-point-p)
1868 (progn
1869 (setq buf (image-dired-associated-dired-buffer))
1870 (get-window-with-predicate
1871 (lambda (window)
1872 (equal (window-buffer window) buf))))
1873 (error "No thumbnail image at point"))))
1875 (defun image-dired-display-window-width (window)
1876 "Return width, in pixels, of WINDOW."
1877 (- (image-dired-window-width-pixels window)
1878 image-dired-display-window-width-correction))
1880 (defun image-dired-display-window-height (window)
1881 "Return height, in pixels, of WINDOW."
1882 (- (image-dired-window-height-pixels window)
1883 image-dired-display-window-height-correction))
1885 (defun image-dired-display-image (file &optional original-size)
1886 "Display image FILE in image buffer.
1887 Use this when you want to display the image, semi sized, in a new
1888 window. The image is sized to fit the display window (using a
1889 temporary file, don't worry). Because of this, it will not be as
1890 quick as opening it directly, but on most modern systems it
1891 should feel snappy enough.
1893 If optional argument ORIGINAL-SIZE is non-nil, display image in its
1894 original size."
1895 (image-dired--check-executable-exists
1896 'image-dired-cmd-create-temp-image-program)
1897 (let ((new-file (expand-file-name image-dired-temp-image-file))
1898 (window (image-dired-display-window))
1899 (image-type 'jpeg))
1900 (setq file (expand-file-name file))
1901 (if (not original-size)
1902 (let* ((spec
1903 (list
1904 (cons ?p image-dired-cmd-create-temp-image-program)
1905 (cons ?w (image-dired-display-window-width window))
1906 (cons ?h (image-dired-display-window-height window))
1907 (cons ?f file)
1908 (cons ?t new-file)))
1909 (ret
1910 (apply #'call-process
1911 image-dired-cmd-create-temp-image-program nil nil nil
1912 (mapcar
1913 (lambda (arg) (format-spec arg spec))
1914 image-dired-cmd-create-temp-image-options))))
1915 (when (not (zerop ret))
1916 (error "Could not resize image")))
1917 (setq image-type (image-type-from-file-name file))
1918 (copy-file file new-file t))
1919 (with-current-buffer (image-dired-create-display-image-buffer)
1920 (let ((inhibit-read-only t))
1921 (erase-buffer)
1922 (clear-image-cache)
1923 (image-dired-insert-image image-dired-temp-image-file image-type 0 0)
1924 (goto-char (point-min))
1925 (set-window-vscroll window 0)
1926 (set-window-hscroll window 0)
1927 (image-dired-update-property 'original-file-name file)))))
1929 (defun image-dired-display-thumbnail-original-image (&optional arg)
1930 "Display current thumbnail's original image in display buffer.
1931 See documentation for `image-dired-display-image' for more information.
1932 With prefix argument ARG, display image in its original size."
1933 (interactive "P")
1934 (let ((file (image-dired-original-file-name)))
1935 (if (not (string-equal major-mode "image-dired-thumbnail-mode"))
1936 (message "Not in image-dired-thumbnail-mode")
1937 (if (not (image-dired-image-at-point-p))
1938 (message "No thumbnail at point")
1939 (if (not file)
1940 (message "No original file name found")
1941 (image-dired-create-display-image-buffer)
1942 (display-buffer image-dired-display-image-buffer)
1943 (image-dired-display-image file arg))))))
1946 ;;;###autoload
1947 (defun image-dired-dired-display-image (&optional arg)
1948 "Display current image file.
1949 See documentation for `image-dired-display-image' for more information.
1950 With prefix argument ARG, display image in its original size."
1951 (interactive "P")
1952 (image-dired-create-display-image-buffer)
1953 (display-buffer image-dired-display-image-buffer)
1954 (image-dired-display-image (dired-get-filename) arg))
1956 (defun image-dired-image-at-point-p ()
1957 "Return true if there is an image-dired thumbnail at point."
1958 (get-text-property (point) 'image-dired-thumbnail))
1960 (defun image-dired-rotate-thumbnail (degrees)
1961 "Rotate thumbnail DEGREES degrees."
1962 (image-dired--check-executable-exists
1963 'image-dired-cmd-rotate-thumbnail-program)
1964 (if (not (image-dired-image-at-point-p))
1965 (message "No thumbnail at point")
1966 (let* ((file (image-dired-thumb-name (image-dired-original-file-name)))
1967 (thumb (expand-file-name file))
1968 (spec (list (cons ?d degrees) (cons ?t thumb))))
1969 (apply #'call-process image-dired-cmd-rotate-thumbnail-program nil nil nil
1970 (mapcar (lambda (arg) (format-spec arg spec))
1971 image-dired-cmd-rotate-thumbnail-options))
1972 (clear-image-cache thumb))))
1974 (defun image-dired-rotate-thumbnail-left ()
1975 "Rotate thumbnail left (counter clockwise) 90 degrees.
1976 The result of the rotation is displayed in the image display area
1977 and a confirmation is needed before the original image files is
1978 overwritten. This confirmation can be turned off using
1979 `image-dired-rotate-original-ask-before-overwrite'."
1980 (interactive)
1981 (image-dired-rotate-thumbnail "270"))
1983 (defun image-dired-rotate-thumbnail-right ()
1984 "Rotate thumbnail counter right (clockwise) 90 degrees.
1985 The result of the rotation is displayed in the image display area
1986 and a confirmation is needed before the original image files is
1987 overwritten. This confirmation can be turned off using
1988 `image-dired-rotate-original-ask-before-overwrite'."
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 (interactive)
2032 (image-dired-rotate-original "270"))
2034 (defun image-dired-rotate-original-right ()
2035 "Rotate original image right (clockwise) 90 degrees."
2036 (interactive)
2037 (image-dired-rotate-original "90"))
2039 (defun image-dired-get-exif-file-name (file)
2040 "Use the image's EXIF information to return a unique file name.
2041 The file name should be unique as long as you do not take more than
2042 one picture per second. The original file name is suffixed at the end
2043 for traceability. The format of the returned file name is
2044 YYYY_MM_DD_HH_MM_DD_ORIG_FILE_NAME.jpg. Used from
2045 `image-dired-copy-with-exif-file-name'."
2046 (let (data no-exif-data-found)
2047 (if (not (eq 'jpeg (image-type (expand-file-name file))))
2048 (setq no-exif-data-found t
2049 data (format-time-string
2050 "%Y:%m:%d %H:%M:%S"
2051 (file-attribute-modification-time
2052 (file-attributes (expand-file-name file)))))
2053 (setq data (image-dired-get-exif-data (expand-file-name file)
2054 "DateTimeOriginal")))
2055 (while (string-match "[ :]" data)
2056 (setq data (replace-match "_" nil nil data)))
2057 (format "%s%s%s" data
2058 (if no-exif-data-found
2059 "_noexif_"
2060 "_")
2061 (file-name-nondirectory file))))
2063 (defun image-dired-thumbnail-set-image-description ()
2064 "Set the ImageDescription EXIF tag for the original image.
2065 If the image already has a value for this tag, it is used as the
2066 default value at the prompt."
2067 (interactive)
2068 (if (not (image-dired-image-at-point-p))
2069 (message "No thumbnail at point")
2070 (let* ((file (image-dired-original-file-name))
2071 (old-value (image-dired-get-exif-data file "ImageDescription")))
2072 (if (eq 0
2073 (image-dired-set-exif-data file "ImageDescription"
2074 (read-string "Value of ImageDescription: "
2075 old-value)))
2076 (message "Successfully wrote ImageDescription tag.")
2077 (error "Could not write ImageDescription tag")))))
2079 (defun image-dired-set-exif-data (file tag-name tag-value)
2080 "In FILE, set EXIF tag TAG-NAME to value TAG-VALUE."
2081 (image-dired--check-executable-exists
2082 'image-dired-cmd-write-exif-data-program)
2083 (let ((spec
2084 (list
2085 (cons ?f (expand-file-name file))
2086 (cons ?t tag-name)
2087 (cons ?v tag-value))))
2088 (apply #'call-process image-dired-cmd-write-exif-data-program nil nil nil
2089 (mapcar (lambda (arg) (format-spec arg spec))
2090 image-dired-cmd-write-exif-data-options))))
2092 (defun image-dired-get-exif-data (file tag-name)
2093 "From FILE, return EXIF tag TAG-NAME."
2094 (image-dired--check-executable-exists
2095 'image-dired-cmd-read-exif-data-program)
2096 (let ((buf (get-buffer-create "*image-dired-get-exif-data*"))
2097 (spec (list (cons ?f file) (cons ?t tag-name)))
2098 tag-value)
2099 (with-current-buffer buf
2100 (delete-region (point-min) (point-max))
2101 (if (not (eq (apply #'call-process image-dired-cmd-read-exif-data-program
2102 nil t nil
2103 (mapcar
2104 (lambda (arg) (format-spec arg spec))
2105 image-dired-cmd-read-exif-data-options))
2107 (error "Could not get EXIF tag")
2108 (goto-char (point-min))
2109 ;; Clean buffer from newlines and carriage returns before
2110 ;; getting final info
2111 (while (search-forward-regexp "[\n\r]" nil t)
2112 (replace-match "" nil t))
2113 (setq tag-value (buffer-substring (point-min) (point-max)))))
2114 tag-value))
2116 (defun image-dired-copy-with-exif-file-name ()
2117 "Copy file with unique name to main image directory.
2118 Copy current or all marked files in dired to a new file in your
2119 main image directory, using a file name generated by
2120 `image-dired-get-exif-file-name'. A typical usage for this if when
2121 copying images from a digital camera into the image directory.
2123 Typically, you would open up the folder with the incoming
2124 digital images, mark the files to be copied, and execute this
2125 function. The result is a couple of new files in
2126 `image-dired-main-image-directory' called
2127 2005_05_08_12_52_00_dscn0319.jpg,
2128 2005_05_08_14_27_45_dscn0320.jpg etc."
2129 (interactive)
2130 (let (new-name
2131 (files (dired-get-marked-files)))
2132 (mapc
2133 (lambda (curr-file)
2134 (setq new-name
2135 (format "%s/%s"
2136 (file-name-as-directory
2137 (expand-file-name image-dired-main-image-directory))
2138 (image-dired-get-exif-file-name curr-file)))
2139 (message "Copying %s to %s" curr-file new-name)
2140 (copy-file curr-file new-name))
2141 files)))
2143 (defun image-dired-display-next-thumbnail-original ()
2144 "In thumbnail buffer, move to next thumbnail and display the image."
2145 (interactive)
2146 (image-dired-forward-image)
2147 (image-dired-display-thumbnail-original-image))
2149 (defun image-dired-display-previous-thumbnail-original ()
2150 "Move to previous thumbnail and display image."
2151 (interactive)
2152 (image-dired-backward-image)
2153 (image-dired-display-thumbnail-original-image))
2155 (defun image-dired-write-comments (file-comments)
2156 "Write file comments to database.
2157 Write file comments to one or more files.
2158 FILE-COMMENTS is an alist on the following form:
2159 ((FILE . COMMENT) ... )"
2160 (image-dired-sane-db-file)
2161 (let (end comment-beg-pos comment-end-pos file comment)
2162 (image-dired--with-db-file
2163 (setq buffer-file-name image-dired-db-file)
2164 (dolist (elt file-comments)
2165 (setq file (car elt)
2166 comment (cdr elt))
2167 (goto-char (point-min))
2168 (if (search-forward-regexp (format "^%s.*$" file) nil t)
2169 (progn
2170 (setq end (point))
2171 (beginning-of-line)
2172 ;; Delete old comment, if any
2173 (when (search-forward ";comment:" end t)
2174 (setq comment-beg-pos (match-beginning 0))
2175 ;; Any tags after the comment?
2176 (if (search-forward ";" end t)
2177 (setq comment-end-pos (- (point) 1))
2178 (setq comment-end-pos end))
2179 ;; Delete comment tag and comment
2180 (delete-region comment-beg-pos comment-end-pos))
2181 ;; Insert new comment
2182 (beginning-of-line)
2183 (unless (search-forward ";" end t)
2184 (end-of-line)
2185 (insert ";"))
2186 (insert (format "comment:%s;" comment)))
2187 ;; File does not exist in database - add it.
2188 (goto-char (point-max))
2189 (insert (format "\n%s;comment:%s" file comment))))
2190 (save-buffer))))
2192 (defun image-dired-update-property (prop value)
2193 "Update text property PROP with value VALUE at point."
2194 (let ((inhibit-read-only t))
2195 (put-text-property
2196 (point) (1+ (point))
2197 prop
2198 value)))
2200 ;;;###autoload
2201 (defun image-dired-dired-comment-files ()
2202 "Add comment to current or marked files in dired."
2203 (interactive)
2204 (let ((comment (image-dired-read-comment)))
2205 (image-dired-write-comments
2206 (mapcar
2207 (lambda (curr-file)
2208 (cons curr-file comment))
2209 (dired-get-marked-files)))))
2211 (defun image-dired-comment-thumbnail ()
2212 "Add comment to current thumbnail in thumbnail buffer."
2213 (interactive)
2214 (let* ((file (image-dired-original-file-name))
2215 (comment (image-dired-read-comment file)))
2216 (image-dired-write-comments (list (cons file comment)))
2217 (image-dired-update-property 'comment comment))
2218 (image-dired-display-thumb-properties))
2220 (defun image-dired-read-comment (&optional file)
2221 "Read comment for an image.
2222 Optionally use old comment from FILE as initial value."
2223 (let ((comment
2224 (read-string
2225 "Comment: "
2226 (if file (image-dired-get-comment file)))))
2227 comment))
2229 (defun image-dired-get-comment (file)
2230 "Get comment for file FILE."
2231 (image-dired-sane-db-file)
2232 (image-dired--with-db-file
2233 (let (end comment-beg-pos comment-end-pos comment)
2234 (when (search-forward-regexp (format "^%s" file) nil t)
2235 (end-of-line)
2236 (setq end (point))
2237 (beginning-of-line)
2238 (when (search-forward ";comment:" end t)
2239 (setq comment-beg-pos (point))
2240 (if (search-forward ";" end t)
2241 (setq comment-end-pos (- (point) 1))
2242 (setq comment-end-pos end))
2243 (setq comment (buffer-substring
2244 comment-beg-pos comment-end-pos))))
2245 comment)))
2247 ;;;###autoload
2248 (defun image-dired-mark-tagged-files ()
2249 "Use regexp to mark files with matching tag.
2250 A `tag' is a keyword, a piece of meta data, associated with an
2251 image file and stored in image-dired's database file. This command
2252 lets you input a regexp and this will be matched against all tags
2253 on all image files in the database file. The files that have a
2254 matching tag will be marked in the dired buffer."
2255 (interactive)
2256 (image-dired-sane-db-file)
2257 (let ((tag (read-string "Mark tagged files (regexp): "))
2258 (hits 0)
2259 files)
2260 (image-dired--with-db-file
2261 ;; Collect matches
2262 (while (search-forward-regexp
2263 (concat "\\(^[^;\n]+\\);.*" tag ".*$") nil t)
2264 (push (match-string 1) files)))
2265 ;; Mark files
2266 (dolist (curr-file files)
2267 ;; I tried using `dired-mark-files-regexp' but it was waaaay to
2268 ;; slow. Don't bother about hits found in other directories
2269 ;; than the current one.
2270 (when (string= (file-name-as-directory
2271 (expand-file-name default-directory))
2272 (file-name-as-directory
2273 (file-name-directory curr-file)))
2274 (setq curr-file (file-name-nondirectory curr-file))
2275 (goto-char (point-min))
2276 (when (search-forward-regexp (format "\\s %s$" curr-file) nil t)
2277 (setq hits (+ hits 1))
2278 (dired-mark 1))))
2279 (message "%d files with matching tag marked." hits)))
2281 (defun image-dired-mouse-display-image (event)
2282 "Use mouse EVENT, call `image-dired-display-image' to display image.
2283 Track this in associated dired buffer if `image-dired-track-movement' is
2284 non-nil."
2285 (interactive "e")
2286 (mouse-set-point event)
2287 (goto-char (posn-point (event-end event)))
2288 (let ((file (image-dired-original-file-name)))
2289 (when file
2290 (if image-dired-track-movement
2291 (image-dired-track-original-file))
2292 (image-dired-create-display-image-buffer)
2293 (display-buffer image-dired-display-image-buffer)
2294 (image-dired-display-image file))))
2296 (defun image-dired-mouse-select-thumbnail (event)
2297 "Use mouse EVENT to select thumbnail image.
2298 Track this in associated dired buffer if `image-dired-track-movement' is
2299 non-nil."
2300 (interactive "e")
2301 (mouse-set-point event)
2302 (goto-char (posn-point (event-end event)))
2303 (if image-dired-track-movement
2304 (image-dired-track-original-file))
2305 (image-dired-display-thumb-properties))
2307 (defun image-dired-mouse-toggle-mark (event)
2308 "Use mouse EVENT to toggle dired mark for thumbnail.
2309 Track this in associated dired buffer if `image-dired-track-movement' is
2310 non-nil."
2311 (interactive "e")
2312 (mouse-set-point event)
2313 (goto-char (posn-point (event-end event)))
2314 (if image-dired-track-movement
2315 (image-dired-track-original-file))
2316 (image-dired-toggle-mark-thumb-original-file))
2318 (defun image-dired-dired-display-properties ()
2319 "Display properties for dired file in the echo area."
2320 (interactive)
2321 (let* ((file (dired-get-filename))
2322 (file-name (file-name-nondirectory file))
2323 (dired-buf (buffer-name (current-buffer)))
2324 (props (mapconcat #'identity (image-dired-list-tags file) ", "))
2325 (comment (image-dired-get-comment file))
2326 (message-log-max nil))
2327 (if file-name
2328 (message "%s"
2329 (image-dired-format-properties-string
2330 dired-buf
2331 file-name
2332 props
2333 comment)))))
2335 (defvar image-dired-tag-file-list nil
2336 "List to store tag-file structure.")
2338 (defvar image-dired-file-tag-list nil
2339 "List to store file-tag structure.")
2341 (defvar image-dired-file-comment-list nil
2342 "List to store file comments.")
2344 (defun image-dired-add-to-tag-file-list (tag file)
2345 "Add relation between TAG and FILE."
2346 (let (curr)
2347 (if image-dired-tag-file-list
2348 (if (setq curr (assoc tag image-dired-tag-file-list))
2349 (if (not (member file curr))
2350 (setcdr curr (cons file (cdr curr))))
2351 (setcdr image-dired-tag-file-list
2352 (cons (list tag file) (cdr image-dired-tag-file-list))))
2353 (setq image-dired-tag-file-list (list (list tag file))))))
2355 (defun image-dired-add-to-tag-file-lists (tag file)
2356 "Helper function used from `image-dired-create-gallery-lists'.
2358 Add TAG to FILE in one list and FILE to TAG in the other.
2360 Lisp structures look like the following:
2362 image-dired-file-tag-list:
2364 ((\"filename1\" \"tag1\" \"tag2\" \"tag3\" ...)
2365 (\"filename2\" \"tag1\" \"tag2\" \"tag3\" ...)
2366 ...)
2368 image-dired-tag-file-list:
2370 ((\"tag1\" \"filename1\" \"filename2\" \"filename3\" ...)
2371 (\"tag2\" \"filename1\" \"filename2\" \"filename3\" ...)
2372 ...)"
2373 ;; Add tag to file list
2374 (let (curr)
2375 (if image-dired-file-tag-list
2376 (if (setq curr (assoc file image-dired-file-tag-list))
2377 (setcdr curr (cons tag (cdr curr)))
2378 (setcdr image-dired-file-tag-list
2379 (cons (list file tag) (cdr image-dired-file-tag-list))))
2380 (setq image-dired-file-tag-list (list (list file tag))))
2381 ;; Add file to tag list
2382 (if image-dired-tag-file-list
2383 (if (setq curr (assoc tag image-dired-tag-file-list))
2384 (if (not (member file curr))
2385 (setcdr curr (cons file (cdr curr))))
2386 (setcdr image-dired-tag-file-list
2387 (cons (list tag file) (cdr image-dired-tag-file-list))))
2388 (setq image-dired-tag-file-list (list (list tag file))))))
2390 (defun image-dired-add-to-file-comment-list (file comment)
2391 "Helper function used from `image-dired-create-gallery-lists'.
2393 For FILE, add COMMENT to list.
2395 Lisp structure looks like the following:
2397 image-dired-file-comment-list:
2399 ((\"filename1\" . \"comment1\")
2400 (\"filename2\" . \"comment2\")
2401 ...)"
2402 (if image-dired-file-comment-list
2403 (if (not (assoc file image-dired-file-comment-list))
2404 (setcdr image-dired-file-comment-list
2405 (cons (cons file comment)
2406 (cdr image-dired-file-comment-list))))
2407 (setq image-dired-file-comment-list (list (cons file comment)))))
2409 (defun image-dired-create-gallery-lists ()
2410 "Create temporary lists used by `image-dired-gallery-generate'."
2411 (image-dired-sane-db-file)
2412 (image-dired--with-db-file
2413 (let (end beg file row-tags)
2414 (setq image-dired-tag-file-list nil)
2415 (setq image-dired-file-tag-list nil)
2416 (setq image-dired-file-comment-list nil)
2417 (goto-char (point-min))
2418 (while (search-forward-regexp "^." nil t)
2419 (end-of-line)
2420 (setq end (point))
2421 (beginning-of-line)
2422 (setq beg (point))
2423 (unless (search-forward ";" end nil)
2424 (error "Something is really wrong, check format of database"))
2425 (setq row-tags (split-string
2426 (buffer-substring beg end) ";"))
2427 (setq file (car row-tags))
2428 (dolist (x (cdr row-tags))
2429 (if (not (string-match "^comment:\\(.*\\)" x))
2430 (image-dired-add-to-tag-file-lists x file)
2431 (image-dired-add-to-file-comment-list file (match-string 1 x)))))))
2432 ;; Sort tag-file list
2433 (setq image-dired-tag-file-list
2434 (sort image-dired-tag-file-list
2435 (lambda (x y)
2436 (string< (car x) (car y))))))
2438 (defun image-dired-hidden-p (file)
2439 "Return t if image FILE has a \"hidden\" tag."
2440 (cl-loop for tag in (cdr (assoc file image-dired-file-tag-list))
2441 if (member tag image-dired-gallery-hidden-tags) return t))
2443 (defun image-dired-gallery-generate ()
2444 "Generate gallery pages.
2445 First we create a couple of Lisp structures from the database to make
2446 it easier to generate, then HTML-files are created in
2447 `image-dired-gallery-dir'."
2448 (interactive)
2449 (if (eq 'per-directory image-dired-thumbnail-storage)
2450 (error "Currently, gallery generation is not supported \
2451 when using per-directory thumbnail file storage"))
2452 (image-dired-create-gallery-lists)
2453 (let ((tags image-dired-tag-file-list)
2454 (index-file (format "%s/index.html" image-dired-gallery-dir))
2455 count tag tag-file
2456 comment file-tags tag-link tag-link-list)
2457 ;; Make sure gallery root exist
2458 (if (file-exists-p image-dired-gallery-dir)
2459 (if (not (file-directory-p image-dired-gallery-dir))
2460 (error "Variable image-dired-gallery-dir is not a directory"))
2461 (make-directory image-dired-gallery-dir))
2462 ;; Open index file
2463 (with-temp-file index-file
2464 (if (file-exists-p index-file)
2465 (insert-file-contents index-file))
2466 (insert "<html>\n")
2467 (insert " <body>\n")
2468 (insert " <h2>Image-Dired Gallery</h2>\n")
2469 (insert (format "<p>\n Gallery generated %s\n <p>\n"
2470 (current-time-string)))
2471 (insert " <h3>Tag index</h3>\n")
2472 (setq count 1)
2473 ;; Pre-generate list of all tag links
2474 (dolist (curr tags)
2475 (setq tag (car curr))
2476 (when (not (member tag image-dired-gallery-hidden-tags))
2477 (setq tag-link (format "<a href=\"%d.html\">%s</a>" count tag))
2478 (if tag-link-list
2479 (setq tag-link-list
2480 (append tag-link-list (list (cons tag tag-link))))
2481 (setq tag-link-list (list (cons tag tag-link))))
2482 (setq count (1+ count))))
2483 (setq count 1)
2484 ;; Main loop where we generated thumbnail pages per tag
2485 (dolist (curr tags)
2486 (setq tag (car curr))
2487 ;; Don't display hidden tags
2488 (when (not (member tag image-dired-gallery-hidden-tags))
2489 ;; Insert link to tag page in index
2490 (insert (format " %s<br>\n" (cdr (assoc tag tag-link-list))))
2491 ;; Open per-tag file
2492 (setq tag-file (format "%s/%s.html" image-dired-gallery-dir count))
2493 (with-temp-file tag-file
2494 (if (file-exists-p tag-file)
2495 (insert-file-contents tag-file))
2496 (erase-buffer)
2497 (insert "<html>\n")
2498 (insert " <body>\n")
2499 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2500 (insert (format " <h2>Images with tag &quot;%s&quot;</h2>" tag))
2501 ;; Main loop for files per tag page
2502 (dolist (file (cdr curr))
2503 (unless (image-dired-hidden-p file)
2504 ;; Insert thumbnail with link to full image
2505 (insert
2506 (format "<a href=\"%s/%s\"><img src=\"%s/%s\"%s></a>\n"
2507 image-dired-gallery-image-root-url
2508 (file-name-nondirectory file)
2509 image-dired-gallery-thumb-image-root-url
2510 (file-name-nondirectory (image-dired-thumb-name file)) file))
2511 ;; Insert comment, if any
2512 (if (setq comment (cdr (assoc file image-dired-file-comment-list)))
2513 (insert (format "<br>\n%s<br>\n" comment))
2514 (insert "<br>\n"))
2515 ;; Insert links to other tags, if any
2516 (when (> (length
2517 (setq file-tags (assoc file image-dired-file-tag-list))) 2)
2518 (insert "[ ")
2519 (dolist (extra-tag file-tags)
2520 ;; Only insert if not file name or the main tag
2521 (if (and (not (equal extra-tag tag))
2522 (not (equal extra-tag file)))
2523 (insert
2524 (format "%s " (cdr (assoc extra-tag tag-link-list))))))
2525 (insert "]<br>\n"))))
2526 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2527 (insert " </body>\n")
2528 (insert "</html>\n"))
2529 (setq count (1+ count))))
2530 (insert " </body>\n")
2531 (insert "</html>"))))
2533 (defvar image-dired-widget-list nil
2534 "List to keep track of meta data in edit buffer.")
2536 (declare-function widget-forward "wid-edit" (arg))
2538 ;;;###autoload
2539 (defun image-dired-dired-edit-comment-and-tags ()
2540 "Edit comment and tags of current or marked image files.
2541 Edit comment and tags for all marked image files in an
2542 easy-to-use form."
2543 (interactive)
2544 (setq image-dired-widget-list nil)
2545 ;; Setup buffer.
2546 (let ((files (dired-get-marked-files)))
2547 (pop-to-buffer-same-window "*Image-Dired Edit Meta Data*")
2548 (kill-all-local-variables)
2549 (make-local-variable 'widget-example-repeat)
2550 (let ((inhibit-read-only t))
2551 (erase-buffer))
2552 (remove-overlays)
2553 ;; Some help for the user.
2554 (widget-insert
2555 "\nEdit comments and tags for each image. Separate multiple tags
2556 with a comma. Move forward between fields using TAB or RET.
2557 Move to the previous field using backtab (S-TAB). Save by
2558 activating the Save button at the bottom of the form or cancel
2559 the operation by activating the Cancel button.\n\n")
2560 ;; Here comes all images and a comment and tag field for each
2561 ;; image.
2562 (let (thumb-file img comment-widget tag-widget)
2564 (dolist (file files)
2566 (setq thumb-file (image-dired-thumb-name file)
2567 img (create-image thumb-file))
2569 (insert-image img)
2570 (widget-insert "\n\nComment: ")
2571 (setq comment-widget
2572 (widget-create 'editable-field
2573 :size 60
2574 :format "%v "
2575 :value (or (image-dired-get-comment file) "")))
2576 (widget-insert "\nTags: ")
2577 (setq tag-widget
2578 (widget-create 'editable-field
2579 :size 60
2580 :format "%v "
2581 :value (or (mapconcat
2582 #'identity
2583 (image-dired-list-tags file)
2584 ",") "")))
2585 ;; Save information in all widgets so that we can use it when
2586 ;; the user saves the form.
2587 (setq image-dired-widget-list
2588 (append image-dired-widget-list
2589 (list (list file comment-widget tag-widget))))
2590 (widget-insert "\n\n")))
2592 ;; Footer with Save and Cancel button.
2593 (widget-insert "\n")
2594 (widget-create 'push-button
2595 :notify
2596 (lambda (&rest _ignore)
2597 (image-dired-save-information-from-widgets)
2598 (bury-buffer)
2599 (message "Done."))
2600 "Save")
2601 (widget-insert " ")
2602 (widget-create 'push-button
2603 :notify
2604 (lambda (&rest _ignore)
2605 (bury-buffer)
2606 (message "Operation canceled."))
2607 "Cancel")
2608 (widget-insert "\n")
2609 (use-local-map widget-keymap)
2610 (widget-setup)
2611 ;; Jump to the first widget.
2612 (widget-forward 1)))
2614 (defun image-dired-save-information-from-widgets ()
2615 "Save information found in `image-dired-widget-list'.
2616 Use the information in `image-dired-widget-list' to save comments and
2617 tags to their respective image file. Internal function used by
2618 `image-dired-dired-edit-comment-and-tags'."
2619 (let (file comment tag-string tag-list lst)
2620 (image-dired-write-comments
2621 (mapcar
2622 (lambda (widget)
2623 (setq file (car widget)
2624 comment (widget-value (cadr widget)))
2625 (cons file comment))
2626 image-dired-widget-list))
2627 (image-dired-write-tags
2628 (dolist (widget image-dired-widget-list lst)
2629 (setq file (car widget)
2630 tag-string (widget-value (car (cddr widget)))
2631 tag-list (split-string tag-string ","))
2632 (dolist (tag tag-list)
2633 (push (cons file tag) lst))))))
2635 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2636 ;;;;;;;;; TEST-SECTION ;;;;;;;;;;;
2637 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2639 ;; (defvar image-dired-dir-max-size 12300000)
2641 ;; (defun image-dired-test-clean-old-files ()
2642 ;; "Clean `image-dired-dir' from old thumbnail files.
2643 ;; \"Oldness\" measured using last access time. If the total size of all
2644 ;; thumbnail files in `image-dired-dir' is larger than 'image-dired-dir-max-size',
2645 ;; old files are deleted until the max size is reached."
2646 ;; (let* ((files
2647 ;; (sort
2648 ;; (mapcar
2649 ;; (lambda (f)
2650 ;; (let ((fattribs (file-attributes f)))
2651 ;; ;; Get last access time and file size
2652 ;; `(,(nth 4 fattribs) ,(nth 7 fattribs) ,f)))
2653 ;; (directory-files (image-dired-dir) t ".+\\.thumb\\..+$"))
2654 ;; ;; Sort function. Compare time between two files.
2655 ;; (lambda (l1 l2)
2656 ;; (time-less-p (car l1) (car l2)))))
2657 ;; (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) files))))
2658 ;; (while (> dirsize image-dired-dir-max-size)
2659 ;; (y-or-n-p
2660 ;; (format "Size of thumbnail directory: %d, delete old file %s? "
2661 ;; dirsize (cadr (cdar files))))
2662 ;; (delete-file (cadr (cdar files)))
2663 ;; (setq dirsize (- dirsize (car (cdar files))))
2664 ;; (setq files (cdr files)))))
2666 ;;;;;;;;;;;;;;;;;;;;;;,
2668 ;; (defun dired-speedbar-buttons (dired-buffer)
2669 ;; (when (and (boundp 'image-dired-use-speedbar)
2670 ;; image-dired-use-speedbar)
2671 ;; (let ((filename (with-current-buffer dired-buffer
2672 ;; (dired-get-filename))))
2673 ;; (when (and (not (string-equal filename (buffer-string)))
2674 ;; (string-match (image-file-name-regexp) filename))
2675 ;; (erase-buffer)
2676 ;; (insert (propertize
2677 ;; filename
2678 ;; 'display
2679 ;; (image-dired-get-thumbnail-image filename)))))))
2681 ;; (setq image-dired-use-speedbar t)
2683 (provide 'image-dired)
2685 ;;; image-dired.el ends here