Move lisp/emacs-lisp/authors.el to admin/
[bpt/emacs.git] / lisp / thumbs.el
CommitLineData
ab6d47ea 1;;; thumbs.el --- Thumbnails previewer for images files
0658b86f 2
ba318903 3;; Copyright (C) 2004-2014 Free Software Foundation, Inc.
0658b86f 4
ab6d47ea 5;; Author: Jean-Philippe Theberge <jphiltheberge@videotron.ca>
34dc21db 6;; Maintainer: emacs-devel@gnu.org
ab6d47ea
RS
7;; Keywords: Multimedia
8
ab6d47ea
RS
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
ab6d47ea 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
ab6d47ea
RS
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
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
ab6d47ea
RS
23
24;;; Commentary:
25
dd2a3d13
NR
26;; 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 29;; programs. If you want to do more complex tasks like categorize and tag
a6009bd1 30;; your images, use image-dired.el
ab6d47ea
RS
31;;
32;; The 'convert' program from 'ImageMagick'
33;; [URL:http://www.imagemagick.org/] is required.
34;;
dd2a3d13
NR
35;; 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 39;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
dd2a3d13 40;;
ab6d47ea
RS
41;; 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
51;; 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
53
54;;; History:
d1c43637 55;;
ab6d47ea
RS
56
57;;; Code:
58
59(require 'dired)
4582a01c 60(require 'cl-lib) ; for cl-gensym
ab6d47ea 61
ab6d47ea
RS
62;; CUSTOMIZATIONS
63
64(defgroup thumbs nil
65 "Thumbnails previewer."
bf247b6e 66 :version "22.1"
ab6d47ea
RS
67 :group 'multimedia)
68
d6c180c4 69(defcustom thumbs-thumbsdir (locate-user-emacs-file "thumbs")
ea6c930a 70 "Directory to store thumbnails."
ab6d47ea
RS
71 :type 'directory
72 :group 'thumbs)
73
74(defcustom thumbs-geometry "100x100"
ea6c930a 75 "Size of thumbnails."
ab6d47ea
RS
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
babfb70b 95;; Unfortunately Windows XP has a program called CONVERT.EXE in
e1dbe924 96;; C:/WINDOWS/SYSTEM32/ for partitioning NTFS systems. So Emacs
babfb70b
NR
97;; can find the one in your ImageMagick directory, you need to
98;; customize this value to the absolute filename.
ab6d47ea 99(defcustom thumbs-conversion-program
f271d3c7 100 (if (eq system-type 'windows-nt)
ab6d47ea
RS
101 "convert.exe"
102 (or (executable-find "convert")
103 "/usr/X11R6/bin/convert"))
ea6c930a 104 "Name of conversion program for thumbnails generation.
ab6d47ea
RS
105It must be 'convert'."
106 :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 116 "Size of button-like border around thumbnails."
67ed6461 117 :type 'integer
ab6d47ea
RS
118 :group 'thumbs)
119
120(defcustom thumbs-margin 2
ea6c930a 121 "Size of the margin around thumbnails.
ab6d47ea 122This is where you see the cursor."
67ed6461 123 :type 'integer
ab6d47ea
RS
124 :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 129than `thumbs-thumbsdir-max-size'."
ab6d47ea
RS
130 :type 'boolean
131 :group 'thumbs)
132
133(defcustom thumbs-image-resizing-step 10
2ff89cf7 134 "Step by which to resize image as a percentage."
67ed6461 135 :type 'integer
ab6d47ea
RS
136 :group 'thumbs)
137
f271d3c7 138(defcustom thumbs-temp-dir temporary-file-directory
ab6d47ea 139 "Temporary directory to use.
f271d3c7
JB
140Defaults to `temporary-file-directory'. Leaving it to
141this value can let another user see some of your images."
ab6d47ea
RS
142 :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 151(defvar thumbs-current-tmp-filename nil
ab6d47ea 152 "Temporary filename of current image.")
6ced5724
JB
153(make-variable-buffer-local 'thumbs-current-tmp-filename)
154
155(defvar thumbs-current-image-filename nil
ab6d47ea 156 "Filename of current image.")
6ced5724
JB
157(make-variable-buffer-local 'thumbs-current-image-filename)
158
dd2a3d13
NR
159(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 164(defvar thumbs-current-image-size nil
ab6d47ea 165 "Size of current image.")
6ced5724
JB
166
167(defvar thumbs-image-num nil
ab6d47ea 168 "Number of current image.")
6ced5724
JB
169(make-variable-buffer-local 'thumbs-image-num)
170
c897e76f
NR
171(defvar thumbs-buffer nil
172 "Name of buffer containing thumbnails associated with image.")
173(make-variable-buffer-local 'thumbs-buffer)
174
6ced5724 175(defvar thumbs-current-dir nil
ab6d47ea 176 "Current directory.")
6ced5724 177
dd2a3d13 178(defvar thumbs-marked-list nil
ab6d47ea 179 "List of marked files.")
0868cd70
NR
180(make-variable-buffer-local 'thumbs-marked-list)
181(put 'thumbs-marked-list 'permanent-local t)
ab6d47ea 182
6cae5f24
JB
183(defsubst thumbs-temp-dir ()
184 (file-name-as-directory (expand-file-name thumbs-temp-dir)))
185
f271d3c7
JB
186(defun thumbs-temp-file ()
187 "Return a unique temporary filename for an image."
188 (format "%s%s-%s.jpg"
6cae5f24 189 (thumbs-temp-dir)
f271d3c7 190 thumbs-temp-prefix
4582a01c 191 (cl-gensym "T")))
f271d3c7
JB
192
193(defun thumbs-thumbsdir ()
194 "Return the current thumbnails directory (from `thumbs-thumbsdir').
195Create the thumbnails directory if it does not exist."
6cae5f24
JB
196 (let ((thumbs-thumbsdir (file-name-as-directory
197 (expand-file-name thumbs-thumbsdir))))
f271d3c7 198 (unless (file-directory-p thumbs-thumbsdir)
6c060884 199 (make-directory thumbs-thumbsdir t)
f271d3c7
JB
200 (message "Creating thumbnails directory"))
201 thumbs-thumbsdir))
ab6d47ea
RS
202
203(defun thumbs-cleanup-thumbsdir ()
204 "Clean the thumbnails directory.
f75049fc
JB
205If 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 207reached."
dd2a3d13 208 (let* ((files-list
ab6d47ea
RS
209 (sort
210 (mapcar
211 (lambda (f)
dd2a3d13
NR
212 (let ((fattribs-list (file-attributes f)))
213 `(,(nth 4 fattribs-list) ,(nth 7 fattribs-list) ,f)))
f271d3c7 214 (directory-files (thumbs-thumbsdir) t (image-file-name-regexp)))
4f91a816 215 (lambda (l1 l2) (time-less-p (car l1) (car l2)))))
dd2a3d13 216 (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) files-list))))
ab6d47ea
RS
217 (while (> dirsize thumbs-thumbsdir-max-size)
218 (progn
dd2a3d13
NR
219 (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 223
40ba43b4 224;; Check the thumbnail directory size and clean it if necessary.
ab6d47ea
RS
225(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 234Optional arguments are:
ab6d47ea 235ARG any arguments to the ACTION command,
f75049fc 236OUTPUT-FORMAT is the file format to output (default is jpeg),
ab6d47ea 237ACTION-PREFIX is the symbol to place before the ACTION command
f75049fc 238 (defaults to '-' but can sometimes be '+')."
a50d1244
NR
239 (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 245
2ff89cf7
NR
246(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 254 "Resize image in current buffer.
2ff89cf7
NR
255If 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 262 (erase-buffer)
2ff89cf7 263 (thumbs-call-convert (or old thumbs-current-image-filename)
ab6d47ea
RS
264 tmp "sample"
265 (concat (number-to-string (car x)) "x"
266 (number-to-string (cdr x))))
2ff89cf7
NR
267 (save-excursion
268 (thumbs-insert-image tmp 'jpeg 0))
ab6d47ea
RS
269 (setq thumbs-current-tmp-filename tmp)))
270
2ff89cf7 271(defun thumbs-resize-image (width height)
f271d3c7 272 "Resize image interactively to specified WIDTH and HEIGHT."
ab6d47ea 273 (interactive "nWidth: \nnHeight: ")
2ff89cf7 274 (thumbs-resize-image-1 nil (cons width height)))
d1c43637 275
2ff89cf7 276(defun thumbs-shrink-image ()
ab6d47ea
RS
277 "Resize image (smaller)."
278 (interactive)
2ff89cf7 279 (thumbs-resize-image-1 -1))
ab6d47ea 280
2ff89cf7 281(defun thumbs-enlarge-image ()
ab6d47ea
RS
282 "Resize image (bigger)."
283 (interactive)
2ff89cf7 284 (thumbs-resize-image-1 1))
ab6d47ea 285
ab6d47ea
RS
286(defun thumbs-thumbname (img)
287 "Return a thumbnail name for the image IMG."
0df06447
JB
288 (convert-standard-filename
289 (let ((filename (expand-file-name img)))
6cae5f24 290 (format "%s%08x-%s.jpg"
f271d3c7 291 (thumbs-thumbsdir)
0df06447
JB
292 (sxhash filename)
293 (subst-char-in-string
294 ?\s ?\_
295 (apply
296 'concat
297 (split-string filename "/")))))))
ab6d47ea
RS
298
299(defun thumbs-make-thumb (img)
300 "Create the thumbnail for IMG."
0df06447
JB
301 (let ((fn (expand-file-name img))
302 (tn (thumbs-thumbname img)))
ab6d47ea 303 (if (or (not (file-exists-p tn))
0658b86f
RS
304 ;; 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
309 (thumbs-call-convert fn tn "sample" thumbs-geometry))
310 tn))
d1c43637 311
ab6d47ea
RS
312(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 317 ((string-match ".*\\.pbm\\'" img) 'pbm)
ab6d47ea
RS
318 ((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
323(declare-function image-size "image.c" (spec &optional pixels frame))
324
ab6d47ea 325(defun thumbs-file-size (img)
0868cd70
NR
326 (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 329 (number-to-string (round (cdr i))))))
d1c43637 330
ab6d47ea
RS
331;;;###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 341If MARKED is non-nil, the image is marked."
ab6d47ea
RS
342 (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
348 (set (make-local-variable 'thumbs-current-image-size)
349 (image-size i t))))
ab6d47ea
RS
350
351(defun thumbs-insert-thumb (img &optional marked)
352 "Insert the thumbnail for IMG at point.
f75049fc 353If MARKED is non-nil, the image is marked."
ab6d47ea 354 (thumbs-insert-image
0658b86f 355 (thumbs-make-thumb img) 'jpeg thumbs-relief marked)
dd2a3d13
NR
356 (add-text-properties (1- (point)) (point)
357 `(thumb-image-file ,img
0868cd70
NR
358 help-echo ,(file-name-nondirectory img)
359 rear-nonsticky help-echo)))
dd2a3d13
NR
360
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 368 (thumbs-insert-thumb img
dd2a3d13 369 (member img thumbs-marked-list))
ab6d47ea 370 (when (= 0 (mod (setq i (1+ i)) thumbs-per-line))
0658b86f 371 (newline)))
dd2a3d13 372 (unless (bobp) (newline))
3e5b780f 373 (if (> diff 0) (message "Type + to display more images."))))
ab6d47ea 374
dd2a3d13 375(defun thumbs-show-thumbs-list (list &optional dir same-window)
0df06447
JB
376 (unless (and (display-images-p)
377 (image-type-available-p 'jpeg))
378 (error "Required image type is not supported in this Emacs session"))
ab6d47ea 379 (funcall (if same-window 'switch-to-buffer 'pop-to-buffer)
dd2a3d13 380 (if dir (concat "*Thumbs: " dir) "*THUMB-View*"))
ab6d47ea
RS
381 (let ((inhibit-read-only t))
382 (erase-buffer)
383 (thumbs-mode)
2ff89cf7 384 (setq thumbs-buffer (current-buffer))
dd2a3d13
NR
385 (if dir (setq default-directory dir))
386 (thumbs-do-thumbs-insertion list)
ab6d47ea 387 (goto-char (point-min))
6ced5724 388 (set (make-local-variable 'thumbs-current-dir) default-directory)))
ab6d47ea
RS
389
390;;;###autoload
dd2a3d13 391(defun thumbs-show-from-dir (dir &optional reg same-window)
ab6d47ea
RS
392 "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
397 (directory-files dir t (or reg (image-file-name-regexp)))
398 dir same-window))
ab6d47ea
RS
399
400;;;###autoload
401(defun thumbs-dired-show-marked ()
dd2a3d13 402 "In dired, make a thumbs buffer with marked files."
ab6d47ea
RS
403 (interactive)
404 (thumbs-show-thumbs-list (dired-get-marked-files) nil t))
405
406;;;###autoload
dd2a3d13 407(defun thumbs-dired-show ()
ab6d47ea
RS
408 "In dired, make a thumbs buffer with all files in current directory."
409 (interactive)
dd2a3d13 410 (thumbs-show-from-dir default-directory nil t))
ab6d47ea
RS
411
412;;;###autoload
dd2a3d13 413(defalias 'thumbs 'thumbs-show-from-dir)
ab6d47ea 414
0658b86f 415(defun thumbs-find-image (img &optional num otherwin)
c897e76f
NR
416 (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
432
433(defun thumbs-find-image-at-point (&optional img otherwin)
434 "Display image IMG for thumbnail at point.
f75049fc 435Use another window if OTHERWIN is t."
ab6d47ea 436 (interactive)
0658b86f
RS
437 (let* ((i (or img (thumbs-current-image))))
438 (thumbs-find-image i (point) otherwin)))
ab6d47ea
RS
439
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
446(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
452(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 460
ab6d47ea
RS
461(defun thumbs-set-image-at-point-to-root-window ()
462 "Set the image at point as the desktop wallpaper."
463 (interactive)
0658b86f
RS
464 (thumbs-call-setroot-command
465 (thumbs-current-image)))
ab6d47ea
RS
466
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
474(defun thumbs-file-alist ()
475 "Make an alist of elements (POS . FILENAME) for all images in thumb buffer."
c897e76f
NR
476 (with-current-buffer thumbs-buffer
477 (save-excursion
478 (let (list)
479 (goto-char (point-min))
480 (while (not (eobp))
0868cd70 481 (unless (eolp)
1acadcfe
NR
482 (if (thumbs-current-image)
483 (push (cons (point-marker)
484 (thumbs-current-image))
485 list)))
c897e76f
NR
486 (forward-char 1))
487 (nreverse list)))))
0658b86f
RS
488
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 500(defun thumbs-delete-images ()
f75049fc 501 "Delete the image at point (and its thumbnail) (or marked files if any)."
ab6d47ea 502 (interactive)
dd2a3d13 503 (let ((files (or thumbs-marked-list (list (thumbs-current-image)))))
0658b86f 504 (if (yes-or-no-p (format "Really delete %d files? " (length files)))
dd2a3d13 505 (let ((thumbs-file-list (thumbs-file-alist))
0658b86f
RS
506 (inhibit-read-only t))
507 (dolist (x files)
508 (let (failure)
509 (condition-case ()
510 (progn
ab6d47ea 511 (delete-file x)
0658b86f
RS
512 (delete-file (thumbs-thumbname x)))
513 (file-error (setq failure t)))
514 (unless failure
dd2a3d13
NR
515 (when (rassoc x thumbs-file-list)
516 (goto-char (car (rassoc x thumbs-file-list)))
0658b86f 517 (delete-region (point) (1+ (point))))
dd2a3d13
NR
518 (setq thumbs-marked-list
519 (delq x thumbs-marked-list)))))))))
0658b86f
RS
520
521(defun thumbs-rename-images (newfile)
f75049fc 522 "Rename the image at point (and its thumbnail) (or marked files if any)."
0658b86f 523 (interactive "FRename to file or directory: ")
dd2a3d13 524 (let ((files (or thumbs-marked-list (list (thumbs-current-image))))
0658b86f
RS
525 failures)
526 (if (and (not (file-directory-p newfile))
dd2a3d13 527 thumbs-marked-list)
0658b86f
RS
528 (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 532 (let ((thumbs-file-list (thumbs-file-alist))
0658b86f
RS
533 (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
546 (when (rassoc file thumbs-file-list)
547 (goto-char (car (rassoc file thumbs-file-list)))
0658b86f 548 (delete-region (point) (1+ (point))))
dd2a3d13
NR
549 (setq thumbs-marked-list
550 (delq file thumbs-marked-list)))))))
0658b86f
RS
551 (if failures
552 (display-warning 'file-error
553 (format "Rename failures for %s into %s"
554 failures newfile)
555 :error))))
ab6d47ea
RS
556
557(defun thumbs-kill-buffer ()
558 "Kill the current buffer."
559 (interactive)
f271d3c7 560 (quit-window t (selected-window)))
ab6d47ea
RS
561
562(defun thumbs-show-image-num (num)
563 "Show the image with number NUM."
0658b86f 564 (let ((image-buffer (get-buffer-create "*Image*")))
c897e76f 565 (let ((img (cdr (nth (1- num) (thumbs-file-alist)))))
0658b86f 566 (with-current-buffer image-buffer
c897e76f
NR
567 (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 574 (setq thumbs-image-num num
c897e76f 575 thumbs-current-image-filename img))))
ab6d47ea 576
dd2a3d13
NR
577(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 585(defun thumbs-next-image ()
f75049fc 586 "Show the next image."
ab6d47ea
RS
587 (interactive)
588 (let* ((i (1+ thumbs-image-num))
c897e76f
NR
589 (number (length (thumbs-file-alist))))
590 (if (= i number) (setq i 1))
0658b86f 591 (thumbs-show-image-num i)))
ab6d47ea 592
dd2a3d13
NR
593(defun thumbs-display-thumbs-buffer ()
594 "Display the associated thumbs buffer."
ab6d47ea 595 (interactive)
dd2a3d13 596 (display-buffer thumbs-buffer))
ab6d47ea
RS
597
598(defun thumbs-redraw-buffer ()
599 "Redraw the current thumbs buffer."
600 (let ((p (point))
0658b86f
RS
601 (inhibit-read-only t)
602 (files (thumbs-file-list)))
603 (erase-buffer)
604 (thumbs-do-thumbs-insertion files)
605 (goto-char p)))
d1c43637 606
ab6d47ea
RS
607(defun thumbs-mark ()
608 "Mark the image at point."
609 (interactive)
0658b86f
RS
610 (let ((elt (thumbs-current-image)))
611 (unless elt
612 (error "No image here"))
dd2a3d13 613 (push elt thumbs-marked-list)
0658b86f
RS
614 (let ((inhibit-read-only t))
615 (delete-char 1)
3e5b780f 616 (thumbs-insert-thumb elt t)))
0658b86f
RS
617 (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 625 (setq thumbs-marked-list (delete elt thumbs-marked-list))
0658b86f
RS
626 (let ((inhibit-read-only t))
627 (delete-char 1)
3e5b780f 628 (thumbs-insert-thumb elt nil)))
0658b86f 629 (when (eolp) (forward-char)))
d1c43637 630
2ff89cf7
NR
631;; cleaning of old temp files
632(mapc 'delete-file
633 (directory-files (thumbs-temp-dir) t thumbs-temp-prefix))
634
ab6d47ea
RS
635;; Image modification routines
636
637(defun thumbs-modify-image (action &optional arg)
638 "Call convert to do ACTION on image with argument ARG.
932fb767 639ACTION and ARG should be a valid convert command."
ab6d47ea 640 (interactive "sAction: \nsValue: ")
2ff89cf7
NR
641 (let* ((buffer-read-only nil)
642 (old thumbs-current-tmp-filename)
643 (tmp (thumbs-temp-file)))
ab6d47ea 644 (erase-buffer)
2ff89cf7 645 (thumbs-call-convert (or old thumbs-current-image-filename)
ab6d47ea
RS
646 tmp
647 action
648 (or arg ""))
2ff89cf7
NR
649 (save-excursion
650 (thumbs-insert-image tmp 'jpeg 0))
ab6d47ea
RS
651 (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
656 (if (or (< emboss 3) (> emboss 31) (zerop (% emboss 2)))
657 (error "Arg must be an odd number between 3 and 31"))
ab6d47ea
RS
658 (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
680(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
684(defun thumbs-forward-char ()
685 "Move forward one image."
686 (interactive)
687 (forward-char)
0658b86f
RS
688 (while (and (not (eobp)) (not (thumbs-current-image)))
689 (forward-char))
ab6d47ea
RS
690 (thumbs-show-name))
691
692(defun thumbs-backward-char ()
693 "Move backward one image."
694 (interactive)
695 (forward-char -1)
0658b86f
RS
696 (while (and (not (bobp)) (not (thumbs-current-image)))
697 (forward-char -1))
ab6d47ea
RS
698 (thumbs-show-name))
699
dd2a3d13
NR
700(defun thumbs-backward-line ()
701 "Move up one line."
702 (interactive)
703 (forward-line -1)
704 (thumbs-show-name))
705
ab6d47ea
RS
706(defun thumbs-forward-line ()
707 "Move down one line."
708 (interactive)
709 (forward-line 1)
710 (thumbs-show-name))
711
dd2a3d13
NR
712(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
718
719(defun thumbs-show-name ()
720 "Show the name of the current file."
721 (interactive)
0658b86f
RS
722 (let ((f (thumbs-current-image)))
723 (and f (message "%s [%s]" f (thumbs-file-size f)))))
ab6d47ea
RS
724
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 730 (sa (read-from-minibuffer "Save image file as: "
ab6d47ea
RS
731 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 744 (define-key map [mouse-2] 'thumbs-mouse-find-image)
ab6d47ea
RS
745 (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 752 (define-key map "+" 'thumbs-show-more-images)
ab6d47ea
RS
753 (define-key map "d" 'thumbs-dired)
754 (define-key map "m" 'thumbs-mark)
0658b86f
RS
755 (define-key map "u" 'thumbs-unmark)
756 (define-key map "R" 'thumbs-rename-images)
757 (define-key map "x" 'thumbs-delete-images)
ab6d47ea
RS
758 (define-key map "s" 'thumbs-show-name)
759 (define-key map "q" 'thumbs-kill-buffer)
760 map)
761 "Keymap for `thumbs-mode'.")
762
0658b86f 763(put 'thumbs-mode 'mode-class 'special)
ab6d47ea
RS
764(define-derived-mode thumbs-mode
765 fundamental-mode "thumbs"
766 "Preview images in a thumbnails buffer"
0868cd70 767 (setq buffer-read-only t))
ab6d47ea
RS
768
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 773 (define-key map "^" 'thumbs-display-thumbs-buffer)
2ff89cf7
NR
774 (define-key map "-" 'thumbs-shrink-image)
775 (define-key map "+" 'thumbs-enlarge-image)
ab6d47ea
RS
776 (define-key map "<" 'thumbs-rotate-left)
777 (define-key map ">" 'thumbs-rotate-right)
778 (define-key map "e" 'thumbs-emboss-image)
2ff89cf7 779 (define-key map "r" 'thumbs-resize-image)
ab6d47ea
RS
780 (define-key map "s" 'thumbs-save-current-image)
781 (define-key map "q" 'thumbs-kill-buffer)
288f8357 782 (define-key map "w" 'thumbs-set-root)
ab6d47ea
RS
783 map)
784 "Keymap for `thumbs-view-image-mode'.")
785
786;; thumbs-view-image-mode
0658b86f 787(put 'thumbs-view-image-mode 'mode-class 'special)
ab6d47ea 788(define-derived-mode thumbs-view-image-mode
288f8357
JPW
789 fundamental-mode "image-view-mode"
790 (setq buffer-read-only t))
ab6d47ea
RS
791
792;;;###autoload
793(defun thumbs-dired-setroot ()
f75049fc 794 "In dired, call the setroot program on the image at point."
ab6d47ea
RS
795 (interactive)
796 (thumbs-call-setroot-command (dired-get-filename)))
797
798;; Modif to dired mode map
dd2a3d13 799(define-key dired-mode-map "\C-ta" 'thumbs-dired-show)
ab6d47ea
RS
800(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