1 ;;; iimage.el --- Inline image minor mode.
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: KOSEKI Yoshinori <kose@meadowy.org>
7 ;; Maintainer: KOSEKI Yoshinori <kose@meadowy.org>
8 ;; Keywords: multimedia
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
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
31 ;; ** Display images in *Info* buffer.
33 ;; (add-hook 'info-mode-hook 'iimage-mode)
35 ;; .texinfo: @file{file://foo.png}
36 ;; .info: `file://foo.png'
38 ;; ** Display images in Wiki buffer.
40 ;; (add-hook 'wiki-mode-hook 'iimage-mode)
42 ;; wiki-file: [[foo.png]]
47 (require 'image-file
))
50 "Support for inline images."
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
))
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
)
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.
74 Examples of image filename patterns to match:
81 :type
'(alist :key-type regexp
:value-type integer
)
84 (defvar iimage-mode-map
85 (let ((map (make-sparse-keymap)))
86 (define-key map
"\C-l" 'iimage-recenter
)
88 "Keymap used in `iimage-mode'.")
90 (defun iimage-recenter (&optional arg
)
91 "Re-draw images and recenter."
93 (iimage-mode-buffer nil
)
94 (iimage-mode-buffer t
)
98 (define-obsolete-function-alias 'turn-on-iimage-mode
'iimage-mode
"24.1")
100 (defun turn-off-iimage-mode ()
101 "Unconditionally turn off iimage mode."
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
)))))
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
))
122 (with-silent-modifications
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).
133 (add-text-properties (match-beginning 0) (match-end 0)
134 `(display ,(create-image file
)
136 (iimage-modification-hook)))
137 (remove-text-properties (match-beginning 0) (match-end 0)
138 '(display modification-hooks
))))))))))
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
))
148 ;;; iimage.el ends here