*** empty log message ***
[bpt/emacs.git] / lisp / thumbs.el
CommitLineData
ab6d47ea 1;;; thumbs.el --- Thumbnails previewer for images files
0658b86f 2
0d30b337 3;; Copyright (C) 2004, 2005 Free Software Foundation, Inc.
0658b86f 4
ab6d47ea 5;; Author: Jean-Philippe Theberge <jphiltheberge@videotron.ca>
ab6d47ea
RS
6;; Keywords: Multimedia
7
ab6d47ea
RS
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
22;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
ab6d47ea
RS
24
25;;; Commentary:
26
dd2a3d13
NR
27;; This package create two new modes: thumbs-mode and thumbs-view-image-mode.
28;; It is used for basic browsing and viewing of images from within Emacs.
29;; Minimal image manipulation functions are also available via external
30;; programs. If you want to do more complex tasks like categorise and tag
31;; your images, use tumme.el
ab6d47ea
RS
32;;
33;; The 'convert' program from 'ImageMagick'
34;; [URL:http://www.imagemagick.org/] is required.
35;;
dd2a3d13
NR
36;; Thanks: Alex Schroeder <alex@gnu.org> for maintaining the package at some
37;; time. The peoples at #emacs@freenode.net for numerous help. RMS
38;; for emacs and the GNU project.
39;;
ab6d47ea 40;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
dd2a3d13 41;;
ab6d47ea
RS
42;; CHANGELOG
43;;
44;; This is version 2.0
45;;
46;; USAGE
47;;
48;; Type M-x thumbs RET DIR RET to view the directory DIR in Thumbs mode.
49;; That should be a directory containing image files.
50;; from dired, C-t m enter in thumbs-mode with all marked files
51;; C-t a enter in thumbs-mode with all files in current-directory
dd2a3d13
NR
52;; In thumbs-mode, pressing <return> on a image will bring you in image view
53;; mode for that image. C-h m will give you a list of available keybinding.
ab6d47ea
RS
54
55;;; History:
d1c43637 56;;
ab6d47ea
RS
57
58;;; Code:
59
60(require 'dired)
61
ab6d47ea
RS
62;; CUSTOMIZATIONS
63
64(defgroup thumbs nil
65 "Thumbnails previewer."
bf247b6e 66 :version "22.1"
ab6d47ea
RS
67 :group 'multimedia)
68
6c060884 69(defcustom thumbs-thumbsdir "~/.emacs.d/thumbs"
ab6d47ea
RS
70 "*Directory to store thumbnails."
71 :type 'directory
72 :group 'thumbs)
73
74(defcustom thumbs-geometry "100x100"
75 "*Size of thumbnails."
76 :type 'string
77 :group 'thumbs)
78
dd2a3d13
NR
79(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 86 :type 'integer
ab6d47ea
RS
87 :group 'thumbs)
88
89(defcustom thumbs-thumbsdir-max-size 50000000
dd2a3d13 90 "Maximum size for thumbnails directory.
b3c70578 91When it reaches that size (in bytes), a warning is sent."
67ed6461 92 :type 'integer
ab6d47ea
RS
93 :group 'thumbs)
94
95(defcustom thumbs-conversion-program
f271d3c7 96 (if (eq system-type 'windows-nt)
ab6d47ea
RS
97 "convert.exe"
98 (or (executable-find "convert")
99 "/usr/X11R6/bin/convert"))
100 "*Name of conversion program for thumbnails generation.
101It must be 'convert'."
102 :type 'string
103 :group 'thumbs)
104
105(defcustom thumbs-setroot-command
106 "xloadimage -onroot -fullscreen *"
107 "Command to set the root window."
108 :type 'string
109 :group 'thumbs)
110
111(defcustom thumbs-relief 5
112 "*Size of button-like border around thumbnails."
67ed6461 113 :type 'integer
ab6d47ea
RS
114 :group 'thumbs)
115
116(defcustom thumbs-margin 2
117 "*Size of the margin around thumbnails.
118This is where you see the cursor."
67ed6461 119 :type 'integer
ab6d47ea
RS
120 :group 'thumbs)
121
122(defcustom thumbs-thumbsdir-auto-clean t
123 "If set, delete older file in the thumbnails directory.
124Deletion is done at load time when the directory size is bigger
b3c70578 125than `thumbs-thumbsdir-max-size'."
ab6d47ea
RS
126 :type 'boolean
127 :group 'thumbs)
128
129(defcustom thumbs-image-resizing-step 10
b3c70578 130 "Step by which to resize image."
67ed6461 131 :type 'integer
ab6d47ea
RS
132 :group 'thumbs)
133
f271d3c7 134(defcustom thumbs-temp-dir temporary-file-directory
ab6d47ea 135 "Temporary directory to use.
f271d3c7
JB
136Defaults to `temporary-file-directory'. Leaving it to
137this value can let another user see some of your images."
ab6d47ea
RS
138 :type 'directory
139 :group 'thumbs)
140
141(defcustom thumbs-temp-prefix "emacsthumbs"
142 "Prefix to add to temp files."
143 :type 'string
144 :group 'thumbs)
145
146;; Initialize some variable, for later use.
6ced5724 147(defvar thumbs-current-tmp-filename nil
ab6d47ea 148 "Temporary filename of current image.")
6ced5724
JB
149(make-variable-buffer-local 'thumbs-current-tmp-filename)
150
151(defvar thumbs-current-image-filename nil
ab6d47ea 152 "Filename of current image.")
6ced5724
JB
153(make-variable-buffer-local 'thumbs-current-image-filename)
154
dd2a3d13
NR
155(defvar thumbs-extra-images 1
156 "Counter for showing extra images in thumbs buffer.")
157(make-variable-buffer-local 'thumbs-extra-images)
158(put 'thumbs-extra-images 'permanent-local t)
159
6ced5724 160(defvar thumbs-current-image-size nil
ab6d47ea 161 "Size of current image.")
6ced5724
JB
162
163(defvar thumbs-image-num nil
ab6d47ea 164 "Number of current image.")
6ced5724
JB
165(make-variable-buffer-local 'thumbs-image-num)
166
c897e76f
NR
167(defvar thumbs-buffer nil
168 "Name of buffer containing thumbnails associated with image.")
169(make-variable-buffer-local 'thumbs-buffer)
170
6ced5724 171(defvar thumbs-current-dir nil
ab6d47ea 172 "Current directory.")
6ced5724 173
dd2a3d13 174(defvar thumbs-marked-list nil
ab6d47ea
RS
175 "List of marked files.")
176
f271d3c7
JB
177(defalias 'thumbs-gensym
178 (if (fboundp 'gensym)
179 'gensym
180 ;; Copied from cl-macs.el
181 (defvar thumbs-gensym-counter 0)
182 (lambda (&optional prefix)
183 "Generate a new uninterned symbol.
184The name is made by appending a number to PREFIX, default \"G\"."
185 (let ((pfix (if (stringp prefix) prefix "G"))
186 (num (if (integerp prefix) prefix
187 (prog1 thumbs-gensym-counter
188 (setq thumbs-gensym-counter
189 (1+ thumbs-gensym-counter))))))
190 (make-symbol (format "%s%d" pfix num))))))
191
6cae5f24
JB
192(defsubst thumbs-temp-dir ()
193 (file-name-as-directory (expand-file-name thumbs-temp-dir)))
194
f271d3c7
JB
195(defun thumbs-temp-file ()
196 "Return a unique temporary filename for an image."
197 (format "%s%s-%s.jpg"
6cae5f24 198 (thumbs-temp-dir)
f271d3c7
JB
199 thumbs-temp-prefix
200 (thumbs-gensym "T")))
201
202(defun thumbs-thumbsdir ()
203 "Return the current thumbnails directory (from `thumbs-thumbsdir').
204Create the thumbnails directory if it does not exist."
6cae5f24
JB
205 (let ((thumbs-thumbsdir (file-name-as-directory
206 (expand-file-name thumbs-thumbsdir))))
f271d3c7 207 (unless (file-directory-p thumbs-thumbsdir)
6c060884 208 (make-directory thumbs-thumbsdir t)
f271d3c7
JB
209 (message "Creating thumbnails directory"))
210 thumbs-thumbsdir))
ab6d47ea
RS
211
212(defun thumbs-cleanup-thumbsdir ()
213 "Clean the thumbnails directory.
f75049fc
JB
214If the total size of all files in `thumbs-thumbsdir' is bigger than
215`thumbs-thumbsdir-max-size', files are deleted until the max size is
ab6d47ea 216reached."
dd2a3d13 217 (let* ((files-list
ab6d47ea
RS
218 (sort
219 (mapcar
220 (lambda (f)
dd2a3d13
NR
221 (let ((fattribs-list (file-attributes f)))
222 `(,(nth 4 fattribs-list) ,(nth 7 fattribs-list) ,f)))
f271d3c7
JB
223 (directory-files (thumbs-thumbsdir) t (image-file-name-regexp)))
224 '(lambda (l1 l2) (time-less-p (car l1) (car l2)))))
dd2a3d13 225 (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) files-list))))
ab6d47ea
RS
226 (while (> dirsize thumbs-thumbsdir-max-size)
227 (progn
dd2a3d13
NR
228 (message "Deleting file %s" (cadr (cdar files-list))))
229 (delete-file (cadr (cdar files-list)))
230 (setq dirsize (- dirsize (car (cdar files-list))))
231 (setq files-list (cdr files-list)))))
ab6d47ea
RS
232
233;; Check the thumbsnail directory size and clean it if necessary.
234(when thumbs-thumbsdir-auto-clean
235 (thumbs-cleanup-thumbsdir))
236
237(defun thumbs-call-convert (filein fileout action
238 &optional arg output-format action-prefix)
239 "Call the convert program.
240FILEIN is the input file,
241FILEOUT is the output file,
242ACTION is the command to send to convert.
f75049fc 243Optional arguments are:
ab6d47ea 244ARG any arguments to the ACTION command,
f75049fc 245OUTPUT-FORMAT is the file format to output (default is jpeg),
ab6d47ea 246ACTION-PREFIX is the symbol to place before the ACTION command
f75049fc 247 (defaults to '-' but can sometimes be '+')."
ab6d47ea
RS
248 (let ((command (format "%s %s%s %s \"%s\" \"%s:%s\""
249 thumbs-conversion-program
250 (or action-prefix "-")
251 action
252 (or arg "")
253 filein
254 (or output-format "jpeg")
255 fileout)))
dd2a3d13 256 (call-process shell-file-name nil nil nil "-c" command)))
ab6d47ea
RS
257
258(defun thumbs-increment-image-size-element (n d)
259 "Increment number N by D percent."
260 (round (+ n (/ (* d n) 100))))
261
262(defun thumbs-decrement-image-size-element (n d)
263 "Decrement number N by D percent."
264 (round (- n (/ (* d n) 100))))
265
266(defun thumbs-increment-image-size (s)
f75049fc 267 "Increment S (a cons of width x height)."
ab6d47ea
RS
268 (cons
269 (thumbs-increment-image-size-element (car s)
270 thumbs-image-resizing-step)
271 (thumbs-increment-image-size-element (cdr s)
272 thumbs-image-resizing-step)))
d1c43637 273
ab6d47ea 274(defun thumbs-decrement-image-size (s)
f75049fc 275 "Decrement S (a cons of width x height)."
ab6d47ea
RS
276 (cons
277 (thumbs-decrement-image-size-element (car s)
278 thumbs-image-resizing-step)
279 (thumbs-decrement-image-size-element (cdr s)
280 thumbs-image-resizing-step)))
281
282(defun thumbs-resize-image (&optional increment size)
283 "Resize image in current buffer.
f271d3c7 284If INCREMENT is set, make the image bigger, else smaller.
ab6d47ea
RS
285Or, alternatively, a SIZE may be specified."
286 (interactive)
287 ;; cleaning of old temp file
0658b86f 288 (condition-case nil
ab6d47ea
RS
289 (apply 'delete-file
290 (directory-files
6cae5f24 291 (thumbs-temp-dir) t
0658b86f
RS
292 thumbs-temp-prefix))
293 (error nil))
ab6d47ea
RS
294 (let ((buffer-read-only nil)
295 (x (if size
296 size
297 (if increment
298 (thumbs-increment-image-size
299 thumbs-current-image-size)
300 (thumbs-decrement-image-size
301 thumbs-current-image-size))))
f271d3c7 302 (tmp (thumbs-temp-file)))
ab6d47ea
RS
303 (erase-buffer)
304 (thumbs-call-convert thumbs-current-image-filename
305 tmp "sample"
306 (concat (number-to-string (car x)) "x"
307 (number-to-string (cdr x))))
308 (thumbs-insert-image tmp 'jpeg 0)
309 (setq thumbs-current-tmp-filename tmp)))
310
311(defun thumbs-resize-interactive (width height)
f271d3c7 312 "Resize image interactively to specified WIDTH and HEIGHT."
ab6d47ea
RS
313 (interactive "nWidth: \nnHeight: ")
314 (thumbs-resize-image nil (cons width height)))
d1c43637 315
ab6d47ea
RS
316(defun thumbs-resize-image-size-down ()
317 "Resize image (smaller)."
318 (interactive)
319 (thumbs-resize-image nil))
320
321(defun thumbs-resize-image-size-up ()
322 "Resize image (bigger)."
323 (interactive)
324 (thumbs-resize-image t))
325
ab6d47ea
RS
326(defun thumbs-thumbname (img)
327 "Return a thumbnail name for the image IMG."
0df06447
JB
328 (convert-standard-filename
329 (let ((filename (expand-file-name img)))
6cae5f24 330 (format "%s%08x-%s.jpg"
f271d3c7 331 (thumbs-thumbsdir)
0df06447
JB
332 (sxhash filename)
333 (subst-char-in-string
334 ?\s ?\_
335 (apply
336 'concat
337 (split-string filename "/")))))))
ab6d47ea
RS
338
339(defun thumbs-make-thumb (img)
340 "Create the thumbnail for IMG."
0df06447
JB
341 (let ((fn (expand-file-name img))
342 (tn (thumbs-thumbname img)))
ab6d47ea 343 (if (or (not (file-exists-p tn))
0658b86f
RS
344 ;; This is not the right fix, but I don't understand
345 ;; the external program or why it produces a geometry
346 ;; unequal to the one requested -- rms.
347;;; (not (equal (thumbs-file-size tn) thumbs-geometry))
348 )
ab6d47ea
RS
349 (thumbs-call-convert fn tn "sample" thumbs-geometry))
350 tn))
d1c43637 351
ab6d47ea
RS
352(defun thumbs-image-type (img)
353 "Return image type from filename IMG."
354 (cond ((string-match ".*\\.jpe?g\\'" img) 'jpeg)
355 ((string-match ".*\\.xpm\\'" img) 'xpm)
356 ((string-match ".*\\.xbm\\'" img) 'xbm)
24bdbffe 357 ((string-match ".*\\.pbm\\'" img) 'pbm)
ab6d47ea
RS
358 ((string-match ".*\\.gif\\'" img) 'gif)
359 ((string-match ".*\\.bmp\\'" img) 'bmp)
360 ((string-match ".*\\.png\\'" img) 'png)
361 ((string-match ".*\\.tiff?\\'" img) 'tiff)))
362
363(defun thumbs-file-size (img)
364 (let ((i (image-size (find-image `((:type ,(thumbs-image-type img) :file ,img))) t)))
365 (concat (number-to-string (round (car i)))
366 "x"
367 (number-to-string (round (cdr i))))))
d1c43637 368
ab6d47ea
RS
369;;;###autoload
370(defun thumbs-find-thumb (img)
371 "Display the thumbnail for IMG."
372 (interactive "f")
373 (find-file (thumbs-make-thumb img)))
374
375(defun thumbs-insert-image (img type relief &optional marked)
376 "Insert image IMG at point.
377TYPE and RELIEF will be used in constructing the image; see `image'
378in the emacs-lisp manual for further documentation.
f75049fc 379If MARKED is non-nil, the image is marked."
ab6d47ea
RS
380 (let ((i `(image :type ,type
381 :file ,img
382 :relief ,relief
383 :conversion ,(if marked 'disabled)
384 :margin ,thumbs-margin)))
385 (insert-image i)
6ced5724
JB
386 (set (make-local-variable 'thumbs-current-image-size)
387 (image-size i t))))
ab6d47ea
RS
388
389(defun thumbs-insert-thumb (img &optional marked)
390 "Insert the thumbnail for IMG at point.
f75049fc 391If MARKED is non-nil, the image is marked."
ab6d47ea 392 (thumbs-insert-image
0658b86f 393 (thumbs-make-thumb img) 'jpeg thumbs-relief marked)
dd2a3d13
NR
394 (add-text-properties (1- (point)) (point)
395 `(thumb-image-file ,img
396 help-echo ,(file-name-nondirectory img))))
397
398(defun thumbs-do-thumbs-insertion (list)
399 "Insert all thumbnails into thumbs buffer."
400 (let* ((i 0)
401 (length (length list))
402 (diff (- length (* thumbs-max-image-number thumbs-extra-images))))
403 (nbutlast list diff)
404 (dolist (img list)
0658b86f 405 (thumbs-insert-thumb img
dd2a3d13 406 (member img thumbs-marked-list))
ab6d47ea 407 (when (= 0 (mod (setq i (1+ i)) thumbs-per-line))
0658b86f 408 (newline)))
dd2a3d13
NR
409 (unless (bobp) (newline))
410 (if diff (message "Type + to display more images."))))
ab6d47ea 411
dd2a3d13 412(defun thumbs-show-thumbs-list (list &optional dir same-window)
0df06447
JB
413 (unless (and (display-images-p)
414 (image-type-available-p 'jpeg))
415 (error "Required image type is not supported in this Emacs session"))
ab6d47ea 416 (funcall (if same-window 'switch-to-buffer 'pop-to-buffer)
dd2a3d13 417 (if dir (concat "*Thumbs: " dir) "*THUMB-View*"))
ab6d47ea
RS
418 (let ((inhibit-read-only t))
419 (erase-buffer)
420 (thumbs-mode)
dd2a3d13
NR
421 (if dir (setq default-directory dir))
422 (thumbs-do-thumbs-insertion list)
ab6d47ea 423 (goto-char (point-min))
6ced5724 424 (set (make-local-variable 'thumbs-current-dir) default-directory)))
ab6d47ea
RS
425
426;;;###autoload
dd2a3d13 427(defun thumbs-show-from-dir (dir &optional reg same-window)
ab6d47ea
RS
428 "Make a preview buffer for all images in DIR.
429Optional argument REG to select file matching a regexp,
430and SAME-WINDOW to show thumbs in the same window."
431 (interactive "DDir: ")
432 (thumbs-show-thumbs-list
dd2a3d13
NR
433 (directory-files dir t (or reg (image-file-name-regexp)))
434 dir same-window))
ab6d47ea
RS
435
436;;;###autoload
437(defun thumbs-dired-show-marked ()
dd2a3d13 438 "In dired, make a thumbs buffer with marked files."
ab6d47ea
RS
439 (interactive)
440 (thumbs-show-thumbs-list (dired-get-marked-files) nil t))
441
442;;;###autoload
dd2a3d13 443(defun thumbs-dired-show ()
ab6d47ea
RS
444 "In dired, make a thumbs buffer with all files in current directory."
445 (interactive)
dd2a3d13 446 (thumbs-show-from-dir default-directory nil t))
ab6d47ea
RS
447
448;;;###autoload
dd2a3d13 449(defalias 'thumbs 'thumbs-show-from-dir)
ab6d47ea 450
0658b86f 451(defun thumbs-find-image (img &optional num otherwin)
c897e76f
NR
452 (let ((buffer (current-buffer)))
453 (funcall
454 (if otherwin 'switch-to-buffer-other-window 'switch-to-buffer)
455 "*Image*")
456 (thumbs-view-image-mode)
457 (setq mode-name
458 (concat "image-view-mode: " (file-name-nondirectory img)
459 " - " (number-to-string num)))
460 (setq thumbs-buffer buffer)
461 (let ((inhibit-read-only t))
462 (setq thumbs-current-image-filename img
463 thumbs-current-tmp-filename nil
464 thumbs-image-num (or num 0))
465 (delete-region (point-min)(point-max))
466 (save-excursion
467 (thumbs-insert-image img (thumbs-image-type img) 0)))))
ab6d47ea
RS
468
469(defun thumbs-find-image-at-point (&optional img otherwin)
470 "Display image IMG for thumbnail at point.
f75049fc 471Use another window if OTHERWIN is t."
ab6d47ea 472 (interactive)
0658b86f
RS
473 (let* ((i (or img (thumbs-current-image))))
474 (thumbs-find-image i (point) otherwin)))
ab6d47ea
RS
475
476(defun thumbs-find-image-at-point-other-window ()
477 "Display image for thumbnail at point in the preview buffer.
478Open another window."
479 (interactive)
480 (thumbs-find-image-at-point nil t))
481
0658b86f
RS
482(defun thumbs-mouse-find-image (event)
483 "Display image for thumbnail at mouse click EVENT."
484 (interactive "e")
485 (mouse-set-point event)
486 (thumbs-find-image-at-point))
487
ab6d47ea
RS
488(defun thumbs-call-setroot-command (img)
489 "Call the setroot program for IMG."
490 (run-hooks 'thumbs-before-setroot-hook)
491 (shell-command (replace-regexp-in-string
492 "\\*"
493 (shell-quote-argument (expand-file-name img))
494 thumbs-setroot-command nil t))
495 (run-hooks 'thumbs-after-setroot-hook))
d1c43637 496
ab6d47ea
RS
497(defun thumbs-set-image-at-point-to-root-window ()
498 "Set the image at point as the desktop wallpaper."
499 (interactive)
0658b86f
RS
500 (thumbs-call-setroot-command
501 (thumbs-current-image)))
ab6d47ea
RS
502
503(defun thumbs-set-root ()
504 "Set the current image as root."
505 (interactive)
506 (thumbs-call-setroot-command
507 (or thumbs-current-tmp-filename
508 thumbs-current-image-filename)))
509
0658b86f
RS
510(defun thumbs-file-alist ()
511 "Make an alist of elements (POS . FILENAME) for all images in thumb buffer."
c897e76f
NR
512 (with-current-buffer thumbs-buffer
513 (save-excursion
514 (let (list)
515 (goto-char (point-min))
516 (while (not (eobp))
1acadcfe
NR
517 (unless (= 0 (mod (point) (1+ thumbs-per-line)))
518 (if (thumbs-current-image)
519 (push (cons (point-marker)
520 (thumbs-current-image))
521 list)))
c897e76f
NR
522 (forward-char 1))
523 (nreverse list)))))
0658b86f
RS
524
525(defun thumbs-file-list ()
526 "Make a list of file names for all images in thumb buffer."
527 (save-excursion
528 (let (list)
529 (goto-char (point-min))
530 (while (not (eobp))
531 (if (thumbs-current-image)
532 (push (thumbs-current-image) list))
533 (forward-char 1))
534 (nreverse list))))
535
ab6d47ea 536(defun thumbs-delete-images ()
f75049fc 537 "Delete the image at point (and its thumbnail) (or marked files if any)."
ab6d47ea 538 (interactive)
dd2a3d13 539 (let ((files (or thumbs-marked-list (list (thumbs-current-image)))))
0658b86f 540 (if (yes-or-no-p (format "Really delete %d files? " (length files)))
dd2a3d13 541 (let ((thumbs-file-list (thumbs-file-alist))
0658b86f
RS
542 (inhibit-read-only t))
543 (dolist (x files)
544 (let (failure)
545 (condition-case ()
546 (progn
ab6d47ea 547 (delete-file x)
0658b86f
RS
548 (delete-file (thumbs-thumbname x)))
549 (file-error (setq failure t)))
550 (unless failure
dd2a3d13
NR
551 (when (rassoc x thumbs-file-list)
552 (goto-char (car (rassoc x thumbs-file-list)))
0658b86f 553 (delete-region (point) (1+ (point))))
dd2a3d13
NR
554 (setq thumbs-marked-list
555 (delq x thumbs-marked-list)))))))))
0658b86f
RS
556
557(defun thumbs-rename-images (newfile)
f75049fc 558 "Rename the image at point (and its thumbnail) (or marked files if any)."
0658b86f 559 (interactive "FRename to file or directory: ")
dd2a3d13 560 (let ((files (or thumbs-marked-list (list (thumbs-current-image))))
0658b86f
RS
561 failures)
562 (if (and (not (file-directory-p newfile))
dd2a3d13 563 thumbs-marked-list)
0658b86f
RS
564 (if (file-exists-p newfile)
565 (error "Renaming marked files to file name `%s'" newfile)
566 (make-directory newfile t)))
567 (if (yes-or-no-p (format "Really rename %d files? " (length files)))
dd2a3d13 568 (let ((thumbs-file-list (thumbs-file-alist))
0658b86f
RS
569 (inhibit-read-only t))
570 (dolist (file files)
571 (let (failure)
572 (condition-case ()
573 (if (file-directory-p newfile)
574 (rename-file file
575 (expand-file-name
576 (file-name-nondirectory file)
577 newfile))
578 (rename-file file newfile))
579 (file-error (setq failure t)
580 (push file failures)))
581 (unless failure
dd2a3d13
NR
582 (when (rassoc file thumbs-file-list)
583 (goto-char (car (rassoc file thumbs-file-list)))
0658b86f 584 (delete-region (point) (1+ (point))))
dd2a3d13
NR
585 (setq thumbs-marked-list
586 (delq file thumbs-marked-list)))))))
0658b86f
RS
587 (if failures
588 (display-warning 'file-error
589 (format "Rename failures for %s into %s"
590 failures newfile)
591 :error))))
ab6d47ea
RS
592
593(defun thumbs-kill-buffer ()
594 "Kill the current buffer."
595 (interactive)
f271d3c7 596 (quit-window t (selected-window)))
ab6d47ea
RS
597
598(defun thumbs-show-image-num (num)
599 "Show the image with number NUM."
0658b86f 600 (let ((image-buffer (get-buffer-create "*Image*")))
c897e76f 601 (let ((img (cdr (nth (1- num) (thumbs-file-alist)))))
0658b86f 602 (with-current-buffer image-buffer
c897e76f
NR
603 (setq mode-name
604 (concat "image-view-mode: " (file-name-nondirectory img)
605 " - " (number-to-string num)))
606 (let ((inhibit-read-only t))
607 (erase-buffer)
608 (thumbs-insert-image img (thumbs-image-type img) 0)
609 (goto-char (point-min))))
d42799bd 610 (setq thumbs-image-num num
c897e76f 611 thumbs-current-image-filename img))))
ab6d47ea 612
dd2a3d13
NR
613(defun thumbs-previous-image ()
614 "Show the previous image."
615 (interactive)
616 (let* ((i (- thumbs-image-num 1))
617 (number (length (thumbs-file-alist))))
618 (if (= i 0) (setq i (1- number)))
619 (thumbs-show-image-num i)))
620
ab6d47ea 621(defun thumbs-next-image ()
f75049fc 622 "Show the next image."
ab6d47ea
RS
623 (interactive)
624 (let* ((i (1+ thumbs-image-num))
c897e76f
NR
625 (number (length (thumbs-file-alist))))
626 (if (= i number) (setq i 1))
0658b86f 627 (thumbs-show-image-num i)))
ab6d47ea 628
dd2a3d13
NR
629(defun thumbs-display-thumbs-buffer ()
630 "Display the associated thumbs buffer."
ab6d47ea 631 (interactive)
dd2a3d13 632 (display-buffer thumbs-buffer))
ab6d47ea
RS
633
634(defun thumbs-redraw-buffer ()
635 "Redraw the current thumbs buffer."
636 (let ((p (point))
0658b86f
RS
637 (inhibit-read-only t)
638 (files (thumbs-file-list)))
639 (erase-buffer)
640 (thumbs-do-thumbs-insertion files)
641 (goto-char p)))
d1c43637 642
ab6d47ea
RS
643(defun thumbs-mark ()
644 "Mark the image at point."
645 (interactive)
0658b86f
RS
646 (let ((elt (thumbs-current-image)))
647 (unless elt
648 (error "No image here"))
dd2a3d13 649 (push elt thumbs-marked-list)
0658b86f
RS
650 (let ((inhibit-read-only t))
651 (delete-char 1)
652 (thumbs-insert-thumb elt t)))
653 (when (eolp) (forward-char)))
654
655(defun thumbs-unmark ()
656 "Unmark the image at point."
657 (interactive)
658 (let ((elt (thumbs-current-image)))
659 (unless elt
660 (error "No image here"))
dd2a3d13 661 (setq thumbs-marked-list (delete elt thumbs-marked-list))
0658b86f
RS
662 (let ((inhibit-read-only t))
663 (delete-char 1)
664 (thumbs-insert-thumb elt nil)))
665 (when (eolp) (forward-char)))
d1c43637 666
ab6d47ea
RS
667;; Image modification routines
668
669(defun thumbs-modify-image (action &optional arg)
670 "Call convert to do ACTION on image with argument ARG.
932fb767 671ACTION and ARG should be a valid convert command."
ab6d47ea
RS
672 (interactive "sAction: \nsValue: ")
673 ;; cleaning of old temp file
674 (mapc 'delete-file
675 (directory-files
6cae5f24 676 (thumbs-temp-dir)
ab6d47ea
RS
677 t
678 thumbs-temp-prefix))
679 (let ((buffer-read-only nil)
f271d3c7 680 (tmp (thumbs-temp-file)))
ab6d47ea
RS
681 (erase-buffer)
682 (thumbs-call-convert thumbs-current-image-filename
683 tmp
684 action
685 (or arg ""))
686 (thumbs-insert-image tmp 'jpeg 0)
687 (setq thumbs-current-tmp-filename tmp)))
688
689(defun thumbs-emboss-image (emboss)
690 "Emboss the image with value EMBOSS."
691 (interactive "nEmboss value: ")
0658b86f
RS
692 (if (or (< emboss 3) (> emboss 31) (zerop (% emboss 2)))
693 (error "Arg must be an odd number between 3 and 31"))
ab6d47ea
RS
694 (thumbs-modify-image "emboss" (number-to-string emboss)))
695
696(defun thumbs-monochrome-image ()
697 "Turn the image to monochrome."
698 (interactive)
699 (thumbs-modify-image "monochrome"))
700
701(defun thumbs-negate-image ()
702 "Negate the image."
703 (interactive)
704 (thumbs-modify-image "negate"))
705
706(defun thumbs-rotate-left ()
707 "Rotate the image 90 degrees counter-clockwise."
708 (interactive)
709 (thumbs-modify-image "rotate" "270"))
710
711(defun thumbs-rotate-right ()
712 "Rotate the image 90 degrees clockwise."
713 (interactive)
714 (thumbs-modify-image "rotate" "90"))
715
0658b86f
RS
716(defun thumbs-current-image ()
717 "Return the name of the image file name at point."
718 (get-text-property (point) 'thumb-image-file))
719
ab6d47ea
RS
720(defun thumbs-forward-char ()
721 "Move forward one image."
722 (interactive)
723 (forward-char)
0658b86f
RS
724 (while (and (not (eobp)) (not (thumbs-current-image)))
725 (forward-char))
ab6d47ea
RS
726 (thumbs-show-name))
727
728(defun thumbs-backward-char ()
729 "Move backward one image."
730 (interactive)
731 (forward-char -1)
0658b86f
RS
732 (while (and (not (bobp)) (not (thumbs-current-image)))
733 (forward-char -1))
ab6d47ea
RS
734 (thumbs-show-name))
735
dd2a3d13
NR
736(defun thumbs-backward-line ()
737 "Move up one line."
738 (interactive)
739 (forward-line -1)
740 (thumbs-show-name))
741
ab6d47ea
RS
742(defun thumbs-forward-line ()
743 "Move down one line."
744 (interactive)
745 (forward-line 1)
746 (thumbs-show-name))
747
dd2a3d13
NR
748(defun thumbs-show-more-images (&optional arg)
749 "Show more than `thumbs-max-image-number' images, if present."
750 (interactive "P")
751 (or arg (setq arg 1))
752 (setq thumbs-extra-images (+ thumbs-extra-images arg))
753 (thumbs-dired-show))
ab6d47ea
RS
754
755(defun thumbs-show-name ()
756 "Show the name of the current file."
757 (interactive)
0658b86f
RS
758 (let ((f (thumbs-current-image)))
759 (and f (message "%s [%s]" f (thumbs-file-size f)))))
ab6d47ea
RS
760
761(defun thumbs-save-current-image ()
762 "Save the current image."
763 (interactive)
764 (let ((f (or thumbs-current-tmp-filename
765 thumbs-current-image-filename))
0658b86f 766 (sa (read-from-minibuffer "Save image file as: "
ab6d47ea
RS
767 thumbs-current-image-filename)))
768 (copy-file f sa)))
769
770(defun thumbs-dired ()
771 "Use `dired' on the current thumbs directory."
772 (interactive)
773 (dired thumbs-current-dir))
774
775;; thumbs-mode
776
777(defvar thumbs-mode-map
778 (let ((map (make-sparse-keymap)))
779 (define-key map [return] 'thumbs-find-image-at-point)
0658b86f 780 (define-key map [mouse-2] 'thumbs-mouse-find-image)
ab6d47ea
RS
781 (define-key map [(meta return)] 'thumbs-find-image-at-point-other-window)
782 (define-key map [(control return)] 'thumbs-set-image-at-point-to-root-window)
783 (define-key map [delete] 'thumbs-delete-images)
784 (define-key map [right] 'thumbs-forward-char)
785 (define-key map [left] 'thumbs-backward-char)
786 (define-key map [up] 'thumbs-backward-line)
787 (define-key map [down] 'thumbs-forward-line)
dd2a3d13 788 (define-key map "+" 'thumbs-show-more-images)
ab6d47ea
RS
789 (define-key map "d" 'thumbs-dired)
790 (define-key map "m" 'thumbs-mark)
0658b86f
RS
791 (define-key map "u" 'thumbs-unmark)
792 (define-key map "R" 'thumbs-rename-images)
793 (define-key map "x" 'thumbs-delete-images)
ab6d47ea
RS
794 (define-key map "s" 'thumbs-show-name)
795 (define-key map "q" 'thumbs-kill-buffer)
796 map)
797 "Keymap for `thumbs-mode'.")
798
0658b86f 799(put 'thumbs-mode 'mode-class 'special)
ab6d47ea
RS
800(define-derived-mode thumbs-mode
801 fundamental-mode "thumbs"
802 "Preview images in a thumbnails buffer"
0658b86f 803 (setq buffer-read-only t)
dd2a3d13 804 (set (make-local-variable 'thumbs-marked-list) nil))
ab6d47ea
RS
805
806(defvar thumbs-view-image-mode-map
807 (let ((map (make-sparse-keymap)))
808 (define-key map [prior] 'thumbs-previous-image)
809 (define-key map [next] 'thumbs-next-image)
dd2a3d13 810 (define-key map "^" 'thumbs-display-thumbs-buffer)
ab6d47ea
RS
811 (define-key map "-" 'thumbs-resize-image-size-down)
812 (define-key map "+" 'thumbs-resize-image-size-up)
813 (define-key map "<" 'thumbs-rotate-left)
814 (define-key map ">" 'thumbs-rotate-right)
815 (define-key map "e" 'thumbs-emboss-image)
816 (define-key map "r" 'thumbs-resize-interactive)
817 (define-key map "s" 'thumbs-save-current-image)
818 (define-key map "q" 'thumbs-kill-buffer)
288f8357 819 (define-key map "w" 'thumbs-set-root)
ab6d47ea
RS
820 map)
821 "Keymap for `thumbs-view-image-mode'.")
822
823;; thumbs-view-image-mode
0658b86f 824(put 'thumbs-view-image-mode 'mode-class 'special)
ab6d47ea 825(define-derived-mode thumbs-view-image-mode
288f8357
JPW
826 fundamental-mode "image-view-mode"
827 (setq buffer-read-only t))
ab6d47ea
RS
828
829;;;###autoload
830(defun thumbs-dired-setroot ()
f75049fc 831 "In dired, call the setroot program on the image at point."
ab6d47ea
RS
832 (interactive)
833 (thumbs-call-setroot-command (dired-get-filename)))
834
835;; Modif to dired mode map
dd2a3d13 836(define-key dired-mode-map "\C-ta" 'thumbs-dired-show)
ab6d47ea
RS
837(define-key dired-mode-map "\C-tm" 'thumbs-dired-show-marked)
838(define-key dired-mode-map "\C-tw" 'thumbs-dired-setroot)
839
840(provide 'thumbs)
841
abceae28 842;; arch-tag: f9ac1ef8-83fc-42c0-8069-1fae43fd2e5c
ab6d47ea 843;;; thumbs.el ends here