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