(thumbs-call-convert): Use call-process directly
[emacs.git] / lisp / thumbs.el
blob395145fd53b4992fab4f4dceb96588a965dd680c
1 ;;; thumbs.el --- Thumbnails previewer for images files
3 ;; Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 ;; Author: Jean-Philippe Theberge <jphiltheberge@videotron.ca>
6 ;; Maintainer: FSF
7 ;; Keywords: Multimedia
9 ;; This file is part of GNU Emacs.
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 3, or (at your option)
14 ;; any later version.
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.
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
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
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 image-dired.el
34 ;; The 'convert' program from 'ImageMagick'
35 ;; [URL:http://www.imagemagick.org/] is required.
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.
41 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
43 ;; CHANGELOG
45 ;; This is version 2.0
47 ;; USAGE
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
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.
56 ;;; History:
59 ;;; Code:
61 (require 'dired)
63 ;; CUSTOMIZATIONS
65 (defgroup thumbs nil
66 "Thumbnails previewer."
67 :version "22.1"
68 :group 'multimedia)
70 (defcustom thumbs-thumbsdir (concat user-emacs-directory "thumbs")
71 "*Directory to store thumbnails."
72 :type 'directory
73 :group 'thumbs)
75 (defcustom thumbs-geometry "100x100"
76 "*Size of thumbnails."
77 :type 'string
78 :group 'thumbs)
80 (defcustom thumbs-per-line 4
81 "Number of thumbnails per line to show in directory."
82 :type 'integer
83 :group 'thumbs)
85 (defcustom thumbs-max-image-number 16
86 "Maximum number of images initially displayed in thumbs buffer."
87 :type 'integer
88 :group 'thumbs)
90 (defcustom thumbs-thumbsdir-max-size 50000000
91 "Maximum size for thumbnails directory.
92 When it reaches that size (in bytes), a warning is sent."
93 :type 'integer
94 :group 'thumbs)
96 (defcustom thumbs-conversion-program
97 (if (eq system-type 'windows-nt)
98 "convert.exe"
99 (or (executable-find "convert")
100 "/usr/X11R6/bin/convert"))
101 "*Name of conversion program for thumbnails generation.
102 It must be 'convert'."
103 :type 'string
104 :group 'thumbs)
106 (defcustom thumbs-setroot-command
107 "xloadimage -onroot -fullscreen *"
108 "Command to set the root window."
109 :type 'string
110 :group 'thumbs)
112 (defcustom thumbs-relief 5
113 "*Size of button-like border around thumbnails."
114 :type 'integer
115 :group 'thumbs)
117 (defcustom thumbs-margin 2
118 "*Size of the margin around thumbnails.
119 This is where you see the cursor."
120 :type 'integer
121 :group 'thumbs)
123 (defcustom thumbs-thumbsdir-auto-clean t
124 "If set, delete older file in the thumbnails directory.
125 Deletion is done at load time when the directory size is bigger
126 than `thumbs-thumbsdir-max-size'."
127 :type 'boolean
128 :group 'thumbs)
130 (defcustom thumbs-image-resizing-step 10
131 "Step by which to resize image as a percentage."
132 :type 'integer
133 :group 'thumbs)
135 (defcustom thumbs-temp-dir temporary-file-directory
136 "Temporary directory to use.
137 Defaults to `temporary-file-directory'. Leaving it to
138 this value can let another user see some of your images."
139 :type 'directory
140 :group 'thumbs)
142 (defcustom thumbs-temp-prefix "emacsthumbs"
143 "Prefix to add to temp files."
144 :type 'string
145 :group 'thumbs)
147 ;; Initialize some variable, for later use.
148 (defvar thumbs-current-tmp-filename nil
149 "Temporary filename of current image.")
150 (make-variable-buffer-local 'thumbs-current-tmp-filename)
152 (defvar thumbs-current-image-filename nil
153 "Filename of current image.")
154 (make-variable-buffer-local 'thumbs-current-image-filename)
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)
161 (defvar thumbs-current-image-size nil
162 "Size of current image.")
164 (defvar thumbs-image-num nil
165 "Number of current image.")
166 (make-variable-buffer-local 'thumbs-image-num)
168 (defvar thumbs-buffer nil
169 "Name of buffer containing thumbnails associated with image.")
170 (make-variable-buffer-local 'thumbs-buffer)
172 (defvar thumbs-current-dir nil
173 "Current directory.")
175 (defvar thumbs-marked-list nil
176 "List of marked files.")
177 (make-variable-buffer-local 'thumbs-marked-list)
178 (put 'thumbs-marked-list 'permanent-local t)
180 (defalias 'thumbs-gensym
181 (if (fboundp 'gensym)
182 'gensym
183 ;; Copied from cl-macs.el
184 (defvar thumbs-gensym-counter 0)
185 (lambda (&optional prefix)
186 "Generate a new uninterned symbol.
187 The name is made by appending a number to PREFIX, default \"G\"."
188 (let ((pfix (if (stringp prefix) prefix "G"))
189 (num (if (integerp prefix) prefix
190 (prog1 thumbs-gensym-counter
191 (setq thumbs-gensym-counter
192 (1+ thumbs-gensym-counter))))))
193 (make-symbol (format "%s%d" pfix num))))))
195 (defsubst thumbs-temp-dir ()
196 (file-name-as-directory (expand-file-name thumbs-temp-dir)))
198 (defun thumbs-temp-file ()
199 "Return a unique temporary filename for an image."
200 (format "%s%s-%s.jpg"
201 (thumbs-temp-dir)
202 thumbs-temp-prefix
203 (thumbs-gensym "T")))
205 (defun thumbs-thumbsdir ()
206 "Return the current thumbnails directory (from `thumbs-thumbsdir').
207 Create the thumbnails directory if it does not exist."
208 (let ((thumbs-thumbsdir (file-name-as-directory
209 (expand-file-name thumbs-thumbsdir))))
210 (unless (file-directory-p thumbs-thumbsdir)
211 (make-directory thumbs-thumbsdir t)
212 (message "Creating thumbnails directory"))
213 thumbs-thumbsdir))
215 (defun thumbs-cleanup-thumbsdir ()
216 "Clean the thumbnails directory.
217 If the total size of all files in `thumbs-thumbsdir' is bigger than
218 `thumbs-thumbsdir-max-size', files are deleted until the max size is
219 reached."
220 (let* ((files-list
221 (sort
222 (mapcar
223 (lambda (f)
224 (let ((fattribs-list (file-attributes f)))
225 `(,(nth 4 fattribs-list) ,(nth 7 fattribs-list) ,f)))
226 (directory-files (thumbs-thumbsdir) t (image-file-name-regexp)))
227 '(lambda (l1 l2) (time-less-p (car l1) (car l2)))))
228 (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) files-list))))
229 (while (> dirsize thumbs-thumbsdir-max-size)
230 (progn
231 (message "Deleting file %s" (cadr (cdar files-list))))
232 (delete-file (cadr (cdar files-list)))
233 (setq dirsize (- dirsize (car (cdar files-list))))
234 (setq files-list (cdr files-list)))))
236 ;; Check the thumbsnail directory size and clean it if necessary.
237 (when thumbs-thumbsdir-auto-clean
238 (thumbs-cleanup-thumbsdir))
240 (defun thumbs-call-convert (filein fileout action
241 &optional arg output-format action-prefix)
242 "Call the convert program.
243 FILEIN is the input file,
244 FILEOUT is the output file,
245 ACTION is the command to send to convert.
246 Optional arguments are:
247 ARG any arguments to the ACTION command,
248 OUTPUT-FORMAT is the file format to output (default is jpeg),
249 ACTION-PREFIX is the symbol to place before the ACTION command
250 (defaults to '-' but can sometimes be '+')."
251 (call-process thumbs-conversion-program nil nil nil
252 (or action-prefix "-")
253 action
254 (or arg "")
255 filein
256 (format "%s:%s" (or output-format "jpeg") fileout)))
258 (defun thumbs-new-image-size (s increment)
259 "New image (a cons of width x height)."
260 (let ((d (* increment thumbs-image-resizing-step)))
261 (cons
262 (round (+ (car s) (/ (* d (car s)) 100)))
263 (round (+ (cdr s) (/ (* d (cdr s)) 100))))))
265 (defun thumbs-resize-image-1 (&optional increment size)
266 "Resize image in current buffer.
267 If SIZE is specified use it. Otherwise make the image larger or
268 smaller according to whether INCREMENT is 1 or -1."
269 (let* ((buffer-read-only nil)
270 (old thumbs-current-tmp-filename)
271 (x (or size
272 (thumbs-new-image-size thumbs-current-image-size increment)))
273 (tmp (thumbs-temp-file)))
274 (erase-buffer)
275 (thumbs-call-convert (or old thumbs-current-image-filename)
276 tmp "sample"
277 (concat (number-to-string (car x)) "x"
278 (number-to-string (cdr x))))
279 (save-excursion
280 (thumbs-insert-image tmp 'jpeg 0))
281 (setq thumbs-current-tmp-filename tmp)))
283 (defun thumbs-resize-image (width height)
284 "Resize image interactively to specified WIDTH and HEIGHT."
285 (interactive "nWidth: \nnHeight: ")
286 (thumbs-resize-image-1 nil (cons width height)))
288 (defun thumbs-shrink-image ()
289 "Resize image (smaller)."
290 (interactive)
291 (thumbs-resize-image-1 -1))
293 (defun thumbs-enlarge-image ()
294 "Resize image (bigger)."
295 (interactive)
296 (thumbs-resize-image-1 1))
298 (defun thumbs-thumbname (img)
299 "Return a thumbnail name for the image IMG."
300 (convert-standard-filename
301 (let ((filename (expand-file-name img)))
302 (format "%s%08x-%s.jpg"
303 (thumbs-thumbsdir)
304 (sxhash filename)
305 (subst-char-in-string
306 ?\s ?\_
307 (apply
308 'concat
309 (split-string filename "/")))))))
311 (defun thumbs-make-thumb (img)
312 "Create the thumbnail for IMG."
313 (let ((fn (expand-file-name img))
314 (tn (thumbs-thumbname img)))
315 (if (or (not (file-exists-p tn))
316 ;; This is not the right fix, but I don't understand
317 ;; the external program or why it produces a geometry
318 ;; unequal to the one requested -- rms.
319 ;;; (not (equal (thumbs-file-size tn) thumbs-geometry))
321 (thumbs-call-convert fn tn "sample" thumbs-geometry))
322 tn))
324 (defun thumbs-image-type (img)
325 "Return image type from filename IMG."
326 (cond ((string-match ".*\\.jpe?g\\'" img) 'jpeg)
327 ((string-match ".*\\.xpm\\'" img) 'xpm)
328 ((string-match ".*\\.xbm\\'" img) 'xbm)
329 ((string-match ".*\\.pbm\\'" img) 'pbm)
330 ((string-match ".*\\.gif\\'" img) 'gif)
331 ((string-match ".*\\.bmp\\'" img) 'bmp)
332 ((string-match ".*\\.png\\'" img) 'png)
333 ((string-match ".*\\.tiff?\\'" img) 'tiff)))
335 (defun thumbs-file-size (img)
336 (let ((i (image-size
337 (find-image `((:type ,(thumbs-image-type img) :file ,img))) t)))
338 (concat (number-to-string (round (car i))) "x"
339 (number-to-string (round (cdr i))))))
341 ;;;###autoload
342 (defun thumbs-find-thumb (img)
343 "Display the thumbnail for IMG."
344 (interactive "f")
345 (find-file (thumbs-make-thumb img)))
347 (defun thumbs-insert-image (img type relief &optional marked)
348 "Insert image IMG at point.
349 TYPE and RELIEF will be used in constructing the image; see `image'
350 in the emacs-lisp manual for further documentation.
351 If MARKED is non-nil, the image is marked."
352 (let ((i `(image :type ,type
353 :file ,img
354 :relief ,relief
355 :conversion ,(if marked 'disabled)
356 :margin ,thumbs-margin)))
357 (insert-image i)
358 (set (make-local-variable 'thumbs-current-image-size)
359 (image-size i t))))
361 (defun thumbs-insert-thumb (img &optional marked)
362 "Insert the thumbnail for IMG at point.
363 If MARKED is non-nil, the image is marked."
364 (thumbs-insert-image
365 (thumbs-make-thumb img) 'jpeg thumbs-relief marked)
366 (add-text-properties (1- (point)) (point)
367 `(thumb-image-file ,img
368 help-echo ,(file-name-nondirectory img)
369 rear-nonsticky help-echo)))
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)
378 (thumbs-insert-thumb img
379 (member img thumbs-marked-list))
380 (when (= 0 (mod (setq i (1+ i)) thumbs-per-line))
381 (newline)))
382 (unless (bobp) (newline))
383 (if (> diff 0) (message "Type + to display more images."))))
385 (defun thumbs-show-thumbs-list (list &optional dir same-window)
386 (unless (and (display-images-p)
387 (image-type-available-p 'jpeg))
388 (error "Required image type is not supported in this Emacs session"))
389 (funcall (if same-window 'switch-to-buffer 'pop-to-buffer)
390 (if dir (concat "*Thumbs: " dir) "*THUMB-View*"))
391 (let ((inhibit-read-only t))
392 (erase-buffer)
393 (thumbs-mode)
394 (setq thumbs-buffer (current-buffer))
395 (if dir (setq default-directory dir))
396 (thumbs-do-thumbs-insertion list)
397 (goto-char (point-min))
398 (set (make-local-variable 'thumbs-current-dir) default-directory)))
400 ;;;###autoload
401 (defun thumbs-show-from-dir (dir &optional reg same-window)
402 "Make a preview buffer for all images in DIR.
403 Optional argument REG to select file matching a regexp,
404 and SAME-WINDOW to show thumbs in the same window."
405 (interactive "DDir: ")
406 (thumbs-show-thumbs-list
407 (directory-files dir t (or reg (image-file-name-regexp)))
408 dir same-window))
410 ;;;###autoload
411 (defun thumbs-dired-show-marked ()
412 "In dired, make a thumbs buffer with marked files."
413 (interactive)
414 (thumbs-show-thumbs-list (dired-get-marked-files) nil t))
416 ;;;###autoload
417 (defun thumbs-dired-show ()
418 "In dired, make a thumbs buffer with all files in current directory."
419 (interactive)
420 (thumbs-show-from-dir default-directory nil t))
422 ;;;###autoload
423 (defalias 'thumbs 'thumbs-show-from-dir)
425 (defun thumbs-find-image (img &optional num otherwin)
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)))))
443 (defun thumbs-find-image-at-point (&optional img otherwin)
444 "Display image IMG for thumbnail at point.
445 Use another window if OTHERWIN is t."
446 (interactive)
447 (let* ((i (or img (thumbs-current-image))))
448 (thumbs-find-image i (point) otherwin)))
450 (defun thumbs-find-image-at-point-other-window ()
451 "Display image for thumbnail at point in the preview buffer.
452 Open another window."
453 (interactive)
454 (thumbs-find-image-at-point nil t))
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))
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))
471 (defun thumbs-set-image-at-point-to-root-window ()
472 "Set the image at point as the desktop wallpaper."
473 (interactive)
474 (thumbs-call-setroot-command
475 (thumbs-current-image)))
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)))
484 (defun thumbs-file-alist ()
485 "Make an alist of elements (POS . FILENAME) for all images in thumb buffer."
486 (with-current-buffer thumbs-buffer
487 (save-excursion
488 (let (list)
489 (goto-char (point-min))
490 (while (not (eobp))
491 (unless (eolp)
492 (if (thumbs-current-image)
493 (push (cons (point-marker)
494 (thumbs-current-image))
495 list)))
496 (forward-char 1))
497 (nreverse list)))))
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))))
510 (defun thumbs-delete-images ()
511 "Delete the image at point (and its thumbnail) (or marked files if any)."
512 (interactive)
513 (let ((files (or thumbs-marked-list (list (thumbs-current-image)))))
514 (if (yes-or-no-p (format "Really delete %d files? " (length files)))
515 (let ((thumbs-file-list (thumbs-file-alist))
516 (inhibit-read-only t))
517 (dolist (x files)
518 (let (failure)
519 (condition-case ()
520 (progn
521 (delete-file x)
522 (delete-file (thumbs-thumbname x)))
523 (file-error (setq failure t)))
524 (unless failure
525 (when (rassoc x thumbs-file-list)
526 (goto-char (car (rassoc x thumbs-file-list)))
527 (delete-region (point) (1+ (point))))
528 (setq thumbs-marked-list
529 (delq x thumbs-marked-list)))))))))
531 (defun thumbs-rename-images (newfile)
532 "Rename the image at point (and its thumbnail) (or marked files if any)."
533 (interactive "FRename to file or directory: ")
534 (let ((files (or thumbs-marked-list (list (thumbs-current-image))))
535 failures)
536 (if (and (not (file-directory-p newfile))
537 thumbs-marked-list)
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)))
542 (let ((thumbs-file-list (thumbs-file-alist))
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
556 (when (rassoc file thumbs-file-list)
557 (goto-char (car (rassoc file thumbs-file-list)))
558 (delete-region (point) (1+ (point))))
559 (setq thumbs-marked-list
560 (delq file thumbs-marked-list)))))))
561 (if failures
562 (display-warning 'file-error
563 (format "Rename failures for %s into %s"
564 failures newfile)
565 :error))))
567 (defun thumbs-kill-buffer ()
568 "Kill the current buffer."
569 (interactive)
570 (quit-window t (selected-window)))
572 (defun thumbs-show-image-num (num)
573 "Show the image with number NUM."
574 (let ((image-buffer (get-buffer-create "*Image*")))
575 (let ((img (cdr (nth (1- num) (thumbs-file-alist)))))
576 (with-current-buffer image-buffer
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))))
584 (setq thumbs-image-num num
585 thumbs-current-image-filename img))))
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)))
595 (defun thumbs-next-image ()
596 "Show the next image."
597 (interactive)
598 (let* ((i (1+ thumbs-image-num))
599 (number (length (thumbs-file-alist))))
600 (if (= i number) (setq i 1))
601 (thumbs-show-image-num i)))
603 (defun thumbs-display-thumbs-buffer ()
604 "Display the associated thumbs buffer."
605 (interactive)
606 (display-buffer thumbs-buffer))
608 (defun thumbs-redraw-buffer ()
609 "Redraw the current thumbs buffer."
610 (let ((p (point))
611 (inhibit-read-only t)
612 (files (thumbs-file-list)))
613 (erase-buffer)
614 (thumbs-do-thumbs-insertion files)
615 (goto-char p)))
617 (defun thumbs-mark ()
618 "Mark the image at point."
619 (interactive)
620 (let ((elt (thumbs-current-image)))
621 (unless elt
622 (error "No image here"))
623 (push elt thumbs-marked-list)
624 (let ((inhibit-read-only t))
625 (delete-char 1)
626 (thumbs-insert-thumb elt t)))
627 (when (eolp) (forward-char)))
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"))
635 (setq thumbs-marked-list (delete elt thumbs-marked-list))
636 (let ((inhibit-read-only t))
637 (delete-char 1)
638 (thumbs-insert-thumb elt nil)))
639 (when (eolp) (forward-char)))
641 ;; cleaning of old temp files
642 (mapc 'delete-file
643 (directory-files (thumbs-temp-dir) t thumbs-temp-prefix))
645 ;; Image modification routines
647 (defun thumbs-modify-image (action &optional arg)
648 "Call convert to do ACTION on image with argument ARG.
649 ACTION and ARG should be a valid convert command."
650 (interactive "sAction: \nsValue: ")
651 (let* ((buffer-read-only nil)
652 (old thumbs-current-tmp-filename)
653 (tmp (thumbs-temp-file)))
654 (erase-buffer)
655 (thumbs-call-convert (or old thumbs-current-image-filename)
657 action
658 (or arg ""))
659 (save-excursion
660 (thumbs-insert-image tmp 'jpeg 0))
661 (setq thumbs-current-tmp-filename tmp)))
663 (defun thumbs-emboss-image (emboss)
664 "Emboss the image with value EMBOSS."
665 (interactive "nEmboss value: ")
666 (if (or (< emboss 3) (> emboss 31) (zerop (% emboss 2)))
667 (error "Arg must be an odd number between 3 and 31"))
668 (thumbs-modify-image "emboss" (number-to-string emboss)))
670 (defun thumbs-monochrome-image ()
671 "Turn the image to monochrome."
672 (interactive)
673 (thumbs-modify-image "monochrome"))
675 (defun thumbs-negate-image ()
676 "Negate the image."
677 (interactive)
678 (thumbs-modify-image "negate"))
680 (defun thumbs-rotate-left ()
681 "Rotate the image 90 degrees counter-clockwise."
682 (interactive)
683 (thumbs-modify-image "rotate" "270"))
685 (defun thumbs-rotate-right ()
686 "Rotate the image 90 degrees clockwise."
687 (interactive)
688 (thumbs-modify-image "rotate" "90"))
690 (defun thumbs-current-image ()
691 "Return the name of the image file name at point."
692 (get-text-property (point) 'thumb-image-file))
694 (defun thumbs-forward-char ()
695 "Move forward one image."
696 (interactive)
697 (forward-char)
698 (while (and (not (eobp)) (not (thumbs-current-image)))
699 (forward-char))
700 (thumbs-show-name))
702 (defun thumbs-backward-char ()
703 "Move backward one image."
704 (interactive)
705 (forward-char -1)
706 (while (and (not (bobp)) (not (thumbs-current-image)))
707 (forward-char -1))
708 (thumbs-show-name))
710 (defun thumbs-backward-line ()
711 "Move up one line."
712 (interactive)
713 (forward-line -1)
714 (thumbs-show-name))
716 (defun thumbs-forward-line ()
717 "Move down one line."
718 (interactive)
719 (forward-line 1)
720 (thumbs-show-name))
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))
729 (defun thumbs-show-name ()
730 "Show the name of the current file."
731 (interactive)
732 (let ((f (thumbs-current-image)))
733 (and f (message "%s [%s]" f (thumbs-file-size f)))))
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))
740 (sa (read-from-minibuffer "Save image file as: "
741 thumbs-current-image-filename)))
742 (copy-file f sa)))
744 (defun thumbs-dired ()
745 "Use `dired' on the current thumbs directory."
746 (interactive)
747 (dired thumbs-current-dir))
749 ;; thumbs-mode
751 (defvar thumbs-mode-map
752 (let ((map (make-sparse-keymap)))
753 (define-key map [return] 'thumbs-find-image-at-point)
754 (define-key map [mouse-2] 'thumbs-mouse-find-image)
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)
762 (define-key map "+" 'thumbs-show-more-images)
763 (define-key map "d" 'thumbs-dired)
764 (define-key map "m" 'thumbs-mark)
765 (define-key map "u" 'thumbs-unmark)
766 (define-key map "R" 'thumbs-rename-images)
767 (define-key map "x" 'thumbs-delete-images)
768 (define-key map "s" 'thumbs-show-name)
769 (define-key map "q" 'thumbs-kill-buffer)
770 map)
771 "Keymap for `thumbs-mode'.")
773 (put 'thumbs-mode 'mode-class 'special)
774 (define-derived-mode thumbs-mode
775 fundamental-mode "thumbs"
776 "Preview images in a thumbnails buffer"
777 (setq buffer-read-only t))
779 (defvar thumbs-view-image-mode-map
780 (let ((map (make-sparse-keymap)))
781 (define-key map [prior] 'thumbs-previous-image)
782 (define-key map [next] 'thumbs-next-image)
783 (define-key map "^" 'thumbs-display-thumbs-buffer)
784 (define-key map "-" 'thumbs-shrink-image)
785 (define-key map "+" 'thumbs-enlarge-image)
786 (define-key map "<" 'thumbs-rotate-left)
787 (define-key map ">" 'thumbs-rotate-right)
788 (define-key map "e" 'thumbs-emboss-image)
789 (define-key map "r" 'thumbs-resize-image)
790 (define-key map "s" 'thumbs-save-current-image)
791 (define-key map "q" 'thumbs-kill-buffer)
792 (define-key map "w" 'thumbs-set-root)
793 map)
794 "Keymap for `thumbs-view-image-mode'.")
796 ;; thumbs-view-image-mode
797 (put 'thumbs-view-image-mode 'mode-class 'special)
798 (define-derived-mode thumbs-view-image-mode
799 fundamental-mode "image-view-mode"
800 (setq buffer-read-only t))
802 ;;;###autoload
803 (defun thumbs-dired-setroot ()
804 "In dired, call the setroot program on the image at point."
805 (interactive)
806 (thumbs-call-setroot-command (dired-get-filename)))
808 ;; Modif to dired mode map
809 (define-key dired-mode-map "\C-ta" 'thumbs-dired-show)
810 (define-key dired-mode-map "\C-tm" 'thumbs-dired-show-marked)
811 (define-key dired-mode-map "\C-tw" 'thumbs-dired-setroot)
813 (provide 'thumbs)
815 ;; arch-tag: f9ac1ef8-83fc-42c0-8069-1fae43fd2e5c
816 ;;; thumbs.el ends here