Mercurial > emacs
comparison lisp/iimage.el @ 108488:925f002ec4ab
* iimage.el: Misc cleanup.
(iimage-mode-map): Move initialization into declaration.
(iimage-mode-buffer): Use with-silent-modifications.
Simplify calling convention. Adjust callers.
(iimage-mode): Don't run hook redundantly.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Tue, 11 May 2010 14:51:27 -0400 |
parents | 1d1d5d9bd884 |
children | bbdc76e1b06c |
comparison
equal
deleted
inserted
replaced
108487:57fe80b13f7a | 108488:925f002ec4ab |
---|---|
53 "Support for inline images." | 53 "Support for inline images." |
54 :version "22.1" | 54 :version "22.1" |
55 :group 'image) | 55 :group 'image) |
56 | 56 |
57 (defconst iimage-version "1.1") | 57 (defconst iimage-version "1.1") |
58 (defvar iimage-mode nil) | |
59 (defvar iimage-mode-map nil) | |
60 | 58 |
61 ;; Set up key map. | 59 (defvar iimage-mode-map |
62 (unless iimage-mode-map | 60 (let ((map (make-sparse-keymap))) |
63 (setq iimage-mode-map (make-sparse-keymap)) | 61 (define-key map "\C-l" 'iimage-recenter) |
64 (define-key iimage-mode-map "\C-l" 'iimage-recenter)) | 62 map)) |
65 | 63 |
66 (defun iimage-recenter (&optional arg) | 64 (defun iimage-recenter (&optional arg) |
67 "Re-draw images and recenter." | 65 "Re-draw images and recenter." |
68 (interactive "P") | 66 (interactive "P") |
69 (iimage-mode-buffer 0) | 67 (iimage-mode-buffer nil) |
70 (iimage-mode-buffer 1) | 68 (iimage-mode-buffer t) |
71 (recenter arg)) | 69 (recenter arg)) |
72 | 70 |
73 (defvar iimage-mode-image-filename-regex | 71 (defvar iimage-mode-image-filename-regex |
74 (concat "[-+./_0-9a-zA-Z]+\\." | 72 (concat "[-+./_0-9a-zA-Z]+\\." |
75 (regexp-opt (nconc (mapcar #'upcase | 73 (regexp-opt (nconc (mapcar #'upcase |
79 | 77 |
80 (defvar iimage-mode-image-regex-alist | 78 (defvar iimage-mode-image-regex-alist |
81 `((,(concat "\\(`?file://\\|\\[\\[\\|<\\|`\\)?" | 79 `((,(concat "\\(`?file://\\|\\[\\[\\|<\\|`\\)?" |
82 "\\(" iimage-mode-image-filename-regex "\\)" | 80 "\\(" iimage-mode-image-filename-regex "\\)" |
83 "\\(\\]\\]\\|>\\|'\\)?") . 2)) | 81 "\\(\\]\\]\\|>\\|'\\)?") . 2)) |
84 "*Alist of filename REGEXP vs NUM. | 82 "*Alist of filename REGEXP vs NUM. |
85 Each element looks like (REGEXP . NUM). | 83 Each element looks like (REGEXP . NUM). |
86 NUM specifies which parenthesized expression in the regexp. | 84 NUM specifies which parenthesized expression in the regexp. |
87 | 85 |
88 Examples of image filename regexps: | 86 Examples of image filename regexps: |
89 file://foo.png | 87 file://foo.png |
90 `file://foo.png' | 88 `file://foo.png' |
91 \\[\\[foo.gif]] | 89 \\[\\[foo.gif]] |
92 <foo.png> | 90 <foo.png> |
93 foo.JPG | 91 foo.JPG") |
94 ") | |
95 | 92 |
96 (defvar iimage-mode-image-search-path nil | 93 (defvar iimage-mode-image-search-path nil |
97 "*List of directories to search for image files for iimage-mode.") | 94 "*List of directories to search for image files for `iimage-mode'.") |
98 | 95 |
99 ;;;###autoload | 96 ;;;###autoload |
100 (defun turn-on-iimage-mode () | 97 (define-obsolete-function-alias 'turn-on-iimage-mode 'iimage-mode "24.1") |
101 "Unconditionally turn on iimage mode." | |
102 (interactive) | |
103 (iimage-mode 1)) | |
104 | 98 |
105 (defun turn-off-iimage-mode () | 99 (defun turn-off-iimage-mode () |
106 "Unconditionally turn off iimage mode." | 100 "Unconditionally turn off iimage mode." |
107 (interactive) | 101 (interactive) |
108 (iimage-mode 0)) | 102 (iimage-mode 0)) |
109 | 103 |
110 (defalias 'iimage-locate-file 'locate-file) | |
111 | |
112 (defun iimage-mode-buffer (arg) | 104 (defun iimage-mode-buffer (arg) |
113 "Display/undisplay images. | 105 "Display images if ARG is non-nil, undisplay them otherwise." |
114 With numeric ARG, display the images if and only if ARG is positive." | 106 (let ((image-path (cons default-directory iimage-mode-image-search-path)) |
115 (interactive) | 107 file) |
116 (let ((ing (if (numberp arg) | 108 (with-silent-modifications |
117 (> arg 0) | 109 (save-excursion |
118 iimage-mode)) | 110 (goto-char (point-min)) |
119 (modp (buffer-modified-p (current-buffer))) | 111 (dolist (pair iimage-mode-image-regex-alist) |
120 file buffer-read-only) | 112 (while (re-search-forward (car pair) nil t) |
121 (save-excursion | 113 (if (and (setq file (match-string (cdr pair))) |
122 (goto-char (point-min)) | 114 (setq file (locate-file file image-path))) |
123 (dolist (pair iimage-mode-image-regex-alist) | 115 ;; FIXME: we don't mark our images, so we can't reliably |
124 (while (re-search-forward (car pair) nil t) | 116 ;; remove them either (we may leave some of ours, and we |
125 (if (and (setq file (match-string (cdr pair))) | 117 ;; may remove other packages's display properties). |
126 (setq file (iimage-locate-file file | 118 (if arg |
127 (cons default-directory | 119 (add-text-properties (match-beginning 0) (match-end 0) |
128 iimage-mode-image-search-path)))) | 120 (list 'display (create-image file))) |
129 (if ing | 121 (remove-text-properties (match-beginning 0) (match-end 0) |
130 (add-text-properties (match-beginning 0) (match-end 0) | 122 '(display)))))))))) |
131 (list 'display (create-image file))) | |
132 (remove-text-properties (match-beginning 0) (match-end 0) | |
133 '(display))))))) | |
134 (set-buffer-modified-p modp))) | |
135 | 123 |
136 ;;;###autoload | 124 ;;;###autoload |
137 (define-minor-mode iimage-mode | 125 (define-minor-mode iimage-mode |
138 "Toggle inline image minor mode." | 126 "Toggle inline image minor mode." |
139 :group 'iimage :lighter " iImg" :keymap iimage-mode-map | 127 :group 'iimage :lighter " iImg" :keymap iimage-mode-map |
140 (run-hooks 'iimage-mode-hook) | |
141 (iimage-mode-buffer iimage-mode)) | 128 (iimage-mode-buffer iimage-mode)) |
142 | 129 |
143 (provide 'iimage) | 130 (provide 'iimage) |
144 | 131 |
145 ;; arch-tag: f6f8e29a-08f6-4a12-9496-51e67441ce65 | 132 ;; arch-tag: f6f8e29a-08f6-4a12-9496-51e67441ce65 |