Port recent MAX_RW_COUNT checks to POSIX
[emacs.git] / lisp / image-dired.el
blob1acb31928b4abfa8f818fae175c7dd79327915f2
1 ;;; image-dired.el --- use dired to browse and manipulate your images -*- lexical-binding: t -*-
2 ;;
3 ;; Copyright (C) 2005-2018 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 (nth 5 thumb-attr)
591 (nth 5 (file-attributes file))))
592 (image-dired-create-thumb file thumb-file))
593 (create-image thumb-file)
594 ;; (list 'image :type 'jpeg
595 ;; :file thumb-file
596 ;; :relief image-dired-thumb-relief :margin image-dired-thumb-margin)
599 (defun image-dired-insert-thumbnail (file original-file-name
600 associated-dired-buffer)
601 "Insert thumbnail image FILE.
602 Add text properties ORIGINAL-FILE-NAME and ASSOCIATED-DIRED-BUFFER."
603 (let (beg end)
604 (setq beg (point))
605 (image-dired-insert-image file
606 ;; TODO: this should depend on the real file type
607 (if (memq image-dired-thumbnail-storage
608 '(standard standard-large))
609 'png 'jpeg)
610 image-dired-thumb-relief
611 image-dired-thumb-margin)
612 (setq end (point))
613 (add-text-properties
614 beg end
615 (list 'image-dired-thumbnail t
616 'original-file-name original-file-name
617 'associated-dired-buffer associated-dired-buffer
618 'tags (image-dired-list-tags original-file-name)
619 'mouse-face 'highlight
620 'comment (image-dired-get-comment original-file-name)))))
622 (defun image-dired-thumb-name (file)
623 "Return thumbnail file name for FILE.
624 Depending on the value of `image-dired-thumbnail-storage', the file
625 name will vary. For central thumbnail file storage, make a
626 MD5-hash of the image file's directory name and add that to make
627 the thumbnail file name unique. For per-directory storage, just
628 add a subdirectory. For standard storage, produce the file name
629 according to the Thumbnail Managing Standard."
630 (cond ((memq image-dired-thumbnail-storage '(standard standard-large))
631 (let* ((xdg (getenv "XDG_CACHE_HOME"))
632 (dir (if (and xdg (file-name-absolute-p xdg))
633 xdg "~/.cache"))
634 (thumbdir (cl-case image-dired-thumbnail-storage
635 (standard "thumbnails/normal")
636 (standard-large "thumbnails/large"))))
637 (expand-file-name
638 (concat (md5 (concat "file://" (expand-file-name file))) ".png")
639 (expand-file-name thumbdir dir))))
640 ((eq 'use-image-dired-dir image-dired-thumbnail-storage)
641 (let* ((f (expand-file-name file))
642 (md5-hash
643 ;; Is MD5 hashes fast enough? The checksum of a
644 ;; thumbnail file name need not be that
645 ;; "cryptographically" good so a faster one could
646 ;; be used here.
647 (md5 (file-name-as-directory (file-name-directory f)))))
648 (format "%s%s%s.thumb.%s"
649 (file-name-as-directory (expand-file-name (image-dired-dir)))
650 (file-name-base f)
651 (if md5-hash (concat "_" md5-hash) "")
652 (file-name-extension f))))
653 ((eq 'per-directory image-dired-thumbnail-storage)
654 (let ((f (expand-file-name file)))
655 (format "%s.image-dired/%s.thumb.%s"
656 (file-name-directory f)
657 (file-name-base f)
658 (file-name-extension f))))))
660 (defun image-dired--check-executable-exists (executable)
661 (unless (executable-find (symbol-value executable))
662 (error "Executable %S not found" executable)))
664 (defun image-dired-thumb-size (dimension)
665 "Return thumb size depending on `image-dired-thumbnail-storage'.
666 DIMENSION should be either the symbol 'width or 'height."
667 (cond
668 ((eq 'standard image-dired-thumbnail-storage) 128)
669 ((eq 'standard-large image-dired-thumbnail-storage) 256)
670 (t (cl-ecase dimension
671 (width image-dired-thumb-width)
672 (height image-dired-thumb-height)))))
674 (defvar image-dired-queue nil
675 "List of items in the queue.
676 Each item has the form (ORIGINAL-FILE TARGET-FILE).")
678 (defvar image-dired-queue-active-jobs 0
679 "Number of active jobs in `image-dired-queue'.")
681 (defvar image-dired-queue-active-limit 2
682 "Maximum number of concurrent jobs permitted for generating images.
683 Increase at own risk.")
685 (defun image-dired-pngnq-thumb (spec)
686 "Quantize thumbnail described by format SPEC with pngnq(1)."
687 (let ((process
688 (apply #'start-process "image-dired-pngnq" nil
689 image-dired-cmd-pngnq-program
690 (mapcar (lambda (arg) (format-spec arg spec))
691 image-dired-cmd-pngnq-options))))
692 (setf (process-sentinel process)
693 (lambda (process status)
694 (if (and (eq (process-status process) 'exit)
695 (zerop (process-exit-status process)))
696 ;; Pass off to pngcrush, or just rename the
697 ;; THUMB-nq8.png file back to THUMB.png
698 (if (and image-dired-cmd-pngcrush-program
699 (executable-find image-dired-cmd-pngcrush-program))
700 (image-dired-pngcrush-thumb spec)
701 (let ((nq8 (cdr (assq ?q spec)))
702 (thumb (cdr (assq ?t spec))))
703 (rename-file nq8 thumb t)))
704 (message "command %S %s" (process-command process)
705 (replace-regexp-in-string "\n" "" status)))))
706 process))
708 (defun image-dired-pngcrush-thumb (spec)
709 "Optimize thumbnail described by format SPEC with pngcrush(1)."
710 ;; If pngnq wasn't run, then the THUMB-nq8.png file does not exist.
711 ;; pngcrush needs an infile and outfile, so we just copy THUMB to
712 ;; THUMB-nq8.png and use the latter as a temp file.
713 (when (not image-dired-cmd-pngnq-program)
714 (let ((temp (cdr (assq ?q spec)))
715 (thumb (cdr (assq ?t spec))))
716 (copy-file thumb temp)))
717 (let ((process
718 (apply #'start-process "image-dired-pngcrush" nil
719 image-dired-cmd-pngcrush-program
720 (mapcar (lambda (arg) (format-spec arg spec))
721 image-dired-cmd-pngcrush-options))))
722 (setf (process-sentinel process)
723 (lambda (process status)
724 (unless (and (eq (process-status process) 'exit)
725 (zerop (process-exit-status process)))
726 (message "command %S %s" (process-command process)
727 (replace-regexp-in-string "\n" "" status)))
728 (when (memq (process-status process) '(exit signal))
729 (let ((temp (cdr (assq ?q spec))))
730 (delete-file temp)))))
731 process))
733 (defun image-dired-optipng-thumb (spec)
734 "Optimize thumbnail described by format SPEC with optipng(1)."
735 (let ((process
736 (apply #'start-process "image-dired-optipng" nil
737 image-dired-cmd-optipng-program
738 (mapcar (lambda (arg) (format-spec arg spec))
739 image-dired-cmd-optipng-options))))
740 (setf (process-sentinel process)
741 (lambda (process status)
742 (unless (and (eq (process-status process) 'exit)
743 (zerop (process-exit-status process)))
744 (message "command %S %s" (process-command process)
745 (replace-regexp-in-string "\n" "" status)))))
746 process))
748 (defun image-dired-create-thumb-1 (original-file thumbnail-file)
749 "For ORIGINAL-FILE, create thumbnail image named THUMBNAIL-FILE."
750 (image-dired--check-executable-exists
751 'image-dired-cmd-create-thumbnail-program)
752 (let* ((width (int-to-string (image-dired-thumb-size 'width)))
753 (height (int-to-string (image-dired-thumb-size 'height)))
754 (modif-time (format-time-string
755 "%s" (nth 5 (file-attributes original-file))))
756 (thumbnail-nq8-file (replace-regexp-in-string ".png\\'" "-nq8.png"
757 thumbnail-file))
758 (spec
759 (list
760 (cons ?w width)
761 (cons ?h height)
762 (cons ?m modif-time)
763 (cons ?f original-file)
764 (cons ?q thumbnail-nq8-file)
765 (cons ?t thumbnail-file)))
766 (thumbnail-dir (file-name-directory thumbnail-file))
767 process)
768 (when (not (file-exists-p thumbnail-dir))
769 (message "Creating thumbnail directory")
770 (make-directory thumbnail-dir t)
771 (set-file-modes thumbnail-dir #o700))
773 ;; Thumbnail file creation processes begin here and are marshaled
774 ;; in a queue by `image-dired-create-thumb'.
775 (setq process
776 (apply #'start-process "image-dired-create-thumbnail" nil
777 image-dired-cmd-create-thumbnail-program
778 (mapcar
779 (lambda (arg) (format-spec arg spec))
780 (if (memq image-dired-thumbnail-storage
781 '(standard standard-large))
782 image-dired-cmd-create-standard-thumbnail-options
783 image-dired-cmd-create-thumbnail-options))))
785 (setf (process-sentinel process)
786 (lambda (process status)
787 ;; Trigger next in queue once a thumbnail has been created
788 (cl-decf image-dired-queue-active-jobs)
789 (image-dired-thumb-queue-run)
790 (if (not (and (eq (process-status process) 'exit)
791 (zerop (process-exit-status process))))
792 (message "Thumb could not be created for %s: %s"
793 (abbreviate-file-name original-file)
794 (replace-regexp-in-string "\n" "" status))
795 (set-file-modes thumbnail-file #o600)
796 (clear-image-cache thumbnail-file)
797 ;; PNG thumbnail has been created since we are
798 ;; following the XDG thumbnail spec, so try to optimize
799 (when (memq image-dired-thumbnail-storage
800 '(standard standard-large))
801 (cond
802 ((and image-dired-cmd-pngnq-program
803 (executable-find image-dired-cmd-pngnq-program))
804 (image-dired-pngnq-thumb spec))
805 ((and image-dired-cmd-pngcrush-program
806 (executable-find image-dired-cmd-pngcrush-program))
807 (image-dired-pngcrush-thumb spec))
808 ((and image-dired-cmd-optipng-program
809 (executable-find image-dired-cmd-optipng-program))
810 (image-dired-optipng-thumb spec)))))))
811 process))
813 (defun image-dired-thumb-queue-run ()
814 "Run a queued job if one exists and not too many jobs are running.
815 Queued items live in `image-dired-queue'."
816 (while (and image-dired-queue
817 (< image-dired-queue-active-jobs
818 image-dired-queue-active-limit))
819 (cl-incf image-dired-queue-active-jobs)
820 (apply #'image-dired-create-thumb-1 (pop image-dired-queue))))
822 (defun image-dired-create-thumb (original-file thumbnail-file)
823 "Add a job for generating thumbnail to `image-dired-queue'."
824 (setq image-dired-queue
825 (nconc image-dired-queue
826 (list (list original-file thumbnail-file))))
827 (run-at-time 0 nil #'image-dired-thumb-queue-run))
829 ;;;###autoload
830 (defun image-dired-dired-toggle-marked-thumbs (&optional arg)
831 "Toggle thumbnails in front of file names in the dired buffer.
832 If no marked file could be found, insert or hide thumbnails on the
833 current line. ARG, if non-nil, specifies the files to use instead
834 of the marked files. If ARG is an integer, use the next ARG (or
835 previous -ARG, if ARG<0) files."
836 (interactive "P")
837 (dired-map-over-marks
838 (let ((image-pos (dired-move-to-filename))
839 (image-file (dired-get-filename nil t))
840 thumb-file
841 overlay)
842 (when (and image-file
843 (string-match-p (image-file-name-regexp) image-file))
844 (setq thumb-file (image-dired-get-thumbnail-image image-file))
845 ;; If image is not already added, then add it.
846 (let ((thumb-ov (cl-loop for ov in (overlays-in (point) (1+ (point)))
847 if (overlay-get ov 'thumb-file) return ov)))
848 (if thumb-ov
849 (delete-overlay thumb-ov)
850 (put-image thumb-file image-pos)
851 (setq overlay
852 (cl-loop for ov in (overlays-in (point) (1+ (point)))
853 if (overlay-get ov 'put-image) return ov))
854 (overlay-put overlay 'image-file image-file)
855 (overlay-put overlay 'thumb-file thumb-file)))))
856 arg ; Show or hide image on ARG next files.
857 'show-progress) ; Update dired display after each image is updated.
858 (add-hook 'dired-after-readin-hook
859 'image-dired-dired-after-readin-hook nil t))
861 (defun image-dired-dired-after-readin-hook ()
862 "Relocate existing thumbnail overlays in dired buffer after reverting.
863 Move them to their corresponding files if they still exist.
864 Otherwise, delete overlays."
865 (mapc (lambda (overlay)
866 (when (overlay-get overlay 'put-image)
867 (let* ((image-file (overlay-get overlay 'image-file))
868 (image-pos (dired-goto-file image-file)))
869 (if image-pos
870 (move-overlay overlay image-pos image-pos)
871 (delete-overlay overlay)))))
872 (overlays-in (point-min) (point-max))))
874 (defun image-dired-next-line-and-display ()
875 "Move to next dired line and display thumbnail image."
876 (interactive)
877 (dired-next-line 1)
878 (image-dired-display-thumbs
879 t (or image-dired-append-when-browsing nil) t)
880 (if image-dired-dired-disp-props
881 (image-dired-dired-display-properties)))
883 (defun image-dired-previous-line-and-display ()
884 "Move to previous dired line and display thumbnail image."
885 (interactive)
886 (dired-previous-line 1)
887 (image-dired-display-thumbs
888 t (or image-dired-append-when-browsing nil) t)
889 (if image-dired-dired-disp-props
890 (image-dired-dired-display-properties)))
892 (defun image-dired-toggle-append-browsing ()
893 "Toggle `image-dired-append-when-browsing'."
894 (interactive)
895 (setq image-dired-append-when-browsing
896 (not image-dired-append-when-browsing))
897 (message "Append browsing %s."
898 (if image-dired-append-when-browsing
899 "on"
900 "off")))
902 (defun image-dired-mark-and-display-next ()
903 "Mark current file in dired and display next thumbnail image."
904 (interactive)
905 (dired-mark 1)
906 (image-dired-display-thumbs
907 t (or image-dired-append-when-browsing nil) t)
908 (if image-dired-dired-disp-props
909 (image-dired-dired-display-properties)))
911 (defun image-dired-toggle-dired-display-properties ()
912 "Toggle `image-dired-dired-disp-props'."
913 (interactive)
914 (setq image-dired-dired-disp-props
915 (not image-dired-dired-disp-props))
916 (message "Dired display properties %s."
917 (if image-dired-dired-disp-props
918 "on"
919 "off")))
921 (defvar image-dired-thumbnail-buffer "*image-dired*"
922 "Image-Dired's thumbnail buffer.")
924 (defun image-dired-create-thumbnail-buffer ()
925 "Create thumb buffer and set `image-dired-thumbnail-mode'."
926 (let ((buf (get-buffer-create image-dired-thumbnail-buffer)))
927 (with-current-buffer buf
928 (setq buffer-read-only t)
929 (if (not (eq major-mode 'image-dired-thumbnail-mode))
930 (image-dired-thumbnail-mode)))
931 buf))
933 (defvar image-dired-display-image-buffer "*image-dired-display-image*"
934 "Where larger versions of the images are display.")
936 (defun image-dired-create-display-image-buffer ()
937 "Create image display buffer and set `image-dired-display-image-mode'."
938 (let ((buf (get-buffer-create image-dired-display-image-buffer)))
939 (with-current-buffer buf
940 (setq buffer-read-only t)
941 (if (not (eq major-mode 'image-dired-display-image-mode))
942 (image-dired-display-image-mode)))
943 buf))
945 (defvar image-dired-saved-window-configuration nil
946 "Saved window configuration.")
948 ;;;###autoload
949 (defun image-dired-dired-with-window-configuration (dir &optional arg)
950 "Open directory DIR and create a default window configuration.
952 Convenience command that:
954 - Opens dired in folder DIR
955 - Splits windows in most useful (?) way
956 - Set `truncate-lines' to t
958 After the command has finished, you would typically mark some
959 image files in dired and type
960 \\[image-dired-display-thumbs] (`image-dired-display-thumbs').
962 If called with prefix argument ARG, skip splitting of windows.
964 The current window configuration is saved and can be restored by
965 calling `image-dired-restore-window-configuration'."
966 (interactive "DDirectory: \nP")
967 (let ((buf (image-dired-create-thumbnail-buffer))
968 (buf2 (image-dired-create-display-image-buffer)))
969 (setq image-dired-saved-window-configuration
970 (current-window-configuration))
971 (dired dir)
972 (delete-other-windows)
973 (when (not arg)
974 (split-window-right)
975 (setq truncate-lines t)
976 (save-excursion
977 (other-window 1)
978 (pop-to-buffer-same-window buf)
979 (select-window (split-window-below))
980 (pop-to-buffer-same-window buf2)
981 (other-window -2)))))
983 (defun image-dired-restore-window-configuration ()
984 "Restore window configuration.
985 Restore any changes to the window configuration made by calling
986 `image-dired-dired-with-window-configuration'."
987 (interactive)
988 (if image-dired-saved-window-configuration
989 (set-window-configuration image-dired-saved-window-configuration)
990 (message "No saved window configuration")))
992 ;;;###autoload
993 (defun image-dired-display-thumbs (&optional arg append do-not-pop)
994 "Display thumbnails of all marked files, in `image-dired-thumbnail-buffer'.
995 If a thumbnail image does not exist for a file, it is created on the
996 fly. With prefix argument ARG, display only thumbnail for file at
997 point (this is useful if you have marked some files but want to show
998 another one).
1000 Recommended usage is to split the current frame horizontally so that
1001 you have the dired buffer in the left window and the
1002 `image-dired-thumbnail-buffer' buffer in the right window.
1004 With optional argument APPEND, append thumbnail to thumbnail buffer
1005 instead of erasing it first.
1007 Optional argument DO-NOT-POP controls if `pop-to-buffer' should be
1008 used or not. If non-nil, use `display-buffer' instead of
1009 `pop-to-buffer'. This is used from functions like
1010 `image-dired-next-line-and-display' and
1011 `image-dired-previous-line-and-display' where we do not want the
1012 thumbnail buffer to be selected."
1013 (interactive "P")
1014 (let ((buf (image-dired-create-thumbnail-buffer))
1015 thumb-name files dired-buf)
1016 (if arg
1017 (setq files (list (dired-get-filename)))
1018 (setq files (dired-get-marked-files)))
1019 (setq dired-buf (current-buffer))
1020 (with-current-buffer buf
1021 (let ((inhibit-read-only t))
1022 (if (not append)
1023 (erase-buffer)
1024 (goto-char (point-max)))
1025 (dolist (curr-file files)
1026 (setq thumb-name (image-dired-thumb-name curr-file))
1027 (when (not (file-exists-p thumb-name))
1028 (image-dired-create-thumb curr-file thumb-name))
1029 (image-dired-insert-thumbnail thumb-name curr-file dired-buf)))
1030 (if do-not-pop
1031 (display-buffer buf)
1032 (pop-to-buffer buf))
1033 (cond ((eq 'dynamic image-dired-line-up-method)
1034 (image-dired-line-up-dynamic))
1035 ((eq 'fixed image-dired-line-up-method)
1036 (image-dired-line-up))
1037 ((eq 'interactive image-dired-line-up-method)
1038 (image-dired-line-up-interactive))
1039 ((eq 'none image-dired-line-up-method)
1040 nil)
1042 (image-dired-line-up-dynamic))))))
1044 ;;;###autoload
1045 (defun image-dired-show-all-from-dir (dir)
1046 "Make a preview buffer for all images in DIR and display it.
1047 If the number of files in DIR matching `image-file-name-regexp'
1048 exceeds `image-dired-show-all-from-dir-max-files', a warning will be
1049 displayed."
1050 (interactive "DImage Dired: ")
1051 (dired dir)
1052 (dired-mark-files-regexp (image-file-name-regexp))
1053 (let ((files (dired-get-marked-files)))
1054 (if (or (<= (length files) image-dired-show-all-from-dir-max-files)
1055 (and (> (length files) image-dired-show-all-from-dir-max-files)
1056 (y-or-n-p
1057 (format
1058 "Directory contains more than %d image files. Proceed? "
1059 image-dired-show-all-from-dir-max-files))))
1060 (progn
1061 (image-dired-display-thumbs)
1062 (pop-to-buffer image-dired-thumbnail-buffer))
1063 (message "Canceled."))))
1065 ;;;###autoload
1066 (defalias 'image-dired 'image-dired-show-all-from-dir)
1068 ;;;###autoload
1069 (define-obsolete-function-alias 'tumme 'image-dired "24.4")
1071 (defun image-dired-sane-db-file ()
1072 "Check if `image-dired-db-file' exists.
1073 If not, try to create it (including any parent directories).
1074 Signal error if there are problems creating it."
1075 (or (file-exists-p image-dired-db-file)
1076 (let (dir buf)
1077 (unless (file-directory-p (setq dir (file-name-directory
1078 image-dired-db-file)))
1079 (make-directory dir t))
1080 (with-current-buffer (setq buf (create-file-buffer
1081 image-dired-db-file))
1082 (write-file image-dired-db-file))
1083 (kill-buffer buf)
1084 (file-exists-p image-dired-db-file))
1085 (error "Could not create %s" image-dired-db-file)))
1087 (defun image-dired-write-tags (file-tags)
1088 "Write file tags to database.
1089 Write each file and tag in FILE-TAGS to the database.
1090 FILE-TAGS is an alist in the following form:
1091 ((FILE . TAG) ... )"
1092 (image-dired-sane-db-file)
1093 (let (end file tag)
1094 (image-dired--with-db-file
1095 (setq buffer-file-name image-dired-db-file)
1096 (dolist (elt file-tags)
1097 (setq file (car elt)
1098 tag (cdr elt))
1099 (goto-char (point-min))
1100 (if (search-forward-regexp (format "^%s.*$" file) nil t)
1101 (progn
1102 (setq end (point))
1103 (beginning-of-line)
1104 (when (not (search-forward (format ";%s" tag) end t))
1105 (end-of-line)
1106 (insert (format ";%s" tag))))
1107 (goto-char (point-max))
1108 (insert (format "\n%s;%s" file tag))))
1109 (save-buffer))))
1111 (defun image-dired-remove-tag (files tag)
1112 "For all FILES, remove TAG from the image database."
1113 (image-dired-sane-db-file)
1114 (image-dired--with-db-file
1115 (setq buffer-file-name image-dired-db-file)
1116 (let (end)
1117 (unless (listp files)
1118 (if (stringp files)
1119 (setq files (list files))
1120 (error "Files must be a string or a list of strings!")))
1121 (dolist (file files)
1122 (goto-char (point-min))
1123 (when (search-forward-regexp (format "^%s" file) nil t)
1124 (end-of-line)
1125 (setq end (point))
1126 (beginning-of-line)
1127 (when (search-forward-regexp (format "\\(;%s\\)" tag) end t)
1128 (delete-region (match-beginning 1) (match-end 1))
1129 ;; Check if file should still be in the database. If
1130 ;; it has no tags or comments, it will be removed.
1131 (end-of-line)
1132 (setq end (point))
1133 (beginning-of-line)
1134 (when (not (search-forward ";" end t))
1135 (kill-line 1)
1136 ;; If on empty line at end of buffer
1137 (and (eobp)
1138 (looking-at "^$")
1139 (delete-char -1)))))))
1140 (save-buffer)))
1142 (defun image-dired-list-tags (file)
1143 "Read all tags for image FILE from the image database."
1144 (image-dired-sane-db-file)
1145 (image-dired--with-db-file
1146 (let (end (tags ""))
1147 (when (search-forward-regexp (format "^%s" file) nil t)
1148 (end-of-line)
1149 (setq end (point))
1150 (beginning-of-line)
1151 (if (search-forward ";" end t)
1152 (if (search-forward "comment:" end t)
1153 (if (search-forward ";" end t)
1154 (setq tags (buffer-substring (point) end)))
1155 (setq tags (buffer-substring (point) end)))))
1156 (split-string tags ";"))))
1158 ;;;###autoload
1159 (defun image-dired-tag-files (arg)
1160 "Tag marked file(s) in dired. With prefix ARG, tag file at point."
1161 (interactive "P")
1162 (let ((tag (read-string "Tags to add (separate tags with a semicolon): "))
1163 files)
1164 (if arg
1165 (setq files (list (dired-get-filename)))
1166 (setq files (dired-get-marked-files)))
1167 (image-dired-write-tags
1168 (mapcar
1169 (lambda (x)
1170 (cons x tag))
1171 files))))
1173 (defun image-dired-tag-thumbnail ()
1174 "Tag current thumbnail."
1175 (interactive)
1176 (let ((tag (read-string "Tags to add (separate tags with a semicolon): ")))
1177 (image-dired-write-tags (list (cons (image-dired-original-file-name) tag))))
1178 (image-dired-update-property
1179 'tags (image-dired-list-tags (image-dired-original-file-name))))
1181 ;;;###autoload
1182 (defun image-dired-delete-tag (arg)
1183 "Remove tag for selected file(s).
1184 With prefix argument ARG, remove tag from file at point."
1185 (interactive "P")
1186 (let ((tag (read-string "Tag to remove: "))
1187 files)
1188 (if arg
1189 (setq files (list (dired-get-filename)))
1190 (setq files (dired-get-marked-files)))
1191 (image-dired-remove-tag files tag)))
1193 (defun image-dired-tag-thumbnail-remove ()
1194 "Remove tag from thumbnail."
1195 (interactive)
1196 (let ((tag (read-string "Tag to remove: ")))
1197 (image-dired-remove-tag (image-dired-original-file-name) tag))
1198 (image-dired-update-property
1199 'tags (image-dired-list-tags (image-dired-original-file-name))))
1201 (defun image-dired-original-file-name ()
1202 "Get original file name for thumbnail or display image at point."
1203 (get-text-property (point) 'original-file-name))
1205 (defun image-dired-file-name-at-point ()
1206 "Get abbreviated file name for thumbnail or display image at point."
1207 (let ((f (image-dired-original-file-name)))
1208 (when f
1209 (abbreviate-file-name f))))
1211 (defun image-dired-associated-dired-buffer ()
1212 "Get associated dired buffer at point."
1213 (get-text-property (point) 'associated-dired-buffer))
1215 (defun image-dired-get-buffer-window (buf)
1216 "Return window where buffer BUF is."
1217 (get-window-with-predicate
1218 (lambda (window)
1219 (equal (window-buffer window) buf))
1220 nil t))
1222 (defun image-dired-track-original-file ()
1223 "Track the original file in the associated dired buffer.
1224 See documentation for `image-dired-toggle-movement-tracking'.
1225 Interactive use only useful if `image-dired-track-movement' is nil."
1226 (interactive)
1227 (let* ((dired-buf (image-dired-associated-dired-buffer))
1228 (file-name (image-dired-original-file-name))
1229 (window (image-dired-get-buffer-window dired-buf)))
1230 (and (buffer-live-p dired-buf) file-name
1231 (with-current-buffer dired-buf
1232 (if (not (dired-goto-file file-name))
1233 (message "Could not track file")
1234 (if window (set-window-point window (point))))))))
1236 (defun image-dired-toggle-movement-tracking ()
1237 "Turn on and off `image-dired-track-movement'.
1238 Tracking of the movements between thumbnail and dired buffer so that
1239 they are \"mirrored\" in the dired buffer. When this is on, moving
1240 around in the thumbnail or dired buffer will find the matching
1241 position in the other buffer."
1242 (interactive)
1243 (setq image-dired-track-movement (not image-dired-track-movement))
1244 (message "Tracking %s" (if image-dired-track-movement "on" "off")))
1246 (defun image-dired-track-thumbnail ()
1247 "Track current dired file's thumb in `image-dired-thumbnail-buffer'.
1248 This is almost the same as what `image-dired-track-original-file' does,
1249 but the other way around."
1250 (let ((file (dired-get-filename))
1251 prop-val found window)
1252 (when (get-buffer image-dired-thumbnail-buffer)
1253 (with-current-buffer image-dired-thumbnail-buffer
1254 (goto-char (point-min))
1255 (while (and (not (eobp))
1256 (not found))
1257 (if (and (setq prop-val
1258 (get-text-property (point) 'original-file-name))
1259 (string= prop-val file))
1260 (setq found t))
1261 (if (not found)
1262 (forward-char 1)))
1263 (when found
1264 (if (setq window (image-dired-thumbnail-window))
1265 (set-window-point window (point)))
1266 (image-dired-display-thumb-properties))))))
1268 (defun image-dired-dired-next-line (&optional arg)
1269 "Call `dired-next-line', then track thumbnail.
1270 This can safely replace `dired-next-line'.
1271 With prefix argument, move ARG lines."
1272 (interactive "P")
1273 (dired-next-line (or arg 1))
1274 (if image-dired-track-movement
1275 (image-dired-track-thumbnail)))
1277 (defun image-dired-dired-previous-line (&optional arg)
1278 "Call `dired-previous-line', then track thumbnail.
1279 This can safely replace `dired-previous-line'.
1280 With prefix argument, move ARG lines."
1281 (interactive "P")
1282 (dired-previous-line (or arg 1))
1283 (if image-dired-track-movement
1284 (image-dired-track-thumbnail)))
1286 (defun image-dired-forward-image (&optional arg)
1287 "Move to next image and display properties.
1288 Optional prefix ARG says how many images to move; default is one
1289 image."
1290 (interactive "p")
1291 (let (pos (steps (or arg 1)))
1292 (dotimes (_ steps)
1293 (if (and (not (eobp))
1294 (save-excursion
1295 (forward-char)
1296 (while (and (not (eobp))
1297 (not (image-dired-image-at-point-p)))
1298 (forward-char))
1299 (setq pos (point))
1300 (image-dired-image-at-point-p)))
1301 (goto-char pos)
1302 (error "At last image"))))
1303 (when image-dired-track-movement
1304 (image-dired-track-original-file))
1305 (image-dired-display-thumb-properties))
1307 (defun image-dired-backward-image (&optional arg)
1308 "Move to previous image and display properties.
1309 Optional prefix ARG says how many images to move; default is one
1310 image."
1311 (interactive "p")
1312 (let (pos (steps (or arg 1)))
1313 (dotimes (_ steps)
1314 (if (and (not (bobp))
1315 (save-excursion
1316 (backward-char)
1317 (while (and (not (bobp))
1318 (not (image-dired-image-at-point-p)))
1319 (backward-char))
1320 (setq pos (point))
1321 (image-dired-image-at-point-p)))
1322 (goto-char pos)
1323 (error "At first image"))))
1324 (when image-dired-track-movement
1325 (image-dired-track-original-file))
1326 (image-dired-display-thumb-properties))
1328 (defun image-dired-next-line ()
1329 "Move to next line and display properties."
1330 (interactive)
1331 (let ((goal-column (current-column)))
1332 (forward-line 1)
1333 (move-to-column goal-column))
1334 ;; If we end up in an empty spot, back up to the next thumbnail.
1335 (if (not (image-dired-image-at-point-p))
1336 (image-dired-backward-image))
1337 (if image-dired-track-movement
1338 (image-dired-track-original-file))
1339 (image-dired-display-thumb-properties))
1342 (defun image-dired-previous-line ()
1343 "Move to previous line and display properties."
1344 (interactive)
1345 (let ((goal-column (current-column)))
1346 (forward-line -1)
1347 (move-to-column goal-column))
1348 ;; If we end up in an empty spot, back up to the next
1349 ;; thumbnail. This should only happen if the user deleted a
1350 ;; thumbnail and did not refresh, so it is not very common. But we
1351 ;; can handle it in a good manner, so why not?
1352 (if (not (image-dired-image-at-point-p))
1353 (image-dired-backward-image))
1354 (if image-dired-track-movement
1355 (image-dired-track-original-file))
1356 (image-dired-display-thumb-properties))
1358 (defun image-dired-format-properties-string (buf file props comment)
1359 "Format display properties.
1360 BUF is the associated dired buffer, FILE is the original image file
1361 name, PROPS is a stringified list of tags and COMMENT is the image file's
1362 comment."
1363 (format-spec
1364 image-dired-display-properties-format
1365 (list
1366 (cons ?b (or buf ""))
1367 (cons ?f file)
1368 (cons ?t (or props ""))
1369 (cons ?c (or comment "")))))
1371 (defun image-dired-display-thumb-properties ()
1372 "Display thumbnail properties in the echo area."
1373 (if (not (eobp))
1374 (let ((file-name (file-name-nondirectory (image-dired-original-file-name)))
1375 (dired-buf (buffer-name (image-dired-associated-dired-buffer)))
1376 (props (mapconcat #'identity (get-text-property (point) 'tags) ", "))
1377 (comment (get-text-property (point) 'comment))
1378 (message-log-max nil))
1379 (if file-name
1380 (message "%s"
1381 (image-dired-format-properties-string
1382 dired-buf
1383 file-name
1384 props
1385 comment))))))
1387 (defun image-dired-dired-file-marked-p ()
1388 "Check whether file on current line is marked or not."
1389 (save-excursion
1390 (beginning-of-line)
1391 (not (looking-at "^ .*$"))))
1393 (defun image-dired-modify-mark-on-thumb-original-file (command)
1394 "Modify mark in dired buffer.
1395 COMMAND is one of `mark' for marking file in dired, `unmark' for
1396 unmarking file in dired or `flag' for flagging file for delete in
1397 dired."
1398 (let ((file-name (image-dired-original-file-name))
1399 (dired-buf (image-dired-associated-dired-buffer)))
1400 (if (not (and dired-buf file-name))
1401 (message "No image, or image with correct properties, at point.")
1402 (with-current-buffer dired-buf
1403 (message "%s" file-name)
1404 (if (dired-goto-file file-name)
1405 (cond ((eq command 'mark) (dired-mark 1))
1406 ((eq command 'unmark) (dired-unmark 1))
1407 ((eq command 'toggle)
1408 (if (image-dired-dired-file-marked-p)
1409 (dired-unmark 1)
1410 (dired-mark 1)))
1411 ((eq command 'flag) (dired-flag-file-deletion 1))))))))
1413 (defun image-dired-mark-thumb-original-file ()
1414 "Mark original image file in associated dired buffer."
1415 (interactive)
1416 (image-dired-modify-mark-on-thumb-original-file 'mark)
1417 (image-dired-forward-image))
1419 (defun image-dired-unmark-thumb-original-file ()
1420 "Unmark original image file in associated dired buffer."
1421 (interactive)
1422 (image-dired-modify-mark-on-thumb-original-file 'unmark)
1423 (image-dired-forward-image))
1425 (defun image-dired-flag-thumb-original-file ()
1426 "Flag original image file for deletion in associated dired buffer."
1427 (interactive)
1428 (image-dired-modify-mark-on-thumb-original-file 'flag)
1429 (image-dired-forward-image))
1431 (defun image-dired-toggle-mark-thumb-original-file ()
1432 "Toggle mark on original image file in associated dired buffer."
1433 (interactive)
1434 (image-dired-modify-mark-on-thumb-original-file 'toggle))
1436 (defun image-dired-jump-original-dired-buffer ()
1437 "Jump to the dired buffer associated with the current image file.
1438 You probably want to use this together with
1439 `image-dired-track-original-file'."
1440 (interactive)
1441 (let ((buf (image-dired-associated-dired-buffer))
1442 window frame)
1443 (setq window (image-dired-get-buffer-window buf))
1444 (if window
1445 (progn
1446 (if (not (equal (selected-frame) (setq frame (window-frame window))))
1447 (select-frame-set-input-focus frame))
1448 (select-window window))
1449 (message "Associated dired buffer not visible"))))
1451 ;;;###autoload
1452 (defun image-dired-jump-thumbnail-buffer ()
1453 "Jump to thumbnail buffer."
1454 (interactive)
1455 (let ((window (image-dired-thumbnail-window))
1456 frame)
1457 (if window
1458 (progn
1459 (if (not (equal (selected-frame) (setq frame (window-frame window))))
1460 (select-frame-set-input-focus frame))
1461 (select-window window))
1462 (message "Thumbnail buffer not visible"))))
1464 (defvar image-dired-thumbnail-mode-line-up-map
1465 (let ((map (make-sparse-keymap)))
1466 ;; map it to "g" so that the user can press it more quickly
1467 (define-key map "g" 'image-dired-line-up-dynamic)
1468 ;; "f" for "fixed" number of thumbs per row
1469 (define-key map "f" 'image-dired-line-up)
1470 ;; "i" for "interactive"
1471 (define-key map "i" 'image-dired-line-up-interactive)
1472 map)
1473 "Keymap for line-up commands in `image-dired-thumbnail-mode'.")
1475 (defvar image-dired-thumbnail-mode-tag-map
1476 (let ((map (make-sparse-keymap)))
1477 ;; map it to "t" so that the user can press it more quickly
1478 (define-key map "t" 'image-dired-tag-thumbnail)
1479 ;; "r" for "remove"
1480 (define-key map "r" 'image-dired-tag-thumbnail-remove)
1481 map)
1482 "Keymap for tag commands in `image-dired-thumbnail-mode'.")
1484 (defvar image-dired-thumbnail-mode-map
1485 (let ((map (make-sparse-keymap)))
1486 (define-key map [right] 'image-dired-forward-image)
1487 (define-key map [left] 'image-dired-backward-image)
1488 (define-key map [up] 'image-dired-previous-line)
1489 (define-key map [down] 'image-dired-next-line)
1490 (define-key map "\C-f" 'image-dired-forward-image)
1491 (define-key map "\C-b" 'image-dired-backward-image)
1492 (define-key map "\C-p" 'image-dired-previous-line)
1493 (define-key map "\C-n" 'image-dired-next-line)
1495 (define-key map "d" 'image-dired-flag-thumb-original-file)
1496 (define-key map [delete] 'image-dired-flag-thumb-original-file)
1497 (define-key map "m" 'image-dired-mark-thumb-original-file)
1498 (define-key map "u" 'image-dired-unmark-thumb-original-file)
1499 (define-key map "." 'image-dired-track-original-file)
1500 (define-key map [tab] 'image-dired-jump-original-dired-buffer)
1502 ;; add line-up map
1503 (define-key map "g" image-dired-thumbnail-mode-line-up-map)
1504 ;; add tag map
1505 (define-key map "t" image-dired-thumbnail-mode-tag-map)
1507 (define-key map "\C-m" 'image-dired-display-thumbnail-original-image)
1508 (define-key map [C-return] 'image-dired-thumbnail-display-external)
1510 (define-key map "l" 'image-dired-rotate-thumbnail-left)
1511 (define-key map "r" 'image-dired-rotate-thumbnail-right)
1512 (define-key map "L" 'image-dired-rotate-original-left)
1513 (define-key map "R" 'image-dired-rotate-original-right)
1515 (define-key map "D" 'image-dired-thumbnail-set-image-description)
1516 (define-key map "\C-d" 'image-dired-delete-char)
1517 (define-key map " " 'image-dired-display-next-thumbnail-original)
1518 (define-key map (kbd "DEL") 'image-dired-display-previous-thumbnail-original)
1519 (define-key map "c" 'image-dired-comment-thumbnail)
1521 ;; Mouse
1522 (define-key map [mouse-2] 'image-dired-mouse-display-image)
1523 (define-key map [mouse-1] 'image-dired-mouse-select-thumbnail)
1524 ;; Seems I must first set C-down-mouse-1 to undefined, or else it
1525 ;; will trigger the buffer menu. If I try to instead bind
1526 ;; C-down-mouse-1 to `image-dired-mouse-toggle-mark', I get a message
1527 ;; about C-mouse-1 not being defined afterwards. Annoying, but I
1528 ;; probably do not completely understand mouse events.
1529 (define-key map [C-down-mouse-1] 'undefined)
1530 (define-key map [C-mouse-1] 'image-dired-mouse-toggle-mark)
1532 ;; Menu
1533 (easy-menu-define nil map
1534 "Menu for `image-dired-thumbnail-mode'."
1535 '("Image-Dired"
1536 ["Quit" quit-window]
1537 ["Delete thumbnail from buffer" image-dired-delete-char]
1538 ["Remove tag from thumbnail" image-dired-tag-thumbnail-remove]
1539 ["Tag thumbnail" image-dired-tag-thumbnail]
1540 ["Comment thumbnail" image-dired-comment-thumbnail]
1541 ["Refresh thumb" image-dired-refresh-thumb]
1542 ["Dynamic line up" image-dired-line-up-dynamic]
1543 ["Line up thumbnails" image-dired-line-up]
1545 ["Rotate thumbnail left" image-dired-rotate-thumbnail-left]
1546 ["Rotate thumbnail right" image-dired-rotate-thumbnail-right]
1547 ["Rotate original left" image-dired-rotate-original-left]
1548 ["Rotate original right" image-dired-rotate-original-right]
1550 ["Toggle movement tracking on/off" image-dired-toggle-movement-tracking]
1552 ["Jump to dired buffer" image-dired-jump-original-dired-buffer]
1553 ["Track original" image-dired-track-original-file]
1555 ["Flag original for deletion" image-dired-flag-thumb-original-file]
1556 ["Unmark original" image-dired-unmark-thumb-original-file]
1557 ["Mark original" image-dired-mark-thumb-original-file]
1559 ["Display in external viewer" image-dired-thumbnail-display-external]
1560 ["Display image" image-dired-display-thumbnail-original-image]))
1561 map)
1562 "Keymap for `image-dired-thumbnail-mode'.")
1564 (defvar image-dired-display-image-mode-map
1565 (let ((map (make-sparse-keymap)))
1566 ;; `image-mode-map' has bindings that do not make sense in image-dired
1567 ;; (set-keymap-parent map image-mode-map)
1568 (define-key map "f" 'image-dired-display-current-image-full)
1569 (define-key map "s" 'image-dired-display-current-image-sized)
1570 (define-key map "g" nil)
1572 ;; Useful bindings from `image-mode-map'
1573 (define-key map [remap forward-char] 'image-forward-hscroll)
1574 (define-key map [remap backward-char] 'image-backward-hscroll)
1575 (define-key map [remap right-char] 'image-forward-hscroll)
1576 (define-key map [remap left-char] 'image-backward-hscroll)
1577 (define-key map [remap previous-line] 'image-previous-line)
1578 (define-key map [remap next-line] 'image-next-line)
1579 (define-key map [remap scroll-up] 'image-scroll-up)
1580 (define-key map [remap scroll-down] 'image-scroll-down)
1581 (define-key map [remap scroll-up-command] 'image-scroll-up)
1582 (define-key map [remap scroll-down-command] 'image-scroll-down)
1583 (define-key map [remap scroll-left] 'image-scroll-left)
1584 (define-key map [remap scroll-right] 'image-scroll-right)
1585 (define-key map [remap move-beginning-of-line] 'image-bol)
1586 (define-key map [remap move-end-of-line] 'image-eol)
1587 (define-key map [remap beginning-of-buffer] 'image-bob)
1588 (define-key map [remap end-of-buffer] 'image-eob)
1590 (easy-menu-define nil map
1591 "Menu for `image-dired-display-image-mode-map'."
1592 '("Image-Dired"
1593 ["Quit" quit-window]
1594 ["Display original, sized to fit" image-dired-display-current-image-sized]
1595 ["Display original, full size" image-dired-display-current-image-full]))
1596 map)
1597 "Keymap for `image-dired-display-image-mode'.")
1599 (defun image-dired-display-current-image-full ()
1600 "Display current image in full size."
1601 (interactive)
1602 (let ((file (image-dired-original-file-name)))
1603 (if file
1604 (progn
1605 (image-dired-display-image file t)
1606 (message "Full size image displayed"))
1607 (error "No original file name at point"))))
1609 (defun image-dired-display-current-image-sized ()
1610 "Display current image in sized to fit window dimensions."
1611 (interactive)
1612 (let ((file (image-dired-original-file-name)))
1613 (if file
1614 (progn
1615 (image-dired-display-image file)
1616 (message "Fitted image displayed"))
1617 (error "No original file name at point"))))
1619 (define-derived-mode image-dired-thumbnail-mode
1620 special-mode "image-dired-thumbnail"
1621 "Browse and manipulate thumbnail images using dired.
1622 Use `image-dired-minor-mode' to get a nice setup."
1623 :group 'image-dired
1624 (buffer-disable-undo)
1625 (add-hook 'file-name-at-point-functions 'image-dired-file-name-at-point nil t))
1627 (define-derived-mode image-dired-display-image-mode
1628 special-mode "image-dired-image-display"
1629 "Mode for displaying and manipulating original image.
1630 Resized or in full-size."
1631 :group 'image-dired
1632 (buffer-disable-undo)
1633 (image-mode-setup-winprops)
1634 (setq cursor-type nil)
1635 (add-hook 'file-name-at-point-functions 'image-dired-file-name-at-point nil t))
1637 (defvar image-dired-minor-mode-map
1638 (let ((map (make-sparse-keymap)))
1639 ;; (set-keymap-parent map dired-mode-map)
1640 ;; Hijack previous and next line movement. Let C-p and C-b be
1641 ;; though...
1642 (define-key map "p" 'image-dired-dired-previous-line)
1643 (define-key map "n" 'image-dired-dired-next-line)
1644 (define-key map [up] 'image-dired-dired-previous-line)
1645 (define-key map [down] 'image-dired-dired-next-line)
1647 (define-key map (kbd "C-S-n") 'image-dired-next-line-and-display)
1648 (define-key map (kbd "C-S-p") 'image-dired-previous-line-and-display)
1649 (define-key map (kbd "C-S-m") 'image-dired-mark-and-display-next)
1651 (define-key map "\C-td" 'image-dired-display-thumbs)
1652 (define-key map [tab] 'image-dired-jump-thumbnail-buffer)
1653 (define-key map "\C-ti" 'image-dired-dired-display-image)
1654 (define-key map "\C-tx" 'image-dired-dired-display-external)
1655 (define-key map "\C-ta" 'image-dired-display-thumbs-append)
1656 (define-key map "\C-t." 'image-dired-display-thumb)
1657 (define-key map "\C-tc" 'image-dired-dired-comment-files)
1658 (define-key map "\C-tf" 'image-dired-mark-tagged-files)
1660 ;; Menu for dired
1661 (easy-menu-define nil map
1662 "Menu for `image-dired-minor-mode'."
1663 '("Image-dired"
1664 ["Copy with EXIF file name" image-dired-copy-with-exif-file-name]
1665 ["Comment files" image-dired-dired-comment-files]
1666 ["Mark tagged files" image-dired-mark-tagged-files]
1667 ["Jump to thumbnail buffer" image-dired-jump-thumbnail-buffer]
1669 ["Toggle movement tracking" image-dired-toggle-movement-tracking]
1670 ["Toggle append browsing" image-dired-toggle-append-browsing]
1671 ["Toggle display properties" image-dired-toggle-dired-display-properties]
1673 ["Display in external viewer" image-dired-dired-display-external]
1674 ["Display image" image-dired-dired-display-image]
1675 ["Display this thumbnail" image-dired-display-thumb]
1676 ["Display thumbnails append" image-dired-display-thumbs-append]
1678 ["Create thumbnails for marked files" image-dired-create-thumbs]
1680 ["Mark and display next" image-dired-mark-and-display-next]
1681 ["Display thumb for previous file" image-dired-previous-line-and-display]
1682 ["Display thumb for next file" image-dired-next-line-and-display]))
1683 map)
1684 "Keymap for `image-dired-minor-mode'.")
1686 ;;;###autoload
1687 (define-minor-mode image-dired-minor-mode
1688 "Setup easy-to-use keybindings for the commands to be used in dired mode.
1689 Note that n, p and <down> and <up> will be hijacked and bound to
1690 `image-dired-dired-x-line'."
1691 :keymap image-dired-minor-mode-map)
1693 ;;;###autoload
1694 (define-obsolete-function-alias 'image-dired-setup-dired-keybindings 'image-dired-minor-mode
1695 "26.1")
1697 (declare-function clear-image-cache "image.c" (&optional filter))
1699 (defun image-dired-create-thumbs (&optional arg)
1700 "Create thumbnail images for all marked files in dired.
1701 With prefix argument ARG, create thumbnails even if they already exist
1702 \(i.e. use this to refresh your thumbnails)."
1703 (interactive "P")
1704 (let (thumb-name)
1705 (dolist (curr-file (dired-get-marked-files))
1706 (setq thumb-name (image-dired-thumb-name curr-file))
1707 ;; If the user overrides the exist check, we must clear the
1708 ;; image cache so that if the user wants to display the
1709 ;; thumbnail, it is not fetched from cache.
1710 (when arg
1711 (clear-image-cache (expand-file-name thumb-name)))
1712 (when (or (not (file-exists-p thumb-name))
1713 arg)
1714 (image-dired-create-thumb curr-file thumb-name)))))
1716 (defvar image-dired-slideshow-timer nil
1717 "Slideshow timer.")
1719 (defvar image-dired-slideshow-count 0
1720 "Keeping track on number of images in slideshow.")
1722 (defvar image-dired-slideshow-times 0
1723 "Number of pictures to display in slideshow.")
1725 (defun image-dired-slideshow-step ()
1726 "Step to next file, if `image-dired-slideshow-times' has not been reached."
1727 (if (< image-dired-slideshow-count image-dired-slideshow-times)
1728 (progn
1729 (message "%s" (1+ image-dired-slideshow-count))
1730 (setq image-dired-slideshow-count (1+ image-dired-slideshow-count))
1731 (image-dired-next-line-and-display))
1732 (image-dired-slideshow-stop)))
1734 (defun image-dired-slideshow-start ()
1735 "Start slideshow.
1736 Ask user for number of images to show and the delay in between."
1737 (interactive)
1738 (setq image-dired-slideshow-count 0)
1739 (setq image-dired-slideshow-times (string-to-number (read-string "How many: ")))
1740 (let ((repeat (string-to-number
1741 (read-string
1742 "Delay, in seconds. Decimals are accepted : " "1"))))
1743 (setq image-dired-slideshow-timer
1744 (run-with-timer
1745 0 repeat
1746 'image-dired-slideshow-step))))
1748 (defun image-dired-slideshow-stop ()
1749 "Cancel slideshow."
1750 (interactive)
1751 (cancel-timer image-dired-slideshow-timer))
1753 (defun image-dired-delete-char ()
1754 "Remove current thumbnail from thumbnail buffer and line up."
1755 (interactive)
1756 (let ((inhibit-read-only t))
1757 (delete-char 1)
1758 (when (= (following-char) ?\s)
1759 (delete-char 1))))
1761 ;;;###autoload
1762 (defun image-dired-display-thumbs-append ()
1763 "Append thumbnails to `image-dired-thumbnail-buffer'."
1764 (interactive)
1765 (image-dired-display-thumbs nil t t))
1767 ;;;###autoload
1768 (defun image-dired-display-thumb ()
1769 "Shorthand for `image-dired-display-thumbs' with prefix argument."
1770 (interactive)
1771 (image-dired-display-thumbs t nil t))
1773 (defun image-dired-line-up ()
1774 "Line up thumbnails according to `image-dired-thumbs-per-row'.
1775 See also `image-dired-line-up-dynamic'."
1776 (interactive)
1777 (let ((inhibit-read-only t))
1778 (goto-char (point-min))
1779 (while (and (not (image-dired-image-at-point-p))
1780 (not (eobp)))
1781 (delete-char 1))
1782 (while (not (eobp))
1783 (forward-char)
1784 (while (and (not (image-dired-image-at-point-p))
1785 (not (eobp)))
1786 (delete-char 1)))
1787 (goto-char (point-min))
1788 (let ((count 0))
1789 (while (not (eobp))
1790 (forward-char)
1791 (if (= image-dired-thumbs-per-row 1)
1792 (insert "\n")
1793 (insert " ")
1794 (setq count (1+ count))
1795 (when (and (= count (- image-dired-thumbs-per-row 1))
1796 (not (eobp)))
1797 (forward-char)
1798 (insert "\n")
1799 (setq count 0)))))
1800 (goto-char (point-min))))
1802 (defun image-dired-line-up-dynamic ()
1803 "Line up thumbnails images dynamically.
1804 Calculate how many thumbnails fit."
1805 (interactive)
1806 (let* ((char-width (frame-char-width))
1807 (width (image-dired-window-width-pixels (image-dired-thumbnail-window)))
1808 (image-dired-thumbs-per-row
1809 (/ width
1810 (+ (* 2 image-dired-thumb-relief)
1811 (* 2 image-dired-thumb-margin)
1812 (image-dired-thumb-size 'width)
1813 char-width))))
1814 (image-dired-line-up)))
1816 (defun image-dired-line-up-interactive ()
1817 "Line up thumbnails interactively.
1818 Ask user how many thumbnails should be displayed per row."
1819 (interactive)
1820 (let ((image-dired-thumbs-per-row
1821 (string-to-number (read-string "How many thumbs per row: "))))
1822 (if (not (> image-dired-thumbs-per-row 0))
1823 (message "Number must be greater than 0")
1824 (image-dired-line-up))))
1826 (defun image-dired-thumbnail-display-external ()
1827 "Display original image for thumbnail at point using external viewer."
1828 (interactive)
1829 (let ((file (image-dired-original-file-name)))
1830 (if (not (image-dired-image-at-point-p))
1831 (message "No thumbnail at point")
1832 (if (not file)
1833 (message "No original file name found")
1834 (start-process "image-dired-thumb-external" nil
1835 image-dired-external-viewer file)))))
1837 ;;;###autoload
1838 (defun image-dired-dired-display-external ()
1839 "Display file at point using an external viewer."
1840 (interactive)
1841 (let ((file (dired-get-filename)))
1842 (start-process "image-dired-external" nil
1843 image-dired-external-viewer file)))
1845 (defun image-dired-window-width-pixels (window)
1846 "Calculate WINDOW width in pixels."
1847 (* (window-width window) (frame-char-width)))
1849 (defun image-dired-window-height-pixels (window)
1850 "Calculate WINDOW height in pixels."
1851 ;; Note: The mode-line consumes one line
1852 (* (- (window-height window) 1) (frame-char-height)))
1854 (defun image-dired-display-window ()
1855 "Return window where `image-dired-display-image-buffer' is visible."
1856 (get-window-with-predicate
1857 (lambda (window)
1858 (equal (buffer-name (window-buffer window)) image-dired-display-image-buffer))
1859 nil t))
1861 (defun image-dired-thumbnail-window ()
1862 "Return window where `image-dired-thumbnail-buffer' is visible."
1863 (get-window-with-predicate
1864 (lambda (window)
1865 (equal (buffer-name (window-buffer window)) image-dired-thumbnail-buffer))
1866 nil t))
1868 (defun image-dired-associated-dired-buffer-window ()
1869 "Return window where associated dired buffer is visible."
1870 (let (buf)
1871 (if (image-dired-image-at-point-p)
1872 (progn
1873 (setq buf (image-dired-associated-dired-buffer))
1874 (get-window-with-predicate
1875 (lambda (window)
1876 (equal (window-buffer window) buf))))
1877 (error "No thumbnail image at point"))))
1879 (defun image-dired-display-window-width (window)
1880 "Return width, in pixels, of WINDOW."
1881 (- (image-dired-window-width-pixels window)
1882 image-dired-display-window-width-correction))
1884 (defun image-dired-display-window-height (window)
1885 "Return height, in pixels, of WINDOW."
1886 (- (image-dired-window-height-pixels window)
1887 image-dired-display-window-height-correction))
1889 (defun image-dired-display-image (file &optional original-size)
1890 "Display image FILE in image buffer.
1891 Use this when you want to display the image, semi sized, in a new
1892 window. The image is sized to fit the display window (using a
1893 temporary file, don't worry). Because of this, it will not be as
1894 quick as opening it directly, but on most modern systems it
1895 should feel snappy enough.
1897 If optional argument ORIGINAL-SIZE is non-nil, display image in its
1898 original size."
1899 (image-dired--check-executable-exists
1900 'image-dired-cmd-create-temp-image-program)
1901 (let ((new-file (expand-file-name image-dired-temp-image-file))
1902 (window (image-dired-display-window))
1903 (image-type 'jpeg))
1904 (setq file (expand-file-name file))
1905 (if (not original-size)
1906 (let* ((spec
1907 (list
1908 (cons ?p image-dired-cmd-create-temp-image-program)
1909 (cons ?w (image-dired-display-window-width window))
1910 (cons ?h (image-dired-display-window-height window))
1911 (cons ?f file)
1912 (cons ?t new-file)))
1913 (ret
1914 (apply #'call-process
1915 image-dired-cmd-create-temp-image-program nil nil nil
1916 (mapcar
1917 (lambda (arg) (format-spec arg spec))
1918 image-dired-cmd-create-temp-image-options))))
1919 (when (not (zerop ret))
1920 (error "Could not resize image")))
1921 (setq image-type (image-type-from-file-name file))
1922 (copy-file file new-file t))
1923 (with-current-buffer (image-dired-create-display-image-buffer)
1924 (let ((inhibit-read-only t))
1925 (erase-buffer)
1926 (clear-image-cache)
1927 (image-dired-insert-image image-dired-temp-image-file image-type 0 0)
1928 (goto-char (point-min))
1929 (set-window-vscroll window 0)
1930 (set-window-hscroll window 0)
1931 (image-dired-update-property 'original-file-name file)))))
1933 (defun image-dired-display-thumbnail-original-image (&optional arg)
1934 "Display current thumbnail's original image in display buffer.
1935 See documentation for `image-dired-display-image' for more information.
1936 With prefix argument ARG, display image in its original size."
1937 (interactive "P")
1938 (let ((file (image-dired-original-file-name)))
1939 (if (not (string-equal major-mode "image-dired-thumbnail-mode"))
1940 (message "Not in image-dired-thumbnail-mode")
1941 (if (not (image-dired-image-at-point-p))
1942 (message "No thumbnail at point")
1943 (if (not file)
1944 (message "No original file name found")
1945 (image-dired-create-display-image-buffer)
1946 (display-buffer image-dired-display-image-buffer)
1947 (image-dired-display-image file arg))))))
1950 ;;;###autoload
1951 (defun image-dired-dired-display-image (&optional arg)
1952 "Display current image file.
1953 See documentation for `image-dired-display-image' for more information.
1954 With prefix argument ARG, display image in its original size."
1955 (interactive "P")
1956 (image-dired-create-display-image-buffer)
1957 (display-buffer image-dired-display-image-buffer)
1958 (image-dired-display-image (dired-get-filename) arg))
1960 (defun image-dired-image-at-point-p ()
1961 "Return true if there is an image-dired thumbnail at point."
1962 (get-text-property (point) 'image-dired-thumbnail))
1964 (defun image-dired-rotate-thumbnail (degrees)
1965 "Rotate thumbnail DEGREES degrees."
1966 (image-dired--check-executable-exists
1967 'image-dired-cmd-rotate-thumbnail-program)
1968 (if (not (image-dired-image-at-point-p))
1969 (message "No thumbnail at point")
1970 (let* ((file (image-dired-thumb-name (image-dired-original-file-name)))
1971 (thumb (expand-file-name file))
1972 (spec (list (cons ?d degrees) (cons ?t thumb))))
1973 (apply #'call-process image-dired-cmd-rotate-thumbnail-program nil nil nil
1974 (mapcar (lambda (arg) (format-spec arg spec))
1975 image-dired-cmd-rotate-thumbnail-options))
1976 (clear-image-cache thumb))))
1978 (defun image-dired-rotate-thumbnail-left ()
1979 "Rotate thumbnail left (counter clockwise) 90 degrees.
1980 The result of the rotation is displayed in the image display area
1981 and a confirmation is needed before the original image files is
1982 overwritten. This confirmation can be turned off using
1983 `image-dired-rotate-original-ask-before-overwrite'."
1984 (interactive)
1985 (image-dired-rotate-thumbnail "270"))
1987 (defun image-dired-rotate-thumbnail-right ()
1988 "Rotate thumbnail counter right (clockwise) 90 degrees.
1989 The result of the rotation is displayed in the image display area
1990 and a confirmation is needed before the original image files is
1991 overwritten. This confirmation can be turned off using
1992 `image-dired-rotate-original-ask-before-overwrite'."
1993 (interactive)
1994 (image-dired-rotate-thumbnail "90"))
1996 (defun image-dired-refresh-thumb ()
1997 "Force creation of new image for current thumbnail."
1998 (interactive)
1999 (let* ((file (image-dired-original-file-name))
2000 (thumb (expand-file-name (image-dired-thumb-name file))))
2001 (clear-image-cache (expand-file-name thumb))
2002 (image-dired-create-thumb file thumb)))
2004 (defun image-dired-rotate-original (degrees)
2005 "Rotate original image DEGREES degrees."
2006 (image-dired--check-executable-exists
2007 'image-dired-cmd-rotate-original-program)
2008 (if (not (image-dired-image-at-point-p))
2009 (message "No image at point")
2010 (let* ((file (image-dired-original-file-name))
2011 (spec
2012 (list
2013 (cons ?d degrees)
2014 (cons ?o (expand-file-name file))
2015 (cons ?t image-dired-temp-rotate-image-file))))
2016 (unless (eq 'jpeg (image-type file))
2017 (error "Only JPEG images can be rotated!"))
2018 (if (not (= 0 (apply #'call-process image-dired-cmd-rotate-original-program
2019 nil nil nil
2020 (mapcar (lambda (arg) (format-spec arg spec))
2021 image-dired-cmd-rotate-original-options))))
2022 (error "Could not rotate image")
2023 (image-dired-display-image image-dired-temp-rotate-image-file)
2024 (if (or (and image-dired-rotate-original-ask-before-overwrite
2025 (y-or-n-p
2026 "Rotate to temp file OK. Overwrite original image? "))
2027 (not image-dired-rotate-original-ask-before-overwrite))
2028 (progn
2029 (copy-file image-dired-temp-rotate-image-file file t)
2030 (image-dired-refresh-thumb))
2031 (image-dired-display-image file))))))
2033 (defun image-dired-rotate-original-left ()
2034 "Rotate original image left (counter clockwise) 90 degrees."
2035 (interactive)
2036 (image-dired-rotate-original "270"))
2038 (defun image-dired-rotate-original-right ()
2039 "Rotate original image right (clockwise) 90 degrees."
2040 (interactive)
2041 (image-dired-rotate-original "90"))
2043 (defun image-dired-get-exif-file-name (file)
2044 "Use the image's EXIF information to return a unique file name.
2045 The file name should be unique as long as you do not take more than
2046 one picture per second. The original file name is suffixed at the end
2047 for traceability. The format of the returned file name is
2048 YYYY_MM_DD_HH_MM_DD_ORIG_FILE_NAME.jpg. Used from
2049 `image-dired-copy-with-exif-file-name'."
2050 (let (data no-exif-data-found)
2051 (if (not (eq 'jpeg (image-type (expand-file-name file))))
2052 (setq no-exif-data-found t
2053 data (format-time-string
2054 "%Y:%m:%d %H:%M:%S"
2055 (file-attribute-modification-time
2056 (file-attributes (expand-file-name file)))))
2057 (setq data (image-dired-get-exif-data (expand-file-name file)
2058 "DateTimeOriginal")))
2059 (while (string-match "[ :]" data)
2060 (setq data (replace-match "_" nil nil data)))
2061 (format "%s%s%s" data
2062 (if no-exif-data-found
2063 "_noexif_"
2064 "_")
2065 (file-name-nondirectory file))))
2067 (defun image-dired-thumbnail-set-image-description ()
2068 "Set the ImageDescription EXIF tag for the original image.
2069 If the image already has a value for this tag, it is used as the
2070 default value at the prompt."
2071 (interactive)
2072 (if (not (image-dired-image-at-point-p))
2073 (message "No thumbnail at point")
2074 (let* ((file (image-dired-original-file-name))
2075 (old-value (image-dired-get-exif-data file "ImageDescription")))
2076 (if (eq 0
2077 (image-dired-set-exif-data file "ImageDescription"
2078 (read-string "Value of ImageDescription: "
2079 old-value)))
2080 (message "Successfully wrote ImageDescription tag.")
2081 (error "Could not write ImageDescription tag")))))
2083 (defun image-dired-set-exif-data (file tag-name tag-value)
2084 "In FILE, set EXIF tag TAG-NAME to value TAG-VALUE."
2085 (image-dired--check-executable-exists
2086 'image-dired-cmd-write-exif-data-program)
2087 (let ((spec
2088 (list
2089 (cons ?f (expand-file-name file))
2090 (cons ?t tag-name)
2091 (cons ?v tag-value))))
2092 (apply #'call-process image-dired-cmd-write-exif-data-program nil nil nil
2093 (mapcar (lambda (arg) (format-spec arg spec))
2094 image-dired-cmd-write-exif-data-options))))
2096 (defun image-dired-get-exif-data (file tag-name)
2097 "From FILE, return EXIF tag TAG-NAME."
2098 (image-dired--check-executable-exists
2099 'image-dired-cmd-read-exif-data-program)
2100 (let ((buf (get-buffer-create "*image-dired-get-exif-data*"))
2101 (spec (list (cons ?f file) (cons ?t tag-name)))
2102 tag-value)
2103 (with-current-buffer buf
2104 (delete-region (point-min) (point-max))
2105 (if (not (eq (apply #'call-process image-dired-cmd-read-exif-data-program
2106 nil t nil
2107 (mapcar
2108 (lambda (arg) (format-spec arg spec))
2109 image-dired-cmd-read-exif-data-options))
2111 (error "Could not get EXIF tag")
2112 (goto-char (point-min))
2113 ;; Clean buffer from newlines and carriage returns before
2114 ;; getting final info
2115 (while (search-forward-regexp "[\n\r]" nil t)
2116 (replace-match "" nil t))
2117 (setq tag-value (buffer-substring (point-min) (point-max)))))
2118 tag-value))
2120 (defun image-dired-copy-with-exif-file-name ()
2121 "Copy file with unique name to main image directory.
2122 Copy current or all marked files in dired to a new file in your
2123 main image directory, using a file name generated by
2124 `image-dired-get-exif-file-name'. A typical usage for this if when
2125 copying images from a digital camera into the image directory.
2127 Typically, you would open up the folder with the incoming
2128 digital images, mark the files to be copied, and execute this
2129 function. The result is a couple of new files in
2130 `image-dired-main-image-directory' called
2131 2005_05_08_12_52_00_dscn0319.jpg,
2132 2005_05_08_14_27_45_dscn0320.jpg etc."
2133 (interactive)
2134 (let (new-name
2135 (files (dired-get-marked-files)))
2136 (mapc
2137 (lambda (curr-file)
2138 (setq new-name
2139 (format "%s/%s"
2140 (file-name-as-directory
2141 (expand-file-name image-dired-main-image-directory))
2142 (image-dired-get-exif-file-name curr-file)))
2143 (message "Copying %s to %s" curr-file new-name)
2144 (copy-file curr-file new-name))
2145 files)))
2147 (defun image-dired-display-next-thumbnail-original ()
2148 "In thumbnail buffer, move to next thumbnail and display the image."
2149 (interactive)
2150 (image-dired-forward-image)
2151 (image-dired-display-thumbnail-original-image))
2153 (defun image-dired-display-previous-thumbnail-original ()
2154 "Move to previous thumbnail and display image."
2155 (interactive)
2156 (image-dired-backward-image)
2157 (image-dired-display-thumbnail-original-image))
2159 (defun image-dired-write-comments (file-comments)
2160 "Write file comments to database.
2161 Write file comments to one or more files.
2162 FILE-COMMENTS is an alist on the following form:
2163 ((FILE . COMMENT) ... )"
2164 (image-dired-sane-db-file)
2165 (let (end comment-beg-pos comment-end-pos file comment)
2166 (image-dired--with-db-file
2167 (setq buffer-file-name image-dired-db-file)
2168 (dolist (elt file-comments)
2169 (setq file (car elt)
2170 comment (cdr elt))
2171 (goto-char (point-min))
2172 (if (search-forward-regexp (format "^%s.*$" file) nil t)
2173 (progn
2174 (setq end (point))
2175 (beginning-of-line)
2176 ;; Delete old comment, if any
2177 (when (search-forward ";comment:" end t)
2178 (setq comment-beg-pos (match-beginning 0))
2179 ;; Any tags after the comment?
2180 (if (search-forward ";" end t)
2181 (setq comment-end-pos (- (point) 1))
2182 (setq comment-end-pos end))
2183 ;; Delete comment tag and comment
2184 (delete-region comment-beg-pos comment-end-pos))
2185 ;; Insert new comment
2186 (beginning-of-line)
2187 (unless (search-forward ";" end t)
2188 (end-of-line)
2189 (insert ";"))
2190 (insert (format "comment:%s;" comment)))
2191 ;; File does not exist in database - add it.
2192 (goto-char (point-max))
2193 (insert (format "\n%s;comment:%s" file comment))))
2194 (save-buffer))))
2196 (defun image-dired-update-property (prop value)
2197 "Update text property PROP with value VALUE at point."
2198 (let ((inhibit-read-only t))
2199 (put-text-property
2200 (point) (1+ (point))
2201 prop
2202 value)))
2204 ;;;###autoload
2205 (defun image-dired-dired-comment-files ()
2206 "Add comment to current or marked files in dired."
2207 (interactive)
2208 (let ((comment (image-dired-read-comment)))
2209 (image-dired-write-comments
2210 (mapcar
2211 (lambda (curr-file)
2212 (cons curr-file comment))
2213 (dired-get-marked-files)))))
2215 (defun image-dired-comment-thumbnail ()
2216 "Add comment to current thumbnail in thumbnail buffer."
2217 (interactive)
2218 (let* ((file (image-dired-original-file-name))
2219 (comment (image-dired-read-comment file)))
2220 (image-dired-write-comments (list (cons file comment)))
2221 (image-dired-update-property 'comment comment))
2222 (image-dired-display-thumb-properties))
2224 (defun image-dired-read-comment (&optional file)
2225 "Read comment for an image.
2226 Optionally use old comment from FILE as initial value."
2227 (let ((comment
2228 (read-string
2229 "Comment: "
2230 (if file (image-dired-get-comment file)))))
2231 comment))
2233 (defun image-dired-get-comment (file)
2234 "Get comment for file FILE."
2235 (image-dired-sane-db-file)
2236 (image-dired--with-db-file
2237 (let (end comment-beg-pos comment-end-pos comment)
2238 (when (search-forward-regexp (format "^%s" file) nil t)
2239 (end-of-line)
2240 (setq end (point))
2241 (beginning-of-line)
2242 (when (search-forward ";comment:" end t)
2243 (setq comment-beg-pos (point))
2244 (if (search-forward ";" end t)
2245 (setq comment-end-pos (- (point) 1))
2246 (setq comment-end-pos end))
2247 (setq comment (buffer-substring
2248 comment-beg-pos comment-end-pos))))
2249 comment)))
2251 ;;;###autoload
2252 (defun image-dired-mark-tagged-files ()
2253 "Use regexp to mark files with matching tag.
2254 A `tag' is a keyword, a piece of meta data, associated with an
2255 image file and stored in image-dired's database file. This command
2256 lets you input a regexp and this will be matched against all tags
2257 on all image files in the database file. The files that have a
2258 matching tag will be marked in the dired buffer."
2259 (interactive)
2260 (image-dired-sane-db-file)
2261 (let ((tag (read-string "Mark tagged files (regexp): "))
2262 (hits 0)
2263 files)
2264 (image-dired--with-db-file
2265 ;; Collect matches
2266 (while (search-forward-regexp
2267 (concat "\\(^[^;\n]+\\);.*" tag ".*$") nil t)
2268 (push (match-string 1) files)))
2269 ;; Mark files
2270 (dolist (curr-file files)
2271 ;; I tried using `dired-mark-files-regexp' but it was waaaay to
2272 ;; slow. Don't bother about hits found in other directories
2273 ;; than the current one.
2274 (when (string= (file-name-as-directory
2275 (expand-file-name default-directory))
2276 (file-name-as-directory
2277 (file-name-directory curr-file)))
2278 (setq curr-file (file-name-nondirectory curr-file))
2279 (goto-char (point-min))
2280 (when (search-forward-regexp (format "\\s %s$" curr-file) nil t)
2281 (setq hits (+ hits 1))
2282 (dired-mark 1))))
2283 (message "%d files with matching tag marked." hits)))
2285 (defun image-dired-mouse-display-image (event)
2286 "Use mouse EVENT, call `image-dired-display-image' to display image.
2287 Track this in associated dired buffer if `image-dired-track-movement' is
2288 non-nil."
2289 (interactive "e")
2290 (mouse-set-point event)
2291 (goto-char (posn-point (event-end event)))
2292 (let ((file (image-dired-original-file-name)))
2293 (when file
2294 (if image-dired-track-movement
2295 (image-dired-track-original-file))
2296 (image-dired-create-display-image-buffer)
2297 (display-buffer image-dired-display-image-buffer)
2298 (image-dired-display-image file))))
2300 (defun image-dired-mouse-select-thumbnail (event)
2301 "Use mouse EVENT to select thumbnail image.
2302 Track this in associated dired buffer if `image-dired-track-movement' is
2303 non-nil."
2304 (interactive "e")
2305 (mouse-set-point event)
2306 (goto-char (posn-point (event-end event)))
2307 (if image-dired-track-movement
2308 (image-dired-track-original-file))
2309 (image-dired-display-thumb-properties))
2311 (defun image-dired-mouse-toggle-mark (event)
2312 "Use mouse EVENT to toggle dired mark for thumbnail.
2313 Track this in associated dired buffer if `image-dired-track-movement' is
2314 non-nil."
2315 (interactive "e")
2316 (mouse-set-point event)
2317 (goto-char (posn-point (event-end event)))
2318 (if image-dired-track-movement
2319 (image-dired-track-original-file))
2320 (image-dired-toggle-mark-thumb-original-file))
2322 (defun image-dired-dired-display-properties ()
2323 "Display properties for dired file in the echo area."
2324 (interactive)
2325 (let* ((file (dired-get-filename))
2326 (file-name (file-name-nondirectory file))
2327 (dired-buf (buffer-name (current-buffer)))
2328 (props (mapconcat #'identity (image-dired-list-tags file) ", "))
2329 (comment (image-dired-get-comment file))
2330 (message-log-max nil))
2331 (if file-name
2332 (message "%s"
2333 (image-dired-format-properties-string
2334 dired-buf
2335 file-name
2336 props
2337 comment)))))
2339 (defvar image-dired-tag-file-list nil
2340 "List to store tag-file structure.")
2342 (defvar image-dired-file-tag-list nil
2343 "List to store file-tag structure.")
2345 (defvar image-dired-file-comment-list nil
2346 "List to store file comments.")
2348 (defun image-dired-add-to-tag-file-list (tag file)
2349 "Add relation between TAG and FILE."
2350 (let (curr)
2351 (if image-dired-tag-file-list
2352 (if (setq curr (assoc tag image-dired-tag-file-list))
2353 (if (not (member file curr))
2354 (setcdr curr (cons file (cdr curr))))
2355 (setcdr image-dired-tag-file-list
2356 (cons (list tag file) (cdr image-dired-tag-file-list))))
2357 (setq image-dired-tag-file-list (list (list tag file))))))
2359 (defun image-dired-add-to-tag-file-lists (tag file)
2360 "Helper function used from `image-dired-create-gallery-lists'.
2362 Add TAG to FILE in one list and FILE to TAG in the other.
2364 Lisp structures look like the following:
2366 image-dired-file-tag-list:
2368 ((\"filename1\" \"tag1\" \"tag2\" \"tag3\" ...)
2369 (\"filename2\" \"tag1\" \"tag2\" \"tag3\" ...)
2370 ...)
2372 image-dired-tag-file-list:
2374 ((\"tag1\" \"filename1\" \"filename2\" \"filename3\" ...)
2375 (\"tag2\" \"filename1\" \"filename2\" \"filename3\" ...)
2376 ...)"
2377 ;; Add tag to file list
2378 (let (curr)
2379 (if image-dired-file-tag-list
2380 (if (setq curr (assoc file image-dired-file-tag-list))
2381 (setcdr curr (cons tag (cdr curr)))
2382 (setcdr image-dired-file-tag-list
2383 (cons (list file tag) (cdr image-dired-file-tag-list))))
2384 (setq image-dired-file-tag-list (list (list file tag))))
2385 ;; Add file to tag list
2386 (if image-dired-tag-file-list
2387 (if (setq curr (assoc tag image-dired-tag-file-list))
2388 (if (not (member file curr))
2389 (setcdr curr (cons file (cdr curr))))
2390 (setcdr image-dired-tag-file-list
2391 (cons (list tag file) (cdr image-dired-tag-file-list))))
2392 (setq image-dired-tag-file-list (list (list tag file))))))
2394 (defun image-dired-add-to-file-comment-list (file comment)
2395 "Helper function used from `image-dired-create-gallery-lists'.
2397 For FILE, add COMMENT to list.
2399 Lisp structure looks like the following:
2401 image-dired-file-comment-list:
2403 ((\"filename1\" . \"comment1\")
2404 (\"filename2\" . \"comment2\")
2405 ...)"
2406 (if image-dired-file-comment-list
2407 (if (not (assoc file image-dired-file-comment-list))
2408 (setcdr image-dired-file-comment-list
2409 (cons (cons file comment)
2410 (cdr image-dired-file-comment-list))))
2411 (setq image-dired-file-comment-list (list (cons file comment)))))
2413 (defun image-dired-create-gallery-lists ()
2414 "Create temporary lists used by `image-dired-gallery-generate'."
2415 (image-dired-sane-db-file)
2416 (image-dired--with-db-file
2417 (let (end beg file row-tags)
2418 (setq image-dired-tag-file-list nil)
2419 (setq image-dired-file-tag-list nil)
2420 (setq image-dired-file-comment-list nil)
2421 (goto-char (point-min))
2422 (while (search-forward-regexp "^." nil t)
2423 (end-of-line)
2424 (setq end (point))
2425 (beginning-of-line)
2426 (setq beg (point))
2427 (unless (search-forward ";" end nil)
2428 (error "Something is really wrong, check format of database"))
2429 (setq row-tags (split-string
2430 (buffer-substring beg end) ";"))
2431 (setq file (car row-tags))
2432 (dolist (x (cdr row-tags))
2433 (if (not (string-match "^comment:\\(.*\\)" x))
2434 (image-dired-add-to-tag-file-lists x file)
2435 (image-dired-add-to-file-comment-list file (match-string 1 x)))))))
2436 ;; Sort tag-file list
2437 (setq image-dired-tag-file-list
2438 (sort image-dired-tag-file-list
2439 (lambda (x y)
2440 (string< (car x) (car y))))))
2442 (defun image-dired-hidden-p (file)
2443 "Return t if image FILE has a \"hidden\" tag."
2444 (cl-loop for tag in (cdr (assoc file image-dired-file-tag-list))
2445 if (member tag image-dired-gallery-hidden-tags) return t))
2447 (defun image-dired-gallery-generate ()
2448 "Generate gallery pages.
2449 First we create a couple of Lisp structures from the database to make
2450 it easier to generate, then HTML-files are created in
2451 `image-dired-gallery-dir'."
2452 (interactive)
2453 (if (eq 'per-directory image-dired-thumbnail-storage)
2454 (error "Currently, gallery generation is not supported \
2455 when using per-directory thumbnail file storage"))
2456 (image-dired-create-gallery-lists)
2457 (let ((tags image-dired-tag-file-list)
2458 (index-file (format "%s/index.html" image-dired-gallery-dir))
2459 count tag tag-file
2460 comment file-tags tag-link tag-link-list)
2461 ;; Make sure gallery root exist
2462 (if (file-exists-p image-dired-gallery-dir)
2463 (if (not (file-directory-p image-dired-gallery-dir))
2464 (error "Variable image-dired-gallery-dir is not a directory"))
2465 (make-directory image-dired-gallery-dir))
2466 ;; Open index file
2467 (with-temp-file index-file
2468 (if (file-exists-p index-file)
2469 (insert-file-contents index-file))
2470 (insert "<html>\n")
2471 (insert " <body>\n")
2472 (insert " <h2>Image-Dired Gallery</h2>\n")
2473 (insert (format "<p>\n Gallery generated %s\n <p>\n"
2474 (current-time-string)))
2475 (insert " <h3>Tag index</h3>\n")
2476 (setq count 1)
2477 ;; Pre-generate list of all tag links
2478 (dolist (curr tags)
2479 (setq tag (car curr))
2480 (when (not (member tag image-dired-gallery-hidden-tags))
2481 (setq tag-link (format "<a href=\"%d.html\">%s</a>" count tag))
2482 (if tag-link-list
2483 (setq tag-link-list
2484 (append tag-link-list (list (cons tag tag-link))))
2485 (setq tag-link-list (list (cons tag tag-link))))
2486 (setq count (1+ count))))
2487 (setq count 1)
2488 ;; Main loop where we generated thumbnail pages per tag
2489 (dolist (curr tags)
2490 (setq tag (car curr))
2491 ;; Don't display hidden tags
2492 (when (not (member tag image-dired-gallery-hidden-tags))
2493 ;; Insert link to tag page in index
2494 (insert (format " %s<br>\n" (cdr (assoc tag tag-link-list))))
2495 ;; Open per-tag file
2496 (setq tag-file (format "%s/%s.html" image-dired-gallery-dir count))
2497 (with-temp-file tag-file
2498 (if (file-exists-p tag-file)
2499 (insert-file-contents tag-file))
2500 (erase-buffer)
2501 (insert "<html>\n")
2502 (insert " <body>\n")
2503 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2504 (insert (format " <h2>Images with tag &quot;%s&quot;</h2>" tag))
2505 ;; Main loop for files per tag page
2506 (dolist (file (cdr curr))
2507 (unless (image-dired-hidden-p file)
2508 ;; Insert thumbnail with link to full image
2509 (insert
2510 (format "<a href=\"%s/%s\"><img src=\"%s/%s\"%s></a>\n"
2511 image-dired-gallery-image-root-url
2512 (file-name-nondirectory file)
2513 image-dired-gallery-thumb-image-root-url
2514 (file-name-nondirectory (image-dired-thumb-name file)) file))
2515 ;; Insert comment, if any
2516 (if (setq comment (cdr (assoc file image-dired-file-comment-list)))
2517 (insert (format "<br>\n%s<br>\n" comment))
2518 (insert "<br>\n"))
2519 ;; Insert links to other tags, if any
2520 (when (> (length
2521 (setq file-tags (assoc file image-dired-file-tag-list))) 2)
2522 (insert "[ ")
2523 (dolist (extra-tag file-tags)
2524 ;; Only insert if not file name or the main tag
2525 (if (and (not (equal extra-tag tag))
2526 (not (equal extra-tag file)))
2527 (insert
2528 (format "%s " (cdr (assoc extra-tag tag-link-list))))))
2529 (insert "]<br>\n"))))
2530 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2531 (insert " </body>\n")
2532 (insert "</html>\n"))
2533 (setq count (1+ count))))
2534 (insert " </body>\n")
2535 (insert "</html>"))))
2537 (defvar image-dired-widget-list nil
2538 "List to keep track of meta data in edit buffer.")
2540 (declare-function widget-forward "wid-edit" (arg))
2542 ;;;###autoload
2543 (defun image-dired-dired-edit-comment-and-tags ()
2544 "Edit comment and tags of current or marked image files.
2545 Edit comment and tags for all marked image files in an
2546 easy-to-use form."
2547 (interactive)
2548 (setq image-dired-widget-list nil)
2549 ;; Setup buffer.
2550 (let ((files (dired-get-marked-files)))
2551 (pop-to-buffer-same-window "*Image-Dired Edit Meta Data*")
2552 (kill-all-local-variables)
2553 (make-local-variable 'widget-example-repeat)
2554 (let ((inhibit-read-only t))
2555 (erase-buffer))
2556 (remove-overlays)
2557 ;; Some help for the user.
2558 (widget-insert
2559 "\nEdit comments and tags for each image. Separate multiple tags
2560 with a comma. Move forward between fields using TAB or RET.
2561 Move to the previous field using backtab (S-TAB). Save by
2562 activating the Save button at the bottom of the form or cancel
2563 the operation by activating the Cancel button.\n\n")
2564 ;; Here comes all images and a comment and tag field for each
2565 ;; image.
2566 (let (thumb-file img comment-widget tag-widget)
2568 (dolist (file files)
2570 (setq thumb-file (image-dired-thumb-name file)
2571 img (create-image thumb-file))
2573 (insert-image img)
2574 (widget-insert "\n\nComment: ")
2575 (setq comment-widget
2576 (widget-create 'editable-field
2577 :size 60
2578 :format "%v "
2579 :value (or (image-dired-get-comment file) "")))
2580 (widget-insert "\nTags: ")
2581 (setq tag-widget
2582 (widget-create 'editable-field
2583 :size 60
2584 :format "%v "
2585 :value (or (mapconcat
2586 #'identity
2587 (image-dired-list-tags file)
2588 ",") "")))
2589 ;; Save information in all widgets so that we can use it when
2590 ;; the user saves the form.
2591 (setq image-dired-widget-list
2592 (append image-dired-widget-list
2593 (list (list file comment-widget tag-widget))))
2594 (widget-insert "\n\n")))
2596 ;; Footer with Save and Cancel button.
2597 (widget-insert "\n")
2598 (widget-create 'push-button
2599 :notify
2600 (lambda (&rest _ignore)
2601 (image-dired-save-information-from-widgets)
2602 (bury-buffer)
2603 (message "Done."))
2604 "Save")
2605 (widget-insert " ")
2606 (widget-create 'push-button
2607 :notify
2608 (lambda (&rest _ignore)
2609 (bury-buffer)
2610 (message "Operation canceled."))
2611 "Cancel")
2612 (widget-insert "\n")
2613 (use-local-map widget-keymap)
2614 (widget-setup)
2615 ;; Jump to the first widget.
2616 (widget-forward 1)))
2618 (defun image-dired-save-information-from-widgets ()
2619 "Save information found in `image-dired-widget-list'.
2620 Use the information in `image-dired-widget-list' to save comments and
2621 tags to their respective image file. Internal function used by
2622 `image-dired-dired-edit-comment-and-tags'."
2623 (let (file comment tag-string tag-list lst)
2624 (image-dired-write-comments
2625 (mapcar
2626 (lambda (widget)
2627 (setq file (car widget)
2628 comment (widget-value (cadr widget)))
2629 (cons file comment))
2630 image-dired-widget-list))
2631 (image-dired-write-tags
2632 (dolist (widget image-dired-widget-list lst)
2633 (setq file (car widget)
2634 tag-string (widget-value (car (cddr widget)))
2635 tag-list (split-string tag-string ","))
2636 (dolist (tag tag-list)
2637 (push (cons file tag) lst))))))
2639 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2640 ;;;;;;;;; TEST-SECTION ;;;;;;;;;;;
2641 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2643 ;; (defvar image-dired-dir-max-size 12300000)
2645 ;; (defun image-dired-test-clean-old-files ()
2646 ;; "Clean `image-dired-dir' from old thumbnail files.
2647 ;; \"Oldness\" measured using last access time. If the total size of all
2648 ;; thumbnail files in `image-dired-dir' is larger than 'image-dired-dir-max-size',
2649 ;; old files are deleted until the max size is reached."
2650 ;; (let* ((files
2651 ;; (sort
2652 ;; (mapcar
2653 ;; (lambda (f)
2654 ;; (let ((fattribs (file-attributes f)))
2655 ;; ;; Get last access time and file size
2656 ;; `(,(nth 4 fattribs) ,(nth 7 fattribs) ,f)))
2657 ;; (directory-files (image-dired-dir) t ".+\\.thumb\\..+$"))
2658 ;; ;; Sort function. Compare time between two files.
2659 ;; (lambda (l1 l2)
2660 ;; (time-less-p (car l1) (car l2)))))
2661 ;; (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) files))))
2662 ;; (while (> dirsize image-dired-dir-max-size)
2663 ;; (y-or-n-p
2664 ;; (format "Size of thumbnail directory: %d, delete old file %s? "
2665 ;; dirsize (cadr (cdar files))))
2666 ;; (delete-file (cadr (cdar files)))
2667 ;; (setq dirsize (- dirsize (car (cdar files))))
2668 ;; (setq files (cdr files)))))
2670 ;;;;;;;;;;;;;;;;;;;;;;,
2672 ;; (defun dired-speedbar-buttons (dired-buffer)
2673 ;; (when (and (boundp 'image-dired-use-speedbar)
2674 ;; image-dired-use-speedbar)
2675 ;; (let ((filename (with-current-buffer dired-buffer
2676 ;; (dired-get-filename))))
2677 ;; (when (and (not (string-equal filename (buffer-string)))
2678 ;; (string-match (image-file-name-regexp) filename))
2679 ;; (erase-buffer)
2680 ;; (insert (propertize
2681 ;; filename
2682 ;; 'display
2683 ;; (image-dired-get-thumbnail-image filename)))))))
2685 ;; (setq image-dired-use-speedbar t)
2687 (provide 'image-dired)
2689 ;;; image-dired.el ends here