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