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