Mercurial > emacs
annotate lisp/thumbs.el @ 55209:a2fdb97fe128
*** empty log message ***
author | Juanma Barranquero <lekktu@gmail.com> |
---|---|
date | Tue, 27 Apr 2004 22:46:52 +0000 |
parents | c2c29cafaa74 |
children | a911edb6dadf |
rev | line source |
---|---|
54186 | 1 ;;; thumbs.el --- Thumbnails previewer for images files |
2 ;;; | |
3 ;; Author: Jean-Philippe Theberge <jphiltheberge@videotron.ca> | |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
4 ;; |
54186 | 5 ;; Thanks: Alex Schroeder <alex@gnu.org> for maintaining the package at some time |
6 ;; The peoples at #emacs@freenode.net for numerous help | |
7 ;; RMS for emacs and the GNU project. | |
8 ;; | |
9 ;; Keywords: Multimedia | |
10 | |
11 (defconst thumbs-version "2.0") | |
12 | |
13 ;; This file is part of GNU Emacs. | |
14 | |
15 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
16 ;; it under the terms of the GNU General Public License as published by | |
17 ;; the Free Software Foundation; either version 2, or (at your option) | |
18 ;; any later version. | |
19 | |
20 ;; GNU Emacs is distributed in the hope that it will be useful, | |
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
23 ;; GNU General Public License for more details. | |
24 | |
25 ;; You should have received a copy of the GNU General Public License | |
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
28 ;; Boston, MA 02111-1307, USA. | |
29 | |
30 ;;; Commentary: | |
31 | |
32 ;; This package create two new mode: thumbs-mode and | |
33 ;; thumbs-view-image-mode. It is used for images browsing and viewing | |
34 ;; from within emacs. Minimal image manipulation functions are also | |
35 ;; available via external programs. | |
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: | |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
55 ;; |
54186 | 56 |
57 ;;; Code: | |
58 | |
59 (require 'dired) | |
60 | |
61 ;; Abort if in-line imaging isn't supported (i.e. Emacs-20.7) | |
62 | |
63 (when (not (display-images-p)) | |
64 (error "Your Emacs version (%S) doesn't support in-line images, | |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
65 was not compiled with image support or is run in console mode. |
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
66 Upgrade to Emacs 21.1 or newer, compile it with image support |
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
67 or use a window-system" |
54186 | 68 emacs-version)) |
69 | |
70 ;; CUSTOMIZATIONS | |
71 | |
72 (defgroup thumbs nil | |
73 "Thumbnails previewer." | |
74 :group 'multimedia) | |
75 | |
76 (defcustom thumbs-thumbsdir | |
77 (expand-file-name "~/.emacs-thumbs") | |
78 "*Directory to store thumbnails." | |
79 :type 'directory | |
80 :group 'thumbs) | |
81 | |
82 (defcustom thumbs-geometry "100x100" | |
83 "*Size of thumbnails." | |
84 :type 'string | |
85 :group 'thumbs) | |
86 | |
87 (defcustom thumbs-per-line 5 | |
88 "*Number of thumbnails per line to show in directory." | |
89 :type 'string | |
90 :group 'thumbs) | |
91 | |
92 (defcustom thumbs-thumbsdir-max-size 50000000 | |
93 "Max size for thumbnails directory. | |
94 When it reach that size (in bytes), a warning is send." | |
95 :type 'string | |
96 :group 'thumbs) | |
97 | |
98 (defcustom thumbs-conversion-program | |
99 (if (equal 'windows-nt system-type) | |
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." | |
116 :type 'string | |
117 :group 'thumbs) | |
118 | |
119 (defcustom thumbs-margin 2 | |
120 "*Size of the margin around thumbnails. | |
121 This is where you see the cursor." | |
122 :type 'string | |
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 | |
128 than 'thumbs-thumbsdir-max-size'." | |
129 :type 'boolean | |
130 :group 'thumbs) | |
131 | |
132 (defcustom thumbs-image-resizing-step 10 | |
133 "Step by wich to resize image." | |
134 :type 'string | |
135 :group 'thumbs) | |
136 | |
137 (defcustom thumbs-temp-dir | |
138 "/tmp/" | |
139 "Temporary directory to use. | |
140 Leaving it to default '/tmp/' can let another user | |
141 see some of your images." | |
142 :type 'directory | |
143 :group 'thumbs) | |
144 | |
145 (defcustom thumbs-temp-prefix "emacsthumbs" | |
146 "Prefix to add to temp files." | |
147 :type 'string | |
148 :group 'thumbs) | |
149 | |
150 ;; Initialize some variable, for later use. | |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
151 (defvar thumbs-temp-file |
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
152 (concat thumbs-temp-dir thumbs-temp-prefix) |
54186 | 153 "Temporary filesname for images.") |
154 | |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
155 (defvar thumbs-current-tmp-filename |
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
156 nil |
54186 | 157 "Temporary filename of current image.") |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
158 (defvar thumbs-current-image-filename |
54186 | 159 nil |
160 "Filename of current image.") | |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
161 (defvar thumbs-current-image-size |
54186 | 162 nil |
163 "Size of current image.") | |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
164 (defvar thumbs-image-num |
54186 | 165 nil |
166 "Number of current image.") | |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
167 (defvar thumbs-current-dir |
54186 | 168 nil |
169 "Current directory.") | |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
170 (defvar thumbs-markedL |
54186 | 171 nil |
172 "List of marked files.") | |
173 | |
174 ;; Make sure auto-image-file-mode is ON. | |
175 (auto-image-file-mode t) | |
176 | |
177 ;; Create the thumbs directory if it does not exists. | |
178 (setq thumbs-thumbsdir (expand-file-name thumbs-thumbsdir)) | |
179 | |
180 (when (not (file-directory-p thumbs-thumbsdir)) | |
181 (progn | |
182 (make-directory thumbs-thumbsdir) | |
183 (message "Creating thumbnails directory"))) | |
184 | |
185 (when (not (fboundp 'ignore-errors)) | |
186 (defmacro ignore-errors (&rest body) | |
187 "Execute FORMS; if anz error occurs, return nil. | |
188 Otherwise, return result of last FORM." | |
189 (let ((err (thumbs-gensym))) | |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
190 (list 'condition-case err (cons 'progn body) '(error nil))))) |
54186 | 191 |
192 (when (not (fboundp 'caddar)) | |
193 (defun caddar (x) | |
194 "Return the `car' of the `cdr' of the `cdr' of the `car' of X." | |
195 (car (cdr (cdr (car x)))))) | |
196 | |
197 (defvar thumbs-gensym-counter 0) | |
198 | |
199 (defun thumbs-gensym (&optional arg) | |
200 "Generate a new uninterned symbol. | |
201 The name is made by appending a number to PREFIX, default \"Thumbs\"." | |
202 (let ((prefix (if (stringp arg) arg "Thumbs")) | |
203 (num (if (integerp arg) arg | |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
204 (prog1 |
54186 | 205 thumbs-gensym-counter |
206 (setq thumbs-gensym-counter (1+ thumbs-gensym-counter)))))) | |
207 (make-symbol (format "%s%d" prefix num)))) | |
208 | |
209 (defun thumbs-cleanup-thumbsdir () | |
210 "Clean the thumbnails directory. | |
211 If the total size of all files in 'thumbs-thumbsdir' is bigger than | |
212 'thumbs-thumbsdir-max-size', files are deleted until the max size is | |
213 reached." | |
214 (let* ((filesL | |
215 (sort | |
216 (mapcar | |
217 (lambda (f) | |
218 (let ((fattribsL (file-attributes f))) | |
219 `(,(nth 4 fattribsL) ,(nth 7 fattribsL) ,f))) | |
220 (directory-files thumbs-thumbsdir t (image-file-name-regexp))) | |
221 '(lambda (l1 l2) (time-less-p (car l1)(car l2))))) | |
222 (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) filesL)))) | |
223 (while (> dirsize thumbs-thumbsdir-max-size) | |
224 (progn | |
225 (message "Deleting file %s" (caddar filesL))) | |
226 (delete-file (caddar filesL)) | |
227 (setq dirsize (- dirsize (cadar filesL))) | |
228 (setq filesL (cdr filesL))))) | |
229 | |
230 ;; Check the thumbsnail directory size and clean it if necessary. | |
231 (when thumbs-thumbsdir-auto-clean | |
232 (thumbs-cleanup-thumbsdir)) | |
233 | |
234 (defun thumbs-call-convert (filein fileout action | |
235 &optional arg output-format action-prefix) | |
236 "Call the convert program. | |
237 FILEIN is the input file, | |
238 FILEOUT is the output file, | |
239 ACTION is the command to send to convert. | |
240 Optional argument are: | |
241 ARG any arguments to the ACTION command, | |
242 OUTPUT-FORMAT is the file format to output, default is jpeg | |
243 ACTION-PREFIX is the symbol to place before the ACTION command | |
244 (default to '-' but can sometime be '+')." | |
245 (let ((command (format "%s %s%s %s \"%s\" \"%s:%s\"" | |
246 thumbs-conversion-program | |
247 (or action-prefix "-") | |
248 action | |
249 (or arg "") | |
250 filein | |
251 (or output-format "jpeg") | |
252 fileout))) | |
253 (shell-command command))) | |
254 | |
255 (defun thumbs-increment-image-size-element (n d) | |
256 "Increment number N by D percent." | |
257 (round (+ n (/ (* d n) 100)))) | |
258 | |
259 (defun thumbs-decrement-image-size-element (n d) | |
260 "Decrement number N by D percent." | |
261 (round (- n (/ (* d n) 100)))) | |
262 | |
263 (defun thumbs-increment-image-size (s) | |
264 "Increment S (a cons of width x heigh)." | |
265 (cons | |
266 (thumbs-increment-image-size-element (car s) | |
267 thumbs-image-resizing-step) | |
268 (thumbs-increment-image-size-element (cdr s) | |
269 thumbs-image-resizing-step))) | |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
270 |
54186 | 271 (defun thumbs-decrement-image-size (s) |
272 "Decrement S (a cons of width x heigh)." | |
273 (cons | |
274 (thumbs-decrement-image-size-element (car s) | |
275 thumbs-image-resizing-step) | |
276 (thumbs-decrement-image-size-element (cdr s) | |
277 thumbs-image-resizing-step))) | |
278 | |
279 (defun thumbs-resize-image (&optional increment size) | |
280 "Resize image in current buffer. | |
281 if INCREMENT is set, make the image bigger, else smaller. | |
282 Or, alternatively, a SIZE may be specified." | |
283 (interactive) | |
284 ;; cleaning of old temp file | |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
285 (ignore-errors |
54186 | 286 (apply 'delete-file |
287 (directory-files | |
288 thumbs-temp-dir t | |
289 thumbs-temp-prefix))) | |
290 (let ((buffer-read-only nil) | |
291 (x (if size | |
292 size | |
293 (if increment | |
294 (thumbs-increment-image-size | |
295 thumbs-current-image-size) | |
296 (thumbs-decrement-image-size | |
297 thumbs-current-image-size)))) | |
298 (tmp (format "%s%s.jpg" thumbs-temp-file (thumbs-gensym)))) | |
299 (erase-buffer) | |
300 (thumbs-call-convert thumbs-current-image-filename | |
301 tmp "sample" | |
302 (concat (number-to-string (car x)) "x" | |
303 (number-to-string (cdr x)))) | |
304 (thumbs-insert-image tmp 'jpeg 0) | |
305 (setq thumbs-current-tmp-filename tmp))) | |
306 | |
307 (defun thumbs-resize-interactive (width height) | |
308 "Resize Image interactively to specified WIDTH and HEIGHT." | |
309 (interactive "nWidth: \nnHeight: ") | |
310 (thumbs-resize-image nil (cons width height))) | |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
311 |
54186 | 312 (defun thumbs-resize-image-size-down () |
313 "Resize image (smaller)." | |
314 (interactive) | |
315 (thumbs-resize-image nil)) | |
316 | |
317 (defun thumbs-resize-image-size-up () | |
318 "Resize image (bigger)." | |
319 (interactive) | |
320 (thumbs-resize-image t)) | |
321 | |
322 (defun thumbs-subst-char-in-string (orig rep string) | |
323 "Replace occurrences of character ORIG with character REP in STRING. | |
324 Return the resulting (new) string. -- (defun borowed to Dave Love)" | |
325 (let ((string (copy-sequence string)) | |
326 (l (length string)) | |
327 (i 0)) | |
328 (while (< i l) | |
329 (if (= (aref string i) orig) | |
330 (aset string i rep)) | |
331 (setq i (1+ i))) | |
332 string)) | |
333 | |
334 (defun thumbs-thumbname (img) | |
335 "Return a thumbnail name for the image IMG." | |
336 (concat thumbs-thumbsdir "/" | |
337 (thumbs-subst-char-in-string | |
338 ?\ ?\_ | |
339 (apply | |
340 'concat | |
341 (split-string | |
342 (expand-file-name img) "/"))))) | |
343 | |
344 (defun thumbs-make-thumb (img) | |
345 "Create the thumbnail for IMG." | |
346 (let* ((fn (expand-file-name img)) | |
347 (tn (thumbs-thumbname img))) | |
348 (if (or (not (file-exists-p tn)) | |
349 (not (equal (thumbs-file-size tn) thumbs-geometry))) | |
350 (thumbs-call-convert fn tn "sample" thumbs-geometry)) | |
351 tn)) | |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
352 |
54186 | 353 (defun thumbs-image-type (img) |
354 "Return image type from filename IMG." | |
355 (cond ((string-match ".*\\.jpe?g\\'" img) 'jpeg) | |
356 ((string-match ".*\\.xpm\\'" img) 'xpm) | |
357 ((string-match ".*\\.xbm\\'" img) 'xbm) | |
358 ((string-match ".*\\.gif\\'" img) 'gif) | |
359 ((string-match ".*\\.bmp\\'" img) 'bmp) | |
360 ((string-match ".*\\.png\\'" img) 'png) | |
361 ((string-match ".*\\.tiff?\\'" img) 'tiff))) | |
362 | |
363 (defun thumbs-file-size (img) | |
364 (let ((i (image-size (find-image `((:type ,(thumbs-image-type img) :file ,img))) t))) | |
365 (concat (number-to-string (round (car i))) | |
366 "x" | |
367 (number-to-string (round (cdr i)))))) | |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
368 |
54186 | 369 ;;;###autoload |
370 (defun thumbs-find-thumb (img) | |
371 "Display the thumbnail for IMG." | |
372 (interactive "f") | |
373 (find-file (thumbs-make-thumb img))) | |
374 | |
375 (defun thumbs-insert-image (img type relief &optional marked) | |
376 "Insert image IMG at point. | |
377 TYPE and RELIEF will be used in constructing the image; see `image' | |
378 in the emacs-lisp manual for further documentation. | |
379 if MARKED is non-nil, the image is marked." | |
380 (let ((i `(image :type ,type | |
381 :file ,img | |
382 :relief ,relief | |
383 :conversion ,(if marked 'disabled) | |
384 :margin ,thumbs-margin))) | |
385 (insert-image i) | |
386 (setq thumbs-current-image-size | |
387 (image-size i t)))) | |
388 | |
389 (defun thumbs-insert-thumb (img &optional marked) | |
390 "Insert the thumbnail for IMG at point. | |
391 if MARKED is non-nil, the image is marked" | |
392 (thumbs-insert-image | |
393 (thumbs-make-thumb img) 'jpeg thumbs-relief marked)) | |
394 | |
395 (defun thumbs-do-thumbs-insertion (L) | |
396 "Insert all thumbs in list L." | |
397 (setq thumbs-fileL nil) | |
398 (let ((i 0)) | |
399 (while L | |
400 (when (= 0 (mod (setq i (1+ i)) thumbs-per-line)) | |
401 (newline)) | |
402 (setq thumbs-fileL (cons (cons (point) | |
403 (car L)) | |
404 thumbs-fileL)) | |
405 (thumbs-insert-thumb (car L) | |
406 (member (car L) thumbs-markedL)) | |
407 (setq L (cdr L))))) | |
408 | |
409 (defun thumbs-show-thumbs-list (L &optional buffer-name same-window) | |
410 (funcall (if same-window 'switch-to-buffer 'pop-to-buffer) | |
411 (or buffer-name "*THUMB-View*")) | |
412 (let ((inhibit-read-only t)) | |
413 (erase-buffer) | |
414 (thumbs-mode) | |
415 (make-variable-buffer-local 'thumbs-fileL) | |
416 (setq thumbs-fileL nil) | |
417 (thumbs-do-thumbs-insertion L) | |
418 (goto-char (point-min)) | |
419 (setq thumbs-current-dir default-directory) | |
420 (make-variable-buffer-local 'thumbs-current-dir))) | |
421 | |
422 ;;;###autoload | |
423 (defun thumbs-show-all-from-dir (dir &optional reg same-window) | |
424 "Make a preview buffer for all images in DIR. | |
425 Optional argument REG to select file matching a regexp, | |
426 and SAME-WINDOW to show thumbs in the same window." | |
427 (interactive "DDir: ") | |
428 (thumbs-show-thumbs-list | |
429 (directory-files dir t | |
430 (or reg (image-file-name-regexp))) | |
431 (concat "*Thumbs: " dir) same-window)) | |
432 | |
433 ;;;###autoload | |
434 (defun thumbs-dired-show-marked () | |
435 "In Dired, make a thumbs buffer with all marked files." | |
436 (interactive) | |
437 (thumbs-show-thumbs-list (dired-get-marked-files) nil t)) | |
438 | |
439 ;;;###autoload | |
440 (defun thumbs-dired-show-all () | |
441 "In dired, make a thumbs buffer with all files in current directory." | |
442 (interactive) | |
443 (thumbs-show-all-from-dir default-directory nil t)) | |
444 | |
445 ;;;###autoload | |
446 (defalias 'thumbs 'thumbs-show-all-from-dir) | |
447 | |
448 (defun thumbs-find-image (img L &optional num otherwin) | |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
449 (funcall |
54186 | 450 (if otherwin 'switch-to-buffer-other-window 'switch-to-buffer) |
451 (concat "*Image: " (file-name-nondirectory img) " - " | |
452 (number-to-string (or num 0)) "*")) | |
453 (thumbs-view-image-mode) | |
454 (let ((inhibit-read-only t)) | |
455 (setq thumbs-current-image-filename img | |
456 thumbs-current-tmp-filename nil | |
457 thumbs-image-num (or num 0)) | |
458 (make-variable-buffer-local 'thumbs-current-image-filename) | |
459 (make-variable-buffer-local 'thumbs-current-tmp-filename) | |
460 (make-variable-buffer-local 'thumbs-current-image-size) | |
461 (make-variable-buffer-local 'thumbs-image-num) | |
462 (make-variable-buffer-local 'thumbs-fileL) | |
463 (setq thumbs-fileL L) | |
464 (delete-region (point-min)(point-max)) | |
465 (thumbs-insert-image img (thumbs-image-type img) 0))) | |
466 | |
467 (defun thumbs-find-image-at-point (&optional img otherwin) | |
468 "Display image IMG for thumbnail at point. | |
469 use another window it OTHERWIN is t." | |
470 (interactive) | |
471 (let* ((L thumbs-fileL) | |
472 (n (point)) | |
473 (i (or img (cdr (assoc n L))))) | |
474 (thumbs-find-image i L n otherwin))) | |
475 | |
476 (defun thumbs-find-image-at-point-other-window () | |
477 "Display image for thumbnail at point in the preview buffer. | |
478 Open another window." | |
479 (interactive) | |
480 (thumbs-find-image-at-point nil t)) | |
481 | |
482 (defun thumbs-call-setroot-command (img) | |
483 "Call the setroot program for IMG." | |
484 (run-hooks 'thumbs-before-setroot-hook) | |
485 (shell-command (replace-regexp-in-string | |
486 "\\*" | |
487 (shell-quote-argument (expand-file-name img)) | |
488 thumbs-setroot-command nil t)) | |
489 (run-hooks 'thumbs-after-setroot-hook)) | |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
490 |
54186 | 491 (defun thumbs-set-image-at-point-to-root-window () |
492 "Set the image at point as the desktop wallpaper." | |
493 (interactive) | |
494 (thumbs-call-setroot-command (cdr (assoc (point) thumbs-fileL)))) | |
495 | |
496 (defun thumbs-set-root () | |
497 "Set the current image as root." | |
498 (interactive) | |
499 (thumbs-call-setroot-command | |
500 (or thumbs-current-tmp-filename | |
501 thumbs-current-image-filename))) | |
502 | |
503 (defun thumbs-delete-images () | |
504 "Delete the image at point (and it's thumbnail) (or marked files if any)." | |
505 (interactive) | |
506 (let ((f (or thumbs-markedL (list (cdr (assoc (point) thumbs-fileL)))))) | |
507 (if (yes-or-no-p "Really delete %d files?" (length f)) | |
508 (progn | |
509 (mapcar (lambda (x) | |
510 (setq thumbs-fileL (delete (rassoc x thumbs-fileL) thumbs-fileL)) | |
511 (delete-file x) | |
512 (delete-file (thumbs-thumbname x))) f) | |
513 (thumbs-redraw-buffer))))) | |
514 | |
515 (defun thumbs-kill-buffer () | |
516 "Kill the current buffer." | |
517 (interactive) | |
518 (let ((buffer (current-buffer))) | |
519 (ignore-errors (delete-window (selected-window))) | |
520 (kill-buffer buffer))) | |
521 | |
522 (defun thumbs-show-image-num (num) | |
523 "Show the image with number NUM." | |
524 (let ((inhibit-read-only t)) | |
525 (delete-region (point-min)(point-max)) | |
526 (let ((i (cdr (assoc num thumbs-fileL)))) | |
527 (thumbs-insert-image i (thumbs-image-type i) 0) | |
528 (sleep-for 2) | |
529 (rename-buffer (concat "*Image: " | |
530 (file-name-nondirectory i) | |
531 " - " | |
532 (number-to-string num) "*"))) | |
533 (setq thumbs-image-num num | |
534 thumbs-current-image-filename i))) | |
535 | |
536 (defun thumbs-next-image () | |
537 "Show next image." | |
538 (interactive) | |
539 (let* ((i (1+ thumbs-image-num)) | |
540 (l (caar thumbs-fileL)) | |
541 (num | |
542 (cond ((assoc i thumbs-fileL) i) | |
543 ((>= i l) 1) | |
544 (t (1+ i))))) | |
545 (thumbs-show-image-num num))) | |
546 | |
547 (defun thumbs-previous-image () | |
548 "Show the previous image." | |
549 (interactive) | |
550 (let* ((i (- thumbs-image-num 1)) | |
551 (l (caar thumbs-fileL)) | |
552 (num | |
553 (cond ((assoc i thumbs-fileL) i) | |
554 ((<= i 1) l) | |
555 (t (- i 1))))) | |
556 (thumbs-show-image-num num))) | |
557 | |
558 (defun thumbs-redraw-buffer () | |
559 "Redraw the current thumbs buffer." | |
560 (let ((p (point)) | |
561 (inhibit-read-only t)) | |
562 (delete-region (point-min)(point-max)) | |
563 (thumbs-do-thumbs-insertion (reverse (mapcar 'cdr thumbs-fileL))) | |
564 (goto-char (1+ p)))) | |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
565 |
54186 | 566 (defun thumbs-mark () |
567 "Mark the image at point." | |
568 (interactive) | |
569 (setq thumbs-markedL (cons (cdr (assoc (point) thumbs-fileL)) thumbs-markedL)) | |
570 (let ((inhibit-read-only t)) | |
571 (delete-char 1) | |
572 (thumbs-insert-thumb (cdr (assoc (point) thumbs-fileL)) t)) | |
573 (when (eolp)(forward-char))) | |
55206
c2c29cafaa74
(time-less-p): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
54193
diff
changeset
|
574 |
54186 | 575 ;; Image modification routines |
576 | |
577 (defun thumbs-modify-image (action &optional arg) | |
578 "Call convert to do ACTION on image with argument ARG. | |
579 ACTION and ARG should be legal convert command." | |
580 (interactive "sAction: \nsValue: ") | |
581 ;; cleaning of old temp file | |
582 (mapc 'delete-file | |
583 (directory-files | |
584 thumbs-temp-dir | |
585 t | |
586 thumbs-temp-prefix)) | |
587 (let ((buffer-read-only nil) | |
588 (tmp (format "%s%s.jpg" thumbs-temp-file (thumbs-gensym)))) | |
589 (erase-buffer) | |
590 (thumbs-call-convert thumbs-current-image-filename | |
591 tmp | |
592 action | |
593 (or arg "")) | |
594 (thumbs-insert-image tmp 'jpeg 0) | |
595 (setq thumbs-current-tmp-filename tmp))) | |
596 | |
597 (defun thumbs-emboss-image (emboss) | |
598 "Emboss the image with value EMBOSS." | |
599 (interactive "nEmboss value: ") | |
600 (if (or (< emboss 3)(> emboss 31)(evenp emboss)) | |
601 (error "Arg must be a odd number between 3 and 31")) | |
602 (thumbs-modify-image "emboss" (number-to-string emboss))) | |
603 | |
604 (defun thumbs-monochrome-image () | |
605 "Turn the image to monochrome." | |
606 (interactive) | |
607 (thumbs-modify-image "monochrome")) | |
608 | |
609 (defun thumbs-negate-image () | |
610 "Negate the image." | |
611 (interactive) | |
612 (thumbs-modify-image "negate")) | |
613 | |
614 (defun thumbs-rotate-left () | |
615 "Rotate the image 90 degrees counter-clockwise." | |
616 (interactive) | |
617 (thumbs-modify-image "rotate" "270")) | |
618 | |
619 (defun thumbs-rotate-right () | |
620 "Rotate the image 90 degrees clockwise." | |
621 (interactive) | |
622 (thumbs-modify-image "rotate" "90")) | |
623 | |
624 (defun thumbs-forward-char () | |
625 "Move forward one image." | |
626 (interactive) | |
627 (forward-char) | |
628 (when (eolp)(forward-char)) | |
629 (thumbs-show-name)) | |
630 | |
631 (defun thumbs-backward-char () | |
632 "Move backward one image." | |
633 (interactive) | |
634 (forward-char -1) | |
635 (thumbs-show-name)) | |
636 | |
637 (defun thumbs-forward-line () | |
638 "Move down one line." | |
639 (interactive) | |
640 (forward-line 1) | |
641 (thumbs-show-name)) | |
642 | |
643 (defun thumbs-backward-line () | |
644 "Move up one line." | |
645 (interactive) | |
646 (forward-line -1) | |
647 (thumbs-show-name)) | |
648 | |
649 (defun thumbs-show-name () | |
650 "Show the name of the current file." | |
651 (interactive) | |
652 (let ((f (cdr (assoc (point) thumbs-fileL)))) | |
653 (message "%s [%s]" f (thumbs-file-size f)))) | |
654 | |
655 (defun thumbs-save-current-image () | |
656 "Save the current image." | |
657 (interactive) | |
658 (let ((f (or thumbs-current-tmp-filename | |
659 thumbs-current-image-filename)) | |
660 (sa (read-from-minibuffer "save file as: " | |
661 thumbs-current-image-filename))) | |
662 (copy-file f sa))) | |
663 | |
664 (defun thumbs-dired () | |
665 "Use `dired' on the current thumbs directory." | |
666 (interactive) | |
667 (dired thumbs-current-dir)) | |
668 | |
669 ;; thumbs-mode | |
670 | |
671 (defvar thumbs-mode-map | |
672 (let ((map (make-sparse-keymap))) | |
673 (define-key map [return] 'thumbs-find-image-at-point) | |
674 (define-key map [(meta return)] 'thumbs-find-image-at-point-other-window) | |
675 (define-key map [(control return)] 'thumbs-set-image-at-point-to-root-window) | |
676 (define-key map [delete] 'thumbs-delete-images) | |
677 (define-key map [right] 'thumbs-forward-char) | |
678 (define-key map [left] 'thumbs-backward-char) | |
679 (define-key map [up] 'thumbs-backward-line) | |
680 (define-key map [down] 'thumbs-forward-line) | |
681 (define-key map "d" 'thumbs-dired) | |
682 (define-key map "m" 'thumbs-mark) | |
683 (define-key map "s" 'thumbs-show-name) | |
684 (define-key map "q" 'thumbs-kill-buffer) | |
685 map) | |
686 "Keymap for `thumbs-mode'.") | |
687 | |
688 (define-derived-mode thumbs-mode | |
689 fundamental-mode "thumbs" | |
690 "Preview images in a thumbnails buffer" | |
691 (make-variable-buffer-local 'thumbs-markedL) | |
692 (setq thumbs-markedL nil)) | |
693 | |
694 (defvar thumbs-view-image-mode-map | |
695 (let ((map (make-sparse-keymap))) | |
696 (define-key map [prior] 'thumbs-previous-image) | |
697 (define-key map [next] 'thumbs-next-image) | |
698 (define-key map "-" 'thumbs-resize-image-size-down) | |
699 (define-key map "+" 'thumbs-resize-image-size-up) | |
700 (define-key map "<" 'thumbs-rotate-left) | |
701 (define-key map ">" 'thumbs-rotate-right) | |
702 (define-key map "e" 'thumbs-emboss-image) | |
703 (define-key map "r" 'thumbs-resize-interactive) | |
704 (define-key map "s" 'thumbs-save-current-image) | |
705 (define-key map "q" 'thumbs-kill-buffer) | |
706 (define-key map "w" 'thunbs-set-root) | |
707 map) | |
708 "Keymap for `thumbs-view-image-mode'.") | |
709 | |
710 ;; thumbs-view-image-mode | |
711 (define-derived-mode thumbs-view-image-mode | |
712 fundamental-mode "image-view-mode") | |
713 | |
714 ;;;###autoload | |
715 (defun thumbs-dired-setroot () | |
716 "In dired, Call the setroot program on the image at point." | |
717 (interactive) | |
718 (thumbs-call-setroot-command (dired-get-filename))) | |
719 | |
720 ;; Modif to dired mode map | |
721 (define-key dired-mode-map "\C-ta" 'thumbs-dired-show-all) | |
722 (define-key dired-mode-map "\C-tm" 'thumbs-dired-show-marked) | |
723 (define-key dired-mode-map "\C-tw" 'thumbs-dired-setroot) | |
724 | |
725 (provide 'thumbs) | |
726 | |
727 ;;; thumbs.el ends here | |
728 | |
729 | |
54193 | 730 ;;; arch-tag: f9ac1ef8-83fc-42c0-8069-1fae43fd2e5c |