Setup quote display only if interactive
[emacs.git] / lisp / thumbs.el
Commit [+]AuthorDateLineData
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +00001;;; thumbs.el --- Thumbnails previewer for images files
0658b86f Richard M. Stallman2004-05-29 15:22:55 +00002
7e09ef09 Paul Eggert2015-01-01 14:26:41 -08003;; Copyright (C) 2004-2015 Free Software Foundation, Inc.
0658b86f Richard M. Stallman2004-05-29 15:22:55 +00004
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +00005;; Author: Jean-Philippe Theberge <jphiltheberge@videotron.ca>
34dc21db Glenn Morris2014-02-09 17:34:22 -08006;; Maintainer: emacs-devel@gnu.org
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +00007;; Keywords: Multimedia
8
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +00009;; This file is part of GNU Emacs.
10
eb3fa2cf Glenn Morris2008-05-06 08:06:51 +000011;; GNU Emacs is free software: you can redistribute it and/or modify
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +000012;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
Glenn Morris2008-05-06 08:06:51 +000013;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +000015
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf Glenn Morris2008-05-06 08:06:51 +000022;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +000023
24;;; Commentary:
25
dd2a3d13
NR
Nick Roberts2006-01-23 23:16:58 +000026;; This package create two new modes: thumbs-mode and thumbs-view-image-mode.
27;; It is used for basic browsing and viewing of images from within Emacs.
28;; Minimal image manipulation functions are also available via external
da6062e6 Paul Eggert2011-11-14 23:55:13 -080029;; programs. If you want to do more complex tasks like categorize and tag
a6009bd1 Chong Yidong2007-04-22 13:44:43 +000030;; your images, use image-dired.el
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +000031;;
32;; The 'convert' program from 'ImageMagick'
33;; [URL:http://www.imagemagick.org/] is required.
34;;
dd2a3d13
NR
Nick Roberts2006-01-23 23:16:58 +000035;; Thanks: Alex Schroeder <alex@gnu.org> for maintaining the package at some
36;; time. The peoples at #emacs@freenode.net for numerous help. RMS
37;; for emacs and the GNU project.
38;;
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +000039;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +000040;;
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +000041;; CHANGELOG
42;;
43;; This is version 2.0
44;;
45;; USAGE
46;;
47;; Type M-x thumbs RET DIR RET to view the directory DIR in Thumbs mode.
48;; That should be a directory containing image files.
49;; from dired, C-t m enter in thumbs-mode with all marked files
50;; C-t a enter in thumbs-mode with all files in current-directory
dd2a3d13
NR
Nick Roberts2006-01-23 23:16:58 +000051;; In thumbs-mode, pressing <return> on a image will bring you in image view
52;; mode for that image. C-h m will give you a list of available keybinding.
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +000053
54;;; History:
d1c43637 Juanma Barranquero2004-04-27 22:43:35 +000055;;
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +000056
57;;; Code:
58
59(require 'dired)
4582a01c Glenn Morris2013-07-11 13:16:19 -040060(require 'cl-lib) ; for cl-gensym
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +000061
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +000062;; CUSTOMIZATIONS
63
64(defgroup thumbs nil
65 "Thumbnails previewer."
bf247b6e Kim F. Storm2005-02-09 15:50:47 +000066 :version "22.1"
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +000067 :group 'multimedia)
68
d6c180c4 Juanma Barranquero2008-10-24 09:39:27 +000069(defcustom thumbs-thumbsdir (locate-user-emacs-file "thumbs")
ea6c930a Juanma Barranquero2008-10-25 00:46:25 +000070 "Directory to store thumbnails."
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +000071 :type 'directory
72 :group 'thumbs)
73
74(defcustom thumbs-geometry "100x100"
ea6c930a Juanma Barranquero2008-10-25 00:46:25 +000075 "Size of thumbnails."
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +000076 :type 'string
77 :group 'thumbs)
78
dd2a3d13
NR
Nick Roberts2006-01-23 23:16:58 +000079(defcustom thumbs-per-line 4
80 "Number of thumbnails per line to show in directory."
81 :type 'integer
82 :group 'thumbs)
83
84(defcustom thumbs-max-image-number 16
85 "Maximum number of images initially displayed in thumbs buffer."
67ed6461 Eli Zaretskii2005-06-11 13:25:16 +000086 :type 'integer
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +000087 :group 'thumbs)
88
89(defcustom thumbs-thumbsdir-max-size 50000000
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +000090 "Maximum size for thumbnails directory.
b3c70578 Juanma Barranquero2005-06-06 15:45:11 +000091When it reaches that size (in bytes), a warning is sent."
67ed6461 Eli Zaretskii2005-06-11 13:25:16 +000092 :type 'integer
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +000093 :group 'thumbs)
94
babfb70b Nick Roberts2007-12-29 23:39:51 +000095;; Unfortunately Windows XP has a program called CONVERT.EXE in
e1dbe924 Paul Eggert2011-11-19 18:29:42 -080096;; C:/WINDOWS/SYSTEM32/ for partitioning NTFS systems. So Emacs
babfb70b
NR
Nick Roberts2007-12-29 23:39:51 +000097;; can find the one in your ImageMagick directory, you need to
98;; customize this value to the absolute filename.
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +000099(defcustom thumbs-conversion-program
f271d3c7 Juanma Barranquero2005-06-09 01:41:23 +0000100 (if (eq system-type 'windows-nt)
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000101 "convert.exe"
102 (or (executable-find "convert")
103 "/usr/X11R6/bin/convert"))
ea6c930a Juanma Barranquero2008-10-25 00:46:25 +0000104 "Name of conversion program for thumbnails generation.
760701eb Paul Eggert2015-08-20 17:33:48 -0700105It must be \"convert\"."
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000106 :type 'string
107 :group 'thumbs)
108
109(defcustom thumbs-setroot-command
110 "xloadimage -onroot -fullscreen *"
111 "Command to set the root window."
112 :type 'string
113 :group 'thumbs)
114
115(defcustom thumbs-relief 5
ea6c930a Juanma Barranquero2008-10-25 00:46:25 +0000116 "Size of button-like border around thumbnails."
67ed6461 Eli Zaretskii2005-06-11 13:25:16 +0000117 :type 'integer
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000118 :group 'thumbs)
119
120(defcustom thumbs-margin 2
ea6c930a Juanma Barranquero2008-10-25 00:46:25 +0000121 "Size of the margin around thumbnails.
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000122This is where you see the cursor."
67ed6461 Eli Zaretskii2005-06-11 13:25:16 +0000123 :type 'integer
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000124 :group 'thumbs)
125
126(defcustom thumbs-thumbsdir-auto-clean t
127 "If set, delete older file in the thumbnails directory.
128Deletion is done at load time when the directory size is bigger
b3c70578 Juanma Barranquero2005-06-06 15:45:11 +0000129than `thumbs-thumbsdir-max-size'."
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000130 :type 'boolean
131 :group 'thumbs)
132
133(defcustom thumbs-image-resizing-step 10
2ff89cf7 Nick Roberts2006-01-24 20:54:23 +0000134 "Step by which to resize image as a percentage."
67ed6461 Eli Zaretskii2005-06-11 13:25:16 +0000135 :type 'integer
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000136 :group 'thumbs)
137
f271d3c7 Juanma Barranquero2005-06-09 01:41:23 +0000138(defcustom thumbs-temp-dir temporary-file-directory
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000139 "Temporary directory to use.
f271d3c7
JB
Juanma Barranquero2005-06-09 01:41:23 +0000140Defaults to `temporary-file-directory'. Leaving it to
141this value can let another user see some of your images."
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000142 :type 'directory
143 :group 'thumbs)
144
145(defcustom thumbs-temp-prefix "emacsthumbs"
146 "Prefix to add to temp files."
147 :type 'string
148 :group 'thumbs)
149
150;; Initialize some variable, for later use.
6ced5724 Juanma Barranquero2005-08-02 10:26:31 +0000151(defvar thumbs-current-tmp-filename nil
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000152 "Temporary filename of current image.")
6ced5724
JB
Juanma Barranquero2005-08-02 10:26:31 +0000153(make-variable-buffer-local 'thumbs-current-tmp-filename)
154
155(defvar thumbs-current-image-filename nil
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000156 "Filename of current image.")
6ced5724
JB
Juanma Barranquero2005-08-02 10:26:31 +0000157(make-variable-buffer-local 'thumbs-current-image-filename)
158
dd2a3d13
NR
Nick Roberts2006-01-23 23:16:58 +0000159(defvar thumbs-extra-images 1
160 "Counter for showing extra images in thumbs buffer.")
161(make-variable-buffer-local 'thumbs-extra-images)
162(put 'thumbs-extra-images 'permanent-local t)
163
6ced5724 Juanma Barranquero2005-08-02 10:26:31 +0000164(defvar thumbs-current-image-size nil
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000165 "Size of current image.")
6ced5724
JB
Juanma Barranquero2005-08-02 10:26:31 +0000166
167(defvar thumbs-image-num nil
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000168 "Number of current image.")
6ced5724
JB
Juanma Barranquero2005-08-02 10:26:31 +0000169(make-variable-buffer-local 'thumbs-image-num)
170
c897e76f
NR
Nick Roberts2006-01-20 04:43:02 +0000171(defvar thumbs-buffer nil
172 "Name of buffer containing thumbnails associated with image.")
173(make-variable-buffer-local 'thumbs-buffer)
174
6ced5724 Juanma Barranquero2005-08-02 10:26:31 +0000175(defvar thumbs-current-dir nil
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000176 "Current directory.")
6ced5724 Juanma Barranquero2005-08-02 10:26:31 +0000177
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000178(defvar thumbs-marked-list nil
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000179 "List of marked files.")
0868cd70
NR
Nick Roberts2006-01-30 09:52:49 +0000180(make-variable-buffer-local 'thumbs-marked-list)
181(put 'thumbs-marked-list 'permanent-local t)
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000182
6cae5f24
JB
Juanma Barranquero2005-06-10 14:59:59 +0000183(defsubst thumbs-temp-dir ()
184 (file-name-as-directory (expand-file-name thumbs-temp-dir)))
185
f271d3c7
JB
Juanma Barranquero2005-06-09 01:41:23 +0000186(defun thumbs-temp-file ()
187 "Return a unique temporary filename for an image."
188 (format "%s%s-%s.jpg"
6cae5f24 Juanma Barranquero2005-06-10 14:59:59 +0000189 (thumbs-temp-dir)
f271d3c7 Juanma Barranquero2005-06-09 01:41:23 +0000190 thumbs-temp-prefix
4582a01c Glenn Morris2013-07-11 13:16:19 -0400191 (cl-gensym "T")))
f271d3c7
JB
Juanma Barranquero2005-06-09 01:41:23 +0000192
193(defun thumbs-thumbsdir ()
194 "Return the current thumbnails directory (from `thumbs-thumbsdir').
195Create the thumbnails directory if it does not exist."
6cae5f24
JB
Juanma Barranquero2005-06-10 14:59:59 +0000196 (let ((thumbs-thumbsdir (file-name-as-directory
197 (expand-file-name thumbs-thumbsdir))))
f271d3c7 Juanma Barranquero2005-06-09 01:41:23 +0000198 (unless (file-directory-p thumbs-thumbsdir)
6c060884 Chong Yidong2005-10-30 22:28:08 +0000199 (make-directory thumbs-thumbsdir t)
f271d3c7
JB
Juanma Barranquero2005-06-09 01:41:23 +0000200 (message "Creating thumbnails directory"))
201 thumbs-thumbsdir))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000202
203(defun thumbs-cleanup-thumbsdir ()
204 "Clean the thumbnails directory.
f75049fc
JB
Juanma Barranquero2005-05-25 14:09:14 +0000205If the total size of all files in `thumbs-thumbsdir' is bigger than
206`thumbs-thumbsdir-max-size', files are deleted until the max size is
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000207reached."
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000208 (let* ((files-list
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000209 (sort
210 (mapcar
211 (lambda (f)
dd2a3d13
NR
Nick Roberts2006-01-23 23:16:58 +0000212 (let ((fattribs-list (file-attributes f)))
213 `(,(nth 4 fattribs-list) ,(nth 7 fattribs-list) ,f)))
f271d3c7 Juanma Barranquero2005-06-09 01:41:23 +0000214 (directory-files (thumbs-thumbsdir) t (image-file-name-regexp)))
4f91a816 Stefan Monnier2011-05-23 14:57:17 -0300215 (lambda (l1 l2) (time-less-p (car l1) (car l2)))))
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000216 (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) files-list))))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000217 (while (> dirsize thumbs-thumbsdir-max-size)
218 (progn
dd2a3d13
NR
Nick Roberts2006-01-23 23:16:58 +0000219 (message "Deleting file %s" (cadr (cdar files-list))))
220 (delete-file (cadr (cdar files-list)))
221 (setq dirsize (- dirsize (car (cdar files-list))))
222 (setq files-list (cdr files-list)))))
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000223
40ba43b4 Paul Eggert2011-11-19 23:30:16 -0800224;; Check the thumbnail directory size and clean it if necessary.
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000225(when thumbs-thumbsdir-auto-clean
226 (thumbs-cleanup-thumbsdir))
227
228(defun thumbs-call-convert (filein fileout action
229 &optional arg output-format action-prefix)
230 "Call the convert program.
231FILEIN is the input file,
232FILEOUT is the output file,
233ACTION is the command to send to convert.
f75049fc Juanma Barranquero2005-05-25 14:09:14 +0000234Optional arguments are:
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000235ARG any arguments to the ACTION command,
f75049fc Juanma Barranquero2005-05-25 14:09:14 +0000236OUTPUT-FORMAT is the file format to output (default is jpeg),
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000237ACTION-PREFIX is the symbol to place before the ACTION command
f75049fc Juanma Barranquero2005-05-25 14:09:14 +0000238 (defaults to '-' but can sometimes be '+')."
a50d1244
NR
Nick Roberts2007-12-28 08:09:57 +0000239 (call-process thumbs-conversion-program nil nil nil
240 (or action-prefix "-")
241 action
242 (or arg "")
243 filein
244 (format "%s:%s" (or output-format "jpeg") fileout)))
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000245
2ff89cf7
NR
Nick Roberts2006-01-24 20:54:23 +0000246(defun thumbs-new-image-size (s increment)
247 "New image (a cons of width x height)."
248 (let ((d (* increment thumbs-image-resizing-step)))
249 (cons
250 (round (+ (car s) (/ (* d (car s)) 100)))
251 (round (+ (cdr s) (/ (* d (cdr s)) 100))))))
252
253(defun thumbs-resize-image-1 (&optional increment size)
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000254 "Resize image in current buffer.
2ff89cf7
NR
Nick Roberts2006-01-24 20:54:23 +0000255If SIZE is specified use it. Otherwise make the image larger or
256smaller according to whether INCREMENT is 1 or -1."
257 (let* ((buffer-read-only nil)
258 (old thumbs-current-tmp-filename)
259 (x (or size
260 (thumbs-new-image-size thumbs-current-image-size increment)))
261 (tmp (thumbs-temp-file)))
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000262 (erase-buffer)
2ff89cf7 Nick Roberts2006-01-24 20:54:23 +0000263 (thumbs-call-convert (or old thumbs-current-image-filename)
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000264 tmp "sample"
265 (concat (number-to-string (car x)) "x"
266 (number-to-string (cdr x))))
2ff89cf7
NR
Nick Roberts2006-01-24 20:54:23 +0000267 (save-excursion
268 (thumbs-insert-image tmp 'jpeg 0))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000269 (setq thumbs-current-tmp-filename tmp)))
270
2ff89cf7 Nick Roberts2006-01-24 20:54:23 +0000271(defun thumbs-resize-image (width height)
f271d3c7 Juanma Barranquero2005-06-09 01:41:23 +0000272 "Resize image interactively to specified WIDTH and HEIGHT."
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000273 (interactive "nWidth: \nnHeight: ")
2ff89cf7 Nick Roberts2006-01-24 20:54:23 +0000274 (thumbs-resize-image-1 nil (cons width height)))
d1c43637 Juanma Barranquero2004-04-27 22:43:35 +0000275
2ff89cf7 Nick Roberts2006-01-24 20:54:23 +0000276(defun thumbs-shrink-image ()
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000277 "Resize image (smaller)."
278 (interactive)
2ff89cf7 Nick Roberts2006-01-24 20:54:23 +0000279 (thumbs-resize-image-1 -1))
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000280
2ff89cf7 Nick Roberts2006-01-24 20:54:23 +0000281(defun thumbs-enlarge-image ()
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000282 "Resize image (bigger)."
283 (interactive)
2ff89cf7 Nick Roberts2006-01-24 20:54:23 +0000284 (thumbs-resize-image-1 1))
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000285
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000286(defun thumbs-thumbname (img)
287 "Return a thumbnail name for the image IMG."
0df06447
JB
Juanma Barranquero2005-05-30 09:45:11 +0000288 (convert-standard-filename
289 (let ((filename (expand-file-name img)))
6cae5f24 Juanma Barranquero2005-06-10 14:59:59 +0000290 (format "%s%08x-%s.jpg"
f271d3c7 Juanma Barranquero2005-06-09 01:41:23 +0000291 (thumbs-thumbsdir)
0df06447
JB
Juanma Barranquero2005-05-30 09:45:11 +0000292 (sxhash filename)
293 (subst-char-in-string
294 ?\s ?\_
295 (apply
296 'concat
297 (split-string filename "/")))))))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000298
299(defun thumbs-make-thumb (img)
300 "Create the thumbnail for IMG."
0df06447
JB
Juanma Barranquero2005-05-30 09:45:11 +0000301 (let ((fn (expand-file-name img))
302 (tn (thumbs-thumbname img)))
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000303 (if (or (not (file-exists-p tn))
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000304 ;; This is not the right fix, but I don't understand
305 ;; the external program or why it produces a geometry
306 ;; unequal to the one requested -- rms.
307;;; (not (equal (thumbs-file-size tn) thumbs-geometry))
308 )
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000309 (thumbs-call-convert fn tn "sample" thumbs-geometry))
310 tn))
d1c43637 Juanma Barranquero2004-04-27 22:43:35 +0000311
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000312(defun thumbs-image-type (img)
313 "Return image type from filename IMG."
314 (cond ((string-match ".*\\.jpe?g\\'" img) 'jpeg)
315 ((string-match ".*\\.xpm\\'" img) 'xpm)
316 ((string-match ".*\\.xbm\\'" img) 'xbm)
24bdbffe Chong Yidong2005-10-24 22:13:08 +0000317 ((string-match ".*\\.pbm\\'" img) 'pbm)
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000318 ((string-match ".*\\.gif\\'" img) 'gif)
319 ((string-match ".*\\.bmp\\'" img) 'bmp)
320 ((string-match ".*\\.png\\'" img) 'png)
321 ((string-match ".*\\.tiff?\\'" img) 'tiff)))
322
aa360da1
GM
Glenn Morris2008-06-12 03:56:20 +0000323(declare-function image-size "image.c" (spec &optional pixels frame))
324
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000325(defun thumbs-file-size (img)
0868cd70
NR
Nick Roberts2006-01-30 09:52:49 +0000326 (let ((i (image-size
327 (find-image `((:type ,(thumbs-image-type img) :file ,img))) t)))
328 (concat (number-to-string (round (car i))) "x"
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000329 (number-to-string (round (cdr i))))))
d1c43637 Juanma Barranquero2004-04-27 22:43:35 +0000330
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000331;;;###autoload
332(defun thumbs-find-thumb (img)
333 "Display the thumbnail for IMG."
334 (interactive "f")
335 (find-file (thumbs-make-thumb img)))
336
337(defun thumbs-insert-image (img type relief &optional marked)
338 "Insert image IMG at point.
339TYPE and RELIEF will be used in constructing the image; see `image'
340in the emacs-lisp manual for further documentation.
f75049fc Juanma Barranquero2005-05-25 14:09:14 +0000341If MARKED is non-nil, the image is marked."
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000342 (let ((i `(image :type ,type
343 :file ,img
344 :relief ,relief
345 :conversion ,(if marked 'disabled)
346 :margin ,thumbs-margin)))
347 (insert-image i)
6ced5724
JB
Juanma Barranquero2005-08-02 10:26:31 +0000348 (set (make-local-variable 'thumbs-current-image-size)
349 (image-size i t))))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000350
351(defun thumbs-insert-thumb (img &optional marked)
352 "Insert the thumbnail for IMG at point.
f75049fc Juanma Barranquero2005-05-25 14:09:14 +0000353If MARKED is non-nil, the image is marked."
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000354 (thumbs-insert-image
0658b86f Richard M. Stallman2004-05-29 15:22:55 +0000355 (thumbs-make-thumb img) 'jpeg thumbs-relief marked)
dd2a3d13
NR
Nick Roberts2006-01-23 23:16:58 +0000356 (add-text-properties (1- (point)) (point)
357 `(thumb-image-file ,img
0868cd70
NR
Nick Roberts2006-01-30 09:52:49 +0000358 help-echo ,(file-name-nondirectory img)
359 rear-nonsticky help-echo)))
dd2a3d13
NR
Nick Roberts2006-01-23 23:16:58 +0000360
361(defun thumbs-do-thumbs-insertion (list)
362 "Insert all thumbnails into thumbs buffer."
363 (let* ((i 0)
364 (length (length list))
365 (diff (- length (* thumbs-max-image-number thumbs-extra-images))))
366 (nbutlast list diff)
367 (dolist (img list)
0658b86f Richard M. Stallman2004-05-29 15:22:55 +0000368 (thumbs-insert-thumb img
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000369 (member img thumbs-marked-list))
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000370 (when (= 0 (mod (setq i (1+ i)) thumbs-per-line))
0658b86f Richard M. Stallman2004-05-29 15:22:55 +0000371 (newline)))
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000372 (unless (bobp) (newline))
3e5b780f Nick Roberts2006-01-26 23:03:17 +0000373 (if (> diff 0) (message "Type + to display more images."))))
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000374
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000375(defun thumbs-show-thumbs-list (list &optional dir same-window)
0df06447
JB
Juanma Barranquero2005-05-30 09:45:11 +0000376 (unless (and (display-images-p)
377 (image-type-available-p 'jpeg))
378 (error "Required image type is not supported in this Emacs session"))
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000379 (funcall (if same-window 'switch-to-buffer 'pop-to-buffer)
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000380 (if dir (concat "*Thumbs: " dir) "*THUMB-View*"))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000381 (let ((inhibit-read-only t))
382 (erase-buffer)
383 (thumbs-mode)
2ff89cf7 Nick Roberts2006-01-24 20:54:23 +0000384 (setq thumbs-buffer (current-buffer))
dd2a3d13
NR
Nick Roberts2006-01-23 23:16:58 +0000385 (if dir (setq default-directory dir))
386 (thumbs-do-thumbs-insertion list)
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000387 (goto-char (point-min))
6ced5724 Juanma Barranquero2005-08-02 10:26:31 +0000388 (set (make-local-variable 'thumbs-current-dir) default-directory)))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000389
390;;;###autoload
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000391(defun thumbs-show-from-dir (dir &optional reg same-window)
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000392 "Make a preview buffer for all images in DIR.
393Optional argument REG to select file matching a regexp,
394and SAME-WINDOW to show thumbs in the same window."
395 (interactive "DDir: ")
396 (thumbs-show-thumbs-list
dd2a3d13
NR
Nick Roberts2006-01-23 23:16:58 +0000397 (directory-files dir t (or reg (image-file-name-regexp)))
398 dir same-window))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000399
400;;;###autoload
401(defun thumbs-dired-show-marked ()
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000402 "In dired, make a thumbs buffer with marked files."
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000403 (interactive)
404 (thumbs-show-thumbs-list (dired-get-marked-files) nil t))
405
406;;;###autoload
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000407(defun thumbs-dired-show ()
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000408 "In dired, make a thumbs buffer with all files in current directory."
409 (interactive)
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000410 (thumbs-show-from-dir default-directory nil t))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000411
412;;;###autoload
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000413(defalias 'thumbs 'thumbs-show-from-dir)
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000414
0658b86f Richard M. Stallman2004-05-29 15:22:55 +0000415(defun thumbs-find-image (img &optional num otherwin)
c897e76f
NR
Nick Roberts2006-01-20 04:43:02 +0000416 (let ((buffer (current-buffer)))
417 (funcall
418 (if otherwin 'switch-to-buffer-other-window 'switch-to-buffer)
419 "*Image*")
420 (thumbs-view-image-mode)
421 (setq mode-name
422 (concat "image-view-mode: " (file-name-nondirectory img)
423 " - " (number-to-string num)))
424 (setq thumbs-buffer buffer)
425 (let ((inhibit-read-only t))
426 (setq thumbs-current-image-filename img
427 thumbs-current-tmp-filename nil
428 thumbs-image-num (or num 0))
429 (delete-region (point-min)(point-max))
430 (save-excursion
431 (thumbs-insert-image img (thumbs-image-type img) 0)))))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000432
433(defun thumbs-find-image-at-point (&optional img otherwin)
434 "Display image IMG for thumbnail at point.
f75049fc Juanma Barranquero2005-05-25 14:09:14 +0000435Use another window if OTHERWIN is t."
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000436 (interactive)
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000437 (let* ((i (or img (thumbs-current-image))))
438 (thumbs-find-image i (point) otherwin)))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000439
440(defun thumbs-find-image-at-point-other-window ()
441 "Display image for thumbnail at point in the preview buffer.
442Open another window."
443 (interactive)
444 (thumbs-find-image-at-point nil t))
445
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000446(defun thumbs-mouse-find-image (event)
447 "Display image for thumbnail at mouse click EVENT."
448 (interactive "e")
449 (mouse-set-point event)
450 (thumbs-find-image-at-point))
451
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000452(defun thumbs-call-setroot-command (img)
453 "Call the setroot program for IMG."
454 (run-hooks 'thumbs-before-setroot-hook)
455 (shell-command (replace-regexp-in-string
456 "\\*"
457 (shell-quote-argument (expand-file-name img))
458 thumbs-setroot-command nil t))
459 (run-hooks 'thumbs-after-setroot-hook))
d1c43637 Juanma Barranquero2004-04-27 22:43:35 +0000460
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000461(defun thumbs-set-image-at-point-to-root-window ()
462 "Set the image at point as the desktop wallpaper."
463 (interactive)
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000464 (thumbs-call-setroot-command
465 (thumbs-current-image)))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000466
467(defun thumbs-set-root ()
468 "Set the current image as root."
469 (interactive)
470 (thumbs-call-setroot-command
471 (or thumbs-current-tmp-filename
472 thumbs-current-image-filename)))
473
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000474(defun thumbs-file-alist ()
475 "Make an alist of elements (POS . FILENAME) for all images in thumb buffer."
c897e76f
NR
Nick Roberts2006-01-20 04:43:02 +0000476 (with-current-buffer thumbs-buffer
477 (save-excursion
478 (let (list)
479 (goto-char (point-min))
480 (while (not (eobp))
0868cd70 Nick Roberts2006-01-30 09:52:49 +0000481 (unless (eolp)
1acadcfe
NR
Nick Roberts2006-01-23 01:09:26 +0000482 (if (thumbs-current-image)
483 (push (cons (point-marker)
484 (thumbs-current-image))
485 list)))
c897e76f
NR
Nick Roberts2006-01-20 04:43:02 +0000486 (forward-char 1))
487 (nreverse list)))))
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000488
489(defun thumbs-file-list ()
490 "Make a list of file names for all images in thumb buffer."
491 (save-excursion
492 (let (list)
493 (goto-char (point-min))
494 (while (not (eobp))
495 (if (thumbs-current-image)
496 (push (thumbs-current-image) list))
497 (forward-char 1))
498 (nreverse list))))
499
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000500(defun thumbs-delete-images ()
f75049fc Juanma Barranquero2005-05-25 14:09:14 +0000501 "Delete the image at point (and its thumbnail) (or marked files if any)."
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000502 (interactive)
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000503 (let ((files (or thumbs-marked-list (list (thumbs-current-image)))))
0658b86f Richard M. Stallman2004-05-29 15:22:55 +0000504 (if (yes-or-no-p (format "Really delete %d files? " (length files)))
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000505 (let ((thumbs-file-list (thumbs-file-alist))
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000506 (inhibit-read-only t))
507 (dolist (x files)
508 (let (failure)
509 (condition-case ()
510 (progn
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000511 (delete-file x)
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000512 (delete-file (thumbs-thumbname x)))
513 (file-error (setq failure t)))
514 (unless failure
dd2a3d13
NR
Nick Roberts2006-01-23 23:16:58 +0000515 (when (rassoc x thumbs-file-list)
516 (goto-char (car (rassoc x thumbs-file-list)))
0658b86f Richard M. Stallman2004-05-29 15:22:55 +0000517 (delete-region (point) (1+ (point))))
dd2a3d13
NR
Nick Roberts2006-01-23 23:16:58 +0000518 (setq thumbs-marked-list
519 (delq x thumbs-marked-list)))))))))
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000520
521(defun thumbs-rename-images (newfile)
f75049fc Juanma Barranquero2005-05-25 14:09:14 +0000522 "Rename the image at point (and its thumbnail) (or marked files if any)."
0658b86f Richard M. Stallman2004-05-29 15:22:55 +0000523 (interactive "FRename to file or directory: ")
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000524 (let ((files (or thumbs-marked-list (list (thumbs-current-image))))
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000525 failures)
526 (if (and (not (file-directory-p newfile))
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000527 thumbs-marked-list)
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000528 (if (file-exists-p newfile)
529 (error "Renaming marked files to file name `%s'" newfile)
530 (make-directory newfile t)))
531 (if (yes-or-no-p (format "Really rename %d files? " (length files)))
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000532 (let ((thumbs-file-list (thumbs-file-alist))
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000533 (inhibit-read-only t))
534 (dolist (file files)
535 (let (failure)
536 (condition-case ()
537 (if (file-directory-p newfile)
538 (rename-file file
539 (expand-file-name
540 (file-name-nondirectory file)
541 newfile))
542 (rename-file file newfile))
543 (file-error (setq failure t)
544 (push file failures)))
545 (unless failure
dd2a3d13
NR
Nick Roberts2006-01-23 23:16:58 +0000546 (when (rassoc file thumbs-file-list)
547 (goto-char (car (rassoc file thumbs-file-list)))
0658b86f Richard M. Stallman2004-05-29 15:22:55 +0000548 (delete-region (point) (1+ (point))))
dd2a3d13
NR
Nick Roberts2006-01-23 23:16:58 +0000549 (setq thumbs-marked-list
550 (delq file thumbs-marked-list)))))))
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000551 (if failures
552 (display-warning 'file-error
553 (format "Rename failures for %s into %s"
554 failures newfile)
555 :error))))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000556
557(defun thumbs-kill-buffer ()
558 "Kill the current buffer."
559 (interactive)
f271d3c7 Juanma Barranquero2005-06-09 01:41:23 +0000560 (quit-window t (selected-window)))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000561
562(defun thumbs-show-image-num (num)
563 "Show the image with number NUM."
0658b86f Richard M. Stallman2004-05-29 15:22:55 +0000564 (let ((image-buffer (get-buffer-create "*Image*")))
c897e76f Nick Roberts2006-01-20 04:43:02 +0000565 (let ((img (cdr (nth (1- num) (thumbs-file-alist)))))
0658b86f Richard M. Stallman2004-05-29 15:22:55 +0000566 (with-current-buffer image-buffer
c897e76f
NR
Nick Roberts2006-01-20 04:43:02 +0000567 (setq mode-name
568 (concat "image-view-mode: " (file-name-nondirectory img)
569 " - " (number-to-string num)))
570 (let ((inhibit-read-only t))
571 (erase-buffer)
572 (thumbs-insert-image img (thumbs-image-type img) 0)
573 (goto-char (point-min))))
d42799bd John Paul Wallington2004-04-28 16:58:44 +0000574 (setq thumbs-image-num num
c897e76f Nick Roberts2006-01-20 04:43:02 +0000575 thumbs-current-image-filename img))))
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000576
dd2a3d13
NR
Nick Roberts2006-01-23 23:16:58 +0000577(defun thumbs-previous-image ()
578 "Show the previous image."
579 (interactive)
580 (let* ((i (- thumbs-image-num 1))
581 (number (length (thumbs-file-alist))))
582 (if (= i 0) (setq i (1- number)))
583 (thumbs-show-image-num i)))
584
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000585(defun thumbs-next-image ()
f75049fc Juanma Barranquero2005-05-25 14:09:14 +0000586 "Show the next image."
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000587 (interactive)
588 (let* ((i (1+ thumbs-image-num))
c897e76f
NR
Nick Roberts2006-01-20 04:43:02 +0000589 (number (length (thumbs-file-alist))))
590 (if (= i number) (setq i 1))
0658b86f Richard M. Stallman2004-05-29 15:22:55 +0000591 (thumbs-show-image-num i)))
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000592
dd2a3d13
NR
Nick Roberts2006-01-23 23:16:58 +0000593(defun thumbs-display-thumbs-buffer ()
594 "Display the associated thumbs buffer."
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000595 (interactive)
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000596 (display-buffer thumbs-buffer))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000597
598(defun thumbs-redraw-buffer ()
599 "Redraw the current thumbs buffer."
600 (let ((p (point))
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000601 (inhibit-read-only t)
602 (files (thumbs-file-list)))
603 (erase-buffer)
604 (thumbs-do-thumbs-insertion files)
605 (goto-char p)))
d1c43637 Juanma Barranquero2004-04-27 22:43:35 +0000606
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000607(defun thumbs-mark ()
608 "Mark the image at point."
609 (interactive)
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000610 (let ((elt (thumbs-current-image)))
611 (unless elt
612 (error "No image here"))
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000613 (push elt thumbs-marked-list)
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000614 (let ((inhibit-read-only t))
615 (delete-char 1)
3e5b780f Nick Roberts2006-01-26 23:03:17 +0000616 (thumbs-insert-thumb elt t)))
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000617 (when (eolp) (forward-char)))
618
619(defun thumbs-unmark ()
620 "Unmark the image at point."
621 (interactive)
622 (let ((elt (thumbs-current-image)))
623 (unless elt
624 (error "No image here"))
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000625 (setq thumbs-marked-list (delete elt thumbs-marked-list))
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000626 (let ((inhibit-read-only t))
627 (delete-char 1)
3e5b780f Nick Roberts2006-01-26 23:03:17 +0000628 (thumbs-insert-thumb elt nil)))
0658b86f Richard M. Stallman2004-05-29 15:22:55 +0000629 (when (eolp) (forward-char)))
d1c43637 Juanma Barranquero2004-04-27 22:43:35 +0000630
2ff89cf7
NR
Nick Roberts2006-01-24 20:54:23 +0000631;; cleaning of old temp files
632(mapc 'delete-file
633 (directory-files (thumbs-temp-dir) t thumbs-temp-prefix))
634
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000635;; Image modification routines
636
637(defun thumbs-modify-image (action &optional arg)
638 "Call convert to do ACTION on image with argument ARG.
932fb767 Werner LEMBERG2005-03-25 09:49:00 +0000639ACTION and ARG should be a valid convert command."
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000640 (interactive "sAction: \nsValue: ")
2ff89cf7
NR
Nick Roberts2006-01-24 20:54:23 +0000641 (let* ((buffer-read-only nil)
642 (old thumbs-current-tmp-filename)
643 (tmp (thumbs-temp-file)))
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000644 (erase-buffer)
2ff89cf7 Nick Roberts2006-01-24 20:54:23 +0000645 (thumbs-call-convert (or old thumbs-current-image-filename)
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000646 tmp
647 action
648 (or arg ""))
2ff89cf7
NR
Nick Roberts2006-01-24 20:54:23 +0000649 (save-excursion
650 (thumbs-insert-image tmp 'jpeg 0))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000651 (setq thumbs-current-tmp-filename tmp)))
652
653(defun thumbs-emboss-image (emboss)
654 "Emboss the image with value EMBOSS."
655 (interactive "nEmboss value: ")
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000656 (if (or (< emboss 3) (> emboss 31) (zerop (% emboss 2)))
657 (error "Arg must be an odd number between 3 and 31"))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000658 (thumbs-modify-image "emboss" (number-to-string emboss)))
659
660(defun thumbs-monochrome-image ()
661 "Turn the image to monochrome."
662 (interactive)
663 (thumbs-modify-image "monochrome"))
664
665(defun thumbs-negate-image ()
666 "Negate the image."
667 (interactive)
668 (thumbs-modify-image "negate"))
669
670(defun thumbs-rotate-left ()
671 "Rotate the image 90 degrees counter-clockwise."
672 (interactive)
673 (thumbs-modify-image "rotate" "270"))
674
675(defun thumbs-rotate-right ()
676 "Rotate the image 90 degrees clockwise."
677 (interactive)
678 (thumbs-modify-image "rotate" "90"))
679
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000680(defun thumbs-current-image ()
681 "Return the name of the image file name at point."
682 (get-text-property (point) 'thumb-image-file))
683
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000684(defun thumbs-forward-char ()
685 "Move forward one image."
686 (interactive)
687 (forward-char)
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000688 (while (and (not (eobp)) (not (thumbs-current-image)))
689 (forward-char))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000690 (thumbs-show-name))
691
692(defun thumbs-backward-char ()
693 "Move backward one image."
694 (interactive)
695 (forward-char -1)
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000696 (while (and (not (bobp)) (not (thumbs-current-image)))
697 (forward-char -1))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000698 (thumbs-show-name))
699
dd2a3d13
NR
Nick Roberts2006-01-23 23:16:58 +0000700(defun thumbs-backward-line ()
701 "Move up one line."
702 (interactive)
703 (forward-line -1)
704 (thumbs-show-name))
705
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000706(defun thumbs-forward-line ()
707 "Move down one line."
708 (interactive)
709 (forward-line 1)
710 (thumbs-show-name))
711
dd2a3d13
NR
Nick Roberts2006-01-23 23:16:58 +0000712(defun thumbs-show-more-images (&optional arg)
713 "Show more than `thumbs-max-image-number' images, if present."
714 (interactive "P")
715 (or arg (setq arg 1))
716 (setq thumbs-extra-images (+ thumbs-extra-images arg))
717 (thumbs-dired-show))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000718
719(defun thumbs-show-name ()
720 "Show the name of the current file."
721 (interactive)
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000722 (let ((f (thumbs-current-image)))
723 (and f (message "%s [%s]" f (thumbs-file-size f)))))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000724
725(defun thumbs-save-current-image ()
726 "Save the current image."
727 (interactive)
728 (let ((f (or thumbs-current-tmp-filename
729 thumbs-current-image-filename))
0658b86f Richard M. Stallman2004-05-29 15:22:55 +0000730 (sa (read-from-minibuffer "Save image file as: "
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000731 thumbs-current-image-filename)))
732 (copy-file f sa)))
733
734(defun thumbs-dired ()
735 "Use `dired' on the current thumbs directory."
736 (interactive)
737 (dired thumbs-current-dir))
738
739;; thumbs-mode
740
741(defvar thumbs-mode-map
742 (let ((map (make-sparse-keymap)))
743 (define-key map [return] 'thumbs-find-image-at-point)
0658b86f Richard M. Stallman2004-05-29 15:22:55 +0000744 (define-key map [mouse-2] 'thumbs-mouse-find-image)
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000745 (define-key map [(meta return)] 'thumbs-find-image-at-point-other-window)
746 (define-key map [(control return)] 'thumbs-set-image-at-point-to-root-window)
747 (define-key map [delete] 'thumbs-delete-images)
748 (define-key map [right] 'thumbs-forward-char)
749 (define-key map [left] 'thumbs-backward-char)
750 (define-key map [up] 'thumbs-backward-line)
751 (define-key map [down] 'thumbs-forward-line)
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000752 (define-key map "+" 'thumbs-show-more-images)
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000753 (define-key map "d" 'thumbs-dired)
754 (define-key map "m" 'thumbs-mark)
0658b86f
RS
Richard M. Stallman2004-05-29 15:22:55 +0000755 (define-key map "u" 'thumbs-unmark)
756 (define-key map "R" 'thumbs-rename-images)
757 (define-key map "x" 'thumbs-delete-images)
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000758 (define-key map "s" 'thumbs-show-name)
759 (define-key map "q" 'thumbs-kill-buffer)
760 map)
761 "Keymap for `thumbs-mode'.")
762
0658b86f Richard M. Stallman2004-05-29 15:22:55 +0000763(put 'thumbs-mode 'mode-class 'special)
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000764(define-derived-mode thumbs-mode
765 fundamental-mode "thumbs"
766 "Preview images in a thumbnails buffer"
0868cd70 Nick Roberts2006-01-30 09:52:49 +0000767 (setq buffer-read-only t))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000768
769(defvar thumbs-view-image-mode-map
770 (let ((map (make-sparse-keymap)))
771 (define-key map [prior] 'thumbs-previous-image)
772 (define-key map [next] 'thumbs-next-image)
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000773 (define-key map "^" 'thumbs-display-thumbs-buffer)
2ff89cf7
NR
Nick Roberts2006-01-24 20:54:23 +0000774 (define-key map "-" 'thumbs-shrink-image)
775 (define-key map "+" 'thumbs-enlarge-image)
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000776 (define-key map "<" 'thumbs-rotate-left)
777 (define-key map ">" 'thumbs-rotate-right)
778 (define-key map "e" 'thumbs-emboss-image)
2ff89cf7 Nick Roberts2006-01-24 20:54:23 +0000779 (define-key map "r" 'thumbs-resize-image)
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000780 (define-key map "s" 'thumbs-save-current-image)
781 (define-key map "q" 'thumbs-kill-buffer)
288f8357 John Paul Wallington2004-09-04 18:34:30 +0000782 (define-key map "w" 'thumbs-set-root)
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000783 map)
784 "Keymap for `thumbs-view-image-mode'.")
785
786;; thumbs-view-image-mode
0658b86f Richard M. Stallman2004-05-29 15:22:55 +0000787(put 'thumbs-view-image-mode 'mode-class 'special)
ab6d47ea Richard M. Stallman2004-02-28 17:47:26 +0000788(define-derived-mode thumbs-view-image-mode
288f8357
JPW
John Paul Wallington2004-09-04 18:34:30 +0000789 fundamental-mode "image-view-mode"
790 (setq buffer-read-only t))
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000791
792;;;###autoload
793(defun thumbs-dired-setroot ()
f75049fc Juanma Barranquero2005-05-25 14:09:14 +0000794 "In dired, call the setroot program on the image at point."
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000795 (interactive)
796 (thumbs-call-setroot-command (dired-get-filename)))
797
798;; Modif to dired mode map
dd2a3d13 Nick Roberts2006-01-23 23:16:58 +0000799(define-key dired-mode-map "\C-ta" 'thumbs-dired-show)
ab6d47ea
RS
Richard M. Stallman2004-02-28 17:47:26 +0000800(define-key dired-mode-map "\C-tm" 'thumbs-dired-show-marked)
801(define-key dired-mode-map "\C-tw" 'thumbs-dired-setroot)
802
803(provide 'thumbs)
804
805;;; thumbs.el ends here