Obsolete gnus-remove-if and gnus-remove-if-not
[emacs.git] / lisp / image-dired.el
blob175d9df5e8ca134040d17c8a2665f38a4dc6f8c3
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 <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; BACKGROUND
27 ;; ==========
29 ;; I needed a program to browse, organize and tag my pictures. I got
30 ;; tired of the old gallery program I used as it did not allow
31 ;; multi-file operations easily. Also, it put things out of my
32 ;; control. Image viewing programs I tested did not allow multi-file
33 ;; operations or did not do what I wanted it to.
35 ;; So, I got the idea to use the wonderful functionality of Emacs and
36 ;; `dired' to do it. It would allow me to do almost anything I wanted,
37 ;; which is basically just to browse all my pictures in an easy way,
38 ;; letting me manipulate and tag them in various ways. `dired' already
39 ;; provide all the file handling and navigation facilities; I only
40 ;; needed to add some functions to display the images.
42 ;; I briefly tried out thumbs.el, and although it seemed more
43 ;; powerful than this package, it did not work the way I wanted to. It
44 ;; was too slow to created thumbnails of all files in a directory (I
45 ;; currently keep all my 2000+ images in the same directory) and
46 ;; browsing the thumbnail buffer was slow too. image-dired.el will not
47 ;; create thumbnails until they are needed and the browsing is done
48 ;; quickly and easily in dired. I copied a great deal of ideas and
49 ;; code from there though... :)
51 ;; `image-dired' stores the thumbnail files in `image-dired-dir'
52 ;; using the file name format ORIGNAME.thumb.ORIGEXT. For example
53 ;; ~/.emacs.d/image-dired/myimage01.thumb.jpg. The "database" is for
54 ;; now just a plain text file with the following format:
56 ;; file-name-non-directory;comment:comment-text;tag1;tag2;tag3;...;tagN
59 ;; PREREQUISITES
60 ;; =============
62 ;; * The ImageMagick package. Currently, `convert' and `mogrify' are
63 ;; used. Find it here: http://www.imagemagick.org.
65 ;; * For non-lossy rotation of JPEG images, the JpegTRAN program is
66 ;; needed.
68 ;; * For `image-dired-get-exif-data' and `image-dired-write-exif-data' to work,
69 ;; the command line tool `exiftool' is needed. It can be found here:
70 ;; http://www.sno.phy.queensu.ca/~phil/exiftool/. These two functions
71 ;; are, among other things, used for writing comments to image files
72 ;; using `image-dired-thumbnail-set-image-description' and to create
73 ;; "unique" file names using `image-dired-get-exif-file-name' (used by
74 ;; `image-dired-copy-with-exif-file-name').
77 ;; USAGE
78 ;; =====
80 ;; This information has been moved to the manual. Type `C-h r' to open
81 ;; the Emacs manual and go to the node Thumbnails by typing `g
82 ;; Image-Dired RET'.
84 ;; Quickstart: M-x image-dired RET DIRNAME RET
86 ;; where DIRNAME is a directory containing image files.
88 ;; LIMITATIONS
89 ;; ===========
91 ;; * Supports all image formats that Emacs and convert supports, but
92 ;; the thumbnails are hard-coded to JPEG format.
94 ;; * WARNING: The "database" format used might be changed so keep a
95 ;; backup of `image-dired-db-file' when testing new versions.
97 ;; * `image-dired-display-image-mode' does not support animation
99 ;; TODO
100 ;; ====
102 ;; * Support gallery creation when using per-directory thumbnail
103 ;; storage.
105 ;; * Some sort of auto-rotate function based on rotate info in the
106 ;; EXIF data.
108 ;; * Investigate if it is possible to also write the tags to the image
109 ;; files.
111 ;; * From thumbs.el: Add an option for clean-up/max-size functionality
112 ;; for thumbnail directory.
114 ;; * From thumbs.el: Add setroot function.
116 ;; * From thumbs.el: Add image resizing, if useful (image-dired's automatic
117 ;; "image fit" might be enough)
119 ;; * From thumbs.el: Add the "modify" commands (emboss, negate,
120 ;; monochrome etc).
122 ;; * Add `image-dired-display-thumbs-ring' and functions to cycle that. Find
123 ;; out which is best, saving old batch just before inserting new, or
124 ;; saving the current batch in the ring when inserting it. Adding it
125 ;; probably needs rewriting `image-dired-display-thumbs' to be more general.
127 ;; * Find some way of toggling on and off really nice keybindings in
128 ;; dired (for example, using C-n or <down> instead of C-S-n). Richard
129 ;; suggested that we could keep C-t as prefix for image-dired commands
130 ;; as it is currently not used in dired. He also suggested that
131 ;; `dired-next-line' and `dired-previous-line' figure out if
132 ;; image-dired is enabled in the current buffer and, if it is, call
133 ;; `image-dired-dired-next-line' and
134 ;; `image-dired-dired-previous-line', respectively. Update: This is
135 ;; partly done; some bindings have now been added to dired.
137 ;; * Enhanced gallery creation with basic CSS-support and pagination
138 ;; of tag pages with many pictures.
140 ;; * Rewrite `image-dired-modify-mark-on-thumb-original-file' to be
141 ;; less ugly.
143 ;; * In some way keep track of buffers and windows and stuff so that
144 ;; it works as the user expects.
146 ;; * More/better documentation
149 ;;; Code:
151 (require 'dired)
152 (require 'format-spec)
153 (require 'image-mode)
154 (require 'widget)
156 (eval-when-compile
157 (require 'cl-lib)
158 (require 'wid-edit))
160 (defgroup image-dired nil
161 "Use dired to browse your images as thumbnails, and more."
162 :prefix "image-dired-"
163 :link '(info-link "(emacs) Image-Dired")
164 :group 'multimedia)
166 (defcustom image-dired-dir (locate-user-emacs-file "image-dired/")
167 "Directory where thumbnail images are stored."
168 :type 'directory
169 :group 'image-dired)
171 (defcustom image-dired-thumbnail-storage 'use-image-dired-dir
172 "How to store image-dired's thumbnail files.
173 Image-Dired can store thumbnail files in one of two ways and this is
174 controlled by this variable. \"Use image-dired dir\" means that the
175 thumbnails are stored in a central directory. \"Per directory\"
176 means that each thumbnail is stored in a subdirectory called
177 \".image-dired\" in the same directory where the image file is.
178 \"Thumbnail Managing Standard\" means that the thumbnails are
179 stored and generated according to the Thumbnail Managing Standard
180 that allows sharing of thumbnails across different programs."
181 :type '(choice :tag "How to store thumbnail files"
182 (const :tag "Use image-dired-dir" use-image-dired-dir)
183 (const :tag "Thumbnail Managing Standard (normal 128x128)" standard)
184 (const :tag "Thumbnail Managing Standard (large 256x256)" standard-large)
185 (const :tag "Per-directory" per-directory))
186 :group 'image-dired)
188 (defcustom image-dired-db-file
189 (expand-file-name ".image-dired_db" image-dired-dir)
190 "Database file where file names and their associated tags are stored."
191 :type 'file
192 :group 'image-dired)
194 (defcustom image-dired-temp-image-file
195 (expand-file-name ".image-dired_temp" image-dired-dir)
196 "Name of temporary image file used by various commands."
197 :type 'file
198 :group 'image-dired)
200 (defcustom image-dired-gallery-dir
201 (expand-file-name ".image-dired_gallery" image-dired-dir)
202 "Directory to store generated gallery html pages.
203 This path needs to be \"shared\" to the public so that it can access
204 the index.html page that image-dired creates."
205 :type 'directory
206 :group 'image-dired)
208 (defcustom image-dired-gallery-image-root-url
209 "http://your.own.server/image-diredpics"
210 "URL where the full size images are to be found.
211 Note that this path has to be configured in your web server. Image-Dired
212 expects to find pictures in this directory."
213 :type 'string
214 :group 'image-dired)
216 (defcustom image-dired-gallery-thumb-image-root-url
217 "http://your.own.server/image-diredthumbs"
218 "URL where the thumbnail images are to be found.
219 Note that this path has to be configured in your web server. Image-Dired
220 expects to find pictures in this directory."
221 :type 'string
222 :group 'image-dired)
224 (defcustom image-dired-cmd-create-thumbnail-program
225 "convert"
226 "Executable used to create thumbnail.
227 Used together with `image-dired-cmd-create-thumbnail-options'."
228 :type 'file
229 :group 'image-dired)
231 (defcustom image-dired-cmd-create-thumbnail-options
232 '("-size" "%wx%h" "%f[0]" "-resize" "%wx%h>" "-strip" "jpeg:%t")
233 "Options of command used to create thumbnail image.
234 Used with `image-dired-cmd-create-thumbnail-program'.
235 Available format specifiers are: %w which is replaced by
236 `image-dired-thumb-width', %h which is replaced by `image-dired-thumb-height',
237 %f which is replaced by the file name of the original image and %t
238 which is replaced by the file name of the thumbnail file."
239 :version "26.1"
240 :type '(repeat (string :tag "Argument"))
241 :group 'image-dired)
243 (defcustom image-dired-cmd-create-temp-image-program "convert"
244 "Executable used to create temporary image.
245 Used together with `image-dired-cmd-create-temp-image-options'."
246 :type 'file
247 :group 'image-dired)
249 (defcustom image-dired-cmd-create-temp-image-options
250 '("-size" "%wx%h" "%f[0]" "-resize" "%wx%h>" "-strip" "jpeg:%t")
251 "Options of command used to create temporary image for display window.
252 Used together with `image-dired-cmd-create-temp-image-program',
253 Available format specifiers are: %w and %h which are replaced by
254 the calculated max size for width and height in the image display window,
255 %f which is replaced by the file name of the original image and %t which
256 is replaced by the file name of the temporary file."
257 :version "26.1"
258 :type '(repeat (string :tag "Argument"))
259 :group 'image-dired)
261 (defcustom image-dired-cmd-pngnq-program
262 (or (executable-find "pngnq")
263 (executable-find "pngnq-s9"))
264 "The file name of the `pngnq' program.
265 It quantizes colors of PNG images down to 256 colors or fewer
266 using the NeuQuant algorithm."
267 :version "26.1"
268 :type '(choice (const :tag "Not Set" nil) file)
269 :group 'image-dired)
271 (defcustom image-dired-cmd-pngnq-options
272 '("-f" "%t")
273 "Arguments to pass `image-dired-cmd-pngnq-program'.
274 Available format specifiers are the same as in
275 `image-dired-cmd-create-thumbnail-options'."
276 :version "26.1"
277 :type '(repeat (string :tag "Argument"))
278 :group 'image-dired)
280 (defcustom image-dired-cmd-pngcrush-program (executable-find "pngcrush")
281 "The file name of the `pngcrush' program.
282 It optimizes the compression of PNG images. Also it adds PNG textual chunks
283 with the information required by the Thumbnail Managing Standard."
284 :type '(choice (const :tag "Not Set" nil) file)
285 :group 'image-dired)
287 (defcustom image-dired-cmd-pngcrush-options
288 `("-q"
289 "-text" "b" "Description" "Thumbnail of file://%f"
290 "-text" "b" "Software" ,(emacs-version)
291 ;; "-text b \"Thumb::Image::Height\" \"%oh\" "
292 ;; "-text b \"Thumb::Image::Mimetype\" \"%mime\" "
293 ;; "-text b \"Thumb::Image::Width\" \"%ow\" "
294 "-text" "b" "Thumb::MTime" "%m"
295 ;; "-text b \"Thumb::Size\" \"%b\" "
296 "-text" "b" "Thumb::URI" "file://%f"
297 "%q" "%t")
298 "Arguments for `image-dired-cmd-pngcrush-program'.
299 Available format specifiers are the same as in
300 `image-dired-cmd-create-thumbnail-options', with %q for a
301 temporary file name (typically generated by pnqnq)"
302 :version "26.1"
303 :type '(repeat (string :tag "Argument"))
304 :group 'image-dired)
306 (defcustom image-dired-cmd-optipng-program (executable-find "optipng")
307 "The file name of the `optipng' program."
308 :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 (thumb-attr (file-attributes thumb-file)))
587 (when (or (not thumb-attr)
588 (time-less-p (nth 5 thumb-attr)
589 (nth 5 (file-attributes file))))
590 (image-dired-create-thumb file thumb-file))
591 (create-image thumb-file)
592 ;; (list 'image :type 'jpeg
593 ;; :file thumb-file
594 ;; :relief image-dired-thumb-relief :margin image-dired-thumb-margin)
597 (defun image-dired-insert-thumbnail (file original-file-name
598 associated-dired-buffer)
599 "Insert thumbnail image FILE.
600 Add text properties ORIGINAL-FILE-NAME and ASSOCIATED-DIRED-BUFFER."
601 (let (beg end)
602 (setq beg (point))
603 (image-dired-insert-image file
604 ;; TODO: this should depend on the real file type
605 (if (memq image-dired-thumbnail-storage
606 '(standard standard-large))
607 'png 'jpeg)
608 image-dired-thumb-relief
609 image-dired-thumb-margin)
610 (setq end (point))
611 (add-text-properties
612 beg end
613 (list 'image-dired-thumbnail t
614 'original-file-name original-file-name
615 'associated-dired-buffer associated-dired-buffer
616 'tags (image-dired-list-tags original-file-name)
617 'mouse-face 'highlight
618 'comment (image-dired-get-comment original-file-name)))))
620 (defun image-dired-thumb-name (file)
621 "Return thumbnail file name for FILE.
622 Depending on the value of `image-dired-thumbnail-storage', the file
623 name will vary. For central thumbnail file storage, make a
624 MD5-hash of the image file's directory name and add that to make
625 the thumbnail file name unique. For per-directory storage, just
626 add a subdirectory. For standard storage, produce the file name
627 according to the Thumbnail Managing Standard."
628 (cond ((memq image-dired-thumbnail-storage '(standard standard-large))
629 (let* ((xdg (getenv "XDG_CACHE_HOME"))
630 (dir (if (and xdg (file-name-absolute-p xdg))
631 xdg "~/.cache"))
632 (thumbdir (cl-case image-dired-thumbnail-storage
633 (standard "thumbnails/normal")
634 (standard-large "thumbnails/large"))))
635 (expand-file-name
636 (concat (md5 (concat "file://" (expand-file-name file))) ".png")
637 (expand-file-name thumbdir dir))))
638 ((eq 'use-image-dired-dir image-dired-thumbnail-storage)
639 (let* ((f (expand-file-name file))
640 (md5-hash
641 ;; Is MD5 hashes fast enough? The checksum of a
642 ;; thumbnail file name need not be that
643 ;; "cryptographically" good so a faster one could
644 ;; be used here.
645 (md5 (file-name-as-directory (file-name-directory f)))))
646 (format "%s%s%s.thumb.%s"
647 (file-name-as-directory (expand-file-name (image-dired-dir)))
648 (file-name-base f)
649 (if md5-hash (concat "_" md5-hash) "")
650 (file-name-extension f))))
651 ((eq 'per-directory image-dired-thumbnail-storage)
652 (let ((f (expand-file-name file)))
653 (format "%s.image-dired/%s.thumb.%s"
654 (file-name-directory f)
655 (file-name-base f)
656 (file-name-extension f))))))
658 (defun image-dired--check-executable-exists (executable)
659 (unless (executable-find (symbol-value executable))
660 (error "Executable %S not found" executable)))
662 (defun image-dired-thumb-size (dimension)
663 "Return thumb size depending on `image-dired-thumbnail-storage'.
664 DIMENSION should be either the symbol 'width or 'height."
665 (cond
666 ((eq 'standard image-dired-thumbnail-storage) 128)
667 ((eq 'standard-large image-dired-thumbnail-storage) 256)
668 (t (cl-ecase dimension
669 (width image-dired-thumb-width)
670 (height image-dired-thumb-height)))))
672 (defvar image-dired-queue nil
673 "List of items in the queue.
674 Each item has the form (ORIGINAL-FILE TARGET-FILE).")
676 (defvar image-dired-queue-active-jobs 0
677 "Number of active jobs in `image-dired-queue'.")
679 (defvar image-dired-queue-active-limit 2
680 "Maximum number of concurrent jobs permitted for generating images.
681 Increase at own risk.")
683 (defun image-dired-pngnq-thumb (spec)
684 "Quantize thumbnail described by format SPEC with pngnq(1)."
685 (let ((process
686 (apply #'start-process "image-dired-pngnq" nil
687 image-dired-cmd-pngnq-program
688 (mapcar (lambda (arg) (format-spec arg spec))
689 image-dired-cmd-pngnq-options))))
690 (setf (process-sentinel process)
691 (lambda (process status)
692 (if (and (eq (process-status process) 'exit)
693 (zerop (process-exit-status process)))
694 ;; Pass off to pngcrush, or just rename the
695 ;; THUMB-nq8.png file back to THUMB.png
696 (if (and image-dired-cmd-pngcrush-program
697 (executable-find image-dired-cmd-pngcrush-program))
698 (image-dired-pngcrush-thumb spec)
699 (let ((nq8 (cdr (assq ?q spec)))
700 (thumb (cdr (assq ?t spec))))
701 (rename-file nq8 thumb t)))
702 (message "command %S %s" (process-command process)
703 (replace-regexp-in-string "\n" "" status)))))
704 process))
706 (defun image-dired-pngcrush-thumb (spec)
707 "Optimize thumbnail described by format SPEC with pngcrush(1)."
708 ;; If pngnq wasn't run, then the THUMB-nq8.png file does not exist.
709 ;; pngcrush needs an infile and outfile, so we just copy THUMB to
710 ;; THUMB-nq8.png and use the latter as a temp file.
711 (when (not image-dired-cmd-pngnq-program)
712 (let ((temp (cdr (assq ?q spec)))
713 (thumb (cdr (assq ?t spec))))
714 (copy-file thumb temp)))
715 (let ((process
716 (apply #'start-process "image-dired-pngcrush" nil
717 image-dired-cmd-pngcrush-program
718 (mapcar (lambda (arg) (format-spec arg spec))
719 image-dired-cmd-pngcrush-options))))
720 (setf (process-sentinel process)
721 (lambda (process status)
722 (unless (and (eq (process-status process) 'exit)
723 (zerop (process-exit-status process)))
724 (message "command %S %s" (process-command process)
725 (replace-regexp-in-string "\n" "" status)))
726 (when (memq (process-status process) '(exit signal))
727 (let ((temp (cdr (assq ?q spec))))
728 (delete-file temp)))))
729 process))
731 (defun image-dired-optipng-thumb (spec)
732 "Optimize thumbnail described by format SPEC with optipng(1)."
733 (let ((process
734 (apply #'start-process "image-dired-optipng" nil
735 image-dired-cmd-optipng-program
736 (mapcar (lambda (arg) (format-spec arg spec))
737 image-dired-cmd-optipng-options))))
738 (setf (process-sentinel process)
739 (lambda (process status)
740 (unless (and (eq (process-status process) 'exit)
741 (zerop (process-exit-status process)))
742 (message "command %S %s" (process-command process)
743 (replace-regexp-in-string "\n" "" status)))))
744 process))
746 (defun image-dired-create-thumb-1 (original-file thumbnail-file)
747 "For ORIGINAL-FILE, create thumbnail image named THUMBNAIL-FILE."
748 (image-dired--check-executable-exists
749 'image-dired-cmd-create-thumbnail-program)
750 (let* ((width (int-to-string (image-dired-thumb-size 'width)))
751 (height (int-to-string (image-dired-thumb-size 'height)))
752 (modif-time (format-time-string
753 "%s" (nth 5 (file-attributes original-file))))
754 (thumbnail-nq8-file (replace-regexp-in-string ".png\\'" "-nq8.png"
755 thumbnail-file))
756 (spec
757 (list
758 (cons ?w width)
759 (cons ?h height)
760 (cons ?m modif-time)
761 (cons ?f original-file)
762 (cons ?q thumbnail-nq8-file)
763 (cons ?t thumbnail-file)))
764 (thumbnail-dir (file-name-directory thumbnail-file))
765 process)
766 (when (not (file-exists-p thumbnail-dir))
767 (message "Creating thumbnail directory")
768 (make-directory thumbnail-dir t)
769 (set-file-modes thumbnail-dir #o700))
771 ;; Thumbnail file creation processes begin here and are marshaled
772 ;; in a queue by `image-dired-create-thumb'.
773 (setq process
774 (apply #'start-process "image-dired-create-thumbnail" nil
775 image-dired-cmd-create-thumbnail-program
776 (mapcar
777 (lambda (arg) (format-spec arg spec))
778 (if (memq image-dired-thumbnail-storage
779 '(standard standard-large))
780 image-dired-cmd-create-standard-thumbnail-options
781 image-dired-cmd-create-thumbnail-options))))
783 (setf (process-sentinel process)
784 (lambda (process status)
785 ;; Trigger next in queue once a thumbnail has been created
786 (cl-decf image-dired-queue-active-jobs)
787 (image-dired-thumb-queue-run)
788 (if (not (and (eq (process-status process) 'exit)
789 (zerop (process-exit-status process))))
790 (message "Thumb could not be created for %s: %s"
791 (abbreviate-file-name original-file)
792 (replace-regexp-in-string "\n" "" status))
793 (set-file-modes thumbnail-file #o600)
794 (clear-image-cache thumbnail-file)
795 ;; PNG thumbnail has been created since we are
796 ;; following the XDG thumbnail spec, so try to optimize
797 (when (memq image-dired-thumbnail-storage
798 '(standard standard-large))
799 (cond
800 ((and image-dired-cmd-pngnq-program
801 (executable-find image-dired-cmd-pngnq-program))
802 (image-dired-pngnq-thumb spec))
803 ((and image-dired-cmd-pngcrush-program
804 (executable-find image-dired-cmd-pngcrush-program))
805 (image-dired-pngcrush-thumb spec))
806 ((and image-dired-cmd-optipng-program
807 (executable-find image-dired-cmd-optipng-program))
808 (image-dired-optipng-thumb spec)))))))
809 process))
811 (defun image-dired-thumb-queue-run ()
812 "Run a queued job if one exists and not too many jobs are running.
813 Queued items live in `image-dired-queue'."
814 (while (and image-dired-queue
815 (< image-dired-queue-active-jobs
816 image-dired-queue-active-limit))
817 (cl-incf image-dired-queue-active-jobs)
818 (apply #'image-dired-create-thumb-1 (pop image-dired-queue))))
820 (defun image-dired-create-thumb (original-file thumbnail-file)
821 "Add a job for generating thumbnail to `image-dired-queue'."
822 (setq image-dired-queue
823 (nconc image-dired-queue
824 (list (list original-file thumbnail-file))))
825 (run-at-time 0 nil #'image-dired-thumb-queue-run))
827 ;;;###autoload
828 (defun image-dired-dired-toggle-marked-thumbs (&optional arg)
829 "Toggle thumbnails in front of file names in the dired buffer.
830 If no marked file could be found, insert or hide thumbnails on the
831 current line. ARG, if non-nil, specifies the files to use instead
832 of the marked files. If ARG is an integer, use the next ARG (or
833 previous -ARG, if ARG<0) files."
834 (interactive "P")
835 (dired-map-over-marks
836 (let ((image-pos (dired-move-to-filename))
837 (image-file (dired-get-filename nil t))
838 thumb-file
839 overlay)
840 (when (and image-file
841 (string-match-p (image-file-name-regexp) image-file))
842 (setq thumb-file (image-dired-get-thumbnail-image image-file))
843 ;; If image is not already added, then add it.
844 (let ((thumb-ov (cl-loop for ov in (overlays-in (point) (1+ (point)))
845 if (overlay-get ov 'thumb-file) return ov)))
846 (if thumb-ov
847 (delete-overlay thumb-ov)
848 (put-image thumb-file image-pos)
849 (setq overlay
850 (cl-loop for ov in (overlays-in (point) (1+ (point)))
851 if (overlay-get ov 'put-image) return ov))
852 (overlay-put overlay 'image-file image-file)
853 (overlay-put overlay 'thumb-file thumb-file)))))
854 arg ; Show or hide image on ARG next files.
855 'show-progress) ; Update dired display after each image is updated.
856 (add-hook 'dired-after-readin-hook
857 'image-dired-dired-after-readin-hook nil t))
859 (defun image-dired-dired-after-readin-hook ()
860 "Relocate existing thumbnail overlays in dired buffer after reverting.
861 Move them to their corresponding files if they still exist.
862 Otherwise, delete overlays."
863 (mapc (lambda (overlay)
864 (when (overlay-get overlay 'put-image)
865 (let* ((image-file (overlay-get overlay 'image-file))
866 (image-pos (dired-goto-file image-file)))
867 (if image-pos
868 (move-overlay overlay image-pos image-pos)
869 (delete-overlay overlay)))))
870 (overlays-in (point-min) (point-max))))
872 (defun image-dired-next-line-and-display ()
873 "Move to next dired line and display thumbnail image."
874 (interactive)
875 (dired-next-line 1)
876 (image-dired-display-thumbs
877 t (or image-dired-append-when-browsing nil) t)
878 (if image-dired-dired-disp-props
879 (image-dired-dired-display-properties)))
881 (defun image-dired-previous-line-and-display ()
882 "Move to previous dired line and display thumbnail image."
883 (interactive)
884 (dired-previous-line 1)
885 (image-dired-display-thumbs
886 t (or image-dired-append-when-browsing nil) t)
887 (if image-dired-dired-disp-props
888 (image-dired-dired-display-properties)))
890 (defun image-dired-toggle-append-browsing ()
891 "Toggle `image-dired-append-when-browsing'."
892 (interactive)
893 (setq image-dired-append-when-browsing
894 (not image-dired-append-when-browsing))
895 (message "Append browsing %s."
896 (if image-dired-append-when-browsing
897 "on"
898 "off")))
900 (defun image-dired-mark-and-display-next ()
901 "Mark current file in dired and display next thumbnail image."
902 (interactive)
903 (dired-mark 1)
904 (image-dired-display-thumbs
905 t (or image-dired-append-when-browsing nil) t)
906 (if image-dired-dired-disp-props
907 (image-dired-dired-display-properties)))
909 (defun image-dired-toggle-dired-display-properties ()
910 "Toggle `image-dired-dired-disp-props'."
911 (interactive)
912 (setq image-dired-dired-disp-props
913 (not image-dired-dired-disp-props))
914 (message "Dired display properties %s."
915 (if image-dired-dired-disp-props
916 "on"
917 "off")))
919 (defvar image-dired-thumbnail-buffer "*image-dired*"
920 "Image-Dired's thumbnail buffer.")
922 (defun image-dired-create-thumbnail-buffer ()
923 "Create thumb buffer and set `image-dired-thumbnail-mode'."
924 (let ((buf (get-buffer-create image-dired-thumbnail-buffer)))
925 (with-current-buffer buf
926 (setq buffer-read-only t)
927 (if (not (eq major-mode 'image-dired-thumbnail-mode))
928 (image-dired-thumbnail-mode)))
929 buf))
931 (defvar image-dired-display-image-buffer "*image-dired-display-image*"
932 "Where larger versions of the images are display.")
934 (defun image-dired-create-display-image-buffer ()
935 "Create image display buffer and set `image-dired-display-image-mode'."
936 (let ((buf (get-buffer-create image-dired-display-image-buffer)))
937 (with-current-buffer buf
938 (setq buffer-read-only t)
939 (if (not (eq major-mode 'image-dired-display-image-mode))
940 (image-dired-display-image-mode)))
941 buf))
943 (defvar image-dired-saved-window-configuration nil
944 "Saved window configuration.")
946 ;;;###autoload
947 (defun image-dired-dired-with-window-configuration (dir &optional arg)
948 "Open directory DIR and create a default window configuration.
950 Convenience command that:
952 - Opens dired in folder DIR
953 - Splits windows in most useful (?) way
954 - Set `truncate-lines' to t
956 After the command has finished, you would typically mark some
957 image files in dired and type
958 \\[image-dired-display-thumbs] (`image-dired-display-thumbs').
960 If called with prefix argument ARG, skip splitting of windows.
962 The current window configuration is saved and can be restored by
963 calling `image-dired-restore-window-configuration'."
964 (interactive "DDirectory: \nP")
965 (let ((buf (image-dired-create-thumbnail-buffer))
966 (buf2 (image-dired-create-display-image-buffer)))
967 (setq image-dired-saved-window-configuration
968 (current-window-configuration))
969 (dired dir)
970 (delete-other-windows)
971 (when (not arg)
972 (split-window-right)
973 (setq truncate-lines t)
974 (save-excursion
975 (other-window 1)
976 (pop-to-buffer-same-window buf)
977 (select-window (split-window-below))
978 (pop-to-buffer-same-window buf2)
979 (other-window -2)))))
981 (defun image-dired-restore-window-configuration ()
982 "Restore window configuration.
983 Restore any changes to the window configuration made by calling
984 `image-dired-dired-with-window-configuration'."
985 (interactive)
986 (if image-dired-saved-window-configuration
987 (set-window-configuration image-dired-saved-window-configuration)
988 (message "No saved window configuration")))
990 ;;;###autoload
991 (defun image-dired-display-thumbs (&optional arg append do-not-pop)
992 "Display thumbnails of all marked files, in `image-dired-thumbnail-buffer'.
993 If a thumbnail image does not exist for a file, it is created on the
994 fly. With prefix argument ARG, display only thumbnail for file at
995 point (this is useful if you have marked some files but want to show
996 another one).
998 Recommended usage is to split the current frame horizontally so that
999 you have the dired buffer in the left window and the
1000 `image-dired-thumbnail-buffer' buffer in the right window.
1002 With optional argument APPEND, append thumbnail to thumbnail buffer
1003 instead of erasing it first.
1005 Optional argument DO-NOT-POP controls if `pop-to-buffer' should be
1006 used or not. If non-nil, use `display-buffer' instead of
1007 `pop-to-buffer'. This is used from functions like
1008 `image-dired-next-line-and-display' and
1009 `image-dired-previous-line-and-display' where we do not want the
1010 thumbnail buffer to be selected."
1011 (interactive "P")
1012 (let ((buf (image-dired-create-thumbnail-buffer))
1013 thumb-name files dired-buf)
1014 (if arg
1015 (setq files (list (dired-get-filename)))
1016 (setq files (dired-get-marked-files)))
1017 (setq dired-buf (current-buffer))
1018 (with-current-buffer buf
1019 (let ((inhibit-read-only t))
1020 (if (not append)
1021 (erase-buffer)
1022 (goto-char (point-max)))
1023 (dolist (curr-file files)
1024 (setq thumb-name (image-dired-thumb-name curr-file))
1025 (when (not (file-exists-p thumb-name))
1026 (image-dired-create-thumb curr-file thumb-name))
1027 (image-dired-insert-thumbnail thumb-name curr-file dired-buf)))
1028 (if do-not-pop
1029 (display-buffer buf)
1030 (pop-to-buffer buf))
1031 (cond ((eq 'dynamic image-dired-line-up-method)
1032 (image-dired-line-up-dynamic))
1033 ((eq 'fixed image-dired-line-up-method)
1034 (image-dired-line-up))
1035 ((eq 'interactive image-dired-line-up-method)
1036 (image-dired-line-up-interactive))
1037 ((eq 'none image-dired-line-up-method)
1038 nil)
1040 (image-dired-line-up-dynamic))))))
1042 ;;;###autoload
1043 (defun image-dired-show-all-from-dir (dir)
1044 "Make a preview buffer for all images in DIR and display it.
1045 If the number of files in DIR matching `image-file-name-regexp'
1046 exceeds `image-dired-show-all-from-dir-max-files', a warning will be
1047 displayed."
1048 (interactive "DImage Dired: ")
1049 (dired dir)
1050 (dired-mark-files-regexp (image-file-name-regexp))
1051 (let ((files (dired-get-marked-files)))
1052 (if (or (<= (length files) image-dired-show-all-from-dir-max-files)
1053 (and (> (length files) image-dired-show-all-from-dir-max-files)
1054 (y-or-n-p
1055 (format
1056 "Directory contains more than %d image files. Proceed? "
1057 image-dired-show-all-from-dir-max-files))))
1058 (progn
1059 (image-dired-display-thumbs)
1060 (pop-to-buffer image-dired-thumbnail-buffer))
1061 (message "Canceled."))))
1063 ;;;###autoload
1064 (defalias 'image-dired 'image-dired-show-all-from-dir)
1066 ;;;###autoload
1067 (define-obsolete-function-alias 'tumme 'image-dired "24.4")
1069 (defun image-dired-sane-db-file ()
1070 "Check if `image-dired-db-file' exists.
1071 If not, try to create it (including any parent directories).
1072 Signal error if there are problems creating it."
1073 (or (file-exists-p image-dired-db-file)
1074 (let (dir buf)
1075 (unless (file-directory-p (setq dir (file-name-directory
1076 image-dired-db-file)))
1077 (make-directory dir t))
1078 (with-current-buffer (setq buf (create-file-buffer
1079 image-dired-db-file))
1080 (write-file image-dired-db-file))
1081 (kill-buffer buf)
1082 (file-exists-p image-dired-db-file))
1083 (error "Could not create %s" image-dired-db-file)))
1085 (defun image-dired-write-tags (file-tags)
1086 "Write file tags to database.
1087 Write each file and tag in FILE-TAGS to the database.
1088 FILE-TAGS is an alist in the following form:
1089 ((FILE . TAG) ... )"
1090 (image-dired-sane-db-file)
1091 (let (end file tag)
1092 (image-dired--with-db-file
1093 (setq buffer-file-name image-dired-db-file)
1094 (dolist (elt file-tags)
1095 (setq file (car elt)
1096 tag (cdr elt))
1097 (goto-char (point-min))
1098 (if (search-forward-regexp (format "^%s.*$" file) nil t)
1099 (progn
1100 (setq end (point))
1101 (beginning-of-line)
1102 (when (not (search-forward (format ";%s" tag) end t))
1103 (end-of-line)
1104 (insert (format ";%s" tag))))
1105 (goto-char (point-max))
1106 (insert (format "\n%s;%s" file tag))))
1107 (save-buffer))))
1109 (defun image-dired-remove-tag (files tag)
1110 "For all FILES, remove TAG from the image database."
1111 (image-dired-sane-db-file)
1112 (image-dired--with-db-file
1113 (setq buffer-file-name image-dired-db-file)
1114 (let (end)
1115 (unless (listp files)
1116 (if (stringp files)
1117 (setq files (list files))
1118 (error "Files must be a string or a list of strings!")))
1119 (dolist (file files)
1120 (goto-char (point-min))
1121 (when (search-forward-regexp (format "^%s" file) nil t)
1122 (end-of-line)
1123 (setq end (point))
1124 (beginning-of-line)
1125 (when (search-forward-regexp (format "\\(;%s\\)" tag) end t)
1126 (delete-region (match-beginning 1) (match-end 1))
1127 ;; Check if file should still be in the database. If
1128 ;; it has no tags or comments, it will be removed.
1129 (end-of-line)
1130 (setq end (point))
1131 (beginning-of-line)
1132 (when (not (search-forward ";" end t))
1133 (kill-line 1)
1134 ;; If on empty line at end of buffer
1135 (and (eobp)
1136 (looking-at "^$")
1137 (delete-char -1)))))))
1138 (save-buffer)))
1140 (defun image-dired-list-tags (file)
1141 "Read all tags for image FILE from the image database."
1142 (image-dired-sane-db-file)
1143 (image-dired--with-db-file
1144 (let (end (tags ""))
1145 (when (search-forward-regexp (format "^%s" file) nil t)
1146 (end-of-line)
1147 (setq end (point))
1148 (beginning-of-line)
1149 (if (search-forward ";" end t)
1150 (if (search-forward "comment:" end t)
1151 (if (search-forward ";" end t)
1152 (setq tags (buffer-substring (point) end)))
1153 (setq tags (buffer-substring (point) end)))))
1154 (split-string tags ";"))))
1156 ;;;###autoload
1157 (defun image-dired-tag-files (arg)
1158 "Tag marked file(s) in dired. With prefix ARG, tag file at point."
1159 (interactive "P")
1160 (let ((tag (read-string "Tags to add (separate tags with a semicolon): "))
1161 files)
1162 (if arg
1163 (setq files (list (dired-get-filename)))
1164 (setq files (dired-get-marked-files)))
1165 (image-dired-write-tags
1166 (mapcar
1167 (lambda (x)
1168 (cons x tag))
1169 files))))
1171 (defun image-dired-tag-thumbnail ()
1172 "Tag current thumbnail."
1173 (interactive)
1174 (let ((tag (read-string "Tags to add (separate tags with a semicolon): ")))
1175 (image-dired-write-tags (list (cons (image-dired-original-file-name) tag))))
1176 (image-dired-update-property
1177 'tags (image-dired-list-tags (image-dired-original-file-name))))
1179 ;;;###autoload
1180 (defun image-dired-delete-tag (arg)
1181 "Remove tag for selected file(s).
1182 With prefix argument ARG, remove tag from file at point."
1183 (interactive "P")
1184 (let ((tag (read-string "Tag to remove: "))
1185 files)
1186 (if arg
1187 (setq files (list (dired-get-filename)))
1188 (setq files (dired-get-marked-files)))
1189 (image-dired-remove-tag files tag)))
1191 (defun image-dired-tag-thumbnail-remove ()
1192 "Remove tag from thumbnail."
1193 (interactive)
1194 (let ((tag (read-string "Tag to remove: ")))
1195 (image-dired-remove-tag (image-dired-original-file-name) tag))
1196 (image-dired-update-property
1197 'tags (image-dired-list-tags (image-dired-original-file-name))))
1199 (defun image-dired-original-file-name ()
1200 "Get original file name for thumbnail or display image at point."
1201 (get-text-property (point) 'original-file-name))
1203 (defun image-dired-file-name-at-point ()
1204 "Get abbreviated file name for thumbnail or display image at point."
1205 (let ((f (image-dired-original-file-name)))
1206 (when f
1207 (abbreviate-file-name f))))
1209 (defun image-dired-associated-dired-buffer ()
1210 "Get associated dired buffer at point."
1211 (get-text-property (point) 'associated-dired-buffer))
1213 (defun image-dired-get-buffer-window (buf)
1214 "Return window where buffer BUF is."
1215 (get-window-with-predicate
1216 (lambda (window)
1217 (equal (window-buffer window) buf))
1218 nil t))
1220 (defun image-dired-track-original-file ()
1221 "Track the original file in the associated dired buffer.
1222 See documentation for `image-dired-toggle-movement-tracking'.
1223 Interactive use only useful if `image-dired-track-movement' is nil."
1224 (interactive)
1225 (let* ((dired-buf (image-dired-associated-dired-buffer))
1226 (file-name (image-dired-original-file-name))
1227 (window (image-dired-get-buffer-window dired-buf)))
1228 (and (buffer-live-p dired-buf) file-name
1229 (with-current-buffer dired-buf
1230 (if (not (dired-goto-file file-name))
1231 (message "Could not track file")
1232 (if window (set-window-point window (point))))))))
1234 (defun image-dired-toggle-movement-tracking ()
1235 "Turn on and off `image-dired-track-movement'.
1236 Tracking of the movements between thumbnail and dired buffer so that
1237 they are \"mirrored\" in the dired buffer. When this is on, moving
1238 around in the thumbnail or dired buffer will find the matching
1239 position in the other buffer."
1240 (interactive)
1241 (setq image-dired-track-movement (not image-dired-track-movement))
1242 (message "Tracking %s" (if image-dired-track-movement "on" "off")))
1244 (defun image-dired-track-thumbnail ()
1245 "Track current dired file's thumb in `image-dired-thumbnail-buffer'.
1246 This is almost the same as what `image-dired-track-original-file' does,
1247 but the other way around."
1248 (let ((file (dired-get-filename))
1249 prop-val found window)
1250 (when (get-buffer image-dired-thumbnail-buffer)
1251 (with-current-buffer image-dired-thumbnail-buffer
1252 (goto-char (point-min))
1253 (while (and (not (eobp))
1254 (not found))
1255 (if (and (setq prop-val
1256 (get-text-property (point) 'original-file-name))
1257 (string= prop-val file))
1258 (setq found t))
1259 (if (not found)
1260 (forward-char 1)))
1261 (when found
1262 (if (setq window (image-dired-thumbnail-window))
1263 (set-window-point window (point)))
1264 (image-dired-display-thumb-properties))))))
1266 (defun image-dired-dired-next-line (&optional arg)
1267 "Call `dired-next-line', then track thumbnail.
1268 This can safely replace `dired-next-line'.
1269 With prefix argument, move ARG lines."
1270 (interactive "P")
1271 (dired-next-line (or arg 1))
1272 (if image-dired-track-movement
1273 (image-dired-track-thumbnail)))
1275 (defun image-dired-dired-previous-line (&optional arg)
1276 "Call `dired-previous-line', then track thumbnail.
1277 This can safely replace `dired-previous-line'.
1278 With prefix argument, move ARG lines."
1279 (interactive "P")
1280 (dired-previous-line (or arg 1))
1281 (if image-dired-track-movement
1282 (image-dired-track-thumbnail)))
1284 (defun image-dired-forward-image (&optional arg)
1285 "Move to next image and display properties.
1286 Optional prefix ARG says how many images to move; default is one
1287 image."
1288 (interactive "p")
1289 (let (pos (steps (or arg 1)))
1290 (dotimes (_ steps)
1291 (if (and (not (eobp))
1292 (save-excursion
1293 (forward-char)
1294 (while (and (not (eobp))
1295 (not (image-dired-image-at-point-p)))
1296 (forward-char))
1297 (setq pos (point))
1298 (image-dired-image-at-point-p)))
1299 (goto-char pos)
1300 (error "At last image"))))
1301 (when image-dired-track-movement
1302 (image-dired-track-original-file))
1303 (image-dired-display-thumb-properties))
1305 (defun image-dired-backward-image (&optional arg)
1306 "Move to previous image and display properties.
1307 Optional prefix ARG says how many images to move; default is one
1308 image."
1309 (interactive "p")
1310 (let (pos (steps (or arg 1)))
1311 (dotimes (_ steps)
1312 (if (and (not (bobp))
1313 (save-excursion
1314 (backward-char)
1315 (while (and (not (bobp))
1316 (not (image-dired-image-at-point-p)))
1317 (backward-char))
1318 (setq pos (point))
1319 (image-dired-image-at-point-p)))
1320 (goto-char pos)
1321 (error "At first image"))))
1322 (when image-dired-track-movement
1323 (image-dired-track-original-file))
1324 (image-dired-display-thumb-properties))
1326 (defun image-dired-next-line ()
1327 "Move to next line and display properties."
1328 (interactive)
1329 (let ((goal-column (current-column)))
1330 (forward-line 1)
1331 (move-to-column goal-column))
1332 ;; If we end up in an empty spot, back up to the next thumbnail.
1333 (if (not (image-dired-image-at-point-p))
1334 (image-dired-backward-image))
1335 (if image-dired-track-movement
1336 (image-dired-track-original-file))
1337 (image-dired-display-thumb-properties))
1340 (defun image-dired-previous-line ()
1341 "Move to previous line and display properties."
1342 (interactive)
1343 (let ((goal-column (current-column)))
1344 (forward-line -1)
1345 (move-to-column goal-column))
1346 ;; If we end up in an empty spot, back up to the next
1347 ;; thumbnail. This should only happen if the user deleted a
1348 ;; thumbnail and did not refresh, so it is not very common. But we
1349 ;; can handle it in a good manner, so why not?
1350 (if (not (image-dired-image-at-point-p))
1351 (image-dired-backward-image))
1352 (if image-dired-track-movement
1353 (image-dired-track-original-file))
1354 (image-dired-display-thumb-properties))
1356 (defun image-dired-format-properties-string (buf file props comment)
1357 "Format display properties.
1358 BUF is the associated dired buffer, FILE is the original image file
1359 name, PROPS is a stringified list of tags and COMMENT is the image file's
1360 comment."
1361 (format-spec
1362 image-dired-display-properties-format
1363 (list
1364 (cons ?b (or buf ""))
1365 (cons ?f file)
1366 (cons ?t (or props ""))
1367 (cons ?c (or comment "")))))
1369 (defun image-dired-display-thumb-properties ()
1370 "Display thumbnail properties in the echo area."
1371 (if (not (eobp))
1372 (let ((file-name (file-name-nondirectory (image-dired-original-file-name)))
1373 (dired-buf (buffer-name (image-dired-associated-dired-buffer)))
1374 (props (mapconcat #'identity (get-text-property (point) 'tags) ", "))
1375 (comment (get-text-property (point) 'comment))
1376 (message-log-max nil))
1377 (if file-name
1378 (message "%s"
1379 (image-dired-format-properties-string
1380 dired-buf
1381 file-name
1382 props
1383 comment))))))
1385 (defun image-dired-dired-file-marked-p ()
1386 "Check whether file on current line is marked or not."
1387 (save-excursion
1388 (beginning-of-line)
1389 (not (looking-at "^ .*$"))))
1391 (defun image-dired-modify-mark-on-thumb-original-file (command)
1392 "Modify mark in dired buffer.
1393 COMMAND is one of `mark' for marking file in dired, `unmark' for
1394 unmarking file in dired or `flag' for flagging file for delete in
1395 dired."
1396 (let ((file-name (image-dired-original-file-name))
1397 (dired-buf (image-dired-associated-dired-buffer)))
1398 (if (not (and dired-buf file-name))
1399 (message "No image, or image with correct properties, at point.")
1400 (with-current-buffer dired-buf
1401 (message "%s" file-name)
1402 (if (dired-goto-file file-name)
1403 (cond ((eq command 'mark) (dired-mark 1))
1404 ((eq command 'unmark) (dired-unmark 1))
1405 ((eq command 'toggle)
1406 (if (image-dired-dired-file-marked-p)
1407 (dired-unmark 1)
1408 (dired-mark 1)))
1409 ((eq command 'flag) (dired-flag-file-deletion 1))))))))
1411 (defun image-dired-mark-thumb-original-file ()
1412 "Mark original image file in associated dired buffer."
1413 (interactive)
1414 (image-dired-modify-mark-on-thumb-original-file 'mark)
1415 (image-dired-forward-image))
1417 (defun image-dired-unmark-thumb-original-file ()
1418 "Unmark original image file in associated dired buffer."
1419 (interactive)
1420 (image-dired-modify-mark-on-thumb-original-file 'unmark)
1421 (image-dired-forward-image))
1423 (defun image-dired-flag-thumb-original-file ()
1424 "Flag original image file for deletion in associated dired buffer."
1425 (interactive)
1426 (image-dired-modify-mark-on-thumb-original-file 'flag)
1427 (image-dired-forward-image))
1429 (defun image-dired-toggle-mark-thumb-original-file ()
1430 "Toggle mark on original image file in associated dired buffer."
1431 (interactive)
1432 (image-dired-modify-mark-on-thumb-original-file 'toggle))
1434 (defun image-dired-jump-original-dired-buffer ()
1435 "Jump to the dired buffer associated with the current image file.
1436 You probably want to use this together with
1437 `image-dired-track-original-file'."
1438 (interactive)
1439 (let ((buf (image-dired-associated-dired-buffer))
1440 window frame)
1441 (setq window (image-dired-get-buffer-window buf))
1442 (if window
1443 (progn
1444 (if (not (equal (selected-frame) (setq frame (window-frame window))))
1445 (select-frame-set-input-focus frame))
1446 (select-window window))
1447 (message "Associated dired buffer not visible"))))
1449 ;;;###autoload
1450 (defun image-dired-jump-thumbnail-buffer ()
1451 "Jump to thumbnail buffer."
1452 (interactive)
1453 (let ((window (image-dired-thumbnail-window))
1454 frame)
1455 (if window
1456 (progn
1457 (if (not (equal (selected-frame) (setq frame (window-frame window))))
1458 (select-frame-set-input-focus frame))
1459 (select-window window))
1460 (message "Thumbnail buffer not visible"))))
1462 (defvar image-dired-thumbnail-mode-line-up-map
1463 (let ((map (make-sparse-keymap)))
1464 ;; map it to "g" so that the user can press it more quickly
1465 (define-key map "g" 'image-dired-line-up-dynamic)
1466 ;; "f" for "fixed" number of thumbs per row
1467 (define-key map "f" 'image-dired-line-up)
1468 ;; "i" for "interactive"
1469 (define-key map "i" 'image-dired-line-up-interactive)
1470 map)
1471 "Keymap for line-up commands in `image-dired-thumbnail-mode'.")
1473 (defvar image-dired-thumbnail-mode-tag-map
1474 (let ((map (make-sparse-keymap)))
1475 ;; map it to "t" so that the user can press it more quickly
1476 (define-key map "t" 'image-dired-tag-thumbnail)
1477 ;; "r" for "remove"
1478 (define-key map "r" 'image-dired-tag-thumbnail-remove)
1479 map)
1480 "Keymap for tag commands in `image-dired-thumbnail-mode'.")
1482 (defvar image-dired-thumbnail-mode-map
1483 (let ((map (make-sparse-keymap)))
1484 (define-key map [right] 'image-dired-forward-image)
1485 (define-key map [left] 'image-dired-backward-image)
1486 (define-key map [up] 'image-dired-previous-line)
1487 (define-key map [down] 'image-dired-next-line)
1488 (define-key map "\C-f" 'image-dired-forward-image)
1489 (define-key map "\C-b" 'image-dired-backward-image)
1490 (define-key map "\C-p" 'image-dired-previous-line)
1491 (define-key map "\C-n" 'image-dired-next-line)
1493 (define-key map "d" 'image-dired-flag-thumb-original-file)
1494 (define-key map [delete] 'image-dired-flag-thumb-original-file)
1495 (define-key map "m" 'image-dired-mark-thumb-original-file)
1496 (define-key map "u" 'image-dired-unmark-thumb-original-file)
1497 (define-key map "." 'image-dired-track-original-file)
1498 (define-key map [tab] 'image-dired-jump-original-dired-buffer)
1500 ;; add line-up map
1501 (define-key map "g" image-dired-thumbnail-mode-line-up-map)
1502 ;; add tag map
1503 (define-key map "t" image-dired-thumbnail-mode-tag-map)
1505 (define-key map "\C-m" 'image-dired-display-thumbnail-original-image)
1506 (define-key map [C-return] 'image-dired-thumbnail-display-external)
1508 (define-key map "l" 'image-dired-rotate-thumbnail-left)
1509 (define-key map "r" 'image-dired-rotate-thumbnail-right)
1510 (define-key map "L" 'image-dired-rotate-original-left)
1511 (define-key map "R" 'image-dired-rotate-original-right)
1513 (define-key map "D" 'image-dired-thumbnail-set-image-description)
1514 (define-key map "\C-d" 'image-dired-delete-char)
1515 (define-key map " " 'image-dired-display-next-thumbnail-original)
1516 (define-key map (kbd "DEL") 'image-dired-display-previous-thumbnail-original)
1517 (define-key map "c" 'image-dired-comment-thumbnail)
1519 ;; Mouse
1520 (define-key map [mouse-2] 'image-dired-mouse-display-image)
1521 (define-key map [mouse-1] 'image-dired-mouse-select-thumbnail)
1522 ;; Seems I must first set C-down-mouse-1 to undefined, or else it
1523 ;; will trigger the buffer menu. If I try to instead bind
1524 ;; C-down-mouse-1 to `image-dired-mouse-toggle-mark', I get a message
1525 ;; about C-mouse-1 not being defined afterwards. Annoying, but I
1526 ;; probably do not completely understand mouse events.
1527 (define-key map [C-down-mouse-1] 'undefined)
1528 (define-key map [C-mouse-1] 'image-dired-mouse-toggle-mark)
1530 ;; Menu
1531 (easy-menu-define nil map
1532 "Menu for `image-dired-thumbnail-mode'."
1533 '("Image-Dired"
1534 ["Quit" quit-window]
1535 ["Delete thumbnail from buffer" image-dired-delete-char]
1536 ["Remove tag from thumbnail" image-dired-tag-thumbnail-remove]
1537 ["Tag thumbnail" image-dired-tag-thumbnail]
1538 ["Comment thumbnail" image-dired-comment-thumbnail]
1539 ["Refresh thumb" image-dired-refresh-thumb]
1540 ["Dynamic line up" image-dired-line-up-dynamic]
1541 ["Line up thumbnails" image-dired-line-up]
1543 ["Rotate thumbnail left" image-dired-rotate-thumbnail-left]
1544 ["Rotate thumbnail right" image-dired-rotate-thumbnail-right]
1545 ["Rotate original left" image-dired-rotate-original-left]
1546 ["Rotate original right" image-dired-rotate-original-right]
1548 ["Toggle movement tracking on/off" image-dired-toggle-movement-tracking]
1550 ["Jump to dired buffer" image-dired-jump-original-dired-buffer]
1551 ["Track original" image-dired-track-original-file]
1553 ["Flag original for deletion" image-dired-flag-thumb-original-file]
1554 ["Unmark original" image-dired-unmark-thumb-original-file]
1555 ["Mark original" image-dired-mark-thumb-original-file]
1557 ["Display in external viewer" image-dired-thumbnail-display-external]
1558 ["Display image" image-dired-display-thumbnail-original-image]))
1559 map)
1560 "Keymap for `image-dired-thumbnail-mode'.")
1562 (defvar image-dired-display-image-mode-map
1563 (let ((map (make-sparse-keymap)))
1564 ;; `image-mode-map' has bindings that do not make sense in image-dired
1565 ;; (set-keymap-parent map image-mode-map)
1566 (define-key map "f" 'image-dired-display-current-image-full)
1567 (define-key map "s" 'image-dired-display-current-image-sized)
1568 (define-key map "g" nil)
1570 ;; Useful bindings from `image-mode-map'
1571 (define-key map [remap forward-char] 'image-forward-hscroll)
1572 (define-key map [remap backward-char] 'image-backward-hscroll)
1573 (define-key map [remap right-char] 'image-forward-hscroll)
1574 (define-key map [remap left-char] 'image-backward-hscroll)
1575 (define-key map [remap previous-line] 'image-previous-line)
1576 (define-key map [remap next-line] 'image-next-line)
1577 (define-key map [remap scroll-up] 'image-scroll-up)
1578 (define-key map [remap scroll-down] 'image-scroll-down)
1579 (define-key map [remap scroll-up-command] 'image-scroll-up)
1580 (define-key map [remap scroll-down-command] 'image-scroll-down)
1581 (define-key map [remap scroll-left] 'image-scroll-left)
1582 (define-key map [remap scroll-right] 'image-scroll-right)
1583 (define-key map [remap move-beginning-of-line] 'image-bol)
1584 (define-key map [remap move-end-of-line] 'image-eol)
1585 (define-key map [remap beginning-of-buffer] 'image-bob)
1586 (define-key map [remap end-of-buffer] 'image-eob)
1588 (easy-menu-define nil map
1589 "Menu for `image-dired-display-image-mode-map'."
1590 '("Image-Dired"
1591 ["Quit" quit-window]
1592 ["Display original, sized to fit" image-dired-display-current-image-sized]
1593 ["Display original, full size" image-dired-display-current-image-full]))
1594 map)
1595 "Keymap for `image-dired-display-image-mode'.")
1597 (defun image-dired-display-current-image-full ()
1598 "Display current image in full size."
1599 (interactive)
1600 (let ((file (image-dired-original-file-name)))
1601 (if file
1602 (progn
1603 (image-dired-display-image file t)
1604 (message "Full size image displayed"))
1605 (error "No original file name at point"))))
1607 (defun image-dired-display-current-image-sized ()
1608 "Display current image in sized to fit window dimensions."
1609 (interactive)
1610 (let ((file (image-dired-original-file-name)))
1611 (if file
1612 (progn
1613 (image-dired-display-image file)
1614 (message "Fitted image displayed"))
1615 (error "No original file name at point"))))
1617 (define-derived-mode image-dired-thumbnail-mode
1618 special-mode "image-dired-thumbnail"
1619 "Browse and manipulate thumbnail images using dired.
1620 Use `image-dired-minor-mode' to get a nice setup."
1621 :group 'image-dired
1622 (buffer-disable-undo)
1623 (add-hook 'file-name-at-point-functions 'image-dired-file-name-at-point nil t))
1625 (define-derived-mode image-dired-display-image-mode
1626 special-mode "image-dired-image-display"
1627 "Mode for displaying and manipulating original image.
1628 Resized or in full-size."
1629 :group 'image-dired
1630 (buffer-disable-undo)
1631 (image-mode-setup-winprops)
1632 (setq cursor-type nil)
1633 (add-hook 'file-name-at-point-functions 'image-dired-file-name-at-point nil t))
1635 (defvar image-dired-minor-mode-map
1636 (let ((map (make-sparse-keymap)))
1637 ;; (set-keymap-parent map dired-mode-map)
1638 ;; Hijack previous and next line movement. Let C-p and C-b be
1639 ;; though...
1640 (define-key map "p" 'image-dired-dired-previous-line)
1641 (define-key map "n" 'image-dired-dired-next-line)
1642 (define-key map [up] 'image-dired-dired-previous-line)
1643 (define-key map [down] 'image-dired-dired-next-line)
1645 (define-key map (kbd "C-S-n") 'image-dired-next-line-and-display)
1646 (define-key map (kbd "C-S-p") 'image-dired-previous-line-and-display)
1647 (define-key map (kbd "C-S-m") 'image-dired-mark-and-display-next)
1649 (define-key map "\C-td" 'image-dired-display-thumbs)
1650 (define-key map [tab] 'image-dired-jump-thumbnail-buffer)
1651 (define-key map "\C-ti" 'image-dired-dired-display-image)
1652 (define-key map "\C-tx" 'image-dired-dired-display-external)
1653 (define-key map "\C-ta" 'image-dired-display-thumbs-append)
1654 (define-key map "\C-t." 'image-dired-display-thumb)
1655 (define-key map "\C-tc" 'image-dired-dired-comment-files)
1656 (define-key map "\C-tf" 'image-dired-mark-tagged-files)
1658 ;; Menu for dired
1659 (easy-menu-define nil map
1660 "Menu for `image-dired-minor-mode'."
1661 '("Image-dired"
1662 ["Copy with EXIF file name" image-dired-copy-with-exif-file-name]
1663 ["Comment files" image-dired-dired-comment-files]
1664 ["Mark tagged files" image-dired-mark-tagged-files]
1665 ["Jump to thumbnail buffer" image-dired-jump-thumbnail-buffer]
1667 ["Toggle movement tracking" image-dired-toggle-movement-tracking]
1668 ["Toggle append browsing" image-dired-toggle-append-browsing]
1669 ["Toggle display properties" image-dired-toggle-dired-display-properties]
1671 ["Display in external viewer" image-dired-dired-display-external]
1672 ["Display image" image-dired-dired-display-image]
1673 ["Display this thumbnail" image-dired-display-thumb]
1674 ["Display thumbnails append" image-dired-display-thumbs-append]
1676 ["Create thumbnails for marked files" image-dired-create-thumbs]
1678 ["Mark and display next" image-dired-mark-and-display-next]
1679 ["Display thumb for previous file" image-dired-previous-line-and-display]
1680 ["Display thumb for next file" image-dired-next-line-and-display]))
1681 map)
1682 "Keymap for `image-dired-minor-mode'.")
1684 ;;;###autoload
1685 (define-minor-mode image-dired-minor-mode
1686 "Setup easy-to-use keybindings for the commands to be used in dired mode.
1687 Note that n, p and <down> and <up> will be hijacked and bound to
1688 `image-dired-dired-x-line'."
1689 :keymap image-dired-minor-mode-map)
1691 ;;;###autoload
1692 (define-obsolete-function-alias 'image-dired-setup-dired-keybindings 'image-dired-minor-mode
1693 "26.1")
1695 (declare-function clear-image-cache "image.c" (&optional filter))
1697 (defun image-dired-create-thumbs (&optional arg)
1698 "Create thumbnail images for all marked files in dired.
1699 With prefix argument ARG, create thumbnails even if they already exist
1700 \(i.e. use this to refresh your thumbnails)."
1701 (interactive "P")
1702 (let (thumb-name)
1703 (dolist (curr-file (dired-get-marked-files))
1704 (setq thumb-name (image-dired-thumb-name curr-file))
1705 ;; If the user overrides the exist check, we must clear the
1706 ;; image cache so that if the user wants to display the
1707 ;; thumbnail, it is not fetched from cache.
1708 (when arg
1709 (clear-image-cache (expand-file-name thumb-name)))
1710 (when (or (not (file-exists-p thumb-name))
1711 arg)
1712 (image-dired-create-thumb curr-file thumb-name)))))
1714 (defvar image-dired-slideshow-timer nil
1715 "Slideshow timer.")
1717 (defvar image-dired-slideshow-count 0
1718 "Keeping track on number of images in slideshow.")
1720 (defvar image-dired-slideshow-times 0
1721 "Number of pictures to display in slideshow.")
1723 (defun image-dired-slideshow-step ()
1724 "Step to next file, if `image-dired-slideshow-times' has not been reached."
1725 (if (< image-dired-slideshow-count image-dired-slideshow-times)
1726 (progn
1727 (message "%s" (1+ image-dired-slideshow-count))
1728 (setq image-dired-slideshow-count (1+ image-dired-slideshow-count))
1729 (image-dired-next-line-and-display))
1730 (image-dired-slideshow-stop)))
1732 (defun image-dired-slideshow-start ()
1733 "Start slideshow.
1734 Ask user for number of images to show and the delay in between."
1735 (interactive)
1736 (setq image-dired-slideshow-count 0)
1737 (setq image-dired-slideshow-times (string-to-number (read-string "How many: ")))
1738 (let ((repeat (string-to-number
1739 (read-string
1740 "Delay, in seconds. Decimals are accepted : " "1"))))
1741 (setq image-dired-slideshow-timer
1742 (run-with-timer
1743 0 repeat
1744 'image-dired-slideshow-step))))
1746 (defun image-dired-slideshow-stop ()
1747 "Cancel slideshow."
1748 (interactive)
1749 (cancel-timer image-dired-slideshow-timer))
1751 (defun image-dired-delete-char ()
1752 "Remove current thumbnail from thumbnail buffer and line up."
1753 (interactive)
1754 (let ((inhibit-read-only t))
1755 (delete-char 1)
1756 (when (= (following-char) ?\s)
1757 (delete-char 1))))
1759 ;;;###autoload
1760 (defun image-dired-display-thumbs-append ()
1761 "Append thumbnails to `image-dired-thumbnail-buffer'."
1762 (interactive)
1763 (image-dired-display-thumbs nil t t))
1765 ;;;###autoload
1766 (defun image-dired-display-thumb ()
1767 "Shorthand for `image-dired-display-thumbs' with prefix argument."
1768 (interactive)
1769 (image-dired-display-thumbs t nil t))
1771 (defun image-dired-line-up ()
1772 "Line up thumbnails according to `image-dired-thumbs-per-row'.
1773 See also `image-dired-line-up-dynamic'."
1774 (interactive)
1775 (let ((inhibit-read-only t))
1776 (goto-char (point-min))
1777 (while (and (not (image-dired-image-at-point-p))
1778 (not (eobp)))
1779 (delete-char 1))
1780 (while (not (eobp))
1781 (forward-char)
1782 (while (and (not (image-dired-image-at-point-p))
1783 (not (eobp)))
1784 (delete-char 1)))
1785 (goto-char (point-min))
1786 (let ((count 0))
1787 (while (not (eobp))
1788 (forward-char)
1789 (if (= image-dired-thumbs-per-row 1)
1790 (insert "\n")
1791 (insert " ")
1792 (setq count (1+ count))
1793 (when (and (= count (- image-dired-thumbs-per-row 1))
1794 (not (eobp)))
1795 (forward-char)
1796 (insert "\n")
1797 (setq count 0)))))
1798 (goto-char (point-min))))
1800 (defun image-dired-line-up-dynamic ()
1801 "Line up thumbnails images dynamically.
1802 Calculate how many thumbnails fit."
1803 (interactive)
1804 (let* ((char-width (frame-char-width))
1805 (width (image-dired-window-width-pixels (image-dired-thumbnail-window)))
1806 (image-dired-thumbs-per-row
1807 (/ width
1808 (+ (* 2 image-dired-thumb-relief)
1809 (* 2 image-dired-thumb-margin)
1810 (image-dired-thumb-size 'width)
1811 char-width))))
1812 (image-dired-line-up)))
1814 (defun image-dired-line-up-interactive ()
1815 "Line up thumbnails interactively.
1816 Ask user how many thumbnails should be displayed per row."
1817 (interactive)
1818 (let ((image-dired-thumbs-per-row
1819 (string-to-number (read-string "How many thumbs per row: "))))
1820 (if (not (> image-dired-thumbs-per-row 0))
1821 (message "Number must be greater than 0")
1822 (image-dired-line-up))))
1824 (defun image-dired-thumbnail-display-external ()
1825 "Display original image for thumbnail at point using external viewer."
1826 (interactive)
1827 (let ((file (image-dired-original-file-name)))
1828 (if (not (image-dired-image-at-point-p))
1829 (message "No thumbnail at point")
1830 (if (not file)
1831 (message "No original file name found")
1832 (start-process "image-dired-thumb-external" nil
1833 image-dired-external-viewer file)))))
1835 ;;;###autoload
1836 (defun image-dired-dired-display-external ()
1837 "Display file at point using an external viewer."
1838 (interactive)
1839 (let ((file (dired-get-filename)))
1840 (start-process "image-dired-external" nil
1841 image-dired-external-viewer file)))
1843 (defun image-dired-window-width-pixels (window)
1844 "Calculate WINDOW width in pixels."
1845 (* (window-width window) (frame-char-width)))
1847 (defun image-dired-window-height-pixels (window)
1848 "Calculate WINDOW height in pixels."
1849 ;; Note: The mode-line consumes one line
1850 (* (- (window-height window) 1) (frame-char-height)))
1852 (defun image-dired-display-window ()
1853 "Return window where `image-dired-display-image-buffer' is visible."
1854 (get-window-with-predicate
1855 (lambda (window)
1856 (equal (buffer-name (window-buffer window)) image-dired-display-image-buffer))
1857 nil t))
1859 (defun image-dired-thumbnail-window ()
1860 "Return window where `image-dired-thumbnail-buffer' is visible."
1861 (get-window-with-predicate
1862 (lambda (window)
1863 (equal (buffer-name (window-buffer window)) image-dired-thumbnail-buffer))
1864 nil t))
1866 (defun image-dired-associated-dired-buffer-window ()
1867 "Return window where associated dired buffer is visible."
1868 (let (buf)
1869 (if (image-dired-image-at-point-p)
1870 (progn
1871 (setq buf (image-dired-associated-dired-buffer))
1872 (get-window-with-predicate
1873 (lambda (window)
1874 (equal (window-buffer window) buf))))
1875 (error "No thumbnail image at point"))))
1877 (defun image-dired-display-window-width (window)
1878 "Return width, in pixels, of WINDOW."
1879 (- (image-dired-window-width-pixels window)
1880 image-dired-display-window-width-correction))
1882 (defun image-dired-display-window-height (window)
1883 "Return height, in pixels, of WINDOW."
1884 (- (image-dired-window-height-pixels window)
1885 image-dired-display-window-height-correction))
1887 (defun image-dired-display-image (file &optional original-size)
1888 "Display image FILE in image buffer.
1889 Use this when you want to display the image, semi sized, in a new
1890 window. The image is sized to fit the display window (using a
1891 temporary file, don't worry). Because of this, it will not be as
1892 quick as opening it directly, but on most modern systems it
1893 should feel snappy enough.
1895 If optional argument ORIGINAL-SIZE is non-nil, display image in its
1896 original size."
1897 (image-dired--check-executable-exists
1898 'image-dired-cmd-create-temp-image-program)
1899 (let ((new-file (expand-file-name image-dired-temp-image-file))
1900 (window (image-dired-display-window))
1901 (image-type 'jpeg))
1902 (setq file (expand-file-name file))
1903 (if (not original-size)
1904 (let* ((spec
1905 (list
1906 (cons ?p image-dired-cmd-create-temp-image-program)
1907 (cons ?w (image-dired-display-window-width window))
1908 (cons ?h (image-dired-display-window-height window))
1909 (cons ?f file)
1910 (cons ?t new-file)))
1911 (ret
1912 (apply #'call-process
1913 image-dired-cmd-create-temp-image-program nil nil nil
1914 (mapcar
1915 (lambda (arg) (format-spec arg spec))
1916 image-dired-cmd-create-temp-image-options))))
1917 (when (not (zerop ret))
1918 (error "Could not resize image")))
1919 (setq image-type (image-type-from-file-name file))
1920 (copy-file file new-file t))
1921 (with-current-buffer (image-dired-create-display-image-buffer)
1922 (let ((inhibit-read-only t))
1923 (erase-buffer)
1924 (clear-image-cache)
1925 (image-dired-insert-image image-dired-temp-image-file image-type 0 0)
1926 (goto-char (point-min))
1927 (set-window-vscroll window 0)
1928 (set-window-hscroll window 0)
1929 (image-dired-update-property 'original-file-name file)))))
1931 (defun image-dired-display-thumbnail-original-image (&optional arg)
1932 "Display current thumbnail's original image in display buffer.
1933 See documentation for `image-dired-display-image' for more information.
1934 With prefix argument ARG, display image in its original size."
1935 (interactive "P")
1936 (let ((file (image-dired-original-file-name)))
1937 (if (not (string-equal major-mode "image-dired-thumbnail-mode"))
1938 (message "Not in image-dired-thumbnail-mode")
1939 (if (not (image-dired-image-at-point-p))
1940 (message "No thumbnail at point")
1941 (if (not file)
1942 (message "No original file name found")
1943 (image-dired-create-display-image-buffer)
1944 (display-buffer image-dired-display-image-buffer)
1945 (image-dired-display-image file arg))))))
1948 ;;;###autoload
1949 (defun image-dired-dired-display-image (&optional arg)
1950 "Display current image file.
1951 See documentation for `image-dired-display-image' for more information.
1952 With prefix argument ARG, display image in its original size."
1953 (interactive "P")
1954 (image-dired-create-display-image-buffer)
1955 (display-buffer image-dired-display-image-buffer)
1956 (image-dired-display-image (dired-get-filename) arg))
1958 (defun image-dired-image-at-point-p ()
1959 "Return true if there is an image-dired thumbnail at point."
1960 (get-text-property (point) 'image-dired-thumbnail))
1962 (defun image-dired-rotate-thumbnail (degrees)
1963 "Rotate thumbnail DEGREES degrees."
1964 (image-dired--check-executable-exists
1965 'image-dired-cmd-rotate-thumbnail-program)
1966 (if (not (image-dired-image-at-point-p))
1967 (message "No thumbnail at point")
1968 (let* ((file (image-dired-thumb-name (image-dired-original-file-name)))
1969 (thumb (expand-file-name file))
1970 (spec (list (cons ?d degrees) (cons ?t thumb))))
1971 (apply #'call-process image-dired-cmd-rotate-thumbnail-program nil nil nil
1972 (mapcar (lambda (arg) (format-spec arg spec))
1973 image-dired-cmd-rotate-thumbnail-options))
1974 (clear-image-cache thumb))))
1976 (defun image-dired-rotate-thumbnail-left ()
1977 "Rotate thumbnail left (counter clockwise) 90 degrees.
1978 The result of the rotation is displayed in the image display area
1979 and a confirmation is needed before the original image files is
1980 overwritten. This confirmation can be turned off using
1981 `image-dired-rotate-original-ask-before-overwrite'."
1982 (interactive)
1983 (image-dired-rotate-thumbnail "270"))
1985 (defun image-dired-rotate-thumbnail-right ()
1986 "Rotate thumbnail counter right (clockwise) 90 degrees.
1987 The result of the rotation is displayed in the image display area
1988 and a confirmation is needed before the original image files is
1989 overwritten. This confirmation can be turned off using
1990 `image-dired-rotate-original-ask-before-overwrite'."
1991 (interactive)
1992 (image-dired-rotate-thumbnail "90"))
1994 (defun image-dired-refresh-thumb ()
1995 "Force creation of new image for current thumbnail."
1996 (interactive)
1997 (let* ((file (image-dired-original-file-name))
1998 (thumb (expand-file-name (image-dired-thumb-name file))))
1999 (clear-image-cache (expand-file-name thumb))
2000 (image-dired-create-thumb file thumb)))
2002 (defun image-dired-rotate-original (degrees)
2003 "Rotate original image DEGREES degrees."
2004 (image-dired--check-executable-exists
2005 'image-dired-cmd-rotate-original-program)
2006 (if (not (image-dired-image-at-point-p))
2007 (message "No image at point")
2008 (let* ((file (image-dired-original-file-name))
2009 (spec
2010 (list
2011 (cons ?d degrees)
2012 (cons ?o (expand-file-name file))
2013 (cons ?t image-dired-temp-rotate-image-file))))
2014 (unless (eq 'jpeg (image-type file))
2015 (error "Only JPEG images can be rotated!"))
2016 (if (not (= 0 (apply #'call-process image-dired-cmd-rotate-original-program
2017 nil nil nil
2018 (mapcar (lambda (arg) (format-spec arg spec))
2019 image-dired-cmd-rotate-original-options))))
2020 (error "Could not rotate image")
2021 (image-dired-display-image image-dired-temp-rotate-image-file)
2022 (if (or (and image-dired-rotate-original-ask-before-overwrite
2023 (y-or-n-p
2024 "Rotate to temp file OK. Overwrite original image? "))
2025 (not image-dired-rotate-original-ask-before-overwrite))
2026 (progn
2027 (copy-file image-dired-temp-rotate-image-file file t)
2028 (image-dired-refresh-thumb))
2029 (image-dired-display-image file))))))
2031 (defun image-dired-rotate-original-left ()
2032 "Rotate original image left (counter clockwise) 90 degrees."
2033 (interactive)
2034 (image-dired-rotate-original "270"))
2036 (defun image-dired-rotate-original-right ()
2037 "Rotate original image right (clockwise) 90 degrees."
2038 (interactive)
2039 (image-dired-rotate-original "90"))
2041 (defun image-dired-get-exif-file-name (file)
2042 "Use the image's EXIF information to return a unique file name.
2043 The file name should be unique as long as you do not take more than
2044 one picture per second. The original file name is suffixed at the end
2045 for traceability. The format of the returned file name is
2046 YYYY_MM_DD_HH_MM_DD_ORIG_FILE_NAME.jpg. Used from
2047 `image-dired-copy-with-exif-file-name'."
2048 (let (data no-exif-data-found)
2049 (if (not (eq 'jpeg (image-type (expand-file-name file))))
2050 (setq no-exif-data-found t
2051 data (format-time-string
2052 "%Y:%m:%d %H:%M:%S"
2053 (file-attribute-modification-time
2054 (file-attributes (expand-file-name file)))))
2055 (setq data (image-dired-get-exif-data (expand-file-name file)
2056 "DateTimeOriginal")))
2057 (while (string-match "[ :]" data)
2058 (setq data (replace-match "_" nil nil data)))
2059 (format "%s%s%s" data
2060 (if no-exif-data-found
2061 "_noexif_"
2062 "_")
2063 (file-name-nondirectory file))))
2065 (defun image-dired-thumbnail-set-image-description ()
2066 "Set the ImageDescription EXIF tag for the original image.
2067 If the image already has a value for this tag, it is used as the
2068 default value at the prompt."
2069 (interactive)
2070 (if (not (image-dired-image-at-point-p))
2071 (message "No thumbnail at point")
2072 (let* ((file (image-dired-original-file-name))
2073 (old-value (image-dired-get-exif-data file "ImageDescription")))
2074 (if (eq 0
2075 (image-dired-set-exif-data file "ImageDescription"
2076 (read-string "Value of ImageDescription: "
2077 old-value)))
2078 (message "Successfully wrote ImageDescription tag.")
2079 (error "Could not write ImageDescription tag")))))
2081 (defun image-dired-set-exif-data (file tag-name tag-value)
2082 "In FILE, set EXIF tag TAG-NAME to value TAG-VALUE."
2083 (image-dired--check-executable-exists
2084 'image-dired-cmd-write-exif-data-program)
2085 (let ((spec
2086 (list
2087 (cons ?f (expand-file-name file))
2088 (cons ?t tag-name)
2089 (cons ?v tag-value))))
2090 (apply #'call-process image-dired-cmd-write-exif-data-program nil nil nil
2091 (mapcar (lambda (arg) (format-spec arg spec))
2092 image-dired-cmd-write-exif-data-options))))
2094 (defun image-dired-get-exif-data (file tag-name)
2095 "From FILE, return EXIF tag TAG-NAME."
2096 (image-dired--check-executable-exists
2097 'image-dired-cmd-read-exif-data-program)
2098 (let ((buf (get-buffer-create "*image-dired-get-exif-data*"))
2099 (spec (list (cons ?f file) (cons ?t tag-name)))
2100 tag-value)
2101 (with-current-buffer buf
2102 (delete-region (point-min) (point-max))
2103 (if (not (eq (apply #'call-process image-dired-cmd-read-exif-data-program
2104 nil t nil
2105 (mapcar
2106 (lambda (arg) (format-spec arg spec))
2107 image-dired-cmd-read-exif-data-options))
2109 (error "Could not get EXIF tag")
2110 (goto-char (point-min))
2111 ;; Clean buffer from newlines and carriage returns before
2112 ;; getting final info
2113 (while (search-forward-regexp "[\n\r]" nil t)
2114 (replace-match "" nil t))
2115 (setq tag-value (buffer-substring (point-min) (point-max)))))
2116 tag-value))
2118 (defun image-dired-copy-with-exif-file-name ()
2119 "Copy file with unique name to main image directory.
2120 Copy current or all marked files in dired to a new file in your
2121 main image directory, using a file name generated by
2122 `image-dired-get-exif-file-name'. A typical usage for this if when
2123 copying images from a digital camera into the image directory.
2125 Typically, you would open up the folder with the incoming
2126 digital images, mark the files to be copied, and execute this
2127 function. The result is a couple of new files in
2128 `image-dired-main-image-directory' called
2129 2005_05_08_12_52_00_dscn0319.jpg,
2130 2005_05_08_14_27_45_dscn0320.jpg etc."
2131 (interactive)
2132 (let (new-name
2133 (files (dired-get-marked-files)))
2134 (mapc
2135 (lambda (curr-file)
2136 (setq new-name
2137 (format "%s/%s"
2138 (file-name-as-directory
2139 (expand-file-name image-dired-main-image-directory))
2140 (image-dired-get-exif-file-name curr-file)))
2141 (message "Copying %s to %s" curr-file new-name)
2142 (copy-file curr-file new-name))
2143 files)))
2145 (defun image-dired-display-next-thumbnail-original ()
2146 "In thumbnail buffer, move to next thumbnail and display the image."
2147 (interactive)
2148 (image-dired-forward-image)
2149 (image-dired-display-thumbnail-original-image))
2151 (defun image-dired-display-previous-thumbnail-original ()
2152 "Move to previous thumbnail and display image."
2153 (interactive)
2154 (image-dired-backward-image)
2155 (image-dired-display-thumbnail-original-image))
2157 (defun image-dired-write-comments (file-comments)
2158 "Write file comments to database.
2159 Write file comments to one or more files.
2160 FILE-COMMENTS is an alist on the following form:
2161 ((FILE . COMMENT) ... )"
2162 (image-dired-sane-db-file)
2163 (let (end comment-beg-pos comment-end-pos file comment)
2164 (image-dired--with-db-file
2165 (setq buffer-file-name image-dired-db-file)
2166 (dolist (elt file-comments)
2167 (setq file (car elt)
2168 comment (cdr elt))
2169 (goto-char (point-min))
2170 (if (search-forward-regexp (format "^%s.*$" file) nil t)
2171 (progn
2172 (setq end (point))
2173 (beginning-of-line)
2174 ;; Delete old comment, if any
2175 (when (search-forward ";comment:" end t)
2176 (setq comment-beg-pos (match-beginning 0))
2177 ;; Any tags after the comment?
2178 (if (search-forward ";" end t)
2179 (setq comment-end-pos (- (point) 1))
2180 (setq comment-end-pos end))
2181 ;; Delete comment tag and comment
2182 (delete-region comment-beg-pos comment-end-pos))
2183 ;; Insert new comment
2184 (beginning-of-line)
2185 (unless (search-forward ";" end t)
2186 (end-of-line)
2187 (insert ";"))
2188 (insert (format "comment:%s;" comment)))
2189 ;; File does not exist in database - add it.
2190 (goto-char (point-max))
2191 (insert (format "\n%s;comment:%s" file comment))))
2192 (save-buffer))))
2194 (defun image-dired-update-property (prop value)
2195 "Update text property PROP with value VALUE at point."
2196 (let ((inhibit-read-only t))
2197 (put-text-property
2198 (point) (1+ (point))
2199 prop
2200 value)))
2202 ;;;###autoload
2203 (defun image-dired-dired-comment-files ()
2204 "Add comment to current or marked files in dired."
2205 (interactive)
2206 (let ((comment (image-dired-read-comment)))
2207 (image-dired-write-comments
2208 (mapcar
2209 (lambda (curr-file)
2210 (cons curr-file comment))
2211 (dired-get-marked-files)))))
2213 (defun image-dired-comment-thumbnail ()
2214 "Add comment to current thumbnail in thumbnail buffer."
2215 (interactive)
2216 (let* ((file (image-dired-original-file-name))
2217 (comment (image-dired-read-comment file)))
2218 (image-dired-write-comments (list (cons file comment)))
2219 (image-dired-update-property 'comment comment))
2220 (image-dired-display-thumb-properties))
2222 (defun image-dired-read-comment (&optional file)
2223 "Read comment for an image.
2224 Optionally use old comment from FILE as initial value."
2225 (let ((comment
2226 (read-string
2227 "Comment: "
2228 (if file (image-dired-get-comment file)))))
2229 comment))
2231 (defun image-dired-get-comment (file)
2232 "Get comment for file FILE."
2233 (image-dired-sane-db-file)
2234 (image-dired--with-db-file
2235 (let (end comment-beg-pos comment-end-pos comment)
2236 (when (search-forward-regexp (format "^%s" file) nil t)
2237 (end-of-line)
2238 (setq end (point))
2239 (beginning-of-line)
2240 (when (search-forward ";comment:" end t)
2241 (setq comment-beg-pos (point))
2242 (if (search-forward ";" end t)
2243 (setq comment-end-pos (- (point) 1))
2244 (setq comment-end-pos end))
2245 (setq comment (buffer-substring
2246 comment-beg-pos comment-end-pos))))
2247 comment)))
2249 ;;;###autoload
2250 (defun image-dired-mark-tagged-files ()
2251 "Use regexp to mark files with matching tag.
2252 A `tag' is a keyword, a piece of meta data, associated with an
2253 image file and stored in image-dired's database file. This command
2254 lets you input a regexp and this will be matched against all tags
2255 on all image files in the database file. The files that have a
2256 matching tag will be marked in the dired buffer."
2257 (interactive)
2258 (image-dired-sane-db-file)
2259 (let ((tag (read-string "Mark tagged files (regexp): "))
2260 (hits 0)
2261 files)
2262 (image-dired--with-db-file
2263 ;; Collect matches
2264 (while (search-forward-regexp
2265 (concat "\\(^[^;\n]+\\);.*" tag ".*$") nil t)
2266 (push (match-string 1) files)))
2267 ;; Mark files
2268 (dolist (curr-file files)
2269 ;; I tried using `dired-mark-files-regexp' but it was waaaay to
2270 ;; slow. Don't bother about hits found in other directories
2271 ;; than the current one.
2272 (when (string= (file-name-as-directory
2273 (expand-file-name default-directory))
2274 (file-name-as-directory
2275 (file-name-directory curr-file)))
2276 (setq curr-file (file-name-nondirectory curr-file))
2277 (goto-char (point-min))
2278 (when (search-forward-regexp (format "\\s %s$" curr-file) nil t)
2279 (setq hits (+ hits 1))
2280 (dired-mark 1))))
2281 (message "%d files with matching tag marked." hits)))
2283 (defun image-dired-mouse-display-image (event)
2284 "Use mouse EVENT, call `image-dired-display-image' to display image.
2285 Track this in associated dired buffer if `image-dired-track-movement' is
2286 non-nil."
2287 (interactive "e")
2288 (mouse-set-point event)
2289 (goto-char (posn-point (event-end event)))
2290 (let ((file (image-dired-original-file-name)))
2291 (when file
2292 (if image-dired-track-movement
2293 (image-dired-track-original-file))
2294 (image-dired-create-display-image-buffer)
2295 (display-buffer image-dired-display-image-buffer)
2296 (image-dired-display-image file))))
2298 (defun image-dired-mouse-select-thumbnail (event)
2299 "Use mouse EVENT to select thumbnail image.
2300 Track this in associated dired buffer if `image-dired-track-movement' is
2301 non-nil."
2302 (interactive "e")
2303 (mouse-set-point event)
2304 (goto-char (posn-point (event-end event)))
2305 (if image-dired-track-movement
2306 (image-dired-track-original-file))
2307 (image-dired-display-thumb-properties))
2309 (defun image-dired-mouse-toggle-mark (event)
2310 "Use mouse EVENT to toggle dired mark for thumbnail.
2311 Track this in associated dired buffer if `image-dired-track-movement' is
2312 non-nil."
2313 (interactive "e")
2314 (mouse-set-point event)
2315 (goto-char (posn-point (event-end event)))
2316 (if image-dired-track-movement
2317 (image-dired-track-original-file))
2318 (image-dired-toggle-mark-thumb-original-file))
2320 (defun image-dired-dired-display-properties ()
2321 "Display properties for dired file in the echo area."
2322 (interactive)
2323 (let* ((file (dired-get-filename))
2324 (file-name (file-name-nondirectory file))
2325 (dired-buf (buffer-name (current-buffer)))
2326 (props (mapconcat #'identity (image-dired-list-tags file) ", "))
2327 (comment (image-dired-get-comment file))
2328 (message-log-max nil))
2329 (if file-name
2330 (message "%s"
2331 (image-dired-format-properties-string
2332 dired-buf
2333 file-name
2334 props
2335 comment)))))
2337 (defvar image-dired-tag-file-list nil
2338 "List to store tag-file structure.")
2340 (defvar image-dired-file-tag-list nil
2341 "List to store file-tag structure.")
2343 (defvar image-dired-file-comment-list nil
2344 "List to store file comments.")
2346 (defun image-dired-add-to-tag-file-list (tag file)
2347 "Add relation between TAG and FILE."
2348 (let (curr)
2349 (if image-dired-tag-file-list
2350 (if (setq curr (assoc tag image-dired-tag-file-list))
2351 (if (not (member file curr))
2352 (setcdr curr (cons file (cdr curr))))
2353 (setcdr image-dired-tag-file-list
2354 (cons (list tag file) (cdr image-dired-tag-file-list))))
2355 (setq image-dired-tag-file-list (list (list tag file))))))
2357 (defun image-dired-add-to-tag-file-lists (tag file)
2358 "Helper function used from `image-dired-create-gallery-lists'.
2360 Add TAG to FILE in one list and FILE to TAG in the other.
2362 Lisp structures look like the following:
2364 image-dired-file-tag-list:
2366 ((\"filename1\" \"tag1\" \"tag2\" \"tag3\" ...)
2367 (\"filename2\" \"tag1\" \"tag2\" \"tag3\" ...)
2368 ...)
2370 image-dired-tag-file-list:
2372 ((\"tag1\" \"filename1\" \"filename2\" \"filename3\" ...)
2373 (\"tag2\" \"filename1\" \"filename2\" \"filename3\" ...)
2374 ...)"
2375 ;; Add tag to file list
2376 (let (curr)
2377 (if image-dired-file-tag-list
2378 (if (setq curr (assoc file image-dired-file-tag-list))
2379 (setcdr curr (cons tag (cdr curr)))
2380 (setcdr image-dired-file-tag-list
2381 (cons (list file tag) (cdr image-dired-file-tag-list))))
2382 (setq image-dired-file-tag-list (list (list file tag))))
2383 ;; Add file to tag list
2384 (if image-dired-tag-file-list
2385 (if (setq curr (assoc tag image-dired-tag-file-list))
2386 (if (not (member file curr))
2387 (setcdr curr (cons file (cdr curr))))
2388 (setcdr image-dired-tag-file-list
2389 (cons (list tag file) (cdr image-dired-tag-file-list))))
2390 (setq image-dired-tag-file-list (list (list tag file))))))
2392 (defun image-dired-add-to-file-comment-list (file comment)
2393 "Helper function used from `image-dired-create-gallery-lists'.
2395 For FILE, add COMMENT to list.
2397 Lisp structure looks like the following:
2399 image-dired-file-comment-list:
2401 ((\"filename1\" . \"comment1\")
2402 (\"filename2\" . \"comment2\")
2403 ...)"
2404 (if image-dired-file-comment-list
2405 (if (not (assoc file image-dired-file-comment-list))
2406 (setcdr image-dired-file-comment-list
2407 (cons (cons file comment)
2408 (cdr image-dired-file-comment-list))))
2409 (setq image-dired-file-comment-list (list (cons file comment)))))
2411 (defun image-dired-create-gallery-lists ()
2412 "Create temporary lists used by `image-dired-gallery-generate'."
2413 (image-dired-sane-db-file)
2414 (image-dired--with-db-file
2415 (let (end beg file row-tags)
2416 (setq image-dired-tag-file-list nil)
2417 (setq image-dired-file-tag-list nil)
2418 (setq image-dired-file-comment-list nil)
2419 (goto-char (point-min))
2420 (while (search-forward-regexp "^." nil t)
2421 (end-of-line)
2422 (setq end (point))
2423 (beginning-of-line)
2424 (setq beg (point))
2425 (unless (search-forward ";" end nil)
2426 (error "Something is really wrong, check format of database"))
2427 (setq row-tags (split-string
2428 (buffer-substring beg end) ";"))
2429 (setq file (car row-tags))
2430 (dolist (x (cdr row-tags))
2431 (if (not (string-match "^comment:\\(.*\\)" x))
2432 (image-dired-add-to-tag-file-lists x file)
2433 (image-dired-add-to-file-comment-list file (match-string 1 x)))))))
2434 ;; Sort tag-file list
2435 (setq image-dired-tag-file-list
2436 (sort image-dired-tag-file-list
2437 (lambda (x y)
2438 (string< (car x) (car y))))))
2440 (defun image-dired-hidden-p (file)
2441 "Return t if image FILE has a \"hidden\" tag."
2442 (cl-loop for tag in (cdr (assoc file image-dired-file-tag-list))
2443 if (member tag image-dired-gallery-hidden-tags) return t))
2445 (defun image-dired-gallery-generate ()
2446 "Generate gallery pages.
2447 First we create a couple of Lisp structures from the database to make
2448 it easier to generate, then HTML-files are created in
2449 `image-dired-gallery-dir'."
2450 (interactive)
2451 (if (eq 'per-directory image-dired-thumbnail-storage)
2452 (error "Currently, gallery generation is not supported \
2453 when using per-directory thumbnail file storage"))
2454 (image-dired-create-gallery-lists)
2455 (let ((tags image-dired-tag-file-list)
2456 (index-file (format "%s/index.html" image-dired-gallery-dir))
2457 count tag tag-file
2458 comment file-tags tag-link tag-link-list)
2459 ;; Make sure gallery root exist
2460 (if (file-exists-p image-dired-gallery-dir)
2461 (if (not (file-directory-p image-dired-gallery-dir))
2462 (error "Variable image-dired-gallery-dir is not a directory"))
2463 (make-directory image-dired-gallery-dir))
2464 ;; Open index file
2465 (with-temp-file index-file
2466 (if (file-exists-p index-file)
2467 (insert-file-contents index-file))
2468 (insert "<html>\n")
2469 (insert " <body>\n")
2470 (insert " <h2>Image-Dired Gallery</h2>\n")
2471 (insert (format "<p>\n Gallery generated %s\n <p>\n"
2472 (current-time-string)))
2473 (insert " <h3>Tag index</h3>\n")
2474 (setq count 1)
2475 ;; Pre-generate list of all tag links
2476 (dolist (curr tags)
2477 (setq tag (car curr))
2478 (when (not (member tag image-dired-gallery-hidden-tags))
2479 (setq tag-link (format "<a href=\"%d.html\">%s</a>" count tag))
2480 (if tag-link-list
2481 (setq tag-link-list
2482 (append tag-link-list (list (cons tag tag-link))))
2483 (setq tag-link-list (list (cons tag tag-link))))
2484 (setq count (1+ count))))
2485 (setq count 1)
2486 ;; Main loop where we generated thumbnail pages per tag
2487 (dolist (curr tags)
2488 (setq tag (car curr))
2489 ;; Don't display hidden tags
2490 (when (not (member tag image-dired-gallery-hidden-tags))
2491 ;; Insert link to tag page in index
2492 (insert (format " %s<br>\n" (cdr (assoc tag tag-link-list))))
2493 ;; Open per-tag file
2494 (setq tag-file (format "%s/%s.html" image-dired-gallery-dir count))
2495 (with-temp-file tag-file
2496 (if (file-exists-p tag-file)
2497 (insert-file-contents tag-file))
2498 (erase-buffer)
2499 (insert "<html>\n")
2500 (insert " <body>\n")
2501 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2502 (insert (format " <h2>Images with tag &quot;%s&quot;</h2>" tag))
2503 ;; Main loop for files per tag page
2504 (dolist (file (cdr curr))
2505 (unless (image-dired-hidden-p file)
2506 ;; Insert thumbnail with link to full image
2507 (insert
2508 (format "<a href=\"%s/%s\"><img src=\"%s/%s\"%s></a>\n"
2509 image-dired-gallery-image-root-url
2510 (file-name-nondirectory file)
2511 image-dired-gallery-thumb-image-root-url
2512 (file-name-nondirectory (image-dired-thumb-name file)) file))
2513 ;; Insert comment, if any
2514 (if (setq comment (cdr (assoc file image-dired-file-comment-list)))
2515 (insert (format "<br>\n%s<br>\n" comment))
2516 (insert "<br>\n"))
2517 ;; Insert links to other tags, if any
2518 (when (> (length
2519 (setq file-tags (assoc file image-dired-file-tag-list))) 2)
2520 (insert "[ ")
2521 (dolist (extra-tag file-tags)
2522 ;; Only insert if not file name or the main tag
2523 (if (and (not (equal extra-tag tag))
2524 (not (equal extra-tag file)))
2525 (insert
2526 (format "%s " (cdr (assoc extra-tag tag-link-list))))))
2527 (insert "]<br>\n"))))
2528 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2529 (insert " </body>\n")
2530 (insert "</html>\n"))
2531 (setq count (1+ count))))
2532 (insert " </body>\n")
2533 (insert "</html>"))))
2535 (defvar image-dired-widget-list nil
2536 "List to keep track of meta data in edit buffer.")
2538 (declare-function widget-forward "wid-edit" (arg))
2540 ;;;###autoload
2541 (defun image-dired-dired-edit-comment-and-tags ()
2542 "Edit comment and tags of current or marked image files.
2543 Edit comment and tags for all marked image files in an
2544 easy-to-use form."
2545 (interactive)
2546 (setq image-dired-widget-list nil)
2547 ;; Setup buffer.
2548 (let ((files (dired-get-marked-files)))
2549 (pop-to-buffer-same-window "*Image-Dired Edit Meta Data*")
2550 (kill-all-local-variables)
2551 (make-local-variable 'widget-example-repeat)
2552 (let ((inhibit-read-only t))
2553 (erase-buffer))
2554 (remove-overlays)
2555 ;; Some help for the user.
2556 (widget-insert
2557 "\nEdit comments and tags for each image. Separate multiple tags
2558 with a comma. Move forward between fields using TAB or RET.
2559 Move to the previous field using backtab (S-TAB). Save by
2560 activating the Save button at the bottom of the form or cancel
2561 the operation by activating the Cancel button.\n\n")
2562 ;; Here comes all images and a comment and tag field for each
2563 ;; image.
2564 (let (thumb-file img comment-widget tag-widget)
2566 (dolist (file files)
2568 (setq thumb-file (image-dired-thumb-name file)
2569 img (create-image thumb-file))
2571 (insert-image img)
2572 (widget-insert "\n\nComment: ")
2573 (setq comment-widget
2574 (widget-create 'editable-field
2575 :size 60
2576 :format "%v "
2577 :value (or (image-dired-get-comment file) "")))
2578 (widget-insert "\nTags: ")
2579 (setq tag-widget
2580 (widget-create 'editable-field
2581 :size 60
2582 :format "%v "
2583 :value (or (mapconcat
2584 #'identity
2585 (image-dired-list-tags file)
2586 ",") "")))
2587 ;; Save information in all widgets so that we can use it when
2588 ;; the user saves the form.
2589 (setq image-dired-widget-list
2590 (append image-dired-widget-list
2591 (list (list file comment-widget tag-widget))))
2592 (widget-insert "\n\n")))
2594 ;; Footer with Save and Cancel button.
2595 (widget-insert "\n")
2596 (widget-create 'push-button
2597 :notify
2598 (lambda (&rest _ignore)
2599 (image-dired-save-information-from-widgets)
2600 (bury-buffer)
2601 (message "Done."))
2602 "Save")
2603 (widget-insert " ")
2604 (widget-create 'push-button
2605 :notify
2606 (lambda (&rest _ignore)
2607 (bury-buffer)
2608 (message "Operation canceled."))
2609 "Cancel")
2610 (widget-insert "\n")
2611 (use-local-map widget-keymap)
2612 (widget-setup)
2613 ;; Jump to the first widget.
2614 (widget-forward 1)))
2616 (defun image-dired-save-information-from-widgets ()
2617 "Save information found in `image-dired-widget-list'.
2618 Use the information in `image-dired-widget-list' to save comments and
2619 tags to their respective image file. Internal function used by
2620 `image-dired-dired-edit-comment-and-tags'."
2621 (let (file comment tag-string tag-list lst)
2622 (image-dired-write-comments
2623 (mapcar
2624 (lambda (widget)
2625 (setq file (car widget)
2626 comment (widget-value (cadr widget)))
2627 (cons file comment))
2628 image-dired-widget-list))
2629 (image-dired-write-tags
2630 (dolist (widget image-dired-widget-list lst)
2631 (setq file (car widget)
2632 tag-string (widget-value (car (cddr widget)))
2633 tag-list (split-string tag-string ","))
2634 (dolist (tag tag-list)
2635 (push (cons file tag) lst))))))
2637 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2638 ;;;;;;;;; TEST-SECTION ;;;;;;;;;;;
2639 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2641 ;; (defvar image-dired-dir-max-size 12300000)
2643 ;; (defun image-dired-test-clean-old-files ()
2644 ;; "Clean `image-dired-dir' from old thumbnail files.
2645 ;; \"Oldness\" measured using last access time. If the total size of all
2646 ;; thumbnail files in `image-dired-dir' is larger than 'image-dired-dir-max-size',
2647 ;; old files are deleted until the max size is reached."
2648 ;; (let* ((files
2649 ;; (sort
2650 ;; (mapcar
2651 ;; (lambda (f)
2652 ;; (let ((fattribs (file-attributes f)))
2653 ;; ;; Get last access time and file size
2654 ;; `(,(nth 4 fattribs) ,(nth 7 fattribs) ,f)))
2655 ;; (directory-files (image-dired-dir) t ".+\\.thumb\\..+$"))
2656 ;; ;; Sort function. Compare time between two files.
2657 ;; (lambda (l1 l2)
2658 ;; (time-less-p (car l1) (car l2)))))
2659 ;; (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) files))))
2660 ;; (while (> dirsize image-dired-dir-max-size)
2661 ;; (y-or-n-p
2662 ;; (format "Size of thumbnail directory: %d, delete old file %s? "
2663 ;; dirsize (cadr (cdar files))))
2664 ;; (delete-file (cadr (cdar files)))
2665 ;; (setq dirsize (- dirsize (car (cdar files))))
2666 ;; (setq files (cdr files)))))
2668 ;;;;;;;;;;;;;;;;;;;;;;,
2670 ;; (defun dired-speedbar-buttons (dired-buffer)
2671 ;; (when (and (boundp 'image-dired-use-speedbar)
2672 ;; image-dired-use-speedbar)
2673 ;; (let ((filename (with-current-buffer dired-buffer
2674 ;; (dired-get-filename))))
2675 ;; (when (and (not (string-equal filename (buffer-string)))
2676 ;; (string-match (image-file-name-regexp) filename))
2677 ;; (erase-buffer)
2678 ;; (insert (propertize
2679 ;; filename
2680 ;; 'display
2681 ;; (image-dired-get-thumbnail-image filename)))))))
2683 ;; (setq image-dired-use-speedbar t)
2685 (provide 'image-dired)
2687 ;;; image-dired.el ends here