]> code.delx.au - gnu-emacs/blob - lisp/iimage.el
Minor iimage.el changes.
[gnu-emacs] / lisp / iimage.el
1 ;;; iimage.el --- Inline image minor mode.
2
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: KOSEKI Yoshinori <kose@meadowy.org>
7 ;; Maintainer: KOSEKI Yoshinori <kose@meadowy.org>
8 ;; Keywords: multimedia
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; Iimage is a minor mode that displays images, when image-filename
28 ;; exists in the buffer.
29 ;; http://www.netlaputa.ne.jp/~kose/Emacs/iimage.html
30 ;;
31 ;; ** Display images in *Info* buffer.
32 ;;
33 ;; (add-hook 'info-mode-hook 'iimage-mode)
34 ;;
35 ;; .texinfo: @file{file://foo.png}
36 ;; .info: `file://foo.png'
37 ;;
38 ;; ** Display images in Wiki buffer.
39 ;;
40 ;; (add-hook 'wiki-mode-hook 'iimage-mode)
41 ;;
42 ;; wiki-file: [[foo.png]]
43
44 ;;; Code:
45
46 (eval-when-compile
47 (require 'image-file))
48
49 (defgroup iimage nil
50 "Support for inline images."
51 :version "22.1"
52 :group 'image)
53
54 (defcustom iimage-mode-image-search-path nil
55 "List of directories to search for image files for iimage-mode."
56 :type '(choice (const nil) (repeat directory))
57 :group 'iimage)
58
59 (defvar iimage-mode-image-filename-regex
60 (concat "[-+./_0-9a-zA-Z]+\\."
61 (regexp-opt (nconc (mapcar #'upcase
62 image-file-name-extensions)
63 image-file-name-extensions)
64 t)))
65
66 (defcustom iimage-mode-image-regex-alist
67 `((,(concat "\\(`?file://\\|\\[\\[\\|<\\|`\\)?"
68 "\\(" iimage-mode-image-filename-regex "\\)"
69 "\\(\\]\\]\\|>\\|'\\)?") . 2))
70 "Alist of filename REGEXP vs NUM.
71 Each element looks like (REGEXP . NUM).
72 NUM specifies which parenthesized expression in the regexp.
73
74 Examples of image filename patterns to match:
75 file://foo.png
76 `file://foo.png'
77 \\[\\[foo.gif]]
78 <foo.png>
79 foo.JPG
80 "
81 :type '(alist :key-type regexp :value-type integer)
82 :group 'iimage)
83
84 (defvar iimage-mode-map
85 (let ((map (make-sparse-keymap)))
86 (define-key map "\C-l" 'iimage-recenter)
87 map)
88 "Keymap used in `iimage-mode'.")
89
90 (defun iimage-recenter (&optional arg)
91 "Re-draw images and recenter."
92 (interactive "P")
93 (iimage-mode-buffer nil)
94 (iimage-mode-buffer t)
95 (recenter arg))
96
97 ;;;###autoload
98 (define-obsolete-function-alias 'turn-on-iimage-mode 'iimage-mode "24.1")
99
100 (defun turn-off-iimage-mode ()
101 "Unconditionally turn off iimage mode."
102 (interactive)
103 (iimage-mode 0))
104
105 (defun iimage-modification-hook (beg end)
106 "Remove display property if a display region is modified."
107 ;;(debug-print "ii1 begin %d, end %d\n" beg end)
108 (let ((inhibit-modification-hooks t)
109 (beg (previous-single-property-change end 'display
110 nil (line-beginning-position)))
111 (end (next-single-property-change beg 'display
112 nil (line-end-position))))
113 (when (and beg end (plist-get (text-properties-at beg) 'display))
114 ;;(debug-print "ii2 begin %d, end %d\n" beg end)
115 (remove-text-properties beg end
116 '(display nil modification-hooks nil)))))
117
118 (defun iimage-mode-buffer (arg)
119 "Display images if ARG is non-nil, undisplay them otherwise."
120 (let ((image-path (cons default-directory iimage-mode-image-search-path))
121 file)
122 (with-silent-modifications
123 (save-excursion
124 (goto-char (point-min))
125 (dolist (pair iimage-mode-image-regex-alist)
126 (while (re-search-forward (car pair) nil t)
127 (when (and (setq file (match-string (cdr pair)))
128 (setq file (locate-file file image-path)))
129 ;; FIXME: we don't mark our images, so we can't reliably
130 ;; remove them either (we may leave some of ours, and we
131 ;; may remove other packages's display properties).
132 (if arg
133 (add-text-properties (match-beginning 0) (match-end 0)
134 `(display ,(create-image file)
135 modification-hooks
136 (iimage-modification-hook)))
137 (remove-text-properties (match-beginning 0) (match-end 0)
138 '(display modification-hooks))))))))))
139
140 ;;;###autoload
141 (define-minor-mode iimage-mode
142 "Toggle inline image minor mode."
143 :group 'iimage :lighter " iImg" :keymap iimage-mode-map
144 (iimage-mode-buffer iimage-mode))
145
146 (provide 'iimage)
147
148 ;;; iimage.el ends here