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