1 ;;; thumbs.el --- Thumbnails previewer for images files
3 ;; Author: Jean-Philippe Theberge <jphiltheberge@videotron.ca>
5 ;; Thanks: Alex Schroeder <alex@gnu.org> for maintaining the package at some time
6 ;; The peoples at #emacs@freenode.net for numerous help
7 ;; RMS for emacs and the GNU project.
9 ;; Keywords: Multimedia
11 (defconst thumbs-version
"2.0")
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
32 ;; This package create two new mode: thumbs-mode and
33 ;; thumbs-view-image-mode. It is used for images browsing and viewing
34 ;; from within emacs. Minimal image manipulation functions are also
35 ;; available via external programs.
37 ;; The 'convert' program from 'ImageMagick'
38 ;; [URL:http://www.imagemagick.org/] is required.
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
43 ;; This is version 2.0
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.
63 ;; Abort if in-line imaging isn't supported (i.e. Emacs-20.7)
65 (when (not (display-images-p))
66 (error "Your Emacs version (%S) doesn't support in-line images,
67 was not compiled with image support or is run in console mode.
68 Upgrade to Emacs 21.1 or newer, compile it with image support
69 or use a window-system"
75 "Thumbnails previewer."
78 (defcustom thumbs-thumbsdir
79 (expand-file-name "~/.emacs-thumbs")
80 "*Directory to store thumbnails."
84 (defcustom thumbs-geometry
"100x100"
85 "*Size of thumbnails."
89 (defcustom thumbs-per-line
5
90 "*Number of thumbnails per line to show in directory."
94 (defcustom thumbs-thumbsdir-max-size
50000000
95 "Max size for thumbnails directory.
96 When it reach that size (in bytes), a warning is send."
100 (defcustom thumbs-conversion-program
101 (if (equal 'windows-nt system-type
)
103 (or (executable-find "convert")
104 "/usr/X11R6/bin/convert"))
105 "*Name of conversion program for thumbnails generation.
106 It must be 'convert'."
110 (defcustom thumbs-setroot-command
111 "xloadimage -onroot -fullscreen *"
112 "Command to set the root window."
116 (defcustom thumbs-relief
5
117 "*Size of button-like border around thumbnails."
121 (defcustom thumbs-margin
2
122 "*Size of the margin around thumbnails.
123 This is where you see the cursor."
127 (defcustom thumbs-thumbsdir-auto-clean t
128 "If set, delete older file in the thumbnails directory.
129 Deletion is done at load time when the directory size is bigger
130 than 'thumbs-thumbsdir-max-size'."
134 (defcustom thumbs-image-resizing-step
10
135 "Step by wich to resize image."
139 (defcustom thumbs-temp-dir
141 "Temporary directory to use.
142 Leaving it to default '/tmp/' can let another user
143 see some of your images."
147 (defcustom thumbs-temp-prefix
"emacsthumbs"
148 "Prefix to add to temp files."
152 ;; Initialize some variable, for later use.
153 (defvar thumbs-temp-file
154 (concat thumbs-temp-dir thumbs-temp-prefix
)
155 "Temporary filesname for images.")
157 (defvar thumbs-current-tmp-filename
159 "Temporary filename of current image.")
160 (defvar thumbs-current-image-filename
162 "Filename of current image.")
163 (defvar thumbs-current-image-size
165 "Size of current image.")
166 (defvar thumbs-image-num
168 "Number of current image.")
169 (defvar thumbs-current-dir
171 "Current directory.")
172 (defvar thumbs-markedL
174 "List of marked files.")
176 ;; Make sure auto-image-file-mode is ON.
177 (auto-image-file-mode t
)
179 ;; Create the thumbs directory if it does not exists.
180 (setq thumbs-thumbsdir
(expand-file-name thumbs-thumbsdir
))
182 (when (not (file-directory-p thumbs-thumbsdir
))
184 (make-directory thumbs-thumbsdir
)
185 (message "Creating thumbnails directory")))
187 (defvar thumbs-gensym-counter
0)
189 (defun thumbs-gensym (&optional arg
)
190 "Generate a new uninterned symbol.
191 The name is made by appending a number to PREFIX, default \"Thumbs\"."
192 (let ((prefix (if (stringp arg
) arg
"Thumbs"))
193 (num (if (integerp arg
) arg
195 thumbs-gensym-counter
196 (setq thumbs-gensym-counter
(1+ thumbs-gensym-counter
))))))
197 (make-symbol (format "%s%d" prefix num
))))
199 (defun thumbs-cleanup-thumbsdir ()
200 "Clean the thumbnails directory.
201 If the total size of all files in 'thumbs-thumbsdir' is bigger than
202 'thumbs-thumbsdir-max-size', files are deleted until the max size is
208 (let ((fattribsL (file-attributes f
)))
209 `(,(nth 4 fattribsL
) ,(nth 7 fattribsL
) ,f
)))
210 (directory-files thumbs-thumbsdir t
(image-file-name-regexp)))
211 '(lambda (l1 l2
) (time-less-p (car l1
)(car l2
)))))
212 (dirsize (apply '+ (mapcar (lambda (x) (cadr x
)) filesL
))))
213 (while (> dirsize thumbs-thumbsdir-max-size
)
215 (message "Deleting file %s" (caddar filesL
)))
216 (delete-file (caddar filesL
))
217 (setq dirsize
(- dirsize
(cadar filesL
)))
218 (setq filesL
(cdr filesL
)))))
220 ;; Check the thumbsnail directory size and clean it if necessary.
221 (when thumbs-thumbsdir-auto-clean
222 (thumbs-cleanup-thumbsdir))
224 (defun thumbs-call-convert (filein fileout action
225 &optional arg output-format action-prefix
)
226 "Call the convert program.
227 FILEIN is the input file,
228 FILEOUT is the output file,
229 ACTION is the command to send to convert.
230 Optional argument are:
231 ARG any arguments to the ACTION command,
232 OUTPUT-FORMAT is the file format to output, default is jpeg
233 ACTION-PREFIX is the symbol to place before the ACTION command
234 (default to '-' but can sometime be '+')."
235 (let ((command (format "%s %s%s %s \"%s\" \"%s:%s\""
236 thumbs-conversion-program
237 (or action-prefix
"-")
241 (or output-format
"jpeg")
243 (shell-command command
)))
245 (defun thumbs-increment-image-size-element (n d
)
246 "Increment number N by D percent."
247 (round (+ n
(/ (* d n
) 100))))
249 (defun thumbs-decrement-image-size-element (n d
)
250 "Decrement number N by D percent."
251 (round (- n
(/ (* d n
) 100))))
253 (defun thumbs-increment-image-size (s)
254 "Increment S (a cons of width x heigh)."
256 (thumbs-increment-image-size-element (car s
)
257 thumbs-image-resizing-step
)
258 (thumbs-increment-image-size-element (cdr s
)
259 thumbs-image-resizing-step
)))
261 (defun thumbs-decrement-image-size (s)
262 "Decrement S (a cons of width x heigh)."
264 (thumbs-decrement-image-size-element (car s
)
265 thumbs-image-resizing-step
)
266 (thumbs-decrement-image-size-element (cdr s
)
267 thumbs-image-resizing-step
)))
269 (defun thumbs-resize-image (&optional increment size
)
270 "Resize image in current buffer.
271 if INCREMENT is set, make the image bigger, else smaller.
272 Or, alternatively, a SIZE may be specified."
274 ;; cleaning of old temp file
279 thumbs-temp-prefix
)))
280 (let ((buffer-read-only nil
)
284 (thumbs-increment-image-size
285 thumbs-current-image-size
)
286 (thumbs-decrement-image-size
287 thumbs-current-image-size
))))
288 (tmp (format "%s%s.jpg" thumbs-temp-file
(thumbs-gensym))))
290 (thumbs-call-convert thumbs-current-image-filename
292 (concat (number-to-string (car x
)) "x"
293 (number-to-string (cdr x
))))
294 (thumbs-insert-image tmp
'jpeg
0)
295 (setq thumbs-current-tmp-filename tmp
)))
297 (defun thumbs-resize-interactive (width height
)
298 "Resize Image interactively to specified WIDTH and HEIGHT."
299 (interactive "nWidth: \nnHeight: ")
300 (thumbs-resize-image nil
(cons width height
)))
302 (defun thumbs-resize-image-size-down ()
303 "Resize image (smaller)."
305 (thumbs-resize-image nil
))
307 (defun thumbs-resize-image-size-up ()
308 "Resize image (bigger)."
310 (thumbs-resize-image t
))
312 (defun thumbs-subst-char-in-string (orig rep string
)
313 "Replace occurrences of character ORIG with character REP in STRING.
314 Return the resulting (new) string. -- (defun borowed to Dave Love)"
315 (let ((string (copy-sequence string
))
319 (if (= (aref string i
) orig
)
324 (defun thumbs-thumbname (img)
325 "Return a thumbnail name for the image IMG."
326 (concat thumbs-thumbsdir
"/"
327 (thumbs-subst-char-in-string
332 (expand-file-name img
) "/")))))
334 (defun thumbs-make-thumb (img)
335 "Create the thumbnail for IMG."
336 (let* ((fn (expand-file-name img
))
337 (tn (thumbs-thumbname img
)))
338 (if (or (not (file-exists-p tn
))
339 (not (equal (thumbs-file-size tn
) thumbs-geometry
)))
340 (thumbs-call-convert fn tn
"sample" thumbs-geometry
))
343 (defun thumbs-image-type (img)
344 "Return image type from filename IMG."
345 (cond ((string-match ".*\\.jpe?g\\'" img
) 'jpeg
)
346 ((string-match ".*\\.xpm\\'" img
) 'xpm
)
347 ((string-match ".*\\.xbm\\'" img
) 'xbm
)
348 ((string-match ".*\\.gif\\'" img
) 'gif
)
349 ((string-match ".*\\.bmp\\'" img
) 'bmp
)
350 ((string-match ".*\\.png\\'" img
) 'png
)
351 ((string-match ".*\\.tiff?\\'" img
) 'tiff
)))
353 (defun thumbs-file-size (img)
354 (let ((i (image-size (find-image `((:type
,(thumbs-image-type img
) :file
,img
))) t
)))
355 (concat (number-to-string (round (car i
)))
357 (number-to-string (round (cdr i
))))))
360 (defun thumbs-find-thumb (img)
361 "Display the thumbnail for IMG."
363 (find-file (thumbs-make-thumb img
)))
365 (defun thumbs-insert-image (img type relief
&optional marked
)
366 "Insert image IMG at point.
367 TYPE and RELIEF will be used in constructing the image; see `image'
368 in the emacs-lisp manual for further documentation.
369 if MARKED is non-nil, the image is marked."
370 (let ((i `(image :type
,type
373 :conversion
,(if marked
'disabled
)
374 :margin
,thumbs-margin
)))
376 (setq thumbs-current-image-size
379 (defun thumbs-insert-thumb (img &optional marked
)
380 "Insert the thumbnail for IMG at point.
381 if MARKED is non-nil, the image is marked"
383 (thumbs-make-thumb img
) 'jpeg thumbs-relief marked
))
385 (defun thumbs-do-thumbs-insertion (L)
386 "Insert all thumbs in list L."
387 (setq thumbs-fileL nil
)
390 (when (= 0 (mod (setq i
(1+ i
)) thumbs-per-line
))
392 (setq thumbs-fileL
(cons (cons (point)
395 (thumbs-insert-thumb (car L
)
396 (member (car L
) thumbs-markedL
))
399 (defun thumbs-show-thumbs-list (L &optional buffer-name same-window
)
400 (funcall (if same-window
'switch-to-buffer
'pop-to-buffer
)
401 (or buffer-name
"*THUMB-View*"))
402 (let ((inhibit-read-only t
))
405 (make-variable-buffer-local 'thumbs-fileL
)
406 (setq thumbs-fileL nil
)
407 (thumbs-do-thumbs-insertion L
)
408 (goto-char (point-min))
409 (setq thumbs-current-dir default-directory
)
410 (make-variable-buffer-local 'thumbs-current-dir
)))
413 (defun thumbs-show-all-from-dir (dir &optional reg same-window
)
414 "Make a preview buffer for all images in DIR.
415 Optional argument REG to select file matching a regexp,
416 and SAME-WINDOW to show thumbs in the same window."
417 (interactive "DDir: ")
418 (thumbs-show-thumbs-list
419 (directory-files dir t
420 (or reg
(image-file-name-regexp)))
421 (concat "*Thumbs: " dir
) same-window
))
424 (defun thumbs-dired-show-marked ()
425 "In Dired, make a thumbs buffer with all marked files."
427 (thumbs-show-thumbs-list (dired-get-marked-files) nil t
))
430 (defun thumbs-dired-show-all ()
431 "In dired, make a thumbs buffer with all files in current directory."
433 (thumbs-show-all-from-dir default-directory nil t
))
436 (defalias 'thumbs
'thumbs-show-all-from-dir
)
438 (defun thumbs-find-image (img L
&optional num otherwin
)
440 (if otherwin
'switch-to-buffer-other-window
'switch-to-buffer
)
441 (concat "*Image: " (file-name-nondirectory img
) " - "
442 (number-to-string (or num
0)) "*"))
443 (thumbs-view-image-mode)
444 (let ((inhibit-read-only t
))
445 (setq thumbs-current-image-filename img
446 thumbs-current-tmp-filename nil
447 thumbs-image-num
(or num
0))
448 (make-variable-buffer-local 'thumbs-current-image-filename
)
449 (make-variable-buffer-local 'thumbs-current-tmp-filename
)
450 (make-variable-buffer-local 'thumbs-current-image-size
)
451 (make-variable-buffer-local 'thumbs-image-num
)
452 (make-variable-buffer-local 'thumbs-fileL
)
453 (setq thumbs-fileL L
)
454 (delete-region (point-min)(point-max))
455 (thumbs-insert-image img
(thumbs-image-type img
) 0)))
457 (defun thumbs-find-image-at-point (&optional img otherwin
)
458 "Display image IMG for thumbnail at point.
459 use another window it OTHERWIN is t."
461 (let* ((L thumbs-fileL
)
463 (i (or img
(cdr (assoc n L
)))))
464 (thumbs-find-image i L n otherwin
)))
466 (defun thumbs-find-image-at-point-other-window ()
467 "Display image for thumbnail at point in the preview buffer.
468 Open another window."
470 (thumbs-find-image-at-point nil t
))
472 (defun thumbs-call-setroot-command (img)
473 "Call the setroot program for IMG."
474 (run-hooks 'thumbs-before-setroot-hook
)
475 (shell-command (replace-regexp-in-string
477 (shell-quote-argument (expand-file-name img
))
478 thumbs-setroot-command nil t
))
479 (run-hooks 'thumbs-after-setroot-hook
))
481 (defun thumbs-set-image-at-point-to-root-window ()
482 "Set the image at point as the desktop wallpaper."
484 (thumbs-call-setroot-command (cdr (assoc (point) thumbs-fileL
))))
486 (defun thumbs-set-root ()
487 "Set the current image as root."
489 (thumbs-call-setroot-command
490 (or thumbs-current-tmp-filename
491 thumbs-current-image-filename
)))
493 (defun thumbs-delete-images ()
494 "Delete the image at point (and it's thumbnail) (or marked files if any)."
496 (let ((f (or thumbs-markedL
(list (cdr (assoc (point) thumbs-fileL
))))))
497 (if (yes-or-no-p (format "Really delete %d files? " (length f
)))
500 (setq thumbs-fileL
(delete (rassoc x thumbs-fileL
) thumbs-fileL
))
502 (delete-file (thumbs-thumbname x
))) f
)
503 (thumbs-redraw-buffer)))))
505 (defun thumbs-kill-buffer ()
506 "Kill the current buffer."
508 (let ((buffer (current-buffer)))
509 (ignore-errors (delete-window (selected-window)))
510 (kill-buffer buffer
)))
512 (defun thumbs-show-image-num (num)
513 "Show the image with number NUM."
514 (let ((inhibit-read-only t
))
515 (delete-region (point-min)(point-max))
516 (let ((i (cdr (assoc num thumbs-fileL
))))
517 (thumbs-insert-image i
(thumbs-image-type i
) 0)
519 (rename-buffer (concat "*Image: "
520 (file-name-nondirectory i
)
522 (number-to-string num
) "*"))
523 (setq thumbs-image-num num
524 thumbs-current-image-filename i
))))
526 (defun thumbs-next-image ()
529 (let* ((i (1+ thumbs-image-num
))
530 (l (caar thumbs-fileL
))
532 (cond ((assoc i thumbs-fileL
) i
)
535 (thumbs-show-image-num num
)))
537 (defun thumbs-previous-image ()
538 "Show the previous image."
540 (let* ((i (- thumbs-image-num
1))
541 (l (caar thumbs-fileL
))
543 (cond ((assoc i thumbs-fileL
) i
)
546 (thumbs-show-image-num num
)))
548 (defun thumbs-redraw-buffer ()
549 "Redraw the current thumbs buffer."
551 (inhibit-read-only t
))
552 (delete-region (point-min)(point-max))
553 (thumbs-do-thumbs-insertion (reverse (mapcar 'cdr thumbs-fileL
)))
556 (defun thumbs-mark ()
557 "Mark the image at point."
559 (setq thumbs-markedL
(cons (cdr (assoc (point) thumbs-fileL
)) thumbs-markedL
))
560 (let ((inhibit-read-only t
))
562 (thumbs-insert-thumb (cdr (assoc (point) thumbs-fileL
)) t
))
563 (when (eolp)(forward-char)))
565 ;; Image modification routines
567 (defun thumbs-modify-image (action &optional arg
)
568 "Call convert to do ACTION on image with argument ARG.
569 ACTION and ARG should be legal convert command."
570 (interactive "sAction: \nsValue: ")
571 ;; cleaning of old temp file
577 (let ((buffer-read-only nil
)
578 (tmp (format "%s%s.jpg" thumbs-temp-file
(thumbs-gensym))))
580 (thumbs-call-convert thumbs-current-image-filename
584 (thumbs-insert-image tmp
'jpeg
0)
585 (setq thumbs-current-tmp-filename tmp
)))
587 (defun thumbs-emboss-image (emboss)
588 "Emboss the image with value EMBOSS."
589 (interactive "nEmboss value: ")
590 (if (or (< emboss
3) (> emboss
31) (zerop (logand emboss
1)))
591 (error "Arg must be a odd number between 3 and 31"))
592 (thumbs-modify-image "emboss" (number-to-string emboss
)))
594 (defun thumbs-monochrome-image ()
595 "Turn the image to monochrome."
597 (thumbs-modify-image "monochrome"))
599 (defun thumbs-negate-image ()
602 (thumbs-modify-image "negate"))
604 (defun thumbs-rotate-left ()
605 "Rotate the image 90 degrees counter-clockwise."
607 (thumbs-modify-image "rotate" "270"))
609 (defun thumbs-rotate-right ()
610 "Rotate the image 90 degrees clockwise."
612 (thumbs-modify-image "rotate" "90"))
614 (defun thumbs-forward-char ()
615 "Move forward one image."
618 (when (eolp)(forward-char))
621 (defun thumbs-backward-char ()
622 "Move backward one image."
627 (defun thumbs-forward-line ()
628 "Move down one line."
633 (defun thumbs-backward-line ()
639 (defun thumbs-show-name ()
640 "Show the name of the current file."
642 (let ((f (cdr (assoc (point) thumbs-fileL
))))
643 (message "%s [%s]" f
(thumbs-file-size f
))))
645 (defun thumbs-save-current-image ()
646 "Save the current image."
648 (let ((f (or thumbs-current-tmp-filename
649 thumbs-current-image-filename
))
650 (sa (read-from-minibuffer "save file as: "
651 thumbs-current-image-filename
)))
654 (defun thumbs-dired ()
655 "Use `dired' on the current thumbs directory."
657 (dired thumbs-current-dir
))
661 (defvar thumbs-mode-map
662 (let ((map (make-sparse-keymap)))
663 (define-key map
[return] 'thumbs-find-image-at-point)
664 (define-key map [(meta return)] 'thumbs-find-image-at-point-other-window)
665 (define-key map [(control return)] 'thumbs-set-image-at-point-to-root-window)
666 (define-key map [delete] 'thumbs-delete-images)
667 (define-key map [right] 'thumbs-forward-char)
668 (define-key map [left] 'thumbs-backward-char)
669 (define-key map [up] 'thumbs-backward-line)
670 (define-key map [down] 'thumbs-forward-line)
671 (define-key map "d" 'thumbs-dired)
672 (define-key map "m" 'thumbs-mark)
673 (define-key map "s" 'thumbs-show-name)
674 (define-key map "q" 'thumbs-kill-buffer)
676 "Keymap for `thumbs-mode'.")
678 (define-derived-mode thumbs-mode
679 fundamental-mode "thumbs"
680 "Preview images in a thumbnails buffer"
681 (make-variable-buffer-local 'thumbs-markedL)
682 (setq thumbs-markedL nil))
684 (defvar thumbs-view-image-mode-map
685 (let ((map (make-sparse-keymap)))
686 (define-key map [prior] 'thumbs-previous-image)
687 (define-key map [next] 'thumbs-next-image)
688 (define-key map "-" 'thumbs-resize-image-size-down)
689 (define-key map "+" 'thumbs-resize-image-size-up)
690 (define-key map "<" 'thumbs-rotate-left)
691 (define-key map ">" 'thumbs-rotate-right)
692 (define-key map "e" 'thumbs-emboss-image)
693 (define-key map "r" 'thumbs-resize-interactive)
694 (define-key map "s" 'thumbs-save-current-image)
695 (define-key map "q" 'thumbs-kill-buffer)
696 (define-key map "w" 'thunbs-set-root)
698 "Keymap for `thumbs-view-image-mode'.")
700 ;; thumbs-view-image-mode
701 (define-derived-mode thumbs-view-image-mode
702 fundamental-mode "image-view-mode")
705 (defun thumbs-dired-setroot ()
706 "In dired, Call the setroot program on the image at point."
708 (thumbs-call-setroot-command (dired-get-filename)))
710 ;; Modif to dired mode map
711 (define-key dired-mode-map "\C-ta" 'thumbs-dired-show-all)
712 (define-key dired-mode-map "\C-tm" 'thumbs-dired-show-marked)
713 (define-key dired-mode-map "\C-tw" 'thumbs-dired-setroot)
717 ;;; thumbs.el ends here
720 ;;; arch-tag: f9ac1ef8-83fc-42c0-8069-1fae43fd2e5c