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