Commit | Line | Data |
---|---|---|
1e77b112 EZ |
1 | ;;; iimage.el --- Inline image minor mode. |
2 | ||
e9bffc61 GM |
3 | ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 |
4 | ;; Free Software Foundation, Inc. | |
1e77b112 EZ |
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 | ||
eb3fa2cf | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
1e77b112 | 13 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
1e77b112 EZ |
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 | |
eb3fa2cf | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
1e77b112 EZ |
24 | |
25 | ;;; Commentary: | |
26 | ||
610101de EZ |
27 | ;; Iimage is a minor mode that displays images, when image-filename |
28 | ;; exists in the buffer. | |
1e77b112 EZ |
29 | ;; http://www.netlaputa.ne.jp/~kose/Emacs/iimage.html |
30 | ;; | |
1e77b112 EZ |
31 | ;; ** Display images in *Info* buffer. |
32 | ;; | |
52da2182 | 33 | ;; (add-hook 'info-mode-hook 'iimage-mode) |
1e77b112 EZ |
34 | ;; |
35 | ;; .texinfo: @file{file://foo.png} | |
36 | ;; .info: `file://foo.png' | |
37 | ;; | |
38 | ;; ** Display images in Wiki buffer. | |
39 | ;; | |
52da2182 | 40 | ;; (add-hook 'wiki-mode-hook 'iimage-mode) |
1e77b112 EZ |
41 | ;; |
42 | ;; wiki-file: [[foo.png]] | |
43 | ||
44 | ;;; Code: | |
45 | ||
46 | (eval-when-compile | |
47 | (require 'image-file)) | |
48 | ||
6786cab2 LK |
49 | (defgroup iimage nil |
50 | "Support for inline images." | |
51 | :version "22.1" | |
52 | :group 'image) | |
53 | ||
52da2182 GM |
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) | |
1e77b112 EZ |
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 | ||
52da2182 | 66 | (defcustom iimage-mode-image-regex-alist |
1e77b112 EZ |
67 | `((,(concat "\\(`?file://\\|\\[\\[\\|<\\|`\\)?" |
68 | "\\(" iimage-mode-image-filename-regex "\\)" | |
69 | "\\(\\]\\]\\|>\\|'\\)?") . 2)) | |
52da2182 | 70 | "Alist of filename REGEXP vs NUM. |
1e77b112 EZ |
71 | Each element looks like (REGEXP . NUM). |
72 | NUM specifies which parenthesized expression in the regexp. | |
73 | ||
52da2182 | 74 | Examples of image filename patterns to match: |
1e77b112 EZ |
75 | file://foo.png |
76 | `file://foo.png' | |
77 | \\[\\[foo.gif]] | |
78 | <foo.png> | |
52da2182 GM |
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'.") | |
1e77b112 | 89 | |
52da2182 GM |
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)) | |
610101de EZ |
96 | |
97 | ;;;###autoload | |
f2b9ed18 | 98 | (define-obsolete-function-alias 'turn-on-iimage-mode 'iimage-mode "24.1") |
1e77b112 EZ |
99 | |
100 | (defun turn-off-iimage-mode () | |
f2b9ed18 | 101 | "Unconditionally turn off iimage mode." |
1e77b112 EZ |
102 | (interactive) |
103 | (iimage-mode 0)) | |
104 | ||
277e6741 MP |
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 | ||
1e77b112 | 118 | (defun iimage-mode-buffer (arg) |
f2b9ed18 SM |
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) | |
277e6741 MP |
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)))))))))) | |
1e77b112 | 139 | |
610101de | 140 | ;;;###autoload |
1e77b112 EZ |
141 | (define-minor-mode iimage-mode |
142 | "Toggle inline image minor mode." | |
6786cab2 | 143 | :group 'iimage :lighter " iImg" :keymap iimage-mode-map |
1e77b112 EZ |
144 | (iimage-mode-buffer iimage-mode)) |
145 | ||
146 | (provide 'iimage) | |
147 | ||
148 | ;;; iimage.el ends here |