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