gnu-emacs-sources
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

tumme.el 0.4.4


From: Mathias Dahl
Subject: tumme.el 0.4.4
Date: 12 May 2005 00:28:21 +0200
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

New stuff:

 Version 0.4.4, 2005-05-10

 * Added `tumme-get-exif-file-name' and
 `tumme-copy-with-exif-file-name'. These commands might not
 be useful for all people because they are very
 specific. See the documentation for each function for more
 information.

 * Added `tumme-display-next-thumbnail-original' and
 `tumme-display-previous-thumbnail-original' to be used for
 easy image browsing in thumbnail buffer.

 * Added support for comments. New function
 `tumme-comment-thumbnail' added, to be used in thumbnail
 buffer.

 * Added `tumme-mark-tagged-files'. Use it in dired buffer
 to mark tagged files.

 * Added `mouse-face' property `highlight' for mouse
 highlighting and had to add a space between each thumbnail
 to avoid whole rows to be highlighted.  Doing this meant
 that I had to update `tumme-line-up' too...

 * Added `tumme-mouse-display-image'. Use mouse-2 to display
 image thumbnail when is highlighted.

 * As suggested by Ehud Karni on gnu.emacs.help, changed
 `tumme-window-DIMENSION-pixels' to use
 `frame-char-DIMENSION' instead of
 `frame-pixel-DIMENSION'. Feels better

 * Corrected a bug in `tumme-window-height-pixels'. I did
 not know that the mode-line consumed one line. Also, after
 experimenting, it seems that the only correction needed for
 the image display width is one single pixel. I left the
 corection variables in there, just in case someone has a
 system that differs.

 Enjoy!

/Mathias

;;; tumme.el --- use dired to browse your images as thumbnails

;; Copyright (C) 2005 Mathias Dahl

;; Version: 0.4.4
;; Keywords: images, thumbnails, dired
;; Author: Mathias Dahl <address@hidden>
;; Maintainer: Mathias Dahl
;; URL: http://www.emacswiki.org/cgi-bin/wiki/Tumme

;; This file is not part of GNU Emacs.

