Mercurial > emacs
comparison lisp/image-mode.el @ 88155:d7ddb3e565de
sync with trunk
author | Henrik Enberg <henrik.enberg@telia.com> |
---|---|
date | Mon, 16 Jan 2006 00:03:54 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
88154:8ce476d3ba36 | 88155:d7ddb3e565de |
---|---|
1 ;;; image-mode.el --- support for visiting image files | |
2 ;; | |
3 ;; Copyright (C) 2005 Free Software Foundation, Inc. | |
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 | |
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
23 ;; Boston, MA 02110-1301, USA. | |
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 | |
39 ;;;###autoload (push '("\\.jpe?g\\'" . image-mode) auto-mode-alist) | |
40 ;;;###autoload (push '("\\.png\\'" . image-mode) auto-mode-alist) | |
41 ;;;###autoload (push '("\\.gif\\'" . image-mode) auto-mode-alist) | |
42 ;;;###autoload (push '("\\.tiff?\\'" . image-mode) auto-mode-alist) | |
43 ;;;###autoload (push '("\\.p[bpgn]m\\'" . image-mode) auto-mode-alist) | |
44 ;;;###autoload (push '("\\.x[bp]m\\'" . image-mode-maybe) auto-mode-alist) | |
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) | |
62 (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t) | |
63 (if (not (get-text-property (point-min) 'display)) | |
64 (image-toggle-display) | |
65 ;; Set next vars when image is already displayed but local | |
66 ;; variables were cleared by kill-all-local-variables | |
67 (setq cursor-type nil truncate-lines t)) | |
68 (run-mode-hooks 'image-mode-hook) | |
69 (message "%s" (concat (substitute-command-keys | |
70 "Type \\[image-toggle-display] to view the image as ") | |
71 (if (get-text-property (point-min) 'display) | |
72 "text" "an image") "."))) | |
73 | |
74 ;;;###autoload | |
75 (define-minor-mode image-minor-mode | |
76 "Toggle Image minor mode. | |
77 With arg, turn Image minor mode on if arg is positive, off otherwise. | |
78 See the command `image-mode' for more information on this mode." | |
79 nil " Image" image-mode-map | |
80 :group 'image | |
81 :version "22.1" | |
82 (if (not image-minor-mode) | |
83 (image-toggle-display-text) | |
84 (if (get-text-property (point-min) 'display) | |
85 (setq cursor-type nil truncate-lines t)) | |
86 (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t) | |
87 (message "%s" (concat (substitute-command-keys | |
88 "Type \\[image-toggle-display] to view the image as ") | |
89 (if (get-text-property (point-min) 'display) | |
90 "text" "an image") ".")))) | |
91 | |
92 ;;;###autoload | |
93 (defun image-mode-maybe () | |
94 "Set major or minor mode for image files. | |
95 Set Image major mode only when there are no other major modes | |
96 associated with a filename in `auto-mode-alist'. When an image | |
97 filename matches another major mode in `auto-mode-alist' then | |
98 set that major mode and Image minor mode. | |
99 | |
100 See commands `image-mode' and `image-minor-mode' for more | |
101 information on these modes." | |
102 (interactive) | |
103 (let* ((mode-alist | |
104 (delq nil (mapcar | |
105 (lambda (elt) | |
106 (unless (memq (or (car-safe (cdr elt)) (cdr elt)) | |
107 '(image-mode image-mode-maybe)) | |
108 elt)) | |
109 auto-mode-alist)))) | |
110 (if (assoc-default buffer-file-name mode-alist 'string-match) | |
111 (let ((auto-mode-alist mode-alist)) | |
112 (set-auto-mode) | |
113 (image-minor-mode t)) | |
114 (image-mode)))) | |
115 | |
116 (defun image-toggle-display-text () | |
117 "Showing the text of the image file." | |
118 (if (get-text-property (point-min) 'display) | |
119 (image-toggle-display))) | |
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) | |
128 (buffer-undo-list t) | |
129 (modified (buffer-modified-p))) | |
130 (remove-list-of-text-properties (point-min) (point-max) | |
131 '(display intangible read-nonsticky | |
132 read-only front-sticky)) | |
133 (set-buffer-modified-p modified) | |
134 (kill-local-variable 'cursor-type) | |
135 (kill-local-variable 'truncate-lines) | |
136 (if (called-interactively-p) | |
137 (message "Repeat this command to go back to displaying the image"))) | |
138 ;; Turn the image data into a real image, but only if the whole file | |
139 ;; was inserted | |
140 (let* ((data | |
141 (string-make-unibyte | |
142 (buffer-substring-no-properties (point-min) (point-max)))) | |
143 (image | |
144 (create-image data nil t)) | |
145 (props | |
146 `(display ,image | |
147 intangible ,image | |
148 rear-nonsticky (display intangible) | |
149 ;; This a cheap attempt to make the whole buffer | |
150 ;; read-only when we're visiting the file (as | |
151 ;; opposed to just inserting it). | |
152 read-only t front-sticky (read-only))) | |
153 (inhibit-read-only t) | |
154 (buffer-undo-list t) | |
155 (modified (buffer-modified-p))) | |
156 (add-text-properties (point-min) (point-max) props) | |
157 (set-buffer-modified-p modified) | |
158 ;; Inhibit the cursor when the buffer contains only an image, | |
159 ;; because cursors look very strange on top of images. | |
160 (setq cursor-type nil) | |
161 ;; This just makes the arrow displayed in the right fringe | |
162 ;; area look correct when the image is wider than the window. | |
163 (setq truncate-lines t) | |
164 (if (called-interactively-p) | |
165 (message "Repeat this command to go back to displaying the file as text"))))) | |
166 | |
167 (provide 'image-mode) | |
168 | |
169 ;; arch-tag: b5b2b7e6-26a7-4b79-96e3-1546b5c4c6cb | |
170 ;;; image-mode.el ends here |