Commit | Line | Data |
---|---|---|
1e77b112 EZ |
1 | ;;; iimage.el --- Inline image minor mode. |
2 | ||
3 | ;; Copyright (C) 2004 Free Software Foundation | |
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 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
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 | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
25 | ||
26 | ;;; Commentary: | |
27 | ||
610101de EZ |
28 | ;; Iimage is a minor mode that displays images, when image-filename |
29 | ;; exists in the buffer. | |
1e77b112 EZ |
30 | ;; http://www.netlaputa.ne.jp/~kose/Emacs/iimage.html |
31 | ;; | |
32 | ;; Add to your `~/.emacs': | |
610101de EZ |
33 | ;; (autoload 'iimage-mode "iimage" "Support Inline image minor mode." t) |
34 | ;; (autoload 'turn-on-iimage-mode "iimage" "Turn on Inline image minor mode." t) | |
1e77b112 EZ |
35 | ;; |
36 | ;; ** Display images in *Info* buffer. | |
37 | ;; | |
38 | ;; (add-hook 'info-mode-hook 'turn-on-iimage-mode) | |
39 | ;; | |
40 | ;; .texinfo: @file{file://foo.png} | |
41 | ;; .info: `file://foo.png' | |
42 | ;; | |
43 | ;; ** Display images in Wiki buffer. | |
44 | ;; | |
45 | ;; (add-hook 'wiki-mode-hook 'turn-on-iimage-mode) | |
46 | ;; | |
47 | ;; wiki-file: [[foo.png]] | |
48 | ||
49 | ;;; Code: | |
50 | ||
51 | (eval-when-compile | |
52 | (require 'image-file)) | |
53 | ||
610101de | 54 | (defconst iimage-version "1.1") |
1e77b112 EZ |
55 | (defvar iimage-mode nil) |
56 | (defvar iimage-mode-map nil) | |
57 | ||
58 | ;; Set up key map. | |
59 | (unless iimage-mode-map | |
60 | (setq iimage-mode-map (make-sparse-keymap)) | |
61 | (define-key iimage-mode-map "\C-l" 'iimage-recenter)) | |
62 | ||
63 | (defun iimage-recenter (&optional arg) | |
64 | "Re-draw images and recenter." | |
65 | (interactive "P") | |
66 | (iimage-mode-buffer 0) | |
67 | (iimage-mode-buffer 1) | |
68 | (recenter arg)) | |
69 | ||
70 | (defvar iimage-mode-image-filename-regex | |
71 | (concat "[-+./_0-9a-zA-Z]+\\." | |
72 | (regexp-opt (nconc (mapcar #'upcase | |
73 | image-file-name-extensions) | |
74 | image-file-name-extensions) | |
75 | t))) | |
76 | ||
77 | (defvar iimage-mode-image-regex-alist | |
78 | `((,(concat "\\(`?file://\\|\\[\\[\\|<\\|`\\)?" | |
79 | "\\(" iimage-mode-image-filename-regex "\\)" | |
80 | "\\(\\]\\]\\|>\\|'\\)?") . 2)) | |
81 | "*Alist of filename REGEXP vs NUM. | |
82 | Each element looks like (REGEXP . NUM). | |
83 | NUM specifies which parenthesized expression in the regexp. | |
84 | ||
85 | image filename regex exsamples: | |
86 | file://foo.png | |
87 | `file://foo.png' | |
88 | \\[\\[foo.gif]] | |
89 | <foo.png> | |
90 | foo.JPG | |
91 | ") | |
92 | ||
610101de EZ |
93 | (defvar iimage-mode-image-search-path nil |
94 | "*List of directories to search for image files for iimage-mode.") | |
95 | ||
96 | ;;;###autoload | |
1e77b112 EZ |
97 | (defun turn-on-iimage-mode () |
98 | "Unconditionally turn on iimage mode." | |
99 | (interactive) | |
100 | (iimage-mode 1)) | |
101 | ||
102 | (defun turn-off-iimage-mode () | |
103 | "Unconditionally turn off iimage mode." | |
104 | (interactive) | |
105 | (iimage-mode 0)) | |
106 | ||
610101de EZ |
107 | ;; Emacs21.3 or earlier does not heve locate-file. |
108 | (if (fboundp 'locate-file) | |
109 | (defalias 'iimage-locate-file 'locate-file) | |
110 | (defun iimage-locate-file (filename path) | |
111 | (locate-library filename t path))) | |
112 | ||
1e77b112 EZ |
113 | (defun iimage-mode-buffer (arg) |
114 | "Display/Undisplay Images. | |
115 | With numeric ARG, display the images if and only if ARG is positive." | |
116 | (interactive) | |
117 | (let ((ing (if (numberp arg) | |
118 | (> arg 0) | |
119 | iimage-mode)) | |
120 | (modp (buffer-modified-p (current-buffer))) | |
121 | file buffer-read-only) | |
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) | |
126 | (if (and (setq file (match-string (cdr pair))) | |
610101de EZ |
127 | (setq file (iimage-locate-file file |
128 | (cons default-directory | |
129 | iimage-mode-image-search-path)))) | |
1e77b112 EZ |
130 | (if ing |
131 | (add-text-properties (match-beginning 0) (match-end 0) | |
132 | (list 'display (create-image file))) | |
133 | (remove-text-properties (match-beginning 0) (match-end 0) | |
134 | '(display))))))) | |
135 | (set-buffer-modified-p modp))) | |
136 | ||
610101de | 137 | ;;;###autoload |
1e77b112 EZ |
138 | (define-minor-mode iimage-mode |
139 | "Toggle inline image minor mode." | |
140 | nil " iImg" iimage-mode-map | |
141 | (run-hooks 'iimage-mode-hook) | |
142 | (iimage-mode-buffer iimage-mode)) | |
143 | ||
144 | (provide 'iimage) | |
145 | ||
ff298da1 | 146 | ;;; arch-tag: f6f8e29a-08f6-4a12-9496-51e67441ce65 |
1e77b112 | 147 | ;;; iimage.el ends here |