1 ;;; tumme.el --- use dired to browse and manipulate your images
3 ;; Copyright (C) 2005, 2006 Free Software Foundation, Inc.
6 ;; Keywords: multimedia
7 ;; Author: Mathias Dahl <mathias.rem0veth1s.dahl@gmail.com>
9 ;; This file is part of GNU Emacs.
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)
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.
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.
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.
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.
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... :)
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
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:
63 ;; file-name-non-directory;comment:comment-text;tag1;tag2;tag3;...;tagN
69 ;; * The ImageMagick package. Currently, `convert' and `mogrify' are
70 ;; used. Find it here: http://www.imagemagick.org.
72 ;; * For non-lossy rotation of JPEG images, the JpegTRAN program is
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').
87 ;; If you plan to use tumme much, setting up key bindings for it in
88 ;; dired is a good idea:
90 ;; (tumme-setup-dired-keybindings)
92 ;; Next, do M-x tumme-dired RET. This will ask you for a directory
93 ;; where image files are stored, setup a useful window configuration
94 ;; and enable the two special modes that tumme provides. NOTE: If you
95 ;; do not want tumme to split your windows, call it with a prefix
98 ;; Start viewing thumbnails by doing C-S-n and C-S-p to go up and down
99 ;; in the dired buffer while at the same time displaying a thumbnail
100 ;; image. The thumbnail images will be created on the fly, and
101 ;; cached. This means that the first time you browse your images, it
102 ;; will be a bit slow because the thumbnails are created. If you want
103 ;; to avoid this, you can pre-create the thumbnail images by marking
104 ;; all images in dired (% m \.jpg$ RET) and then do M-x
105 ;; tumme-create-thumbs.
107 ;; Next, try `tumme-display-thumbs' (C-t d). If no file is marked, a
108 ;; thumbnail for the file at point will show up in
109 ;; `tumme-thumbnail-buffer'. If one or more files are marked,
110 ;; thumbnails for those files will be displayed.
112 ;; Pressing TAB will switch to the window containing the
113 ;; `tumme-thumbnail-buffer' buffer. In there you can move between
114 ;; thumbnail images and display a semi-sized version in an Emacs
115 ;; buffer (RET), or the original image in an external viewer
116 ;; (C-RET). By pressing SPC or DEL you will navigate back and fort
117 ;; while at the same time displaying each image in Emacs. You can also
118 ;; navigate using arrow keys. Comment a file by pressing "c". Press
119 ;; TAB to get back to dired.
121 ;; While in dired mode, you can tag and comment files, you can tell
122 ;; `tumme' to mark files with a certain tag (using a regexp) etc.
124 ;; The easiest way to see the available commands is to use the Tumme
125 ;; menus added in tumme-thumbnail-mode and dired-mode.
131 ;; * Supports all image formats that Emacs and convert supports, but
132 ;; the thumbnails are hard-coded to JPEG format.
134 ;; * WARNING: The "database" format used might be changed so keep a
135 ;; backup of `tumme-db-file' when testing new versions.
141 ;; * Support gallery creation when using per-directory thumbnail
144 ;; * Some sort of auto-rotate function based on rotate info in the
147 ;; * Check if exiftool exist before trying to call it to give a better
150 ;; * Investigate if it is possible to also write the tags to the image
153 ;; * From thumbs.el: Add an option for clean-up/max-size functionality
154 ;; for thumbnail directory.
156 ;; * From thumbs.el: Add setroot function.
158 ;; * From thumbs.el: Add image resizing, if useful (tumme's automatic
159 ;; "image fit" might be enough)
161 ;; * From thumbs.el: Add the "modify" commands (emboss, negate,
164 ;; * Asynchronous creation of thumbnails.
166 ;; * Add `tumme-display-thumbs-ring' and functions to cycle that. Find
167 ;; out which is best, saving old batch just before inserting new, or
168 ;; saving the current batch in the ring when inserting it. Adding it
169 ;; probably needs rewriting `tumme-display-thumbs' to be more general.
171 ;; * Find some way of toggling on and off really nice keybindings in
172 ;; dired (for example, using C-n or <down> instead of C-S-n). Richard
173 ;; suggested that we could keep C-t as prefix for tumme commands as it
174 ;; is currently not used in dired. He also suggested that
175 ;; `dired-next-line' and `dired-previous-line' figure out if tumme is
176 ;; enabled in the current buffer and, if it is, call
177 ;; `tumme-dired-next-line' and `tumme-dired-previous-line',
178 ;; respectively. Update: This is partly done; some bindings have now
179 ;; been added to dired.
181 ;; * Enhanced gallery creation with basic CSS-support and pagination
182 ;; of tag pages with many pictures.
184 ;; * Rewrite `tumme-modify-mark-on-thumb-original-file' to be less
187 ;; * In some way keep track of buffers and windows and stuff so that
188 ;; it works as the user expects.
190 ;; * More/better documentation
196 (require 'format-spec)
199 "Use dired to browse your images as thumbnails, and more."
203 (defcustom tumme-dir "~/.emacs.d/tumme/"
204 "*Directory where thumbnail images are stored."
208 (defcustom tumme-thumbnail-storage 'use-tumme-dir
209 "*How to store tumme's thumbnail files.
210 Tumme can store thumbnail files in one of two ways and this is
211 controlled by this variable. \"Use tumme dir\" means that the
212 thumbnails are stored in a central directory. \"Per directory\"
213 means that each thumbnail is stored in a subdirectory called
214 \".tumme\" in the same directory where the image file is.
215 \"Thumbnail Managing Standard\" means that the thumbnails are
216 stored and generated according to the Thumbnail Managing Standard
217 that allows sharing of thumbnails across different programs."
218 :type '(choice :tag "How to store thumbnail files"
219 (const :tag "Thumbnail Managing Standard" standard)
220 (const :tag "Use tumme-dir" use-tumme-dir)
221 (const :tag "Per-directory" per-directory))
224 (defcustom tumme-db-file "~/.emacs.d/tumme/.tumme_db"
225 "*Database file where file names and their associated tags are stored."
229 (defcustom tumme-temp-image-file "~/.emacs.d/tumme/.tumme_temp"
230 "*Name of temporary image file used by various commands."
234 (defcustom tumme-gallery-dir "~/.emacs.d/tumme/.tumme_gallery"
235 "*Directory to store generated gallery html pages.
236 This path needs to be \"shared\" to the public so that it can access
237 the index.html page that tumme creates."
241 (defcustom tumme-gallery-image-root-url
242 "http://your.own.server/tummepics"
243 "*URL where the full size images are to be found.
244 Note that this path has to be configured in your web server. Tumme
245 expects to find pictures in this directory."
249 (defcustom tumme-gallery-thumb-image-root-url
250 "http://your.own.server/tummethumbs"
251 "*URL where the thumbnail images are to be found.
252 Note that this path has to be configured in your web server. Tumme
253 expects to find pictures in this directory."
257 (defcustom tumme-cmd-create-thumbnail-program
259 "*Executable used to create thumbnail.
260 Used together with `tumme-cmd-create-thumbnail-options'."
264 (defcustom tumme-cmd-create-thumbnail-options
265 "%p -size %wx%h \"%f\" -resize %wx%h +profile \"*\" jpeg:\"%t\""
266 "*Format of command used to create thumbnail image.
267 Available options are %p which is replaced by
268 `tumme-cmd-create-thumbnail-program', %w which is replaced by
269 `tumme-thumb-width', %h which is replaced by `tumme-thumb-height',
270 %f which is replaced by the file name of the original image and %t
271 which is replaced by the file name of the thumbnail file."
275 (defcustom tumme-cmd-create-temp-image-program
277 "*Executable used to create temporary image.
278 Used together with `tumme-cmd-create-temp-image-options'."
282 (defcustom tumme-cmd-create-temp-image-options
283 "%p -size %wx%h \"%f\" -resize %wx%h +profile \"*\" jpeg:\"%t\""
284 "*Format of command used to create temporary image for display window.
285 Available options are %p which is replaced by
286 `tumme-cmd-create-temp-image-program', %w and %h which is replaced by
287 the calculated max size for width and height in the image display window,
288 %f which is replaced by the file name of the original image and %t which
289 is replaced by the file name of the temporary file."
293 (defcustom tumme-cmd-pngnq-program (executable-find "pngnq")
294 "*The file name of the `pngnq' program.
295 It quantizes colors of PNG images down to 256 colors."
296 :type '(choice (const :tag "Not Set" nil) string)
299 (defcustom tumme-cmd-pngcrush-program (executable-find "pngcrush")
300 "*The file name of the `pngcrush' program.
301 It optimizes the compression of PNG images. Also it adds PNG textual chunks
302 with the information required by the Thumbnail Managing Standard."
303 :type '(choice (const :tag "Not Set" nil) string)
306 (defcustom tumme-cmd-create-standard-thumbnail-command
308 tumme-cmd-create-thumbnail-program " "
309 "-size %wx%h \"%f\" "
310 (unless (or tumme-cmd-pngcrush-program tumme-cmd-pngnq-program)
312 "-set \"Thumb::MTime\" \"%m\" "
313 "-set \"Thumb::URI\" \"file://%f\" "
314 "-set \"Description\" \"Thumbnail of file://%f\" "
315 "-set \"Software\" \"" (emacs-version) "\" "))
316 "-thumbnail %wx%h png:\"%t\""
317 (if tumme-cmd-pngnq-program
319 " ; " tumme-cmd-pngnq-program " -f \"%t\""
320 (unless tumme-cmd-pngcrush-program
322 (if tumme-cmd-pngcrush-program
324 (unless tumme-cmd-pngcrush-program
326 " ; " tumme-cmd-pngcrush-program " -q "
327 "-text b \"Description\" \"Thumbnail of file://%f\" "
328 "-text b \"Software\" \"" (emacs-version) "\" "
329 ;; "-text b \"Thumb::Image::Height\" \"%oh\" "
330 ;; "-text b \"Thumb::Image::Mimetype\" \"%mime\" "
331 ;; "-text b \"Thumb::Image::Width\" \"%ow\" "
332 "-text b \"Thumb::MTime\" \"%m\" "
333 ;; "-text b \"Thumb::Size\" \"%b\" "
334 "-text b \"Thumb::URI\" \"file://%f\" "
337 "*Command to create thumbnails according to the Thumbnail Managing Standard."
341 (defcustom tumme-cmd-rotate-thumbnail-program
343 "*Executable used to rotate thumbnail.
344 Used together with `tumme-cmd-rotate-thumbnail-options'."
348 (defcustom tumme-cmd-rotate-thumbnail-options
349 "%p -rotate %d \"%t\""
350 "*Format of command used to rotate thumbnail image.
351 Available options are %p which is replaced by
352 `tumme-cmd-rotate-thumbnail-program', %d which is replaced by the
353 number of (positive) degrees to rotate the image, normally 90 or 270
354 \(for 90 degrees right and left), %t which is replaced by the file name
355 of the thumbnail file."
359 (defcustom tumme-cmd-rotate-original-program
361 "*Executable used to rotate original image.
362 Used together with `tumme-cmd-rotate-original-options'."
366 (defcustom tumme-cmd-rotate-original-options
367 "%p -rotate %d -copy all \"%o\" > %t"
368 "*Format of command used to rotate original image.
369 Available options are %p which is replaced by
370 `tumme-cmd-rotate-original-program', %d which is replaced by the
371 number of (positive) degrees to rotate the image, normally 90 or
372 270 \(for 90 degrees right and left), %o which is replaced by the
373 original image file name and %t which is replaced by
374 `tumme-temp-image-file'."
378 (defcustom tumme-temp-rotate-image-file
379 "~/.emacs.d/tumme/.tumme_rotate_temp"
380 "*Temporary file for rotate operations."
384 (defcustom tumme-rotate-original-ask-before-overwrite t
385 "Confirm overwrite of original file after rotate operation.
386 If non-nil, ask user for confirmation before overwriting the
387 original file with `tumme-temp-rotate-image-file'."
391 (defcustom tumme-cmd-write-exif-data-program
393 "*Program used to write EXIF data to image.
394 Used together with `tumme-cmd-write-exif-data-options'."
398 (defcustom tumme-cmd-write-exif-data-options
399 "%p -%t=\"%v\" \"%f\""
400 "*Format of command used to write EXIF data.
401 Available options are %p which is replaced by
402 `tumme-cmd-write-exif-data-program', %f which is replaced by the
403 image file name, %t which is replaced by the tag name and %v
404 which is replaced by the tag value."
408 (defcustom tumme-cmd-read-exif-data-program
410 "*Program used to read EXIF data to image.
411 Used together with `tumme-cmd-read-exif-data-program-options'."
415 (defcustom tumme-cmd-read-exif-data-options
416 "%p -s -s -s -%t \"%f\""
417 "*Format of command used to read EXIF data.
418 Available options are %p which is replaced by
419 `tumme-cmd-write-exif-data-options', %f which is replaced
420 by the image file name and %t which is replaced by the tag name."
424 (defcustom tumme-gallery-hidden-tags
425 (list "private" "hidden" "pending")
426 "*List of \"hidden\" tags.
427 Used by `tumme-gallery-generate' to leave out \"hidden\" images."
428 :type '(repeat string)
431 (defcustom tumme-thumb-size (if (eq 'standard tumme-thumbnail-storage) 128 100)
432 "Size of thumbnails, in pixels.
433 This is the default size for both `tumme-thumb-width' and `tumme-thumb-height'."
437 (defcustom tumme-thumb-width tumme-thumb-size
438 "Width of thumbnails, in pixels."
442 (defcustom tumme-thumb-height tumme-thumb-size
443 "Height of thumbnails, in pixels."
447 (defcustom tumme-thumb-relief 2
448 "*Size of button-like border around thumbnails."
452 (defcustom tumme-thumb-margin 2
453 "*Size of the margin around thumbnails.
454 This is where you see the cursor."
458 (defcustom tumme-line-up-method 'dynamic
459 "*Default method for line-up of thumbnails in thumbnail buffer.
460 Used by `tumme-display-thumbs' and other functions that needs to
461 line-up thumbnails. Dynamic means to use the available width of the
462 window containing the thumbnail buffer, Fixed means to use
463 `tumme-thumbs-per-row', Interactive is for asking the user, and No
464 line-up means that no automatic line-up will be done."
465 :type '(choice :tag "Default line-up method"
466 (const :tag "Dynamic" dynamic)
467 (const :tag "Fixed" fixed)
468 (const :tag "Interactive" interactive)
469 (const :tag "No line-up" none))
472 (defcustom tumme-thumbs-per-row 3
473 "*Number of thumbnails to display per row in thumb buffer."
477 (defcustom tumme-display-window-width-correction 1
478 "*Number to be used to correct image display window width.
479 Change if the default (1) does not work (i.e. if the image does not
484 (defcustom tumme-display-window-height-correction 0
485 "*Number to be used to correct image display window height.
486 Change if the default (0) does not work (i.e. if the image does not
491 (defcustom tumme-track-movement nil
492 "The current state of the tracking and mirroring.
493 For more information, see the documentation for
494 `tumme-toggle-movement-tracking'."
498 (defcustom tumme-append-when-browsing nil
499 "Append thumbnails in thumbnail buffer when browsing.
500 If non-nil, using `tumme-next-line-and-display' and
501 `tumme-previous-line-and-display' will leave a trail of thumbnail
502 images in the thumbnail buffer. If you enable this and want to clean
503 the thumbnail buffer because it is filled with too many thumbmnails,
504 just call `tumme-display-thumb' to display only the image at point.
505 This value can be toggled using `tumme-toggle-append-browsing'."
509 (defcustom tumme-dired-disp-props t
510 "If non-nil, display properties for dired file when browsing.
511 Used by `tumme-next-line-and-display',
512 `tumme-previous-line-and-display' and `tumme-mark-and-display-next'.
513 If the database file is large, this can slow down image browsing in
514 dired and you might want to turn it off."
518 (defcustom tumme-display-properties-format "%b: %f (%t): %c"
519 "*Display format for thumbnail properties.
520 %b is replaced with associated dired buffer name, %f with file name
521 \(without path) of original image file, %t with the list of tags and %c
526 (defcustom tumme-external-viewer
527 ;; TODO: use mailcap, dired-guess-shell-alist-default, dired-view-command-alist
528 (cond ((executable-find "display"))
529 ((executable-find "xli"))
530 ((executable-find "qiv") "qiv -t"))
531 "*Name of external viewer.
532 Including parameters. Used when displaying original image from
533 `tumme-thumbnail-mode'."
537 (defcustom tumme-main-image-directory "~/pics/"
538 "*Name of main image directory, if any.
539 Used by `tumme-copy-with-exif-file-name'."
543 (defcustom tumme-show-all-from-dir-max-files 50
544 "*Maximum number of files to show using`tumme-show-all-from-dir'.
545 before warning the user."
550 "Return the current thumbnails directory (from variable `tumme-dir').
551 Create the thumbnails directory if it does not exist."
552 (let ((tumme-dir (file-name-as-directory
553 (expand-file-name tumme-dir))))
554 (unless (file-directory-p tumme-dir)
555 (make-directory tumme-dir t)
556 (message "Creating thumbnails directory"))
559 (defun tumme-insert-image (file type relief margin)
560 "Insert image FILE of image TYPE, using RELIEF and MARGIN, at point."
562 (let ((i `(image :type ,type
568 (defun tumme-get-thumbnail-image (file)
569 "Return the image descriptor for a thumbnail of image file FILE."
570 (unless (string-match (image-file-name-regexp) file)
571 (error "%s is not a valid image file" file))
572 (let ((thumb-file (tumme-thumb-name file)))
573 (unless (and (file-exists-p thumb-file)
574 (<= (float-time (nth 5 (file-attributes file)))
575 (float-time (nth 5 (file-attributes thumb-file)))))
576 (tumme-create-thumb file thumb-file))
577 (create-image thumb-file)
578 ;; (list 'image :type 'jpeg
580 ;; :relief tumme-thumb-relief :margin tumme-thumb-margin)
583 (defun tumme-insert-thumbnail (file original-file-name
584 associated-dired-buffer)
585 "Insert thumbnail image FILE.
586 Add text properties ORIGINAL-FILE-NAME and ASSOCIATED-DIRED-BUFFER."
589 (tumme-insert-image file
590 ;; TODO: this should depend on the real file type
591 (if (eq 'standard tumme-thumbnail-storage)
598 (list 'tumme-thumbnail t
599 'original-file-name original-file-name
600 'associated-dired-buffer associated-dired-buffer
601 'tags (tumme-list-tags original-file-name)
602 'mouse-face 'highlight
603 'comment (tumme-get-comment original-file-name)))))
605 (defun tumme-thumb-name (file)
606 "Return thumbnail file name for FILE.
607 Depending on the value of `tumme-thumbnail-storage', the file
608 name will vary. For central thumbnail file storage, make a
609 MD5-hash of the image file's directory name and add that to make
610 the thumbnail file name unique. For per-directory storage, just
611 add a subdirectory. For standard storage, produce the file name
612 according to the Thumbnail Managing Standard."
613 (cond ((eq 'standard tumme-thumbnail-storage)
615 (concat "~/.thumbnails/normal/"
616 (md5 (concat "file://" (expand-file-name file))) ".png")))
617 ((eq 'use-tumme-dir tumme-thumbnail-storage)
618 (let* ((f (expand-file-name file))
620 ;; Is MD5 hashes fast enough? The checksum of a
621 ;; thumbnail file name need not be that
622 ;; "cryptographically" good so a faster one could
624 (md5 (file-name-as-directory (file-name-directory f)))))
625 (format "%s%s%s.thumb.%s"
626 (file-name-as-directory (expand-file-name (tumme-dir)))
627 (file-name-sans-extension (file-name-nondirectory f))
628 (if md5-hash (concat "_" md5-hash) "")
629 (file-name-extension f))))
630 ((eq 'per-directory tumme-thumbnail-storage)
631 (let ((f (expand-file-name file)))
632 (format "%s.tumme/%s.thumb.%s"
633 (file-name-directory f)
634 (file-name-sans-extension (file-name-nondirectory f))
635 (file-name-extension f))))))
637 (defun tumme-create-thumb (original-file thumbnail-file)
638 "For ORIGINAL-FILE, create thumbnail image named THUMBNAIL-FILE."
639 (let* ((width (int-to-string tumme-thumb-width))
640 (height (int-to-string tumme-thumb-height))
641 (modif-time (format "%.0f" (float-time (nth 5 (file-attributes
643 (thumbnail-nq8-file (replace-regexp-in-string ".png\\'" "-nq8.png"
647 (if (eq 'standard tumme-thumbnail-storage)
648 tumme-cmd-create-standard-thumbnail-command
649 tumme-cmd-create-thumbnail-options)
651 (cons ?p tumme-cmd-create-thumbnail-program)
655 (cons ?f original-file)
656 (cons ?q thumbnail-nq8-file)
657 (cons ?t thumbnail-file))))
659 (when (not (file-exists-p
660 (setq thumbnail-dir (file-name-directory thumbnail-file))))
661 (message "Creating thumbnail directory.")
662 (make-directory thumbnail-dir))
663 (shell-command command nil)))
666 (defun tumme-dired-insert-marked-thumbs ()
667 "Insert thumbnails before file names of marked files in the dired buffer."
669 (dired-map-over-marks
670 (let* ((image-pos (dired-move-to-filename))
671 (image-file (dired-get-filename))
672 (thumb-file (tumme-get-thumbnail-image image-file))
674 ;; If image is not already added, then add it.
675 (unless (delq nil (mapcar (lambda (o) (overlay-get o 'put-image))
676 ;; Can't use (overlays-at (point)), BUG?
677 (overlays-in (point) (1+ (point)))))
678 (put-image thumb-file image-pos)
679 (setq overlay (car (delq nil (mapcar (lambda (o) (and (overlay-get o 'put-image) o))
680 (overlays-in (point) (1+ (point)))))))
681 (overlay-put overlay 'image-file image-file)
682 (overlay-put overlay 'thumb-file thumb-file)))
684 (add-hook 'dired-after-readin-hook 'tumme-dired-after-readin-hook nil t))
686 (defun tumme-dired-after-readin-hook ()
687 "Relocate existing thumbnail overlays in dired buffer after reverting.
688 Move them to their corresponding files if they are still exist.
689 Otherwise, delete overlays."
690 (mapc (lambda (overlay)
691 (when (overlay-get overlay 'put-image)
692 (let* ((image-file (overlay-get overlay 'image-file))
693 (image-pos (dired-goto-file image-file)))
695 (move-overlay overlay image-pos image-pos)
696 (delete-overlay overlay)))))
697 (overlays-in (point-min) (point-max))))
699 (defun tumme-next-line-and-display ()
700 "Move to next dired line and display thumbnail image."
703 (tumme-display-thumbs
704 t (or tumme-append-when-browsing nil) t)
705 (if tumme-dired-disp-props
706 (tumme-dired-display-properties)))
708 (defun tumme-previous-line-and-display ()
709 "Move to previous dired line and display thumbnail image."
711 (dired-previous-line 1)
712 (tumme-display-thumbs
713 t (or tumme-append-when-browsing nil) t)
714 (if tumme-dired-disp-props
715 (tumme-dired-display-properties)))
717 (defun tumme-toggle-append-browsing ()
718 "Toggle `tumme-append-when-browsing'."
720 (setq tumme-append-when-browsing
721 (not tumme-append-when-browsing))
722 (message "Append browsing %s."
723 (if tumme-append-when-browsing
727 (defun tumme-mark-and-display-next ()
728 "Mark current file in dired and display next thumbnail image."
731 (tumme-display-thumbs
732 t (or tumme-append-when-browsing nil) t)
733 (if tumme-dired-disp-props
734 (tumme-dired-display-properties)))
736 (defun tumme-toggle-dired-display-properties ()
737 "Toggle `tumme-dired-disp-props'."
739 (setq tumme-dired-disp-props
740 (not tumme-dired-disp-props))
741 (message "Dired display properties %s."
742 (if tumme-dired-disp-props
746 (defvar tumme-thumbnail-buffer "*tumme*"
747 "Tumme's thumbnail buffer.")
749 (defun tumme-create-thumbnail-buffer ()
750 "Create thumb buffer and set `tumme-thumbnail-mode'."
751 (let ((buf (get-buffer-create tumme-thumbnail-buffer)))
754 (setq buffer-read-only t)
755 (if (not (eq major-mode 'tumme-thumbnail-mode))
756 (tumme-thumbnail-mode)))
759 (defvar tumme-display-image-buffer "*tumme-display-image*"
760 "Where larger versions of the images are display.")
762 (defun tumme-create-display-image-buffer ()
763 "Create image display buffer and set `tumme-display-image-mode'."
764 (let ((buf (get-buffer-create tumme-display-image-buffer)))
767 (setq buffer-read-only t)
768 (if (not (eq major-mode 'tumme-display-image-mode))
769 (tumme-display-image-mode)))
772 (defvar tumme-saved-window-configuration nil
773 "Saved window configuration.")
776 (defun tumme-dired-with-window-configuration (dir &optional arg)
777 "Open directory DIR and create a default window configuration.
779 Convenience command that:
781 - Opens dired in folder DIR
782 - Splits windows in most useful (?) way
783 - Set `truncate-lines' to t
785 After the command has finished, you would typically mark some
786 image files in dired and type
787 \\[tumme-display-thumbs] (`tumme-display-thumbs').
789 If called with prefix argument ARG, skip splitting of windows.
791 The current window configuration is saved and can be restored by
792 calling `tumme-restore-window-configuration'."
793 (interactive "DDirectory: \nP")
794 (let ((buf (tumme-create-thumbnail-buffer))
795 (buf2 (tumme-create-display-image-buffer)))
796 (setq tumme-saved-window-configuration
797 (current-window-configuration))
799 (delete-other-windows)
801 (split-window-horizontally)
802 (setq truncate-lines t)
805 (switch-to-buffer buf)
806 (split-window-vertically)
808 (switch-to-buffer buf2)
809 (other-window -2)))))
811 (defun tumme-restore-window-configuration ()
812 "Restore window configuration.
813 Restore any changes to the window configuration made by calling
814 `tumme-dired-with-window-configuration'."
816 (if tumme-saved-window-configuration
817 (set-window-configuration tumme-saved-window-configuration)
818 (message "No saved window configuration")))
821 (defun tumme-display-thumbs (&optional arg append do-not-pop)
822 "Display thumbnails of all marked files, in `tumme-thumbnail-buffer'.
823 If a thumbnail image does not exist for a file, it is created on the
824 fly. With prefix argument ARG, display only thumbnail for file at
825 point (this is useful if you have marked some files but want to show
828 Recommended usage is to split the current frame horizontally so that
829 you have the dired buffer in the left window and the
830 `tumme-thumbnail-buffer' buffer in the right window.
832 With optional argument APPEND, append thumbnail to thumbnail buffer
833 instead of erasing it first.
835 Option argument DO-NOT-POP controls if `pop-to-buffer' should be
836 used or not. If non-nil, use `display-buffer' instead of
837 `pop-to-buffer'. This is used from functions like
838 `tumme-next-line-and-display' and
839 `tumme-previous-line-and-display' where we do not want the
840 thumbnail buffer to be selected."
842 (let ((buf (tumme-create-thumbnail-buffer))
843 curr-file thumb-name files count dired-buf beg)
845 (setq files (list (dired-get-filename)))
846 (setq files (dired-get-marked-files)))
847 (setq dired-buf (current-buffer))
850 (let ((inhibit-read-only t))
853 (goto-char (point-max)))
856 (setq thumb-name (tumme-thumb-name curr-file))
857 (if (and (not (file-exists-p thumb-name))
858 (not (= 0 (tumme-create-thumb curr-file thumb-name))))
859 (message "Thumb could not be created for file %s" curr-file)
860 (tumme-insert-thumbnail thumb-name curr-file dired-buf)))
862 (cond ((eq 'dynamic tumme-line-up-method)
863 (tumme-line-up-dynamic))
864 ((eq 'fixed tumme-line-up-method)
866 ((eq 'interactive tumme-line-up-method)
867 (tumme-line-up-interactive))
868 ((eq 'none tumme-line-up-method)
871 (tumme-line-up-dynamic))))
873 (display-buffer tumme-thumbnail-buffer)
874 (pop-to-buffer tumme-thumbnail-buffer))))
876 (defun tumme-show-all-from-dir (dir)
877 "Make a preview buffer for all images in DIR and display it.
878 If the number of files in DIR matching `image-file-name-regexp'
879 exceeds `tumme-show-all-from-dir-max-files', a warning will be
881 (interactive "DDir: ")
883 (dired-mark-files-regexp (image-file-name-regexp))
884 (let ((files (dired-get-marked-files)))
885 (if (or (<= (length files) tumme-show-all-from-dir-max-files)
886 (and (> (length files) tumme-show-all-from-dir-max-files)
889 "Directory contains more than %d image files. Proceed? "
890 tumme-show-all-from-dir-max-files))))
892 (tumme-display-thumbs)
893 (pop-to-buffer tumme-thumbnail-buffer))
894 (message "Cancelled."))))
897 (defalias 'tumme 'tumme-show-all-from-dir)
899 (defun tumme-write-tag (files tag)
900 "For all FILES, writes TAG to the image database."
903 (setq buf (find-file tumme-db-file))
904 (if (not (listp files))
906 (setq files (list files))
907 (error "Files must be a string or a list of strings!")))
910 (goto-char (point-min))
911 (if (search-forward-regexp
912 (format "^%s" file) nil t)
917 (if (not (search-forward (format ";%s" tag) end t))
920 (insert (format ";%s" tag)))))
921 (goto-char (point-max))
922 (insert (format "\n%s;%s" file tag))))
927 (defun tumme-remove-tag (files tag)
928 "For all FILES, remove TAG from the image database."
931 (setq buf (find-file tumme-db-file))
932 (if (not (listp files))
934 (setq files (list files))
935 (error "Files must be a string or a list of strings!")))
938 (goto-char (point-min))
939 (if (search-forward-regexp
940 (format "^%s" file) nil t)
945 (if (search-forward-regexp (format "\\(;%s\\)" tag) end t)
947 (delete-region (match-beginning 1) (match-end 1))
948 ;; Check if file should still be in the database. If
949 ;; it has no tags or comments, it will be removed.
953 (if (not (search-forward ";" end t))
956 ;; If on empty line at end of buffer
959 (delete-backward-char 1)))))))))
964 (defun tumme-list-tags (file)
965 "Read all tags for image FILE from the image database."
967 (let (end buf (tags ""))
968 (setq buf (find-file tumme-db-file))
969 (goto-char (point-min))
970 (if (search-forward-regexp
971 (format "^%s" file) nil t)
976 (if (search-forward ";" end t)
977 (if (search-forward "comment:" end t)
978 (if (search-forward ";" end t)
979 (setq tags (buffer-substring (point) end)))
980 (setq tags (buffer-substring (point) end))))))
982 (split-string tags ";"))))
985 (defun tumme-tag-files (arg)
986 "Tag marked file(s) in dired. With prefix ARG, tag file at point."
988 (let ((tag (read-string "Tags to add (separate tags with a semicolon): "))
991 (setq files (dired-get-filename))
992 (setq files (dired-get-marked-files)))
993 (tumme-write-tag files tag)))
995 (defun tumme-tag-thumbnail ()
996 "Tag current thumbnail."
998 (let ((tag (read-string "Tags to add (separate tags with a semicolon): ")))
999 (tumme-write-tag (tumme-original-file-name) tag))
1000 (tumme-update-property
1001 'tags (tumme-list-tags (tumme-original-file-name))))
1004 (defun tumme-tag-remove (arg)
1005 "Remove tag for selected file(s).
1006 With prefix argument ARG, remove tag from file at point."
1008 (let ((tag (read-string "Tag to remove: "))
1011 (setq files (list (dired-get-filename)))
1012 (setq files (dired-get-marked-files)))
1013 (tumme-remove-tag files tag)))
1015 (defun tumme-tag-thumbnail-remove ()
1016 "Remove tag from thumbnail."
1018 (let ((tag (read-string "Tag to remove: ")))
1019 (tumme-remove-tag (tumme-original-file-name) tag))
1020 (tumme-update-property
1021 'tags (tumme-list-tags (tumme-original-file-name))))
1023 (defun tumme-original-file-name ()
1024 "Get original file name for thumbnail or display image at point."
1025 (get-text-property (point) 'original-file-name))
1027 (defun tumme-associated-dired-buffer ()
1028 "Get associated dired buffer at point."
1029 (get-text-property (point) 'associated-dired-buffer))
1031 (defun tumme-get-buffer-window (buf)
1032 "Return window where buffer BUF is."
1033 (get-window-with-predicate
1035 (equal (window-buffer window) buf))
1038 (defun tumme-track-original-file ()
1039 "Track the original file in the associated dired buffer.
1040 See documentation for `tumme-toggle-movement-tracking'. Interactive
1041 use only useful if `tumme-track-movement' is nil."
1043 (let ((old-buf (current-buffer))
1044 (dired-buf (tumme-associated-dired-buffer))
1045 (file-name (tumme-original-file-name)))
1046 (if (and dired-buf file-name)
1048 (setq file-name (file-name-nondirectory file-name))
1049 (set-buffer dired-buf)
1050 (goto-char (point-min))
1051 (if (not (search-forward file-name nil t))
1052 (message "Could not track file")
1053 (dired-move-to-filename)
1055 (tumme-get-buffer-window dired-buf) (point)))
1056 (set-buffer old-buf)))))
1058 (defun tumme-toggle-movement-tracking ()
1059 "Turn on and off `tumme-track-movement'.
1060 Tracking of the movements between thumbnail and dired buffer so that
1061 they are \"mirrored\" in the dired buffer. When this is on, moving
1062 around in the thumbnail or dired buffer will find the matching
1063 position in the other buffer."
1065 (setq tumme-track-movement (not tumme-track-movement))
1066 (message "Tracking %s" (if tumme-track-movement "on" "off")))
1068 (defun tumme-track-thumbnail ()
1069 "Track current dired file's thumb in `tumme-thumbnail-buffer'.
1070 This is almost the same as what `tumme-track-original-file' does, but
1071 the other way around."
1072 (let ((file (dired-get-filename))
1073 (old-buf (current-buffer))
1075 (if (get-buffer tumme-thumbnail-buffer)
1077 (set-buffer tumme-thumbnail-buffer)
1078 (goto-char (point-min))
1079 (while (and (not (eobp))
1081 (if (and (setq prop-val
1082 (get-text-property (point) 'original-file-name))
1083 (string= prop-val file))
1090 (tumme-thumbnail-window) (point))
1091 (tumme-display-thumb-properties)))
1092 (set-buffer old-buf)))))
1094 (defun tumme-dired-next-line (&optional arg)
1095 "Call `dired-next-line', then track thumbnail.
1096 This can safely replace `dired-next-line'. With prefix argument, move
1099 (dired-next-line (or arg 1))
1100 (if tumme-track-movement
1101 (tumme-track-thumbnail)))
1103 (defun tumme-dired-previous-line (&optional arg)
1104 "Call `dired-previous-line', then track thumbnail.
1105 This can safely replace `dired-previous-line'. With prefix argument,
1108 (dired-previous-line (or arg 1))
1109 (if tumme-track-movement
1110 (tumme-track-thumbnail)))
1112 (defun tumme-forward-char ()
1113 "Move to next image and display properties."
1115 ;; Before we move, make sure that there is an image two positions
1119 (tumme-image-at-point-p))
1122 (while (and (not (eobp))
1123 (not (tumme-image-at-point-p)))
1125 (if tumme-track-movement
1126 (tumme-track-original-file))))
1127 (tumme-display-thumb-properties))
1129 (defun tumme-backward-char ()
1130 "Move to previous image and display properties."
1135 (while (and (not (bobp))
1136 (not (tumme-image-at-point-p)))
1138 (if tumme-track-movement
1139 (tumme-track-original-file))))
1140 (tumme-display-thumb-properties))
1142 (defun tumme-next-line ()
1143 "Move to next line and display properties."
1146 ;; If we end up in an empty spot, back up to the next thumbnail.
1147 (if (not (tumme-image-at-point-p))
1148 (tumme-backward-char))
1149 (if tumme-track-movement
1150 (tumme-track-original-file))
1151 (tumme-display-thumb-properties))
1154 (defun tumme-previous-line ()
1155 "Move to previous line and display properties."
1158 ;; If we end up in an empty spot, back up to the next
1159 ;; thumbnail. This should only happen if the user deleted a
1160 ;; thumbnail and did not refresh, so it is not very common. But we
1161 ;; can handle it in a good manner, so why not?
1162 (if (not (tumme-image-at-point-p))
1163 (tumme-backward-char))
1164 (if tumme-track-movement
1165 (tumme-track-original-file))
1166 (tumme-display-thumb-properties))
1168 (defun tumme-format-properties-string (buf file props comment)
1169 "Format display properties.
1170 BUF is the associated dired buffer, FILE is the original image file
1171 name, PROPS is a list of tags and COMMENT is the image files's
1174 tumme-display-properties-format
1178 (cons ?t (or (princ props) ""))
1179 (cons ?c (or comment "")))))
1181 (defun tumme-display-thumb-properties ()
1182 "Display thumbnail properties in the echo area."
1184 (let ((file-name (file-name-nondirectory (tumme-original-file-name)))
1185 (dired-buf (buffer-name (tumme-associated-dired-buffer)))
1188 (get-text-property (point) 'tags)
1190 (comment (get-text-property (point) 'comment)))
1193 (tumme-format-properties-string
1199 (defun tumme-dired-file-marked-p ()
1200 "Check whether file on current line is marked or not."
1203 (not (looking-at "^ .*$"))))
1205 (defun tumme-modify-mark-on-thumb-original-file (command)
1206 "Modify mark in dired buffer.
1207 This is quite ugly but I don't know how to implemented in a better
1208 way. COMMAND is one of 'mark for marking file in dired, 'unmark for
1209 unmarking file in dired or 'flag for flagging file for delete in
1211 (let ((file-name (tumme-original-file-name))
1212 (dired-buf (tumme-associated-dired-buffer)))
1213 (if (not (and dired-buf file-name))
1214 (message "No image, or image with correct properties, at point.")
1217 (setq file-name (file-name-nondirectory file-name))
1218 (set-buffer dired-buf)
1219 (goto-char (point-min))
1220 (if (search-forward file-name nil t)
1221 (cond ((eq command 'mark) (dired-mark 1))
1222 ((eq command 'unmark) (dired-unmark 1))
1223 ((eq command 'toggle)
1224 (if (tumme-dired-file-marked-p)
1227 ((eq command 'flag) (dired-flag-file-deletion 1))))))))
1229 (defun tumme-mark-thumb-original-file ()
1230 "Mark original image file in associated dired buffer."
1232 (tumme-modify-mark-on-thumb-original-file 'mark)
1233 (tumme-forward-char))
1235 (defun tumme-unmark-thumb-original-file ()
1236 "Unmark original image file in associated dired buffer."
1238 (tumme-modify-mark-on-thumb-original-file 'unmark)
1239 (tumme-forward-char))
1241 (defun tumme-flag-thumb-original-file ()
1242 "Flag original image file for deletion in associated dired buffer."
1244 (tumme-modify-mark-on-thumb-original-file 'flag)
1245 (tumme-forward-char))
1247 (defun tumme-toggle-mark-thumb-original-file ()
1248 "Toggle mark on original image file in associated dired buffer."
1250 (tumme-modify-mark-on-thumb-original-file 'toggle))
1252 (defun tumme-jump-original-dired-buffer ()
1253 "Jump to the dired buffer associated with the current image file.
1254 You probably want to use this together with
1255 `tumme-track-original-file'."
1257 (let ((buf (tumme-associated-dired-buffer))
1259 (setq window (tumme-get-buffer-window buf))
1262 (if (not (equal (selected-frame) (setq frame (window-frame window))))
1263 (select-frame-set-input-focus frame))
1264 (select-window window))
1265 (message "Associated dired buffer not visible"))))
1268 (defun tumme-jump-thumbnail-buffer ()
1269 "Jump to thumbnail buffer."
1271 (let ((window (tumme-thumbnail-window))
1275 (if (not (equal (selected-frame) (setq frame (window-frame window))))
1276 (select-frame-set-input-focus frame))
1277 (select-window window))
1278 (message "Thumbnail buffer not visible"))))
1280 (defvar tumme-thumbnail-mode-map (make-sparse-keymap)
1281 "Keymap for `tumme-thumbnail-mode'.")
1283 (defvar tumme-thumbnail-mode-line-up-map (make-sparse-keymap)
1284 "Keymap for line-up commands in `tumme-thumbnail-mode'.")
1286 (defvar tumme-thumbnail-mode-tag-map (make-sparse-keymap)
1287 "Keymap for tag commands in `tumme-thumbnail-mode'.")
1289 (defun tumme-define-thumbnail-mode-keymap ()
1290 "Define keymap for `tumme-thumbnail-mode'."
1293 (define-key tumme-thumbnail-mode-map [right] 'tumme-forward-char)
1294 (define-key tumme-thumbnail-mode-map [left] 'tumme-backward-char)
1295 (define-key tumme-thumbnail-mode-map [up] 'tumme-previous-line)
1296 (define-key tumme-thumbnail-mode-map [down] 'tumme-next-line)
1297 (define-key tumme-thumbnail-mode-map "\C-f" 'tumme-forward-char)
1298 (define-key tumme-thumbnail-mode-map "\C-b" 'tumme-backward-char)
1299 (define-key tumme-thumbnail-mode-map "\C-p" 'tumme-previous-line)
1300 (define-key tumme-thumbnail-mode-map "\C-n" 'tumme-next-line)
1302 (define-key tumme-thumbnail-mode-map "d" 'tumme-flag-thumb-original-file)
1303 (define-key tumme-thumbnail-mode-map [delete]
1304 'tumme-flag-thumb-original-file)
1305 (define-key tumme-thumbnail-mode-map "m" 'tumme-mark-thumb-original-file)
1306 (define-key tumme-thumbnail-mode-map "u" 'tumme-unmark-thumb-original-file)
1307 (define-key tumme-thumbnail-mode-map "." 'tumme-track-original-file)
1308 (define-key tumme-thumbnail-mode-map [tab] 'tumme-jump-original-dired-buffer)
1311 (define-key tumme-thumbnail-mode-map "g" tumme-thumbnail-mode-line-up-map)
1313 ;; map it to "g" so that the user can press it more quickly
1314 (define-key tumme-thumbnail-mode-line-up-map "g" 'tumme-line-up-dynamic)
1315 ;; "f" for "fixed" number of thumbs per row
1316 (define-key tumme-thumbnail-mode-line-up-map "f" 'tumme-line-up)
1317 ;; "i" for "interactive"
1318 (define-key tumme-thumbnail-mode-line-up-map "i" 'tumme-line-up-interactive)
1321 (define-key tumme-thumbnail-mode-map "t" tumme-thumbnail-mode-tag-map)
1323 ;; map it to "t" so that the user can press it more quickly
1324 (define-key tumme-thumbnail-mode-tag-map "t" 'tumme-tag-thumbnail)
1326 (define-key tumme-thumbnail-mode-tag-map "r" 'tumme-tag-thumbnail-remove)
1328 (define-key tumme-thumbnail-mode-map "\C-m"
1329 'tumme-display-thumbnail-original-image)
1330 (define-key tumme-thumbnail-mode-map [C-return]
1331 'tumme-thumbnail-display-external)
1333 (define-key tumme-thumbnail-mode-map "l" 'tumme-rotate-thumbnail-left)
1334 (define-key tumme-thumbnail-mode-map "r" 'tumme-rotate-thumbnail-right)
1336 (define-key tumme-thumbnail-mode-map "L" 'tumme-rotate-original-left)
1337 (define-key tumme-thumbnail-mode-map "R" 'tumme-rotate-original-right)
1339 (define-key tumme-thumbnail-mode-map "D" 'tumme-thumbnail-set-image-description)
1341 (define-key tumme-thumbnail-mode-map "\C-d" 'tumme-delete-char)
1342 (define-key tumme-thumbnail-mode-map " "
1343 'tumme-display-next-thumbnail-original)
1344 (define-key tumme-thumbnail-mode-map
1345 (kbd "DEL") 'tumme-display-previous-thumbnail-original)
1346 (define-key tumme-thumbnail-mode-map "c" 'tumme-comment-thumbnail)
1347 (define-key tumme-thumbnail-mode-map "q" 'tumme-kill-buffer-and-window)
1350 (define-key tumme-thumbnail-mode-map [mouse-2] 'tumme-mouse-display-image)
1351 (define-key tumme-thumbnail-mode-map [mouse-1] 'tumme-mouse-select-thumbnail)
1353 ;; Seems I must first set C-down-mouse-1 to undefined, or else it
1354 ;; will trigger the buffer menu. If I try to instead bind
1355 ;; C-down-mouse-1 to `tumme-mouse-toggle-mark', I get a message
1356 ;; about C-mouse-1 not being defined afterwards. Annoying, but I
1357 ;; probably do not completely understand mouse events.
1359 (define-key tumme-thumbnail-mode-map [C-down-mouse-1] 'undefined)
1360 (define-key tumme-thumbnail-mode-map [C-mouse-1] 'tumme-mouse-toggle-mark)
1363 (define-key tumme-thumbnail-mode-map [menu-bar tumme]
1364 (cons "Tumme" (make-sparse-keymap "Tumme")))
1366 (define-key tumme-thumbnail-mode-map
1367 [menu-bar tumme tumme-kill-buffer-and-window]
1368 '("Quit" . tumme-kill-buffer-and-window))
1370 (define-key tumme-thumbnail-mode-map
1371 [menu-bar tumme tumme-delete-char]
1372 '("Delete thumbnail from buffer" . tumme-delete-char))
1374 (define-key tumme-thumbnail-mode-map
1375 [menu-bar tumme tumme-tag-thumbnail-remove]
1376 '("Remove tag from thumbnail" . tumme-tag-thumbnail-remove))
1378 (define-key tumme-thumbnail-mode-map
1379 [menu-bar tumme tumme-tag-thumbnail]
1380 '("Tag thumbnail" . tumme-tag-thumbnail))
1382 (define-key tumme-thumbnail-mode-map
1383 [menu-bar tumme tumme-comment-thumbnail]
1384 '("Comment thumbnail" . tumme-comment-thumbnail))
1386 (define-key tumme-thumbnail-mode-map
1387 [menu-bar tumme tumme-refresh-thumb]
1388 '("Refresh thumb" . tumme-refresh-thumb))
1389 (define-key tumme-thumbnail-mode-map
1390 [menu-bar tumme tumme-line-up-dynamic]
1391 '("Dynamic line up" . tumme-line-up-dynamic))
1392 (define-key tumme-thumbnail-mode-map
1393 [menu-bar tumme tumme-line-up]
1394 '("Line up thumbnails" . tumme-line-up))
1396 (define-key tumme-thumbnail-mode-map
1397 [menu-bar tumme tumme-rotate-thumbnail-left]
1398 '("Rotate thumbnail left" . tumme-rotate-thumbnail-left))
1399 (define-key tumme-thumbnail-mode-map
1400 [menu-bar tumme tumme-rotate-thumbnail-right]
1401 '("Rotate thumbnail right" . tumme-rotate-thumbnail-right))
1403 (define-key tumme-thumbnail-mode-map
1404 [menu-bar tumme tumme-rotate-original-left]
1405 '("Rotate original left" . tumme-rotate-original-left))
1406 (define-key tumme-thumbnail-mode-map
1407 [menu-bar tumme tumme-rotate-original-right]
1408 '("Rotate original right" . tumme-rotate-original-right))
1410 (define-key tumme-thumbnail-mode-map
1411 [menu-bar tumme tumme-toggle-movement-tracking]
1412 '("Toggle movement tracking on/off" . tumme-toggle-movement-tracking))
1414 (define-key tumme-thumbnail-mode-map
1415 [menu-bar tumme tumme-jump-original-dired-buffer]
1416 '("Jump to dired buffer" . tumme-jump-original-dired-buffer))
1417 (define-key tumme-thumbnail-mode-map
1418 [menu-bar tumme tumme-track-original-file]
1419 '("Track original" . tumme-track-original-file))
1421 (define-key tumme-thumbnail-mode-map
1422 [menu-bar tumme tumme-flag-thumb-original-file]
1423 '("Flag original for deletion" . tumme-flag-thumb-original-file))
1424 (define-key tumme-thumbnail-mode-map
1425 [menu-bar tumme tumme-unmark-thumb-original-file]
1426 '("Unmark original" . tumme-unmark-thumb-original-file))
1427 (define-key tumme-thumbnail-mode-map
1428 [menu-bar tumme tumme-mark-thumb-original-file]
1429 '("Mark original" . tumme-mark-thumb-original-file))
1431 (define-key tumme-thumbnail-mode-map
1432 [menu-bar tumme tumme-thumbnail-display-external]
1433 '("Display in external viewer" . tumme-thumbnail-display-external))
1434 (define-key tumme-thumbnail-mode-map
1435 [menu-bar tumme tumme-display-thumbnail-original-image]
1436 '("Display image" . tumme-display-thumbnail-original-image)))
1438 (defvar tumme-display-image-mode-map (make-sparse-keymap)
1439 "Keymap for `tumme-display-image-mode'.")
1441 (defun tumme-define-display-image-mode-keymap ()
1442 "Define keymap for `tumme-display-image-mode'."
1445 (define-key tumme-display-image-mode-map "q" 'tumme-kill-buffer-and-window)
1447 (define-key tumme-display-image-mode-map "f"
1448 'tumme-display-current-image-full)
1450 (define-key tumme-display-image-mode-map "s"
1451 'tumme-display-current-image-sized)
1454 (define-key tumme-display-image-mode-map [menu-bar tumme]
1455 (cons "Tumme" (make-sparse-keymap "Tumme")))
1457 (define-key tumme-display-image-mode-map
1458 [menu-bar tumme tumme-kill-buffer-and-window]
1459 '("Quit" . tumme-kill-buffer-and-window))
1461 (define-key tumme-display-image-mode-map
1462 [menu-bar tumme tumme-display-current-image-sized]
1463 '("Display original, sized to fit" . tumme-display-current-image-sized))
1465 (define-key tumme-display-image-mode-map
1466 [menu-bar tumme tumme-display-current-image-full]
1467 '("Display original, full size" . tumme-display-current-image-full))
1471 (defun tumme-display-current-image-full ()
1472 "Display current image in full size."
1474 (let ((file (tumme-original-file-name)))
1477 (tumme-display-image file t)
1478 (message "Full size image displayed"))
1479 (error "No original file name at point"))))
1481 (defun tumme-display-current-image-sized ()
1482 "Display current image in sized to fit window dimensions."
1484 (let ((file (tumme-original-file-name)))
1487 (tumme-display-image file)
1488 (message "Full size image displayed"))
1489 (error "No original file name at point"))))
1491 (define-derived-mode tumme-thumbnail-mode
1492 fundamental-mode "tumme-thumbnail"
1493 "Browse and manipulate thumbnail images using dired.
1494 Use `tumme-dired' and `tumme-setup-dired-keybindings' to get a
1495 nice setup to start with."
1496 (tumme-define-thumbnail-mode-keymap)
1497 (message "tumme-thumbnail-mode enabled"))
1499 (define-derived-mode tumme-display-image-mode
1500 fundamental-mode "tumme-image-display"
1501 "Mode for displaying and manipulating original image.
1502 Resized or in full-size."
1503 (tumme-define-display-image-mode-keymap)
1504 (message "tumme-display-image-mode enabled"))
1507 (defun tumme-setup-dired-keybindings ()
1508 "Setup easy-to-use keybindings for the commands to be used in dired mode.
1509 Note that n, p and <down> and <up> will be hijacked and bound to
1510 `tumme-dired-x-line'."
1513 ;; Hijack previous and next line movement. Let C-p and C-b be
1516 (define-key dired-mode-map "p" 'tumme-dired-previous-line)
1517 (define-key dired-mode-map "n" 'tumme-dired-next-line)
1518 (define-key dired-mode-map [up] 'tumme-dired-previous-line)
1519 (define-key dired-mode-map [down] 'tumme-dired-next-line)
1521 (define-key dired-mode-map (kbd "C-S-n") 'tumme-next-line-and-display)
1522 (define-key dired-mode-map (kbd "C-S-p") 'tumme-previous-line-and-display)
1523 (define-key dired-mode-map (kbd "C-S-m") 'tumme-mark-and-display-next)
1525 (define-key dired-mode-map "\C-td" 'tumme-display-thumbs)
1526 (define-key dired-mode-map "\C-tt" 'tumme-tag-files)
1527 (define-key dired-mode-map "\C-tr" 'tumme-tag-remove)
1528 (define-key dired-mode-map [tab] 'tumme-jump-thumbnail-buffer)
1529 (define-key dired-mode-map "\C-ti" 'tumme-display-dired-image)
1530 (define-key dired-mode-map "\C-tx" 'tumme-dired-display-external)
1531 (define-key dired-mode-map "\C-ta" 'tumme-display-thumbs-append)
1532 (define-key dired-mode-map "\C-t." 'tumme-display-thumb)
1533 (define-key dired-mode-map "\C-tc" 'tumme-dired-comment-files)
1534 (define-key dired-mode-map "\C-tf" 'tumme-mark-tagged-files)
1537 (define-key dired-mode-map [menu-bar tumme]
1538 (cons "Tumme" (make-sparse-keymap "Tumme")))
1540 (define-key dired-mode-map [menu-bar tumme tumme-copy-with-exif-file-name]
1541 '("Copy with EXIF file name" . tumme-copy-with-exif-file-name))
1543 (define-key dired-mode-map [menu-bar tumme tumme-dired-comment-files]
1544 '("Comment files" . tumme-dired-comment-files))
1546 (define-key dired-mode-map [menu-bar tumme tumme-mark-tagged-files]
1547 '("Mark tagged files" . tumme-mark-tagged-files))
1549 (define-key dired-mode-map [menu-bar tumme tumme-tag-remove]
1550 '("Remove tag from files" . tumme-tag-remove))
1552 (define-key dired-mode-map [menu-bar tumme tumme-tag-files]
1553 '("Tag files" . tumme-tag-files))
1555 (define-key dired-mode-map [menu-bar tumme tumme-jump-thumbnail-buffer]
1556 '("Jump to thumbnail buffer" . tumme-jump-thumbnail-buffer))
1558 (define-key dired-mode-map [menu-bar tumme tumme-toggle-movement-tracking]
1559 '("Toggle movement tracking" . tumme-toggle-movement-tracking))
1561 (define-key dired-mode-map
1562 [menu-bar tumme tumme-toggle-append-browsing]
1563 '("Toggle append browsing" . tumme-toggle-append-browsing))
1565 (define-key dired-mode-map
1566 [menu-bar tumme tumme-toggle-disp-props]
1567 '("Toggle display properties" . tumme-toggle-dired-display-properties))
1569 (define-key dired-mode-map
1570 [menu-bar tumme tumme-dired-display-external]
1571 '("Display in external viewer" . tumme-dired-display-external))
1572 (define-key dired-mode-map
1573 [menu-bar tumme tumme-display-dired-image]
1574 '("Display image" . tumme-display-dired-image))
1575 (define-key dired-mode-map
1576 [menu-bar tumme tumme-display-thumb]
1577 '("Display this thumbnail" . tumme-display-thumb))
1578 (define-key dired-mode-map
1579 [menu-bar tumme tumme-display-thumbs-append]
1580 '("Display thumbnails append" . tumme-display-thumbs-append))
1581 (define-key dired-mode-map
1582 [menu-bar tumme tumme-display-thumbs]
1583 '("Display thumbnails" . tumme-display-thumbs))
1585 (define-key dired-mode-map
1586 [menu-bar tumme tumme-create-thumbs]
1587 '("Create thumbnails for marked files" . tumme-create-thumbs))
1589 (define-key dired-mode-map
1590 [menu-bar tumme tumme-mark-and-display-next]
1591 '("Mark and display next" . tumme-mark-and-display-next))
1592 (define-key dired-mode-map
1593 [menu-bar tumme tumme-previous-line-and-display]
1594 '("Display thumb for previous file" . tumme-previous-line-and-display))
1595 (define-key dired-mode-map
1596 [menu-bar tumme tumme-next-line-and-display]
1597 '("Display thumb for next file" . tumme-next-line-and-display)))
1599 (defun tumme-create-thumbs (&optional arg)
1600 "Create thumbnail images for all marked files in dired.
1601 With prefix argument ARG, create thumbnails even if they already exist
1602 \(i.e. use this to refresh your thumbnails)."
1604 (let (curr-file thumb-name files count)
1605 (setq files (dired-get-marked-files))
1608 (setq thumb-name (tumme-thumb-name curr-file))
1609 ;; If the user overrides the exist check, we must clear the
1610 ;; image cache so that if the user wants to display the
1611 ;; thumnail, it is not fetched from cache.
1613 (clear-image-cache))
1614 (if (or (not (file-exists-p thumb-name))
1616 (if (not (= 0 (tumme-create-thumb curr-file
1617 (tumme-thumb-name curr-file))))
1618 (error "Thumb could not be created"))))
1621 (defvar tumme-slideshow-timer nil
1624 (defvar tumme-slideshow-count 0
1625 "Keeping track on number of images in slideshow.")
1627 (defvar tumme-slideshow-times 0
1628 "Number of pictures to display in slideshow.")
1630 (defun tumme-slideshow-step ()
1631 "Step to next file, if `tumme-slideshow-times' has not been reached."
1632 (if (< tumme-slideshow-count tumme-slideshow-times)
1634 (message "%s" (1+ tumme-slideshow-count))
1635 (setq tumme-slideshow-count (1+ tumme-slideshow-count))
1636 (tumme-next-line-and-display))
1637 (tumme-slideshow-stop)))
1639 (defun tumme-slideshow-start ()
1641 Ask user for number of images to show and the delay in between."
1643 (setq tumme-slideshow-count 0)
1644 (setq tumme-slideshow-times (string-to-number (read-string "How many: ")))
1645 (let ((repeat (string-to-number
1647 "Delay, in seconds. Decimals are accepted : " "1"))))
1648 (setq tumme-slideshow-timer
1651 'tumme-slideshow-step))))
1653 (defun tumme-slideshow-stop ()
1656 (cancel-timer tumme-slideshow-timer))
1658 (defun tumme-delete-char ()
1659 "Remove current thumbnail from thumbnail buffer and line up."
1661 (let ((inhibit-read-only t))
1663 (if (looking-at " ")
1667 (defun tumme-display-thumbs-append ()
1668 "Append thumbnails to `tumme-thumbnail-buffer'."
1670 (tumme-display-thumbs nil t t))
1673 (defun tumme-display-thumb ()
1674 "Shorthard for `tumme-display-thumbs' with prefix argument."
1676 (tumme-display-thumbs t nil t))
1678 (defun tumme-line-up ()
1679 "Line up thumbnails according to `tumme-thumbs-per-row'.
1680 See also `tumme-line-up-dynamic'."
1682 (let ((inhibit-read-only t))
1683 (goto-char (point-min))
1684 (while (and (not (tumme-image-at-point-p))
1689 (while (and (not (tumme-image-at-point-p))
1692 (goto-char (point-min))
1696 (if (= tumme-thumbs-per-row 1)
1699 (setq count (1+ count))
1700 (if (= count (- tumme-thumbs-per-row 1))
1705 (goto-char (point-min))))
1707 (defun tumme-line-up-dynamic ()
1708 "Line up thumbnails images dynamically.
1709 Calculate how many thumbnails fit."
1711 (let* ((char-width (frame-char-width))
1712 (width (tumme-window-width-pixels (tumme-thumbnail-window)))
1713 (tumme-thumbs-per-row
1715 (+ (* 2 tumme-thumb-relief)
1716 (* 2 tumme-thumb-margin)
1717 tumme-thumb-width char-width))))
1720 (defun tumme-line-up-interactive ()
1721 "Line up thumbnails interactively.
1722 Ask user how many thumbnails should be displayed per row."
1724 (let ((tumme-thumbs-per-row
1725 (string-to-number (read-string "How many thumbs per row: "))))
1726 (if (not (> tumme-thumbs-per-row 0))
1727 (message "Number must be greater than 0")
1730 (defun tumme-thumbnail-display-external ()
1731 "Display original image for thumbnail at point using external viewer."
1734 (let ((file (tumme-original-file-name)))
1735 (if (not (tumme-image-at-point-p))
1736 (message "No thumbnail at point")
1738 (message "No original file name found")
1739 (shell-command (format "%s \"%s\""
1740 tumme-external-viewer
1744 (defun tumme-dired-display-external ()
1745 "Display file at point using an external viewer."
1747 (let ((file (dired-get-filename)))
1748 (shell-command (format "%s \"%s\""
1749 tumme-external-viewer
1752 (defun tumme-window-width-pixels (window)
1753 "Calculate WINDOW width in pixels."
1754 (* (window-width window) (frame-char-width)))
1756 (defun tumme-window-height-pixels (window)
1757 "Calculate WINDOW height in pixels."
1758 ;; Note: The mode-line consumes one line
1759 (* (- (window-height window) 1) (frame-char-height)))
1761 (defun tumme-display-window ()
1762 "Return window where `tumme-display-image-buffer' is visible."
1763 (get-window-with-predicate
1765 (equal (buffer-name (window-buffer window)) tumme-display-image-buffer))
1768 (defun tumme-thumbnail-window ()
1769 "Return window where `tumme-thumbnail-buffer' is visible."
1770 (get-window-with-predicate
1772 (equal (buffer-name (window-buffer window)) tumme-thumbnail-buffer))
1775 (defun tumme-associated-dired-buffer-window ()
1776 "Return window where associated dired buffer is visible."
1778 (if (tumme-image-at-point-p)
1780 (setq buf (tumme-associated-dired-buffer))
1781 (get-window-with-predicate
1783 (equal (window-buffer window) buf))))
1784 (error "No thumbnail image at point"))))
1786 (defun tumme-display-window-width ()
1787 "Return width, in pixels, of tumme's image display window."
1788 (- (tumme-window-width-pixels (tumme-display-window))
1789 tumme-display-window-width-correction))
1791 (defun tumme-display-window-height ()
1792 "Return height, in pixels, of tumme's image display window."
1793 (- (tumme-window-height-pixels (tumme-display-window))
1794 tumme-display-window-height-correction))
1796 (defun tumme-display-image (file &optional original-size)
1797 "Display image FILE in image buffer.
1798 Use this when you want to display the image, semi sized, in a window
1799 next to the thumbnail window - typically a three-window configuration
1800 with dired to the left, thumbnail window to the upper right and image
1801 window to the lower right. The image is sized to fit the display
1802 window (using a temporary file, don't worry). Because of this, it
1803 will not be as quick as opening it directly, but on most modern
1804 systems it should feel snappy enough.
1806 If optional argument ORIGINAL-SIZE is non-nil, display image in its
1808 (let ((new-file (expand-file-name tumme-temp-image-file))
1809 width height command ret)
1810 (setq file (expand-file-name file))
1811 (if (not original-size)
1813 (setq width (tumme-display-window-width))
1814 (setq height (tumme-display-window-height))
1817 tumme-cmd-create-temp-image-options
1819 (cons ?p tumme-cmd-create-temp-image-program)
1823 (cons ?t new-file))))
1824 (setq ret (shell-command command nil))
1826 (error "Could not resize image")))
1827 (copy-file file new-file t))
1829 (set-buffer (tumme-create-display-image-buffer))
1830 (let ((inhibit-read-only t))
1833 (tumme-insert-image tumme-temp-image-file 'jpeg 0 0)
1834 (goto-char (point-min))
1835 (tumme-update-property 'original-file-name file)))))
1837 (defun tumme-display-thumbnail-original-image (&optional arg)
1838 "Display current thumbnail's original image in display buffer.
1839 See documentation for `tumme-display-image' for more information.
1840 With prefix argument ARG, display image in its original size."
1842 (let ((file (tumme-original-file-name)))
1843 (if (not (string-equal major-mode "tumme-thumbnail-mode"))
1844 (message "Not in tumme-thumbnail-mode")
1845 (if (not (tumme-image-at-point-p))
1846 (message "No thumbnail at point")
1848 (message "No original file name found")
1849 (tumme-display-image file arg)
1850 (display-buffer tumme-display-image-buffer))))))
1853 (defun tumme-display-dired-image (&optional arg)
1854 "Display current image file.
1855 See documentation for `tumme-display-image' for more information.
1856 With prefix argument ARG, display image in its original size."
1858 (tumme-display-image (dired-get-filename) arg))
1860 (defun tumme-image-at-point-p ()
1861 "Return true if there is a tumme thumbnail at point."
1862 (get-text-property (point) 'tumme-thumbnail))
1864 (defun tumme-rotate-thumbnail (degrees)
1865 "Rotate thumbnail DEGREES degrees."
1866 (if (not (tumme-image-at-point-p))
1867 (message "No thumbnail at point")
1868 (let ((file (tumme-thumb-name (tumme-original-file-name)))
1870 (setq command (format-spec
1871 tumme-cmd-rotate-thumbnail-options
1873 (cons ?p tumme-cmd-rotate-thumbnail-program)
1875 (cons ?t (expand-file-name file)))))
1876 (shell-command command nil)
1877 ;; Clear the cache to refresh image. I wish I could just refresh
1878 ;; the current file but I do not know how to do that. Yet...
1879 (clear-image-cache))))
1881 (defun tumme-rotate-thumbnail-left ()
1882 "Rotate thumbnail left (counter clockwise) 90 degrees.
1883 The result of the rotation is displayed in the image display area
1884 and a confirmation is needed before the original image files is
1885 overwritten. This confirmation can be turned off using
1886 `tumme-rotate-original-ask-before-overwrite'."
1888 (tumme-rotate-thumbnail "270"))
1890 (defun tumme-rotate-thumbnail-right ()
1891 "Rotate thumbnail counter right (clockwise) 90 degrees.
1892 The result of the rotation is displayed in the image display area
1893 and a confirmation is needed before the original image files is
1894 overwritten. This confirmation can be turned off using
1895 `tumme-rotate-original-ask-before-overwrite'."
1897 (tumme-rotate-thumbnail "90"))
1899 (defun tumme-refresh-thumb ()
1900 "Force creation of new image for current thumbnail."
1902 (let ((file (tumme-original-file-name)))
1904 (tumme-create-thumb file (tumme-thumb-name file))))
1906 (defun tumme-rotate-original (degrees)
1907 "Rotate original image DEGREES degrees."
1908 (if (not (tumme-image-at-point-p))
1909 (message "No image at point")
1910 (let ((file (tumme-original-file-name))
1912 (if (not (string-match "\.[jJ][pP[eE]?[gG]$" file))
1913 (error "Only JPEG images can be rotated!"))
1914 (setq command (format-spec
1915 tumme-cmd-rotate-original-options
1917 (cons ?p tumme-cmd-rotate-original-program)
1919 (cons ?o (expand-file-name file))
1920 (cons ?t tumme-temp-rotate-image-file))))
1921 (if (not (= 0 (shell-command command nil)))
1922 (error "Could not rotate image")
1923 (tumme-display-image tumme-temp-rotate-image-file)
1924 (if (or (and tumme-rotate-original-ask-before-overwrite
1925 (y-or-n-p "Rotate to temp file OK. Overwrite original image? "))
1926 (not tumme-rotate-original-ask-before-overwrite))
1928 (copy-file tumme-temp-rotate-image-file file t)
1929 (tumme-refresh-thumb))
1930 (tumme-display-image file))))))
1932 (defun tumme-rotate-original-left ()
1933 "Rotate original image left (counter clockwise) 90 degrees."
1935 (tumme-rotate-original "270"))
1937 (defun tumme-rotate-original-right ()
1938 "Rotate original image right (clockwise) 90 degrees."
1940 (tumme-rotate-original "90"))
1942 (defun tumme-get-exif-file-name (file)
1943 "Use the image's EXIF information to return a unique file name.
1944 The file name should be unique as long as you do not take more than
1945 one picture per second. The original file name is suffixed at the end
1946 for traceability. The format of the returned file name is
1947 YYYY_MM_DD_HH_MM_DD_ORIG_FILE_NAME.jpg. Used from
1948 `tumme-copy-with-exif-file-name'."
1949 (let (data no-exif-data-found)
1950 (if (not (string-match "\.[Jj][Pp][Ee]?[Gg]$" (expand-file-name file)))
1952 (setq no-exif-data-found t)
1956 (nth 5 (file-attributes (expand-file-name file))))))
1957 (setq data (tumme-get-exif-data (expand-file-name file) "DateTimeOriginal")))
1958 (while (string-match "[ :]" data)
1959 (setq data (replace-match "_" nil nil data)))
1960 (format "%s%s%s" data
1961 (if no-exif-data-found
1964 (file-name-nondirectory file))))
1966 (defun tumme-thumbnail-set-image-description ()
1967 "Set the ImageDescription EXIF tag for the original image.
1968 If the image already has a value for this tag, it is used as the
1969 default value at the prompt."
1971 (if (not (tumme-image-at-point-p))
1972 (message "No thumbnail at point")
1973 (let* ((file (tumme-original-file-name))
1974 (old-value (tumme-get-exif-data file "ImageDescription")))
1976 (tumme-set-exif-data file "ImageDescription"
1977 (read-string "Value of ImageDescription: " old-value)))
1978 (message "Successfully wrote ImageDescription tag.")
1979 (error "Could not write ImageDescription tag")))))
1981 (defun tumme-set-exif-data (file tag-name tag-value)
1982 "In FILE, set EXIF tag TAG-NAME to value TAG-VALUE."
1984 (setq command (format-spec
1985 tumme-cmd-write-exif-data-options
1987 (cons ?p tumme-cmd-write-exif-data-program)
1988 (cons ?f (expand-file-name file))
1990 (cons ?v tag-value))))
1991 (shell-command command nil)))
1993 (defun tumme-get-exif-data (file tag-name)
1994 "From FILE, return EXIF tag TAG-NAME."
1995 (let ((buf (get-buffer-create "*tumme-get-exif-data*"))
1997 (setq command (format-spec
1998 tumme-cmd-read-exif-data-options
2000 (cons ?p tumme-cmd-read-exif-data-program)
2002 (cons ?t tag-name))))
2005 (delete-region (point-min) (point-max))
2006 (if (not (eq (shell-command command buf) 0))
2007 (error "Could not get EXIF tag")
2008 (goto-char (point-min))
2009 ;; Clean buffer from newlines and carriage returns before
2010 ;; getting final info
2011 (while (search-forward-regexp "[\n\r]" nil t)
2012 (replace-match "" nil t))
2013 (setq tag-value (buffer-substring (point-min) (point-max)))))
2016 (defun tumme-copy-with-exif-file-name ()
2017 "Copy file with unique name to main image directory.
2018 Copy current or all marked files in dired to a new file in your
2019 main image directory, using a file name generated by
2020 `tumme-get-exif-file-name'. A typical usage for this if when
2021 copying images from a digital camera into the image directory.
2023 Typically, you would open up the folder with the incoming
2024 digital images, mark the files to be copied, and execute this
2025 function. The result is a couple of new files in
2026 `tumme-main-image-directory' called
2027 2005_05_08_12_52_00_dscn0319.jpg,
2028 2005_05_08_14_27_45_dscn0320.jpg etc."
2031 (files (dired-get-marked-files)))
2036 (file-name-as-directory
2037 (expand-file-name tumme-main-image-directory))
2038 (tumme-get-exif-file-name curr-file)))
2039 (message "Copying %s to %s" curr-file new-name)
2040 (copy-file curr-file new-name))
2043 (defun tumme-display-next-thumbnail-original ()
2044 "In thubnail buffer, move to next thumbnail and display the image."
2046 (tumme-forward-char)
2047 (tumme-display-thumbnail-original-image))
2049 (defun tumme-display-previous-thumbnail-original ()
2050 "Move to previous thumbnail and display image."
2053 (tumme-backward-char)
2054 (tumme-display-thumbnail-original-image))
2056 (defun tumme-write-comment (file comment)
2057 "For FILE, write comment COMMENT in database."
2059 (let (end buf comment-beg)
2060 (setq buf (find-file tumme-db-file))
2061 (goto-char (point-min))
2062 (if (search-forward-regexp
2063 (format "^%s" file) nil t)
2068 ;; Delete old comment, if any
2069 (cond ((search-forward ";comment:" end t)
2070 (setq comment-beg (match-beginning 0))
2071 ;; Any tags after the comment?
2072 (if (search-forward ";" end t)
2073 (setq comment-end (- (point) 1))
2074 (setq comment-end end))
2075 ;; Delete comment tag and comment
2076 (delete-region comment-beg comment-end)))
2077 ;; Insert new comment
2079 (if (not (search-forward ";" end t))
2083 (insert (format "comment:%s;" comment)))
2084 ;; File does not exist in databse - add it.
2085 (goto-char (point-max))
2086 (insert (format "\n%s;comment:%s" file comment)))
2088 (kill-buffer buf))))
2090 (defun tumme-update-property (prop value)
2091 "Update text property PROP with value VALUE at point."
2092 (let ((inhibit-read-only t))
2094 (point) (1+ (point))
2099 (defun tumme-dired-comment-files ()
2100 "Add comment to current or marked files in dired."
2102 (let ((files (dired-get-marked-files))
2103 (comment (tumme-read-comment)))
2106 (tumme-write-comment curr-file comment))
2109 (defun tumme-comment-thumbnail ()
2110 "Add comment to current thumbnail in thumbnail buffer."
2112 (let* ((file (tumme-original-file-name))
2113 (comment (tumme-read-comment file)))
2114 (tumme-write-comment file comment)
2115 (tumme-update-property 'comment comment))
2116 (tumme-display-thumb-properties))
2118 (defun tumme-read-comment (&optional file)
2119 "Read comment for an image.
2120 Read comment for an image, optionally using old comment from FILE
2125 (if file (tumme-get-comment file)))))
2128 (defun tumme-get-comment (file)
2129 "Get comment for file FILE."
2131 (let (end buf comment-beg comment (base-name (file-name-nondirectory file)))
2132 (setq buf (find-file tumme-db-file))
2133 (goto-char (point-min))
2134 (if (search-forward-regexp
2135 (format "^%s" base-name) nil t)
2140 (cond ((search-forward ";comment:" end t)
2141 (setq comment-beg (point))
2142 (if (search-forward ";" end t)
2143 (setq comment-end (- (point) 1))
2144 (setq comment-end end))
2145 (setq comment (buffer-substring
2146 comment-beg comment-end))))))
2151 (defun tumme-mark-tagged-files ()
2152 "Use regexp to mark files with matching tag.
2153 A `tag' is a keyword, a piece of meta data, associated with an
2154 image file and stored in tumme's database file. This command
2155 lets you input a regexp and this will be matched against all tags
2156 on all image files in the database file. The files that have a
2157 matching tags will be marked in the dired buffer."
2159 (let ((tag (read-string "Mark tagged files (regexp): "))
2163 (setq buf (find-file tumme-db-file))
2164 (goto-char (point-min))
2166 (while (search-forward-regexp
2167 (concat "\\(^[^;\n]+\\);.*" tag ".*$") nil t)
2168 (setq files (append (list (match-string 1)) files)))
2172 ;; I tried using `dired-mark-files-regexp' but it was
2175 ;; Don't bother about hits found in other directories than
2177 (when (string= (file-name-as-directory
2178 (expand-file-name default-directory))
2179 (file-name-as-directory
2180 (file-name-directory curr-file)))
2181 (setq curr-file (file-name-nondirectory curr-file))
2182 (goto-char (point-min))
2183 (when (search-forward-regexp (format "\\s %s$" curr-file) nil t)
2184 (setq hits (+ hits 1))
2187 (message "%d files with matching tag marked." hits)))
2189 (defun tumme-mouse-display-image (event)
2190 "Use mouse EVENT, call `tumme-display-image' to display image.
2191 Track this in associated dired buffer if `tumme-track-movement' is
2195 (mouse-set-point event)
2196 (goto-char (posn-point (event-end event)))
2197 (setq file (tumme-original-file-name))
2198 (if tumme-track-movement
2199 (tumme-track-original-file))
2200 (tumme-display-image file)))
2202 (defun tumme-mouse-select-thumbnail (event)
2203 "Use mouse EVENT to select thumbnail image.
2204 Track this in associated dired buffer if `tumme-track-movement' is
2208 (mouse-set-point event)
2209 (goto-char (posn-point (event-end event)))
2210 (if tumme-track-movement
2211 (tumme-track-original-file)))
2212 (tumme-display-thumb-properties))
2214 (defun tumme-mouse-toggle-mark (event)
2215 "Use mouse EVENT to toggle dired mark for thumbnail.
2216 Track this in associated dired buffer if `tumme-track-movement' is
2220 (mouse-set-point event)
2221 (goto-char (posn-point (event-end event)))
2222 (if tumme-track-movement
2223 (tumme-track-original-file)))
2224 (tumme-toggle-mark-thumb-original-file))
2226 (defun tumme-dired-display-properties ()
2227 "Display properties for dired file in the echo area."
2229 (let* ((file (dired-get-filename))
2230 (file-name (file-name-nondirectory file))
2231 (dired-buf (buffer-name (current-buffer)))
2234 (tumme-list-tags file)
2236 (comment (tumme-get-comment file)))
2239 (tumme-format-properties-string
2245 (defvar tumme-tag-file-list nil
2246 "List to store tag-file structure.")
2248 (defvar tumme-file-tag-list nil
2249 "List to store file-tag structure.")
2251 (defvar tumme-file-comment-list nil
2252 "List to store file comments.")
2254 (defun tumme-add-to-tag-file-list (tag file)
2255 "Add relation between TAG and FILE."
2257 (if tumme-tag-file-list
2258 (if (setq curr (assoc tag tumme-tag-file-list))
2259 (if (not (member file curr))
2260 (setcdr curr (cons file (cdr curr))))
2261 (setcdr tumme-tag-file-list
2262 (cons (list tag file) (cdr tumme-tag-file-list))))
2263 (setq tumme-tag-file-list (list (list tag file))))))
2265 (defun tumme-add-to-tag-file-lists (tag file)
2266 "Helper function used from `tumme-create-gallery-lists'.
2268 Add TAG to FILE in one list and FILE to TAG in the other.
2270 Lisp structures look like the following:
2272 tumme-file-tag-list:
2274 ((\"filename1\" \"tag1\" \"tag2\" \"tag3\" ...)
2275 (\"filename2\" \"tag1\" \"tag2\" \"tag3\" ...)
2278 tumme-tag-file-list:
2280 ((\"tag1\" \"filename1\" \"filename2\" \"filename3\" ...)
2281 (\"tag2\" \"filename1\" \"filename2\" \"filename3\" ...)
2283 ;; Add tag to file list
2285 (if tumme-file-tag-list
2286 (if (setq curr (assoc file tumme-file-tag-list))
2287 (setcdr curr (cons tag (cdr curr)))
2288 (setcdr tumme-file-tag-list
2289 (cons (list file tag) (cdr tumme-file-tag-list))))
2290 (setq tumme-file-tag-list (list (list file tag))))
2291 ;; Add file to tag list
2292 (if tumme-tag-file-list
2293 (if (setq curr (assoc tag tumme-tag-file-list))
2294 (if (not (member file curr))
2295 (setcdr curr (cons file (cdr curr))))
2296 (setcdr tumme-tag-file-list
2297 (cons (list tag file) (cdr tumme-tag-file-list))))
2298 (setq tumme-tag-file-list (list (list tag file))))))
2300 (defun tumme-add-to-file-comment-list (file comment)
2301 "Helper function used from `tumme-create-gallery-lists'.
2303 For FILE, add COMMENT to list.
2305 Lisp structure looks like the following:
2307 tumme-file-comment-list:
2309 ((\"filename1\" . \"comment1\")
2310 (\"filename2\" . \"comment2\")
2312 (if tumme-file-comment-list
2313 (if (not (assoc file tumme-file-comment-list))
2314 (setcdr tumme-file-comment-list
2315 (cons (cons file comment)
2316 (cdr tumme-file-comment-list))))
2317 (setq tumme-file-comment-list (list (cons file comment)))))
2319 (defun tumme-create-gallery-lists ()
2320 "Create temporary lists used by `tumme-gallery-generate'."
2321 (let ((buf (find-file tumme-db-file))
2322 end beg file row-tags)
2323 (setq tumme-tag-file-list nil)
2324 (setq tumme-file-tag-list nil)
2325 (setq tumme-file-comment-list nil)
2326 (goto-char (point-min))
2327 (while (search-forward-regexp "^." nil t)
2332 (if (not (search-forward ";" end nil))
2333 (error "Something is really wrong, check format of database"))
2334 (setq row-tags (split-string
2335 (buffer-substring beg end) ";"))
2336 (setq file (car row-tags))
2339 (if (not (string-match "^comment:\\(.*\\)" x))
2340 (tumme-add-to-tag-file-lists x file)
2341 (tumme-add-to-file-comment-list file (match-string 1 x))))
2344 ;; Sort tag-file list
2345 (setq tumme-tag-file-list
2346 (sort tumme-tag-file-list
2348 (string< (car x) (car y))))))
2350 (defun tumme-hidden-p (file)
2351 "Return t if image FILE has a \"hidden\" tag."
2355 (if (member tag tumme-gallery-hidden-tags)
2357 (cdr (assoc file tumme-file-tag-list)))
2360 (defun tumme-gallery-generate ()
2361 "Generate gallery pages.
2362 First we create a couple of Lisp structures from the database to make
2363 it easier to generate, then HTML-files are created in
2364 `tumme-gallery-dir'"
2366 (if (eq 'per-directory tumme-thumbnail-storage)
2367 (error "Currently, gallery generation is not supported \
2368 when using per-directory thumbnail file storage"))
2369 (tumme-create-gallery-lists)
2370 (let ((tags tumme-tag-file-list)
2371 count curr tag index-buf tag-buf
2372 comment file-tags tag-link tag-link-list)
2373 ;; Make sure gallery root exist
2374 (if (file-exists-p tumme-gallery-dir)
2375 (if (not (file-directory-p tumme-gallery-dir))
2376 (error "Variable tumme-gallery-dir is not a directory"))
2377 (make-directory tumme-gallery-dir))
2379 (setq index-buf (find-file
2380 (format "%s/index.html" tumme-gallery-dir)))
2383 (insert " <body>\n")
2384 (insert " <h2>Tumme Gallery</h2>\n")
2385 (insert (format "<p>\n Gallery generated %s\n <p>\n"
2386 (current-time-string)))
2387 (insert " <h3>Tag index</h3>\n")
2389 ;; Pre-generate list of all tag links
2392 (setq tag (car curr))
2393 (when (not (member tag tumme-gallery-hidden-tags))
2394 (setq tag-link (format "<a href=\"%d.html\">%s</a>" count tag))
2397 (append tag-link-list (list (cons tag tag-link))))
2398 (setq tag-link-list (list (cons tag tag-link))))
2399 (setq count (1+ count))))
2402 ;; Main loop where we generated thumbnail pages per tag
2405 (setq tag (car curr))
2406 ;; Don't display hidden tags
2407 (when (not (member tag tumme-gallery-hidden-tags))
2408 ;; Insert link to tag page in index
2409 (insert (format " %s<br>\n" (cdr (assoc tag tag-link-list))))
2410 ;; Open per-tag file
2411 (setq tag-buf (find-file
2412 (format "%s/%s.html" tumme-gallery-dir count)))
2415 (insert " <body>\n")
2416 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2417 (insert (format " <h2>Images with tag "%s"</h2>" tag))
2418 ;; Main loop for files per tag page
2421 (when (not (tumme-hidden-p file))
2422 ;; Insert thumbnail with link to full image
2424 (format "<a href=\"%s/%s\"><img src=\"%s/%s\"%s></a>\n"
2425 tumme-gallery-image-root-url (file-name-nondirectory file)
2426 tumme-gallery-thumb-image-root-url
2427 (file-name-nondirectory (tumme-thumb-name file)) file))
2428 ;; Insert comment, if any
2429 (if (setq comment (cdr (assoc file tumme-file-comment-list)))
2430 (insert (format "<br>\n%s<br>\n" comment))
2432 ;; Insert links to other tags, if any
2434 (setq file-tags (assoc file tumme-file-tag-list))) 2)
2438 ;; Only insert if not file name or the main tag
2439 (if (and (not (equal extra-tag tag))
2440 (not (equal extra-tag file)))
2442 (format "%s " (cdr (assoc extra-tag tag-link-list))))))
2444 (insert "]<br>\n"))))
2446 (insert " <p><a href=\"index.html\">Index</a></p>\n")
2447 (insert " </body>\n")
2448 (insert "</html>\n")
2450 (kill-buffer tag-buf)
2451 (setq count (1+ count))))
2453 (insert " </body>\n")
2456 (kill-buffer index-buf)))
2458 (defun tumme-kill-buffer-and-window ()
2459 "Kill the current buffer and, if possible, also the window."
2461 (let ((buffer (current-buffer)))
2463 (delete-window (selected-window))
2465 (kill-buffer buffer)))
2467 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2468 ;;;;;;;;; TEST-SECTION ;;;;;;;;;;;
2469 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2471 ;; (defvar tumme-dir-max-size 12300000)
2473 ;; (defun tumme-test-clean-old-files ()
2474 ;; "Clean `tumme-dir' from old thumbnail files.
2475 ;; \"Oldness\" measured using last access time. If the total size of all
2476 ;; thumbnail files in `tumme-dir' is larger than 'tumme-dir-max-size',
2477 ;; old files are deleted until the max size is reached."
2482 ;; (let ((fattribs (file-attributes f)))
2483 ;; ;; Get last access time and file size
2484 ;; `(,(nth 4 fattribs) ,(nth 7 fattribs) ,f)))
2485 ;; (directory-files (tumme-dir) t ".+\.thumb\..+$"))
2486 ;; ;; Sort function. Compare time between two files.
2488 ;; (time-less-p (car l1) (car l2)))))
2489 ;; (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) files))))
2490 ;; (while (> dirsize tumme-dir-max-size)
2492 ;; (format "Size of thumbnail directory: %d, delete old file %s? "
2493 ;; dirsize (cadr (cdar files))))
2494 ;; (delete-file (cadr (cdar files)))
2495 ;; (setq dirsize (- dirsize (car (cdar files))))
2496 ;; (setq files (cdr files)))))
2498 ;;;;;;;;;;;;;;;;;;;;;;,
2500 ;; (defun dired-speedbar-buttons (dired-buffer)
2501 ;; (when (and (boundp 'tumme-use-speedbar)
2502 ;; tumme-use-speedbar)
2503 ;; (let ((filename (with-current-buffer dired-buffer
2504 ;; (dired-get-filename))))
2505 ;; (when (and (not (string-equal filename (buffer-string)))
2506 ;; (string-match (image-file-name-regexp) filename))
2508 ;; (insert (propertize
2511 ;; (tumme-get-thumbnail-image filename)))))))
2513 ;; (setq tumme-use-speedbar t)
2517 ;; arch-tag: 9d11411d-331f-4380-8b44-8adfe3a0343e
2518 ;;; tumme.el ends here