Update FAQ section on large files (Bug#37818)
[emacs.git] / lisp / thumbs.el
blob2b1eb4630d021b4416ced65711d89083736d720d
1 ;;; thumbs.el --- Thumbnails previewer for images files
3 ;; Copyright (C) 2004-2019 Free Software Foundation, Inc.
5 ;; Author: Jean-Philippe Theberge <jphiltheberge@videotron.ca>
6 ;; Maintainer: emacs-devel@gnu.org
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 of the License, or
14 ;; (at your option) 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. If not, see <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
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
29 ;; programs. If you want to do more complex tasks like categorize and tag
30 ;; your images, use image-dired.el
32 ;; The 'convert' program from 'ImageMagick'
33 ;; [URL:http://www.imagemagick.org/] is required.
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.
39 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41 ;; CHANGELOG
43 ;; This is version 2.0
45 ;; USAGE
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 an image will bring you in image view
52 ;; mode for that image. C-h m will give you a list of available keybinding.
54 ;;; History:
57 ;;; Code:
59 (require 'dired)
60 (require 'cl-lib) ; for cl-gensym
62 ;; CUSTOMIZATIONS
64 (defgroup thumbs nil
65 "Thumbnails previewer."
66 :version "22.1"
67 :group 'multimedia)
69 (defcustom thumbs-thumbsdir (locate-user-emacs-file "thumbs")
70 "Directory to store thumbnails."
71 :type 'directory
72 :group 'thumbs)
74 (defcustom thumbs-geometry "100x100"
75 "Size of thumbnails."
76 :type 'string
77 :group 'thumbs)
79 (defcustom thumbs-per-line 4
80 "Number of thumbnails per line to show in directory."
81 :type 'integer
82 :group 'thumbs)
84 (defcustom thumbs-max-image-number 16
85 "Maximum number of images initially displayed in thumbs buffer."
86 :type 'integer
87 :group 'thumbs)
89 (defcustom thumbs-thumbsdir-max-size 50000000
90 "Maximum size for thumbnails directory.
91 When it reaches that size (in bytes), a warning is sent."
92 :type 'integer
93 :group 'thumbs)
95 ;; Unfortunately Windows XP has a program called CONVERT.EXE in
96 ;; C:/WINDOWS/SYSTEM32/ for partitioning NTFS systems. So Emacs
97 ;; can find the one in your ImageMagick directory, you need to
98 ;; customize this value to the absolute filename.
99 (defcustom thumbs-conversion-program
100 (if (eq system-type 'windows-nt)
101 "convert.exe"
102 (or (executable-find "convert")
103 "/usr/X11R6/bin/convert"))
104 "Name of conversion program for thumbnails generation.
105 It must be \"convert\"."
106 :type 'string
107 :group 'thumbs)
109 (defcustom thumbs-setroot-command
110 "xloadimage -onroot -fullscreen *"
111 "Command to set the root window."
112 :type 'string
113 :group 'thumbs)
115 (defcustom thumbs-relief 5
116 "Size of button-like border around thumbnails."
117 :type 'integer
118 :group 'thumbs)
120 (defcustom thumbs-margin 2
121 "Size of the margin around thumbnails.
122 This is where you see the cursor."
123 :type 'integer
124 :group 'thumbs)
126 (defcustom thumbs-thumbsdir-auto-clean t
127 "If set, delete older file in the thumbnails directory.
128 Deletion is done at load time when the directory size is bigger
129 than `thumbs-thumbsdir-max-size'."
130 :type 'boolean
131 :group 'thumbs)
133 (defcustom thumbs-image-resizing-step 10
134 "Step by which to resize image as a percentage."
135 :type 'integer
136 :group 'thumbs)
138 (defcustom thumbs-temp-dir temporary-file-directory
139 "Temporary directory to use.
140 Defaults to `temporary-file-directory'. Leaving it to
141 this value can let another user see some of your images."
142 :type 'directory
143 :group 'thumbs)
145 (defcustom thumbs-temp-prefix "emacsthumbs"
146 "Prefix to add to temp files."
147 :type 'string
148 :group 'thumbs)
150 ;; Initialize some variable, for later use.
151 (defvar thumbs-current-tmp-filename nil
152 "Temporary filename of current image.")
153 (make-variable-buffer-local 'thumbs-current-tmp-filename)
155 (defvar thumbs-current-image-filename nil
156 "Filename of current image.")
157 (make-variable-buffer-local 'thumbs-current-image-filename)
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)
164 (defvar thumbs-current-image-size nil
165 "Size of current image.")
167 (defvar thumbs-image-num nil
168 "Number of current image.")
169 (make-variable-buffer-local 'thumbs-image-num)
171 (defvar thumbs-buffer nil
172 "Name of buffer containing thumbnails associated with image.")
173 (make-variable-buffer-local 'thumbs-buffer)
175 (defvar thumbs-current-dir nil
176 "Current directory.")
178 (defvar thumbs-marked-list nil
179 "List of marked files.")
180 (make-variable-buffer-local 'thumbs-marked-list)
181 (put 'thumbs-marked-list 'permanent-local t)
183 (defsubst thumbs-temp-dir ()
184 (file-name-as-directory (expand-file-name thumbs-temp-dir)))
186 (defun thumbs-temp-file ()
187 "Return a unique temporary filename for an image."
188 (format "%s%s-%s.jpg"
189 (thumbs-temp-dir)
190 thumbs-temp-prefix
191 (cl-gensym "T")))
193 (defun thumbs-thumbsdir ()
194 "Return the current thumbnails directory (from `thumbs-thumbsdir').
195 Create the thumbnails directory if it does not exist."
196 (let ((thumbs-thumbsdir (file-name-as-directory
197 (expand-file-name thumbs-thumbsdir))))
198 (unless (file-directory-p thumbs-thumbsdir)
199 (make-directory thumbs-thumbsdir t)
200 (message "Creating thumbnails directory"))
201 thumbs-thumbsdir))
203 (defun thumbs-cleanup-thumbsdir ()
204 "Clean the thumbnails directory.
205 If 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
207 reached."
208 (let* ((files-list
209 (sort
210 (mapcar
211 (lambda (f)
212 (let ((fattribs-list (file-attributes f)))
213 `(,(file-attribute-access-time fattribs-list)
214 ,(file-attribute-size fattribs-list)
215 ,f)))
216 (directory-files (thumbs-thumbsdir) t (image-file-name-regexp)))
217 (lambda (l1 l2) (time-less-p (car l1) (car l2)))))
218 (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) files-list))))
219 (while (> dirsize thumbs-thumbsdir-max-size)
220 (progn
221 (message "Deleting file %s" (cadr (cdar files-list))))
222 (delete-file (cadr (cdar files-list)))
223 (setq dirsize (- dirsize (car (cdar files-list))))
224 (setq files-list (cdr files-list)))))
226 ;; Check the thumbnail directory size and clean it if necessary.
227 (when thumbs-thumbsdir-auto-clean
228 (thumbs-cleanup-thumbsdir))
230 (defun thumbs-call-convert (filein fileout action
231 &optional arg output-format action-prefix)
232 "Call the convert program.
233 FILEIN is the input file,
234 FILEOUT is the output file,
235 ACTION is the command to send to convert.
236 Optional arguments are:
237 ARG any arguments to the ACTION command,
238 OUTPUT-FORMAT is the file format to output (default is jpeg),
239 ACTION-PREFIX is the symbol to place before the ACTION command
240 (defaults to `-' but can sometimes be `+')."
241 (call-process thumbs-conversion-program nil nil nil
242 (or action-prefix "-")
243 action
244 (or arg "")
245 filein
246 (format "%s:%s" (or output-format "jpeg") fileout)))
248 (defun thumbs-new-image-size (s increment)
249 "New image (a cons of width x height)."
250 (let ((d (* increment thumbs-image-resizing-step)))
251 (cons
252 (round (+ (car s) (/ (* d (car s)) 100)))
253 (round (+ (cdr s) (/ (* d (cdr s)) 100))))))
255 (defun thumbs-resize-image-1 (&optional increment size)
256 "Resize image in current buffer.
257 If SIZE is specified use it. Otherwise make the image larger or
258 smaller according to whether INCREMENT is 1 or -1."
259 (let* ((buffer-read-only nil)
260 (old thumbs-current-tmp-filename)
261 (x (or size
262 (thumbs-new-image-size thumbs-current-image-size increment)))
263 (tmp (thumbs-temp-file)))
264 (erase-buffer)
265 (thumbs-call-convert (or old thumbs-current-image-filename)
266 tmp "sample"
267 (concat (number-to-string (car x)) "x"
268 (number-to-string (cdr x))))
269 (save-excursion
270 (thumbs-insert-image tmp 'jpeg 0))
271 (setq thumbs-current-tmp-filename tmp)))
273 (defun thumbs-resize-image (width height)
274 "Resize image interactively to specified WIDTH and HEIGHT."
275 (interactive "nWidth: \nnHeight: ")
276 (thumbs-resize-image-1 nil (cons width height)))
278 (defun thumbs-shrink-image ()
279 "Resize image (smaller)."
280 (interactive)
281 (thumbs-resize-image-1 -1))
283 (defun thumbs-enlarge-image ()
284 "Resize image (bigger)."
285 (interactive)
286 (thumbs-resize-image-1 1))
288 (defun thumbs-thumbname (img)
289 "Return a thumbnail name for the image IMG."
290 (convert-standard-filename
291 (let ((filename (expand-file-name img)))
292 (format "%s%08x-%s.jpg"
293 (thumbs-thumbsdir)
294 (sxhash filename)
295 (subst-char-in-string
296 ?\s ?\_
297 (apply
298 'concat
299 (split-string filename "/")))))))
301 (defun thumbs-make-thumb (img)
302 "Create the thumbnail for IMG."
303 (let ((fn (expand-file-name img))
304 (tn (thumbs-thumbname img)))
305 (if (or (not (file-exists-p tn))
306 ;; This is not the right fix, but I don't understand
307 ;; the external program or why it produces a geometry
308 ;; unequal to the one requested -- rms.
309 ;;; (not (equal (thumbs-file-size tn) thumbs-geometry))
311 (thumbs-call-convert fn tn "sample" thumbs-geometry))
312 tn))
314 (defun thumbs-image-type (img)
315 "Return image type from filename IMG."
316 (cond ((string-match ".*\\.jpe?g\\'" img) 'jpeg)
317 ((string-match ".*\\.xpm\\'" img) 'xpm)
318 ((string-match ".*\\.xbm\\'" img) 'xbm)
319 ((string-match ".*\\.pbm\\'" img) 'pbm)
320 ((string-match ".*\\.gif\\'" img) 'gif)
321 ((string-match ".*\\.bmp\\'" img) 'bmp)
322 ((string-match ".*\\.png\\'" img) 'png)
323 ((string-match ".*\\.tiff?\\'" img) 'tiff)))
325 (declare-function image-size "image.c" (spec &optional pixels frame))
327 (defun thumbs-file-size (img)
328 (let ((i (image-size
329 (find-image `((:type ,(thumbs-image-type img) :file ,img))) t)))
330 (concat (number-to-string (round (car i))) "x"
331 (number-to-string (round (cdr i))))))
333 ;;;###autoload
334 (defun thumbs-find-thumb (img)
335 "Display the thumbnail for IMG."
336 (interactive "f")
337 (find-file (thumbs-make-thumb img)))
339 (defun thumbs-insert-image (img type relief &optional marked)
340 "Insert image IMG at point.
341 TYPE and RELIEF will be used in constructing the image; see `image'
342 in the emacs-lisp manual for further documentation.
343 If MARKED is non-nil, the image is marked."
344 (let ((i `(image :type ,type
345 :file ,img
346 :relief ,relief
347 :conversion ,(if marked 'disabled)
348 :margin ,thumbs-margin)))
349 (insert-image i)
350 (set (make-local-variable 'thumbs-current-image-size)
351 (image-size i t))))
353 (defun thumbs-insert-thumb (img &optional marked)
354 "Insert the thumbnail for IMG at point.
355 If MARKED is non-nil, the image is marked."
356 (thumbs-insert-image
357 (thumbs-make-thumb img) 'jpeg thumbs-relief marked)
358 (add-text-properties (1- (point)) (point)
359 `(thumb-image-file ,img
360 help-echo ,(file-name-nondirectory img)
361 rear-nonsticky help-echo)))
363 (defun thumbs-do-thumbs-insertion (list)
364 "Insert all thumbnails into thumbs buffer."
365 (let* ((i 0)
366 (length (length list))
367 (diff (- length (* thumbs-max-image-number thumbs-extra-images))))
368 (nbutlast list diff)
369 (dolist (img list)
370 (thumbs-insert-thumb img
371 (member img thumbs-marked-list))
372 (when (= 0 (mod (setq i (1+ i)) thumbs-per-line))
373 (newline)))
374 (unless (bobp) (newline))
375 (if (> diff 0) (message "Type + to display more images."))))
377 (defun thumbs-show-thumbs-list (list &optional dir same-window)
378 (unless (and (display-images-p)
379 (image-type-available-p 'jpeg))
380 (error "Required image type is not supported in this Emacs session"))
381 (funcall (if same-window 'switch-to-buffer 'pop-to-buffer)
382 (if dir (concat "*Thumbs: " dir) "*THUMB-View*"))
383 (let ((inhibit-read-only t))
384 (erase-buffer)
385 (thumbs-mode)
386 (setq thumbs-buffer (current-buffer))
387 (if dir (setq default-directory dir))
388 (thumbs-do-thumbs-insertion list)
389 (goto-char (point-min))
390 (set (make-local-variable 'thumbs-current-dir) default-directory)))
392 ;;;###autoload
393 (defun thumbs-show-from-dir (dir &optional reg same-window)
394 "Make a preview buffer for all images in DIR.
395 Optional argument REG to select file matching a regexp,
396 and SAME-WINDOW to show thumbs in the same window."
397 (interactive "DDir: ")
398 (thumbs-show-thumbs-list
399 (directory-files dir t (or reg (image-file-name-regexp)))
400 dir same-window))
402 ;;;###autoload
403 (defun thumbs-dired-show-marked ()
404 "In dired, make a thumbs buffer with marked files."
405 (interactive)
406 (thumbs-show-thumbs-list (dired-get-marked-files) nil t))
408 ;;;###autoload
409 (defun thumbs-dired-show ()
410 "In dired, make a thumbs buffer with all files in current directory."
411 (interactive)
412 (thumbs-show-from-dir default-directory nil t))
414 ;;;###autoload
415 (defalias 'thumbs 'thumbs-show-from-dir)
417 (defun thumbs-find-image (img &optional num otherwin)
418 (let ((buffer (current-buffer)))
419 (funcall
420 (if otherwin 'switch-to-buffer-other-window 'switch-to-buffer)
421 "*Image*")
422 (thumbs-view-image-mode)
423 (setq mode-name
424 (concat "image-view-mode: " (file-name-nondirectory img)
425 " - " (number-to-string num)))
426 (setq thumbs-buffer buffer)
427 (let ((inhibit-read-only t))
428 (setq thumbs-current-image-filename img
429 thumbs-current-tmp-filename nil
430 thumbs-image-num (or num 0))
431 (delete-region (point-min)(point-max))
432 (save-excursion
433 (thumbs-insert-image img (thumbs-image-type img) 0)))))
435 (defun thumbs-find-image-at-point (&optional img otherwin)
436 "Display image IMG for thumbnail at point.
437 Use another window if OTHERWIN is t."
438 (interactive)
439 (let* ((i (or img (thumbs-current-image))))
440 (thumbs-find-image i (point) otherwin)))
442 (defun thumbs-find-image-at-point-other-window ()
443 "Display image for thumbnail at point in the preview buffer.
444 Open another window."
445 (interactive)
446 (thumbs-find-image-at-point nil t))
448 (defun thumbs-mouse-find-image (event)
449 "Display image for thumbnail at mouse click EVENT."
450 (interactive "e")
451 (mouse-set-point event)
452 (thumbs-find-image-at-point))
454 (defun thumbs-call-setroot-command (img)
455 "Call the setroot program for IMG."
456 (run-hooks 'thumbs-before-setroot-hook)
457 (shell-command (replace-regexp-in-string
458 "\\*"
459 (shell-quote-argument (expand-file-name img))
460 thumbs-setroot-command nil t))
461 (run-hooks 'thumbs-after-setroot-hook))
463 (defun thumbs-set-image-at-point-to-root-window ()
464 "Set the image at point as the desktop wallpaper."
465 (interactive)
466 (thumbs-call-setroot-command
467 (thumbs-current-image)))
469 (defun thumbs-set-root ()
470 "Set the current image as root."
471 (interactive)
472 (thumbs-call-setroot-command
473 (or thumbs-current-tmp-filename
474 thumbs-current-image-filename)))
476 (defun thumbs-file-alist ()
477 "Make an alist of elements (POS . FILENAME) for all images in thumb buffer."
478 (with-current-buffer thumbs-buffer
479 (save-excursion
480 (let (list)
481 (goto-char (point-min))
482 (while (not (eobp))
483 (unless (eolp)
484 (if (thumbs-current-image)
485 (push (cons (point-marker)
486 (thumbs-current-image))
487 list)))
488 (forward-char 1))
489 (nreverse list)))))
491 (defun thumbs-file-list ()
492 "Make a list of file names for all images in thumb buffer."
493 (save-excursion
494 (let (list)
495 (goto-char (point-min))
496 (while (not (eobp))
497 (if (thumbs-current-image)
498 (push (thumbs-current-image) list))
499 (forward-char 1))
500 (nreverse list))))
502 (defun thumbs-delete-images ()
503 "Delete the image at point (and its thumbnail) (or marked files if any)."
504 (interactive)
505 (let ((files (or thumbs-marked-list (list (thumbs-current-image)))))
506 (if (yes-or-no-p (format "Really delete %d files? " (length files)))
507 (let ((thumbs-file-list (thumbs-file-alist))
508 (inhibit-read-only t))
509 (dolist (x files)
510 (let (failure)
511 (condition-case ()
512 (progn
513 (delete-file x)
514 (delete-file (thumbs-thumbname x)))
515 (file-error (setq failure t)))
516 (unless failure
517 (when (rassoc x thumbs-file-list)
518 (goto-char (car (rassoc x thumbs-file-list)))
519 (delete-region (point) (1+ (point))))
520 (setq thumbs-marked-list
521 (delq x thumbs-marked-list)))))))))
523 (defun thumbs-rename-images (newfile)
524 "Rename the image at point (and its thumbnail) (or marked files if any)."
525 (interactive "FRename to file or directory: ")
526 (let ((files (or thumbs-marked-list (list (thumbs-current-image))))
527 failures)
528 (when thumbs-marked-list
529 (make-directory newfile t)
530 (setq newfile (file-name-as-directory newfile)))
531 (if (yes-or-no-p (format "Really rename %d files? " (length files)))
532 (let ((thumbs-file-list (thumbs-file-alist))
533 (inhibit-read-only t))
534 (dolist (file files)
535 (let (failure)
536 (condition-case ()
537 (rename-file file newfile)
538 (file-error (setq failure t)
539 (push file failures)))
540 (unless failure
541 (when (rassoc file thumbs-file-list)
542 (goto-char (car (rassoc file thumbs-file-list)))
543 (delete-region (point) (1+ (point))))
544 (setq thumbs-marked-list
545 (delq file thumbs-marked-list)))))))
546 (if failures
547 (display-warning 'file-error
548 (format "Rename failures for %s into %s"
549 failures newfile)
550 :error))))
552 (defun thumbs-kill-buffer ()
553 "Kill the current buffer."
554 (interactive)
555 (quit-window t (selected-window)))
557 (defun thumbs-show-image-num (num)
558 "Show the image with number NUM."
559 (let ((image-buffer (get-buffer-create "*Image*")))
560 (let ((img (cdr (nth (1- num) (thumbs-file-alist)))))
561 (with-current-buffer image-buffer
562 (setq mode-name
563 (concat "image-view-mode: " (file-name-nondirectory img)
564 " - " (number-to-string num)))
565 (let ((inhibit-read-only t))
566 (erase-buffer)
567 (thumbs-insert-image img (thumbs-image-type img) 0)
568 (goto-char (point-min))))
569 (setq thumbs-image-num num
570 thumbs-current-image-filename img))))
572 (defun thumbs-previous-image ()
573 "Show the previous image."
574 (interactive)
575 (let* ((i (- thumbs-image-num 1))
576 (number (length (thumbs-file-alist))))
577 (if (= i 0) (setq i (1- number)))
578 (thumbs-show-image-num i)))
580 (defun thumbs-next-image ()
581 "Show the next image."
582 (interactive)
583 (let* ((i (1+ thumbs-image-num))
584 (number (length (thumbs-file-alist))))
585 (if (= i number) (setq i 1))
586 (thumbs-show-image-num i)))
588 (defun thumbs-display-thumbs-buffer ()
589 "Display the associated thumbs buffer."
590 (interactive)
591 (display-buffer thumbs-buffer))
593 (defun thumbs-redraw-buffer ()
594 "Redraw the current thumbs buffer."
595 (let ((p (point))
596 (inhibit-read-only t)
597 (files (thumbs-file-list)))
598 (erase-buffer)
599 (thumbs-do-thumbs-insertion files)
600 (goto-char p)))
602 (defun thumbs-mark ()
603 "Mark the image at point."
604 (interactive)
605 (let ((elt (thumbs-current-image)))
606 (unless elt
607 (error "No image here"))
608 (push elt thumbs-marked-list)
609 (let ((inhibit-read-only t))
610 (delete-char 1)
611 (thumbs-insert-thumb elt t)))
612 (when (eolp) (forward-char)))
614 (defun thumbs-unmark ()
615 "Unmark the image at point."
616 (interactive)
617 (let ((elt (thumbs-current-image)))
618 (unless elt
619 (error "No image here"))
620 (setq thumbs-marked-list (delete elt thumbs-marked-list))
621 (let ((inhibit-read-only t))
622 (delete-char 1)
623 (thumbs-insert-thumb elt nil)))
624 (when (eolp) (forward-char)))
626 ;; cleaning of old temp files
627 (mapc 'delete-file
628 (directory-files (thumbs-temp-dir) t thumbs-temp-prefix))
630 ;; Image modification routines
632 (defun thumbs-modify-image (action &optional arg)
633 "Call convert to do ACTION on image with argument ARG.
634 ACTION and ARG should be a valid convert command."
635 (interactive "sAction: \nsValue: ")
636 (let* ((buffer-read-only nil)
637 (old thumbs-current-tmp-filename)
638 (tmp (thumbs-temp-file)))
639 (erase-buffer)
640 (thumbs-call-convert (or old thumbs-current-image-filename)
642 action
643 (or arg ""))
644 (save-excursion
645 (thumbs-insert-image tmp 'jpeg 0))
646 (setq thumbs-current-tmp-filename tmp)))
648 (defun thumbs-emboss-image (emboss)
649 "Emboss the image with value EMBOSS."
650 (interactive "nEmboss value: ")
651 (if (or (< emboss 3) (> emboss 31) (zerop (% emboss 2)))
652 (error "Arg must be an odd number between 3 and 31"))
653 (thumbs-modify-image "emboss" (number-to-string emboss)))
655 (defun thumbs-monochrome-image ()
656 "Turn the image to monochrome."
657 (interactive)
658 (thumbs-modify-image "monochrome"))
660 (defun thumbs-negate-image ()
661 "Negate the image."
662 (interactive)
663 (thumbs-modify-image "negate"))
665 (defun thumbs-rotate-left ()
666 "Rotate the image 90 degrees counter-clockwise."
667 (interactive)
668 (thumbs-modify-image "rotate" "270"))
670 (defun thumbs-rotate-right ()
671 "Rotate the image 90 degrees clockwise."
672 (interactive)
673 (thumbs-modify-image "rotate" "90"))
675 (defun thumbs-current-image ()
676 "Return the name of the image file name at point."
677 (get-text-property (point) 'thumb-image-file))
679 (defun thumbs-forward-char ()
680 "Move forward one image."
681 (interactive)
682 (forward-char)
683 (while (and (not (eobp)) (not (thumbs-current-image)))
684 (forward-char))
685 (thumbs-show-name))
687 (defun thumbs-backward-char ()
688 "Move backward one image."
689 (interactive)
690 (forward-char -1)
691 (while (and (not (bobp)) (not (thumbs-current-image)))
692 (forward-char -1))
693 (thumbs-show-name))
695 (defun thumbs-backward-line ()
696 "Move up one line."
697 (interactive)
698 (forward-line -1)
699 (thumbs-show-name))
701 (defun thumbs-forward-line ()
702 "Move down one line."
703 (interactive)
704 (forward-line 1)
705 (thumbs-show-name))
707 (defun thumbs-show-more-images (&optional arg)
708 "Show more than `thumbs-max-image-number' images, if present."
709 (interactive "P")
710 (or arg (setq arg 1))
711 (setq thumbs-extra-images (+ thumbs-extra-images arg))
712 (thumbs-dired-show))
714 (defun thumbs-show-name ()
715 "Show the name of the current file."
716 (interactive)
717 (let ((f (thumbs-current-image)))
718 (and f (message "%s [%s]" f (thumbs-file-size f)))))
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))
725 (sa (read-from-minibuffer "Save image file as: "
726 thumbs-current-image-filename)))
727 (copy-file f sa)))
729 (defun thumbs-dired ()
730 "Use `dired' on the current thumbs directory."
731 (interactive)
732 (dired thumbs-current-dir))
734 ;; thumbs-mode
736 (defvar thumbs-mode-map
737 (let ((map (make-sparse-keymap)))
738 (define-key map [return] 'thumbs-find-image-at-point)
739 (define-key map [mouse-2] 'thumbs-mouse-find-image)
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 "+" 'thumbs-show-more-images)
748 (define-key map "d" 'thumbs-dired)
749 (define-key map "m" 'thumbs-mark)
750 (define-key map "u" 'thumbs-unmark)
751 (define-key map "R" 'thumbs-rename-images)
752 (define-key map "x" 'thumbs-delete-images)
753 (define-key map "s" 'thumbs-show-name)
754 (define-key map "q" 'thumbs-kill-buffer)
755 map)
756 "Keymap for `thumbs-mode'.")
758 (put 'thumbs-mode 'mode-class 'special)
759 (define-derived-mode thumbs-mode
760 fundamental-mode "thumbs"
761 "Preview images in a thumbnails buffer."
762 (setq buffer-read-only t))
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-display-thumbs-buffer)
769 (define-key map "-" 'thumbs-shrink-image)
770 (define-key map "+" 'thumbs-enlarge-image)
771 (define-key map "<" 'thumbs-rotate-left)
772 (define-key map ">" 'thumbs-rotate-right)
773 (define-key map "e" 'thumbs-emboss-image)
774 (define-key map "r" 'thumbs-resize-image)
775 (define-key map "s" 'thumbs-save-current-image)
776 (define-key map "q" 'thumbs-kill-buffer)
777 (define-key map "w" 'thumbs-set-root)
778 map)
779 "Keymap for `thumbs-view-image-mode'.")
781 ;; thumbs-view-image-mode
782 (put 'thumbs-view-image-mode 'mode-class 'special)
783 (define-derived-mode thumbs-view-image-mode
784 fundamental-mode "image-view-mode"
785 (setq buffer-read-only t))
787 ;;;###autoload
788 (defun thumbs-dired-setroot ()
789 "In dired, call the setroot program on the image at point."
790 (interactive)
791 (thumbs-call-setroot-command (dired-get-filename)))
793 ;; Modif to dired mode map
794 (define-key dired-mode-map "\C-ta" 'thumbs-dired-show)
795 (define-key dired-mode-map "\C-tm" 'thumbs-dired-show-marked)
796 (define-key dired-mode-map "\C-tw" 'thumbs-dired-setroot)
798 (provide 'thumbs)
800 ;;; thumbs.el ends here