thumbs.el 26 KB

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