Mercurial > emacs
annotate lisp/image-mode.el @ 70387:a995a8745b40
*** empty log message ***
author | YAMAMOTO Mitsuharu <mituharu@math.s.chiba-u.ac.jp> |
---|---|
date | Fri, 05 May 2006 06:45:26 +0000 |
parents | 3e313b3afda5 |
children | 488f02720eed |
rev | line source |
---|---|
60694 | 1 ;;; image-mode.el --- support for visiting image files |
2 ;; | |
68651
3bd95f4f2941
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
68495
diff
changeset
|
3 ;; Copyright (C) 2005, 2006 Free Software Foundation, Inc. |
60694 | 4 ;; |
5 ;; Author: Richard Stallman <rms@gnu.org> | |
6 ;; Keywords: multimedia | |
7 | |
8 ;; This file is part of GNU Emacs. | |
9 | |
10 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 ;; it under the terms of the GNU General Public License as published by | |
12 ;; the Free Software Foundation; either version 2, or (at your option) | |
13 ;; any later version. | |
14 | |
15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
64091 | 22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
23 ;; Boston, MA 02110-1301, USA. | |
60694 | 24 |
25 ;;; Commentary: | |
26 | |
27 ;; Defines a major mode for visiting image files | |
28 ;; that allows conversion between viewing the text of the file | |
29 ;; and viewing the file as an image. Viewing the image | |
30 ;; works by putting a `display' text-property on the | |
31 ;; image data, with the image-data still present underneath; if the | |
32 ;; resulting buffer file is saved to another name it will correctly save | |
33 ;; the image data to the new file. | |
34 | |
35 ;;; Code: | |
36 | |
37 (require 'image) | |
38 | |
60937
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
39 ;;;###autoload (push '("\\.jpe?g\\'" . image-mode) auto-mode-alist) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
40 ;;;###autoload (push '("\\.png\\'" . image-mode) auto-mode-alist) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
41 ;;;###autoload (push '("\\.gif\\'" . image-mode) auto-mode-alist) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
42 ;;;###autoload (push '("\\.tiff?\\'" . image-mode) auto-mode-alist) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
43 ;;;###autoload (push '("\\.p[bpgn]m\\'" . image-mode) auto-mode-alist) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
44 ;;;###autoload (push '("\\.x[bp]m\\'" . image-mode-maybe) auto-mode-alist) |
60694 | 45 |
46 (defvar image-mode-map | |
47 (let ((map (make-sparse-keymap))) | |
48 (define-key map "\C-c\C-c" 'image-toggle-display) | |
49 map) | |
50 "Major mode keymap for Image mode.") | |
51 | |
52 ;;;###autoload | |
53 (defun image-mode () | |
54 "Major mode for image files. | |
55 You can use \\<image-mode-map>\\[image-toggle-display] | |
56 to toggle between display as an image and display as text." | |
57 (interactive) | |
58 (kill-all-local-variables) | |
59 (setq mode-name "Image") | |
60 (setq major-mode 'image-mode) | |
61 (use-local-map image-mode-map) | |
60937
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
62 (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
63 (if (not (get-text-property (point-min) 'display)) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
64 (image-toggle-display) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
65 ;; Set next vars when image is already displayed but local |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
66 ;; variables were cleared by kill-all-local-variables |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
67 (setq cursor-type nil truncate-lines t)) |
60694 | 68 (run-mode-hooks 'image-mode-hook) |
65582
4d1085b02d64
Message format spec fixes (1)
Deepak Goel <deego@gnufans.org>
parents:
64091
diff
changeset
|
69 (message "%s" (concat (substitute-command-keys |
60937
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
70 "Type \\[image-toggle-display] to view the image as ") |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
71 (if (get-text-property (point-min) 'display) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
72 "text" "an image") "."))) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
73 |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
74 ;;;###autoload |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
75 (define-minor-mode image-minor-mode |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
76 "Toggle Image minor mode. |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
77 With arg, turn Image minor mode on if arg is positive, off otherwise. |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
78 See the command `image-mode' for more information on this mode." |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
79 nil " Image" image-mode-map |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
80 :group 'image |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
81 :version "22.1" |
60943
258772850c9a
(image-minor-mode): Set `cursor-type' and `truncate-lines' if the
Juri Linkov <juri@jurta.org>
parents:
60937
diff
changeset
|
82 (if (not image-minor-mode) |
258772850c9a
(image-minor-mode): Set `cursor-type' and `truncate-lines' if the
Juri Linkov <juri@jurta.org>
parents:
60937
diff
changeset
|
83 (image-toggle-display-text) |
258772850c9a
(image-minor-mode): Set `cursor-type' and `truncate-lines' if the
Juri Linkov <juri@jurta.org>
parents:
60937
diff
changeset
|
84 (if (get-text-property (point-min) 'display) |
258772850c9a
(image-minor-mode): Set `cursor-type' and `truncate-lines' if the
Juri Linkov <juri@jurta.org>
parents:
60937
diff
changeset
|
85 (setq cursor-type nil truncate-lines t)) |
258772850c9a
(image-minor-mode): Set `cursor-type' and `truncate-lines' if the
Juri Linkov <juri@jurta.org>
parents:
60937
diff
changeset
|
86 (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t) |
65582
4d1085b02d64
Message format spec fixes (1)
Deepak Goel <deego@gnufans.org>
parents:
64091
diff
changeset
|
87 (message "%s" (concat (substitute-command-keys |
60943
258772850c9a
(image-minor-mode): Set `cursor-type' and `truncate-lines' if the
Juri Linkov <juri@jurta.org>
parents:
60937
diff
changeset
|
88 "Type \\[image-toggle-display] to view the image as ") |
258772850c9a
(image-minor-mode): Set `cursor-type' and `truncate-lines' if the
Juri Linkov <juri@jurta.org>
parents:
60937
diff
changeset
|
89 (if (get-text-property (point-min) 'display) |
258772850c9a
(image-minor-mode): Set `cursor-type' and `truncate-lines' if the
Juri Linkov <juri@jurta.org>
parents:
60937
diff
changeset
|
90 "text" "an image") ".")))) |
60937
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
91 |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
92 ;;;###autoload |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
93 (defun image-mode-maybe () |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
94 "Set major or minor mode for image files. |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
95 Set Image major mode only when there are no other major modes |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
96 associated with a filename in `auto-mode-alist'. When an image |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
97 filename matches another major mode in `auto-mode-alist' then |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
98 set that major mode and Image minor mode. |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
99 |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
100 See commands `image-mode' and `image-minor-mode' for more |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
101 information on these modes." |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
102 (interactive) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
103 (let* ((mode-alist |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
104 (delq nil (mapcar |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
105 (lambda (elt) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
106 (unless (memq (or (car-safe (cdr elt)) (cdr elt)) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
107 '(image-mode image-mode-maybe)) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
108 elt)) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
109 auto-mode-alist)))) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
110 (if (assoc-default buffer-file-name mode-alist 'string-match) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
111 (let ((auto-mode-alist mode-alist)) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
112 (set-auto-mode) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
113 (image-minor-mode t)) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
114 (image-mode)))) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
115 |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
116 (defun image-toggle-display-text () |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
117 "Showing the text of the image file." |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
118 (if (get-text-property (point-min) 'display) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
119 (image-toggle-display))) |
60694 | 120 |
121 (defun image-toggle-display () | |
122 "Start or stop displaying an image file as the actual image. | |
123 This command toggles between showing the text of the image file | |
124 and showing the image as an image." | |
125 (interactive) | |
126 (if (get-text-property (point-min) 'display) | |
127 (let ((inhibit-read-only t) | |
60780 | 128 (buffer-undo-list t) |
129 (modified (buffer-modified-p))) | |
60694 | 130 (remove-list-of-text-properties (point-min) (point-max) |
131 '(display intangible read-nonsticky | |
132 read-only front-sticky)) | |
60780 | 133 (set-buffer-modified-p modified) |
60694 | 134 (kill-local-variable 'cursor-type) |
135 (kill-local-variable 'truncate-lines) | |
60937
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
136 (if (called-interactively-p) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
137 (message "Repeat this command to go back to displaying the image"))) |
60694 | 138 ;; Turn the image data into a real image, but only if the whole file |
139 ;; was inserted | |
68495
1239e70c4e3c
* image-mode.el (image-toggle-display): Use file name if possible,
Chong Yidong <cyd@stupidchicken.com>
parents:
65582
diff
changeset
|
140 (let* ((image |
1239e70c4e3c
* image-mode.el (image-toggle-display): Use file name if possible,
Chong Yidong <cyd@stupidchicken.com>
parents:
65582
diff
changeset
|
141 (if (and (buffer-file-name) |
70097
3e313b3afda5
(image-toggle-display): Handle tar and arc subfiles.
Richard M. Stallman <rms@gnu.org>
parents:
68749
diff
changeset
|
142 (not (buffer-modified-p)) |
3e313b3afda5
(image-toggle-display): Handle tar and arc subfiles.
Richard M. Stallman <rms@gnu.org>
parents:
68749
diff
changeset
|
143 (not (and (boundp 'archive-superior-buffer) |
3e313b3afda5
(image-toggle-display): Handle tar and arc subfiles.
Richard M. Stallman <rms@gnu.org>
parents:
68749
diff
changeset
|
144 archive-superior-buffer)) |
3e313b3afda5
(image-toggle-display): Handle tar and arc subfiles.
Richard M. Stallman <rms@gnu.org>
parents:
68749
diff
changeset
|
145 (not (and (boundp 'tar-superior-buffer) |
3e313b3afda5
(image-toggle-display): Handle tar and arc subfiles.
Richard M. Stallman <rms@gnu.org>
parents:
68749
diff
changeset
|
146 tar-superior-buffer))) |
68749
c0b14a7a6a49
* image-mode.el (image-toggle-display): Clear image cache if using
Chong Yidong <cyd@stupidchicken.com>
parents:
68651
diff
changeset
|
147 (progn (clear-image-cache) |
c0b14a7a6a49
* image-mode.el (image-toggle-display): Clear image cache if using
Chong Yidong <cyd@stupidchicken.com>
parents:
68651
diff
changeset
|
148 (create-image (buffer-file-name))) |
68495
1239e70c4e3c
* image-mode.el (image-toggle-display): Use file name if possible,
Chong Yidong <cyd@stupidchicken.com>
parents:
65582
diff
changeset
|
149 (create-image |
1239e70c4e3c
* image-mode.el (image-toggle-display): Use file name if possible,
Chong Yidong <cyd@stupidchicken.com>
parents:
65582
diff
changeset
|
150 (string-make-unibyte |
1239e70c4e3c
* image-mode.el (image-toggle-display): Use file name if possible,
Chong Yidong <cyd@stupidchicken.com>
parents:
65582
diff
changeset
|
151 (buffer-substring-no-properties (point-min) (point-max))) |
1239e70c4e3c
* image-mode.el (image-toggle-display): Use file name if possible,
Chong Yidong <cyd@stupidchicken.com>
parents:
65582
diff
changeset
|
152 nil t))) |
60694 | 153 (props |
154 `(display ,image | |
155 intangible ,image | |
156 rear-nonsticky (display intangible) | |
157 ;; This a cheap attempt to make the whole buffer | |
158 ;; read-only when we're visiting the file (as | |
159 ;; opposed to just inserting it). | |
160 read-only t front-sticky (read-only))) | |
60937
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
161 (inhibit-read-only t) |
60780 | 162 (buffer-undo-list t) |
163 (modified (buffer-modified-p))) | |
60694 | 164 (add-text-properties (point-min) (point-max) props) |
60780 | 165 (set-buffer-modified-p modified) |
60694 | 166 ;; Inhibit the cursor when the buffer contains only an image, |
167 ;; because cursors look very strange on top of images. | |
168 (setq cursor-type nil) | |
169 ;; This just makes the arrow displayed in the right fringe | |
170 ;; area look correct when the image is wider than the window. | |
171 (setq truncate-lines t) | |
60937
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
172 (if (called-interactively-p) |
ddcbccff39ce
Optimize image filename extension regexps in
Juri Linkov <juri@jurta.org>
parents:
60780
diff
changeset
|
173 (message "Repeat this command to go back to displaying the file as text"))))) |
60694 | 174 |
175 (provide 'image-mode) | |
176 | |
60697
d9c9ad74e719
Changes from arch/CVS synchronization
Miles Bader <miles@gnu.org>
parents:
60694
diff
changeset
|
177 ;; arch-tag: b5b2b7e6-26a7-4b79-96e3-1546b5c4c6cb |
60694 | 178 ;;; image-mode.el ends here |