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