]> code.delx.au - gnu-emacs/blobdiff - lisp/tumme.el
(unload-feature): Handle (t . SYMBOL) entries in load history.
[gnu-emacs] / lisp / tumme.el
index 0436a0cb55e124e1655174eaea8473d9a59b66dd..8d4527d8ff4d3b5f30382b17a6c93b848b33f44c 100644 (file)
@@ -1,6 +1,6 @@
 ;;; tumme.el --- use dired to browse and manipulate your images
 ;;
-;; Copyright (C) 2005, 2006 Free Software Foundation, Inc.
+;; Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
 ;;
 ;; Version: 0.4.11
 ;; Keywords: multimedia
@@ -57,8 +57,8 @@
 ;;
 ;;  `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:
+;; ~/.emacs.d/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
 ;;
 ;; USAGE
 ;; =====
 ;;
-;; If you plan to use tumme much, setting up key bindings for it in
-;; dired is a good idea:
+;; This information has been moved to the manual.  Type `C-h r' to open
+;; the Emacs manual and go to the node Thumbnails by typing `g
+;; Thumbnails RET'.
 ;;
-;;   (tumme-setup-dired-keybindings)
-;;
-;; Next, do M-x tumme-dired RET.  This will ask you for a directory
-;; where image files are stored, setup a useful window configuration
-;; and enable the two special modes that tumme provides.  NOTE: If you
-;; do not want tumme to split your windows, call it with a prefix
-;; argument.
-;;
-;; Start 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.  The thumbnail images will be created on the fly, and
-;; cached.  This means that the first time you browse your images, it
-;; will be a bit slow because the thumbnails are created.  If you want
-;; to avoid this, you can pre-create the thumbnail images by marking
-;; all images in dired (% m \.jpg$ RET) and then do M-x
-;; tumme-create-thumbs.
-;;
-;; Next, try `tumme-display-thumbs' (C-t d).  If no file is marked, a
-;; thumbnail for the file at point will show up in
-;; `tumme-thumbnail-buffer'.  If one or more files are marked,
-;; thumbnails for those files will be displayed.
-;;
-;; Pressing TAB will switch to the window containing the
-;; `tumme-thumbnail-buffer' 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-thumbnail-mode and dired-mode.
+;; Quickstart: M-x tumme RET DIRNAME RET
 ;;
+;; where DIRNAME is a directory containing image files.
 ;;
 ;; LIMITATIONS
 ;; ===========
 ;; backup of `tumme-db-file' when testing new versions.
 ;;
 ;;
-;;; 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-buffer' 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.
-;;
-;; Version 0.4.5, 2005-05-19
-;;
-;; * Added `tumme-line-up-dynamic' that calculates the number of
-;; thumbnails that will fit in the thumbnail buffer's window and
-;; `tumme-line-up-interactive' that asks the user.
-;;
-;; * Changed `tumme-display-thumbs' to call one of the `tumme-line-up'
-;; functions instead of doing the line-up itself.
-;;
-;; * Finally! Added experimental gallery creation.  See customizable
-;; variables `tumme-gallery-dir', `tumme-gallery-image-root-url' and
-;; `tumme-gallery-thumb-image-root-url' and new command
-;; `tumme-gallery-generate'.  Not beatiful, but it works quite
-;; well.  Probably needs some CSS-stuff in it eventually.  Also, I'm not
-;; sure this is the way I want to generate my image galleries in the
-;; future.  After all, static pages cannot do what dynamic pages using
-;; PHP et al can do.  Serves like a proof-of-concept of the tagging
-;; though.
-;;
-;; * Added option to hide images with certain tags.  See
-;; `tumme-gallery-hidden-tags'.
-;;
-;; * Added `tumme-tag-thumbnail' for tagging files from thumbnail
-;; buffer.
-;;
-;; * Added `tumme-tag-remove' and `tumme-tag-thumbnail-remove' so that
-;; you can remove tags.  Sorry if I have kept you waiting for
-;; this... :)
-;;
-;; * Added option `tumme-append-when-browsing' and new command
-;; `tumme-toggle-append-browsing'.
-;;
-;; Version 0.4.6, 2005-05-21
-;;
-;; * Changed `tumme-thumb-name' to always use ".jpg" as file extension
-;; for thumbnail files, instead of using the extension from the
-;; original file's name.  This was a very easy way to open up for
-;; allowing browsing of all image file types that Emacs support,
-;; assuming ImageMagick supports it too.
-;;
-;; * Fixed bug in `tumme-create-thumb' `tumme-rotate-thumbnail' and
-;; `tumme-display-image' by adding quotes around the file names.  The
-;; conversion failed if the file name, or path, contained a
-;; space.  Also expanded the file name, as convert (or is it bash?)
-;; does not work as expected for paths like "~/.tumme...".
-;;
-;; * Fixed another "space bug" :) in `tumme-display-external'.
-;;
-;; * In call to convert, added "jpeg:" in front of the output file
-;; name, so that all generated files becomes JPEG files.  For now, only
-;; useful if `tumme-temp-image-file' does not end in .jpg.
-;;
-;; Version 0.4.7, 2005-05-26
-;;
-;; * Change header line of tumme.el so that it does not wrap and cause
-;; evaluation problems for people getting the source from Usenet.
-;;
-;; * Changed `tumme-write-tag' slightly to get better performance when
-;; tagging many files.
-;;
-;; * Fixed bug in `tumme-create-gallery-lists' that made it puke if
-;; there was empty lines in the database.  Changed the code so that it
-;; does not car about that.  Also, fixed `tumme-remove-tag' so that it
-;; tries not to add empty lines at the end of the database.
-;;
-;; * Changed all commands that execute shell commands to be
-;; configurable using the `tumme-cmd-x' custom variables.  This makes
-;; it easier to switch among different image conversion tools which
-;; might use different syntax and options.
-;;
-;; * Added `tumme-toggle-dired-display-properties'.
-;;
-;; * Added `tumme-thumb-file-name-format' and changed
-;; `tumme-thumb-name' to make it possible to configure the format of
-;; thumbnail files.  Did not make it customizable yet though.  It might
-;; be a bad idea to be able to switch between formats...
-;;
-;; * Changed `tumme-display-window' so that it looks for tumme's
-;; display window in all frames.  Useful if you want to create an own
-;; frame for displaying the temporary image.
-;;
-;; * After changing the call to `get-window-with-predicate' to scan
-;; all frames for tumme's special buffers in visible windows, and also
-;; changing the way tumme tracks thumbnail movement in the dired
-;; buffer (now using `set-buffer' together with `set-window-point'),
-;; tumme now works quite happily with all three buffers in different
-;; frames.  This empowers the user to setup the special buffers the way
-;; that best fits his need at the time.  Jumping between dired and
-;; `tumme-thumbnail-buffer' work independent on in which frames they
-;; are.
-;;
-;; * Renamed `tumme-track-movement-in-dired' to
-;; `tumme-toggle-movement-tracking'.
-;;
-;; * Added `tumme-track-thumbnail' for movement tracking from dired
-;; buffer, analoguous to the tracking done in thumbnail buffer.  Both
-;; uses the same custom variable `tumme-track-movement' which can be
-;; toggled on and off with `tumme-toggle-movement-tracking'.  This is
-;; neat.  :) Changed `tumme-setup-dired-keybindings' to make use of
-;; this in the best way.  Read more about this there.
-;;
-;; Version 0.4.8, 2005-06-05
-;;
-;; * Changed `tumme-display-dired-image' and
-;; `tumme-display-thumbnail-original-image' so that when called with a
-;; prefix argument, the image is not resized in the display
-;; buffer.  This will be useful for later additions of image
-;; manipulation commands.
-;;
-;; * Added `tumme-kill-buffer-and-window' to make it easy to kill the
-;; tumme buffers.
-;;
-;; * Renamed `tumme-mode' to `tumme-thumbnail-mode'.
-;;
-;; * `tumme-tag-thumbnail' and `tumme-tag-thumbnail-remove' now
-;; updates the tags property for the thumbnail.
-;;
-;; * Added `tumme-dired-display-external' to display images in
-;; external viewer from dired buffer.
-;;
-;; * Added support for multiple files in `tumme-remove-tag' to
-;; increase performance.
-;;
-;; * Added `tumme-display-image-mode' so that we can add image
-;; manipulation commands there.
-;;
-;; * Added call to `tumme-display-thumb-properties' in
-;; `tumme-track-thumbnail'.
-;;
-;; * Added command `tumme-display-current-image-in-full-size' to be
-;; used in `tumme-display-image-mode'.
-;;
-;; * Changed `tumme-display-image' to call
-;; `tumme-create-display-image-buffer' so that we are sure that
-;; `tumme-display-image-buffer' is always available.
-;;
-;; * Added optional prefix argument to `tumme-dired-folder' that tells
-;; it to skip the window splitting and just creates the needed
-;; buffers.
-;;
-;; * Fixed bug somewhere that relied on `tumme-dired-folder' having
-;; created the `tumme-display-image-buffer'.  Now `tumme-dired-folder'
-;; *should* not be necessary to call at all, just convenient.
-;;
-;; * Added tracking to `tumme-mouse-display-image'.
-;;
-;; * Added `tumme-mouse-select-thumbnail' and bound mouse-1 to it, so
-;; that selecting a thumbnail will track the original file.
-;;
-;; * Added three new custom variables, `tumme-cmd-ACTION-program' to
-;; make the command options cleaner and easier to read.
-;;
-;; * Added `tumme-display-properties-format' and
-;; `tumme-format-properties-string' to make it possible to configure
-;; the display format of the image file's properties.
-;;
-;; * Added missing (require 'format-spec)
-;;
-;; Version 0.4.9, 2005-09-25
-;;
-;; * Fixed bug in `tumme-display-thumbs'.  If a thumbnail file could
-;; not be created for some reason (bad file for example), even if
-;; several other thumbnails was created sucessfully, the code
-;; generated an error and never continued doing the line-up.
-;;
-;; * Made tumme.el pass the M-x checkdoc test, phew!
-;;
-;; * Added `tumme-rotate-original', `tumme-rotate-original-left' and
-;; `tumme-rotate-original-right' to rotate the original image from
-;; thumbnail view. By default it uses JpegTRAN to rotate the images
-;; non-lossy. Only works on JPEG images. The two new commands were
-;; added to thumbnail mode. Thanks to Colin Marquardt who told me
-;; about the "-copy all" option to jpegtran.
-;;
-;; * Added the functions `tumme-get-exif-data' and
-;; `tumme-set-exif-data' for reading and writing EXIF data to image files.
-;;
-;; * Rewrote `tumme-get-exif-file-name': now uses
-;; `tumme-get-exif-data'. Slight change to replace spaces with
-;; underscores (tt seems not all cameras use the exact same format for
-;; DateTimeOriginal). Added code for handling files that has no
-;; EXIF-data (use file's timestamp instead).
-;;
-;; * Changed from using the exif program to exiftool because exiftool
-;; also handles writing of EXIF data, which is very useful.
-;;
-;; * Added the command `tumme-thumbnail-set-image-description' that
-;; can be used to set the EXIF tag ImageDescription. Thanks to Colin
-;; Marquardt for the suggestion.
-;;
-;; * Added `tumme-toggle-mark-thumb-original-file' and
-;; `tumme-mouse-toggle-mark' and changed
-;; `tumme-modify-mark-on-thumb-original-file' to support toggling of
-;; mark of original image file in dired, from
-;; `tumme-thumbnail-mode'. Bound C-down-mouse-1
-;; `tumme-mouse-toggle-mark' to in `tumme-thumbnail-mode'.
-;;
-;; * Changed `tumme-mouse-select-thumbnail' to also display properties
-;; after the file is selected.
-;;
-;; Version 0.4.10, 2005-11-07
-;;
-;; * Renamed `tumme-dired-folder' to `tumme-dired'.
-;;
-;; * Changed format of the database file slightly, now the full path
-;; and file name is used. Had to change most of the tag functions
-;; (writing, reading, searching) slightly to cope with the change. If
-;; you are an old tumme user, you have to update your database
-;; manually, probably you only need to prefix all rows with a
-;; directory name to get the full path and file name.
-;;
-;; * Removed `tumme-thumb-file-name-format'. Added
-;; `tumme-thumbnail-storage' and changed `tumme-thumb-name' to provide
-;; two different thumbnail storage schemes. It is no longer necessary
-;; to have unique image file names to use tumme fully.
-;;
-;; * As a consequence of the above, gallery generation is currently
-;; not supported if per-directory thumbnail file storage is used.
-;;
-;; * Changed parameters to `tumme-create-thumb'.
-;;
-;; * To be included in Emacs 22.
-;;
-;;
-;; Version 0.4.11, 2006-MM-DD
-;;
-;; * Changed `tumme-display-thumbs' so that it calls `display-buffer'
-;; after generating the thumbnails and changed
-;; `tumme-display-thumbnail-original-image' to display the image
-;; buffer. These small changes should make it easier for a user to
-;; start using tumme.
-;;
-;; * Added `tumme-show-all-from-dir' to mimic thumbs.el's easy-to-use
-;; `thumbs' command. A new customize option,
-;; `tumme-show-all-from-dir-max-files' was added too.
-;;
-;; * Renamed `tumme-dired' to `tumme-dired-with-window-configuration'
-;; and added code to save the window configuration before messing it
-;; up. The saved window configuration can be restored using the new
-;; command `tumme-restore-window-configuration'.
-;;
-;; * Added `tumme-get-thumbnail-image', created by Chong Yidong. His
-;; own comments: ..., that just takes the original filename and
-;; returns a thumbnail image descriptor.  Then third-party libraries
-;; won't have to muck around with tumme.el's internal functions like
-;; `thumme-thumb-name', `tumme-create-thumb', etc. His code to get
-;; speedbar display tumme thumbnails, might be integrated soon.
-;;
 ;; TODO
 ;; ====
 ;;
 ;; probably needs rewriting `tumme-display-thumbs' to be more general.
 ;;
 ;; * 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). Richard
+;; dired (for example, using C-n or <down> instead of C-S-n).  Richard
 ;; suggested that we could keep C-t as prefix for tumme commands as it
-;; is currently not used in dired. He also suggested that
+;; is currently not used in dired.  He also suggested that
 ;; `dired-next-line' and `dired-previous-line' figure out if tumme is
 ;; enabled in the current buffer and, if it is, call
 ;; `tumme-dired-next-line' and `tumme-dired-previous-line',
