Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / iimage.el
blobdd654ad7b4057fe17dd5697cf646cdf772f77721
1 ;;; iimage.el --- Inline image minor mode.
3 ;; Copyright (C) 2004-2014 Free Software Foundation, Inc.
5 ;; Author: KOSEKI Yoshinori <kose@meadowy.org>
6 ;; Maintainer: KOSEKI Yoshinori <kose@meadowy.org>
7 ;; Keywords: multimedia
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;;; Commentary:
26 ;; Iimage is a minor mode that displays images, when image-filename
27 ;; exists in the buffer.
28 ;; http://www.netlaputa.ne.jp/~kose/Emacs/iimage.html
30 ;; ** Display images in *Info* buffer.
32 ;; (add-hook 'info-mode-hook 'iimage-mode)
34 ;; .texinfo: @file{file://foo.png}
35 ;; .info: `file://foo.png'
37 ;; ** Display images in Wiki buffer.
39 ;; (add-hook 'wiki-mode-hook 'iimage-mode)
41 ;; wiki-file: [[foo.png]]
43 ;;; Code:
45 (eval-when-compile
46 (require 'image-file))
48 (defgroup iimage nil
49 "Support for inline images."
50 :version "22.1"
51 :group 'image)
53 (defcustom iimage-mode-image-search-path nil
54 "List of directories to search for image files for iimage-mode."
55 :type '(choice (const nil) (repeat directory))
56 :group 'iimage)
58 (defvar iimage-mode-image-filename-regex
59 (concat "[-+./_0-9a-zA-Z]+\\."
60 (regexp-opt (nconc (mapcar #'upcase
61 image-file-name-extensions)
62 image-file-name-extensions)
63 t)))
65 (defcustom iimage-mode-image-regex-alist
66 `((,(concat "\\(`?file://\\|\\[\\[\\|<\\|`\\)?"
67 "\\(" iimage-mode-image-filename-regex "\\)"
68 "\\(\\]\\]\\|>\\|'\\)?") . 2))
69 "Alist of filename REGEXP vs NUM.
70 Each element looks like (REGEXP . NUM).
71 NUM specifies which parenthesized expression in the regexp.
73 Examples of image filename patterns to match:
74 file://foo.png
75 `file://foo.png'
76 \\[\\[foo.gif]]
77 <foo.png>
78 foo.JPG
80 :type '(alist :key-type regexp :value-type integer)
81 :group 'iimage)
83 (defvar iimage-mode-map
84 (let ((map (make-sparse-keymap)))
85 (define-key map "\C-l" 'iimage-recenter)
86 map)
87 "Keymap used in `iimage-mode'.")
89 (defun iimage-recenter (&optional arg)
90 "Re-draw images and recenter."
91 (interactive "P")
92 (iimage-mode-buffer nil)
93 (iimage-mode-buffer t)
94 (recenter arg))
96 ;;;###autoload
97 (define-obsolete-function-alias 'turn-on-iimage-mode 'iimage-mode "24.1")
99 (defun turn-off-iimage-mode ()
100 "Unconditionally turn off iimage mode."
101 (interactive)
102 (iimage-mode 0))
104 (defun iimage-modification-hook (beg end)
105 "Remove display property if a display region is modified."
106 ;;(debug-print "ii1 begin %d, end %d\n" beg end)
107 (let ((inhibit-modification-hooks t)
108 (beg (previous-single-property-change end 'display
109 nil (line-beginning-position)))
110 (end (next-single-property-change beg 'display
111 nil (line-end-position))))
112 (when (and beg end (plist-get (text-properties-at beg) 'display))
113 ;;(debug-print "ii2 begin %d, end %d\n" beg end)
114 (remove-text-properties beg end
115 '(display nil modification-hooks nil)))))
117 (defun iimage-mode-buffer (arg)
118 "Display images if ARG is non-nil, undisplay them otherwise."
119 (let ((image-path (cons default-directory iimage-mode-image-search-path))
120 file)
121 (with-silent-modifications
122 (save-excursion
123 (goto-char (point-min))
124 (dolist (pair iimage-mode-image-regex-alist)
125 (while (re-search-forward (car pair) nil t)
126 (when (and (setq file (match-string (cdr pair)))
127 (setq file (locate-file file image-path)))
128 ;; FIXME: we don't mark our images, so we can't reliably
129 ;; remove them either (we may leave some of ours, and we
130 ;; may remove other packages's display properties).
131 (if arg
132 (add-text-properties (match-beginning 0) (match-end 0)
133 `(display ,(create-image file)
134 modification-hooks
135 (iimage-modification-hook)))
136 (remove-text-properties (match-beginning 0) (match-end 0)
137 '(display modification-hooks))))))))))
139 ;;;###autoload
140 (define-minor-mode iimage-mode nil
141 :group 'iimage :lighter " iImg" :keymap iimage-mode-map
142 (iimage-mode-buffer iimage-mode))
144 (provide 'iimage)
146 ;;; iimage.el ends here