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