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

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

tumme.el 0.4


From: Mathias Dahl
Subject: tumme.el 0.4
Date: 03 May 2005 02:54:22 +0200
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

 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.

Enjoy!

/Mathias

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

;; Copyright (C) 2005 Mathias Dahl

;; Version: 0.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:

;;  About the name: tumme means thumb in Swedish. Google did not give
;; me any hits for "tumme.el" so I thought it would be safe to use.
;;
;;  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. Programs that did allow multi-file operations 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... :)
;;
;;  `tumme' stores the thumbnail files in `tumme-dir' using the file
;; name format ORIGNAME.thumb.ORIGEXT. For example
;; ~/.tumme/myimage01.thumb.jpg and the "database" is for now just a
;; plain text file with the following format:
;;
;; file-name-non-directory;tag1;tag2;tag3;...;tagn
;;

;;; Feedback

;; Feedback is welcome.

;;; Prerequisites:

;; * The convert program from the ImageMagick package

;;; Usage:

;; First, do M-x tumme-dired-folder RET. This will setup a useful
;; window- and buffer-configuration for you.
;;
;; Next, do M-x tumme-setup-dired-keybindings RET, which will set up
;; keybindings in dired mode that works quite well.

;;; 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.

;;; Bugs:

;; I'm not sure if I really consider this a bug, but 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.

;;; Todo:

;; - Commands to open the images in full size, either in an emacs
;; buffer or external program, or both.
;;
;; - Find some way of toggling on and off really nice keybindings (for
;; example, using C-n or <down> instead of C-S-n) to the interactive
;; functions.
;;
;; - Command to refresh thumbnail images. For now, you have to delete
;; the old files manually.
;;
;; - 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 and more stable.
;;
;; - In some way keep track of buffers and windows and stuff so that
;; it works as the user expects.
;;
;; - Functionality to use tags for marking files in dired buffer.
;;

;;; 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_db"
  "*Database file where file names and their associated tags are stored."
  :type 'string
  :group 'tumme)

(defcustom tumme-tags-file "~/.tumme_tags"
  "*Database file where the tags are stored."
  :type 'string
  :group 'tumme)

(defcustom tumme-file-name-display-format "\n%s"
  "*Format used to display the file name to the right of the
picture. `%s' is the file name. If you want extra space between each
thumbnail, add extra newlines at the end of the format. If you want to
have the file name next to the thumbnail, use no newline at all."
  :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-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)

(defun tumme-ins-img (file original-file-name
                           associated-dired-buffer)
  "Insert thumbnail image."
  (let (beg end
            (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 'original-file-name original-file-name
           'associated-dired-buffer associated-dired-buffer))))

(defun tumme-ins-img (file original-file-name
                           associated-dired-buffer)
  "Insert thumbnail image."
  (let (beg end tags
            (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 'original-file-name original-file-name
           'associated-dired-buffer associated-dired-buffer
           'tags (tumme-list-tags 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-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 the window horizontally
 - Set truncate-lines to t
"
  (interactive)
  (let ((buf (tumme-create-thumb-buffer)))
    (call-interactively 'dired)
    (split-window-horizontally)
    (setq truncate-lines t)
    (save-excursion
      (other-window 1)
      (switch-to-buffer buf)
      (other-window -1))))

(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))))
       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)
            (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-mode-track-original-file ()
  "Track the original file in the associated dired buffer. See
documentation for `tumme-track-movement-in-dired'"
  (interactive)
  (let ((old-buf (current-buffer))
        (dired-buf (get-text-property (point) 'associated-dired-buffer))
        (file-name (get-text-property (point) '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-mode-track-movement-in-dired ()
  "Turn on and off 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-mode-forward-char ()
  "Move to next image and display properties"
  (interactive)
  (forward-char)
  (if (looking-at "\n")
         (forward-char))
  (if tumme-track-movement
      (tumme-mode-track-original-file))
  (tumme-mode-display-thumb-properties))

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

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

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

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

(defun tumme-mode-modify-mark-on-thumb-original-file (command)
  "This is quite ugly but I don't know how to implemented in a better
way."
  (let ((dired-buf (get-text-property (point) 'associated-dired-buffer))
        (file-name (get-text-property (point) 'original-file-name)))
    (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)))
          (message "Blä!"))))))

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

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

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

(defun tumme-mode-jump-thumb-original-dired-buffer ()
  "Jump to the dired buffer associated with the current image
file. You probably want to use this together with
`tumme-mode-track-original-file'"
  (interactive)
  (let ((dired-buf (get-text-property (point) 'associated-dired-buffer)))
        (switch-to-buffer-other-window dired-buf)))

(defvar tumme-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map [right] 'tumme-mode-forward-char)
    (define-key map [left] 'tumme-mode-backward-char)
    (define-key map [up] 'tumme-mode-previous-line)
    (define-key map [down] 'tumme-mode-next-line)
    (define-key map "\C-f" 'tumme-mode-forward-char)
    (define-key map "\C-b" 'tumme-mode-backward-char)
    (define-key map "\C-p" 'tumme-mode-previous-line)
    (define-key map "\C-n" 'tumme-mode-next-line)
    (define-key map "d" 'tumme-mode-flag-thumb-original-file)
    (define-key map [delete] 'tumme-mode-flag-thumb-original-file)
    (define-key map "m" 'tumme-mode-mark-thumb-original-file)
    (define-key map "u" 'tumme-mode-unmark-thumb-original-file)
    (define-key map "." 'tumme-mode-track-original-file)
    (define-key map "j" 'tumme-mode-jump-thumb-original-dired-buffer)
    (define-key map "g" 'tumme-mode-line-up)
    map)
  "Keymap for `tumme-mode'.")

(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.")

(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-<return>") 'tumme-display-thumbs)
  (define-key dired-mode-map (kbd "C-S-t") 'tumme-tag-files))

(defun tumme-create-thumbs (&optional arg)
  (interactive "P")
  (let (curr-file thumb-name files count)
    (if arg
        (setq files (list (dired-get-filename)))
      (setq files (dired-get-marked-files)))
    (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"))))
     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-mode-delete-char ()
  "Remove current thumbnail from thumbnail buffer and try to be
smart."
  (interactive)
  (setq inhibit-read-only t)
  (delete-char 1)
  (if (and (looking-at "$")
           (not (eobp)))
      (delete-char 1))
  (if (eobp)
      (tumme-mode-backward-char))
  (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-mode-line-up ()
  "Line up thumbnails according to `tumme-thumbs-per-row'"
  (interactive)
  (setq inhibit-read-only t)
  (goto-char (point-min))
  (while (search-forward "\n" nil t)
    (replace-match ""))
  (goto-char (point-min))
  (let ((count 0))
    (while (not (eobp))
      (forward-char)
      (setq count (1+ count))
      (if (= count tumme-thumbs-per-row)
          (progn
            (insert "\n")
            (setq count 0)))))
  (goto-char (point-min))
  (setq inhibit-read-only nil))

(provide 'tumme)

;;; tumme.el ends here


reply via email to

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