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