1 ;;; descr-text.el --- describe text mode
3 ;; Copyright (c) 1994, 95, 96, 2001, 02, 03, 04 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 (eval-when-compile (require 'button
) (require 'quail
))
33 (defun describe-text-done ()
34 "Delete the current window or bury the current buffer."
36 (if (> (count-windows) 1)
40 (defvar describe-text-mode-map
41 (let ((map (make-sparse-keymap)))
42 (set-keymap-parent map widget-keymap
)
44 "Keymap for `describe-text-mode'.")
46 (defcustom describe-text-mode-hook nil
47 "List of hook functions ran by `describe-text-mode'."
51 (defun describe-text-mode ()
52 "Major mode for buffers created by `describe-char'.
54 \\{describe-text-mode-map}
55 Entry to this mode calls the value of `describe-text-mode-hook'
56 if that value is non-nil."
57 (kill-all-local-variables)
58 (setq major-mode
'describe-text-mode
59 mode-name
"Describe-Text")
60 (use-local-map describe-text-mode-map
)
62 (add-hook 'change-major-mode-hook
'font-lock-defontify nil t
)
63 (run-hooks 'describe-text-mode-hook
))
65 ;;; Describe-Text Utilities.
67 (defun describe-text-widget (widget)
68 "Insert text to describe WIDGET in the current buffer."
70 :notify
`(lambda (&rest ignore
)
71 (widget-browse ',widget
))
72 (format "%S" (if (symbolp widget
)
76 (widget-create 'info-link
:tag
"widget" "(widget)Top"))
78 (defun describe-text-sexp (sexp)
79 "Insert a short description of SEXP in the current buffer."
80 (let ((pp (condition-case signal
82 (error (prin1-to-string signal
)))))
83 (when (string-match "\n\\'" pp
)
84 (setq pp
(substring pp
0 (1- (length pp
)))))
85 (if (cond ((string-match "\n" pp
)
87 ((> (length pp
) (- (window-width) (current-column)))
91 (widget-create 'push-button
93 :action
(lambda (widget &optional event
)
94 (with-output-to-temp-buffer
96 (princ (widget-get widget
:value
))))
99 (defun describe-property-list (properties)
100 "Insert a description of PROPERTIES in the current buffer.
101 PROPERTIES should be a list of overlay or text properties.
102 The `category', `face' and `font-lock-face' properties are made
103 into widget buttons that call `describe-text-category' or
104 `describe-face' when pushed."
105 ;; Sort the properties by the size of their value.
106 (dolist (elt (sort (let ((ret nil
)
111 (setq key
(pop properties
)
114 (unless (or (memq key
'(category face font-lock-face
117 (setq val
(pp-to-string val
)
119 (push (list key val len
) ret
))
124 (let ((key (nth 0 elt
))
126 (widget-insert (propertize (format " %-20s " key
)
127 'font-lock-face
'italic
))
128 (cond ((eq key
'category
)
130 :notify
`(lambda (&rest ignore
)
131 (describe-text-category ',value
))
132 (format "%S" value
)))
133 ((memq key
'(face font-lock-face
))
135 :notify
`(lambda (&rest ignore
)
136 (describe-face ',value
))
137 (format "%S" value
)))
138 ((eq key
'syntax-table
)
139 (widget-create 'push-button
141 :action
(lambda (widget &optional event
)
142 (with-output-to-temp-buffer
144 (pp (widget-get widget
:value
))))
147 (describe-text-widget value
))
149 (widget-insert value
))))
150 (widget-insert "\n")))
152 ;;; Describe-Text Commands.
154 (defun describe-text-category (category)
155 "Describe a text property category."
158 (with-output-to-temp-buffer "*Help*"
159 (set-buffer standard-output
)
160 (widget-insert "Category " (format "%S" category
) ":\n\n")
161 (describe-property-list (symbol-plist category
))
163 (goto-char (point-min)))))
166 (defun describe-text-properties (pos &optional output-buffer
)
167 "Describe widgets, buttons, overlays and text properties at POS.
168 Interactively, describe them for the character after point.
169 If optional second argument OUTPUT-BUFFER is non-nil,
170 insert the output into that buffer, and don't initialize or clear it
173 (if (>= pos
(point-max))
174 (error "No character follows specified position"))
176 (describe-text-properties-1 pos output-buffer
)
177 (if (not (or (text-properties-at pos
) (overlays-at pos
)))
178 (message "This is plain text.")
179 (let ((buffer (current-buffer)))
180 (when (eq buffer
(get-buffer "*Help*"))
181 (error "Can't do self inspection"))
183 (with-output-to-temp-buffer "*Help*"
184 (set-buffer standard-output
)
185 (setq output-buffer
(current-buffer))
186 (widget-insert "Text content at position " (format "%d" pos
) ":\n\n")
187 (with-current-buffer buffer
188 (describe-text-properties-1 pos output-buffer
))
190 (goto-char (point-min))))))))
192 (defun describe-text-properties-1 (pos output-buffer
)
193 (let* ((properties (text-properties-at pos
))
194 (overlays (overlays-at pos
))
195 (wid-field (get-char-property pos
'field
))
196 (wid-button (get-char-property pos
'button
))
197 (wid-doc (get-char-property pos
'widget-doc
))
198 ;; If button.el is not loaded, we have no buttons in the text.
199 (button (and (fboundp 'button-at
) (button-at pos
)))
200 (button-type (and button
(button-type button
)))
201 (button-label (and button
(button-label button
)))
202 (widget (or wid-field wid-button wid-doc
)))
203 (with-current-buffer output-buffer
205 (when (widgetp widget
)
207 (widget-insert (cond (wid-field "This is an editable text area")
208 (wid-button "This is an active area")
209 (wid-doc "This is documentation text")))
210 (widget-insert " of a ")
211 (describe-text-widget widget
)
212 (widget-insert ".\n\n"))
214 (when (and button
(not (widgetp wid-button
)))
216 (widget-insert "Here is a " (format "%S" button-type
)
217 " button labeled `" button-label
"'.\n\n"))
221 (if (eq (length overlays
) 1)
222 (widget-insert "There is an overlay here:\n")
223 (widget-insert "There are " (format "%d" (length overlays
))
224 " overlays here:\n"))
225 (dolist (overlay overlays
)
226 (widget-insert " From " (format "%d" (overlay-start overlay
))
227 " to " (format "%d" (overlay-end overlay
)) "\n")
228 (describe-property-list (overlay-properties overlay
)))
229 (widget-insert "\n"))
233 (widget-insert "There are text properties here:\n")
234 (describe-property-list properties
)))))
236 (defcustom describe-char-unicodedata-file nil
237 "Location of Unicode data file.
238 This is the UnicodeData.txt file from the Unicode consortium, used for
239 diagnostics. If it is non-nil `describe-char-after' will print data
240 looked up from it. This facility is mostly of use to people doing
241 multilingual development.
243 This is a fairly large file, not typically present on GNU systems. At
244 the time of writing it is at
245 <URL:http://www.unicode.org/Public/UNIDATA/UnicodeData.txt>."
248 :type
'(choice (const :tag
"None" nil
)
251 ;; We could convert the unidata file into a Lispy form once-for-all
252 ;; and distribute it for loading on demand. It might be made more
253 ;; space-efficient by splitting strings word-wise and replacing them
254 ;; with lists of symbols interned in a private obarray, e.g.
255 ;; "LATIN SMALL LETTER A" => '(LATIN SMALL LETTER A).
257 ;; Fixme: Check whether this needs updating for Unicode 4.
258 (defun describe-char-unicode-data (char)
259 "Return a list of Unicode data for unicode CHAR.
260 Each element is a list of a property description and the property value.
261 The list is null if CHAR isn't found in `describe-char-unicodedata-file'."
262 (when describe-char-unicodedata-file
263 (unless (file-exists-p describe-char-unicodedata-file
)
264 (error "`unicodedata-file' %s not found" describe-char-unicodedata-file
))
266 ;; Find file in fundamental mode to avoid, e.g. flyspell turned
267 ;; on for .txt. Don't use RAWFILE arg in case of DOS line endings.
268 (let ((auto-mode-alist))
269 (find-file-noselect describe-char-unicodedata-file
))
270 (goto-char (point-min))
271 (let ((hex (format "%04X" char
))
273 (if (re-search-forward (concat "^" hex
) nil t
)
275 ;; It's not listed explicitly. Look for ranges, e.g. CJK
276 ;; ideographs, and check whether it's in one of them.
277 (while (and (re-search-forward "^\\([^;]+\\);[^;]+First>;" nil t
)
279 (string-to-number (match-string 1) 16)))
282 (looking-at "^\\([^;]+\\);[^;]+Last>;")
285 (string-to-number (match-string 1) 16))))))
286 (if (and (>= char first
)
290 (let ((fields (mapcar (lambda (elt)
291 (if (> (length elt
) 0)
295 (line-beginning-position)
298 ;; The length depends on whether the last field was empty.
299 (unless (or (= 13 (length fields
))
300 (= 14 (length fields
)))
301 (error "Invalid contents in %s" describe-char-unicodedata-file
))
302 ;; The field names and values lists are slightly
303 ;; modified from Mule-UCS unidata.el.
305 (list "Name" (let ((name (nth 0 fields
)))
306 ;; Check for <..., First>, <..., Last>
307 (if (string-match "\\`\\(<[^,]+\\)," name
)
308 (concat (match-string 1 name
) ">")
313 '(("Lu" .
"uppercase letter")
314 ("Ll" .
"lowercase letter")
315 ("Lt" .
"titlecase letter")
316 ("Mn" .
"non-spacing mark")
317 ("Mc" .
"spacing-combining mark")
318 ("Me" .
"enclosing mark")
319 ("Nd" .
"decimal digit")
320 ("Nl" .
"letter number")
321 ("No" .
"other number")
322 ("Zs" .
"space separator")
323 ("Zl" .
"line separator")
324 ("Zp" .
"paragraph separator")
325 ("Cc" .
"other control")
326 ("Cf" .
"other format")
328 ("Co" .
"private use")
329 ("Cn" .
"not assigned")
330 ("Lm" .
"modifier letter")
331 ("Lo" .
"other letter")
332 ("Pc" .
"connector punctuation")
333 ("Pd" .
"dash punctuation")
334 ("Ps" .
"open punctuation")
335 ("Pe" .
"close punctuation")
336 ("Pi" .
"initial-quotation punctuation")
337 ("Pf" .
"final-quotation punctuation")
338 ("Po" .
"other punctuation")
339 ("Sm" .
"math symbol")
340 ("Sc" .
"currency symbol")
341 ("Sk" .
"modifier symbol")
342 ("So" .
"other symbol")))))
343 (list "Combining class"
345 (string-to-number (nth 2 fields
))
347 (1 .
"Overlays and interior")
349 (8 .
"Hiragana/Katakana voicing marks")
351 (10 .
"Start of fixed position classes")
352 (199 .
"End of fixed position classes")
353 (200 .
"Below left attached")
354 (202 .
"Below attached")
355 (204 .
"Below right attached")
356 (208 .
"Left attached (reordrant around \
357 single base character)")
358 (210 .
"Right attached")
359 (212 .
"Above left attached")
360 (214 .
"Above attached")
361 (216 .
"Above right attached")
364 (222 .
"Below right")
365 (224 .
"Left (reordrant around single base \
370 (232 .
"Above right")
371 (233 .
"Double below")
372 (234 .
"Double above")
373 (240 .
"Below (iota subscript)")))))
374 (list "Bidi category"
377 '(("L" .
"Left-to-Right")
378 ("LRE" .
"Left-to-Right Embedding")
379 ("LRO" .
"Left-to-Right Override")
380 ("R" .
"Right-to-Left")
381 ("AL" .
"Right-to-Left Arabic")
382 ("RLE" .
"Right-to-Left Embedding")
383 ("RLO" .
"Right-to-Left Override")
384 ("PDF" .
"Pop Directional Format")
385 ("EN" .
"European Number")
386 ("ES" .
"European Number Separator")
387 ("ET" .
"European Number Terminator")
388 ("AN" .
"Arabic Number")
389 ("CS" .
"Common Number Separator")
390 ("NSM" .
"Non-Spacing Mark")
391 ("BN" .
"Boundary Neutral")
392 ("B" .
"Paragraph Separator")
393 ("S" .
"Segment Separator")
394 ("WS" .
"Whitespace")
395 ("ON" .
"Other Neutrals")))))
399 (let* ((parts (split-string (nth 4 fields
)))
401 (if (string-match "\\`<\\(.+\\)>\\'" info
)
402 (setq info
(match-string 1 info
))
404 (if info
(setq parts
(cdr parts
)))
405 ;; Maybe printing ? for unrepresentable unicodes
406 ;; here and below should be changed?
407 (setq parts
(mapconcat
409 (string (or (decode-char
411 (string-to-number arg
16))
414 (concat info parts
))))
415 (list "Decimal digit value"
419 (list "Numeric value"
422 (if (equal "Y" (nth 8 fields
))
424 (list "Old name" (nth 9 fields
))
425 (list "ISO 10646 comment" (nth 10 fields
))
426 (list "Uppercase" (and (nth 11 fields
)
427 (string (or (decode-char
432 (list "Lowercase" (and (nth 12 fields
)
433 (string (or (decode-char
438 (list "Titlecase" (and (nth 13 fields
)
439 (string (or (decode-char
445 ;; Return information about how CHAR is displayed at the buffer
446 ;; position POS. If the selected frame is on a graphic display,
447 ;; return a cons (FONTNAME . GLYPH-CODE). Otherwise, return a string
448 ;; describing the terminal codes for the character.
449 (defun describe-char-display (pos char
)
450 (if (display-graphic-p (selected-frame))
451 (internal-char-font pos char
)
452 (let* ((coding (terminal-coding-system))
453 (encoded (encode-coding-char char coding
)))
455 (encoded-string-description encoded coding
)))))
459 (defun describe-char (pos)
460 "Describe the character after POS (interactively, the character after point).
461 The information includes character code, charset and code points in it,
462 syntax, category, how the character is encoded in a file,
463 character composition information (if relevant),
464 as well as widgets, buttons, overlays, and text properties."
466 (if (>= pos
(point-max))
467 (error "No character follows specified position"))
468 (let* ((char (char-after pos
))
469 (charset (char-charset char
))
470 (composition (find-composition pos nil nil t
))
471 (component-chars nil
)
472 (display-table (or (window-display-table)
474 standard-display-table
))
475 (disp-vector (and display-table
(aref display-table char
)))
476 (multibyte-p enable-multibyte-characters
)
477 (overlays (mapcar #'(lambda (o) (overlay-properties o
))
479 item-list max-width unicode
)
482 (memq 'mule-utf-8
(find-coding-systems-region pos
(1+ pos
)))
483 (get-char-property pos
'untranslated-utf-8
))
484 (setq unicode
(or (get-char-property pos
'untranslated-utf-8
)
485 (encode-char char
'ucs
))))
488 ,(format "%s (0%o, %d, 0x%x%s)"
489 (apply 'propertize
(if (not multibyte-p
)
490 (single-key-description char
)
492 (single-key-description char
)
494 (char-to-string char
))))
495 (text-properties-at pos
))
498 (format ", U+%04X" unicode
)
501 ,(symbol-name charset
)
502 ,(format "(%s)" (charset-description charset
)))
504 ,(let ((split (split-char char
)))
505 (if (= (charset-dimension charset
) 1)
506 (format "%d" (nth 1 split
))
507 (format "%d %d" (nth 1 split
) (nth 2 split
)))))
509 ,(let ((syntax (syntax-after pos
)))
511 (internal-describe-syntax-value syntax
)
514 ,@(let ((category-set (char-category-set char
)))
515 (if (not category-set
)
517 (mapcar #'(lambda (x) (format "%c:%s "
518 x
(category-docstring x
)))
519 (category-set-mnemonics category-set
)))))
520 ,@(let ((props (aref char-code-property-table char
))
524 (push (format "%s:" (pop props
)) ps
)
525 (push (format "%s;" (pop props
)) ps
))
526 (list (cons "Properties" (nreverse ps
)))))
528 ,@(let ((key-list (and current-input-method
529 (quail-find-key char
))))
532 (mapconcat #'(lambda (x) (concat "\"" x
"\""))
535 ,(encoded-string-description
536 (string-as-unibyte (char-to-string char
)) nil
))
538 ,@(let* ((coding buffer-file-coding-system
)
539 (encoded (encode-coding-char char coding
)))
541 (list (encoded-string-description encoded coding
)
542 (format "(encoded by coding system %S)" coding
))
543 (list "not encodable by coding system"
544 (symbol-name coding
)))))
548 (setq disp-vector
(copy-sequence disp-vector
))
549 (dotimes (i (length disp-vector
))
550 (setq char
(aref disp-vector i
))
552 (cons char
(describe-char-display pos char
))))
553 (format "by display table entry [%s] (see below)"
554 (mapconcat #'(lambda (x) (format "?%c" (car x
)))
557 (let ((from (car composition
))
558 (to (nth 1 composition
))
560 (components (nth 2 composition
))
563 (and (< from pos
) (buffer-substring from pos
)))
564 (setcar (cdr composition
)
565 (and (< next to
) (buffer-substring next to
)))
566 (dotimes (i (length components
))
567 (if (integerp (setq ch
(aref components i
)))
568 (push (cons ch
(describe-char-display pos ch
))
570 (setq component-chars
(nreverse component-chars
))
571 (format "composed to form \"%s\" (see below)"
572 (buffer-substring from to
))))
574 (let ((display (describe-char-display pos char
)))
575 (if (display-graphic-p (selected-frame))
578 "by this font (glyph code)\n"
579 (format " %s (0x%02X)"
580 (car display
) (cdr display
)))
583 (format "terminal code %s" display
)
584 "not encodable for terminal"))))))
585 ,@(let ((unicodedata (and unicode
586 (describe-char-unicode-data unicode
))))
588 (cons (list "Unicode data" " ") unicodedata
)))))
589 (setq max-width
(apply #'max
(mapcar #'(lambda (x) (length (car x
)))
591 (with-output-to-temp-buffer "*Help*"
592 (with-current-buffer standard-output
593 (set-buffer-multibyte multibyte-p
)
594 (let ((formatter (format "%%%ds:" max-width
)))
595 (dolist (elt item-list
)
597 (insert (format formatter
(car elt
)))
598 (dolist (clm (cdr elt
))
599 (when (>= (+ (current-column)
600 (or (string-match "\n" clm
)
601 (string-width clm
)) 1)
604 (indent-to (1+ max-width
)))
609 (goto-char (point-min))
610 (re-search-forward "character:[ \t\n]+")
613 (mapc #'(lambda (props)
614 (let ((o (make-overlay pos
(1+ pos
))))
616 (overlay-put o
(car props
) (nth 1 props
))
617 (setq props
(cddr props
)))))
622 "\nThe display table entry is displayed by ")
623 (if (display-graphic-p (selected-frame))
625 (insert "these fonts (glyph codes):\n")
626 (dotimes (i (length disp-vector
))
627 (insert (car (aref disp-vector i
)) ?
:
628 (propertize " " 'display
'(space :align-to
5))
629 (if (cdr (aref disp-vector i
))
630 (format "%s (0x%02X)" (cadr (aref disp-vector i
))
631 (cddr (aref disp-vector i
)))
634 (insert "these terminal codes:\n")
635 (dotimes (i (length disp-vector
))
636 (insert (car (aref disp-vector i
))
637 (propertize " " 'display
'(space :align-to
5))
638 (or (cdr (aref disp-vector i
)) "-- not encodable --")
642 (insert "\nComposed")
643 (if (car composition
)
644 (if (cadr composition
)
645 (insert " with the surrounding characters \""
646 (car composition
) "\" and \""
647 (cadr composition
) "\"")
648 (insert " with the preceding character(s) \""
649 (car composition
) "\""))
650 (if (cadr composition
)
651 (insert " with the following character(s) \""
652 (cadr composition
) "\"")))
653 (insert " by the rule:\n\t("
654 (mapconcat (lambda (x)
655 (format (if (consp x
) "%S" "?%c") x
))
659 (insert "\nThe component character(s) are displayed by ")
660 (if (display-graphic-p (selected-frame))
662 (insert "these fonts (glyph codes):")
663 (dolist (elt component-chars
)
664 (insert "\n " (car elt
) ?
:
665 (propertize " " 'display
'(space :align-to
5))
667 (format "%s (0x%02X)" (cadr elt
) (cddr elt
))
669 (insert "these terminal codes:")
670 (dolist (elt component-chars
)
671 (insert "\n " (car elt
) ":"
672 (propertize " " 'display
'(space :align-to
5))
673 (or (cdr elt
) "-- not encodable --"))))
674 (insert "\nSee the variable `reference-point-alist' for "
675 "the meaning of the rule.\n"))
677 (describe-text-properties pos
(current-buffer))
678 (describe-text-mode)))))
680 (defalias 'describe-char-after
'describe-char
)
681 (make-obsolete 'describe-char-after
'describe-char
"21.5")
683 (provide 'descr-text
)
685 ;;; arch-tag: fc55a498-f3e9-4312-b5bd-98cc02480af1
686 ;;; descr-text.el ends here