;; This is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
;; License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;;
;; BACKGROUND
;; ==========
;;
;;  I needed a program to browse, organize and tag my pictures. I got
;; tired of the old gallery program I used as it did not allow
;; multi-file operations easily. Also, it put things out of my
;; control. Image viewing programs I tested did not allow multi-file
;; operations or did not do what I wanted it to.
;;
;;  So, I got the idea to use the wonderful functionality of emacs and
;; `dired' to do it. It would allow me to do almost anything I wanted,
;; which is basically just to browse all my pictures in an easy way,
;; letting me manipulate and tag them in various ways. `dired' already
;; provide all the file handling and navigation facilities; I only
;; needed to add some functions to display the images.
;;
;;  I briefly tried out thumbs.el, and although it seemed more
;; powerful than this package, it did not work the way I wanted to. It
;; was too slow to created thumbnails of all files in a directory (I
;; currently keep all my 2000+ images in the same directory) and
;; browsing the thumbnail buffer was slow too. tumme.el will not
;; create thumbnails until they are needed and the browsing is done
;; quickly and easily in dired. I copied a great deal of ideas and
;; code from there though... :)
;;
;;  About the name: tumme means thumb in Swedish and it is used for
;; working with thumbnails, so... :)
;;
;;  `tumme' stores the thumbnail files in `tumme-dir' using the file
;; name format ORIGNAME.thumb.ORIGEXT. For example
;; ~/.tumme/myimage01.thumb.jpg. The "database" is for now just a
;; plain text file with the following format:
;;
;; file-name-non-directory;comment:comment-text;tag1;tag2;tag3;...;tagn
;;
;;
;; PREREQUISITES
;; =============
;;
;; * The ImageMagick package. Currently, `convert' and `mogrify' are
;; used. Find it here: http://www.imagemagick.org
;;
;; * For `tumme-get-exif-file-name' and
;; `tumme-copy-with-exif-file-name' to work, the command line tool
;; `exif' is needed. It can be found here:
;; http://sourceforge.net/projects/libexif/.
;;
;;
;; USAGE:
;; =====
;;
;; First, do M-x tumme-dired-folder RET. This will setup a useful
;; window configuration for you.
;;
;; Next, do M-x tumme-setup-dired-keybindings RET, which will set up
;; keybindings and a menu name Tumme in dired mode.
;;
;; Start of viewing thumbnails by doing C-S-n and C-S-p to go up and
;; down in the dired buffer while at the same time displaying a
;; thumbnail image.
;;
;; Next, try `tumme-display-thumbs' (C-td). If no file is marked, one
;; thumbnail will show up in the *tumme* buffer. If one or more files
;; are marked, all of them will be displayed.
;;
;; Pressing TAB will switch to the window containing the *tumme*
;; buffer. In there you can move between thumbnail images and display
;; a semi-sized version in an Emacs buffer (RET), or the original
;; image in an external viewer (C-RET). By pressing SPC or DEL you
;; will navigate back and fort while at the same time displaying each
;; image in Emacs. You can also navigate using arrow keys. Comment a
;; file by pressing "c". Press TAB to get back to dired.
;;
;; While in dired mode, you can tag and comment files, you can tell
;; `tumme' to mark files with a certain tag (using a regexp) etc.
;;
;; The easiest way to see the available commands is to use the Tumme
;; menus added in tumme-mode and dired-mode.
;;
;;
;; LIMITATIONS:
;; ============
;;
;; * Only tested on GNU Emacs 21.3.2 under Mandrake Linux. I have got
;; reports that `insert-image' does not work on XEmacs. I will
;; probably never try to fix that, sorry. This project was mostly
;; aimed at fixing my own itch, and I am not interested in installing
;; and using XEmacs.
;;
;; * In order to work well, `tumme' require that all your images have
;; unique names. The reason is the way thumbnail file names are
;; generated. I will probably not fix this problem as my images all
;; have unique names.
;;
;; * Only support for JPEG images for now
;;
;; * WARNING: The "database" format used might be changed so keep a
;; backup of `tumme-db-file' when testing new versions.
;;
;;
;; CHANGE HISTORY:
;; ===============
;;
;; Version 0.1, 2005-04-16
;;
;; * First release, only browsing support for now.
;;
;; Version 0.2, 2005-04-21
;;
;; * Changed calls to dired-filename-at-point to dired-get-filename
;;
;; Version 0.3, 2005-04-25
;;
;;   Quite a lot of changes:
;;
;; * Added basic image tagging support. No commands that make use of
;;   it yet.
;;
;; * Added text properties for the thumbnail images to be able to
;;   track where they came from originally. Used in `tumme-mode'.
;;
;; * Added `tumme-mode' to be used when navigating the thumbnail
;;   buffer.  Currently, there are commands to mark, unmark, flag and
;;   jump to the original file in associated dired buffer.
;;
;; * When moving around in the thumbnail buffer (in `tumme-mode'), the
;;   user can turn on tracking of the movements and let them be
;;   mirrored in the associated dired buffer.
;;
;; * In this version I have been looking at some ideas in thumbs.el,
;;   for example the image margin and relief and the `thumbs-mode'
;;   which I copied and made the `tumme-mode' from.
;;
;; Version 0.4, 2005-05-02
;;
;; * Renamed the functions that are to be used in `tumme-mode' in the
;;   thumbnail buffer.
;;
;; * The mark, unmark and flag commands in `tumme-mode' now also moves
;;   to next thumbnail, like how dired normally works.
;;
;; * Added `tumme-mode-line-up', `tumme-display-thumbs-append' and
;;   `tumme-mode-delete-char'.
;;
;; * Each thumbnail's tags is now displayed when navigating among the
;;   thumbnails in the thumbnail buffer.
;;
;; * Added simple slideshow functionality.
;;
;; Version 0.4.1, 2005-05-05
;;
;; * Fixed bug in `tumme-flag-thumb-original-file'
;;
;; * Added commands to display original image in external viewer
;;   (`tumme-display-external') and in a Emacs buffer
;;   (`tumme-display-image').
;;
;; * Minor code clean-up
;;
;; * Renamed some functions back again...
;;
;; * Added rotation of thumbnail images (90 degrees left and right)
;;
;; Version 0.4.2, 2005-05-06
;;
;; * Removed need for `tumme-display-image-size' in
;;   `tumme-display-image'. Now, the maximum image size that fits in
;;   *tumme-display* is calculated automatically. Introduced two
;;   correction variables, `tumme-display-window-width-correction' and
;;   `tumme-display-window-height-correction' to be used to correct
;;   width and height depending on width and height of window
;;   decorations, fringes etc. This works really well!
;;
;; Version 0.4.3, 2005-05-07
;;
;; * Added menus to `dired-mode' and `tumme-mode'
;;
;; * Added `tumme-mark-and-display-next'
;;
;; * Added `tumme-jump-thumbnail-buffer'
;;
;; * Bound TAB in `dired-mode-map' and `tumme-mode-map' to
;;   `tumme-jump-thumbnail-buffer' and
;;   `tumme-jump-original-dired-buffer', respectively.
;;
;; * Changed `tumme-display-image' to be more general. Now, it can be
;;   used from both thumbnail buffer and dired buffer by calling
;;   `tumme-display-thumbnail-original-image' and
;;   `tumme-display-dired-image', respectively.
;;
;; Version 0.4.4, 2005-05-10
;;
;; * Added `tumme-get-exif-file-name' and
;; `tumme-copy-with-exif-file-name'. These commands might not be
;; useful for all people because they are very specific. See the
;; documentation for each function for more information.
;;
;; * Added `tumme-display-next-thumbnail-original' and
;; `tumme-display-previous-thumbnail-original' to be used for easy
;; image browsing in thumbnail buffer.
;;
;; * Added support for comments. New function
;; `tumme-comment-thumbnail' added, to be used in thumbnail buffer.
;;
;; * Added `tumme-mark-tagged-files'. Use it in dired buffer to mark
;; tagged files.
;;
;; * Added `mouse-face' property `highlight' for mouse highlighting
;; and had to add a space between each thumbnail to avoid whole rows
;; to be highlighted.  Doing this meant that I had to update
;; `tumme-line-up' too...
;;
;; * Added `tumme-mouse-display-image'. Use mouse-2 to display image
;; thumbnail when is highlighted.
;;
;; * As suggested by Ehud Karni on gnu.emacs.help, changed
;; `tumme-window-DIMENSION-pixels' to use `frame-char-DIMENSION'
;; instead of `frame-pixel-DIMENSION'. Feels better
;;
;; * Corrected a bug in `tumme-window-height-pixels'. I did not know
;; that the mode-line consumed one line. Also, after experimenting, it
;; seems that the only correction needed for the image display width
;; is one single pixel. I left the corection variables in there, just
;; in case someone has a system that differs.
;;
;; TODO
;; ====
;;
;; * Find some way of toggling on and off really nice keybindings in
;; dired (for example, using C-n or <down> instead of C-S-n).
;;
;; * Commands to create simple HTML-albums, where the images will be
;; browsable per tag, date file name etc.
;;
;; * Some sort of EXIF support using an external EXIF tool.
;;
;; * Rewrite `tumme-modify-mark-on-thumb-original-file' to be less
;; ugly.
;;
;; * In some way keep track of buffers and windows and stuff so that
;; it works as the user expects.
;;
;; * Lossless rotate of JPEG files using JpegTRAN
;;
;; * Support other formats than JPEG

;; Code:

(defgroup tumme nil
  "Use dired to browse your images as thumbnails"
  :prefix "tumme-"
  :group 'files)

(defcustom tumme-dir "~/.tumme/"
  "*Directory where thumbnail images for are stored."
  :type 'string
  :group 'tumme)

(defcustom tumme-db-file "~/.tumme/.tumme_db"
  "*Database file where file names and their associated tags are stored."
  :type 'string
  :group 'tumme)

(defcustom tumme-temp-image-file "~/.tumme/.tumme_temp"
  "*Name of temporary image file use by
`tumme-display-in-image-buffer'"
  :type 'string
  :group 'tumme)

(defcustom tumme-thumb-size 100
  "Size of thumbnails, in pixels"
  :type 'integer
  :group 'tumme)

(defcustom tumme-thumb-relief 2
  "*Size of button-like border around thumbnails."
  :type 'integer
  :group 'tumme)

(defcustom tumme-thumb-margin 2
  "*Size of the margin around thumbnails.
This is where you see the cursor."
  :type 'integer
  :group 'tumme)

(defcustom tumme-thumbs-per-row 3
  "*Number of thumbnails to display per row in thumb buffer."
  :type 'integer
  :group 'tumme)

(defcustom tumme-display-window-width-correction 1
  "*Number to be used to correct image display window height if the
default (1) does not work (i.e. if the image does not completely fit)"
  :type 'integer
  :group 'tumme)

(defcustom tumme-display-window-height-correction 0
  "*Number to be used to correct image display window height if the
default (0) does not work (i.e. if the image does not completely fit)"
  :type 'integer
  :group 'tumme)

(defcustom tumme-track-movement nil
  "The current state of the tracking and mirroring. For more
information, see the documentation for
`tumme-track-movement-in-dired'"
  :type 'boolean
  :group 'tumme)

(defcustom tumme-external-viewer "qiv -t"
  "*Name of external viewer, including parameters, to use when
displaying original image from `tumme-mode'"
  :type 'string
  :group 'tumme)

(defcustom tumme-main-image-directory "~/pics"
  "*Name of main image directory, if any. Used by
`tumme-copy-with-exif-file-name'."
  :type 'string
  :group 'tumme)

(defun tumme-ins-img (file original-file-name
                           associated-dired-buffer)
  "Insert thumbnail image."
  (let ((i `(image :type ,'jpeg
                   :file ,file
                   :relief ,tumme-thumb-relief
                   :margin ,tumme-thumb-margin)))
    (setq beg (point))
    (insert-image i)
    (setq end (point))
    (add-text-properties
     beg end
     (list 'tumme-thumbnail t
           'original-file-name original-file-name
           'associated-dired-buffer associated-dired-buffer
           'tags (tumme-list-tags original-file-name)
           'mouse-face 'highlight
           'comment (tumme-get-comment original-file-name)))))

(defun tumme-thumb-name (file)
  "Return thumbnail file name for FILE"
  (format "%s%s.thumb.%s"
          (expand-file-name tumme-dir)
          (file-name-sans-extension (file-name-nondirectory file))
          (file-name-extension file)))

(defun tumme-create-thumb (file)
  "Using the convert program, create thumbnail image for FILE and
place it in `tumme-dir'"
  (let* ((size (int-to-string tumme-thumb-size))
         (command
          (format
           "convert -size %sx%s %s -resize %sx%s +profile \"*\" %s"
           size size
           file
           size size
           (tumme-thumb-name file))))
    (if (file-exists-p tumme-dir)
        (if (not (file-directory-p tumme-dir))
            (error "%s exists but is not a directory!" tumme-dir))
      (make-directory tumme-dir))
    (shell-command command nil)))

(defun tumme-next-line-and-display ()
  "Move to next dired line and display thumbnail image"
  (interactive)
  (dired-next-line 1)
  (tumme-display-thumbs t))

(defun tumme-previous-line-and-display ()
  "Move to previous dired line and display thumbnail image"
  (interactive)
  (dired-previous-line 1)
  (tumme-display-thumbs t))

(defun tumme-mark-and-display-next ()
  "Mark current file, move to next dired line and display thumbnail
image"
  (interactive)
  (dired-mark 1)
  (tumme-display-thumbs t))

(defun tumme-create-thumb-buffer ()
  "Create thumb buffer and set `tumme-mode'"
  (let ((buf (get-buffer-create "*tumme*")))
    (save-excursion
      (set-buffer buf)
      (setq buffer-read-only t)
      (if (not (eq major-mode 'tumme-mode))
          (tumme-mode)))
    buf))

(defun tumme-dired-folder ()
  "Convenience command that:

 - Opens dired in folder specified by the user
 - Splits windows in most useful (?) way
 - Set truncate-lines to t
"
  (interactive)
  (let ((buf (tumme-create-thumb-buffer))
        (buf2 (get-buffer-create "*tumme-display*")))
    (call-interactively 'dired)
    (split-window-horizontally)
    (setq truncate-lines t)
    (save-excursion
      (other-window 1)
      (switch-to-buffer buf)
      (split-window-vertically)
      (other-window 1)
      (switch-to-buffer buf2)
      (other-window -2))))

(defun tumme-display-thumbs (&optional arg append)
  "Display thumbnail images of all marked files in buffer *tumme*. If
a thumbnail image does not exist for a file, it is created on the
fly. With prefix arg, display only thumbnail for file at point (this
is useful if you have marked some files but want to show another
one).

Recommended usage is to split the current frame horizontally so that
you have the dired buffer in the left window and the *tumme* buffer in
the right window.
"
  (interactive "P")
  (let ((buf (tumme-create-thumb-buffer))
        curr-file thumb-name files count dired-buf beg)
    (if arg
        (setq files (list (dired-get-filename)))
      (setq files (dired-get-marked-files)))
    (setq dired-buf (current-buffer))
    (save-excursion
      (set-buffer buf)
      (setq inhibit-read-only t)
      (if (not append)
          (progn
            (erase-buffer)
            (setq count 0))
        (goto-char (point-max))
        (beginning-of-line)
        (setq beg (point))
        (end-of-line)
        (setq
         count
         (length (buffer-substring-no-properties beg (point)))))
      (mapcar
       (lambda (curr-file)
         (setq thumb-name (tumme-thumb-name curr-file))
         (if (not (file-exists-p thumb-name))
             (if (not (= 0 (tumme-create-thumb curr-file)))
                 (error "Thumb could not be created")))
         (tumme-ins-img thumb-name curr-file
                        dired-buf)
         (end-of-line)
         (goto-char (point-max))
         (setq count (1+ count))
         (if (= count tumme-thumbs-per-row)
             (progn
               (insert "\n")
               (setq count 0))
           (insert " ")))
       files)
      (setq inhibit-read-only nil))))

(defun tumme-write-tag (file tag)
  "Internal function that write tag TAG for image file FILE to the
image database."
  (save-excursion
    (let (end buf)
      (setq buf (find-file tumme-db-file))
      (goto-char (point-min))
      (if (search-forward-regexp
           (format "^%s" (file-name-nondirectory file)) nil t)
          (progn
            (end-of-line)
            (setq end (point))
            (beginning-of-line)
            (if (not (search-forward (format ";%s" tag) end t))
                (progn
                  (end-of-line)
                  (insert (format ";%s" tag)))))
        (goto-char (point-max))
        (insert (format "\n%s;%s" (file-name-nondirectory file) tag)))
      (save-buffer)
      (kill-buffer buf))))

(defun tumme-list-tags (file)
  "Internal function that reads all tags for image file FILE from the
image database."
  (save-excursion
    (let (end buf (tags ""))
      (setq buf (find-file tumme-db-file))
      (goto-char (point-min))
      (if (search-forward-regexp
           (format "^%s" (file-name-nondirectory file)) nil t)
          (progn
            (end-of-line)
            (setq end (point))
            (beginning-of-line)
            (if (search-forward ";" end t)
                (if (search-forward "comment:" end t)
                    (if (search-forward ";" end t)
                        (setq tags (buffer-substring (point) end)))
                  (setq tags (buffer-substring (point) end))))))
      (kill-buffer buf)
      (split-string tags ";"))))

(defun tumme-tag-files (arg)
  "Simple tag functionality. Whatever you enter as tag will be added,
if it does not exist, to the image database."
  (interactive "P")
  (let ((tag (read-string "Tag: "))
        curr-file files)
    (if arg
        (setq files (list (dired-get-filename)))
      (setq files (dired-get-marked-files)))
    (mapcar
     (lambda (curr-file)
       (tumme-write-tag curr-file tag))
     files)))

(defun tumme-original-file-name ()
  (get-text-property (point) 'original-file-name))

(defun tumme-associated-dired-buffer ()
  (get-text-property (point) 'associated-dired-buffer))

(defun tumme-track-original-file ()
  "Track the original file in the associated dired buffer. See
documentation for `tumme-track-movement-in-dired'. Interactive use
only useful if `tumme-track-movement' is off."
  (interactive)
  (let ((old-buf (current-buffer))
        (dired-buf (tumme-associated-dired-buffer))
        (file-name (tumme-original-file-name)))
    (if (and dired-buf file-name)
        (progn
          (setq file-name (file-name-nondirectory file-name))
          (switch-to-buffer-other-window dired-buf)
          (goto-char (point-min))
          (if (not (search-forward file-name nil t))
              (message "Could not track file")
            (dired-move-to-filename))
          (switch-to-buffer-other-window old-buf)))))

(defun tumme-track-movement-in-dired ()
  "Turn on and off `tumme-track-movement' - tracking of the movements
in the thumbnail buffer so that they are \"mirrored\" in the
associated dired buffer. When this is on, moving around in the
thumbnail buffer will find the original file in the associated dired
buffer (where the image-insertion was done from) and place the cursor
on the correct file."
  (interactive)
  (setq tumme-track-movement (not tumme-track-movement))
  (message "Tracking %s" (if tumme-track-movement "on" "off")))

(defun tumme-forward-char ()
  "Move to next image and display properties"
  (interactive)
  (forward-char)
  (while (and (not (eobp))
              (not (tumme-image-at-point-p)))
    (forward-char))
  (if tumme-track-movement
      (tumme-track-original-file))
  (tumme-display-thumb-properties))

(defun tumme-forward-char ()
  "Move to next image and display properties"
  (interactive)
  (forward-char)
  (if (looking-at "[\n ]")
      (forward-char))
  (if tumme-track-movement
      (tumme-track-original-file))
  (tumme-display-thumb-properties))


(defun tumme-backward-char ()
  "Move to previous image and display properties"
  (interactive)
  (backward-char)
  (while (and (not (eobp))
              (not (tumme-image-at-point-p)))
    (backward-char))
  (if tumme-track-movement
      (tumme-track-original-file))
  (tumme-display-thumb-properties))

(defun tumme-backward-char ()
  "Move to previous image and display properties"
  (interactive)
  (backward-char)
  (if (looking-at "[\n ]")
      (backward-char))
  (if tumme-track-movement
      (tumme-track-original-file))
  (tumme-display-thumb-properties))

(defun tumme-next-line ()
  "Move to next line and display properties"
  (interactive)
  (next-line 1)
  (if tumme-track-movement
      (tumme-track-original-file))
  (tumme-display-thumb-properties))

(defun tumme-previous-line ()
  "Move to previous line and display properties"
  (interactive)
  (previous-line 1)
  (if tumme-track-movement
      (tumme-track-original-file))
  (tumme-display-thumb-properties))

(defun tumme-display-thumb-properties ()
  "Display thumbnail properties in the echo area"
  (if (not (eobp))
      (let ((file-name (tumme-original-file-name))
            (dired-buf (tumme-associated-dired-buffer))
            (props (get-text-property (point) 'tags))
            (comment (get-text-property (point) 'comment)))
        (if file-name
            (message "%s %s%s"
                     (file-name-nondirectory
                      file-name)
                     (or props
                         "(no tags)")
                     (if comment
                         (format ": %s" comment)
                       " [no comment]"))))))

(defun tumme-modify-mark-on-thumb-original-file (command)
  "This is quite ugly but I don't know how to implemented in a better
way."
  (let ((file-name (tumme-original-file-name))
        (dired-buf (tumme-associated-dired-buffer)))
    (if (not (and dired-buf file-name))
        (message "No image, or image with correct properties, at point.")
      (save-excursion
        (message file-name)
        (setq file-name (file-name-nondirectory file-name))
        (set-buffer dired-buf)
        (goto-char (point-min))
        (if (search-forward file-name nil t)
            (cond ((eq command 'mark) (dired-mark 1))
                  ((eq command 'unmark) (dired-unmark 1))
                  ((eq command 'flag) (dired-flag-file-deletion 1))))))))

(defun tumme-mark-thumb-original-file ()
  "Mark original image file in associated dired buffer"
  (interactive)
  (tumme-modify-mark-on-thumb-original-file 'mark)
  (tumme-forward-char))

(defun tumme-unmark-thumb-original-file ()
  "Unmark original image file in associated dired buffer"
  (interactive)
  (tumme-modify-mark-on-thumb-original-file 'unmark)
  (tumme-forward-char))

(defun tumme-flag-thumb-original-file ()
  "Flag original image file for deletion in associated dired buffer"
  (interactive)
  (tumme-modify-mark-on-thumb-original-file 'flag)
  (tumme-forward-char))

(defun tumme-jump-original-dired-buffer ()
  "Jump to the dired buffer associated with the current image
file. You probably want to use this together with
`tumme-track-original-file'"
  (interactive)
  (let ((window (tumme-associated-dired-buffer-window)))
    (if window
        (select-window window)
      (message "Associated dired buffer not visible"))))

(defun tumme-jump-thumbnail-buffer ()
  "Jump to thumbnail buffer"
  (interactive)
  (let ((window (tumme-thumbnail-window)))
    (if window
        (select-window window)
      (message "Thumbnail buffer not visible"))))

(defvar tumme-mode-map (make-sparse-keymap)
  "Keymap for `tumme-mode'")

(defun tumme-define-keymap ()

  ;; Keys
  (define-key tumme-mode-map [right] 'tumme-forward-char)
  (define-key tumme-mode-map [left] 'tumme-backward-char)
  (define-key tumme-mode-map [up] 'tumme-previous-line)
  (define-key tumme-mode-map [down] 'tumme-next-line)
  (define-key tumme-mode-map "\C-f" 'tumme-forward-char)
  (define-key tumme-mode-map "\C-b" 'tumme-backward-char)
  (define-key tumme-mode-map "\C-p" 'tumme-previous-line)
  (define-key tumme-mode-map "\C-n" 'tumme-next-line)

  (define-key tumme-mode-map "d" 'tumme-flag-thumb-original-file)
  (define-key tumme-mode-map [delete] 'tumme-flag-thumb-original-file)
  (define-key tumme-mode-map "m" 'tumme-mark-thumb-original-file)
  (define-key tumme-mode-map "u" 'tumme-unmark-thumb-original-file)
  (define-key tumme-mode-map "." 'tumme-track-original-file)
  (define-key tumme-mode-map [tab] 'tumme-jump-original-dired-buffer)
  (define-key tumme-mode-map "g" 'tumme-line-up)
  (define-key tumme-mode-map "\C-m" 'tumme-display-thumbnail-original-image)
  (define-key tumme-mode-map [C-return] 'tumme-display-external)
  (define-key tumme-mode-map "l" 'tumme-rotate-thumbnail-left)
  (define-key tumme-mode-map "r" 'tumme-rotate-thumbnail-right)
  (define-key tumme-mode-map "\C-d" 'tumme-delete-char)
  (define-key tumme-mode-map " " 'tumme-display-next-thumbnail-original)
  (define-key tumme-mode-map 
    (kbd "DEL") 'tumme-display-previous-thumbnail-original)
  (define-key tumme-mode-map "c" 'tumme-comment-thumbnail)
  (define-key tumme-mode-map [mouse-2] 'tumme-mouse-display-image)

  ;; Menu
  (define-key tumme-mode-map [menu-bar tumme]
    (cons "Tumme" (make-sparse-keymap "Tumme")))

  (define-key tumme-mode-map [menu-bar tumme tumme-delete-char]
    '("Delete thumbnail from buffer" . tumme-delete-char))

  (define-key tumme-mode-map [menu-bar tumme tumme-comment-thumbnail]
    '("Comment thumbnail" . tumme-comment-thumbnail))

  (define-key tumme-mode-map [menu-bar tumme tumme-refresh-thumb]
    '("Refresh thumb" . tumme-refresh-thumb))
  (define-key tumme-mode-map [menu-bar tumme tumme-line-up]
    '("Line up thumbnails" . tumme-line-up))

  (define-key tumme-mode-map [menu-bar tumme tumme-rotate-thumbnail-left]
    '("Rotate thumbnail left" . tumme-rotate-thumbnail-left))
  (define-key tumme-mode-map [menu-bar tumme tumme-rotate-thumbnail-right]
    '("Rotate thumbnail right" . tumme-rotate-thumbnail-right))

  (define-key tumme-mode-map [menu-bar tumme tumme-track-movement-in-dired]
    '("Toggle movement tracking on/off" . tumme-track-movement-in-dired))

  (define-key tumme-mode-map [menu-bar tumme tumme-jump-original-dired-buffer]
    '("Jump to dired buffer" . tumme-jump-original-dired-buffer))
  (define-key tumme-mode-map [menu-bar tumme tumme-track-original-file]
    '("Track original" . tumme-track-original-file))

  (define-key tumme-mode-map [menu-bar tumme tumme-flag-thumb-original-file]
    '("Flag original for deletion" . tumme-flag-thumb-original-file))
  (define-key tumme-mode-map [menu-bar tumme tumme-unmark-thumb-original-file]
    '("Unmark original" . tumme-unmark-thumb-original-file))
  (define-key tumme-mode-map [menu-bar tumme tumme-mark-thumb-original-file]
    '("Mark original" . tumme-mark-thumb-original-file))

  (define-key tumme-mode-map [menu-bar tumme tumme-display-external]
    '("Display in external viewer" . tumme-display-external))
  (define-key tumme-mode-map [menu-bar tumme 
                                       tumme-display-thumbnail-original-image]
    '("Display image" . tumme-display-thumbnail-original-image)))

(define-derived-mode tumme-mode
  fundamental-mode "tumme"
  "Browse and manipulate thumbnail images using dired. Use
`tumme-dired-folder' and `tumme-setup-dired-keybindings' to get a nice
setup to start with."
  (tumme-define-keymap)
  (message "tumme-mode enabled"))

(defun tumme-setup-dired-keybindings ()
  "Setup easy-to-use keybindings for the commands to be used in dired
mode."
  (interactive)

  (require 'dired)

  (define-key dired-mode-map (kbd "C-S-n") 'tumme-next-line-and-display)
  (define-key dired-mode-map (kbd "C-S-p") 'tumme-previous-line-and-display)
  (define-key dired-mode-map (kbd "C-S-m") 'tumme-mark-and-display-next)

  (define-key dired-mode-map "\C-td" 'tumme-display-thumbs)
  (define-key dired-mode-map "\C-tt" 'tumme-tag-files)
  (define-key dired-mode-map [tab] 'tumme-jump-thumbnail-buffer)
  (define-key dired-mode-map "\C-ti" 'tumme-display-dired-image)
  (define-key dired-mode-map "\C-ta" 'tumme-display-thumbs-append)
  (define-key dired-mode-map "\C-t." 'tumme-display-thumb)
  (define-key dired-mode-map "\C-tc" 'tumme-dired-comment-files)
  (define-key dired-mode-map "\C-tf" 'tumme-mark-tagged-files)

  ;; Menu for dired
  (define-key dired-mode-map [menu-bar tumme]
    (cons "Tumme" (make-sparse-keymap "Tumme")))

  (define-key dired-mode-map [menu-bar tumme tumme-copy-with-exif-file-name]
    '("Copy with EXIF file name" . tumme-copy-with-exif-file-name))

  (define-key dired-mode-map [menu-bar tumme tumme-dired-comment-files]
    '("Comment files" . tumme-dired-comment-files))

  (define-key dired-mode-map [menu-bar tumme tumme-mark-tagged-files]
    '("Mark tagged files" . tumme-mark-tagged-files))

  (define-key dired-mode-map [menu-bar tumme tumme-tag-files]
    '("Tag files" . tumme-tag-files))

  (define-key dired-mode-map [menu-bar tumme tumme-jump-thumbnail-buffer]
    '("Jump to thumbnail buffer" . tumme-jump-thumbnail-buffer))

  (define-key dired-mode-map [menu-bar tumme tumme-display-dired-image]
    '("Display image" . tumme-display-dired-image))
  (define-key dired-mode-map [menu-bar tumme tumme-display-thumb]
    '("Display this thumbnail" . tumme-display-thumb))
  (define-key dired-mode-map [menu-bar tumme tumme-display-thumbs-append]
    '("Display thumbnails append" . tumme-display-thumbs-append))
  (define-key dired-mode-map [menu-bar tumme tumme-display-thumbs]
    '("Display thumbnails" . tumme-display-thumbs))

  (define-key dired-mode-map [menu-bar tumme tumme-create-thumbs]
    '("Create thumbnails for marked files" . tumme-create-thumbs))

  (define-key dired-mode-map [menu-bar tumme tumme-mark-and-display-next]
    '("Mark and display next" . tumme-mark-and-display-next))
  (define-key dired-mode-map [menu-bar tumme tumme-previous-line-and-display]
    '("Display thumb for previous file" . tumme-previous-line-and-display))
  (define-key dired-mode-map [menu-bar tumme tumme-next-line-and-display]
    '("Display thumb for next file" . tumme-next-line-and-display)))

(defun tumme-create-thumbs (&optional arg)
  "Create thumbnail images for all marked files in dired. With prefix
arg, create thumbnails even if they already exist (i.e. use this to
refresh your thumbnails)."
  (interactive "P")
  (let (curr-file thumb-name files count)
    (setq files (dired-get-marked-files))
    (mapcar
     (lambda (curr-file)
       (setq thumb-name (tumme-thumb-name curr-file))
       ;; If the user overrides the exist check, we must clear the
       ;; image cache so that if the user wants to display the
       ;; thumnail, it is not fetched from cache.
       (if arg
           (clear-image-cache))
       (if (or (not (file-exists-p thumb-name))
               arg)
           (if (not (= 0 (tumme-create-thumb curr-file)))
               (error "Thumb could not be created"))))
     files)))

(defvar tumme-slideshow-timer nil
  "Slideshow timer")

(defvar tumme-slideshow-count 0
  "Counter to keep track on how many pictures that have been displayed
during this slideshow")

(defvar tumme-slideshow-times 0
  "Number of pictures to display in slideshow")

(defun tumme-slideshow-step ()
  "Step to next file, if `tumme-slideshow-times' has not been
reached."
  (if (< tumme-slideshow-count tumme-slideshow-times)
      (progn
        (message "%s" (1+ tumme-slideshow-count))
        (setq tumme-slideshow-count (1+ tumme-slideshow-count))
        (tumme-next-line-and-display))
    (tumme-slideshow-stop)))

(defun tumme-slideshow-start ()
  "Start slideshow. Ask user for number of images to show and the
delay in between."
  (interactive)
  (setq tumme-slideshow-count 0)
  (setq tumme-slideshow-times (string-to-int (read-string "How many: ")))
  (let ((repeat (string-to-number
                 (read-string
                  "Delay, in seconds. Decimals are accepted : " "1"))))
    (setq tumme-slideshow-timer
          (run-with-timer
           0 repeat
           'tumme-slideshow-step))))

(defun tumme-slideshow-stop ()
  "Cancel slideshow"
  (interactive)
  (cancel-timer tumme-slideshow-timer))

(defun tumme-delete-char ()
  "Remove current thumbnail from thumbnail buffer and line up."
  (interactive)
  (setq inhibit-read-only t)
  (delete-char 1)
  (if (looking-at " ")
      (delete-char 1))
  (setq inhibit-read-only nil))

(defun tumme-display-thumbs-append ()
  "Like `tumme-display-thumbs', but append instead of replace
thumbnails."
  (interactive)
  (tumme-display-thumbs nil t))

(defun tumme-display-thumb ()
  "Shorthard for `tumme-display-thumbs' with prefix argument"
  (interactive)
  (tumme-display-thumbs t))

(defun tumme-line-up ()
  "Line up thumbnails according to `tumme-thumbs-per-row'"
  (interactive)
  (setq inhibit-read-only t)
  (goto-char (point-min))
  (while (and (not (tumme-image-at-point-p))
              (not (eobp)))
    (delete-char 1))
  (while (not (eobp))
    (forward-char)
    (while (and (not (tumme-image-at-point-p))
                (not (eobp)))
      (delete-char 1)))
  (goto-char (point-min))
  (let ((count 0))
    (while (not (eobp))
      (forward-char)
      (insert " ")
      (setq count (1+ count))
      (if (= count (- tumme-thumbs-per-row 1))
          (progn
            (forward-char)
            (insert "\n")
            (setq count 0)))))
  (goto-char (point-min))
  (setq inhibit-read-only nil))

(defun tumme-display-external ()
  "Display original image for thumbnail at point using external
viewer"
  (interactive)
  (let ((file (tumme-original-file-name)))
    (if (not (tumme-image-at-point-p))
        (message "No thumbnail at point")
      (if (not file)
          (message "No original file name found")
        (shell-command (format "%s %s"
                               tumme-external-viewer
                               (tumme-original-file-name)))))))

(defun tumme-window-width-pixels (window)
  "Calculate window width in pixels."
    (* (window-width window) (frame-char-width)))

(defun tumme-window-height-pixels (window)
  "Calculate window height in pixels."
  ;; Note: The mode-line consumes one line
    (* (- (window-height window) 1) (frame-char-height)))

(defun tumme-display-window ()
  "Return window where *tumme-display* is visible"
  (get-window-with-predicate
   (lambda (window)
     (equal (buffer-name (window-buffer window)) "*tumme-display*"))))

(defun tumme-thumbnail-window ()
  "Return window where *tumme* is visible"
  (get-window-with-predicate
   (lambda (window)
     (equal (buffer-name (window-buffer window)) "*tumme*"))))

(defun tumme-associated-dired-buffer-window ()
  "Return window where associated dired buffer is visible"
  (let ((buf (tumme-associated-dired-buffer)))
    (if (tumme-image-at-point-p)
        (get-window-with-predicate
         (lambda (window)
           (equal (window-buffer window) buf)))
      (error "No thumbnail image at point"))))

(defun tumme-display-window-width ()
  "Return width, in pixels, of tumme's image display window"
  (- (tumme-window-width-pixels (tumme-display-window))
     tumme-display-window-width-correction))

(defun tumme-display-window-height ()
  "Return height, in pixels, of tumme's image display window"
  (- (tumme-window-height-pixels (tumme-display-window))
     tumme-display-window-height-correction))

(defun tumme-display-image (file)
  "Display image in image buffer. Use this when you want to display
the image, semi sized, in a window next to the thumbnail window -
typically a three-window configuration with dired to the left,
thumbnail window to the upper right and image window to the lower
right. The image is sized to fit the display window (using a temporary
file, don't worry). Because of this, it will not be as quick as
opening it directly, but on most modern systems it should feel snappy
enough."
  (let ((size-x (tumme-display-window-width))
        (size-y (tumme-display-window-height))
        command)
    (save-excursion
      (set-buffer "*tumme-display*")
      (erase-buffer)
      (setq command
            (format
             "convert -size %sx%s %s -resize %sx%s +profile \"*\" %s"
             size-x size-y
             file
             size-x size-y
             tumme-temp-image-file))
      (shell-command command nil)
      (insert-image-file tumme-temp-image-file))))

(defun tumme-display-thumbnail-original-image ()
  "Display current thumbnail's original image. See documentation for
`tumme-display-image' for more information."
  (interactive)
  (let ((file (tumme-original-file-name)))
    (if (not (string-equal major-mode "tumme-mode"))
        (message "Not in tumme-mode")
      (if (not (tumme-image-at-point-p))
          (message "No thumbnail at point")
        (if (not file)
            (message "No original file name found")
          (tumme-display-image file))))))

(defun tumme-display-dired-image ()
  "Display current image file. See documentation for
`tumme-display-image' for more information."
  (interactive)
  (tumme-display-image (dired-get-filename)))

(defun tumme-image-at-point-p ()
  "Return true if there is a tumme thumbnail at point"
  (get-text-property (point) 'tumme-thumbnail))

(defun tumme-rotate-thumbnail (degrees)
  "Rotate thumbnail DEGREES degrees"
  (if (not (tumme-image-at-point-p))
      (message "No thumbnail at point")
    (let ((file (tumme-thumb-name (tumme-original-file-name)))
          command)
      (setq command (format "mogrify -rotate %s %s" degrees file))
      (shell-command command nil)
      ;; Clear the cache to refresh image. I wish I could just refresh
      ;; the current file but I do not know how to do that. Yet...
      (clear-image-cache))))

(defun tumme-rotate-thumbnail-left ()
  "Rotate thumbnail left (counter clockwise) 90 degrees"
  (interactive)
  (tumme-rotate-thumbnail "-90"))

(defun tumme-rotate-thumbnail-right ()
  "Rotate thumbnail counter right (clockwise) 90 degrees"
  (interactive)
  (tumme-rotate-thumbnail "90"))

(defun tumme-refresh-thumb ()
  "Force creation of new image for current thumbnail"
  (interactive)
  (clear-image-cache)
  (tumme-create-thumb (tumme-original-file-name)))

(defun tumme-get-exif-file-name (file)
  "Uses the image's EXIF information to return a hopefully unique file
name (should be unique as long as you do not take more than one
picture per second). The original file name is suffixed at the end for
traceability. The format of the returned file name is
YYYY_MM_DD_HH_MM_DD_ORIG_FILE_NAME.jpg. Used from
`tumme-copy-with-exif-file-name'"
  (let (command ret data
                (buf (get-buffer-create "*tumme-exif*")))
    (setq command (format "exif -t DateTime %s | grep Value"
                          file))
    (save-excursion
      (set-buffer buf)
      (erase-buffer)
      (setq ret (shell-command command buf))

      (if (not (= ret 0))
          (error "Could not extract EXIF information!")

        (goto-char (point-min))

        ;; Remove all but the timestamp

        (while (search-forward "  Value: " nil t)
          (replace-match "" nil t))

        (goto-char (point-min))

        ;; Replace spaces and colons with underscore in order to get
        ;; useful file names.

        (while (search-forward-regexp "[: ]" nil t)
          (replace-match "_" nil t))

        (goto-char (point-min))

        ;; Clean buffer from newlines and carriage returns before
        ;; getting final info

        (while (search-forward-regexp "[\n\r]" nil t)
          (replace-match "" nil t))

        (setq data (buffer-substring-no-properties (point-min) (point-max)))

        (format "%s_%s" data
                (file-name-nondirectory file))))))

(defun tumme-copy-with-exif-file-name ()
  "Copy current or all marked files in dired to a new file in your
main image directory, using a file name generated by
`tumme-get-exif-file-name'. This might or might not be useful for
other people, but I use it each time I fetch images from my digital
camera, for copying the images into my main image directory.

 Typically I open up the folder where I store my incoming digital
images, with file names like dscn0319.jpg, dscn0320.jpg etc., mark the
files I want to copy into my main image directory, and execute this
function. The result is a couple of new files in
`tumme-main-image-directory' called 2005_05_08_12_52_00_dscn0319.jpg,
2005_05_08_14_27_45_dscn0320.jpg etc.

 When the images are safely in my main image directory I start to
browse and tag them using rest of the functionality in `tumme'."
  (interactive)
  (let (new-name
        (files (dired-get-marked-files)))
    (mapcar
     (lambda (curr-file)
       (setq new-name
             (format "%s/%s"
                     (expand-file-name tumme-main-image-directory)
                     (tumme-get-exif-file-name curr-file)))
       (message "Copying %s to %s" curr-file new-name)
       (copy-file curr-file new-name))
     files)))

(defun tumme-display-next-thumbnail-original ()
  "In thubnail buffer, move to next thumbnail and display the image."
  (interactive)
  (tumme-forward-char)
  (tumme-display-thumbnail-original-image))

(defun tumme-display-previous-thumbnail-original ()
  "In thubnail buffer, move to previous thumbnail and display the
image."
  (interactive)
  (tumme-backward-char)
  (tumme-display-thumbnail-original-image))

(defun tumme-write-comment (file comment)
  "Write comment COMMENT for file FILE"
  (save-excursion
    (let (end buf
              (base-name (file-name-nondirectory file)))
      (setq buf (find-file tumme-db-file))
      (goto-char (point-min))
      (if (search-forward-regexp
           (format "^%s" base-name) nil t)
          (progn
            (end-of-line)
            (setq end (point))
            (beginning-of-line)
            ;; Delete old comment, if any
            (cond ((search-forward ";comment:" end t)
                   (setq comment-beg (match-beginning 0))
                   ;; Any tags after the comment?
                   (if (search-forward ";" end t)
                       (setq comment-end (- (point) 1))
                     (setq comment-end end))
                   ;; Delete comment tag and comment
                   (delete-region comment-beg comment-end)))
            ;; Insert new comment
            (beginning-of-line)
            (if (not (search-forward ";" end t))
                (progn
                  (end-of-line)
                  (insert ";")))
            (insert (format "comment:%s;" comment)))
        ;; File does not exist in databse - add it.
        (goto-char (point-max))
        (insert (format "\n%s;comment:%s" base-name comment)))
      (save-buffer)
      (kill-buffer buf))))

(defun tumme-update-property (prop value)
  "Update text property PROP with value VALUE at point"
  (setq inhibit-read-only t)
  (put-text-property
   (point) (1+ (point))
   prop
   value)
  (setq inhibit-read-only nil))

(defun tumme-dired-comment-files ()
  "Add comment to current or marked files in dired"
  (interactive)
  (let ((files (dired-get-marked-files))
         (comment (tumme-read-comment)))
    (mapcar
     (lambda (curr-file)
       (tumme-write-comment curr-file comment))
     files)))

(defun tumme-comment-thumbnail ()
  "Add comment to current thumbnail in thumbnail buffer."
  (interactive)
  (let* ((file (tumme-original-file-name))
         (comment (tumme-read-comment file)))
    (tumme-write-comment file comment)
    (tumme-update-property 'comment comment))
  (tumme-display-thumb-properties))

(defun tumme-read-comment (&optional file)
  "Read comment, optionally using old comment from FILE as initial
value"
  (let ((comment
         (read-string
          "Comment: "
          (if file (tumme-get-comment file)))))
    comment))

(defun tumme-get-comment (file)
  "Get comment for file FILE"
  (save-excursion
    (let (end buf comment (base-name (file-name-nondirectory file)))
      (setq buf (find-file tumme-db-file))
      (goto-char (point-min))
      (if (search-forward-regexp
           (format "^%s" base-name) nil t)
          (progn
            (end-of-line)
            (setq end (point))
            (beginning-of-line)
            (cond ((search-forward ";comment:" end t)
                   (setq comment-beg (point))
                   (if (search-forward ";" end t)
                       (setq comment-end (- (point) 1))
                     (setq comment-end end))
                   (setq comment (buffer-substring 
                                  comment-beg comment-end))))))
      (kill-buffer buf)
      comment)))

(defun tumme-mark-tagged-files ()
  "Use regexp to mark files with matching tag."
  (interactive)
  (let (files buf
        (tag (read-string "Mark tagged files (regexp): ")))
    (save-excursion
      (setq buf (find-file tumme-db-file))
      (goto-char (point-min))
      ;; Collect matches
      (while (search-forward-regexp (concat "\\(^[^;]+\\);.*" tag ".*$") nil t)
        (setq files (append (list (match-string 1)) files)))
      (kill-buffer buf)
      ;; Mark files
      (mapcar
       (lambda (curr-file)
         ;; I tried using `dired-mark-files-regexp' but it was
         ;; waaaay to slow.
         (goto-char (point-min))
         (if (search-forward-regexp (format "\\s %s$" curr-file) nil t)
             (dired-mark 1)))
       files))
    (message "%d files with matching tag marked." (length files))))

(defun tumme-mouse-display-image (event)
  (interactive "e")
  (let (file)
    (save-excursion
      (set-buffer (window-buffer (posn-window (event-end event))))
      (save-excursion
        (goto-char (posn-point (event-end event)))
        (setq file (tumme-original-file-name))))
    (select-window (posn-window (event-end event)))
    (tumme-display-image file)))

(provide 'tumme)

;;; tumme.el ends here


reply via email to

[Prev in Thread] Current Thread [Next in Thread]