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