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