Allow 'browse-url-emacs' to fetch URL in the selected window
[emacs.git] / lisp / thumbs.el
blob26c9935429f08f4182e210db8a6e66789e22239d
1 ;;; thumbs.el --- Thumbnails previewer for images files
3 ;; Copyright (C) 2004-2018 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 `(,(nth 4 fattribs-list) ,(nth 7 fattribs-list) ,f)))
214 (directory-files (thumbs-thumbsdir) t (image-file-name-regexp)))
215 (lambda (l1 l2) (time-less-p (car l1) (car l2)))))
216 (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) files-list))))
217 (while (> dirsize thumbs-thumbsdir-max-size)
218 (progn
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)))))
224 ;; Check the thumbnail directory size and clean it if necessary.
225 (when thumbs-thumbsdir-auto-clean
226 (thumbs-cleanup-thumbsdir))
228 (defun thumbs-call-convert (filein fileout action
229 &optional arg output-format action-prefix)
230 "Call the convert program.
231 FILEIN is the input file,
232 FILEOUT is the output file,
233 ACTION is the command to send to convert.
234 Optional arguments are:
235 ARG any arguments to the ACTION command,
236 OUTPUT-FORMAT is the file format to output (default is jpeg),
237 ACTION-PREFIX is the symbol to place before the ACTION command
238 (defaults to `-' but can sometimes be `+')."
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)))
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))))))
253 (defun thumbs-resize-image-1 (&optional increment size)
254 "Resize image in current buffer.
255 If SIZE is specified use it. Otherwise make the image larger or
256 smaller 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)))
262 (erase-buffer)
263 (thumbs-call-convert (or old thumbs-current-image-filename)
264 tmp "sample"
265 (concat (number-to-string (car x)) "x"
266 (number-to-string (cdr x))))
267 (save-excursion
268 (thumbs-insert-image tmp 'jpeg 0))
269 (setq thumbs-current-tmp-filename tmp)))
271 (defun thumbs-resize-image (width height)
272 "Resize image interactively to specified WIDTH and HEIGHT."
273 (interactive "nWidth: \nnHeight: ")
274 (thumbs-resize-image-1 nil (cons width height)))
276 (defun thumbs-shrink-image ()
277 "Resize image (smaller)."
278 (interactive)
279 (thumbs-resize-image-1 -1))
281 (defun thumbs-enlarge-image ()
282 "Resize image (bigger)."
283 (interactive)
284 (thumbs-resize-image-1 1))
286 (defun thumbs-thumbname (img)
287 "Return a thumbnail name for the image IMG."
288 (convert-standard-filename
289 (let ((filename (expand-file-name img)))
290 (format "%s%08x-%s.jpg"
291 (thumbs-thumbsdir)
292 (sxhash filename)
293 (subst-char-in-string
294 ?\s ?\_
295 (apply
296 'concat
297 (split-string filename "/")))))))
299 (defun thumbs-make-thumb (img)
300 "Create the thumbnail for IMG."
301 (let ((fn (expand-file-name img))
302 (tn (thumbs-thumbname img)))
303 (if (or (not (file-exists-p tn))
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))
309 (thumbs-call-convert fn tn "sample" thumbs-geometry))
310 tn))
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)
317 ((string-match ".*\\.pbm\\'" img) 'pbm)
318 ((string-match ".*\\.gif\\'" img) 'gif)
319 ((string-match ".*\\.bmp\\'" img) 'bmp)
320 ((string-match ".*\\.png\\'" img) 'png)
321 ((string-match ".*\\.tiff?\\'" img) 'tiff)))
323 (declare-function image-size "image.c" (spec &optional pixels frame))
325 (defun thumbs-file-size (img)
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"
329 (number-to-string (round (cdr i))))))
331 ;;;###autoload
332 (defun thumbs-find-thumb (img)
333 "Display the thumbnail for IMG."
334 (interactive "f")
335 (find-file (thumbs-make-thumb img)))
337 (defun thumbs-insert-image (img type relief &optional marked)
338 "Insert image IMG at point.
339 TYPE and RELIEF will be used in constructing the image; see `image'
340 in the emacs-lisp manual for further documentation.
341 If MARKED is non-nil, the image is marked."
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)
348 (set (make-local-variable 'thumbs-current-image-size)
349 (image-size i t))))
351 (defun thumbs-insert-thumb (img &optional marked)
352 "Insert the thumbnail for IMG at point.
353 If MARKED is non-nil, the image is marked."
354 (thumbs-insert-image
355 (thumbs-make-thumb img) 'jpeg thumbs-relief marked)
356 (add-text-properties (1- (point)) (point)
357 `(thumb-image-file ,img
358 help-echo ,(file-name-nondirectory img)
359 rear-nonsticky help-echo)))
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)
368 (thumbs-insert-thumb img
369 (member img thumbs-marked-list))
370 (when (= 0 (mod (setq i (1+ i)) thumbs-per-line))
371 (newline)))
372 (unless (bobp) (newline))
373 (if (> diff 0) (message "Type + to display more images."))))
375 (defun thumbs-show-thumbs-list (list &optional dir same-window)
376 (unless (and (display-images-p)
377 (image-type-available-p 'jpeg))
378 (error "Required image type is not supported in this Emacs session"))
379 (funcall (if same-window 'switch-to-buffer 'pop-to-buffer)
380 (if dir (concat "*Thumbs: " dir) "*THUMB-View*"))
381 (let ((inhibit-read-only t))
382 (erase-buffer)
383 (thumbs-mode)
384 (setq thumbs-buffer (current-buffer))
385 (if dir (setq default-directory dir))
386 (thumbs-do-thumbs-insertion list)
387 (goto-char (point-min))
388 (set (make-local-variable 'thumbs-current-dir) default-directory)))
390 ;;;###autoload
391 (defun thumbs-show-from-dir (dir &optional reg same-window)
392 "Make a preview buffer for all images in DIR.
393 Optional argument REG to select file matching a regexp,
394 and SAME-WINDOW to show thumbs in the same window."
395 (interactive "DDir: ")
396 (thumbs-show-thumbs-list
397 (directory-files dir t (or reg (image-file-name-regexp)))
398 dir same-window))
400 ;;;###autoload
401 (defun thumbs-dired-show-marked ()
402 "In dired, make a thumbs buffer with marked files."
403 (interactive)
404 (thumbs-show-thumbs-list (dired-get-marked-files) nil t))
406 ;;;###autoload
407 (defun thumbs-dired-show ()
408 "In dired, make a thumbs buffer with all files in current directory."
409 (interactive)
410 (thumbs-show-from-dir default-directory nil t))
412 ;;;###autoload
413 (defalias 'thumbs 'thumbs-show-from-dir)
415 (defun thumbs-find-image (img &optional num otherwin)
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)))))
433 (defun thumbs-find-image-at-point (&optional img otherwin)
434 "Display image IMG for thumbnail at point.
435 Use another window if OTHERWIN is t."
436 (interactive)
437 (let* ((i (or img (thumbs-current-image))))
438 (thumbs-find-image i (point) otherwin)))
440 (defun thumbs-find-image-at-point-other-window ()
441 "Display image for thumbnail at point in the preview buffer.
442 Open another window."
443 (interactive)
444 (thumbs-find-image-at-point nil t))
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))
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))
461 (defun thumbs-set-image-at-point-to-root-window ()
462 "Set the image at point as the desktop wallpaper."
463 (interactive)
464 (thumbs-call-setroot-command
465 (thumbs-current-image)))
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)))
474 (defun thumbs-file-alist ()
475 "Make an alist of elements (POS . FILENAME) for all images in thumb buffer."
476 (with-current-buffer thumbs-buffer
477 (save-excursion
478 (let (list)
479 (goto-char (point-min))
480 (while (not (eobp))
481 (unless (eolp)
482 (if (thumbs-current-image)
483 (push (cons (point-marker)
484 (thumbs-current-image))
485 list)))
486 (forward-char 1))
487 (nreverse list)))))
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))))
500 (defun thumbs-delete-images ()
501 "Delete the image at point (and its thumbnail) (or marked files if any)."
502 (interactive)
503 (let ((files (or thumbs-marked-list (list (thumbs-current-image)))))
504 (if (yes-or-no-p (format "Really delete %d files? " (length files)))
505 (let ((thumbs-file-list (thumbs-file-alist))
506 (inhibit-read-only t))
507 (dolist (x files)
508 (let (failure)
509 (condition-case ()
510 (progn
511 (delete-file x)
512 (delete-file (thumbs-thumbname x)))
513 (file-error (setq failure t)))
514 (unless failure
515 (when (rassoc x thumbs-file-list)
516 (goto-char (car (rassoc x thumbs-file-list)))
517 (delete-region (point) (1+ (point))))
518 (setq thumbs-marked-list
519 (delq x thumbs-marked-list)))))))))
521 (defun thumbs-rename-images (newfile)
522 "Rename the image at point (and its thumbnail) (or marked files if any)."
523 (interactive "FRename to file or directory: ")
524 (let ((files (or thumbs-marked-list (list (thumbs-current-image))))
525 failures)
526 (when thumbs-marked-list
527 (make-directory newfile t)
528 (setq newfile (file-name-as-directory newfile)))
529 (if (yes-or-no-p (format "Really rename %d files? " (length files)))
530 (let ((thumbs-file-list (thumbs-file-alist))
531 (inhibit-read-only t))
532 (dolist (file files)
533 (let (failure)
534 (condition-case ()
535 (rename-file file newfile)
536 (file-error (setq failure t)
537 (push file failures)))
538 (unless failure
539 (when (rassoc file thumbs-file-list)
540 (goto-char (car (rassoc file thumbs-file-list)))
541 (delete-region (point) (1+ (point))))
542 (setq thumbs-marked-list
543 (delq file thumbs-marked-list)))))))
544 (if failures
545 (display-warning 'file-error
546 (format "Rename failures for %s into %s"
547 failures newfile)
548 :error))))
550 (defun thumbs-kill-buffer ()
551 "Kill the current buffer."
552 (interactive)
553 (quit-window t (selected-window)))
555 (defun thumbs-show-image-num (num)
556 "Show the image with number NUM."
557 (let ((image-buffer (get-buffer-create "*Image*")))
558 (let ((img (cdr (nth (1- num) (thumbs-file-alist)))))
559 (with-current-buffer image-buffer
560 (setq mode-name
561 (concat "image-view-mode: " (file-name-nondirectory img)
562 " - " (number-to-string num)))
563 (let ((inhibit-read-only t))
564 (erase-buffer)
565 (thumbs-insert-image img (thumbs-image-type img) 0)
566 (goto-char (point-min))))
567 (setq thumbs-image-num num
568 thumbs-current-image-filename img))))
570 (defun thumbs-previous-image ()
571 "Show the previous image."
572 (interactive)
573 (let* ((i (- thumbs-image-num 1))
574 (number (length (thumbs-file-alist))))
575 (if (= i 0) (setq i (1- number)))
576 (thumbs-show-image-num i)))
578 (defun thumbs-next-image ()
579 "Show the next image."
580 (interactive)
581 (let* ((i (1+ thumbs-image-num))
582 (number (length (thumbs-file-alist))))
583 (if (= i number) (setq i 1))
584 (thumbs-show-image-num i)))
586 (defun thumbs-display-thumbs-buffer ()
587 "Display the associated thumbs buffer."
588 (interactive)
589 (display-buffer thumbs-buffer))
591 (defun thumbs-redraw-buffer ()
592 "Redraw the current thumbs buffer."
593 (let ((p (point))
594 (inhibit-read-only t)
595 (files (thumbs-file-list)))
596 (erase-buffer)
597 (thumbs-do-thumbs-insertion files)
598 (goto-char p)))
600 (defun thumbs-mark ()
601 "Mark the image at point."
602 (interactive)
603 (let ((elt (thumbs-current-image)))
604 (unless elt
605 (error "No image here"))
606 (push elt thumbs-marked-list)
607 (let ((inhibit-read-only t))
608 (delete-char 1)
609 (thumbs-insert-thumb elt t)))
610 (when (eolp) (forward-char)))
612 (defun thumbs-unmark ()
613 "Unmark the image at point."
614 (interactive)
615 (let ((elt (thumbs-current-image)))
616 (unless elt
617 (error "No image here"))
618 (setq thumbs-marked-list (delete elt thumbs-marked-list))
619 (let ((inhibit-read-only t))
620 (delete-char 1)
621 (thumbs-insert-thumb elt nil)))
622 (when (eolp) (forward-char)))
624 ;; cleaning of old temp files
625 (mapc 'delete-file
626 (directory-files (thumbs-temp-dir) t thumbs-temp-prefix))
628 ;; Image modification routines
630 (defun thumbs-modify-image (action &optional arg)
631 "Call convert to do ACTION on image with argument ARG.
632 ACTION and ARG should be a valid convert command."
633 (interactive "sAction: \nsValue: ")
634 (let* ((buffer-read-only nil)
635 (old thumbs-current-tmp-filename)
636 (tmp (thumbs-temp-file)))
637 (erase-buffer)
638 (thumbs-call-convert (or old thumbs-current-image-filename)
640 action
641 (or arg ""))
642 (save-excursion
643 (thumbs-insert-image tmp 'jpeg 0))
644 (setq thumbs-current-tmp-filename tmp)))
646 (defun thumbs-emboss-image (emboss)
647 "Emboss the image with value EMBOSS."
648 (interactive "nEmboss value: ")
649 (if (or (< emboss 3) (> emboss 31) (zerop (% emboss 2)))
650 (error "Arg must be an odd number between 3 and 31"))
651 (thumbs-modify-image "emboss" (number-to-string emboss)))
653 (defun thumbs-monochrome-image ()
654 "Turn the image to monochrome."
655 (interactive)
656 (thumbs-modify-image "monochrome"))
658 (defun thumbs-negate-image ()
659 "Negate the image."
660 (interactive)
661 (thumbs-modify-image "negate"))
663 (defun thumbs-rotate-left ()
664 "Rotate the image 90 degrees counter-clockwise."
665 (interactive)
666 (thumbs-modify-image "rotate" "270"))
668 (defun thumbs-rotate-right ()
669 "Rotate the image 90 degrees clockwise."
670 (interactive)
671 (thumbs-modify-image "rotate" "90"))
673 (defun thumbs-current-image ()
674 "Return the name of the image file name at point."
675 (get-text-property (point) 'thumb-image-file))
677 (defun thumbs-forward-char ()
678 "Move forward one image."
679 (interactive)
680 (forward-char)
681 (while (and (not (eobp)) (not (thumbs-current-image)))
682 (forward-char))
683 (thumbs-show-name))
685 (defun thumbs-backward-char ()
686 "Move backward one image."
687 (interactive)
688 (forward-char -1)
689 (while (and (not (bobp)) (not (thumbs-current-image)))
690 (forward-char -1))
691 (thumbs-show-name))
693 (defun thumbs-backward-line ()
694 "Move up one line."
695 (interactive)
696 (forward-line -1)
697 (thumbs-show-name))
699 (defun thumbs-forward-line ()
700 "Move down one line."
701 (interactive)
702 (forward-line 1)
703 (thumbs-show-name))
705 (defun thumbs-show-more-images (&optional arg)
706 "Show more than `thumbs-max-image-number' images, if present."
707 (interactive "P")
708 (or arg (setq arg 1))
709 (setq thumbs-extra-images (+ thumbs-extra-images arg))
710 (thumbs-dired-show))
712 (defun thumbs-show-name ()
713 "Show the name of the current file."
714 (interactive)
715 (let ((f (thumbs-current-image)))
716 (and f (message "%s [%s]" f (thumbs-file-size f)))))
718 (defun thumbs-save-current-image ()
719 "Save the current image."
720 (interactive)
721 (let ((f (or thumbs-current-tmp-filename
722 thumbs-current-image-filename))
723 (sa (read-from-minibuffer "Save image file as: "
724 thumbs-current-image-filename)))
725 (copy-file f sa)))
727 (defun thumbs-dired ()
728 "Use `dired' on the current thumbs directory."
729 (interactive)
730 (dired thumbs-current-dir))
732 ;; thumbs-mode
734 (defvar thumbs-mode-map
735 (let ((map (make-sparse-keymap)))
736 (define-key map [return] 'thumbs-find-image-at-point)
737 (define-key map [mouse-2] 'thumbs-mouse-find-image)
738 (define-key map [(meta return)] 'thumbs-find-image-at-point-other-window)
739 (define-key map [(control return)] 'thumbs-set-image-at-point-to-root-window)
740 (define-key map [delete] 'thumbs-delete-images)
741 (define-key map [right] 'thumbs-forward-char)
742 (define-key map [left] 'thumbs-backward-char)
743 (define-key map [up] 'thumbs-backward-line)
744 (define-key map [down] 'thumbs-forward-line)
745 (define-key map "+" 'thumbs-show-more-images)
746 (define-key map "d" 'thumbs-dired)
747 (define-key map "m" 'thumbs-mark)
748 (define-key map "u" 'thumbs-unmark)
749 (define-key map "R" 'thumbs-rename-images)
750 (define-key map "x" 'thumbs-delete-images)
751 (define-key map "s" 'thumbs-show-name)
752 (define-key map "q" 'thumbs-kill-buffer)
753 map)
754 "Keymap for `thumbs-mode'.")
756 (put 'thumbs-mode 'mode-class 'special)
757 (define-derived-mode thumbs-mode
758 fundamental-mode "thumbs"
759 "Preview images in a thumbnails buffer"
760 (setq buffer-read-only t))
762 (defvar thumbs-view-image-mode-map
763 (let ((map (make-sparse-keymap)))
764 (define-key map [prior] 'thumbs-previous-image)
765 (define-key map [next] 'thumbs-next-image)
766 (define-key map "^" 'thumbs-display-thumbs-buffer)
767 (define-key map "-" 'thumbs-shrink-image)
768 (define-key map "+" 'thumbs-enlarge-image)
769 (define-key map "<" 'thumbs-rotate-left)
770 (define-key map ">" 'thumbs-rotate-right)
771 (define-key map "e" 'thumbs-emboss-image)
772 (define-key map "r" 'thumbs-resize-image)
773 (define-key map "s" 'thumbs-save-current-image)
774 (define-key map "q" 'thumbs-kill-buffer)
775 (define-key map "w" 'thumbs-set-root)
776 map)
777 "Keymap for `thumbs-view-image-mode'.")
779 ;; thumbs-view-image-mode
780 (put 'thumbs-view-image-mode 'mode-class 'special)
781 (define-derived-mode thumbs-view-image-mode
782 fundamental-mode "image-view-mode"
783 (setq buffer-read-only t))
785 ;;;###autoload
786 (defun thumbs-dired-setroot ()
787 "In dired, call the setroot program on the image at point."
788 (interactive)
789 (thumbs-call-setroot-command (dired-get-filename)))
791 ;; Modif to dired mode map
792 (define-key dired-mode-map "\C-ta" 'thumbs-dired-show)
793 (define-key dired-mode-map "\C-tm" 'thumbs-dired-show-marked)
794 (define-key dired-mode-map "\C-tw" 'thumbs-dired-setroot)
796 (provide 'thumbs)
798 ;;; thumbs.el ends here