-;; respectively.
+;; respectively.  Update: This is partly done; some bindings have now
+;; been added to dired.
 ;;
 ;; * Enhanced gallery creation with basic CSS-support and pagination
 ;; of tag pages with many pictures.
 
 (require 'dired)
 (require 'format-spec)
+(require 'widget)
+
+(eval-when-compile
+  (require 'wid-edit))
 
 (defgroup tumme nil
   "Use dired to browse your images as thumbnails, and more."
   :prefix "tumme-"
-  :group 'files)
+  :group 'multimedia)
 
-(defcustom tumme-dir "~/.tumme/"
-  "*Directory where thumbnail images for are stored."
+(defcustom tumme-dir "~/.emacs.d/tumme/"
+  "Directory where thumbnail images are stored."
   :type 'string
   :group 'tumme)
 
 (defcustom tumme-thumbnail-storage 'use-tumme-dir
-  "*How to store tumme's thumbnail files.
+  "How to store tumme's thumbnail files.
 Tumme can store thumbnail files in one of two ways and this is
 controlled by this variable.  \"Use tumme dir\" means that the
 thumbnails are stored in a central directory.  \"Per directory\"
 means that each thumbnail is stored in a subdirectory called
-\".tumme\" in the same directory where the image file is."
+\".tumme\" in the same directory where the image file is.
+\"Thumbnail Managing Standard\" means that the thumbnails are
+stored and generated according to the Thumbnail Managing Standard
+that allows sharing of thumbnails across different programs."
   :type '(choice :tag "How to store thumbnail files"
+                 (const :tag "Thumbnail Managing Standard" standard)
                  (const :tag "Use tumme-dir" use-tumme-dir)
                  (const :tag "Per-directory" per-directory))
   :group 'tumme)
 
-(defcustom tumme-db-file "~/.tumme/.tumme_db"
-  "*Database file where file names and their associated tags are stored."
+(defcustom tumme-db-file "~/.emacs.d/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 used by various commands."
+(defcustom tumme-temp-image-file "~/.emacs.d/tumme/.tumme_temp"
+  "Name of temporary image file used by various commands."
   :type 'string
   :group 'tumme)
 
-(defcustom tumme-gallery-dir "~/.tumme/.tumme_gallery"
-  "*Directory to store generated gallery html pages.
+(defcustom tumme-gallery-dir "~/.emacs.d/tumme/.tumme_gallery"
+  "Directory to store generated gallery html pages.
 This path needs to be \"shared\" to the public so that it can access
 the index.html page that tumme creates."
   :type 'string
@@ -608,7 +211,7 @@ the index.html page that tumme creates."
 
 (defcustom tumme-gallery-image-root-url
 "http://your.own.server/tummepics"
-  "*URL where the full size images are to be found.
+  "URL where the full size images are to be found.
 Note that this path has to be configured in your web server.  Tumme
 expects to find pictures in this directory."
   :type 'string
@@ -616,7 +219,7 @@ expects to find pictures in this directory."
 
 (defcustom tumme-gallery-thumb-image-root-url
 "http://your.own.server/tummethumbs"
-  "*URL where the thumbnail images are to be found.
+  "URL where the thumbnail images are to be found.
 Note that this path has to be configured in your web server.  Tumme
 expects to find pictures in this directory."
   :type 'string
@@ -624,50 +227,98 @@ expects to find pictures in this directory."
 
 (defcustom tumme-cmd-create-thumbnail-program
   "convert"
-  "*Executable used to create thumbnail.
+  "Executable used to create thumbnail.
 Used together with `tumme-cmd-create-thumbnail-options'."
   :type 'string
   :group 'tumme)
 
 (defcustom tumme-cmd-create-thumbnail-options
-  "%p -size %sx%s \"%f\" -resize %sx%s +profile \"*\" jpeg:\"%t\""
-  "*Format of command used to create thumbnail image.
+  "%p -size %wx%h \"%f\" -resize %wx%h +profile \"*\" jpeg:\"%t\""
+  "Format of command used to create thumbnail image.
 Available options are %p which is replaced by
-`tumme-cmd-create-thumbnail-program', %s which is replaced by
-`tumme-thumb-size', %f which is replaced by the file name of the
-original image and %t which is replaced by the file name of the
-thumbnail file."
+`tumme-cmd-create-thumbnail-program', %w which is replaced by
+`tumme-thumb-width', %h which is replaced by `tumme-thumb-height',
+%f which is replaced by the file name of the original image and %t
+which is replaced by the file name of the thumbnail file."
   :type 'string
   :group 'tumme)
 
 (defcustom tumme-cmd-create-temp-image-program
   "convert"
-  "*Executable used to create temporary image.
+  "Executable used to create temporary image.
 Used together with `tumme-cmd-create-temp-image-options'."
   :type 'string
   :group 'tumme)
 
 (defcustom tumme-cmd-create-temp-image-options
-  "%p -size %xx%y \"%f\" -resize %xx%y +profile \"*\" jpeg:\"%t\""
-  "*Format of command used to create temporary image for display window.
+  "%p -size %wx%h \"%f\" -resize %wx%h +profile \"*\" jpeg:\"%t\""
+  "Format of command used to create temporary image for display window.
 Available options are %p which is replaced by
-`tumme-cmd-create-temp-image-program', %x and %y which is replaced by
-the calculated max size for x and y in the image display window, %f
-which is replaced by the file name of the original image and %t which
+`tumme-cmd-create-temp-image-program', %w and %h which is replaced by
+the calculated max size for width and height in the image display window,
+%f which is replaced by the file name of the original image and %t which
 is replaced by the file name of the temporary file."
   :type 'string
   :group 'tumme)
 
+(defcustom tumme-cmd-pngnq-program (executable-find "pngnq")
+  "The file name of the `pngnq' program.
+It quantizes colors of PNG images down to 256 colors."
+  :type '(choice (const :tag "Not Set" nil) string)
+  :group 'tumme)
+
+(defcustom tumme-cmd-pngcrush-program (executable-find "pngcrush")
+  "The file name of the `pngcrush' program.
+It optimizes the compression of PNG images.  Also it adds PNG textual chunks
+with the information required by the Thumbnail Managing Standard."
+  :type '(choice (const :tag "Not Set" nil) string)
+  :group 'tumme)
+
+(defcustom tumme-cmd-create-standard-thumbnail-command
+  (concat
+   tumme-cmd-create-thumbnail-program " "
+   "-size %wx%h \"%f\" "
+   (unless (or tumme-cmd-pngcrush-program tumme-cmd-pngnq-program)
+     (concat
+      "-set \"Thumb::MTime\" \"%m\" "
+      "-set \"Thumb::URI\" \"file://%f\" "
+      "-set \"Description\" \"Thumbnail of file://%f\" "
+      "-set \"Software\" \"" (emacs-version) "\" "))
+   "-thumbnail %wx%h png:\"%t\""
+   (if tumme-cmd-pngnq-program
+       (concat
+        " ; " tumme-cmd-pngnq-program " -f \"%t\""
+        (unless tumme-cmd-pngcrush-program
+          " ; mv %q %t")))
+   (if tumme-cmd-pngcrush-program
+       (concat
+        (unless tumme-cmd-pngcrush-program
+          " ; cp %t %q")
+        " ; " tumme-cmd-pngcrush-program " -q "
+        "-text b \"Description\" \"Thumbnail of file://%f\" "
+        "-text b \"Software\" \"" (emacs-version) "\" "
+        ;; "-text b \"Thumb::Image::Height\" \"%oh\" "
+        ;; "-text b \"Thumb::Image::Mimetype\" \"%mime\" "
+        ;; "-text b \"Thumb::Image::Width\" \"%ow\" "
+        "-text b \"Thumb::MTime\" \"%m\" "
+        ;; "-text b \"Thumb::Size\" \"%b\" "
+        "-text b \"Thumb::URI\" \"file://%f\" "
+        "%q %t"
+        " ; rm %q")))
+  "Command to create thumbnails according to the Thumbnail Managing Standard."
+  :type 'string
+  :group 'tumme)
+
 (defcustom tumme-cmd-rotate-thumbnail-program
   "mogrify"
-  "*Executable used to rotate thumbnail.
+  "Executable used to rotate thumbnail.
 Used together with `tumme-cmd-rotate-thumbnail-options'."
   :type 'string
   :group 'tumme)
 
 (defcustom tumme-cmd-rotate-thumbnail-options
   "%p -rotate %d \"%t\""
-  "*Format of command used to rotate thumbnail image.
+  "Format of command used to rotate thumbnail image.
 Available options are %p which is replaced by
 `tumme-cmd-rotate-thumbnail-program', %d which is replaced by the
 number of (positive) degrees to rotate the image, normally 90 or 270
@@ -678,14 +329,14 @@ of the thumbnail file."
 
 (defcustom tumme-cmd-rotate-original-program
   "jpegtran"
-  "*Executable used to rotate original image.
+  "Executable used to rotate original image.
 Used together with `tumme-cmd-rotate-original-options'."
   :type 'string
   :group 'tumme)
 
 (defcustom tumme-cmd-rotate-original-options
-  "%p -rotate %d -copy all \"%o\" > %t"
-  "*Format of command used to rotate original image.
+  "%p -rotate %d -copy all -outfile %t \"%o\""
+  "Format of command used to rotate original image.
 Available options are %p which is replaced by
 `tumme-cmd-rotate-original-program', %d which is replaced by the
 number of (positive) degrees to rotate the image, normally 90 or
@@ -696,8 +347,8 @@ original image file name and %t which is replaced by
   :group 'tumme)
 
 (defcustom tumme-temp-rotate-image-file
-  "~/.tumme/.tumme_rotate_temp"
-  "*Temporary file for rotate operations."
+  "~/.emacs.d/tumme/.tumme_rotate_temp"
+  "Temporary file for rotate operations."
   :type 'string
   :group 'tumme)
 
@@ -710,14 +361,14 @@ original file with `tumme-temp-rotate-image-file'."
 
 (defcustom tumme-cmd-write-exif-data-program
   "exiftool"
-  "*Program used to write EXIF data to image.
+  "Program used to write EXIF data to image.
 Used together with `tumme-cmd-write-exif-data-options'."
   :type 'string
   :group 'tumme)
 
 (defcustom tumme-cmd-write-exif-data-options
   "%p -%t=\"%v\" \"%f\""
-  "*Format of command used to write EXIF data.
+  "Format of command used to write EXIF data.
 Available options are %p which is replaced by
 `tumme-cmd-write-exif-data-program', %f which is replaced by the
 image file name, %t which is replaced by the tag name and %v
@@ -727,14 +378,14 @@ which is replaced by the tag value."
 
 (defcustom tumme-cmd-read-exif-data-program
   "exiftool"
-  "*Program used to read EXIF data to image.
+  "Program used to read EXIF data to image.
 Used together with `tumme-cmd-read-exif-data-program-options'."
   :type 'string
   :group 'tumme)
 
 (defcustom tumme-cmd-read-exif-data-options
   "%p -s -s -s -%t \"%f\""
-  "*Format of command used to read EXIF data.
+  "Format of command used to read EXIF data.
 Available options are %p which is replaced by
 `tumme-cmd-write-exif-data-options', %f which is replaced
 by the image file name and %t which is replaced by the tag name."
@@ -743,29 +394,40 @@ by the image file name and %t which is replaced by the tag name."
 
 (defcustom tumme-gallery-hidden-tags
   (list "private" "hidden" "pending")
-  "*List of \"hidden\" tags.
+  "List of \"hidden\" tags.
 Used by `tumme-gallery-generate' to leave out \"hidden\" images."
   :type '(repeat string)
   :group 'tumme)
 
-(defcustom tumme-thumb-size 100
-  "Size of thumbnails, in pixels."
+(defcustom tumme-thumb-size (if (eq 'standard tumme-thumbnail-storage) 128 100)
+  "Size of thumbnails, in pixels.
+This is the default size for both `tumme-thumb-width' and `tumme-thumb-height'."
+  :type 'integer
+  :group 'tumme)
+
+(defcustom tumme-thumb-width tumme-thumb-size
+  "Width of thumbnails, in pixels."
+  :type 'integer
+  :group 'tumme)
+
+(defcustom tumme-thumb-height tumme-thumb-size
+  "Height of thumbnails, in pixels."
   :type 'integer
   :group 'tumme)
 
 (defcustom tumme-thumb-relief 2
-  "*Size of button-like border around thumbnails."
+  "Size of button-like border around thumbnails."
   :type 'integer
   :group 'tumme)
 
 (defcustom tumme-thumb-margin 2
-  "*Size of the margin around thumbnails.
+  "Size of the margin around thumbnails.
 This is where you see the cursor."
   :type 'integer
   :group 'tumme)
 
 (defcustom tumme-line-up-method 'dynamic
-  "*Default method for line-up of thumbnails in thumbnail buffer.
+  "Default method for line-up of thumbnails in thumbnail buffer.
 Used by `tumme-display-thumbs' and other functions that needs to
 line-up thumbnails.  Dynamic means to use the available width of the
 window containing the thumbnail buffer, Fixed means to use
@@ -779,25 +441,25 @@ line-up means that no automatic line-up will be done."
   :group 'tumme)
 
 (defcustom tumme-thumbs-per-row 3
-  "*Number of thumbnails to display per row in thumb buffer."
+  "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 width.
+  "Number to be used to correct image display window width.
 Change 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.
+  "Number to be used to correct image display window height.
 Change 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
+(defcustom tumme-track-movement t
   "The current state of the tracking and mirroring.
 For more information, see the documentation for
 `tumme-toggle-movement-tracking'."
@@ -825,32 +487,47 @@ dired and you might want to turn it off."
   :group 'tumme)
 
 (defcustom tumme-display-properties-format "%b: %f (%t): %c"
-  "*Display format for thumbnail properties.
+  "Display format for thumbnail properties.
 %b is replaced with associated dired buffer name, %f with file name
 \(without path) of original image file, %t with the list of tags and %c
 with the comment."
   :type 'string
   :group 'tumme)
 
-(defcustom tumme-external-viewer "qiv -t"
-  "*Name of external viewer.
+(defcustom tumme-external-viewer
+  ;; TODO: Use mailcap, dired-guess-shell-alist-default,
+  ;; dired-view-command-alist.
+  (cond ((executable-find "display"))
+        ((executable-find "xli"))
+        ((executable-find "qiv") "qiv -t"))
+  "Name of external viewer.
 Including parameters.  Used when displaying original image from
 `tumme-thumbnail-mode'."
   :type 'string
   :group 'tumme)
 
 (defcustom tumme-main-image-directory "~/pics/"
-  "*Name of main image directory, if any.
+  "Name of main image directory, if any.
 Used by `tumme-copy-with-exif-file-name'."
   :type 'string
   :group 'tumme)
 
 (defcustom tumme-show-all-from-dir-max-files 50
-  "*Maximum number of files to show using`tumme-show-all-from-dir'.
+  "Maximum number of files to show using `tumme-show-all-from-dir'.
 before warning the user."
   :type 'integer
   :group 'tumme)
 
+(defun tumme-dir ()
+  "Return the current thumbnails directory (from variable `tumme-dir').
+Create the thumbnails directory if it does not exist."
+  (let ((tumme-dir (file-name-as-directory
+                    (expand-file-name tumme-dir))))
+    (unless (file-directory-p tumme-dir)
+      (make-directory tumme-dir t)
+      (message "Creating thumbnails directory"))
+    tumme-dir))
+
 (defun tumme-insert-image (file type relief margin)
   "Insert image FILE of image TYPE, using RELIEF and MARGIN, at point."
 
@@ -869,8 +546,11 @@ before warning the user."
                 (<= (float-time (nth 5 (file-attributes file)))
                     (float-time (nth 5 (file-attributes thumb-file)))))
       (tumme-create-thumb file thumb-file))
-    (list 'image :type 'jpeg :file thumb-file
-         :relief tumme-thumb-relief :margin tumme-thumb-margin)))
+    (create-image thumb-file)
+;;     (list 'image :type 'jpeg
+;;           :file thumb-file
+;;       :relief tumme-thumb-relief :margin tumme-thumb-margin)
+    ))
 
 (defun tumme-insert-thumbnail (file original-file-name
                                     associated-dired-buffer)
@@ -879,7 +559,9 @@ Add text properties ORIGINAL-FILE-NAME and ASSOCIATED-DIRED-BUFFER."
   (let (beg end)
     (setq beg (point))
     (tumme-insert-image file
-                        'jpeg
+                        ;; TODO: this should depend on the real file type
+                        (if (eq 'standard tumme-thumbnail-storage)
+                            'png 'jpeg)
                         tumme-thumb-relief
                         tumme-thumb-margin)
     (setq end (point))
@@ -898,52 +580,102 @@ Depending on the value of `tumme-thumbnail-storage', the file
 name will vary.  For central thumbnail file storage, make a
 MD5-hash of the image file's directory name and add that to make
 the thumbnail file name unique.  For per-directory storage, just
-add a subdirectory."
-  (let ((f (expand-file-name file))
-        md5-hash)
-    (format "%s%s%s.thumb.%s"
-            (cond ((eq 'use-tumme-dir tumme-thumbnail-storage)
-                   ;; Is MD5 hashes fast enough? The checksum of a
-                   ;; thumbnail file name need not be that
-                   ;; "cryptographically" good so a faster one could
-                   ;; be used here.
-                   (setq md5-hash (md5 (file-name-as-directory
-                                        (file-name-directory file))))
-                   (file-name-as-directory (expand-file-name tumme-dir)))
-                  ((eq 'per-directory tumme-thumbnail-storage)
-                   (format "%s.tumme/"
-                           (file-name-directory f))))
-            (file-name-sans-extension
-             (file-name-nondirectory f))
-            (if md5-hash
-                (concat "_" md5-hash)
-              "")
-            (file-name-extension f))))
+add a subdirectory.  For standard storage, produce the file name
+according to the Thumbnail Managing Standard."
+  (cond ((eq 'standard tumme-thumbnail-storage)
+         (expand-file-name
+          (concat "~/.thumbnails/normal/"
+                  (md5 (concat "file://" (expand-file-name file))) ".png")))
+        ((eq 'use-tumme-dir tumme-thumbnail-storage)
+         (let* ((f (expand-file-name file))
+                (md5-hash
+                 ;; Is MD5 hashes fast enough? The checksum of a
+                 ;; thumbnail file name need not be that
+                 ;; "cryptographically" good so a faster one could
+                 ;; be used here.
+                 (md5 (file-name-as-directory (file-name-directory f)))))
+           (format "%s%s%s.thumb.%s"
+                   (file-name-as-directory (expand-file-name (tumme-dir)))
+                   (file-name-sans-extension (file-name-nondirectory f))
+                   (if md5-hash (concat "_" md5-hash) "")
+                   (file-name-extension f))))
+        ((eq 'per-directory tumme-thumbnail-storage)
+         (let ((f (expand-file-name file)))
+           (format "%s.tumme/%s.thumb.%s"
+                   (file-name-directory f)
+                   (file-name-sans-extension (file-name-nondirectory f))
+                   (file-name-extension f))))))
 
 (defun tumme-create-thumb (original-file thumbnail-file)
   "For ORIGINAL-FILE, create thumbnail image named THUMBNAIL-FILE."
-  (let* ((size (int-to-string tumme-thumb-size))
+  (let* ((width (int-to-string tumme-thumb-width))
+         (height (int-to-string tumme-thumb-height))
+         (modif-time (format "%.0f" (float-time (nth 5 (file-attributes
+                                                        original-file)))))
+         (thumbnail-nq8-file (replace-regexp-in-string ".png\\'" "-nq8.png"
+                                                       thumbnail-file))
          (command
           (format-spec
-           tumme-cmd-create-thumbnail-options
+           (if (eq 'standard tumme-thumbnail-storage)
+               tumme-cmd-create-standard-thumbnail-command
+             tumme-cmd-create-thumbnail-options)
            (list
             (cons ?p tumme-cmd-create-thumbnail-program)
-            (cons ?s size)
+            (cons ?w width)
+            (cons ?h height)
+            (cons ?m modif-time)
             (cons ?f original-file)
+            (cons ?q thumbnail-nq8-file)
             (cons ?t thumbnail-file))))
          thumbnail-dir)
     (when (not (file-exists-p
                 (setq thumbnail-dir (file-name-directory thumbnail-file))))
       (message "Creating thumbnail directory.")
       (make-directory thumbnail-dir))
-    (shell-command command nil)))
+    (call-process shell-file-name nil nil nil shell-command-switch command)))
+
+;;;###autoload
+(defun tumme-dired-insert-marked-thumbs ()
+  "Insert thumbnails before file names of marked files in the dired buffer."
+  (interactive)
+  (dired-map-over-marks
+   (let* ((image-pos (dired-move-to-filename))
+          (image-file (dired-get-filename))
+          (thumb-file (tumme-get-thumbnail-image image-file))
+          overlay)
+     ;; If image is not already added, then add it.
+     (unless (delq nil (mapcar (lambda (o) (overlay-get o 'put-image))
+                               ;; Can't use (overlays-at (point)), BUG?
+                               (overlays-in (point) (1+ (point)))))
+       (put-image thumb-file image-pos)
+       (setq
+       overlay
+       (car (delq nil (mapcar (lambda (o) (and (overlay-get o 'put-image) o))
+                              (overlays-in (point) (1+ (point)))))))
+       (overlay-put overlay 'image-file image-file)
+       (overlay-put overlay 'thumb-file thumb-file)))
+   nil)
+  (add-hook 'dired-after-readin-hook 'tumme-dired-after-readin-hook nil t))
+
+(defun tumme-dired-after-readin-hook ()
+  "Relocate existing thumbnail overlays in dired buffer after reverting.
+Move them to their corresponding files if they are still exist.
+Otherwise, delete overlays."
+  (mapc (lambda (overlay)
+          (when (overlay-get overlay 'put-image)
+            (let* ((image-file (overlay-get overlay 'image-file))
+                   (image-pos (dired-goto-file image-file)))
+              (if image-pos
+                  (move-overlay overlay image-pos image-pos)
+                (delete-overlay overlay)))))
+        (overlays-in (point-min) (point-max))))
 
 (defun tumme-next-line-and-display ()
   "Move to next dired line and display thumbnail image."
   (interactive)
   (dired-next-line 1)
   (tumme-display-thumbs
-   t (or tumme-append-when-browsing nil))
+   t (or tumme-append-when-browsing nil) t)
   (if tumme-dired-disp-props
       (tumme-dired-display-properties)))
 
@@ -952,7 +684,7 @@ add a subdirectory."
   (interactive)
   (dired-previous-line 1)
   (tumme-display-thumbs
-   t (or tumme-append-when-browsing nil))
+   t (or tumme-append-when-browsing nil) t)
   (if tumme-dired-disp-props
       (tumme-dired-display-properties)))
 
@@ -971,7 +703,7 @@ add a subdirectory."
   (interactive)
   (dired-mark 1)
   (tumme-display-thumbs
-   t (or tumme-append-when-browsing nil))
+   t (or tumme-append-when-browsing nil) t)
   (if tumme-dired-disp-props
       (tumme-dired-display-properties)))
 
@@ -991,8 +723,7 @@ add a subdirectory."
 (defun tumme-create-thumbnail-buffer ()
   "Create thumb buffer and set `tumme-thumbnail-mode'."
   (let ((buf (get-buffer-create tumme-thumbnail-buffer)))
-    (save-excursion
-      (set-buffer buf)
+    (with-current-buffer buf
       (setq buffer-read-only t)
       (if (not (eq major-mode 'tumme-thumbnail-mode))
           (tumme-thumbnail-mode)))
@@ -1004,8 +735,7 @@ add a subdirectory."
 (defun tumme-create-display-image-buffer ()
   "Create image display buffer and set `tumme-display-image-mode'."
   (let ((buf (get-buffer-create tumme-display-image-buffer)))
-    (save-excursion
-      (set-buffer buf)
+    (with-current-buffer buf
       (setq buffer-read-only t)
       (if (not (eq major-mode 'tumme-display-image-mode))
           (tumme-display-image-mode)))
@@ -1060,7 +790,7 @@ Restore any changes to the window configuration made by calling
     (message "No saved window configuration")))
 
 ;;;###autoload
-(defun tumme-display-thumbs (&optional arg append)
+(defun tumme-display-thumbs (&optional arg append do-not-pop)
   "Display thumbnails of all marked files, in `tumme-thumbnail-buffer'.
 If a thumbnail image does not exist for a file, it is created on the
 fly.  With prefix argument ARG, display only thumbnail for file at
@@ -1072,7 +802,14 @@ you have the dired buffer in the left window and the
 `tumme-thumbnail-buffer' buffer in the right window.
 
 With optional argument APPEND, append thumbnail to thumbnail buffer
-instead of erasing it first."
+instead of erasing it first.
+
+Option argument DO-NOT-POP controls if `pop-to-buffer' should be
+used or not.  If non-nil, use `display-buffer' instead of
+`pop-to-buffer'.  This is used from functions like
+`tumme-next-line-and-display' and
+`tumme-previous-line-and-display' where we do not want the
+thumbnail buffer to be selected."
   (interactive "P")
   (let ((buf (tumme-create-thumbnail-buffer))
         curr-file thumb-name files count dired-buf beg)
@@ -1080,8 +817,7 @@ instead of erasing it first."
         (setq files (list (dired-get-filename)))
       (setq files (dired-get-marked-files)))
     (setq dired-buf (current-buffer))
-    (save-excursion
-      (set-buffer buf)
+    (with-current-buffer buf
       (let ((inhibit-read-only t))
         (if (not append)
             (erase-buffer)
@@ -1104,8 +840,11 @@ instead of erasing it first."
              nil)
             (t
              (tumme-line-up-dynamic))))
-    (pop-to-buffer tumme-thumbnail-buffer)))
+    (if do-not-pop
+        (display-buffer tumme-thumbnail-buffer)
+      (pop-to-buffer tumme-thumbnail-buffer))))
 
+;;;###autoload
 (defun tumme-show-all-from-dir (dir)
   "Make a preview buffer for all images in DIR and display it.
 If the number of files in DIR matching `image-file-name-regexp'
@@ -1129,33 +868,27 @@ displayed."
 ;;;###autoload
 (defalias 'tumme 'tumme-show-all-from-dir)
 
-(defun tumme-write-tag (files tag)
-  "For all FILES, writes TAG to the image database."
-  (save-excursion
-    (let (end buf)
-      (setq buf (find-file tumme-db-file))
-      (if (not (listp files))
-          (if (stringp files)
-              (setq files (list files))
-            (error "Files must be a string or a list of strings!")))
-      (mapcar
-       (lambda (file)
-         (goto-char (point-min))
-         (if (search-forward-regexp
-              (format "^%s" 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 tag))))
-       files)
-      (save-buffer)
-      (kill-buffer buf))))
+(defun tumme-write-tags (file-tags)
+  "Write file tags to database.
+Write each file and tag in FILE-TAGS to the database.  FILE-TAGS
+is an alist in the following form:
+ ((FILE . TAG) ... )"
+  (let (end file tag)
+    (with-temp-file tumme-db-file
+      (insert-file-contents tumme-db-file)
+      (dolist (elt file-tags)
+       (setq file (car elt)
+             tag (cdr elt))
+       (goto-char (point-min))
+       (if (search-forward-regexp (format "^%s.*$" file) nil t)
+           (progn
+             (setq end (point))
+             (beginning-of-line)
+             (when (not (search-forward (format ";%s" tag) end t))
+               (end-of-line)
+               (insert (format ";%s" tag))))
+         (goto-char (point-max))
+         (insert (format "\n%s;%s" file tag)))))))
 
 (defun tumme-remove-tag (files tag)
   "For all FILES, remove TAG from the image database."
@@ -1169,27 +902,24 @@ displayed."
       (mapcar
        (lambda (file)
          (goto-char (point-min))
-         (if (search-forward-regexp
-              (format "^%s" file) nil t)
-             (progn
-               (end-of-line)
-               (setq end (point))
-               (beginning-of-line)
-               (if (search-forward-regexp (format "\\(;%s\\)" tag) end t)
-                   (progn
-                     (delete-region (match-beginning 1) (match-end 1))
-                     ;; Check if file should still be in the database. If
-                     ;; it has no tags or comments, it will be removed.
-                     (end-of-line)
-                     (setq end (point))
-                     (beginning-of-line)
-                     (if (not (search-forward ";" end t))
-                         (progn
-                           (kill-line 1)
-                           ;; If on empty line at end of buffer
-                           (if (and (eobp)
-                                    (looking-at "^$"))
-                               (delete-backward-char 1)))))))))
+         (when (search-forward-regexp
+                (format "^%s" file) nil t)
+           (end-of-line)
+           (setq end (point))
+           (beginning-of-line)
+           (when (search-forward-regexp (format "\\(;%s\\)" tag) end t)
+             (delete-region (match-beginning 1) (match-end 1))
+             ;; Check if file should still be in the database. If
+             ;; it has no tags or comments, it will be removed.
+             (end-of-line)
+             (setq end (point))
+             (beginning-of-line)
+             (when (not (search-forward ";" end t))
+               (kill-line 1)
+               ;; If on empty line at end of buffer
+               (when (and (eobp)
+                          (looking-at "^$"))
+                 (delete-backward-char 1))))))
        files)
       (save-buffer)
       (kill-buffer buf))))
@@ -1200,17 +930,16 @@ displayed."
     (let (end buf (tags ""))
       (setq buf (find-file tumme-db-file))
       (goto-char (point-min))
-      (if (search-forward-regexp
-           (format "^%s" 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))))))
+      (when (search-forward-regexp
+             (format "^%s" file) nil t)
+        (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 ";"))))
 
@@ -1218,23 +947,27 @@ displayed."
 (defun tumme-tag-files (arg)
   "Tag marked file(s) in dired.  With prefix ARG, tag file at point."
   (interactive "P")
-  (let ((tag (read-string "Tag to add: "))
+  (let ((tag (read-string "Tags to add (separate tags with a semicolon): "))
         curr-file files)
     (if arg
-        (setq files (dired-get-filename))
+        (setq files (list (dired-get-filename)))
       (setq files (dired-get-marked-files)))
-    (tumme-write-tag files tag)))
+    (tumme-write-tags
+     (mapcar
+      (lambda (x)
+        (cons x tag))
+      files))))
 
 (defun tumme-tag-thumbnail ()
   "Tag current thumbnail."
   (interactive)
-  (let ((tag (read-string "Tag to add: ")))
-    (tumme-write-tag (tumme-original-file-name) tag))
+  (let ((tag (read-string "Tags to add (separate tags with a semicolon): ")))
+    (tumme-write-tags (list (cons (tumme-original-file-name) tag))))
   (tumme-update-property
    'tags (tumme-list-tags (tumme-original-file-name))))
 
 ;;;###autoload
-(defun tumme-tag-remove (arg)
+(defun tumme-delete-tag (arg)
   "Remove tag for selected file(s).
 With prefix argument ARG, remove tag from file at point."
   (interactive "P")
@@ -1276,17 +1009,16 @@ use only useful if `tumme-track-movement' is nil."
   (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))
-          (set-buffer dired-buf)
-          (goto-char (point-min))
-          (if (not (search-forward file-name nil t))
-              (message "Could not track file")
-            (dired-move-to-filename)
-            (set-window-point
-             (tumme-get-buffer-window dired-buf) (point)))
-          (set-buffer old-buf)))))
+    (when (and (buffer-live-p dired-buf) file-name)
+      (setq file-name (file-name-nondirectory file-name))
+      (set-buffer dired-buf)
+      (goto-char (point-min))
+      (if (not (search-forward file-name nil t))
+          (message "Could not track file")
+        (dired-move-to-filename)
+        (set-window-point
+         (tumme-get-buffer-window dired-buf) (point)))
+      (set-buffer old-buf))))
 
 (defun tumme-toggle-movement-tracking ()
   "Turn on and off `tumme-track-movement'.
@@ -1305,24 +1037,22 @@ the other way around."
   (let ((file (dired-get-filename))
         (old-buf (current-buffer))
         prop-val found)
-    (if (get-buffer tumme-thumbnail-buffer)
-        (progn
-          (set-buffer tumme-thumbnail-buffer)
-          (goto-char (point-min))
-          (while (and (not (eobp))
-                      (not found))
-            (if (and (setq prop-val
-                           (get-text-property (point) 'original-file-name))
-                     (string= prop-val file))
-                (setq found t))
-            (if (not found)
-                (forward-char 1)))
-          (if found
-              (progn
-                (set-window-point
-                 (tumme-thumbnail-window) (point))
-                (tumme-display-thumb-properties)))
-          (set-buffer old-buf)))))
+    (when (get-buffer tumme-thumbnail-buffer)
+      (set-buffer tumme-thumbnail-buffer)
+      (goto-char (point-min))
+      (while (and (not (eobp))
+                  (not found))
+        (if (and (setq prop-val
+                       (get-text-property (point) 'original-file-name))
+                 (string= prop-val file))
+            (setq found t))
+        (if (not found)
+            (forward-char 1)))
+      (when found
+        (set-window-point
+         (tumme-thumbnail-window) (point))
+        (tumme-display-thumb-properties))
+      (set-buffer old-buf))))
 
 (defun tumme-dired-next-line (&optional arg)
   "Call `dired-next-line', then track thumbnail.
@@ -1342,34 +1072,46 @@ move ARG lines."
   (if tumme-track-movement
       (tumme-track-thumbnail)))
 
-(defun tumme-forward-char ()
-  "Move to next image and display properties."
-  (interactive)
-  ;; Before we move, make sure that there is an image two positions
-  ;; forward.
-  (if (save-excursion
-        (forward-char 2)
-        (tumme-image-at-point-p))
-      (progn
-        (forward-char)
-        (while (and (not (eobp))
-                    (not (tumme-image-at-point-p)))
-          (forward-char))
-        (if tumme-track-movement
-            (tumme-track-original-file))))
+(defun tumme-forward-image (&optional arg)
+  "Move to next image and display properties.
+Optional prefix ARG says how many images to move; default is one
+image."
+  (interactive "p")
+  (let (pos (steps (or arg 1)))
+    (dotimes (i steps)
+      (if (and (not (eobp))
+               (save-excursion
+                 (forward-char)
+                 (while (and (not (eobp))
+                             (not (tumme-image-at-point-p)))
+                   (forward-char))
+                 (setq pos (point))
+                 (tumme-image-at-point-p)))
+          (goto-char pos)
+        (error "At last image"))))
+  (when tumme-track-movement
+    (tumme-track-original-file))
   (tumme-display-thumb-properties))
 
-(defun tumme-backward-char ()
-  "Move to previous image and display properties."
-  (interactive)
-  (if (not (bobp))
-      (progn
-        (backward-char)
-        (while (and (not (bobp))
-                    (not (tumme-image-at-point-p)))
-          (backward-char))
-        (if tumme-track-movement
-            (tumme-track-original-file))))
+(defun tumme-backward-image (&optional arg)
+  "Move to previous image and display properties.
+Optional prefix ARG says how many images to move; default is one
+image."
+  (interactive "p")
+  (let (pos (steps (or arg 1)))
+    (dotimes (i steps)
+      (if (and (not (bobp))
+               (save-excursion
+                 (backward-char)
+                 (while (and (not (bobp))
+                             (not (tumme-image-at-point-p)))
+                   (backward-char))
+                 (setq pos (point))
+                 (tumme-image-at-point-p)))
+          (goto-char pos)
+        (error "At first image"))))
+  (when tumme-track-movement
+    (tumme-track-original-file))
   (tumme-display-thumb-properties))
 
 (defun tumme-next-line ()
@@ -1378,7 +1120,7 @@ move ARG lines."
   (next-line 1)
   ;; If we end up in an empty spot, back up to the next thumbnail.
   (if (not (tumme-image-at-point-p))
-      (tumme-backward-char))
+      (tumme-backward-image))
   (if tumme-track-movement
       (tumme-track-original-file))
   (tumme-display-thumb-properties))
@@ -1393,7 +1135,7 @@ move ARG lines."
   ;; thumbnail and did not refresh, so it is not very common. But we
   ;; can handle it in a good manner, so why not?
   (if (not (tumme-image-at-point-p))
-      (tumme-backward-char))
+      (tumme-backward-image))
   (if tumme-track-movement
       (tumme-track-original-file))
   (tumme-display-thumb-properties))
@@ -1406,7 +1148,7 @@ comment."
   (format-spec
    tumme-display-properties-format
    (list
-    (cons ?b buf)
+    (cons ?b (or buf ""))
     (cons ?f file)
     (cons ?t (or (princ props) ""))
     (cons ?c (or comment "")))))
@@ -1445,10 +1187,9 @@ dired."
         (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
+    (with-current-buffer dired-buf
         (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))
@@ -1463,19 +1204,19 @@ dired."
   "Mark original image file in associated dired buffer."
   (interactive)
   (tumme-modify-mark-on-thumb-original-file 'mark)
-  (tumme-forward-char))
+  (tumme-forward-image))
 
 (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))
+  (tumme-forward-image))
 
 (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))
+  (tumme-forward-image))
 
 (defun tumme-toggle-mark-thumb-original-file ()
   "Toggle mark on original image file in associated dired buffer."
@@ -1523,12 +1264,12 @@ You probably want to use this together with
   "Define keymap for `tumme-thumbnail-mode'."
 
   ;; Keys
-  (define-key tumme-thumbnail-mode-map [right] 'tumme-forward-char)
-  (define-key tumme-thumbnail-mode-map [left] 'tumme-backward-char)
+  (define-key tumme-thumbnail-mode-map [right] 'tumme-forward-image)
+  (define-key tumme-thumbnail-mode-map [left] 'tumme-backward-image)
   (define-key tumme-thumbnail-mode-map [up] 'tumme-previous-line)
   (define-key tumme-thumbnail-mode-map [down] 'tumme-next-line)
-  (define-key tumme-thumbnail-mode-map "\C-f" 'tumme-forward-char)
-  (define-key tumme-thumbnail-mode-map "\C-b" 'tumme-backward-char)
+  (define-key tumme-thumbnail-mode-map "\C-f" 'tumme-forward-image)
+  (define-key tumme-thumbnail-mode-map "\C-b" 'tumme-backward-image)
   (define-key tumme-thumbnail-mode-map "\C-p" 'tumme-previous-line)
   (define-key tumme-thumbnail-mode-map "\C-n" 'tumme-next-line)
 
@@ -1569,7 +1310,8 @@ You probably want to use this together with
   (define-key tumme-thumbnail-mode-map "L" 'tumme-rotate-original-left)
   (define-key tumme-thumbnail-mode-map "R" 'tumme-rotate-original-right)
 
-  (define-key tumme-thumbnail-mode-map "D" 'tumme-thumbnail-set-image-description)
+  (define-key tumme-thumbnail-mode-map "D"
+    'tumme-thumbnail-set-image-description)
 
   (define-key tumme-thumbnail-mode-map "\C-d" 'tumme-delete-char)
   (define-key tumme-thumbnail-mode-map " "
@@ -1757,9 +1499,9 @@ Note that n, p and <down> and <up> will be hijacked and bound to
 
   (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 "\C-tr" 'tumme-tag-remove)
+  (define-key dired-mode-map "\C-tr" 'tumme-delete-tag)
   (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-ti" 'tumme-dired-display-image)
   (define-key dired-mode-map "\C-tx" 'tumme-dired-display-external)
   (define-key dired-mode-map "\C-ta" 'tumme-display-thumbs-append)
   (define-key dired-mode-map "\C-t." 'tumme-display-thumb)
@@ -1779,8 +1521,8 @@ Note that n, p and <down> and <up> will be hijacked and bound to
   (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-remove]
-    '("Remove tag from files" . tumme-tag-remove))
+  (define-key dired-mode-map [menu-bar tumme tumme-delete-tag]
+    '("Remove tag from files" . tumme-delete-tag))
 
   (define-key dired-mode-map [menu-bar tumme tumme-tag-files]
     '("Tag files" . tumme-tag-files))
@@ -1803,8 +1545,8 @@ Note that n, p and <down> and <up> will be hijacked and bound to
     [menu-bar tumme tumme-dired-display-external]
     '("Display in external viewer" . tumme-dired-display-external))
   (define-key dired-mode-map
-    [menu-bar tumme tumme-display-dired-image]
-    '("Display image" . tumme-display-dired-image))
+    [menu-bar tumme tumme-dired-display-image]
+    '("Display image" . tumme-dired-display-image))
   (define-key dired-mode-map
     [menu-bar tumme tumme-display-thumb]
     '("Display this thumbnail" . tumme-display-thumb))
@@ -1900,13 +1642,13 @@ Ask user for number of images to show and the delay in between."
 (defun tumme-display-thumbs-append ()
   "Append thumbnails to `tumme-thumbnail-buffer'."
   (interactive)
-  (tumme-display-thumbs nil t))
+  (tumme-display-thumbs nil t t))
 
 ;;;###autoload
 (defun tumme-display-thumb ()
-  "Shorthard for `tumme-display-thumbs' with prefix argument."
+  "Shorthand for `tumme-display-thumbs' with prefix argument."
   (interactive)
-  (tumme-display-thumbs t))
+  (tumme-display-thumbs t nil t))
 
 (defun tumme-line-up ()
   "Line up thumbnails according to `tumme-thumbs-per-row'.
@@ -1930,11 +1672,11 @@ See also `tumme-line-up-dynamic'."
             (insert "\n")
           (insert " ")
           (setq count (1+ count))
-          (if (= count (- tumme-thumbs-per-row 1))
-              (progn
-                (forward-char)
-                (insert "\n")
-                (setq count 0))))))
+          (when (and (= count (- tumme-thumbs-per-row 1))
+                    (not (eobp)))
+            (forward-char)
+            (insert "\n")
+            (setq count 0)))))
     (goto-char (point-min))))
 
 (defun tumme-line-up-dynamic ()
@@ -1947,7 +1689,7 @@ Calculate how many thumbnails fit."
          (/ width
             (+ (* 2 tumme-thumb-relief)
                (* 2 tumme-thumb-margin)
-               tumme-thumb-size char-width))))
+               tumme-thumb-width char-width))))
     (tumme-line-up)))
 
 (defun tumme-line-up-interactive ()
@@ -1962,25 +1704,22 @@ Ask user how many thumbnails should be displayed per row."
 
 (defun tumme-thumbnail-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
-                               file))))))
+        (call-process shell-file-name nil nil nil shell-command-switch
+                     (format "%s \"%s\"" tumme-external-viewer file))))))
 
 ;;;###autoload
 (defun tumme-dired-display-external ()
   "Display file at point using an external viewer."
   (interactive)
   (let ((file (dired-get-filename)))
-    (shell-command (format "%s \"%s\""
-                           tumme-external-viewer
-                           file))))
+    (call-process shell-file-name nil nil nil shell-command-switch
+                 (format "%s \"%s\"" tumme-external-viewer file))))
 
 (defun tumme-window-width-pixels (window)
   "Calculate WINDOW width in pixels."
@@ -2028,38 +1767,36 @@ Ask user how many thumbnails should be displayed per row."
 
 (defun tumme-display-image (file &optional original-size)
   "Display image FILE 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.
+Use this when you want to display the image, semi sized, in a new
+window.  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.
 
 If optional argument ORIGINAL-SIZE is non-nil, display image in its
 original size."
   (let ((new-file (expand-file-name tumme-temp-image-file))
-        size-x size-y command ret)
+        width height command ret)
     (setq file (expand-file-name file))
     (if (not original-size)
         (progn
-          (setq size-x (tumme-display-window-width))
-          (setq size-y (tumme-display-window-height))
+          (setq width (tumme-display-window-width))
+          (setq height (tumme-display-window-height))
           (setq command
                 (format-spec
                  tumme-cmd-create-temp-image-options
                  (list
                   (cons ?p tumme-cmd-create-temp-image-program)
-                  (cons ?x size-x)
-                  (cons ?y size-y)
+                  (cons ?w width)
+                  (cons ?h height)
                   (cons ?f file)
                   (cons ?t new-file))))
-          (setq ret (shell-command command nil))
+          (setq ret (call-process shell-file-name nil nil nil
+                                 shell-command-switch command))
           (if (not (= 0 ret))
               (error "Could not resize image")))
       (copy-file file new-file t))
-    (save-excursion
-      (set-buffer (tumme-create-display-image-buffer))
+    (with-current-buffer (tumme-create-display-image-buffer)
       (let ((inhibit-read-only t))
         (erase-buffer)
         (clear-image-cache)
@@ -2079,15 +1816,19 @@ With prefix argument ARG, display image in its original size."
           (message "No thumbnail at point")
         (if (not file)
             (message "No original file name found")
-          (tumme-display-image file arg)
-          (display-buffer tumme-display-image-buffer))))))
+         (tumme-create-display-image-buffer)
+          (display-buffer tumme-display-image-buffer)
+          (tumme-display-image file arg))))))
+
 
 ;;;###autoload
-(defun tumme-display-dired-image (&optional arg)
+(defun tumme-dired-display-image (&optional arg)
   "Display current image file.
 See documentation for `tumme-display-image' for more information.
 With prefix argument ARG, display image in its original size."
   (interactive "P")
+  (tumme-create-display-image-buffer)
+  (display-buffer tumme-display-image-buffer)
   (tumme-display-image (dired-get-filename) arg))
 
 (defun tumme-image-at-point-p ()
@@ -2106,7 +1847,7 @@ With prefix argument ARG, display image in its original size."
                       (cons ?p tumme-cmd-rotate-thumbnail-program)
                       (cons ?d degrees)
                       (cons ?t (expand-file-name file)))))
-      (shell-command command nil)
+      (call-process shell-file-name nil nil nil shell-command-switch command)
       ;; 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))))
@@ -2151,11 +1892,13 @@ overwritten.  This confirmation can be turned off using
                       (cons ?d degrees)
                       (cons ?o (expand-file-name file))
                       (cons ?t tumme-temp-rotate-image-file))))
-      (if (not (= 0 (shell-command command nil)))
+      (if (not (= 0 (call-process shell-file-name nil nil nil
+                                 shell-command-switch command)))
           (error "Could not rotate image")
         (tumme-display-image tumme-temp-rotate-image-file)
         (if (or (and tumme-rotate-original-ask-before-overwrite
-                     (y-or-n-p "Rotate to temp file OK.  Overwrite original image? "))
+                     (y-or-n-p
+                     "Rotate to temp file OK.  Overwrite original image? "))
                 (not tumme-rotate-original-ask-before-overwrite))
             (progn
               (copy-file tumme-temp-rotate-image-file file t)
@@ -2187,7 +1930,8 @@ YYYY_MM_DD_HH_MM_DD_ORIG_FILE_NAME.jpg.  Used from
                 (format-time-string
                  "%Y:%m:%d %H:%M:%S"
                  (nth 5 (file-attributes (expand-file-name file))))))
-      (setq data (tumme-get-exif-data (expand-file-name file) "DateTimeOriginal")))
+      (setq data (tumme-get-exif-data (expand-file-name file)
+                                     "DateTimeOriginal")))
     (while (string-match "[ :]" data)
       (setq data (replace-match "_" nil nil data)))
     (format "%s%s%s" data
@@ -2207,7 +1951,8 @@ default value at the prompt."
            (old-value (tumme-get-exif-data file "ImageDescription")))
       (if (eq 0
               (tumme-set-exif-data file "ImageDescription"
-                                   (read-string "Value of ImageDescription: " old-value)))
+                                   (read-string "Value of ImageDescription: "
+                                               old-value)))
           (message "Successfully wrote ImageDescription tag.")
         (error "Could not write ImageDescription tag")))))
 
@@ -2221,7 +1966,7 @@ default value at the prompt."
                     (cons ?f (expand-file-name file))
                     (cons ?t tag-name)
                     (cons ?v tag-value))))
-    (shell-command command nil)))
+    (call-process shell-file-name nil nil nil shell-command-switch command)))
 
 (defun tumme-get-exif-data (file tag-name)
   "From FILE, return EXIF tag TAG-NAME."
@@ -2233,10 +1978,10 @@ default value at the prompt."
                     (cons ?p tumme-cmd-read-exif-data-program)
                     (cons ?f file)
                     (cons ?t tag-name))))
-    (save-excursion
-      (set-buffer buf)
+    (with-current-buffer buf
       (delete-region (point-min) (point-max))
-      (if (not (eq (shell-command command buf) 0))
+      (if (not (eq (call-process shell-file-name nil t nil
+                                shell-command-switch command) 0))
           (error "Could not get EXIF tag")
         (goto-char (point-min))
         ;; Clean buffer from newlines and carriage returns before
@@ -2276,50 +2021,49 @@ function.  The result is a couple of new files in
 (defun tumme-display-next-thumbnail-original ()
   "In thubnail buffer, move to next thumbnail and display the image."
   (interactive)
-  (tumme-forward-char)
+  (tumme-forward-image)
   (tumme-display-thumbnail-original-image))
 
 (defun tumme-display-previous-thumbnail-original ()
   "Move to previous thumbnail and display image."
-
   (interactive)
-  (tumme-backward-char)
+  (tumme-backward-image)
   (tumme-display-thumbnail-original-image))
 
-(defun tumme-write-comment (file comment)
-  "For FILE, write comment COMMENT in database."
-  (save-excursion
-    (let (end buf comment-beg
-              (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-write-comments (file-comments)
+  "Write file comments to database.
+Write file comments to one or more files.  FILE-COMMENTS is an alist on
+the following form:
+ ((FILE . COMMENT) ... )"
+  (let (end comment-beg-pos comment-end-pos file comment)
+    (with-temp-file tumme-db-file
+      (insert-file-contents tumme-db-file)
+      (dolist (elt file-comments)
+       (setq file (car elt)
+             comment (cdr elt))
+       (goto-char (point-min))
+       (if (search-forward-regexp (format "^%s.*$" file) nil t)
+           (progn
+             (setq end (point))
+             (beginning-of-line)
+             ;; Delete old comment, if any
+             (when (search-forward ";comment:" end t)
+               (setq comment-beg-pos (match-beginning 0))
+               ;; Any tags after the comment?
+               (if (search-forward ";" end t)
+                   (setq comment-end-pos (- (point) 1))
+                 (setq comment-end-pos end))
+               ;; Delete comment tag and comment
+               (delete-region comment-beg-pos comment-end-pos))
+             ;; Insert new comment
+             (beginning-of-line)
+             (unless (search-forward ";" end t)
+               (end-of-line)
+               (insert ";"))
+             (insert (format "comment:%s;" comment)))
+         ;; File does not exist in database - add it.
+         (goto-char (point-max))
+         (insert (format "\n%s;comment:%s" file comment)))))))
 
 (defun tumme-update-property (prop value)
   "Update text property PROP with value VALUE at point."
@@ -2333,25 +2077,26 @@ function.  The result is a couple of new files in
 (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)))
+  (let ((comment (tumme-read-comment)))
+    (tumme-write-comments
+     (mapcar
+      (lambda (curr-file)
+        (cons curr-file comment))
+      (dired-get-marked-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-write-comments (list (cons 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."
-
+  "Read comment for an image.
+Read comment for an image, optionally using old comment from FILE
+as initial value."
   (let ((comment
          (read-string
           "Comment: "
@@ -2361,28 +2106,32 @@ function.  The result is a couple of new files in
 (defun tumme-get-comment (file)
   "Get comment for file FILE."
   (save-excursion
-    (let (end buf comment-beg comment (base-name (file-name-nondirectory file)))
+    (let (end buf comment-beg-pos comment-end-pos comment)
       (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))))))
+      (when (search-forward-regexp
+             (format "^%s" file) nil t)
+        (end-of-line)
+        (setq end (point))
+        (beginning-of-line)
+        (cond ((search-forward ";comment:" end t)
+               (setq comment-beg-pos (point))
+               (if (search-forward ";" end t)
+                   (setq comment-end-pos (- (point) 1))
+                 (setq comment-end-pos end))
+               (setq comment (buffer-substring
+                              comment-beg-pos comment-end-pos)))))
       (kill-buffer buf)
       comment)))
 
 ;;;###autoload
 (defun tumme-mark-tagged-files ()
-  "Use regexp to mark files with matching tag."
+  "Use regexp to mark files with matching tag.
+A `tag' is a keyword, a piece of meta data, associated with an
+image file and stored in tumme's database file.  This command
+lets you input a regexp and this will be matched against all tags
+on all image files in the database file.  The files that have a
+matching tags will be marked in the dired buffer."
   (interactive)
   (let ((tag (read-string "Mark tagged files (regexp): "))
         (hits 0)
@@ -2392,7 +2141,7 @@ function.  The result is a couple of new files in
       (goto-char (point-min))
       ;; Collect matches
       (while (search-forward-regexp
-              (concat "\\(^[^;]+\\);.*" tag ".*$") nil t)
+              (concat "\\(^[^;\n]+\\);.*" tag ".*$") nil t)
         (setq files (append (list (match-string 1)) files)))
       (kill-buffer buf)
       ;; Mark files
@@ -2425,6 +2174,8 @@ non-nil."
     (setq file (tumme-original-file-name))
     (if tumme-track-movement
         (tumme-track-original-file))
+    (tumme-create-display-image-buffer)
+    (display-buffer tumme-display-image-buffer)
     (tumme-display-image file)))
 
 (defun tumme-mouse-select-thumbnail (event)
@@ -2650,7 +2401,8 @@ when using per-directory thumbnail file storage"))
               ;; Insert thumbnail with link to full image
               (insert
                (format "<a href=\"%s/%s\"><img src=\"%s/%s\"%s></a>\n"
-                       tumme-gallery-image-root-url (file-name-nondirectory file)
+                       tumme-gallery-image-root-url
+                      (file-name-nondirectory file)
                        tumme-gallery-thumb-image-root-url
                        (file-name-nondirectory (tumme-thumb-name file)) file))
               ;; Insert comment, if any
@@ -2692,6 +2444,107 @@ when using per-directory thumbnail file storage"))
       (error nil))
     (kill-buffer buffer)))
 
+(defvar tumme-widget-list nil
+  "List to keep track of meta data in edit buffer.")
+
+;;;###autoload
+(defun tumme-dired-edit-comment-and-tags ()
+  "Edit comment and tags of current or marked image files.
+Edit comment and tags for all marked image files in an
+easy-to-use form."
+  (interactive)
+  (setq tumme-widget-list nil)
+  ;; Setup buffer.
+  (let ((files (dired-get-marked-files)))
+    (switch-to-buffer "*Tumme Edit Meta Data*")
+    (kill-all-local-variables)
+    (make-local-variable 'widget-example-repeat)
+    (let ((inhibit-read-only t))
+      (erase-buffer))
+    (remove-overlays)
+    ;; Some help for the user.
+    (widget-insert
+"\nEdit comments and tags for each image.  Separate multiple tags
+with a comma.  Move forward between fields using TAB or RET.
+Move to the previous field using backtab (S-TAB).  Save by
+activating the Save button at the bottom of the form or cancel
+the operation by activating the Cancel button.\n\n")
+    ;; Here comes all images and a comment and tag field for each
+    ;; image.
+    (let (thumb-file img comment-widget tag-widget)
+
+      (dolist (file files)
+
+       (setq thumb-file (tumme-thumb-name file)
+             img (create-image thumb-file))
+
+       (insert-image img)
+       (widget-insert "\n\nComment: ")
+       (setq comment-widget
+             (widget-create 'editable-field
+                            :size 60
+                            :format "%v "
+                            :value (or (tumme-get-comment file) "")))
+       (widget-insert "\nTags:    ")
+       (setq tag-widget
+             (widget-create 'editable-field
+                            :size 60
+                            :format "%v "
+                            :value (or (mapconcat
+                                        (lambda (tag)
+                                          tag)
+                                        (tumme-list-tags file)
+                                        ",") "")))
+       ;; Save information in all widgets so that we can use it when
+       ;; the user saves the form.
+       (setq tumme-widget-list
+             (append tumme-widget-list
+                     (list (list file comment-widget tag-widget))))
+       (widget-insert "\n\n")))
+
+    ;; Footer with Save and Cancel button.
+    (widget-insert "\n")
+    (widget-create 'push-button
+                 :notify
+                 (lambda (&rest ignore)
+                   (tumme-save-information-from-widgets)
+                   (bury-buffer)
+                   (message "Done."))
+                 "Save")
+    (widget-insert " ")
+    (widget-create 'push-button
+                   :notify
+                   (lambda (&rest ignore)
+                     (bury-buffer)
+                     (message "Operation canceled."))
+                   "Cancel")
+    (widget-insert "\n")
+    (use-local-map widget-keymap)
+    (widget-setup)
+    ;; Jump to the first widget.
+    (widget-forward 1)))
+
+(defun tumme-save-information-from-widgets ()
+  "Save information found in `tumme-widget-list'.
+Use the information in `tumme-widget-list' to save comments and
+tags to their respective image file.  Internal function used by
+`tumme-dired-edit-comment-and-tags'."
+  (let (file comment tag-string tag-list lst)
+    (tumme-write-comments
+          (mapcar
+           (lambda (widget)
+             (setq file (car widget)
+                   comment (widget-value (cadr widget)))
+             (cons file comment))
+           tumme-widget-list))
+    (tumme-write-tags
+     (dolist (widget tumme-widget-list lst)
+       (setq file (car widget)
+             tag-string (widget-value (car (cddr widget)))
+             tag-list (split-string tag-string ","))
+       (dolist (tag tag-list)
+         (push (cons file tag) lst))))))
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;;;;;;;; TEST-SECTION ;;;;;;;;;;;
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -2710,7 +2563,7 @@ when using per-directory thumbnail file storage"))
 ;;               (let ((fattribs (file-attributes f)))
 ;;                 ;; Get last access time and file size
 ;;                 `(,(nth 4 fattribs) ,(nth 7 fattribs) ,f)))
-;;             (directory-files tumme-dir t ".+\.thumb\..+$"))
+;;             (directory-files (tumme-dir) t ".+\.thumb\..+$"))
 ;;            ;; Sort function. Compare time between two files.
 ;;            '(lambda (l1 l2)
 ;;               (time-less-p (car l1) (car l2)))))