(Expanding Abbrevs): Fix previous fix.
[emacs.git] / lisp / iimage.el
blob9b183ebb01d2006ebd8dfcf3d308deb569bb6e67
1 ;;; iimage.el --- Inline image minor mode.
3 ;; Copyright (C) 2004 Free Software Foundation
5 ;; Author: KOSEKI Yoshinori <kose@meadowy.org>
6 ;; Maintainer: KOSEKI Yoshinori <kose@meadowy.org>
7 ;; Keywords: multimedia
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
28 ;; Iimage is a minor mode that displays images, when image-filename
29 ;; exists in the buffer.
30 ;; http://www.netlaputa.ne.jp/~kose/Emacs/iimage.html
32 ;; Add to your `~/.emacs':
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)
36 ;; ** Display images in *Info* buffer.
38 ;; (add-hook 'info-mode-hook 'turn-on-iimage-mode)
40 ;; .texinfo: @file{file://foo.png}
41 ;; .info: `file://foo.png'
43 ;; ** Display images in Wiki buffer.
45 ;; (add-hook 'wiki-mode-hook 'turn-on-iimage-mode)
47 ;; wiki-file: [[foo.png]]
49 ;;; Code:
51 (eval-when-compile
52 (require 'image-file))
54 (defconst iimage-version "1.1")
55 (defvar iimage-mode nil)
56 (defvar iimage-mode-map nil)
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))
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))
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)))
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.
85 image filename regex exsamples:
86 file://foo.png
87 `file://foo.png'
88 \\[\\[foo.gif]]
89 <foo.png>
90 foo.JPG
93 (defvar iimage-mode-image-search-path nil
94 "*List of directories to search for image files for iimage-mode.")
96 ;;;###autoload
97 (defun turn-on-iimage-mode ()
98 "Unconditionally turn on iimage mode."
99 (interactive)
100 (iimage-mode 1))
102 (defun turn-off-iimage-mode ()
103 "Unconditionally turn off iimage mode."
104 (interactive)
105 (iimage-mode 0))
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)))
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)))
127 (setq file (iimage-locate-file file
128 (cons default-directory
129 iimage-mode-image-search-path))))
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)))
137 ;;;###autoload
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))
144 (provide 'iimage)
146 ;;; arch-tag: f6f8e29a-08f6-4a12-9496-51e67441ce65
147 ;;; iimage.el ends here