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