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