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>
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)
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.
27 ;;; Describe-Text Mode.
31 (defun describe-text-done ()
32 "Delete the current window or bury the current buffer."
34 (if (> (count-windows) 1)
38 (defvar describe-text-mode-map
39 (let ((map (make-sparse-keymap)))
40 (set-keymap-parent map widget-keymap
)
42 "Keymap for `describe-text-mode'.")
44 (defcustom describe-text-mode-hook nil
45 "List of hook functions ran by `describe-text-mode'."
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
)
59 (run-hooks 'describe-text-mode-hook
))
61 ;;; Describe-Text Utilities.
63 (defun describe-text-widget (widget)
64 "Insert text to describe WIDGET in the current buffer."
66 :notify
`(lambda (&rest ignore
)
67 (widget-browse ',widget
))
68 (format "%S" (if (symbolp widget
)
72 (widget-create 'info-link
:tag
"widget" "(widget)Top"))
74 (defun describe-text-sexp (sexp)
75 "Insert a short description of SEXP in the current buffer."
76 (let ((pp (condition-case signal
78 (error (prin1-to-string signal
)))))
79 (when (string-match "\n\\'" pp
)
80 (setq pp
(substring pp
0 (1- (length pp
)))))
81 (if (cond ((string-match "\n" pp
)
83 ((> (length pp
) (- (window-width) (current-column)))
87 (widget-create 'push-button
89 :action
(lambda (widget &optional event
)
90 (with-output-to-temp-buffer
92 (princ (widget-get widget
:value
))))
95 (defun describe-property-list (properties)
96 "Insert a description of PROPERTIES in the current buffer.
97 PROPERTIES should be a list of overlay or text properties.
98 The `category' property is made into a widget button that call
99 `describe-text-category' when pushed."
100 ;; Sort the properties by the size of their value.
101 (dolist (elt (sort (let ((ret nil
)
106 (setq key
(pop properties
)
109 (unless (or (eq key
'category
)
111 (setq val
(pp-to-string val
)
113 (push (list key val len
) ret
))
118 (let ((key (nth 0 elt
))
120 (widget-insert (propertize (format " %-20s" key
)
121 'font-lock-face
'italic
))
122 (cond ((eq key
'category
)
124 :notify
`(lambda (&rest ignore
)
125 (describe-text-category ',value
))
126 (format "%S" value
)))
128 (describe-text-widget value
))
130 (widget-insert value
))))
131 (widget-insert "\n")))
133 ;;; Describe-Text Commands.
135 (defun describe-text-category (category)
136 "Describe a text property category."
138 (when (get-buffer "*Text Category*")
139 (kill-buffer "*Text Category*"))
141 (with-output-to-temp-buffer "*Text Category*"
142 (set-buffer "*Text Category*")
143 (widget-insert "Category " (format "%S" category
) ":\n\n")
144 (describe-property-list (symbol-plist category
))
146 (goto-char (point-min)))))
149 (defun describe-text-properties (pos &optional output-buffer
)
150 "Describe widgets, buttons, overlays and text properties at POS.
151 Interactively, describe them for the character after point.
152 If optional second argument OUTPUT-BUFFER is non-nil,
153 insert the output into that buffer, and don't initialize or clear it
156 (when (eq (current-buffer) (get-buffer "*Text Description*"))
157 (error "Can't do self inspection"))
158 (if (>= pos
(point-max))
159 (error "No character follows specified position"))
161 (describe-text-properties-1 pos output-buffer
)
162 (if (not (or (text-properties-at pos
) (overlays-at pos
)))
163 (message "This is plain text.")
164 (when (get-buffer "*Text Description*")
165 (kill-buffer "*Text Description*"))
166 (let ((buffer (current-buffer)))
168 (with-output-to-temp-buffer "*Text Description*"
169 (set-buffer "*Text Description*")
170 (setq output-buffer
(current-buffer))
171 (widget-insert "Text content at position " (format "%d" pos
) ":\n\n")
172 (with-current-buffer buffer
173 (describe-text-properties-1 pos output-buffer
))
175 (goto-char (point-min))))))))
177 (defun describe-text-properties-1 (pos output-buffer
)
178 (let* ((properties (text-properties-at pos
))
179 (overlays (overlays-at pos
))
181 (wid-field (get-char-property pos
'field
))
182 (wid-button (get-char-property pos
'button
))
183 (wid-doc (get-char-property pos
'widget-doc
))
184 ;; If button.el is not loaded, we have no buttons in the text.
185 (button (and (fboundp 'button-at
) (button-at pos
)))
186 (button-type (and button
(button-type button
)))
187 (button-label (and button
(button-label button
)))
188 (widget (or wid-field wid-button wid-doc
)))
189 (with-current-buffer output-buffer
191 (when (widgetp widget
)
193 (widget-insert (cond (wid-field "This is an editable text area")
194 (wid-button "This is an active area")
195 (wid-doc "This is documentation text")))
196 (widget-insert " of a ")
197 (describe-text-widget widget
)
198 (widget-insert ".\n\n"))
200 (when (and button
(not (widgetp wid-button
)))
202 (widget-insert "Here is a " (format "%S" button-type
)
203 " button labeled `" button-label
"'.\n\n"))
207 (if (eq (length overlays
) 1)
208 (widget-insert "There is an overlay here:\n")
209 (widget-insert "There are " (format "%d" (length overlays
))
210 " overlays here:\n"))
211 (dolist (overlay overlays
)
212 (widget-insert " From " (format "%d" (overlay-start overlay
))
213 " to " (format "%d" (overlay-end overlay
)) "\n")
214 (describe-property-list (overlay-properties overlay
)))
215 (widget-insert "\n"))
219 (widget-insert "There are text properties here:\n")
220 (describe-property-list properties
)))))
223 (defun describe-char (pos)
224 "Describe the character after POS (interactively, the character after point).
225 The information includes character code, charset and code points in it,
226 syntax, category, how the character is encoded in a file,
227 character composition information (if relevant),
228 as well as widgets, buttons, overlays, and text properties."
230 (when (eq (current-buffer) (get-buffer "*Text Description*"))
231 (error "Can't do self inspection"))
232 (if (>= pos
(point-max))
233 (error "No character follows specified position"))
234 (let* ((char (char-after pos
))
235 (charset (char-charset char
))
236 (buffer (current-buffer))
237 (composition (find-composition (point) nil nil t
))
238 (composed (if composition
(buffer-substring (car composition
)
239 (nth 1 composition
))))
240 (multibyte-p enable-multibyte-characters
)
242 (if (eq charset
'unknown
)
245 ,(format "%s (0%o, %d, 0x%x) -- invalid character code"
247 (single-key-description char
)
248 (char-to-string char
))
252 ,(format "%s (0%o, %d, 0x%x)" (if (< char
256)
253 (single-key-description char
)
254 (char-to-string char
))
257 ,(symbol-name charset
)
258 ,(format "(%s)" (charset-description charset
)))
260 ,(let ((split (split-char char
)))
261 (if (= (charset-dimension charset
) 1)
262 (format "%d" (nth 1 split
))
263 (format "%d %d" (nth 1 split
) (nth 2 split
)))))
265 ,(let ((syntax (get-char-property (point) 'syntax-table
)))
267 (internal-describe-syntax-value
268 (if (consp syntax
) syntax
269 (aref (or syntax
(syntax-table)) char
)))
272 ,@(let ((category-set (char-category-set char
)))
273 (if (not category-set
)
275 (mapcar #'(lambda (x) (format "%c:%s "
276 x
(category-docstring x
)))
277 (category-set-mnemonics category-set
)))))
278 ,@(let ((props (aref char-code-property-table char
))
282 (push (format "%s:" (pop props
)) ps
)
283 (push (format "%s;" (pop props
)) ps
))
284 (list (cons "Properties" (nreverse ps
)))))
286 ,(encoded-string-description
287 (string-as-unibyte (char-to-string char
)) nil
))
289 ,@(let* ((coding buffer-file-coding-system
)
290 (encoded (encode-coding-char char coding
)))
292 (list (encoded-string-description encoded coding
)
293 (format "(encoded by coding system %S)" coding
))
294 (list "not encodable by coding system"
295 (symbol-name coding
)))))
296 ,@(if (or (memq 'mule-utf-8
297 (find-coding-systems-region (point) (1+ (point))))
298 (get-char-property (point) 'untranslated-utf-8
))
299 (let ((uc (or (get-char-property (point)
301 (encode-char (char-after) 'ucs
))))
303 (list (list "Unicode"
304 (format "%04X" uc
))))))
305 ,(if (display-graphic-p (selected-frame))
306 (list "font" (or (internal-char-font (point))
308 (list "terminal code"
309 (let* ((coding (terminal-coding-system))
310 (encoded (encode-coding-char char coding
)))
312 (encoded-string-description encoded coding
)
313 "not encodable")))))))
314 (setq max-width
(apply #'max
(mapcar #'(lambda (x) (length (car x
)))
316 (when (get-buffer "*Help*")
317 (kill-buffer "*Help*"))
318 (with-output-to-temp-buffer "*Help*"
320 (set-buffer standard-output
)
321 (set-buffer-multibyte multibyte-p
)
322 (let ((formatter (format "%%%ds:" max-width
)))
323 (dolist (elt item-list
)
324 (insert (format formatter
(car elt
)))
325 (dolist (clm (cdr elt
))
326 (when (>= (+ (current-column)
327 (or (string-match "\n" clm
)
328 (string-width clm
)) 1)
331 (indent-to (1+ max-width
)))
335 (insert "\nComposed with the following character(s) "
336 (mapconcat (lambda (x) (format "`%c'" x
))
337 (substring composed
1)
339 " to form `" composed
"'")
340 (if (nth 3 composition
)
342 (insert "\nby the rule ("
343 (mapconcat (lambda (x)
344 (format (if (consp x
) "%S" "?%c") x
))
348 "See the variable `reference-point-alist' for "
349 "the meaning of the rule.\n")))
351 (let ((output (current-buffer)))
352 (with-current-buffer buffer
353 (describe-text-properties pos output
))
354 (describe-text-mode))))))
356 (provide 'descr-text
)
358 ;;; descr-text.el ends here