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