]> code.delx.au - gnu-emacs/blob - lisp/tumme.el
(tumme-write-tags): Add.
[gnu-emacs] / 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 (list (dired-get-filename)))
981 (setq files (dired-get-marked-files)))
982 (tumme-write-tags
983 (mapcar
984 (lambda (x)
985 (cons x tag))
986 files))))
987
988 (defun tumme-tag-thumbnail ()
989 "Tag current thumbnail."
990 (interactive)
991 (let ((tag (read-string "Tags to add (separate tags with a semicolon): ")))
992 (tumme-write-tags (list (cons (tumme-original-file-name) tag))))
993 (tumme-update-property
994 'tags (tumme-list-tags (tumme-original-file-name))))
995
996 ;;;###autoload
997 (defun tumme-delete-tag (arg)
998 "Remove tag for selected file(s).
999 With prefix argument ARG, remove tag from file at point."
1000 (interactive "P")
1001 (let ((tag (read-string "Tag to remove: "))
1002 files)
1003 (if arg
1004 (setq files (list (dired-get-filename)))
1005 (setq files (dired-get-marked-files)))
1006 (tumme-remove-tag files tag)))
1007
1008 (defun tumme-tag-thumbnail-remove ()
1009 "Remove tag from thumbnail."
1010 (interactive)
1011 (let ((tag (read-string "Tag to remove: ")))
1012 (tumme-remove-tag (tumme-original-file-name) tag))
1013 (tumme-update-property
1014 'tags (tumme-list-tags (tumme-original-file-name))))
1015
1016 (defun tumme-original-file-name ()
1017 "Get original file name for thumbnail or display image at point."
1018 (get-text-property (point) 'original-file-name))
1019
1020 (defun tumme-associated-dired-buffer ()
1021 "Get associated dired buffer at point."
1022 (get-text-property (point) 'associated-dired-buffer))
1023
1024 (defun tumme-get-buffer-window (buf)
1025 "Return window where buffer BUF is."
1026 (get-window-with-predicate
1027 (lambda (window)
1028 (equal (window-buffer window) buf))
1029 nil t))
1030
1031 (defun tumme-track-original-file ()
1032 "Track the original file in the associated dired buffer.
1033 See documentation for `tumme-toggle-movement-tracking'. Interactive
1034 use only useful if `tumme-track-movement' is nil."
1035 (interactive)
1036 (let ((old-buf (current-buffer))
1037 (dired-buf (tumme-associated-dired-buffer))
1038 (file-name (tumme-original-file-name)))
1039 (when (and dired-buf file-name)
1040 (setq file-name (file-name-nondirectory file-name))
1041 (set-buffer dired-buf)
1042 (goto-char (point-min))
1043 (if (not (search-forward file-name nil t))
1044 (message "Could not track file")
1045 (dired-move-to-filename)
1046 (set-window-point
1047 (tumme-get-buffer-window dired-buf) (point)))
1048 (set-buffer old-buf))))
1049
1050 (defun tumme-toggle-movement-tracking ()
1051 "Turn on and off `tumme-track-movement'.
1052 Tracking of the movements between thumbnail and dired buffer so that
1053 they are \"mirrored\" in the dired buffer. When this is on, moving
1054 around in the thumbnail or dired buffer will find the matching
1055 position in the other buffer."
1056 (interactive)
1057 (setq tumme-track-movement (not tumme-track-movement))
1058 (message "Tracking %s" (if tumme-track-movement "on" "off")))
1059
1060 (defun tumme-track-thumbnail ()
1061 "Track current dired file's thumb in `tumme-thumbnail-buffer'.
1062 This is almost the same as what `tumme-track-original-file' does, but
1063 the other way around."
1064 (let ((file (dired-get-filename))
1065 (old-buf (current-buffer))
1066 prop-val found)
1067 (when (get-buffer tumme-thumbnail-buffer)
1068 (set-buffer tumme-thumbnail-buffer)
1069 (goto-char (point-min))
1070 (while (and (not (eobp))
1071 (not found))
1072 (if (and (setq prop-val
1073 (get-text-property (point) 'original-file-name))
1074 (string= prop-val file))
1075 (setq found t))
1076 (if (not found)
1077 (forward-char 1)))
1078 (when found
1079 (set-window-point
1080 (tumme-thumbnail-window) (point))
1081 (tumme-display-thumb-properties))
1082 (set-buffer old-buf))))
1083
1084 (defun tumme-dired-next-line (&optional arg)
1085 "Call `dired-next-line', then track thumbnail.
1086 This can safely replace `dired-next-line'. With prefix argument, move
1087 ARG lines."
1088 (interactive "P")
1089 (dired-next-line (or arg 1))
1090 (if tumme-track-movement
1091 (tumme-track-thumbnail)))
1092
1093 (defun tumme-dired-previous-line (&optional arg)
1094 "Call `dired-previous-line', then track thumbnail.
1095 This can safely replace `dired-previous-line'. With prefix argument,
1096 move ARG lines."
1097 (interactive "P")
1098 (dired-previous-line (or arg 1))
1099 (if tumme-track-movement
1100 (tumme-track-thumbnail)))
1101
1102 (defun tumme-forward-char ()
1103 "Move to next image and display properties."
1104 (interactive)
1105 ;; Before we move, make sure that there is an image two positions
1106 ;; forward.
1107 (when (save-excursion
1108 (forward-char 2)
1109 (tumme-image-at-point-p))
1110 (forward-char)
1111 (while (and (not (eobp))
1112 (not (tumme-image-at-point-p)))
1113 (forward-char))
1114 (if tumme-track-movement
1115 (tumme-track-original-file)))
1116 (tumme-display-thumb-properties))
1117
1118 (defun tumme-backward-char ()
1119 "Move to previous image and display properties."
1120 (interactive)
1121 (when (not (bobp))
1122 (backward-char)
1123 (while (and (not (bobp))
1124 (not (tumme-image-at-point-p)))
1125 (backward-char))
1126 (if tumme-track-movement
1127 (tumme-track-original-file)))
1128 (tumme-display-thumb-properties))
1129
1130 (defun tumme-next-line ()
1131 "Move to next line and display properties."
1132 (interactive)
1133 (next-line 1)
1134 ;; If we end up in an empty spot, back up to the next thumbnail.
1135 (if (not (tumme-image-at-point-p))
1136 (tumme-backward-char))
1137 (if tumme-track-movement
1138 (tumme-track-original-file))
1139 (tumme-display-thumb-properties))
1140
1141
1142 (defun tumme-previous-line ()
1143 "Move to previous line and display properties."
1144 (interactive)
1145 (previous-line 1)
1146 ;; If we end up in an empty spot, back up to the next
1147 ;; thumbnail. This should only happen if the user deleted a
1148 ;; thumbnail and did not refresh, so it is not very common. But we
1149 ;; can handle it in a good manner, so why not?
1150 (if (not (tumme-image-at-point-p))
1151 (tumme-backward-char))
1152 (if tumme-track-movement
1153 (tumme-track-original-file))
1154 (tumme-display-thumb-properties))
1155
1156 (defun tumme-format-properties-string (buf file props comment)
1157 "Format display properties.
1158 BUF is the associated dired buffer, FILE is the original image file
1159 name, PROPS is a list of tags and COMMENT is the image files's
1160 comment."
1161 (format-spec
1162 tumme-display-properties-format
1163 (list
1164 (cons ?b buf)
1165 (cons ?f file)
1166 (cons ?t (or (princ props) ""))
1167 (cons ?c (or comment "")))))
1168
1169 (defun tumme-display-thumb-properties ()
1170 "Display thumbnail properties in the echo area."
1171 (if (not (eobp))
1172 (let ((file-name (file-name-nondirectory (tumme-original-file-name)))
1173 (dired-buf (buffer-name (tumme-associated-dired-buffer)))
1174 (props (mapconcat
1175 'princ
1176 (get-text-property (point) 'tags)
1177 ", "))
1178 (comment (get-text-property (point) 'comment)))
1179 (if file-name
1180 (message
1181 (tumme-format-properties-string
1182 dired-buf
1183 file-name
1184 props
1185 comment))))))
1186
1187 (defun tumme-dired-file-marked-p ()
1188 "Check whether file on current line is marked or not."
1189 (save-excursion
1190 (beginning-of-line)
1191 (not (looking-at "^ .*$"))))
1192
1193 (defun tumme-modify-mark-on-thumb-original-file (command)
1194 "Modify mark in dired buffer.
1195 This is quite ugly but I don't know how to implemented in a better
1196 way. COMMAND is one of 'mark for marking file in dired, 'unmark for
1197 unmarking file in dired or 'flag for flagging file for delete in
1198 dired."
1199 (let ((file-name (tumme-original-file-name))
1200 (dired-buf (tumme-associated-dired-buffer)))
1201 (if (not (and dired-buf file-name))
1202 (message "No image, or image with correct properties, at point.")
1203 (with-current-buffer dired-buf
1204 (message file-name)
1205 (setq file-name (file-name-nondirectory file-name))
1206 (goto-char (point-min))
1207 (if (search-forward file-name nil t)
1208 (cond ((eq command 'mark) (dired-mark 1))
1209 ((eq command 'unmark) (dired-unmark 1))
1210 ((eq command 'toggle)
1211 (if (tumme-dired-file-marked-p)
1212 (dired-unmark 1)
1213 (dired-mark 1)))
1214 ((eq command 'flag) (dired-flag-file-deletion 1))))))))
1215
1216 (defun tumme-mark-thumb-original-file ()
1217 "Mark original image file in associated dired buffer."
1218 (interactive)
1219 (tumme-modify-mark-on-thumb-original-file 'mark)
1220 (tumme-forward-char))
1221
1222 (defun tumme-unmark-thumb-original-file ()
1223 "Unmark original image file in associated dired buffer."
1224 (interactive)
1225 (tumme-modify-mark-on-thumb-original-file 'unmark)
1226 (tumme-forward-char))
1227
1228 (defun tumme-flag-thumb-original-file ()
1229 "Flag original image file for deletion in associated dired buffer."
1230 (interactive)
1231 (tumme-modify-mark-on-thumb-original-file 'flag)
1232 (tumme-forward-char))
1233
1234 (defun tumme-toggle-mark-thumb-original-file ()
1235 "Toggle mark on original image file in associated dired buffer."
1236 (interactive)
1237 (tumme-modify-mark-on-thumb-original-file 'toggle))
1238
1239 (defun tumme-jump-original-dired-buffer ()
1240 "Jump to the dired buffer associated with the current image file.
1241 You probably want to use this together with
1242 `tumme-track-original-file'."
1243 (interactive)
1244 (let ((buf (tumme-associated-dired-buffer))
1245 window frame)
1246 (setq window (tumme-get-buffer-window buf))
1247 (if window
1248 (progn
1249 (if (not (equal (selected-frame) (setq frame (window-frame window))))
1250 (select-frame-set-input-focus frame))
1251 (select-window window))
1252 (message "Associated dired buffer not visible"))))
1253
1254 ;;;###autoload
1255 (defun tumme-jump-thumbnail-buffer ()
1256 "Jump to thumbnail buffer."
1257 (interactive)
1258 (let ((window (tumme-thumbnail-window))
1259 frame)
1260 (if window
1261 (progn
1262 (if (not (equal (selected-frame) (setq frame (window-frame window))))
1263 (select-frame-set-input-focus frame))
1264 (select-window window))
1265 (message "Thumbnail buffer not visible"))))
1266
1267 (defvar tumme-thumbnail-mode-map (make-sparse-keymap)
1268 "Keymap for `tumme-thumbnail-mode'.")
1269
1270 (defvar tumme-thumbnail-mode-line-up-map (make-sparse-keymap)
1271 "Keymap for line-up commands in `tumme-thumbnail-mode'.")
1272
1273 (defvar tumme-thumbnail-mode-tag-map (make-sparse-keymap)
1274 "Keymap for tag commands in `tumme-thumbnail-mode'.")
1275
1276 (defun tumme-define-thumbnail-mode-keymap ()
1277 "Define keymap for `tumme-thumbnail-mode'."
1278
1279 ;; Keys
1280 (define-key tumme-thumbnail-mode-map [right] 'tumme-forward-char)
1281 (define-key tumme-thumbnail-mode-map [left] 'tumme-backward-char)
1282 (define-key tumme-thumbnail-mode-map [up] 'tumme-previous-line)
1283 (define-key tumme-thumbnail-mode-map [down] 'tumme-next-line)
1284 (define-key tumme-thumbnail-mode-map "\C-f" 'tumme-forward-char)
1285 (define-key tumme-thumbnail-mode-map "\C-b" 'tumme-backward-char)
1286 (define-key tumme-thumbnail-mode-map "\C-p" 'tumme-previous-line)
1287 (define-key tumme-thumbnail-mode-map "\C-n" 'tumme-next-line)
1288
1289 (define-key tumme-thumbnail-mode-map "d" 'tumme-flag-thumb-original-file)
1290 (define-key tumme-thumbnail-mode-map [delete]
1291 'tumme-flag-thumb-original-file)
1292 (define-key tumme-thumbnail-mode-map "m" 'tumme-mark-thumb-original-file)
1293 (define-key tumme-thumbnail-mode-map "u" 'tumme-unmark-thumb-original-file)
1294 (define-key tumme-thumbnail-mode-map "." 'tumme-track-original-file)
1295 (define-key tumme-thumbnail-mode-map [tab] 'tumme-jump-original-dired-buffer)
1296
1297 ;; add line-up map
1298 (define-key tumme-thumbnail-mode-map "g" tumme-thumbnail-mode-line-up-map)
1299
1300 ;; map it to "g" so that the user can press it more quickly
1301 (define-key tumme-thumbnail-mode-line-up-map "g" 'tumme-line-up-dynamic)
1302 ;; "f" for "fixed" number of thumbs per row
1303 (define-key tumme-thumbnail-mode-line-up-map "f" 'tumme-line-up)
1304 ;; "i" for "interactive"
1305 (define-key tumme-thumbnail-mode-line-up-map "i" 'tumme-line-up-interactive)
1306
1307 ;; add tag map
1308 (define-key tumme-thumbnail-mode-map "t" tumme-thumbnail-mode-tag-map)
1309
1310 ;; map it to "t" so that the user can press it more quickly
1311 (define-key tumme-thumbnail-mode-tag-map "t" 'tumme-tag-thumbnail)
1312 ;; "r" for "remove"
1313 (define-key tumme-thumbnail-mode-tag-map "r" 'tumme-tag-thumbnail-remove)
1314
1315 (define-key tumme-thumbnail-mode-map "\C-m"
1316 'tumme-display-thumbnail-original-image)
1317 (define-key tumme-thumbnail-mode-map [C-return]
1318 'tumme-thumbnail-display-external)
1319
1320 (define-key tumme-thumbnail-mode-map "l" 'tumme-rotate-thumbnail-left)
1321 (define-key tumme-thumbnail-mode-map "r" 'tumme-rotate-thumbnail-right)
1322
1323 (define-key tumme-thumbnail-mode-map "L" 'tumme-rotate-original-left)
1324 (define-key tumme-thumbnail-mode-map "R" 'tumme-rotate-original-right)
1325
1326 (define-key tumme-thumbnail-mode-map "D"
1327 'tumme-thumbnail-set-image-description)
1328
1329 (define-key tumme-thumbnail-mode-map "\C-d" 'tumme-delete-char)
1330 (define-key tumme-thumbnail-mode-map " "
1331 'tumme-display-next-thumbnail-original)
1332 (define-key tumme-thumbnail-mode-map
1333 (kbd "DEL") 'tumme-display-previous-thumbnail-original)
1334 (define-key tumme-thumbnail-mode-map "c" 'tumme-comment-thumbnail)
1335 (define-key tumme-thumbnail-mode-map "q" 'tumme-kill-buffer-and-window)
1336
1337 ;; Mouse
1338 (define-key tumme-thumbnail-mode-map [mouse-2] 'tumme-mouse-display-image)
1339 (define-key tumme-thumbnail-mode-map [mouse-1] 'tumme-mouse-select-thumbnail)
1340
1341 ;; Seems I must first set C-down-mouse-1 to undefined, or else it
1342 ;; will trigger the buffer menu. If I try to instead bind
1343 ;; C-down-mouse-1 to `tumme-mouse-toggle-mark', I get a message
1344 ;; about C-mouse-1 not being defined afterwards. Annoying, but I
1345 ;; probably do not completely understand mouse events.
1346
1347 (define-key tumme-thumbnail-mode-map [C-down-mouse-1] 'undefined)
1348 (define-key tumme-thumbnail-mode-map [C-mouse-1] 'tumme-mouse-toggle-mark)
1349
1350 ;; Menu
1351 (define-key tumme-thumbnail-mode-map [menu-bar tumme]
1352 (cons "Tumme" (make-sparse-keymap "Tumme")))
1353
1354 (define-key tumme-thumbnail-mode-map
1355 [menu-bar tumme tumme-kill-buffer-and-window]
1356 '("Quit" . tumme-kill-buffer-and-window))
1357
1358 (define-key tumme-thumbnail-mode-map
1359 [menu-bar tumme tumme-delete-char]
1360 '("Delete thumbnail from buffer" . tumme-delete-char))
1361
1362 (define-key tumme-thumbnail-mode-map
1363 [menu-bar tumme tumme-tag-thumbnail-remove]
1364 '("Remove tag from thumbnail" . tumme-tag-thumbnail-remove))
1365
1366 (define-key tumme-thumbnail-mode-map
1367 [menu-bar tumme tumme-tag-thumbnail]
1368 '("Tag thumbnail" . tumme-tag-thumbnail))
1369
1370 (define-key tumme-thumbnail-mode-map
1371 [menu-bar tumme tumme-comment-thumbnail]
1372 '("Comment thumbnail" . tumme-comment-thumbnail))
1373
1374 (define-key tumme-thumbnail-mode-map
1375 [menu-bar tumme tumme-refresh-thumb]
1376 '("Refresh thumb" . tumme-refresh-thumb))
1377 (define-key tumme-thumbnail-mode-map
1378 [menu-bar tumme tumme-line-up-dynamic]
1379 '("Dynamic line up" . tumme-line-up-dynamic))
1380 (define-key tumme-thumbnail-mode-map
1381 [menu-bar tumme tumme-line-up]
1382 '("Line up thumbnails" . tumme-line-up))
1383
1384 (define-key tumme-thumbnail-mode-map
1385 [menu-bar tumme tumme-rotate-thumbnail-left]
1386 '("Rotate thumbnail left" . tumme-rotate-thumbnail-left))
1387 (define-key tumme-thumbnail-mode-map
1388 [menu-bar tumme tumme-rotate-thumbnail-right]
1389 '("Rotate thumbnail right" . tumme-rotate-thumbnail-right))
1390
1391 (define-key tumme-thumbnail-mode-map
1392 [menu-bar tumme tumme-rotate-original-left]
1393 '("Rotate original left" . tumme-rotate-original-left))
1394 (define-key tumme-thumbnail-mode-map
1395 [menu-bar tumme tumme-rotate-original-right]
1396 '("Rotate original right" . tumme-rotate-original-right))
1397
1398 (define-key tumme-thumbnail-mode-map
1399 [menu-bar tumme tumme-toggle-movement-tracking]
1400 '("Toggle movement tracking on/off" . tumme-toggle-movement-tracking))
1401
1402 (define-key tumme-thumbnail-mode-map
1403 [menu-bar tumme tumme-jump-original-dired-buffer]
1404 '("Jump to dired buffer" . tumme-jump-original-dired-buffer))
1405 (define-key tumme-thumbnail-mode-map
1406 [menu-bar tumme tumme-track-original-file]
1407 '("Track original" . tumme-track-original-file))
1408
1409 (define-key tumme-thumbnail-mode-map
1410 [menu-bar tumme tumme-flag-thumb-original-file]
1411 '("Flag original for deletion" . tumme-flag-thumb-original-file))
1412 (define-key tumme-thumbnail-mode-map
1413 [menu-bar tumme tumme-unmark-thumb-original-file]
1414 '("Unmark original" . tumme-unmark-thumb-original-file))
1415 (define-key tumme-thumbnail-mode-map
1416 [menu-bar tumme tumme-mark-thumb-original-file]
1417 '("Mark original" . tumme-mark-thumb-original-file))
1418
1419 (define-key tumme-thumbnail-mode-map
1420 [menu-bar tumme tumme-thumbnail-display-external]
1421 '("Display in external viewer" . tumme-thumbnail-display-external))
1422 (define-key tumme-thumbnail-mode-map
1423 [menu-bar tumme tumme-display-thumbnail-original-image]
1424 '("Display image" . tumme-display-thumbnail-original-image)))
1425
1426 (defvar tumme-display-image-mode-map (make-sparse-keymap)
1427 "Keymap for `tumme-display-image-mode'.")
1428
1429 (defun tumme-define-display-image-mode-keymap ()
1430 "Define keymap for `tumme-display-image-mode'."
1431
1432 ;; Keys
1433 (define-key tumme-display-image-mode-map "q" 'tumme-kill-buffer-and-window)
1434
1435 (define-key tumme-display-image-mode-map "f"
1436 'tumme-display-current-image-full)
1437
1438 (define-key tumme-display-image-mode-map "s"
1439 'tumme-display-current-image-sized)
1440
1441 ;; Menu
1442 (define-key tumme-display-image-mode-map [menu-bar tumme]
1443 (cons "Tumme" (make-sparse-keymap "Tumme")))
1444
1445 (define-key tumme-display-image-mode-map
1446 [menu-bar tumme tumme-kill-buffer-and-window]
1447 '("Quit" . tumme-kill-buffer-and-window))
1448
1449 (define-key tumme-display-image-mode-map
1450 [menu-bar tumme tumme-display-current-image-sized]
1451 '("Display original, sized to fit" . tumme-display-current-image-sized))
1452
1453 (define-key tumme-display-image-mode-map
1454 [menu-bar tumme tumme-display-current-image-full]
1455 '("Display original, full size" . tumme-display-current-image-full))
1456
1457 )
1458
1459 (defun tumme-display-current-image-full ()
1460 "Display current image in full size."
1461 (interactive)
1462 (let ((file (tumme-original-file-name)))
1463 (if file
1464 (progn
1465 (tumme-display-image file t)
1466 (message "Full size image displayed"))
1467 (error "No original file name at point"))))
1468
1469 (defun tumme-display-current-image-sized ()
1470 "Display current image in sized to fit window dimensions."
1471 (interactive)
1472 (let ((file (tumme-original-file-name)))
1473 (if file
1474 (progn
1475 (tumme-display-image file)
1476 (message "Full size image displayed"))
1477 (error "No original file name at point"))))
1478
1479 (define-derived-mode tumme-thumbnail-mode
1480 fundamental-mode "tumme-thumbnail"
1481 "Browse and manipulate thumbnail images using dired.
1482 Use `tumme-dired' and `tumme-setup-dired-keybindings' to get a
1483 nice setup to start with."
1484 (tumme-define-thumbnail-mode-keymap)
1485 (message "tumme-thumbnail-mode enabled"))
1486
1487 (define-derived-mode tumme-display-image-mode
1488 fundamental-mode "tumme-image-display"
1489 "Mode for displaying and manipulating original image.
1490 Resized or in full-size."
1491 (tumme-define-display-image-mode-keymap)
1492 (message "tumme-display-image-mode enabled"))
1493
1494 ;;;###autoload
1495 (defun tumme-setup-dired-keybindings ()
1496 "Setup easy-to-use keybindings for the commands to be used in dired mode.
1497 Note that n, p and <down> and <up> will be hijacked and bound to
1498 `tumme-dired-x-line'."
1499 (interactive)
1500
1501 ;; Hijack previous and next line movement. Let C-p and C-b be
1502 ;; though...
1503
1504 (define-key dired-mode-map "p" 'tumme-dired-previous-line)
1505 (define-key dired-mode-map "n" 'tumme-dired-next-line)
1506 (define-key dired-mode-map [up] 'tumme-dired-previous-line)
1507 (define-key dired-mode-map [down] 'tumme-dired-next-line)
1508
1509 (define-key dired-mode-map (kbd "C-S-n") 'tumme-next-line-and-display)
1510 (define-key dired-mode-map (kbd "C-S-p") 'tumme-previous-line-and-display)
1511 (define-key dired-mode-map (kbd "C-S-m") 'tumme-mark-and-display-next)
1512
1513 (define-key dired-mode-map "\C-td" 'tumme-display-thumbs)
1514 (define-key dired-mode-map "\C-tt" 'tumme-tag-files)
1515 (define-key dired-mode-map "\C-tr" 'tumme-delete-tag)
1516 (define-key dired-mode-map [tab] 'tumme-jump-thumbnail-buffer)
1517 (define-key dired-mode-map "\C-ti" 'tumme-dired-display-image)
1518 (define-key dired-mode-map "\C-tx" 'tumme-dired-display-external)
1519 (define-key dired-mode-map "\C-ta" 'tumme-display-thumbs-append)
1520 (define-key dired-mode-map "\C-t." 'tumme-display-thumb)
1521 (define-key dired-mode-map "\C-tc" 'tumme-dired-comment-files)
1522 (define-key dired-mode-map "\C-tf" 'tumme-mark-tagged-files)
1523
1524 ;; Menu for dired
1525 (define-key dired-mode-map [menu-bar tumme]
1526 (cons "Tumme" (make-sparse-keymap "Tumme")))
1527
1528 (define-key dired-mode-map [menu-bar tumme tumme-copy-with-exif-file-name]
1529 '("Copy with EXIF file name" . tumme-copy-with-exif-file-name))
1530
1531 (define-key dired-mode-map [menu-bar tumme tumme-dired-comment-files]
1532 '("Comment files" . tumme-dired-comment-files))
1533
1534 (define-key dired-mode-map [menu-bar tumme tumme-mark-tagged-files]
1535 '("Mark tagged files" . tumme-mark-tagged-files))
1536
1537 (define-key dired-mode-map [menu-bar tumme tumme-delete-tag]
1538 '("Remove tag from files" . tumme-delete-tag))
1539
1540 (define-key dired-mode-map [menu-bar tumme tumme-tag-files]
1541 '("Tag files" . tumme-tag-files))
1542
1543 (define-key dired-mode-map [menu-bar tumme tumme-jump-thumbnail-buffer]
1544 '("Jump to thumbnail buffer" . tumme-jump-thumbnail-buffer))
1545
1546 (define-key dired-mode-map [menu-bar tumme tumme-toggle-movement-tracking]
1547 '("Toggle movement tracking" . tumme-toggle-movement-tracking))
1548
1549 (define-key dired-mode-map
1550 [menu-bar tumme tumme-toggle-append-browsing]
1551 '("Toggle append browsing" . tumme-toggle-append-browsing))
1552
1553 (define-key dired-mode-map
1554 [menu-bar tumme tumme-toggle-disp-props]
1555 '("Toggle display properties" . tumme-toggle-dired-display-properties))
1556
1557 (define-key dired-mode-map
1558 [menu-bar tumme tumme-dired-display-external]
1559 '("Display in external viewer" . tumme-dired-display-external))
1560 (define-key dired-mode-map
1561 [menu-bar tumme tumme-dired-display-image]
1562 '("Display image" . tumme-dired-display-image))
1563 (define-key dired-mode-map
1564 [menu-bar tumme tumme-display-thumb]
1565 '("Display this thumbnail" . tumme-display-thumb))
1566 (define-key dired-mode-map
1567 [menu-bar tumme tumme-display-thumbs-append]
1568 '("Display thumbnails append" . tumme-display-thumbs-append))
1569 (define-key dired-mode-map
1570 [menu-bar tumme tumme-display-thumbs]
1571 '("Display thumbnails" . tumme-display-thumbs))
1572
1573 (define-key dired-mode-map
1574 [menu-bar tumme tumme-create-thumbs]
1575 '("Create thumbnails for marked files" . tumme-create-thumbs))
1576
1577 (define-key dired-mode-map
1578 [menu-bar tumme tumme-mark-and-display-next]
1579 '("Mark and display next" . tumme-mark-and-display-next))
1580 (define-key dired-mode-map
1581 [menu-bar tumme tumme-previous-line-and-display]
1582 '("Display thumb for previous file" . tumme-previous-line-and-display))
1583 (define-key dired-mode-map
1584 [menu-bar tumme tumme-next-line-and-display]
1585 '("Display thumb for next file" . tumme-next-line-and-display)))
1586
1587 (defun tumme-create-thumbs (&optional arg)
1588 "Create thumbnail images for all marked files in dired.
1589 With prefix argument ARG, create thumbnails even if they already exist
1590 \(i.e. use this to refresh your thumbnails)."
1591 (interactive "P")
1592 (let (curr-file thumb-name files count)
1593 (setq files (dired-get-marked-files))
1594 (mapcar
1595 (lambda (curr-file)
1596 (setq thumb-name (tumme-thumb-name curr-file))
1597 ;; If the user overrides the exist check, we must clear the
1598 ;; image cache so that if the user wants to display the
1599 ;; thumnail, it is not fetched from cache.
1600 (if arg
1601 (clear-image-cache))
1602 (if (or (not (file-exists-p thumb-name))
1603 arg)
1604 (if (not (= 0 (tumme-create-thumb curr-file
1605 (tumme-thumb-name curr-file))))
1606 (error "Thumb could not be created"))))
1607 files)))
1608
1609 (defvar tumme-slideshow-timer nil
1610 "Slideshow timer.")
1611
1612 (defvar tumme-slideshow-count 0
1613 "Keeping track on number of images in slideshow.")
1614
1615 (defvar tumme-slideshow-times 0
1616 "Number of pictures to display in slideshow.")
1617
1618 (defun tumme-slideshow-step ()
1619 "Step to next file, if `tumme-slideshow-times' has not been reached."
1620 (if (< tumme-slideshow-count tumme-slideshow-times)
1621 (progn
1622 (message "%s" (1+ tumme-slideshow-count))
1623 (setq tumme-slideshow-count (1+ tumme-slideshow-count))
1624 (tumme-next-line-and-display))
1625 (tumme-slideshow-stop)))
1626
1627 (defun tumme-slideshow-start ()
1628 "Start slideshow.
1629 Ask user for number of images to show and the delay in between."
1630 (interactive)
1631 (setq tumme-slideshow-count 0)
1632 (setq tumme-slideshow-times (string-to-number (read-string "How many: ")))
1633 (let ((repeat (string-to-number
1634 (read-string
1635 "Delay, in seconds. Decimals are accepted : " "1"))))
1636 (setq tumme-slideshow-timer
1637 (run-with-timer
1638 0 repeat
1639 'tumme-slideshow-step))))
1640
1641 (defun tumme-slideshow-stop ()
1642 "Cancel slideshow."
1643 (interactive)
1644 (cancel-timer tumme-slideshow-timer))
1645
1646 (defun tumme-delete-char ()
1647 "Remove current thumbnail from thumbnail buffer and line up."
1648 (interactive)
1649 (let ((inhibit-read-only t))
1650 (delete-char 1)
1651 (if (looking-at " ")
1652 (delete-char 1))))
1653
1654 ;;;###autoload
1655 (defun tumme-display-thumbs-append ()
1656 "Append thumbnails to `tumme-thumbnail-buffer'."
1657 (interactive)
1658 (tumme-display-thumbs nil t t))
1659
1660 ;;;###autoload
1661 (defun tumme-display-thumb ()
1662 "Shorthard for `tumme-display-thumbs' with prefix argument."
1663 (interactive)
1664 (tumme-display-thumbs t nil t))
1665
1666 (defun tumme-line-up ()
1667 "Line up thumbnails according to `tumme-thumbs-per-row'.
1668 See also `tumme-line-up-dynamic'."
1669 (interactive)
1670 (let ((inhibit-read-only t))
1671 (goto-char (point-min))
1672 (while (and (not (tumme-image-at-point-p))
1673 (not (eobp)))
1674 (delete-char 1))
1675 (while (not (eobp))
1676 (forward-char)
1677 (while (and (not (tumme-image-at-point-p))
1678 (not (eobp)))
1679 (delete-char 1)))
1680 (goto-char (point-min))
1681 (let ((count 0))
1682 (while (not (eobp))
1683 (forward-char)
1684 (if (= tumme-thumbs-per-row 1)
1685 (insert "\n")
1686 (insert " ")
1687 (setq count (1+ count))
1688 (when (and (= count (- tumme-thumbs-per-row 1))
1689 (not (eobp)))
1690 (forward-char)
1691 (insert "\n")
1692 (setq count 0)))))
1693 (goto-char (point-min))))
1694
1695 (defun tumme-line-up-dynamic ()
1696 "Line up thumbnails images dynamically.
1697 Calculate how many thumbnails fit."
1698 (interactive)
1699 (let* ((char-width (frame-char-width))
1700 (width (tumme-window-width-pixels (tumme-thumbnail-window)))
1701 (tumme-thumbs-per-row
1702 (/ width
1703 (+ (* 2 tumme-thumb-relief)
1704 (* 2 tumme-thumb-margin)
1705 tumme-thumb-width char-width))))
1706 (tumme-line-up)))
1707
1708 (defun tumme-line-up-interactive ()
1709 "Line up thumbnails interactively.
1710 Ask user how many thumbnails should be displayed per row."
1711 (interactive)
1712 (let ((tumme-thumbs-per-row
1713 (string-to-number (read-string "How many thumbs per row: "))))
1714 (if (not (> tumme-thumbs-per-row 0))
1715 (message "Number must be greater than 0")
1716 (tumme-line-up))))
1717
1718 (defun tumme-thumbnail-display-external ()
1719 "Display original image for thumbnail at point using external viewer."
1720 (interactive)
1721 (let ((file (tumme-original-file-name)))
1722 (if (not (tumme-image-at-point-p))
1723 (message "No thumbnail at point")
1724 (if (not file)
1725 (message "No original file name found")
1726 (call-process shell-file-name nil nil nil shell-command-switch
1727 (format "%s \"%s\"" tumme-external-viewer file))))))
1728
1729 ;;;###autoload
1730 (defun tumme-dired-display-external ()
1731 "Display file at point using an external viewer."
1732 (interactive)
1733 (let ((file (dired-get-filename)))
1734 (call-process shell-file-name nil nil nil shell-command-switch
1735 (format "%s \"%s\"" tumme-external-viewer file))))
1736
1737 (defun tumme-window-width-pixels (window)
1738 "Calculate WINDOW width in pixels."
1739 (* (window-width window) (frame-char-width)))
1740
1741 (defun tumme-window-height-pixels (window)
1742 "Calculate WINDOW height in pixels."
1743 ;; Note: The mode-line consumes one line
1744 (* (- (window-height window) 1) (frame-char-height)))
1745
1746 (defun tumme-display-window ()
1747 "Return window where `tumme-display-image-buffer' is visible."
1748 (get-window-with-predicate
1749 (lambda (window)
1750 (equal (buffer-name (window-buffer window)) tumme-display-image-buffer))
1751 nil t))
1752
1753 (defun tumme-thumbnail-window ()
1754 "Return window where `tumme-thumbnail-buffer' is visible."
1755 (get-window-with-predicate
1756 (lambda (window)
1757 (equal (buffer-name (window-buffer window)) tumme-thumbnail-buffer))
1758 nil t))
1759
1760 (defun tumme-associated-dired-buffer-window ()
1761 "Return window where associated dired buffer is visible."
1762 (let (buf)
1763 (if (tumme-image-at-point-p)
1764 (progn
1765 (setq buf (tumme-associated-dired-buffer))
1766 (get-window-with-predicate
1767 (lambda (window)
1768 (equal (window-buffer window) buf))))
1769 (error "No thumbnail image at point"))))
1770
1771 (defun tumme-display-window-width ()
1772 "Return width, in pixels, of tumme's image display window."
1773 (- (tumme-window-width-pixels (tumme-display-window))
1774 tumme-display-window-width-correction))
1775
1776 (defun tumme-display-window-height ()
1777 "Return height, in pixels, of tumme's image display window."
1778 (- (tumme-window-height-pixels (tumme-display-window))
1779 tumme-display-window-height-correction))
1780
1781 (defun tumme-display-image (file &optional original-size)
1782 "Display image FILE in image buffer.
1783 Use this when you want to display the image, semi sized, in a new
1784 window. The image is sized to fit the display window (using a
1785 temporary file, don't worry). Because of this, it will not be as
1786 quick as opening it directly, but on most modern systems it
1787 should feel snappy enough.
1788
1789 If optional argument ORIGINAL-SIZE is non-nil, display image in its
1790 original size."
1791 (let ((new-file (expand-file-name tumme-temp-image-file))
1792 width height command ret)
1793 (setq file (expand-file-name file))
1794 (if (not original-size)
1795 (progn
1796 (setq width (tumme-display-window-width))
1797 (setq height (tumme-display-window-height))
1798 (setq command
1799 (format-spec
1800 tumme-cmd-create-temp-image-options
1801 (list
1802 (cons ?p tumme-cmd-create-temp-image-program)
1803 (cons ?w width)
1804 (cons ?h height)
1805 (cons ?f file)
1806 (cons ?t new-file))))
1807 (setq ret (call-process shell-file-name nil nil nil
1808 shell-command-switch command))
1809 (if (not (= 0 ret))
1810 (error "Could not resize image")))
1811 (copy-file file new-file t))
1812 (with-current-buffer (tumme-create-display-image-buffer)
1813 (let ((inhibit-read-only t))
1814 (erase-buffer)
1815 (clear-image-cache)
1816 (tumme-insert-image tumme-temp-image-file 'jpeg 0 0)
1817 (goto-char (point-min))
1818 (tumme-update-property 'original-file-name file)))))
1819
1820 (defun tumme-display-thumbnail-original-image (&optional arg)
1821 "Display current thumbnail's original image in display buffer.
1822 See documentation for `tumme-display-image' for more information.
1823 With prefix argument ARG, display image in its original size."
1824 (interactive "P")
1825 (let ((file (tumme-original-file-name)))
1826 (if (not (string-equal major-mode "tumme-thumbnail-mode"))
1827 (message "Not in tumme-thumbnail-mode")
1828 (if (not (tumme-image-at-point-p))
1829 (message "No thumbnail at point")
1830 (if (not file)
1831 (message "No original file name found")
1832 (tumme-create-display-image-buffer)
1833 (display-buffer tumme-display-image-buffer)
1834 (tumme-display-image file arg))))))
1835
1836
1837 ;;;###autoload
1838 (defun tumme-dired-display-image (&optional arg)
1839 "Display current image file.
1840 See documentation for `tumme-display-image' for more information.
1841 With prefix argument ARG, display image in its original size."
1842 (interactive "P")
1843 (tumme-create-display-image-buffer)
1844 (display-buffer tumme-display-image-buffer)
1845 (tumme-display-image (dired-get-filename) arg))
1846
1847 (defun tumme-image-at-point-p ()
1848 "Return true if there is a tumme thumbnail at point."
1849 (get-text-property (point) 'tumme-thumbnail))
1850
1851 (defun tumme-rotate-thumbnail (degrees)
1852 "Rotate thumbnail DEGREES degrees."
1853 (if (not (tumme-image-at-point-p))
1854 (message "No thumbnail at point")
1855 (let ((file (tumme-thumb-name (tumme-original-file-name)))
1856 command)
1857 (setq command (format-spec
1858 tumme-cmd-rotate-thumbnail-options
1859 (list
1860 (cons ?p tumme-cmd-rotate-thumbnail-program)
1861 (cons ?d degrees)
1862 (cons ?t (expand-file-name file)))))
1863 (call-process shell-file-name nil nil nil shell-command-switch command)
1864 ;; Clear the cache to refresh image. I wish I could just refresh
1865 ;; the current file but I do not know how to do that. Yet...
1866 (clear-image-cache))))
1867
1868 (defun tumme-rotate-thumbnail-left ()
1869 "Rotate thumbnail left (counter clockwise) 90 degrees.
1870 The result of the rotation is displayed in the image display area
1871 and a confirmation is needed before the original image files is
1872 overwritten. This confirmation can be turned off using
1873 `tumme-rotate-original-ask-before-overwrite'."
1874 (interactive)
1875 (tumme-rotate-thumbnail "270"))
1876
1877 (defun tumme-rotate-thumbnail-right ()
1878 "Rotate thumbnail counter right (clockwise) 90 degrees.
1879 The result of the rotation is displayed in the image display area
1880 and a confirmation is needed before the original image files is
1881 overwritten. This confirmation can be turned off using
1882 `tumme-rotate-original-ask-before-overwrite'."
1883 (interactive)
1884 (tumme-rotate-thumbnail "90"))
1885
1886 (defun tumme-refresh-thumb ()
1887 "Force creation of new image for current thumbnail."
1888 (interactive)
1889 (let ((file (tumme-original-file-name)))
1890 (clear-image-cache)
1891 (tumme-create-thumb file (tumme-thumb-name file))))
1892
1893 (defun tumme-rotate-original (degrees)
1894 "Rotate original image DEGREES degrees."
1895 (if (not (tumme-image-at-point-p))
1896 (message "No image at point")
1897 (let ((file (tumme-original-file-name))
1898 command temp-file)
1899 (if (not (string-match "\.[jJ][pP[eE]?[gG]$" file))
1900 (error "Only JPEG images can be rotated!"))
1901 (setq command (format-spec
1902 tumme-cmd-rotate-original-options
1903 (list
1904 (cons ?p tumme-cmd-rotate-original-program)
1905 (cons ?d degrees)
1906 (cons ?o (expand-file-name file))
1907 (cons ?t tumme-temp-rotate-image-file))))
1908 (if (not (= 0 (call-process shell-file-name nil nil nil
1909 shell-command-switch command)))
1910 (error "Could not rotate image")
1911 (tumme-display-image tumme-temp-rotate-image-file)
1912 (if (or (and tumme-rotate-original-ask-before-overwrite
1913 (y-or-n-p
1914 "Rotate to temp file OK. Overwrite original image? "))
1915 (not tumme-rotate-original-ask-before-overwrite))
1916 (progn
1917 (copy-file tumme-temp-rotate-image-file file t)
1918 (tumme-refresh-thumb))
1919 (tumme-display-image file))))))
1920
1921 (defun tumme-rotate-original-left ()
1922 "Rotate original image left (counter clockwise) 90 degrees."
1923 (interactive)
1924 (tumme-rotate-original "270"))
1925
1926 (defun tumme-rotate-original-right ()
1927 "Rotate original image right (clockwise) 90 degrees."
1928 (interactive)
1929 (tumme-rotate-original "90"))
1930
1931 (defun tumme-get-exif-file-name (file)
1932 "Use the image's EXIF information to return a unique file name.
1933 The file name should be unique as long as you do not take more than
1934 one picture per second. The original file name is suffixed at the end
1935 for traceability. The format of the returned file name is
1936 YYYY_MM_DD_HH_MM_DD_ORIG_FILE_NAME.jpg. Used from
1937 `tumme-copy-with-exif-file-name'."
1938 (let (data no-exif-data-found)
1939 (if (not (string-match "\.[Jj][Pp][Ee]?[Gg]$" (expand-file-name file)))
1940 (progn
1941 (setq no-exif-data-found t)
1942 (setq data
1943 (format-time-string
1944 "%Y:%m:%d %H:%M:%S"
1945 (nth 5 (file-attributes (expand-file-name file))))))
1946 (setq data (tumme-get-exif-data (expand-file-name file)
1947 "DateTimeOriginal")))
1948 (while (string-match "[ :]" data)
1949 (setq data (replace-match "_" nil nil data)))
1950 (format "%s%s%s" data
1951 (if no-exif-data-found
1952 "_noexif_"
1953 "_")
1954 (file-name-nondirectory file))))
1955
1956 (defun tumme-thumbnail-set-image-description ()
1957 "Set the ImageDescription EXIF tag for the original image.
1958 If the image already has a value for this tag, it is used as the
1959 default value at the prompt."
1960 (interactive)
1961 (if (not (tumme-image-at-point-p))
1962 (message "No thumbnail at point")
1963 (let* ((file (tumme-original-file-name))
1964 (old-value (tumme-get-exif-data file "ImageDescription")))
1965 (if (eq 0
1966 (tumme-set-exif-data file "ImageDescription"
1967 (read-string "Value of ImageDescription: "
1968 old-value)))
1969 (message "Successfully wrote ImageDescription tag.")
1970 (error "Could not write ImageDescription tag")))))
1971
1972 (defun tumme-set-exif-data (file tag-name tag-value)
1973 "In FILE, set EXIF tag TAG-NAME to value TAG-VALUE."
1974 (let (command)
1975 (setq command (format-spec
1976 tumme-cmd-write-exif-data-options
1977 (list
1978 (cons ?p tumme-cmd-write-exif-data-program)
1979 (cons ?f (expand-file-name file))
1980 (cons ?t tag-name)
1981 (cons ?v tag-value))))
1982 (call-process shell-file-name nil nil nil shell-command-switch command)))
1983
1984 (defun tumme-get-exif-data (file tag-name)
1985 "From FILE, return EXIF tag TAG-NAME."
1986 (let ((buf (get-buffer-create "*tumme-get-exif-data*"))
1987 command tag-value)
1988 (setq command (format-spec
1989 tumme-cmd-read-exif-data-options
1990 (list
1991 (cons ?p tumme-cmd-read-exif-data-program)
1992 (cons ?f file)
1993 (cons ?t tag-name))))
1994 (with-current-buffer buf
1995 (delete-region (point-min) (point-max))
1996 (if (not (eq (call-process shell-file-name nil t nil
1997 shell-command-switch command) 0))
1998 (error "Could not get EXIF tag")
1999 (goto-char (point-min))
2000 ;; Clean buffer from newlines and carriage returns before
2001 ;; getting final info
2002 (while (search-forward-regexp "[\n\r]" nil t)
2003 (replace-match "" nil t))
2004 (setq tag-value (buffer-substring (point-min) (point-max)))))
2005 tag-value))
2006
2007 (defun tumme-copy-with-exif-file-name ()
2008 "Copy file with unique name to main image directory.
2009 Copy current or all marked files in dired to a new file in your
2010 main image directory, using a file name generated by
2011 `tumme-get-exif-file-name'. A typical usage for this if when
2012 copying images from a digital camera into the image directory.
2013
2014 Typically, you would open up the folder with the incoming
2015 digital images, mark the files to be copied, and execute this
2016 function. The result is a couple of new files in
2017 `tumme-main-image-directory' called
2018 2005_05_08_12_52_00_dscn0319.jpg,
2019 2005_05_08_14_27_45_dscn0320.jpg etc."
2020 (interactive)
2021 (let (new-name
2022 (files (dired-get-marked-files)))
2023 (mapcar
2024 (lambda (curr-file)
2025 (setq new-name
2026 (format "%s/%s"
2027 (file-name-as-directory
2028 (expand-file-name tumme-main-image-directory))
2029 (tumme-get-exif-file-name curr-file)))
2030 (message "Copying %s to %s" curr-file new-name)
2031 (copy-file curr-file new-name))
2032 files)))
2033
2034 (defun tumme-display-next-thumbnail-original ()
2035 "In thubnail buffer, move to next thumbnail and display the image."
2036 (interactive)
2037 (tumme-forward-char)
2038 (tumme-display-thumbnail-original-image))
2039
2040 (defun tumme-display-previous-thumbnail-original ()
2041 "Move to previous thumbnail and display image."
2042
2043 (interactive)
2044 (tumme-backward-char)
2045 (tumme-display-thumbnail-original-image))
2046
2047 (defun tumme-write-comment (file comment)
2048 "For FILE, write comment COMMENT in database."
2049 (save-excursion
2050 (let (end buf comment-beg)
2051 (setq buf (find-file tumme-db-file))
2052 (goto-char (point-min))
2053 (if (search-forward-regexp
2054 (format "^%s" file) nil t)
2055 (progn
2056 (end-of-line)
2057 (setq end (point))
2058 (beginning-of-line)
2059 ;; Delete old comment, if any
2060 (cond ((search-forward ";comment:" end t)
2061 (setq comment-beg (match-beginning 0))
2062 ;; Any tags after the comment?
2063 (if (search-forward ";" end t)
2064 (setq comment-end (- (point) 1))
2065 (setq comment-end end))
2066 ;; Delete comment tag and comment
2067 (delete-region comment-beg comment-end)))
2068 ;; Insert new comment
2069 (beginning-of-line)
2070 (if (not (search-forward ";" end t))
2071 (progn
2072 (end-of-line)
2073 (insert ";")))
2074 (insert (format "comment:%s;" comment)))
2075 ;; File does not exist in databse - add it.
2076 (goto-char (point-max))
2077 (insert (format "\n%s;comment:%s" file comment)))
2078 (save-buffer)
2079 (kill-buffer buf))))
2080
2081 (defun tumme-write-comments (file-comments)
2082 "Write file comments to database.
2083 Write file comments to one or more files. FILE-COMMENTS is an alist on
2084 the following form:
2085 ((FILE . COMMENT) ... )"
2086 (let (end comment-beg file comment)
2087 (with-temp-file tumme-db-file
2088 (insert-file-contents tumme-db-file)
2089 (dolist (elt file-comments)
2090 (setq file (car elt)
2091 comment (cdr elt))
2092 (goto-char (point-min))
2093 (if (search-forward-regexp (format "^%s.*$" file) nil t)
2094 (progn
2095 (setq end (point))
2096 (beginning-of-line)
2097 ;; Delete old comment, if any
2098 (when (search-forward ";comment:" end t)
2099 (setq comment-beg (match-beginning 0))
2100 ;; Any tags after the comment?
2101 (if (search-forward ";" end t)
2102 (setq comment-end (- (point) 1))
2103 (setq comment-end end))
2104 ;; Delete comment tag and comment
2105 (delete-region comment-beg comment-end))
2106 ;; Insert new comment
2107 (beginning-of-line)
2108 (unless (search-forward ";" end t)
2109 (end-of-line)
2110 (insert ";"))
2111 (insert (format "comment:%s;" comment)))
2112 ;; File does not exist in database - add it.
2113 (goto-char (point-max))
2114 (insert (format "\n%s;comment:%s" file comment)))))))
2115
2116 (defun tumme-update-property (prop value)
2117 "Update text property PROP with value VALUE at point."
2118 (let ((inhibit-read-only t))
2119 (put-text-property
2120 (point) (1+ (point))
2121 prop
2122 value)))
2123
2124 ;;;###autoload
2125 (defun tumme-dired-comment-files ()
2126 "Add comment to current or marked files in dired."
2127 (interactive)
2128 (let ((comment (tumme-read-comment)))
2129 (tumme-write-comments
2130 (mapcar
2131 (lambda (curr-file)
2132 (cons curr-file comment))
2133 (dired-get-marked-files)))))
2134
2135 (defun tumme-comment-thumbnail ()
2136 "Add comment to current thumbnail in thumbnail buffer."
2137 (interactive)
2138 (let* ((file (tumme-original-file-name))
2139 (comment (tumme-read-comment file)))
2140 (tumme-write-comments (list (cons file comment)))
2141 (tumme-update-property 'comment comment))
2142 (tumme-display-thumb-properties))
2143
2144 (defun tumme-read-comment (&optional file)
2145 "Read comment for an image.
2146 Read comment for an image, optionally using old comment from FILE
2147 as initial value."
2148 (let ((comment
2149 (read-string
2150 "Comment: "
2151 (if file (tumme-get-comment file)))))
2152 comment))
2153
2154 (defun tumme-get-comment (file)
2155 "Get comment for file FILE."
2156 (save-excursion
2157 (let (end buf comment-beg comment)
2158 (setq buf (find-file tumme-db-file))
2159 (goto-char (point-min))
2160 (when (search-forward-regexp
2161 (format "^%s" file) nil t)
2162 (end-of-line)
2163 (setq end (point))
2164 (beginning-of-line)
2165 (cond ((search-forward ";comment:" end t)
2166 (setq comment-beg (point))
2167 (if (search-forward ";" end t)
2168 (setq comment-end (- (point) 1))
2169 (setq comment-end end))
2170 (setq comment (buffer-substring
2171 comment-beg comment-end)))))
2172 (kill-buffer buf)
2173 comment)))
2174
2175 ;;;###autoload
2176 (defun tumme-mark-tagged-files ()
2177 "Use regexp to mark files with matching tag.
2178 A `tag' is a keyword, a piece of meta data, associated with an
2179 image file and stored in tumme's database file. This command
2180 lets you input a regexp and this will be matched against all tags
2181 on all image files in the database file. The files that have a
2182 matching tags will be marked in the dired buffer."
2183 (interactive)
2184 (let ((tag (read-string "Mark tagged files (regexp): "))
2185 (hits 0)
2186 files buf)
2187 (save-excursion
2188 (setq buf (find-file tumme-db-file))
2189 (goto-char (point-min))
2190 ;; Collect matches
2191 (while (search-forward-regexp
2192 (concat "\\(^[^;\n]+\\);.*" tag ".*$") nil t)
2193 (setq files (append (list (match-string 1)) files)))
2194 (kill-buffer buf)
2195 ;; Mark files
2196 (mapcar
2197 ;; I tried using `dired-mark-files-regexp' but it was
2198 ;; waaaay to slow.
2199 (lambda (curr-file)
2200 ;; Don't bother about hits found in other directories than
2201 ;; the current one.
2202 (when (string= (file-name-as-directory
2203 (expand-file-name default-directory))
2204 (file-name-as-directory
2205 (file-name-directory curr-file)))
2206 (setq curr-file (file-name-nondirectory curr-file))
2207 (goto-char (point-min))
2208 (when (search-forward-regexp (format "\\s %s$" curr-file) nil t)
2209 (setq hits (+ hits 1))
2210 (dired-mark 1))))
2211 files))
2212 (message "%d files with matching tag marked." hits)))
2213
2214 (defun tumme-mouse-display-image (event)
2215 "Use mouse EVENT, call `tumme-display-image' to display image.
2216 Track this in associated dired buffer if `tumme-track-movement' is
2217 non-nil."
2218 (interactive "e")
2219 (let (file)
2220 (mouse-set-point event)
2221 (goto-char (posn-point (event-end event)))
2222 (setq file (tumme-original-file-name))
2223 (if tumme-track-movement
2224 (tumme-track-original-file))
2225 (tumme-create-display-image-buffer)
2226 (display-buffer tumme-display-image-buffer)
2227 (tumme-display-image file)))
2228
2229 (defun tumme-mouse-select-thumbnail (event)
2230 "Use mouse EVENT to select thumbnail image.
2231 Track this in associated dired buffer if `tumme-track-movement' is
2232 non-nil."
2233 (interactive "e")
2234 (let (file)
2235 (mouse-set-point event)
2236 (goto-char (posn-point (event-end event)))
2237 (if tumme-track-movement
2238 (tumme-track-original-file)))
2239 (tumme-display-thumb-properties))
2240
2241 (defun tumme-mouse-toggle-mark (event)
2242 "Use mouse EVENT to toggle dired mark for thumbnail.
2243 Track this in associated dired buffer if `tumme-track-movement' is
2244 non-nil."
2245 (interactive "e")
2246 (let (file)
2247 (mouse-set-point event)
2248 (goto-char (posn-point (event-end event)))
2249 (if tumme-track-movement
2250 (tumme-track-original-file)))
2251 (tumme-toggle-mark-thumb-original-file))
2252
2253 (defun tumme-dired-display-properties ()
2254 "Display properties for dired file in the echo area."
2255 (interactive)
2256 (let* ((file (dired-get-filename))
2257 (file-name (file-name-nondirectory file))
2258 (dired-buf (buffer-name (current-buffer)))
2259 (props (mapconcat
2260 'princ
2261 (tumme-list-tags file)
2262 ", "))
2263 (comment (tumme-get-comment file)))
2264 (if file-name
2265 (message
2266 (tumme-format-properties-string
2267 dired-buf
2268 file-name
2269 props
2270 comment)))))
2271
2272 (defvar tumme-tag-file-list nil
2273 "List to store tag-file structure.")
2274
2275 (defvar tumme-file-tag-list nil
2276 "List to store file-tag structure.")
2277
2278 (defvar tumme-file-comment-list nil
2279 "List to store file comments.")
2280
2281 (defun tumme-add-to-tag-file-list (tag file)
2282 "Add relation between TAG and FILE."
2283 (let (curr)
2284 (if tumme-tag-file-list
2285 (if (setq curr (assoc tag tumme-tag-file-list))
2286 (if (not (member file curr))
2287 (setcdr curr (cons file (cdr curr))))
2288 (setcdr tumme-tag-file-list
2289 (cons (list tag file) (cdr tumme-tag-file-list))))
2290 (setq tumme-tag-file-list (list (list tag file))))))
2291
2292 (defun tumme-add-to-tag-file-lists (tag file)
2293 "Helper function used from `tumme-create-gallery-lists'.
2294
2295 Add TAG to FILE in one list and FILE to TAG in the other.
2296
2297 Lisp structures look like the following:
2298
2299 tumme-file-tag-list:
2300
2301 ((\"filename1\" \"tag1\" \"tag2\" \"tag3\" ...)
2302 (\"filename2\" \"tag1\" \"tag2\" \"tag3\" ...)
2303 ...)
2304
2305 tumme-tag-file-list:
2306
2307 ((\"tag1\" \"filename1\" \"filename2\" \"filename3\" ...)
2308 (\"tag2\" \"filename1\" \"filename2\" \"filename3\" ...)
2309 ...)"
2310 ;; Add tag to file list
2311 (let (curr)
2312 (if tumme-file-tag-list
2313 (if (setq curr (assoc file tumme-file-tag-list))
2314 (setcdr curr (cons tag (cdr curr)))
2315 (setcdr tumme-file-tag-list
2316 (cons (list file tag) (cdr tumme-file-tag-list))))
2317 (setq tumme-file-tag-list (list (list file tag))))
2318 ;; Add file to tag list
2319 (if tumme-tag-file-list
2320 (if (setq curr (assoc tag tumme-tag-file-list))
2321 (if (not (member file curr))
2322 (setcdr curr (cons file (cdr curr))))
2323 (setcdr tumme-tag-file-list
2324 (cons (list tag file) (cdr tumme-tag-file-list))))
2325 (setq tumme-tag-file-list (list (list tag file))))))
2326
2327 (defun tumme-add-to-file-comment-list (file comment)
2328 "Helper function used from `tumme-create-gallery-lists'.
2329
2330 For FILE, add COMMENT to list.
2331
2332 Lisp structure looks like the following:
2333
2334 tumme-file-comment-list:
2335
2336 ((\"filename1\" . \"comment1\")
2337 (\"filename2\" . \"comment2\")
2338 ...)"
2339 (if tumme-file-comment-list
2340 (if (not (assoc file tumme-file-comment-list))
2341 (setcdr tumme-file-comment-list
2342 (cons (cons file comment)
2343 (cdr tumme-file-comment-list))))
2344 (setq tumme-file-comment-list (list (cons file comment)))))
2345
2346 (defun tumme-create-gallery-lists ()
2347 "Create temporary lists used by `tumme-gallery-generate'."
2348 (let ((buf (find-file tumme-db-file))
2349 end beg file row-tags)
2350 (setq tumme-tag-file-list nil)
2351 (setq tumme-file-tag-list nil)
2352 (setq tumme-file-comment-list nil)
2353 (goto-char (point-min))
2354 (while (search-forward-regexp "^." nil t)
2355 (end-of-line)
2356 (setq end (point))
2357 (beginning-of-line)
2358 (setq beg (point))
2359 (if (not (search-forward ";" end nil))
2360 (error "Something is really wrong, check format of database"))
2361 (setq row-tags (split-string
2362 (buffer-substring beg end) ";"))
2363 (setq file (car row-tags))
2364 (mapc
2365 (lambda (x)
2366 (if (not (string-match "^comment:\\(.*\\)" x))
2367 (tumme-add-to-tag-file-lists x file)
2368 (tumme-add-to-file-comment-list file (match-string 1 x))))
2369 (cdr row-tags)))
2370 (kill-buffer buf))
2371 ;; Sort tag-file list
2372 (setq tumme-tag-file-list
2373 (sort tumme-tag-file-list
2374 (lambda (x y)
2375 (string< (car x) (car y))))))
2376
2377 (defun tumme-hidden-p (file)
2378 "Return t if image FILE has a \"hidden\" tag."
2379 (let (hidden)
2380 (mapc
2381 (lambda (tag)
2382 (if (member tag tumme-gallery-hidden-tags)
2383 (setq hidden t)))
2384 (cdr (assoc file tumme-file-tag-list)))
2385 hidden))
2386
2387 (defun tumme-gallery-generate ()
2388 "Generate gallery pages.
2389 First we create a couple of Lisp structures from the database to make
2390 it easier to generate, then HTML-files are created in
2391 `tumme-gallery-dir'"
2392 (interactive)
2393 (if (eq 'per-directory tumme-thumbnail-storage)
2394 (error "Currently, gallery generation is not supported \
2395 when using per-directory thumbnail file storage"))
2396 (tumme-create-gallery-lists)
2397 (let ((tags tumme-tag-file-list)
2398 count curr tag index-buf tag-buf
2399 comment file-tags tag-link tag-link-list)
2400 ;; Make sure gallery root exist
2401 (if (file-exists-p tumme-gallery-dir)
2402 (if (not (file-directory-p tumme-gallery-dir))
2403 (error "Variable tumme-gallery-dir is not a directory"))
2404 (make-directory tumme-gallery-dir))
2405 ;; Open index file
2406 (setq index-buf (find-file
2407 (format "%s/index.html" tumme-gallery-dir)))
2408 (erase-buffer)
2409 (insert "<html>\n")
2410 (insert " <body>\n")
2411 (insert " <h2>Tumme Gallery</h2>\n")
2412 (insert (format "<p>\n Gallery generated %s\n <p>\n"
2413 (current-time-string)))
2414 (insert " <h3>Tag index</h3>\n")
2415 (setq count 1)
2416 ;; Pre-generate list of all tag links
2417 (mapc
2418 (lambda (curr)
2419 (setq tag (car curr))
2420 (when (not (member tag tumme-gallery-hidden-tags))
2421 (setq tag-link (format "<a href=\"%d.html\">%s</a>" count tag))
2422 (if tag-link-list
2423 (setq tag-link-list
2424 (append tag-link-list (list (cons tag tag-link))))
2425 (setq tag-link-list (list (cons tag tag-link))))
2426 (setq count (1+ count))))
2427 tags)
2428 (setq count 1)
2429 ;; Main loop where we generated thumbnail pages per tag
2430 (mapc
2431 (lambda (curr)
2432 (setq tag (car curr))
2433 ;; Don't display hidden tags
2434 (when (not (member tag tumme-gallery-hidden-tags))
2435 ;; Insert link to tag page in index
2436 (insert (format " %s<br>\n" (cdr (assoc tag tag-link-list))))
2437 ;; Open per-tag file
2438 (setq tag-buf (find-file
2439 (format "%s/%s.html" tumme-gallery-dir count)))
2440 (erase-buffer)
2441 (insert "<html>\n")
2442 (insert " <body>\n")
2443 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2444 (insert (format " <h2>Images with tag &quot;%s&quot;</h2>" tag))
2445 ;; Main loop for files per tag page
2446 (mapc
2447 (lambda (file)
2448 (when (not (tumme-hidden-p file))
2449 ;; Insert thumbnail with link to full image
2450 (insert
2451 (format "<a href=\"%s/%s\"><img src=\"%s/%s\"%s></a>\n"
2452 tumme-gallery-image-root-url
2453 (file-name-nondirectory file)
2454 tumme-gallery-thumb-image-root-url
2455 (file-name-nondirectory (tumme-thumb-name file)) file))
2456 ;; Insert comment, if any
2457 (if (setq comment (cdr (assoc file tumme-file-comment-list)))
2458 (insert (format "<br>\n%s<br>\n" comment))
2459 (insert "<br>\n"))
2460 ;; Insert links to other tags, if any
2461 (when (> (length
2462 (setq file-tags (assoc file tumme-file-tag-list))) 2)
2463 (insert "[ ")
2464 (mapc
2465 (lambda (extra-tag)
2466 ;; Only insert if not file name or the main tag
2467 (if (and (not (equal extra-tag tag))
2468 (not (equal extra-tag file)))
2469 (insert
2470 (format "%s " (cdr (assoc extra-tag tag-link-list))))))
2471 file-tags)
2472 (insert "]<br>\n"))))
2473 (cdr curr))
2474 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2475 (insert " </body>\n")
2476 (insert "</html>\n")
2477 (save-buffer)
2478 (kill-buffer tag-buf)
2479 (setq count (1+ count))))
2480 tags)
2481 (insert " </body>\n")
2482 (insert "</html>")
2483 (save-buffer)
2484 (kill-buffer index-buf)))
2485
2486 (defun tumme-kill-buffer-and-window ()
2487 "Kill the current buffer and, if possible, also the window."
2488 (interactive)
2489 (let ((buffer (current-buffer)))
2490 (condition-case nil
2491 (delete-window (selected-window))
2492 (error nil))
2493 (kill-buffer buffer)))
2494
2495 (defvar tumme-widget-list nil
2496 "List to keep track of meta data in edit buffer")
2497
2498 ;;;###autoload
2499 (defun tumme-dired-edit-comment-and-tags ()
2500 "Edit comment and tags of current or marked image files.
2501 Edit comment and tags for all marked image files in an
2502 easy-to-use form."
2503 (interactive)
2504 (setq tumme-widget-list nil)
2505 ;; Setup buffer.
2506 (let ((files (dired-get-marked-files)))
2507 (switch-to-buffer "*Tumme Edit Meta Data*")
2508 (kill-all-local-variables)
2509 (make-local-variable 'widget-example-repeat)
2510 (let ((inhibit-read-only t))
2511 (erase-buffer))
2512 (remove-overlays)
2513 ;; Some help for the user.
2514 (widget-insert
2515 "\nEdit comments and tags for each image. Separate multiple tags
2516 with a comma. Move forward between fields using TAB or RET.
2517 Move to the previous field using backtab (S-TAB). Save by
2518 activating the Save button at the bottom of the form or cancel
2519 the operation by activating the Cancel button.\n\n")
2520 ;; Here comes all images and a comment and tag field for each
2521 ;; image.
2522 (let (thumb-file img comment-widget tag-widget)
2523
2524 (dolist (file files)
2525
2526 (setq thumb-file (tumme-thumb-name file)
2527 img (create-image thumb-file))
2528
2529 (insert-image img)
2530 (widget-insert "\n\nComment: ")
2531 (setq comment-widget
2532 (widget-create 'editable-field
2533 :size 60
2534 :format "%v "
2535 :value (or (tumme-get-comment file) "")))
2536 (widget-insert "\nTags: ")
2537 (setq tag-widget
2538 (widget-create 'editable-field
2539 :size 60
2540 :format "%v "
2541 :value (or (mapconcat
2542 (lambda (tag)
2543 tag)
2544 (tumme-list-tags file)
2545 ",") "")))
2546 ;; Save information in all widgets so that we can use it when
2547 ;; the user saves the form.
2548 (setq tumme-widget-list
2549 (append tumme-widget-list
2550 (list (list file comment-widget tag-widget))))
2551 (widget-insert "\n\n")))
2552
2553 ;; Footer with Save and Cancel button.
2554 (widget-insert "\n")
2555 (widget-create 'push-button
2556 :notify
2557 (lambda (&rest ignore)
2558 (tumme-save-information-from-widgets)
2559 (bury-buffer)
2560 (message "Done."))
2561 "Save")
2562 (widget-insert " ")
2563 (widget-create 'push-button
2564 :notify
2565 (lambda (&rest ignore)
2566 (bury-buffer)
2567 (message "Operation canceled."))
2568 "Cancel")
2569 (widget-insert "\n")
2570 (use-local-map widget-keymap)
2571 (widget-setup)
2572 ;; Jump to the first widget.
2573 (widget-forward 1)))
2574
2575 (defun tumme-save-information-from-widgets ()
2576 "Save information found in `tumme-widget-list'.
2577 Use the information in `tumme-widget-list' to save comments and
2578 tags to their respective image file. Internal function used by
2579 `tumme-dired-edit-comment-and-tags'."
2580 (let (file comment tag-string tag-list lst)
2581 (tumme-write-comments
2582 (mapcar
2583 (lambda (widget)
2584 (setq file (car widget)
2585 comment (widget-value (cadr widget)))
2586 (cons file comment))
2587 tumme-widget-list))
2588 (tumme-write-tags
2589 (dolist (widget tumme-widget-list lst)
2590 (setq file (car widget)
2591 tag-string (widget-value (car (cddr widget)))
2592 tag-list (split-string tag-string ","))
2593 (dolist (tag tag-list)
2594 (push (cons file tag) lst))))))
2595
2596 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2597 ;;;;;;;;; TEST-SECTION ;;;;;;;;;;;
2598 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2599
2600 ;; (defvar tumme-dir-max-size 12300000)
2601
2602 ;; (defun tumme-test-clean-old-files ()
2603 ;; "Clean `tumme-dir' from old thumbnail files.
2604 ;; \"Oldness\" measured using last access time. If the total size of all
2605 ;; thumbnail files in `tumme-dir' is larger than 'tumme-dir-max-size',
2606 ;; old files are deleted until the max size is reached."
2607 ;; (let* ((files
2608 ;; (sort
2609 ;; (mapcar
2610 ;; (lambda (f)
2611 ;; (let ((fattribs (file-attributes f)))
2612 ;; ;; Get last access time and file size
2613 ;; `(,(nth 4 fattribs) ,(nth 7 fattribs) ,f)))
2614 ;; (directory-files (tumme-dir) t ".+\.thumb\..+$"))
2615 ;; ;; Sort function. Compare time between two files.
2616 ;; '(lambda (l1 l2)
2617 ;; (time-less-p (car l1) (car l2)))))
2618 ;; (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) files))))
2619 ;; (while (> dirsize tumme-dir-max-size)
2620 ;; (y-or-n-p
2621 ;; (format "Size of thumbnail directory: %d, delete old file %s? "
2622 ;; dirsize (cadr (cdar files))))
2623 ;; (delete-file (cadr (cdar files)))
2624 ;; (setq dirsize (- dirsize (car (cdar files))))
2625 ;; (setq files (cdr files)))))
2626
2627 ;;;;;;;;;;;;;;;;;;;;;;,
2628
2629 ;; (defun dired-speedbar-buttons (dired-buffer)
2630 ;; (when (and (boundp 'tumme-use-speedbar)
2631 ;; tumme-use-speedbar)
2632 ;; (let ((filename (with-current-buffer dired-buffer
2633 ;; (dired-get-filename))))
2634 ;; (when (and (not (string-equal filename (buffer-string)))
2635 ;; (string-match (image-file-name-regexp) filename))
2636 ;; (erase-buffer)
2637 ;; (insert (propertize
2638 ;; filename
2639 ;; 'display
2640 ;; (tumme-get-thumbnail-image filename)))))))
2641
2642 ;; (setq tumme-use-speedbar t)
2643
2644 (provide 'tumme)
2645
2646 ;; arch-tag: 9d11411d-331f-4380-8b44-8adfe3a0343e
2647 ;;; tumme.el ends here