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