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