* ediff-init.el (ediff-get-next-window): bug fix.
[emacs.git] / lisp / descr-text.el
blob5ef3a2990d87bbd7217a3a3e347574090302c063
1 ;;; descr-text.el --- describe text mode
3 ;; Copyright (c) 1994, 1995, 1996, 2001, 2002 Free Software Foundation, Inc.
5 ;; Author: Boris Goldowsky <boris@gnu.org>
6 ;; Keywords: faces
8 ;; This file is part of GNU Emacs.
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.
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.
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., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;;; Describe-Text Mode.
29 ;;; Code:
31 (defun describe-text-done ()
32 "Delete the current window or bury the current buffer."
33 (interactive)
34 (if (> (count-windows) 1)
35 (delete-window)
36 (bury-buffer)))
38 (defvar describe-text-mode-map
39 (let ((map (make-sparse-keymap)))
40 (set-keymap-parent map widget-keymap)
41 map)
42 "Keymap for `describe-text-mode'.")
44 (defcustom describe-text-mode-hook nil
45 "List of hook functions ran by `describe-text-mode'."
46 :type 'hook)
48 (defun describe-text-mode ()
49 "Major mode for buffers created by `describe-char'.
51 \\{describe-text-mode-map}
52 Entry to this mode calls the value of `describe-text-mode-hook'
53 if that value is non-nil."
54 (kill-all-local-variables)
55 (setq major-mode 'describe-text-mode
56 mode-name "Describe-Text")
57 (use-local-map describe-text-mode-map)
58 (widget-setup)
59 (add-hook 'change-major-mode-hook 'font-lock-defontify nil t)
60 (run-hooks 'describe-text-mode-hook))
62 ;;; Describe-Text Utilities.
64 (defun describe-text-widget (widget)
65 "Insert text to describe WIDGET in the current buffer."
66 (widget-create 'link
67 :notify `(lambda (&rest ignore)
68 (widget-browse ',widget))
69 (format "%S" (if (symbolp widget)
70 widget
71 (car widget))))
72 (widget-insert " ")
73 (widget-create 'info-link :tag "widget" "(widget)Top"))
75 (defun describe-text-sexp (sexp)
76 "Insert a short description of SEXP in the current buffer."
77 (let ((pp (condition-case signal
78 (pp-to-string sexp)
79 (error (prin1-to-string signal)))))
80 (when (string-match "\n\\'" pp)
81 (setq pp (substring pp 0 (1- (length pp)))))
82 (if (cond ((string-match "\n" pp)
83 nil)
84 ((> (length pp) (- (window-width) (current-column)))
85 nil)
86 (t t))
87 (widget-insert pp)
88 (widget-create 'push-button
89 :tag "show"
90 :action (lambda (widget &optional event)
91 (with-output-to-temp-buffer
92 "*Pp Eval Output*"
93 (princ (widget-get widget :value))))
94 pp))))
96 (defun describe-property-list (properties)
97 "Insert a description of PROPERTIES in the current buffer.
98 PROPERTIES should be a list of overlay or text properties.
99 The `category' property is made into a widget button that call
100 `describe-text-category' when pushed."
101 ;; Sort the properties by the size of their value.
102 (dolist (elt (sort (let ((ret nil)
103 (key nil)
104 (val nil)
105 (len nil))
106 (while properties
107 (setq key (pop properties)
108 val (pop properties)
109 len 0)
110 (unless (or (eq key 'category)
111 (widgetp val))
112 (setq val (pp-to-string val)
113 len (length val)))
114 (push (list key val len) ret))
115 ret)
116 (lambda (a b)
117 (< (nth 2 a)
118 (nth 2 b)))))
119 (let ((key (nth 0 elt))
120 (value (nth 1 elt)))
121 (widget-insert (propertize (format " %-20s " key)
122 'font-lock-face 'italic))
123 (cond ((eq key 'category)
124 (widget-create 'link
125 :notify `(lambda (&rest ignore)
126 (describe-text-category ',value))
127 (format "%S" value)))
128 ((widgetp value)
129 (describe-text-widget value))
131 (widget-insert value))))
132 (widget-insert "\n")))
134 ;;; Describe-Text Commands.
136 (defun describe-text-category (category)
137 "Describe a text property category."
138 (interactive "S")
139 (when (get-buffer "*Text Category*")
140 (kill-buffer "*Text Category*"))
141 (save-excursion
142 (with-output-to-temp-buffer "*Text Category*"
143 (set-buffer "*Text Category*")
144 (widget-insert "Category " (format "%S" category) ":\n\n")
145 (describe-property-list (symbol-plist category))
146 (describe-text-mode)
147 (goto-char (point-min)))))
149 ;;;###autoload
150 (defun describe-text-properties (pos &optional output-buffer)
151 "Describe widgets, buttons, overlays and text properties at POS.
152 Interactively, describe them for the character after point.
153 If optional second argument OUTPUT-BUFFER is non-nil,
154 insert the output into that buffer, and don't initialize or clear it
155 otherwise."
156 (interactive "d")
157 (when (eq (current-buffer) (get-buffer "*Text Description*"))
158 (error "Can't do self inspection"))
159 (if (>= pos (point-max))
160 (error "No character follows specified position"))
161 (if output-buffer
162 (describe-text-properties-1 pos output-buffer)
163 (if (not (or (text-properties-at pos) (overlays-at pos)))
164 (message "This is plain text.")
165 (let ((buffer (current-buffer)))
166 (save-excursion
167 (with-output-to-temp-buffer "*Text Description*"
168 (set-buffer "*Text Description*")
169 (setq output-buffer (current-buffer))
170 (widget-insert "Text content at position " (format "%d" pos) ":\n\n")
171 (with-current-buffer buffer
172 (describe-text-properties-1 pos output-buffer))
173 (describe-text-mode)
174 (goto-char (point-min))))))))
176 (defun describe-text-properties-1 (pos output-buffer)
177 (let* ((properties (text-properties-at pos))
178 (overlays (overlays-at pos))
179 overlay
180 (wid-field (get-char-property pos 'field))
181 (wid-button (get-char-property pos 'button))
182 (wid-doc (get-char-property pos 'widget-doc))
183 ;; If button.el is not loaded, we have no buttons in the text.
184 (button (and (fboundp 'button-at) (button-at pos)))
185 (button-type (and button (button-type button)))
186 (button-label (and button (button-label button)))
187 (widget (or wid-field wid-button wid-doc)))
188 (with-current-buffer output-buffer
189 ;; Widgets
190 (when (widgetp widget)
191 (newline)
192 (widget-insert (cond (wid-field "This is an editable text area")
193 (wid-button "This is an active area")
194 (wid-doc "This is documentation text")))
195 (widget-insert " of a ")
196 (describe-text-widget widget)
197 (widget-insert ".\n\n"))
198 ;; Buttons
199 (when (and button (not (widgetp wid-button)))
200 (newline)
201 (widget-insert "Here is a " (format "%S" button-type)
202 " button labeled `" button-label "'.\n\n"))
203 ;; Overlays
204 (when overlays
205 (newline)
206 (if (eq (length overlays) 1)
207 (widget-insert "There is an overlay here:\n")
208 (widget-insert "There are " (format "%d" (length overlays))
209 " overlays here:\n"))
210 (dolist (overlay overlays)
211 (widget-insert " From " (format "%d" (overlay-start overlay))
212 " to " (format "%d" (overlay-end overlay)) "\n")
213 (describe-property-list (overlay-properties overlay)))
214 (widget-insert "\n"))
215 ;; Text properties
216 (when properties
217 (newline)
218 (widget-insert "There are text properties here:\n")
219 (describe-property-list properties)))))
221 ;;;###autoload
222 (defun describe-char (pos)
223 "Describe the character after POS (interactively, the character after point).
224 The information includes character code, charset and code points in it,
225 syntax, category, how the character is encoded in a file,
226 character composition information (if relevant),
227 as well as widgets, buttons, overlays, and text properties."
228 (interactive "d")
229 (when (eq (current-buffer) (get-buffer "*Text Description*"))
230 (error "Can't do self inspection"))
231 (if (>= pos (point-max))
232 (error "No character follows specified position"))
233 (let* ((char (char-after pos))
234 (charset (char-charset char))
235 (buffer (current-buffer))
236 (composition (find-composition (point) nil nil t))
237 (composed (if composition (buffer-substring (car composition)
238 (nth 1 composition))))
239 (multibyte-p enable-multibyte-characters)
240 item-list max-width)
241 (if (eq charset 'unknown)
242 (setq item-list
243 `(("character"
244 ,(format "%s (0%o, %d, 0x%x) -- invalid character code"
245 (if (< char 256)
246 (single-key-description char)
247 (char-to-string char))
248 char char char))))
249 (setq item-list
250 `(("character"
251 ,(format "%s (0%o, %d, 0x%x)" (if (< char 256)
252 (single-key-description char)
253 (char-to-string char))
254 char char char))
255 ("charset"
256 ,(symbol-name charset)
257 ,(format "(%s)" (charset-description charset)))
258 ("code point"
259 ,(let ((split (split-char char)))
260 (if (= (charset-dimension charset) 1)
261 (format "%d" (nth 1 split))
262 (format "%d %d" (nth 1 split) (nth 2 split)))))
263 ("syntax"
264 ,(let ((syntax (get-char-property (point) 'syntax-table)))
265 (with-temp-buffer
266 (internal-describe-syntax-value
267 (if (consp syntax) syntax
268 (aref (or syntax (syntax-table)) char)))
269 (buffer-string))))
270 ("category"
271 ,@(let ((category-set (char-category-set char)))
272 (if (not category-set)
273 '("-- none --")
274 (mapcar #'(lambda (x) (format "%c:%s "
275 x (category-docstring x)))
276 (category-set-mnemonics category-set)))))
277 ,@(let ((props (aref char-code-property-table char))
279 (when props
280 (while props
281 (push (format "%s:" (pop props)) ps)
282 (push (format "%s;" (pop props)) ps))
283 (list (cons "Properties" (nreverse ps)))))
284 ("buffer code"
285 ,(encoded-string-description
286 (string-as-unibyte (char-to-string char)) nil))
287 ("file code"
288 ,@(let* ((coding buffer-file-coding-system)
289 (encoded (encode-coding-char char coding)))
290 (if encoded
291 (list (encoded-string-description encoded coding)
292 (format "(encoded by coding system %S)" coding))
293 (list "not encodable by coding system"
294 (symbol-name coding)))))
295 ,@(if (or (memq 'mule-utf-8
296 (find-coding-systems-region (point) (1+ (point))))
297 (get-char-property (point) 'untranslated-utf-8))
298 (let ((uc (or (get-char-property (point)
299 'untranslated-utf-8)
300 (encode-char (char-after) 'ucs))))
301 (if uc
302 (list (list "Unicode"
303 (format "%04X" uc))))))
304 ,(if (display-graphic-p (selected-frame))
305 (list "font" (or (internal-char-font (point))
306 "-- none --"))
307 (list "terminal code"
308 (let* ((coding (terminal-coding-system))
309 (encoded (encode-coding-char char coding)))
310 (if encoded
311 (encoded-string-description encoded coding)
312 "not encodable")))))))
313 (setq max-width (apply #'max (mapcar #'(lambda (x) (length (car x)))
314 item-list)))
315 (when (get-buffer "*Help*")
316 (kill-buffer "*Help*"))
317 (with-output-to-temp-buffer "*Help*"
318 (save-excursion
319 (set-buffer standard-output)
320 (set-buffer-multibyte multibyte-p)
321 (let ((formatter (format "%%%ds:" max-width)))
322 (dolist (elt item-list)
323 (insert (format formatter (car elt)))
324 (dolist (clm (cdr elt))
325 (when (>= (+ (current-column)
326 (or (string-match "\n" clm)
327 (string-width clm)) 1)
328 (frame-width))
329 (insert "\n")
330 (indent-to (1+ max-width)))
331 (insert " " clm))
332 (insert "\n")))
333 (when composition
334 (insert "\nComposed with the following character(s) "
335 (mapconcat (lambda (x) (format "`%c'" x))
336 (substring composed 1)
337 ", ")
338 " to form `" composed "'")
339 (if (nth 3 composition)
340 (insert ".\n")
341 (insert "\nby the rule ("
342 (mapconcat (lambda (x)
343 (format (if (consp x) "%S" "?%c") x))
344 (nth 2 composition)
345 " ")
346 ").\n"
347 "See the variable `reference-point-alist' for "
348 "the meaning of the rule.\n")))
350 (let ((output (current-buffer)))
351 (with-current-buffer buffer
352 (describe-text-properties pos output))
353 (describe-text-mode))))))
355 (provide 'descr-text)
357 ;;; descr-text.el ends here