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