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