(tar-header): New field `header-start'.
[emacs.git] / lisp / image-dired.el
blob0bd3ab5641d5eaf40a386555d10d9b6314c2e190
1 ;;; image-dired.el --- use dired to browse and manipulate your images
2 ;;
3 ;; Copyright (C) 2005, 2006, 2007, 2008 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 (concat user-emacs-directory "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 (concat user-emacs-directory "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 (concat user-emacs-directory "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 (concat user-emacs-directory "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 (concat user-emacs-directory "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 the
370 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' and `image-dired-thumb-height'."
401 :type 'integer
402 :group 'image-dired)
404 (defcustom image-dired-thumb-width image-dired-thumb-size
405 "Width of thumbnails, in pixels."
406 :type 'integer
407 :group 'image-dired)
409 (defcustom image-dired-thumb-height image-dired-thumb-size
410 "Height of thumbnails, in pixels."
411 :type 'integer
412 :group 'image-dired)
414 (defcustom image-dired-thumb-relief 2
415 "Size of button-like border around thumbnails."
416 :type 'integer
417 :group 'image-dired)
419 (defcustom image-dired-thumb-margin 2
420 "Size of the margin around thumbnails.
421 This is where you see the cursor."
422 :type 'integer
423 :group 'image-dired)
425 (defcustom image-dired-line-up-method 'dynamic
426 "Default method for line-up of thumbnails in thumbnail buffer.
427 Used by `image-dired-display-thumbs' and other functions that needs to
428 line-up thumbnails. Dynamic means to use the available width of the
429 window containing the thumbnail buffer, Fixed means to use
430 `image-dired-thumbs-per-row', Interactive is for asking the user, and No
431 line-up means that no automatic line-up will be done."
432 :type '(choice :tag "Default line-up method"
433 (const :tag "Dynamic" dynamic)
434 (const :tag "Fixed" fixed)
435 (const :tag "Interactive" interactive)
436 (const :tag "No line-up" none))
437 :group 'image-dired)
439 (defcustom image-dired-thumbs-per-row 3
440 "Number of thumbnails to display per row in thumb buffer."
441 :type 'integer
442 :group 'image-dired)
444 (defcustom image-dired-display-window-width-correction 1
445 "Number to be used to correct image display window width.
446 Change if the default (1) does not work (i.e. if the image does not
447 completely fit)."
448 :type 'integer
449 :group 'image-dired)
451 (defcustom image-dired-display-window-height-correction 0
452 "Number to be used to correct image display window height.
453 Change if the default (0) does not work (i.e. if the image does not
454 completely fit)."
455 :type 'integer
456 :group 'image-dired)
458 (defcustom image-dired-track-movement t
459 "The current state of the tracking and mirroring.
460 For more information, see the documentation for
461 `image-dired-toggle-movement-tracking'."
462 :type 'boolean
463 :group 'image-dired)
465 (defcustom image-dired-append-when-browsing nil
466 "Append thumbnails in thumbnail buffer when browsing.
467 If non-nil, using `image-dired-next-line-and-display' and
468 `image-dired-previous-line-and-display' will leave a trail of thumbnail
469 images in the thumbnail buffer. If you enable this and want to clean
470 the thumbnail buffer because it is filled with too many thumbmnails,
471 just call `image-dired-display-thumb' to display only the image at point.
472 This value can be toggled using `image-dired-toggle-append-browsing'."
473 :type 'boolean
474 :group 'image-dired)
476 (defcustom image-dired-dired-disp-props t
477 "If non-nil, display properties for dired file when browsing.
478 Used by `image-dired-next-line-and-display',
479 `image-dired-previous-line-and-display' and `image-dired-mark-and-display-next'.
480 If the database file is large, this can slow down image browsing in
481 dired and you might want to turn it off."
482 :type 'boolean
483 :group 'image-dired)
485 (defcustom image-dired-display-properties-format "%b: %f (%t): %c"
486 "Display format for thumbnail properties.
487 %b is replaced with associated dired buffer name, %f with file name
488 \(without path) of original image file, %t with the list of tags and %c
489 with the comment."
490 :type 'string
491 :group 'image-dired)
493 (defcustom image-dired-external-viewer
494 ;; TODO: Use mailcap, dired-guess-shell-alist-default,
495 ;; dired-view-command-alist.
496 (cond ((executable-find "display"))
497 ((executable-find "xli"))
498 ((executable-find "qiv") "qiv -t"))
499 "Name of external viewer.
500 Including parameters. Used when displaying original image from
501 `image-dired-thumbnail-mode'."
502 :type 'string
503 :group 'image-dired)
505 (defcustom image-dired-main-image-directory "~/pics/"
506 "Name of main image directory, if any.
507 Used by `image-dired-copy-with-exif-file-name'."
508 :type 'string
509 :group 'image-dired)
511 (defcustom image-dired-show-all-from-dir-max-files 50
512 "Maximum number of files to show using `image-dired-show-all-from-dir'.
513 before warning the user."
514 :type 'integer
515 :group 'image-dired)
517 (defun image-dired-dir ()
518 "Return the current thumbnails directory (from variable `image-dired-dir').
519 Create the thumbnails directory if it does not exist."
520 (let ((image-dired-dir (file-name-as-directory
521 (expand-file-name image-dired-dir))))
522 (unless (file-directory-p image-dired-dir)
523 (make-directory image-dired-dir t)
524 (message "Creating thumbnails directory"))
525 image-dired-dir))
527 (defun image-dired-insert-image (file type relief margin)
528 "Insert image FILE of image TYPE, using RELIEF and MARGIN, at point."
530 (let ((i `(image :type ,type
531 :file ,file
532 :relief ,relief
533 :margin ,margin)))
534 (insert-image i)))
536 (defun image-dired-get-thumbnail-image (file)
537 "Return the image descriptor for a thumbnail of image file FILE."
538 (unless (string-match (image-file-name-regexp) file)
539 (error "%s is not a valid image file" file))
540 (let ((thumb-file (image-dired-thumb-name file)))
541 (unless (and (file-exists-p thumb-file)
542 (<= (float-time (nth 5 (file-attributes file)))
543 (float-time (nth 5 (file-attributes thumb-file)))))
544 (image-dired-create-thumb file thumb-file))
545 (create-image thumb-file)
546 ;; (list 'image :type 'jpeg
547 ;; :file thumb-file
548 ;; :relief image-dired-thumb-relief :margin image-dired-thumb-margin)
551 (defun image-dired-insert-thumbnail (file original-file-name
552 associated-dired-buffer)
553 "Insert thumbnail image FILE.
554 Add text properties ORIGINAL-FILE-NAME and ASSOCIATED-DIRED-BUFFER."
555 (let (beg end)
556 (setq beg (point))
557 (image-dired-insert-image file
558 ;; TODO: this should depend on the real file type
559 (if (eq 'standard image-dired-thumbnail-storage)
560 'png 'jpeg)
561 image-dired-thumb-relief
562 image-dired-thumb-margin)
563 (setq end (point))
564 (add-text-properties
565 beg end
566 (list 'image-dired-thumbnail t
567 'original-file-name original-file-name
568 'associated-dired-buffer associated-dired-buffer
569 'tags (image-dired-list-tags original-file-name)
570 'mouse-face 'highlight
571 'comment (image-dired-get-comment original-file-name)))))
573 (defun image-dired-thumb-name (file)
574 "Return thumbnail file name for FILE.
575 Depending on the value of `image-dired-thumbnail-storage', the file
576 name will vary. For central thumbnail file storage, make a
577 MD5-hash of the image file's directory name and add that to make
578 the thumbnail file name unique. For per-directory storage, just
579 add a subdirectory. For standard storage, produce the file name
580 according to the Thumbnail Managing Standard."
581 (cond ((eq 'standard image-dired-thumbnail-storage)
582 (expand-file-name
583 (concat "~/.thumbnails/normal/"
584 (md5 (concat "file://" (expand-file-name file))) ".png")))
585 ((eq 'use-image-dired-dir image-dired-thumbnail-storage)
586 (let* ((f (expand-file-name file))
587 (md5-hash
588 ;; Is MD5 hashes fast enough? The checksum of a
589 ;; thumbnail file name need not be that
590 ;; "cryptographically" good so a faster one could
591 ;; be used here.
592 (md5 (file-name-as-directory (file-name-directory f)))))
593 (format "%s%s%s.thumb.%s"
594 (file-name-as-directory (expand-file-name (image-dired-dir)))
595 (file-name-sans-extension (file-name-nondirectory f))
596 (if md5-hash (concat "_" md5-hash) "")
597 (file-name-extension f))))
598 ((eq 'per-directory image-dired-thumbnail-storage)
599 (let ((f (expand-file-name file)))
600 (format "%s.image-dired/%s.thumb.%s"
601 (file-name-directory f)
602 (file-name-sans-extension (file-name-nondirectory f))
603 (file-name-extension f))))))
605 (defun image-dired-create-thumb (original-file thumbnail-file)
606 "For ORIGINAL-FILE, create thumbnail image named THUMBNAIL-FILE."
607 (let* ((width (int-to-string image-dired-thumb-width))
608 (height (int-to-string image-dired-thumb-height))
609 (modif-time (format "%.0f" (float-time (nth 5 (file-attributes
610 original-file)))))
611 (thumbnail-nq8-file (replace-regexp-in-string ".png\\'" "-nq8.png"
612 thumbnail-file))
613 (command
614 (format-spec
615 (if (eq 'standard image-dired-thumbnail-storage)
616 image-dired-cmd-create-standard-thumbnail-command
617 image-dired-cmd-create-thumbnail-options)
618 (list
619 (cons ?p image-dired-cmd-create-thumbnail-program)
620 (cons ?w width)
621 (cons ?h height)
622 (cons ?m modif-time)
623 (cons ?f original-file)
624 (cons ?q thumbnail-nq8-file)
625 (cons ?t thumbnail-file))))
626 thumbnail-dir)
627 (when (not (file-exists-p
628 (setq thumbnail-dir (file-name-directory thumbnail-file))))
629 (message "Creating thumbnail directory.")
630 (make-directory thumbnail-dir))
631 (call-process shell-file-name nil nil nil shell-command-switch command)))
633 ;;;###autoload
634 (defun image-dired-dired-insert-marked-thumbs ()
635 "Insert thumbnails before file names of marked files in the dired buffer."
636 (interactive)
637 (dired-map-over-marks
638 (let* ((image-pos (dired-move-to-filename))
639 (image-file (dired-get-filename))
640 (thumb-file (image-dired-get-thumbnail-image image-file))
641 overlay)
642 ;; If image is not already added, then add it.
643 (unless (delq nil (mapcar (lambda (o) (overlay-get o 'put-image))
644 ;; Can't use (overlays-at (point)), BUG?
645 (overlays-in (point) (1+ (point)))))
646 (put-image thumb-file image-pos)
647 (setq
648 overlay
649 (car (delq nil (mapcar (lambda (o) (and (overlay-get o 'put-image) o))
650 (overlays-in (point) (1+ (point)))))))
651 (overlay-put overlay 'image-file image-file)
652 (overlay-put overlay 'thumb-file thumb-file)))
653 nil)
654 (add-hook 'dired-after-readin-hook 'image-dired-dired-after-readin-hook nil t))
656 (defun image-dired-dired-after-readin-hook ()
657 "Relocate existing thumbnail overlays in dired buffer after reverting.
658 Move them to their corresponding files if they are still exist.
659 Otherwise, delete overlays."
660 (mapc (lambda (overlay)
661 (when (overlay-get overlay 'put-image)
662 (let* ((image-file (overlay-get overlay 'image-file))
663 (image-pos (dired-goto-file image-file)))
664 (if image-pos
665 (move-overlay overlay image-pos image-pos)
666 (delete-overlay overlay)))))
667 (overlays-in (point-min) (point-max))))
669 (defun image-dired-next-line-and-display ()
670 "Move to next dired line and display thumbnail image."
671 (interactive)
672 (dired-next-line 1)
673 (image-dired-display-thumbs
674 t (or image-dired-append-when-browsing nil) t)
675 (if image-dired-dired-disp-props
676 (image-dired-dired-display-properties)))
678 (defun image-dired-previous-line-and-display ()
679 "Move to previous dired line and display thumbnail image."
680 (interactive)
681 (dired-previous-line 1)
682 (image-dired-display-thumbs
683 t (or image-dired-append-when-browsing nil) t)
684 (if image-dired-dired-disp-props
685 (image-dired-dired-display-properties)))
687 (defun image-dired-toggle-append-browsing ()
688 "Toggle `image-dired-append-when-browsing'."
689 (interactive)
690 (setq image-dired-append-when-browsing
691 (not image-dired-append-when-browsing))
692 (message "Append browsing %s."
693 (if image-dired-append-when-browsing
694 "on"
695 "off")))
697 (defun image-dired-mark-and-display-next ()
698 "Mark current file in dired and display next thumbnail image."
699 (interactive)
700 (dired-mark 1)
701 (image-dired-display-thumbs
702 t (or image-dired-append-when-browsing nil) t)
703 (if image-dired-dired-disp-props
704 (image-dired-dired-display-properties)))
706 (defun image-dired-toggle-dired-display-properties ()
707 "Toggle `image-dired-dired-disp-props'."
708 (interactive)
709 (setq image-dired-dired-disp-props
710 (not image-dired-dired-disp-props))
711 (message "Dired display properties %s."
712 (if image-dired-dired-disp-props
713 "on"
714 "off")))
716 (defvar image-dired-thumbnail-buffer "*image-dired*"
717 "Image-Dired's thumbnail buffer.")
719 (defun image-dired-create-thumbnail-buffer ()
720 "Create thumb buffer and set `image-dired-thumbnail-mode'."
721 (let ((buf (get-buffer-create image-dired-thumbnail-buffer)))
722 (with-current-buffer buf
723 (setq buffer-read-only t)
724 (if (not (eq major-mode 'image-dired-thumbnail-mode))
725 (image-dired-thumbnail-mode)))
726 buf))
728 (defvar image-dired-display-image-buffer "*image-dired-display-image*"
729 "Where larger versions of the images are display.")
731 (defun image-dired-create-display-image-buffer ()
732 "Create image display buffer and set `image-dired-display-image-mode'."
733 (let ((buf (get-buffer-create image-dired-display-image-buffer)))
734 (with-current-buffer buf
735 (setq buffer-read-only t)
736 (if (not (eq major-mode 'image-dired-display-image-mode))
737 (image-dired-display-image-mode)))
738 buf))
740 (defvar image-dired-saved-window-configuration nil
741 "Saved window configuration.")
743 ;;;###autoload
744 (defun image-dired-dired-with-window-configuration (dir &optional arg)
745 "Open directory DIR and create a default window configuration.
747 Convenience command that:
749 - Opens dired in folder DIR
750 - Splits windows in most useful (?) way
751 - Set `truncate-lines' to t
753 After the command has finished, you would typically mark some
754 image files in dired and type
755 \\[image-dired-display-thumbs] (`image-dired-display-thumbs').
757 If called with prefix argument ARG, skip splitting of windows.
759 The current window configuration is saved and can be restored by
760 calling `image-dired-restore-window-configuration'."
761 (interactive "DDirectory: \nP")
762 (let ((buf (image-dired-create-thumbnail-buffer))
763 (buf2 (image-dired-create-display-image-buffer)))
764 (setq image-dired-saved-window-configuration
765 (current-window-configuration))
766 (dired dir)
767 (delete-other-windows)
768 (when (not arg)
769 (split-window-horizontally)
770 (setq truncate-lines t)
771 (save-excursion
772 (other-window 1)
773 (switch-to-buffer buf)
774 (split-window-vertically)
775 (other-window 1)
776 (switch-to-buffer buf2)
777 (other-window -2)))))
779 (defun image-dired-restore-window-configuration ()
780 "Restore window configuration.
781 Restore any changes to the window configuration made by calling
782 `image-dired-dired-with-window-configuration'."
783 (interactive)
784 (if image-dired-saved-window-configuration
785 (set-window-configuration image-dired-saved-window-configuration)
786 (message "No saved window configuration")))
788 ;;;###autoload
789 (defun image-dired-display-thumbs (&optional arg append do-not-pop)
790 "Display thumbnails of all marked files, in `image-dired-thumbnail-buffer'.
791 If a thumbnail image does not exist for a file, it is created on the
792 fly. With prefix argument ARG, display only thumbnail for file at
793 point (this is useful if you have marked some files but want to show
794 another one).
796 Recommended usage is to split the current frame horizontally so that
797 you have the dired buffer in the left window and the
798 `image-dired-thumbnail-buffer' buffer in the right window.
800 With optional argument APPEND, append thumbnail to thumbnail buffer
801 instead of erasing it first.
803 Option argument DO-NOT-POP controls if `pop-to-buffer' should be
804 used or not. If non-nil, use `display-buffer' instead of
805 `pop-to-buffer'. This is used from functions like
806 `image-dired-next-line-and-display' and
807 `image-dired-previous-line-and-display' where we do not want the
808 thumbnail buffer to be selected."
809 (interactive "P")
810 (let ((buf (image-dired-create-thumbnail-buffer))
811 curr-file thumb-name files count dired-buf beg)
812 (if arg
813 (setq files (list (dired-get-filename)))
814 (setq files (dired-get-marked-files)))
815 (setq dired-buf (current-buffer))
816 (with-current-buffer buf
817 (let ((inhibit-read-only t))
818 (if (not append)
819 (erase-buffer)
820 (goto-char (point-max)))
821 (mapc
822 (lambda (curr-file)
823 (setq thumb-name (image-dired-thumb-name curr-file))
824 (if (and (not (file-exists-p thumb-name))
825 (not (= 0 (image-dired-create-thumb curr-file thumb-name))))
826 (message "Thumb could not be created for file %s" curr-file)
827 (image-dired-insert-thumbnail thumb-name curr-file dired-buf)))
828 files))
829 (cond ((eq 'dynamic image-dired-line-up-method)
830 (image-dired-line-up-dynamic))
831 ((eq 'fixed image-dired-line-up-method)
832 (image-dired-line-up))
833 ((eq 'interactive image-dired-line-up-method)
834 (image-dired-line-up-interactive))
835 ((eq 'none image-dired-line-up-method)
836 nil)
838 (image-dired-line-up-dynamic))))
839 (if do-not-pop
840 (display-buffer image-dired-thumbnail-buffer)
841 (pop-to-buffer image-dired-thumbnail-buffer))))
843 ;;;###autoload
844 (defun image-dired-show-all-from-dir (dir)
845 "Make a preview buffer for all images in DIR and display it.
846 If the number of files in DIR matching `image-file-name-regexp'
847 exceeds `image-dired-show-all-from-dir-max-files', a warning will be
848 displayed."
849 (interactive "DDir: ")
850 (dired dir)
851 (dired-mark-files-regexp (image-file-name-regexp))
852 (let ((files (dired-get-marked-files)))
853 (if (or (<= (length files) image-dired-show-all-from-dir-max-files)
854 (and (> (length files) image-dired-show-all-from-dir-max-files)
855 (y-or-n-p
856 (format
857 "Directory contains more than %d image files. Proceed? "
858 image-dired-show-all-from-dir-max-files))))
859 (progn
860 (image-dired-display-thumbs)
861 (pop-to-buffer image-dired-thumbnail-buffer))
862 (message "Cancelled."))))
864 ;;;###autoload
865 (defalias 'image-dired 'image-dired-show-all-from-dir)
867 ;;;###autoload
868 (defalias 'tumme 'image-dired-show-all-from-dir)
870 (defun image-dired-sane-db-file ()
871 "Check if `image-dired-db-file' exists.
872 If not, try to create it (including any parent directories).
873 Signal error if there are problems creating it."
874 (or (file-exists-p image-dired-db-file)
875 (let (dir buf)
876 (unless (file-directory-p (setq dir (file-name-directory
877 image-dired-db-file)))
878 (make-directory dir t))
879 (with-current-buffer (setq buf (create-file-buffer
880 image-dired-db-file))
881 (write-file image-dired-db-file))
882 (kill-buffer buf)
883 (file-exists-p image-dired-db-file))
884 (error "Could not create %s" image-dired-db-file)))
886 (defun image-dired-write-tags (file-tags)
887 "Write file tags to database.
888 Write each file and tag in FILE-TAGS to the database. FILE-TAGS
889 is an alist in the following form:
890 ((FILE . TAG) ... )"
891 (image-dired-sane-db-file)
892 (let (end file tag)
893 (with-temp-file image-dired-db-file
894 (insert-file-contents image-dired-db-file)
895 (dolist (elt file-tags)
896 (setq file (car elt)
897 tag (cdr elt))
898 (goto-char (point-min))
899 (if (search-forward-regexp (format "^%s.*$" file) nil t)
900 (progn
901 (setq end (point))
902 (beginning-of-line)
903 (when (not (search-forward (format ";%s" tag) end t))
904 (end-of-line)
905 (insert (format ";%s" tag))))
906 (goto-char (point-max))
907 (insert (format "\n%s;%s" file tag)))))))
909 (defun image-dired-remove-tag (files tag)
910 "For all FILES, remove TAG from the image database."
911 (image-dired-sane-db-file)
912 (save-excursion
913 (let (end buf start)
914 (setq buf (find-file image-dired-db-file))
915 (if (not (listp files))
916 (if (stringp files)
917 (setq files (list files))
918 (error "Files must be a string or a list of strings!")))
919 (mapc
920 (lambda (file)
921 (goto-char (point-min))
922 (when (search-forward-regexp
923 (format "^%s" file) nil t)
924 (end-of-line)
925 (setq end (point))
926 (beginning-of-line)
927 (when (search-forward-regexp (format "\\(;%s\\)" tag) end t)
928 (delete-region (match-beginning 1) (match-end 1))
929 ;; Check if file should still be in the database. If
930 ;; it has no tags or comments, it will be removed.
931 (end-of-line)
932 (setq end (point))
933 (beginning-of-line)
934 (when (not (search-forward ";" end t))
935 (kill-line 1)
936 ;; If on empty line at end of buffer
937 (when (and (eobp)
938 (looking-at "^$"))
939 (delete-backward-char 1))))))
940 files)
941 (save-buffer)
942 (kill-buffer buf))))
944 (defun image-dired-list-tags (file)
945 "Read all tags for image FILE from the image database."
946 (image-dired-sane-db-file)
947 (save-excursion
948 (let (end buf (tags ""))
949 (setq buf (find-file image-dired-db-file))
950 (goto-char (point-min))
951 (when (search-forward-regexp
952 (format "^%s" file) nil t)
953 (end-of-line)
954 (setq end (point))
955 (beginning-of-line)
956 (if (search-forward ";" end t)
957 (if (search-forward "comment:" end t)
958 (if (search-forward ";" end t)
959 (setq tags (buffer-substring (point) end)))
960 (setq tags (buffer-substring (point) end)))))
961 (kill-buffer buf)
962 (split-string tags ";"))))
964 ;;;###autoload
965 (defun image-dired-tag-files (arg)
966 "Tag marked file(s) in dired. With prefix ARG, tag file at point."
967 (interactive "P")
968 (let ((tag (read-string "Tags to add (separate tags with a semicolon): "))
969 curr-file files)
970 (if arg
971 (setq files (list (dired-get-filename)))
972 (setq files (dired-get-marked-files)))
973 (image-dired-write-tags
974 (mapcar
975 (lambda (x)
976 (cons x tag))
977 files))))
979 (defun image-dired-tag-thumbnail ()
980 "Tag current thumbnail."
981 (interactive)
982 (let ((tag (read-string "Tags to add (separate tags with a semicolon): ")))
983 (image-dired-write-tags (list (cons (image-dired-original-file-name) tag))))
984 (image-dired-update-property
985 'tags (image-dired-list-tags (image-dired-original-file-name))))
987 ;;;###autoload
988 (defun image-dired-delete-tag (arg)
989 "Remove tag for selected file(s).
990 With prefix argument ARG, remove tag from file at point."
991 (interactive "P")
992 (let ((tag (read-string "Tag to remove: "))
993 files)
994 (if arg
995 (setq files (list (dired-get-filename)))
996 (setq files (dired-get-marked-files)))
997 (image-dired-remove-tag files tag)))
999 (defun image-dired-tag-thumbnail-remove ()
1000 "Remove tag from thumbnail."
1001 (interactive)
1002 (let ((tag (read-string "Tag to remove: ")))
1003 (image-dired-remove-tag (image-dired-original-file-name) tag))
1004 (image-dired-update-property
1005 'tags (image-dired-list-tags (image-dired-original-file-name))))
1007 (defun image-dired-original-file-name ()
1008 "Get original file name for thumbnail or display image at point."
1009 (get-text-property (point) 'original-file-name))
1011 (defun image-dired-associated-dired-buffer ()
1012 "Get associated dired buffer at point."
1013 (get-text-property (point) 'associated-dired-buffer))
1015 (defun image-dired-get-buffer-window (buf)
1016 "Return window where buffer BUF is."
1017 (get-window-with-predicate
1018 (lambda (window)
1019 (equal (window-buffer window) buf))
1020 nil t))
1022 (defun image-dired-track-original-file ()
1023 "Track the original file in the associated dired buffer.
1024 See documentation for `image-dired-toggle-movement-tracking'. Interactive
1025 use only useful if `image-dired-track-movement' is nil."
1026 (interactive)
1027 (let ((old-buf (current-buffer))
1028 (dired-buf (image-dired-associated-dired-buffer))
1029 (file-name (image-dired-original-file-name)))
1030 (when (and (buffer-live-p dired-buf) file-name)
1031 (set-buffer dired-buf)
1032 (if (not (dired-goto-file file-name))
1033 (message "Could not track file")
1034 (set-window-point
1035 (image-dired-get-buffer-window dired-buf) (point)))
1036 (set-buffer old-buf))))
1038 (defun image-dired-toggle-movement-tracking ()
1039 "Turn on and off `image-dired-track-movement'.
1040 Tracking of the movements between thumbnail and dired buffer so that
1041 they are \"mirrored\" in the dired buffer. When this is on, moving
1042 around in the thumbnail or dired buffer will find the matching
1043 position in the other buffer."
1044 (interactive)
1045 (setq image-dired-track-movement (not image-dired-track-movement))
1046 (message "Tracking %s" (if image-dired-track-movement "on" "off")))
1048 (defun image-dired-track-thumbnail ()
1049 "Track current dired file's thumb in `image-dired-thumbnail-buffer'.
1050 This is almost the same as what `image-dired-track-original-file' does, but
1051 the other way around."
1052 (let ((file (dired-get-filename))
1053 (old-buf (current-buffer))
1054 prop-val found)
1055 (when (get-buffer image-dired-thumbnail-buffer)
1056 (set-buffer image-dired-thumbnail-buffer)
1057 (goto-char (point-min))
1058 (while (and (not (eobp))
1059 (not found))
1060 (if (and (setq prop-val
1061 (get-text-property (point) 'original-file-name))
1062 (string= prop-val file))
1063 (setq found t))
1064 (if (not found)
1065 (forward-char 1)))
1066 (when found
1067 (set-window-point
1068 (image-dired-thumbnail-window) (point))
1069 (image-dired-display-thumb-properties))
1070 (set-buffer old-buf))))
1072 (defun image-dired-dired-next-line (&optional arg)
1073 "Call `dired-next-line', then track thumbnail.
1074 This can safely replace `dired-next-line'. With prefix argument, move
1075 ARG lines."
1076 (interactive "P")
1077 (dired-next-line (or arg 1))
1078 (if image-dired-track-movement
1079 (image-dired-track-thumbnail)))
1081 (defun image-dired-dired-previous-line (&optional arg)
1082 "Call `dired-previous-line', then track thumbnail.
1083 This can safely replace `dired-previous-line'. With prefix argument,
1084 move ARG lines."
1085 (interactive "P")
1086 (dired-previous-line (or arg 1))
1087 (if image-dired-track-movement
1088 (image-dired-track-thumbnail)))
1090 (defun image-dired-forward-image (&optional arg)
1091 "Move to next image and display properties.
1092 Optional prefix ARG says how many images to move; default is one
1093 image."
1094 (interactive "p")
1095 (let (pos (steps (or arg 1)))
1096 (dotimes (i steps)
1097 (if (and (not (eobp))
1098 (save-excursion
1099 (forward-char)
1100 (while (and (not (eobp))
1101 (not (image-dired-image-at-point-p)))
1102 (forward-char))
1103 (setq pos (point))
1104 (image-dired-image-at-point-p)))
1105 (goto-char pos)
1106 (error "At last image"))))
1107 (when image-dired-track-movement
1108 (image-dired-track-original-file))
1109 (image-dired-display-thumb-properties))
1111 (defun image-dired-backward-image (&optional arg)
1112 "Move to previous image and display properties.
1113 Optional prefix ARG says how many images to move; default is one
1114 image."
1115 (interactive "p")
1116 (let (pos (steps (or arg 1)))
1117 (dotimes (i steps)
1118 (if (and (not (bobp))
1119 (save-excursion
1120 (backward-char)
1121 (while (and (not (bobp))
1122 (not (image-dired-image-at-point-p)))
1123 (backward-char))
1124 (setq pos (point))
1125 (image-dired-image-at-point-p)))
1126 (goto-char pos)
1127 (error "At first image"))))
1128 (when image-dired-track-movement
1129 (image-dired-track-original-file))
1130 (image-dired-display-thumb-properties))
1132 (defun image-dired-next-line ()
1133 "Move to next line and display properties."
1134 (interactive)
1135 (forward-line 1)
1136 ;; If we end up in an empty spot, back up to the next thumbnail.
1137 (if (not (image-dired-image-at-point-p))
1138 (image-dired-backward-image))
1139 (if image-dired-track-movement
1140 (image-dired-track-original-file))
1141 (image-dired-display-thumb-properties))
1144 (defun image-dired-previous-line ()
1145 "Move to previous line and display properties."
1146 (interactive)
1147 (forward-line -1)
1148 ;; If we end up in an empty spot, back up to the next
1149 ;; thumbnail. This should only happen if the user deleted a
1150 ;; thumbnail and did not refresh, so it is not very common. But we
1151 ;; can handle it in a good manner, so why not?
1152 (if (not (image-dired-image-at-point-p))
1153 (image-dired-backward-image))
1154 (if image-dired-track-movement
1155 (image-dired-track-original-file))
1156 (image-dired-display-thumb-properties))
1158 (defun image-dired-format-properties-string (buf file props comment)
1159 "Format display properties.
1160 BUF is the associated dired buffer, FILE is the original image file
1161 name, PROPS is a list of tags and COMMENT is the image files's
1162 comment."
1163 (format-spec
1164 image-dired-display-properties-format
1165 (list
1166 (cons ?b (or buf ""))
1167 (cons ?f file)
1168 (cons ?t (or (princ props) ""))
1169 (cons ?c (or comment "")))))
1171 (defun image-dired-display-thumb-properties ()
1172 "Display thumbnail properties in the echo area."
1173 (if (not (eobp))
1174 (let ((file-name (file-name-nondirectory (image-dired-original-file-name)))
1175 (dired-buf (buffer-name (image-dired-associated-dired-buffer)))
1176 (props (mapconcat
1177 'princ
1178 (get-text-property (point) 'tags)
1179 ", "))
1180 (comment (get-text-property (point) 'comment)))
1181 (if file-name
1182 (message "%s"
1183 (image-dired-format-properties-string
1184 dired-buf
1185 file-name
1186 props
1187 comment))))))
1189 (defun image-dired-dired-file-marked-p ()
1190 "Check whether file on current line is marked or not."
1191 (save-excursion
1192 (beginning-of-line)
1193 (not (looking-at "^ .*$"))))
1195 (defun image-dired-modify-mark-on-thumb-original-file (command)
1196 "Modify mark in dired buffer.
1197 COMMAND is one of 'mark for marking file in dired, 'unmark for
1198 unmarking file in dired or 'flag for flagging file for delete in
1199 dired."
1200 (let ((file-name (image-dired-original-file-name))
1201 (dired-buf (image-dired-associated-dired-buffer)))
1202 (if (not (and dired-buf file-name))
1203 (message "No image, or image with correct properties, at point.")
1204 (with-current-buffer dired-buf
1205 (message "%s" file-name)
1206 (if (dired-goto-file file-name)
1207 (cond ((eq command 'mark) (dired-mark 1))
1208 ((eq command 'unmark) (dired-unmark 1))
1209 ((eq command 'toggle)
1210 (if (image-dired-dired-file-marked-p)
1211 (dired-unmark 1)
1212 (dired-mark 1)))
1213 ((eq command 'flag) (dired-flag-file-deletion 1))))))))
1215 (defun image-dired-mark-thumb-original-file ()
1216 "Mark original image file in associated dired buffer."
1217 (interactive)
1218 (image-dired-modify-mark-on-thumb-original-file 'mark)
1219 (image-dired-forward-image))
1221 (defun image-dired-unmark-thumb-original-file ()
1222 "Unmark original image file in associated dired buffer."
1223 (interactive)
1224 (image-dired-modify-mark-on-thumb-original-file 'unmark)
1225 (image-dired-forward-image))
1227 (defun image-dired-flag-thumb-original-file ()
1228 "Flag original image file for deletion in associated dired buffer."
1229 (interactive)
1230 (image-dired-modify-mark-on-thumb-original-file 'flag)
1231 (image-dired-forward-image))
1233 (defun image-dired-toggle-mark-thumb-original-file ()
1234 "Toggle mark on original image file in associated dired buffer."
1235 (interactive)
1236 (image-dired-modify-mark-on-thumb-original-file 'toggle))
1238 (defun image-dired-jump-original-dired-buffer ()
1239 "Jump to the dired buffer associated with the current image file.
1240 You probably want to use this together with
1241 `image-dired-track-original-file'."
1242 (interactive)
1243 (let ((buf (image-dired-associated-dired-buffer))
1244 window frame)
1245 (setq window (image-dired-get-buffer-window buf))
1246 (if window
1247 (progn
1248 (if (not (equal (selected-frame) (setq frame (window-frame window))))
1249 (select-frame-set-input-focus frame))
1250 (select-window window))
1251 (message "Associated dired buffer not visible"))))
1253 ;;;###autoload
1254 (defun image-dired-jump-thumbnail-buffer ()
1255 "Jump to thumbnail buffer."
1256 (interactive)
1257 (let ((window (image-dired-thumbnail-window))
1258 frame)
1259 (if window
1260 (progn
1261 (if (not (equal (selected-frame) (setq frame (window-frame window))))
1262 (select-frame-set-input-focus frame))
1263 (select-window window))
1264 (message "Thumbnail buffer not visible"))))
1266 (defvar image-dired-thumbnail-mode-map (make-sparse-keymap)
1267 "Keymap for `image-dired-thumbnail-mode'.")
1269 (defvar image-dired-thumbnail-mode-line-up-map (make-sparse-keymap)
1270 "Keymap for line-up commands in `image-dired-thumbnail-mode'.")
1272 (defvar image-dired-thumbnail-mode-tag-map (make-sparse-keymap)
1273 "Keymap for tag commands in `image-dired-thumbnail-mode'.")
1275 (defun image-dired-define-thumbnail-mode-keymap ()
1276 "Define keymap for `image-dired-thumbnail-mode'."
1278 ;; Keys
1279 (define-key image-dired-thumbnail-mode-map [right] 'image-dired-forward-image)
1280 (define-key image-dired-thumbnail-mode-map [left] 'image-dired-backward-image)
1281 (define-key image-dired-thumbnail-mode-map [up] 'image-dired-previous-line)
1282 (define-key image-dired-thumbnail-mode-map [down] 'image-dired-next-line)
1283 (define-key image-dired-thumbnail-mode-map "\C-f" 'image-dired-forward-image)
1284 (define-key image-dired-thumbnail-mode-map "\C-b" 'image-dired-backward-image)
1285 (define-key image-dired-thumbnail-mode-map "\C-p" 'image-dired-previous-line)
1286 (define-key image-dired-thumbnail-mode-map "\C-n" 'image-dired-next-line)
1288 (define-key image-dired-thumbnail-mode-map "d" 'image-dired-flag-thumb-original-file)
1289 (define-key image-dired-thumbnail-mode-map [delete]
1290 'image-dired-flag-thumb-original-file)
1291 (define-key image-dired-thumbnail-mode-map "m" 'image-dired-mark-thumb-original-file)
1292 (define-key image-dired-thumbnail-mode-map "u" 'image-dired-unmark-thumb-original-file)
1293 (define-key image-dired-thumbnail-mode-map "." 'image-dired-track-original-file)
1294 (define-key image-dired-thumbnail-mode-map [tab] 'image-dired-jump-original-dired-buffer)
1296 ;; add line-up map
1297 (define-key image-dired-thumbnail-mode-map "g" image-dired-thumbnail-mode-line-up-map)
1299 ;; map it to "g" so that the user can press it more quickly
1300 (define-key image-dired-thumbnail-mode-line-up-map "g" 'image-dired-line-up-dynamic)
1301 ;; "f" for "fixed" number of thumbs per row
1302 (define-key image-dired-thumbnail-mode-line-up-map "f" 'image-dired-line-up)
1303 ;; "i" for "interactive"
1304 (define-key image-dired-thumbnail-mode-line-up-map "i" 'image-dired-line-up-interactive)
1306 ;; add tag map
1307 (define-key image-dired-thumbnail-mode-map "t" image-dired-thumbnail-mode-tag-map)
1309 ;; map it to "t" so that the user can press it more quickly
1310 (define-key image-dired-thumbnail-mode-tag-map "t" 'image-dired-tag-thumbnail)
1311 ;; "r" for "remove"
1312 (define-key image-dired-thumbnail-mode-tag-map "r" 'image-dired-tag-thumbnail-remove)
1314 (define-key image-dired-thumbnail-mode-map "\C-m"
1315 'image-dired-display-thumbnail-original-image)
1316 (define-key image-dired-thumbnail-mode-map [C-return]
1317 'image-dired-thumbnail-display-external)
1319 (define-key image-dired-thumbnail-mode-map "l" 'image-dired-rotate-thumbnail-left)
1320 (define-key image-dired-thumbnail-mode-map "r" 'image-dired-rotate-thumbnail-right)
1322 (define-key image-dired-thumbnail-mode-map "L" 'image-dired-rotate-original-left)
1323 (define-key image-dired-thumbnail-mode-map "R" 'image-dired-rotate-original-right)
1325 (define-key image-dired-thumbnail-mode-map "D"
1326 'image-dired-thumbnail-set-image-description)
1328 (define-key image-dired-thumbnail-mode-map "\C-d" 'image-dired-delete-char)
1329 (define-key image-dired-thumbnail-mode-map " "
1330 'image-dired-display-next-thumbnail-original)
1331 (define-key image-dired-thumbnail-mode-map
1332 (kbd "DEL") 'image-dired-display-previous-thumbnail-original)
1333 (define-key image-dired-thumbnail-mode-map "c" 'image-dired-comment-thumbnail)
1334 (define-key image-dired-thumbnail-mode-map "q" 'image-dired-kill-buffer-and-window)
1336 ;; Mouse
1337 (define-key image-dired-thumbnail-mode-map [mouse-2] 'image-dired-mouse-display-image)
1338 (define-key image-dired-thumbnail-mode-map [mouse-1] 'image-dired-mouse-select-thumbnail)
1340 ;; Seems I must first set C-down-mouse-1 to undefined, or else it
1341 ;; will trigger the buffer menu. If I try to instead bind
1342 ;; C-down-mouse-1 to `image-dired-mouse-toggle-mark', I get a message
1343 ;; about C-mouse-1 not being defined afterwards. Annoying, but I
1344 ;; probably do not completely understand mouse events.
1346 (define-key image-dired-thumbnail-mode-map [C-down-mouse-1] 'undefined)
1347 (define-key image-dired-thumbnail-mode-map [C-mouse-1] 'image-dired-mouse-toggle-mark)
1349 ;; Menu
1350 (define-key image-dired-thumbnail-mode-map [menu-bar image-dired]
1351 (cons "Image-Dired" (make-sparse-keymap "Image-Dired")))
1353 (define-key image-dired-thumbnail-mode-map
1354 [menu-bar image-dired image-dired-kill-buffer-and-window]
1355 '("Quit" . image-dired-kill-buffer-and-window))
1357 (define-key image-dired-thumbnail-mode-map
1358 [menu-bar image-dired image-dired-delete-char]
1359 '("Delete thumbnail from buffer" . image-dired-delete-char))
1361 (define-key image-dired-thumbnail-mode-map
1362 [menu-bar image-dired image-dired-tag-thumbnail-remove]
1363 '("Remove tag from thumbnail" . image-dired-tag-thumbnail-remove))
1365 (define-key image-dired-thumbnail-mode-map
1366 [menu-bar image-dired image-dired-tag-thumbnail]
1367 '("Tag thumbnail" . image-dired-tag-thumbnail))
1369 (define-key image-dired-thumbnail-mode-map
1370 [menu-bar image-dired image-dired-comment-thumbnail]
1371 '("Comment thumbnail" . image-dired-comment-thumbnail))
1373 (define-key image-dired-thumbnail-mode-map
1374 [menu-bar image-dired image-dired-refresh-thumb]
1375 '("Refresh thumb" . image-dired-refresh-thumb))
1376 (define-key image-dired-thumbnail-mode-map
1377 [menu-bar image-dired image-dired-line-up-dynamic]
1378 '("Dynamic line up" . image-dired-line-up-dynamic))
1379 (define-key image-dired-thumbnail-mode-map
1380 [menu-bar image-dired image-dired-line-up]
1381 '("Line up thumbnails" . image-dired-line-up))
1383 (define-key image-dired-thumbnail-mode-map
1384 [menu-bar image-dired image-dired-rotate-thumbnail-left]
1385 '("Rotate thumbnail left" . image-dired-rotate-thumbnail-left))
1386 (define-key image-dired-thumbnail-mode-map
1387 [menu-bar image-dired image-dired-rotate-thumbnail-right]
1388 '("Rotate thumbnail right" . image-dired-rotate-thumbnail-right))
1390 (define-key image-dired-thumbnail-mode-map
1391 [menu-bar image-dired image-dired-rotate-original-left]
1392 '("Rotate original left" . image-dired-rotate-original-left))
1393 (define-key image-dired-thumbnail-mode-map
1394 [menu-bar image-dired image-dired-rotate-original-right]
1395 '("Rotate original right" . image-dired-rotate-original-right))
1397 (define-key image-dired-thumbnail-mode-map
1398 [menu-bar image-dired image-dired-toggle-movement-tracking]
1399 '("Toggle movement tracking on/off" . image-dired-toggle-movement-tracking))
1401 (define-key image-dired-thumbnail-mode-map
1402 [menu-bar image-dired image-dired-jump-original-dired-buffer]
1403 '("Jump to dired buffer" . image-dired-jump-original-dired-buffer))
1404 (define-key image-dired-thumbnail-mode-map
1405 [menu-bar image-dired image-dired-track-original-file]
1406 '("Track original" . image-dired-track-original-file))
1408 (define-key image-dired-thumbnail-mode-map
1409 [menu-bar image-dired image-dired-flag-thumb-original-file]
1410 '("Flag original for deletion" . image-dired-flag-thumb-original-file))
1411 (define-key image-dired-thumbnail-mode-map
1412 [menu-bar image-dired image-dired-unmark-thumb-original-file]
1413 '("Unmark original" . image-dired-unmark-thumb-original-file))
1414 (define-key image-dired-thumbnail-mode-map
1415 [menu-bar image-dired image-dired-mark-thumb-original-file]
1416 '("Mark original" . image-dired-mark-thumb-original-file))
1418 (define-key image-dired-thumbnail-mode-map
1419 [menu-bar image-dired image-dired-thumbnail-display-external]
1420 '("Display in external viewer" . image-dired-thumbnail-display-external))
1421 (define-key image-dired-thumbnail-mode-map
1422 [menu-bar image-dired image-dired-display-thumbnail-original-image]
1423 '("Display image" . image-dired-display-thumbnail-original-image)))
1425 (defvar image-dired-display-image-mode-map (make-sparse-keymap)
1426 "Keymap for `image-dired-display-image-mode'.")
1428 (defun image-dired-define-display-image-mode-keymap ()
1429 "Define keymap for `image-dired-display-image-mode'."
1431 ;; Keys
1432 (define-key image-dired-display-image-mode-map "q" 'image-dired-kill-buffer-and-window)
1434 (define-key image-dired-display-image-mode-map "f"
1435 'image-dired-display-current-image-full)
1437 (define-key image-dired-display-image-mode-map "s"
1438 'image-dired-display-current-image-sized)
1440 ;; Menu
1441 (define-key image-dired-display-image-mode-map [menu-bar image-dired]
1442 (cons "Image-Dired" (make-sparse-keymap "Image-Dired")))
1444 (define-key image-dired-display-image-mode-map
1445 [menu-bar image-dired image-dired-kill-buffer-and-window]
1446 '("Quit" . image-dired-kill-buffer-and-window))
1448 (define-key image-dired-display-image-mode-map
1449 [menu-bar image-dired image-dired-display-current-image-sized]
1450 '("Display original, sized to fit" . image-dired-display-current-image-sized))
1452 (define-key image-dired-display-image-mode-map
1453 [menu-bar image-dired image-dired-display-current-image-full]
1454 '("Display original, full size" . image-dired-display-current-image-full))
1458 (defun image-dired-display-current-image-full ()
1459 "Display current image in full size."
1460 (interactive)
1461 (let ((file (image-dired-original-file-name)))
1462 (if file
1463 (progn
1464 (image-dired-display-image file t)
1465 (message "Full size image displayed"))
1466 (error "No original file name at point"))))
1468 (defun image-dired-display-current-image-sized ()
1469 "Display current image in sized to fit window dimensions."
1470 (interactive)
1471 (let ((file (image-dired-original-file-name)))
1472 (if file
1473 (progn
1474 (image-dired-display-image file)
1475 (message "Full size image displayed"))
1476 (error "No original file name at point"))))
1478 (define-derived-mode image-dired-thumbnail-mode
1479 fundamental-mode "image-dired-thumbnail"
1480 "Browse and manipulate thumbnail images using dired.
1481 Use `image-dired-dired' and `image-dired-setup-dired-keybindings' to get a
1482 nice setup to start with."
1483 (image-dired-define-thumbnail-mode-keymap)
1484 (message "image-dired-thumbnail-mode enabled"))
1486 (define-derived-mode image-dired-display-image-mode
1487 fundamental-mode "image-dired-image-display"
1488 "Mode for displaying and manipulating original image.
1489 Resized or in full-size."
1490 (image-dired-define-display-image-mode-keymap)
1491 (message "image-dired-display-image-mode enabled"))
1493 ;;;###autoload
1494 (defun image-dired-setup-dired-keybindings ()
1495 "Setup easy-to-use keybindings for the commands to be used in dired mode.
1496 Note that n, p and <down> and <up> will be hijacked and bound to
1497 `image-dired-dired-x-line'."
1498 (interactive)
1500 ;; Hijack previous and next line movement. Let C-p and C-b be
1501 ;; though...
1503 (define-key dired-mode-map "p" 'image-dired-dired-previous-line)
1504 (define-key dired-mode-map "n" 'image-dired-dired-next-line)
1505 (define-key dired-mode-map [up] 'image-dired-dired-previous-line)
1506 (define-key dired-mode-map [down] 'image-dired-dired-next-line)
1508 (define-key dired-mode-map (kbd "C-S-n") 'image-dired-next-line-and-display)
1509 (define-key dired-mode-map (kbd "C-S-p") 'image-dired-previous-line-and-display)
1510 (define-key dired-mode-map (kbd "C-S-m") 'image-dired-mark-and-display-next)
1512 (define-key dired-mode-map "\C-td" 'image-dired-display-thumbs)
1513 (define-key dired-mode-map "\C-tt" 'image-dired-tag-files)
1514 (define-key dired-mode-map "\C-tr" 'image-dired-delete-tag)
1515 (define-key dired-mode-map [tab] 'image-dired-jump-thumbnail-buffer)
1516 (define-key dired-mode-map "\C-ti" 'image-dired-dired-display-image)
1517 (define-key dired-mode-map "\C-tx" 'image-dired-dired-display-external)
1518 (define-key dired-mode-map "\C-ta" 'image-dired-display-thumbs-append)
1519 (define-key dired-mode-map "\C-t." 'image-dired-display-thumb)
1520 (define-key dired-mode-map "\C-tc" 'image-dired-dired-comment-files)
1521 (define-key dired-mode-map "\C-tf" 'image-dired-mark-tagged-files)
1523 ;; Menu for dired
1524 (define-key dired-mode-map [menu-bar image-dired]
1525 (cons "Image-Dired" (make-sparse-keymap "Image-Dired")))
1527 (define-key dired-mode-map [menu-bar image-dired image-dired-copy-with-exif-file-name]
1528 '("Copy with EXIF file name" . image-dired-copy-with-exif-file-name))
1530 (define-key dired-mode-map [menu-bar image-dired image-dired-dired-comment-files]
1531 '("Comment files" . image-dired-dired-comment-files))
1533 (define-key dired-mode-map [menu-bar image-dired image-dired-mark-tagged-files]
1534 '("Mark tagged files" . image-dired-mark-tagged-files))
1536 (define-key dired-mode-map [menu-bar image-dired image-dired-delete-tag]
1537 '("Remove tag from files" . image-dired-delete-tag))
1539 (define-key dired-mode-map [menu-bar image-dired image-dired-tag-files]
1540 '("Tag files" . image-dired-tag-files))
1542 (define-key dired-mode-map [menu-bar image-dired image-dired-jump-thumbnail-buffer]
1543 '("Jump to thumbnail buffer" . image-dired-jump-thumbnail-buffer))
1545 (define-key dired-mode-map [menu-bar image-dired image-dired-toggle-movement-tracking]
1546 '("Toggle movement tracking" . image-dired-toggle-movement-tracking))
1548 (define-key dired-mode-map
1549 [menu-bar image-dired image-dired-toggle-append-browsing]
1550 '("Toggle append browsing" . image-dired-toggle-append-browsing))
1552 (define-key dired-mode-map
1553 [menu-bar image-dired image-dired-toggle-disp-props]
1554 '("Toggle display properties" . image-dired-toggle-dired-display-properties))
1556 (define-key dired-mode-map
1557 [menu-bar image-dired image-dired-dired-display-external]
1558 '("Display in external viewer" . image-dired-dired-display-external))
1559 (define-key dired-mode-map
1560 [menu-bar image-dired image-dired-dired-display-image]
1561 '("Display image" . image-dired-dired-display-image))
1562 (define-key dired-mode-map
1563 [menu-bar image-dired image-dired-display-thumb]
1564 '("Display this thumbnail" . image-dired-display-thumb))
1565 (define-key dired-mode-map
1566 [menu-bar image-dired image-dired-display-thumbs-append]
1567 '("Display thumbnails append" . image-dired-display-thumbs-append))
1568 (define-key dired-mode-map
1569 [menu-bar image-dired image-dired-display-thumbs]
1570 '("Display thumbnails" . image-dired-display-thumbs))
1572 (define-key dired-mode-map
1573 [menu-bar image-dired image-dired-create-thumbs]
1574 '("Create thumbnails for marked files" . image-dired-create-thumbs))
1576 (define-key dired-mode-map
1577 [menu-bar image-dired image-dired-mark-and-display-next]
1578 '("Mark and display next" . image-dired-mark-and-display-next))
1579 (define-key dired-mode-map
1580 [menu-bar image-dired image-dired-previous-line-and-display]
1581 '("Display thumb for previous file" . image-dired-previous-line-and-display))
1582 (define-key dired-mode-map
1583 [menu-bar image-dired image-dired-next-line-and-display]
1584 '("Display thumb for next file" . image-dired-next-line-and-display)))
1586 (defun image-dired-create-thumbs (&optional arg)
1587 "Create thumbnail images for all marked files in dired.
1588 With prefix argument ARG, create thumbnails even if they already exist
1589 \(i.e. use this to refresh your thumbnails)."
1590 (interactive "P")
1591 (let (curr-file thumb-name files count)
1592 (setq files (dired-get-marked-files))
1593 (mapcar
1594 (lambda (curr-file)
1595 (setq thumb-name (image-dired-thumb-name curr-file))
1596 ;; If the user overrides the exist check, we must clear the
1597 ;; image cache so that if the user wants to display the
1598 ;; thumnail, it is not fetched from cache.
1599 (if arg
1600 (clear-image-cache))
1601 (if (or (not (file-exists-p thumb-name))
1602 arg)
1603 (if (not (= 0 (image-dired-create-thumb curr-file
1604 (image-dired-thumb-name curr-file))))
1605 (error "Thumb could not be created"))))
1606 files)))
1608 (defvar image-dired-slideshow-timer nil
1609 "Slideshow timer.")
1611 (defvar image-dired-slideshow-count 0
1612 "Keeping track on number of images in slideshow.")
1614 (defvar image-dired-slideshow-times 0
1615 "Number of pictures to display in slideshow.")
1617 (defun image-dired-slideshow-step ()
1618 "Step to next file, if `image-dired-slideshow-times' has not been reached."
1619 (if (< image-dired-slideshow-count image-dired-slideshow-times)
1620 (progn
1621 (message "%s" (1+ image-dired-slideshow-count))
1622 (setq image-dired-slideshow-count (1+ image-dired-slideshow-count))
1623 (image-dired-next-line-and-display))
1624 (image-dired-slideshow-stop)))
1626 (defun image-dired-slideshow-start ()
1627 "Start slideshow.
1628 Ask user for number of images to show and the delay in between."
1629 (interactive)
1630 (setq image-dired-slideshow-count 0)
1631 (setq image-dired-slideshow-times (string-to-number (read-string "How many: ")))
1632 (let ((repeat (string-to-number
1633 (read-string
1634 "Delay, in seconds. Decimals are accepted : " "1"))))
1635 (setq image-dired-slideshow-timer
1636 (run-with-timer
1637 0 repeat
1638 'image-dired-slideshow-step))))
1640 (defun image-dired-slideshow-stop ()
1641 "Cancel slideshow."
1642 (interactive)
1643 (cancel-timer image-dired-slideshow-timer))
1645 (defun image-dired-delete-char ()
1646 "Remove current thumbnail from thumbnail buffer and line up."
1647 (interactive)
1648 (let ((inhibit-read-only t))
1649 (delete-char 1)
1650 (if (looking-at " ")
1651 (delete-char 1))))
1653 ;;;###autoload
1654 (defun image-dired-display-thumbs-append ()
1655 "Append thumbnails to `image-dired-thumbnail-buffer'."
1656 (interactive)
1657 (image-dired-display-thumbs nil t t))
1659 ;;;###autoload
1660 (defun image-dired-display-thumb ()
1661 "Shorthand for `image-dired-display-thumbs' with prefix argument."
1662 (interactive)
1663 (image-dired-display-thumbs t nil t))
1665 (defun image-dired-line-up ()
1666 "Line up thumbnails according to `image-dired-thumbs-per-row'.
1667 See also `image-dired-line-up-dynamic'."
1668 (interactive)
1669 (let ((inhibit-read-only t))
1670 (goto-char (point-min))
1671 (while (and (not (image-dired-image-at-point-p))
1672 (not (eobp)))
1673 (delete-char 1))
1674 (while (not (eobp))
1675 (forward-char)
1676 (while (and (not (image-dired-image-at-point-p))
1677 (not (eobp)))
1678 (delete-char 1)))
1679 (goto-char (point-min))
1680 (let ((count 0))
1681 (while (not (eobp))
1682 (forward-char)
1683 (if (= image-dired-thumbs-per-row 1)
1684 (insert "\n")
1685 (insert " ")
1686 (setq count (1+ count))
1687 (when (and (= count (- image-dired-thumbs-per-row 1))
1688 (not (eobp)))
1689 (forward-char)
1690 (insert "\n")
1691 (setq count 0)))))
1692 (goto-char (point-min))))
1694 (defun image-dired-line-up-dynamic ()
1695 "Line up thumbnails images dynamically.
1696 Calculate how many thumbnails fit."
1697 (interactive)
1698 (let* ((char-width (frame-char-width))
1699 (width (image-dired-window-width-pixels (image-dired-thumbnail-window)))
1700 (image-dired-thumbs-per-row
1701 (/ width
1702 (+ (* 2 image-dired-thumb-relief)
1703 (* 2 image-dired-thumb-margin)
1704 image-dired-thumb-width char-width))))
1705 (image-dired-line-up)))
1707 (defun image-dired-line-up-interactive ()
1708 "Line up thumbnails interactively.
1709 Ask user how many thumbnails should be displayed per row."
1710 (interactive)
1711 (let ((image-dired-thumbs-per-row
1712 (string-to-number (read-string "How many thumbs per row: "))))
1713 (if (not (> image-dired-thumbs-per-row 0))
1714 (message "Number must be greater than 0")
1715 (image-dired-line-up))))
1717 (defun image-dired-thumbnail-display-external ()
1718 "Display original image for thumbnail at point using external viewer."
1719 (interactive)
1720 (let ((file (image-dired-original-file-name)))
1721 (if (not (image-dired-image-at-point-p))
1722 (message "No thumbnail at point")
1723 (if (not file)
1724 (message "No original file name found")
1725 (call-process shell-file-name nil nil nil shell-command-switch
1726 (format "%s \"%s\"" image-dired-external-viewer file))))))
1728 ;;;###autoload
1729 (defun image-dired-dired-display-external ()
1730 "Display file at point using an external viewer."
1731 (interactive)
1732 (let ((file (dired-get-filename)))
1733 (call-process shell-file-name nil nil nil shell-command-switch
1734 (format "%s \"%s\"" image-dired-external-viewer file))))
1736 (defun image-dired-window-width-pixels (window)
1737 "Calculate WINDOW width in pixels."
1738 (* (window-width window) (frame-char-width)))
1740 (defun image-dired-window-height-pixels (window)
1741 "Calculate WINDOW height in pixels."
1742 ;; Note: The mode-line consumes one line
1743 (* (- (window-height window) 1) (frame-char-height)))
1745 (defun image-dired-display-window ()
1746 "Return window where `image-dired-display-image-buffer' is visible."
1747 (get-window-with-predicate
1748 (lambda (window)
1749 (equal (buffer-name (window-buffer window)) image-dired-display-image-buffer))
1750 nil t))
1752 (defun image-dired-thumbnail-window ()
1753 "Return window where `image-dired-thumbnail-buffer' is visible."
1754 (get-window-with-predicate
1755 (lambda (window)
1756 (equal (buffer-name (window-buffer window)) image-dired-thumbnail-buffer))
1757 nil t))
1759 (defun image-dired-associated-dired-buffer-window ()
1760 "Return window where associated dired buffer is visible."
1761 (let (buf)
1762 (if (image-dired-image-at-point-p)
1763 (progn
1764 (setq buf (image-dired-associated-dired-buffer))
1765 (get-window-with-predicate
1766 (lambda (window)
1767 (equal (window-buffer window) buf))))
1768 (error "No thumbnail image at point"))))
1770 (defun image-dired-display-window-width ()
1771 "Return width, in pixels, of image-dired's image display window."
1772 (- (image-dired-window-width-pixels (image-dired-display-window))
1773 image-dired-display-window-width-correction))
1775 (defun image-dired-display-window-height ()
1776 "Return height, in pixels, of image-dired's image display window."
1777 (- (image-dired-window-height-pixels (image-dired-display-window))
1778 image-dired-display-window-height-correction))
1780 (defun image-dired-display-image (file &optional original-size)
1781 "Display image FILE in image buffer.
1782 Use this when you want to display the image, semi sized, in a new
1783 window. The image is sized to fit the display window (using a
1784 temporary file, don't worry). Because of this, it will not be as
1785 quick as opening it directly, but on most modern systems it
1786 should feel snappy enough.
1788 If optional argument ORIGINAL-SIZE is non-nil, display image in its
1789 original size."
1790 (let ((new-file (expand-file-name image-dired-temp-image-file))
1791 width height command ret
1792 (image-type 'jpeg))
1793 (setq file (expand-file-name file))
1794 (if (not original-size)
1795 (progn
1796 (setq width (image-dired-display-window-width))
1797 (setq height (image-dired-display-window-height))
1798 (setq command
1799 (format-spec
1800 image-dired-cmd-create-temp-image-options
1801 (list
1802 (cons ?p image-dired-cmd-create-temp-image-program)
1803 (cons ?w width)
1804 (cons ?h height)
1805 (cons ?f file)
1806 (cons ?t new-file))))
1807 (setq ret (call-process shell-file-name nil nil nil
1808 shell-command-switch command))
1809 (if (not (= 0 ret))
1810 (error "Could not resize image")))
1811 (setq image-type (image-type-from-file-name file))
1812 (copy-file file new-file t))
1813 (with-current-buffer (image-dired-create-display-image-buffer)
1814 (let ((inhibit-read-only t))
1815 (erase-buffer)
1816 (clear-image-cache)
1817 (image-dired-insert-image image-dired-temp-image-file image-type 0 0)
1818 (goto-char (point-min))
1819 (image-dired-update-property 'original-file-name file)))))
1821 (defun image-dired-display-thumbnail-original-image (&optional arg)
1822 "Display current thumbnail's original image in display buffer.
1823 See documentation for `image-dired-display-image' for more information.
1824 With prefix argument ARG, display image in its original size."
1825 (interactive "P")
1826 (let ((file (image-dired-original-file-name)))
1827 (if (not (string-equal major-mode "image-dired-thumbnail-mode"))
1828 (message "Not in image-dired-thumbnail-mode")
1829 (if (not (image-dired-image-at-point-p))
1830 (message "No thumbnail at point")
1831 (if (not file)
1832 (message "No original file name found")
1833 (image-dired-create-display-image-buffer)
1834 (display-buffer image-dired-display-image-buffer)
1835 (image-dired-display-image file arg))))))
1838 ;;;###autoload
1839 (defun image-dired-dired-display-image (&optional arg)
1840 "Display current image file.
1841 See documentation for `image-dired-display-image' for more information.
1842 With prefix argument ARG, display image in its original size."
1843 (interactive "P")
1844 (image-dired-create-display-image-buffer)
1845 (display-buffer image-dired-display-image-buffer)
1846 (image-dired-display-image (dired-get-filename) arg))
1848 (defun image-dired-image-at-point-p ()
1849 "Return true if there is an image-dired thumbnail at point."
1850 (get-text-property (point) 'image-dired-thumbnail))
1852 (defun image-dired-rotate-thumbnail (degrees)
1853 "Rotate thumbnail DEGREES degrees."
1854 (if (not (image-dired-image-at-point-p))
1855 (message "No thumbnail at point")
1856 (let ((file (image-dired-thumb-name (image-dired-original-file-name)))
1857 command)
1858 (setq command (format-spec
1859 image-dired-cmd-rotate-thumbnail-options
1860 (list
1861 (cons ?p image-dired-cmd-rotate-thumbnail-program)
1862 (cons ?d degrees)
1863 (cons ?t (expand-file-name file)))))
1864 (call-process shell-file-name nil nil nil shell-command-switch command)
1865 ;; Clear the cache to refresh image. I wish I could just refresh
1866 ;; the current file but I do not know how to do that. Yet...
1867 (clear-image-cache))))
1869 (defun image-dired-rotate-thumbnail-left ()
1870 "Rotate thumbnail left (counter clockwise) 90 degrees.
1871 The result of the rotation is displayed in the image display area
1872 and a confirmation is needed before the original image files is
1873 overwritten. This confirmation can be turned off using
1874 `image-dired-rotate-original-ask-before-overwrite'."
1875 (interactive)
1876 (image-dired-rotate-thumbnail "270"))
1878 (defun image-dired-rotate-thumbnail-right ()
1879 "Rotate thumbnail counter right (clockwise) 90 degrees.
1880 The result of the rotation is displayed in the image display area
1881 and a confirmation is needed before the original image files is
1882 overwritten. This confirmation can be turned off using
1883 `image-dired-rotate-original-ask-before-overwrite'."
1884 (interactive)
1885 (image-dired-rotate-thumbnail "90"))
1887 (defun image-dired-refresh-thumb ()
1888 "Force creation of new image for current thumbnail."
1889 (interactive)
1890 (let ((file (image-dired-original-file-name)))
1891 (clear-image-cache)
1892 (image-dired-create-thumb file (image-dired-thumb-name file))))
1894 (defun image-dired-rotate-original (degrees)
1895 "Rotate original image DEGREES degrees."
1896 (if (not (image-dired-image-at-point-p))
1897 (message "No image at point")
1898 (let ((file (image-dired-original-file-name))
1899 command temp-file)
1900 (if (not (string-match "\.[jJ][pP[eE]?[gG]$" file))
1901 (error "Only JPEG images can be rotated!"))
1902 (setq command (format-spec
1903 image-dired-cmd-rotate-original-options
1904 (list
1905 (cons ?p image-dired-cmd-rotate-original-program)
1906 (cons ?d degrees)
1907 (cons ?o (expand-file-name file))
1908 (cons ?t image-dired-temp-rotate-image-file))))
1909 (if (not (= 0 (call-process shell-file-name nil nil nil
1910 shell-command-switch command)))
1911 (error "Could not rotate image")
1912 (image-dired-display-image image-dired-temp-rotate-image-file)
1913 (if (or (and image-dired-rotate-original-ask-before-overwrite
1914 (y-or-n-p
1915 "Rotate to temp file OK. Overwrite original image? "))
1916 (not image-dired-rotate-original-ask-before-overwrite))
1917 (progn
1918 (copy-file image-dired-temp-rotate-image-file file t)
1919 (image-dired-refresh-thumb))
1920 (image-dired-display-image file))))))
1922 (defun image-dired-rotate-original-left ()
1923 "Rotate original image left (counter clockwise) 90 degrees."
1924 (interactive)
1925 (image-dired-rotate-original "270"))
1927 (defun image-dired-rotate-original-right ()
1928 "Rotate original image right (clockwise) 90 degrees."
1929 (interactive)
1930 (image-dired-rotate-original "90"))
1932 (defun image-dired-get-exif-file-name (file)
1933 "Use the image's EXIF information to return a unique file name.
1934 The file name should be unique as long as you do not take more than
1935 one picture per second. The original file name is suffixed at the end
1936 for traceability. The format of the returned file name is
1937 YYYY_MM_DD_HH_MM_DD_ORIG_FILE_NAME.jpg. Used from
1938 `image-dired-copy-with-exif-file-name'."
1939 (let (data no-exif-data-found)
1940 (if (not (string-match "\.[Jj][Pp][Ee]?[Gg]$" (expand-file-name file)))
1941 (progn
1942 (setq no-exif-data-found t)
1943 (setq data
1944 (format-time-string
1945 "%Y:%m:%d %H:%M:%S"
1946 (nth 5 (file-attributes (expand-file-name file))))))
1947 (setq data (image-dired-get-exif-data (expand-file-name file)
1948 "DateTimeOriginal")))
1949 (while (string-match "[ :]" data)
1950 (setq data (replace-match "_" nil nil data)))
1951 (format "%s%s%s" data
1952 (if no-exif-data-found
1953 "_noexif_"
1954 "_")
1955 (file-name-nondirectory file))))
1957 (defun image-dired-thumbnail-set-image-description ()
1958 "Set the ImageDescription EXIF tag for the original image.
1959 If the image already has a value for this tag, it is used as the
1960 default value at the prompt."
1961 (interactive)
1962 (if (not (image-dired-image-at-point-p))
1963 (message "No thumbnail at point")
1964 (let* ((file (image-dired-original-file-name))
1965 (old-value (image-dired-get-exif-data file "ImageDescription")))
1966 (if (eq 0
1967 (image-dired-set-exif-data file "ImageDescription"
1968 (read-string "Value of ImageDescription: "
1969 old-value)))
1970 (message "Successfully wrote ImageDescription tag.")
1971 (error "Could not write ImageDescription tag")))))
1973 (defun image-dired-set-exif-data (file tag-name tag-value)
1974 "In FILE, set EXIF tag TAG-NAME to value TAG-VALUE."
1975 (let (command)
1976 (setq command (format-spec
1977 image-dired-cmd-write-exif-data-options
1978 (list
1979 (cons ?p image-dired-cmd-write-exif-data-program)
1980 (cons ?f (expand-file-name file))
1981 (cons ?t tag-name)
1982 (cons ?v tag-value))))
1983 (call-process shell-file-name nil nil nil shell-command-switch command)))
1985 (defun image-dired-get-exif-data (file tag-name)
1986 "From FILE, return EXIF tag TAG-NAME."
1987 (let ((buf (get-buffer-create "*image-dired-get-exif-data*"))
1988 command tag-value)
1989 (setq command (format-spec
1990 image-dired-cmd-read-exif-data-options
1991 (list
1992 (cons ?p image-dired-cmd-read-exif-data-program)
1993 (cons ?f file)
1994 (cons ?t tag-name))))
1995 (with-current-buffer buf
1996 (delete-region (point-min) (point-max))
1997 (if (not (eq (call-process shell-file-name nil t nil
1998 shell-command-switch command) 0))
1999 (error "Could not get EXIF tag")
2000 (goto-char (point-min))
2001 ;; Clean buffer from newlines and carriage returns before
2002 ;; getting final info
2003 (while (search-forward-regexp "[\n\r]" nil t)
2004 (replace-match "" nil t))
2005 (setq tag-value (buffer-substring (point-min) (point-max)))))
2006 tag-value))
2008 (defun image-dired-copy-with-exif-file-name ()
2009 "Copy file with unique name to main image directory.
2010 Copy current or all marked files in dired to a new file in your
2011 main image directory, using a file name generated by
2012 `image-dired-get-exif-file-name'. A typical usage for this if when
2013 copying images from a digital camera into the image directory.
2015 Typically, you would open up the folder with the incoming
2016 digital images, mark the files to be copied, and execute this
2017 function. The result is a couple of new files in
2018 `image-dired-main-image-directory' called
2019 2005_05_08_12_52_00_dscn0319.jpg,
2020 2005_05_08_14_27_45_dscn0320.jpg etc."
2021 (interactive)
2022 (let (new-name
2023 (files (dired-get-marked-files)))
2024 (mapcar
2025 (lambda (curr-file)
2026 (setq new-name
2027 (format "%s/%s"
2028 (file-name-as-directory
2029 (expand-file-name image-dired-main-image-directory))
2030 (image-dired-get-exif-file-name curr-file)))
2031 (message "Copying %s to %s" curr-file new-name)
2032 (copy-file curr-file new-name))
2033 files)))
2035 (defun image-dired-display-next-thumbnail-original ()
2036 "In thubnail buffer, move to next thumbnail and display the image."
2037 (interactive)
2038 (image-dired-forward-image)
2039 (image-dired-display-thumbnail-original-image))
2041 (defun image-dired-display-previous-thumbnail-original ()
2042 "Move to previous thumbnail and display image."
2043 (interactive)
2044 (image-dired-backward-image)
2045 (image-dired-display-thumbnail-original-image))
2047 (defun image-dired-write-comments (file-comments)
2048 "Write file comments to database.
2049 Write file comments to one or more files. FILE-COMMENTS is an alist on
2050 the following form:
2051 ((FILE . COMMENT) ... )"
2052 (image-dired-sane-db-file)
2053 (let (end comment-beg-pos comment-end-pos file comment)
2054 (with-temp-file image-dired-db-file
2055 (insert-file-contents image-dired-db-file)
2056 (dolist (elt file-comments)
2057 (setq file (car elt)
2058 comment (cdr elt))
2059 (goto-char (point-min))
2060 (if (search-forward-regexp (format "^%s.*$" file) nil t)
2061 (progn
2062 (setq end (point))
2063 (beginning-of-line)
2064 ;; Delete old comment, if any
2065 (when (search-forward ";comment:" end t)
2066 (setq comment-beg-pos (match-beginning 0))
2067 ;; Any tags after the comment?
2068 (if (search-forward ";" end t)
2069 (setq comment-end-pos (- (point) 1))
2070 (setq comment-end-pos end))
2071 ;; Delete comment tag and comment
2072 (delete-region comment-beg-pos comment-end-pos))
2073 ;; Insert new comment
2074 (beginning-of-line)
2075 (unless (search-forward ";" end t)
2076 (end-of-line)
2077 (insert ";"))
2078 (insert (format "comment:%s;" comment)))
2079 ;; File does not exist in database - add it.
2080 (goto-char (point-max))
2081 (insert (format "\n%s;comment:%s" file comment)))))))
2083 (defun image-dired-update-property (prop value)
2084 "Update text property PROP with value VALUE at point."
2085 (let ((inhibit-read-only t))
2086 (put-text-property
2087 (point) (1+ (point))
2088 prop
2089 value)))
2091 ;;;###autoload
2092 (defun image-dired-dired-comment-files ()
2093 "Add comment to current or marked files in dired."
2094 (interactive)
2095 (let ((comment (image-dired-read-comment)))
2096 (image-dired-write-comments
2097 (mapcar
2098 (lambda (curr-file)
2099 (cons curr-file comment))
2100 (dired-get-marked-files)))))
2102 (defun image-dired-comment-thumbnail ()
2103 "Add comment to current thumbnail in thumbnail buffer."
2104 (interactive)
2105 (let* ((file (image-dired-original-file-name))
2106 (comment (image-dired-read-comment file)))
2107 (image-dired-write-comments (list (cons file comment)))
2108 (image-dired-update-property 'comment comment))
2109 (image-dired-display-thumb-properties))
2111 (defun image-dired-read-comment (&optional file)
2112 "Read comment for an image.
2113 Read comment for an image, optionally using old comment from FILE
2114 as initial value."
2115 (let ((comment
2116 (read-string
2117 "Comment: "
2118 (if file (image-dired-get-comment file)))))
2119 comment))
2121 (defun image-dired-get-comment (file)
2122 "Get comment for file FILE."
2123 (image-dired-sane-db-file)
2124 (save-excursion
2125 (let (end buf comment-beg-pos comment-end-pos comment)
2126 (setq buf (find-file image-dired-db-file))
2127 (goto-char (point-min))
2128 (when (search-forward-regexp
2129 (format "^%s" file) nil t)
2130 (end-of-line)
2131 (setq end (point))
2132 (beginning-of-line)
2133 (cond ((search-forward ";comment:" end t)
2134 (setq comment-beg-pos (point))
2135 (if (search-forward ";" end t)
2136 (setq comment-end-pos (- (point) 1))
2137 (setq comment-end-pos end))
2138 (setq comment (buffer-substring
2139 comment-beg-pos comment-end-pos)))))
2140 (kill-buffer buf)
2141 comment)))
2143 ;;;###autoload
2144 (defun image-dired-mark-tagged-files ()
2145 "Use regexp to mark files with matching tag.
2146 A `tag' is a keyword, a piece of meta data, associated with an
2147 image file and stored in image-dired's database file. This command
2148 lets you input a regexp and this will be matched against all tags
2149 on all image files in the database file. The files that have a
2150 matching tags will be marked in the dired buffer."
2151 (interactive)
2152 (image-dired-sane-db-file)
2153 (let ((tag (read-string "Mark tagged files (regexp): "))
2154 (hits 0)
2155 files buf)
2156 (save-excursion
2157 (setq buf (find-file image-dired-db-file))
2158 (goto-char (point-min))
2159 ;; Collect matches
2160 (while (search-forward-regexp
2161 (concat "\\(^[^;\n]+\\);.*" tag ".*$") nil t)
2162 (setq files (append (list (match-string 1)) files)))
2163 (kill-buffer buf)
2164 ;; Mark files
2165 (mapc
2166 ;; I tried using `dired-mark-files-regexp' but it was
2167 ;; waaaay to slow.
2168 (lambda (curr-file)
2169 ;; Don't bother about hits found in other directories than
2170 ;; the current one.
2171 (when (string= (file-name-as-directory
2172 (expand-file-name default-directory))
2173 (file-name-as-directory
2174 (file-name-directory curr-file)))
2175 (setq curr-file (file-name-nondirectory curr-file))
2176 (goto-char (point-min))
2177 (when (search-forward-regexp (format "\\s %s$" curr-file) nil t)
2178 (setq hits (+ hits 1))
2179 (dired-mark 1))))
2180 files))
2181 (message "%d files with matching tag marked." hits)))
2183 (defun image-dired-mouse-display-image (event)
2184 "Use mouse EVENT, call `image-dired-display-image' to display image.
2185 Track this in associated dired buffer if `image-dired-track-movement' is
2186 non-nil."
2187 (interactive "e")
2188 (let (file)
2189 (mouse-set-point event)
2190 (goto-char (posn-point (event-end event)))
2191 (setq file (image-dired-original-file-name))
2192 (if image-dired-track-movement
2193 (image-dired-track-original-file))
2194 (image-dired-create-display-image-buffer)
2195 (display-buffer image-dired-display-image-buffer)
2196 (image-dired-display-image file)))
2198 (defun image-dired-mouse-select-thumbnail (event)
2199 "Use mouse EVENT to select thumbnail image.
2200 Track this in associated dired buffer if `image-dired-track-movement' is
2201 non-nil."
2202 (interactive "e")
2203 (let (file)
2204 (mouse-set-point event)
2205 (goto-char (posn-point (event-end event)))
2206 (if image-dired-track-movement
2207 (image-dired-track-original-file)))
2208 (image-dired-display-thumb-properties))
2210 (defun image-dired-mouse-toggle-mark (event)
2211 "Use mouse EVENT to toggle dired mark for thumbnail.
2212 Track this in associated dired buffer if `image-dired-track-movement' is
2213 non-nil."
2214 (interactive "e")
2215 (let (file)
2216 (mouse-set-point event)
2217 (goto-char (posn-point (event-end event)))
2218 (if image-dired-track-movement
2219 (image-dired-track-original-file)))
2220 (image-dired-toggle-mark-thumb-original-file))
2222 (defun image-dired-dired-display-properties ()
2223 "Display properties for dired file in the echo area."
2224 (interactive)
2225 (let* ((file (dired-get-filename))
2226 (file-name (file-name-nondirectory file))
2227 (dired-buf (buffer-name (current-buffer)))
2228 (props (mapconcat
2229 'princ
2230 (image-dired-list-tags file)
2231 ", "))
2232 (comment (image-dired-get-comment file)))
2233 (if file-name
2234 (message "%s"
2235 (image-dired-format-properties-string
2236 dired-buf
2237 file-name
2238 props
2239 comment)))))
2241 (defvar image-dired-tag-file-list nil
2242 "List to store tag-file structure.")
2244 (defvar image-dired-file-tag-list nil
2245 "List to store file-tag structure.")
2247 (defvar image-dired-file-comment-list nil
2248 "List to store file comments.")
2250 (defun image-dired-add-to-tag-file-list (tag file)
2251 "Add relation between TAG and FILE."
2252 (let (curr)
2253 (if image-dired-tag-file-list
2254 (if (setq curr (assoc tag image-dired-tag-file-list))
2255 (if (not (member file curr))
2256 (setcdr curr (cons file (cdr curr))))
2257 (setcdr image-dired-tag-file-list
2258 (cons (list tag file) (cdr image-dired-tag-file-list))))
2259 (setq image-dired-tag-file-list (list (list tag file))))))
2261 (defun image-dired-add-to-tag-file-lists (tag file)
2262 "Helper function used from `image-dired-create-gallery-lists'.
2264 Add TAG to FILE in one list and FILE to TAG in the other.
2266 Lisp structures look like the following:
2268 image-dired-file-tag-list:
2270 ((\"filename1\" \"tag1\" \"tag2\" \"tag3\" ...)
2271 (\"filename2\" \"tag1\" \"tag2\" \"tag3\" ...)
2272 ...)
2274 image-dired-tag-file-list:
2276 ((\"tag1\" \"filename1\" \"filename2\" \"filename3\" ...)
2277 (\"tag2\" \"filename1\" \"filename2\" \"filename3\" ...)
2278 ...)"
2279 ;; Add tag to file list
2280 (let (curr)
2281 (if image-dired-file-tag-list
2282 (if (setq curr (assoc file image-dired-file-tag-list))
2283 (setcdr curr (cons tag (cdr curr)))
2284 (setcdr image-dired-file-tag-list
2285 (cons (list file tag) (cdr image-dired-file-tag-list))))
2286 (setq image-dired-file-tag-list (list (list file tag))))
2287 ;; Add file to tag list
2288 (if image-dired-tag-file-list
2289 (if (setq curr (assoc tag image-dired-tag-file-list))
2290 (if (not (member file curr))
2291 (setcdr curr (cons file (cdr curr))))
2292 (setcdr image-dired-tag-file-list
2293 (cons (list tag file) (cdr image-dired-tag-file-list))))
2294 (setq image-dired-tag-file-list (list (list tag file))))))
2296 (defun image-dired-add-to-file-comment-list (file comment)
2297 "Helper function used from `image-dired-create-gallery-lists'.
2299 For FILE, add COMMENT to list.
2301 Lisp structure looks like the following:
2303 image-dired-file-comment-list:
2305 ((\"filename1\" . \"comment1\")
2306 (\"filename2\" . \"comment2\")
2307 ...)"
2308 (if image-dired-file-comment-list
2309 (if (not (assoc file image-dired-file-comment-list))
2310 (setcdr image-dired-file-comment-list
2311 (cons (cons file comment)
2312 (cdr image-dired-file-comment-list))))
2313 (setq image-dired-file-comment-list (list (cons file comment)))))
2315 (defun image-dired-create-gallery-lists ()
2316 "Create temporary lists used by `image-dired-gallery-generate'."
2317 (image-dired-sane-db-file)
2318 (let ((buf (find-file image-dired-db-file))
2319 end beg file row-tags)
2320 (setq image-dired-tag-file-list nil)
2321 (setq image-dired-file-tag-list nil)
2322 (setq image-dired-file-comment-list nil)
2323 (goto-char (point-min))
2324 (while (search-forward-regexp "^." nil t)
2325 (end-of-line)
2326 (setq end (point))
2327 (beginning-of-line)
2328 (setq beg (point))
2329 (if (not (search-forward ";" end nil))
2330 (error "Something is really wrong, check format of database"))
2331 (setq row-tags (split-string
2332 (buffer-substring beg end) ";"))
2333 (setq file (car row-tags))
2334 (mapc
2335 (lambda (x)
2336 (if (not (string-match "^comment:\\(.*\\)" x))
2337 (image-dired-add-to-tag-file-lists x file)
2338 (image-dired-add-to-file-comment-list file (match-string 1 x))))
2339 (cdr row-tags)))
2340 (kill-buffer buf))
2341 ;; Sort tag-file list
2342 (setq image-dired-tag-file-list
2343 (sort image-dired-tag-file-list
2344 (lambda (x y)
2345 (string< (car x) (car y))))))
2347 (defun image-dired-hidden-p (file)
2348 "Return t if image FILE has a \"hidden\" tag."
2349 (let (hidden)
2350 (mapc
2351 (lambda (tag)
2352 (if (member tag image-dired-gallery-hidden-tags)
2353 (setq hidden t)))
2354 (cdr (assoc file image-dired-file-tag-list)))
2355 hidden))
2357 (defun image-dired-gallery-generate ()
2358 "Generate gallery pages.
2359 First we create a couple of Lisp structures from the database to make
2360 it easier to generate, then HTML-files are created in
2361 `image-dired-gallery-dir'"
2362 (interactive)
2363 (if (eq 'per-directory image-dired-thumbnail-storage)
2364 (error "Currently, gallery generation is not supported \
2365 when using per-directory thumbnail file storage"))
2366 (image-dired-create-gallery-lists)
2367 (let ((tags image-dired-tag-file-list)
2368 count curr tag index-buf tag-buf
2369 comment file-tags tag-link tag-link-list)
2370 ;; Make sure gallery root exist
2371 (if (file-exists-p image-dired-gallery-dir)
2372 (if (not (file-directory-p image-dired-gallery-dir))
2373 (error "Variable image-dired-gallery-dir is not a directory"))
2374 (make-directory image-dired-gallery-dir))
2375 ;; Open index file
2376 (setq index-buf (find-file
2377 (format "%s/index.html" image-dired-gallery-dir)))
2378 (erase-buffer)
2379 (insert "<html>\n")
2380 (insert " <body>\n")
2381 (insert " <h2>Image-Dired Gallery</h2>\n")
2382 (insert (format "<p>\n Gallery generated %s\n <p>\n"
2383 (current-time-string)))
2384 (insert " <h3>Tag index</h3>\n")
2385 (setq count 1)
2386 ;; Pre-generate list of all tag links
2387 (mapc
2388 (lambda (curr)
2389 (setq tag (car curr))
2390 (when (not (member tag image-dired-gallery-hidden-tags))
2391 (setq tag-link (format "<a href=\"%d.html\">%s</a>" count tag))
2392 (if tag-link-list
2393 (setq tag-link-list
2394 (append tag-link-list (list (cons tag tag-link))))
2395 (setq tag-link-list (list (cons tag tag-link))))
2396 (setq count (1+ count))))
2397 tags)
2398 (setq count 1)
2399 ;; Main loop where we generated thumbnail pages per tag
2400 (mapc
2401 (lambda (curr)
2402 (setq tag (car curr))
2403 ;; Don't display hidden tags
2404 (when (not (member tag image-dired-gallery-hidden-tags))
2405 ;; Insert link to tag page in index
2406 (insert (format " %s<br>\n" (cdr (assoc tag tag-link-list))))
2407 ;; Open per-tag file
2408 (setq tag-buf (find-file
2409 (format "%s/%s.html" image-dired-gallery-dir count)))
2410 (erase-buffer)
2411 (insert "<html>\n")
2412 (insert " <body>\n")
2413 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2414 (insert (format " <h2>Images with tag &quot;%s&quot;</h2>" tag))
2415 ;; Main loop for files per tag page
2416 (mapc
2417 (lambda (file)
2418 (when (not (image-dired-hidden-p file))
2419 ;; Insert thumbnail with link to full image
2420 (insert
2421 (format "<a href=\"%s/%s\"><img src=\"%s/%s\"%s></a>\n"
2422 image-dired-gallery-image-root-url
2423 (file-name-nondirectory file)
2424 image-dired-gallery-thumb-image-root-url
2425 (file-name-nondirectory (image-dired-thumb-name file)) file))
2426 ;; Insert comment, if any
2427 (if (setq comment (cdr (assoc file image-dired-file-comment-list)))
2428 (insert (format "<br>\n%s<br>\n" comment))
2429 (insert "<br>\n"))
2430 ;; Insert links to other tags, if any
2431 (when (> (length
2432 (setq file-tags (assoc file image-dired-file-tag-list))) 2)
2433 (insert "[ ")
2434 (mapc
2435 (lambda (extra-tag)
2436 ;; Only insert if not file name or the main tag
2437 (if (and (not (equal extra-tag tag))
2438 (not (equal extra-tag file)))
2439 (insert
2440 (format "%s " (cdr (assoc extra-tag tag-link-list))))))
2441 file-tags)
2442 (insert "]<br>\n"))))
2443 (cdr curr))
2444 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2445 (insert " </body>\n")
2446 (insert "</html>\n")
2447 (save-buffer)
2448 (kill-buffer tag-buf)
2449 (setq count (1+ count))))
2450 tags)
2451 (insert " </body>\n")
2452 (insert "</html>")
2453 (save-buffer)
2454 (kill-buffer index-buf)))
2456 (defun image-dired-kill-buffer-and-window ()
2457 "Kill the current buffer and, if possible, also the window."
2458 (interactive)
2459 (let ((buffer (current-buffer)))
2460 (condition-case nil
2461 (delete-window (selected-window))
2462 (error nil))
2463 (kill-buffer buffer)))
2465 (defvar image-dired-widget-list nil
2466 "List to keep track of meta data in edit buffer.")
2468 ;;;###autoload
2469 (defun image-dired-dired-edit-comment-and-tags ()
2470 "Edit comment and tags of current or marked image files.
2471 Edit comment and tags for all marked image files in an
2472 easy-to-use form."
2473 (interactive)
2474 (setq image-dired-widget-list nil)
2475 ;; Setup buffer.
2476 (let ((files (dired-get-marked-files)))
2477 (switch-to-buffer "*Image-Dired Edit Meta Data*")
2478 (kill-all-local-variables)
2479 (make-local-variable 'widget-example-repeat)
2480 (let ((inhibit-read-only t))
2481 (erase-buffer))
2482 (remove-overlays)
2483 ;; Some help for the user.
2484 (widget-insert
2485 "\nEdit comments and tags for each image. Separate multiple tags
2486 with a comma. Move forward between fields using TAB or RET.
2487 Move to the previous field using backtab (S-TAB). Save by
2488 activating the Save button at the bottom of the form or cancel
2489 the operation by activating the Cancel button.\n\n")
2490 ;; Here comes all images and a comment and tag field for each
2491 ;; image.
2492 (let (thumb-file img comment-widget tag-widget)
2494 (dolist (file files)
2496 (setq thumb-file (image-dired-thumb-name file)
2497 img (create-image thumb-file))
2499 (insert-image img)
2500 (widget-insert "\n\nComment: ")
2501 (setq comment-widget
2502 (widget-create 'editable-field
2503 :size 60
2504 :format "%v "
2505 :value (or (image-dired-get-comment file) "")))
2506 (widget-insert "\nTags: ")
2507 (setq tag-widget
2508 (widget-create 'editable-field
2509 :size 60
2510 :format "%v "
2511 :value (or (mapconcat
2512 (lambda (tag)
2513 tag)
2514 (image-dired-list-tags file)
2515 ",") "")))
2516 ;; Save information in all widgets so that we can use it when
2517 ;; the user saves the form.
2518 (setq image-dired-widget-list
2519 (append image-dired-widget-list
2520 (list (list file comment-widget tag-widget))))
2521 (widget-insert "\n\n")))
2523 ;; Footer with Save and Cancel button.
2524 (widget-insert "\n")
2525 (widget-create 'push-button
2526 :notify
2527 (lambda (&rest ignore)
2528 (image-dired-save-information-from-widgets)
2529 (bury-buffer)
2530 (message "Done."))
2531 "Save")
2532 (widget-insert " ")
2533 (widget-create 'push-button
2534 :notify
2535 (lambda (&rest ignore)
2536 (bury-buffer)
2537 (message "Operation canceled."))
2538 "Cancel")
2539 (widget-insert "\n")
2540 (use-local-map widget-keymap)
2541 (widget-setup)
2542 ;; Jump to the first widget.
2543 (widget-forward 1)))
2545 (defun image-dired-save-information-from-widgets ()
2546 "Save information found in `image-dired-widget-list'.
2547 Use the information in `image-dired-widget-list' to save comments and
2548 tags to their respective image file. Internal function used by
2549 `image-dired-dired-edit-comment-and-tags'."
2550 (let (file comment tag-string tag-list lst)
2551 (image-dired-write-comments
2552 (mapcar
2553 (lambda (widget)
2554 (setq file (car widget)
2555 comment (widget-value (cadr widget)))
2556 (cons file comment))
2557 image-dired-widget-list))
2558 (image-dired-write-tags
2559 (dolist (widget image-dired-widget-list lst)
2560 (setq file (car widget)
2561 tag-string (widget-value (car (cddr widget)))
2562 tag-list (split-string tag-string ","))
2563 (dolist (tag tag-list)
2564 (push (cons file tag) lst))))))
2566 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2567 ;;;;;;;;; TEST-SECTION ;;;;;;;;;;;
2568 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2570 ;; (defvar image-dired-dir-max-size 12300000)
2572 ;; (defun image-dired-test-clean-old-files ()
2573 ;; "Clean `image-dired-dir' from old thumbnail files.
2574 ;; \"Oldness\" measured using last access time. If the total size of all
2575 ;; thumbnail files in `image-dired-dir' is larger than 'image-dired-dir-max-size',
2576 ;; old files are deleted until the max size is reached."
2577 ;; (let* ((files
2578 ;; (sort
2579 ;; (mapcar
2580 ;; (lambda (f)
2581 ;; (let ((fattribs (file-attributes f)))
2582 ;; ;; Get last access time and file size
2583 ;; `(,(nth 4 fattribs) ,(nth 7 fattribs) ,f)))
2584 ;; (directory-files (image-dired-dir) t ".+\.thumb\..+$"))
2585 ;; ;; Sort function. Compare time between two files.
2586 ;; '(lambda (l1 l2)
2587 ;; (time-less-p (car l1) (car l2)))))
2588 ;; (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) files))))
2589 ;; (while (> dirsize image-dired-dir-max-size)
2590 ;; (y-or-n-p
2591 ;; (format "Size of thumbnail directory: %d, delete old file %s? "
2592 ;; dirsize (cadr (cdar files))))
2593 ;; (delete-file (cadr (cdar files)))
2594 ;; (setq dirsize (- dirsize (car (cdar files))))
2595 ;; (setq files (cdr files)))))
2597 ;;;;;;;;;;;;;;;;;;;;;;,
2599 ;; (defun dired-speedbar-buttons (dired-buffer)
2600 ;; (when (and (boundp 'image-dired-use-speedbar)
2601 ;; image-dired-use-speedbar)
2602 ;; (let ((filename (with-current-buffer dired-buffer
2603 ;; (dired-get-filename))))
2604 ;; (when (and (not (string-equal filename (buffer-string)))
2605 ;; (string-match (image-file-name-regexp) filename))
2606 ;; (erase-buffer)
2607 ;; (insert (propertize
2608 ;; filename
2609 ;; 'display
2610 ;; (image-dired-get-thumbnail-image filename)))))))
2612 ;; (setq image-dired-use-speedbar t)
2614 (provide 'image-dired)
2616 ;; arch-tag: 9d11411d-331f-4380-8b44-8adfe3a0343e
2617 ;;; image-dired.el ends here