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