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