1 ;;; descr-text.el --- describe text mode
3 ;; Copyright (C) 1994, 1995, 1996, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Boris Goldowsky <boris@gnu.org>
8 ;; Keywords: faces, i18n, Unicode, multilingual
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;;; Describe-Text Mode.
33 (eval-when-compile (require 'quail
))
36 ;;; Describe-Text Utilities.
38 (defun describe-text-widget (widget)
39 "Insert text to describe WIDGET in the current buffer."
41 (symbol-name (if (symbolp widget
) widget
(car widget
)))
42 'action
`(lambda (&rest ignore
)
43 (widget-browse ',widget
))
44 'help-echo
"mouse-2, RET: browse this widget")
47 "(widget)Top" 'type
'help-info
'help-args
'("(widget)Top")))
49 (defun describe-text-sexp (sexp)
50 "Insert a short description of SEXP in the current buffer."
51 (let ((pp (condition-case signal
53 (error (prin1-to-string signal
)))))
54 (when (string-match "\n\\'" pp
)
55 (setq pp
(substring pp
0 (1- (length pp
)))))
56 (if (cond ((string-match "\n" pp
)
58 ((> (length pp
) (- (window-width) (current-column)))
63 "[Show]" 'action
`(lambda (&rest ignore
)
64 (with-output-to-temp-buffer
67 'help-echo
"mouse-2, RET: pretty print value in another buffer"))))
69 (defun describe-property-list (properties)
70 "Insert a description of PROPERTIES in the current buffer.
71 PROPERTIES should be a list of overlay or text properties.
72 The `category', `face' and `font-lock-face' properties are made
73 into help buttons that call `describe-text-category' or
74 `describe-face' when pushed."
75 ;; Sort the properties by the size of their value.
76 (dolist (elt (sort (let (ret)
78 (push (list (pop properties
) (pop properties
)) ret
))
80 (lambda (a b
) (string< (prin1-to-string (nth 0 a
) t
)
81 (prin1-to-string (nth 0 b
) t
)))))
82 (let ((key (nth 0 elt
))
84 (insert (propertize (format " %-20s " key
)
85 'face
'help-argument-name
))
86 (cond ((eq key
'category
)
89 'action
`(lambda (&rest ignore
)
90 (describe-text-category ',value
))
91 'help-echo
"mouse-2, RET: describe this category"))
92 ((memq key
'(face font-lock-face mouse-face
))
95 'type
'help-face
'help-args
(list value
)))
97 (describe-text-widget value
))
99 (describe-text-sexp value
))))
102 ;;; Describe-Text Commands.
104 (defun describe-text-category (category)
105 "Describe a text property category."
106 (interactive "SCategory: ")
107 (help-setup-xref (list #'describe-text-category category
) (interactive-p))
109 (with-output-to-temp-buffer "*Help*"
110 (set-buffer standard-output
)
111 (insert "Category " (format "%S" category
) ":\n\n")
112 (describe-property-list (symbol-plist category
))
113 (goto-char (point-min)))))
116 (defun describe-text-properties (pos &optional output-buffer
)
117 "Describe widgets, buttons, overlays and text properties at POS.
118 Interactively, describe them for the character after point.
119 If optional second argument OUTPUT-BUFFER is non-nil,
120 insert the output into that buffer, and don't initialize or clear it
123 (if (>= pos
(point-max))
124 (error "No character follows specified position"))
126 (describe-text-properties-1 pos output-buffer
)
127 (if (not (or (text-properties-at pos
) (overlays-at pos
)))
128 (message "This is plain text.")
129 (let ((buffer (current-buffer))
130 (target-buffer "*Help*"))
131 (when (eq buffer
(get-buffer target-buffer
))
132 (setq target-buffer
"*Help*<2>"))
134 (with-output-to-temp-buffer target-buffer
135 (set-buffer standard-output
)
136 (setq output-buffer
(current-buffer))
137 (insert "Text content at position " (format "%d" pos
) ":\n\n")
138 (with-current-buffer buffer
139 (describe-text-properties-1 pos output-buffer
))
140 (goto-char (point-min))))))))
142 (defun describe-text-properties-1 (pos output-buffer
)
143 (let* ((properties (text-properties-at pos
))
144 (overlays (overlays-at pos
))
145 (wid-field (get-char-property pos
'field
))
146 (wid-button (get-char-property pos
'button
))
147 (wid-doc (get-char-property pos
'widget-doc
))
148 ;; If button.el is not loaded, we have no buttons in the text.
149 (button (and (fboundp 'button-at
) (button-at pos
)))
150 (button-type (and button
(button-type button
)))
151 (button-label (and button
(button-label button
)))
152 (widget (or wid-field wid-button wid-doc
)))
153 (with-current-buffer output-buffer
155 (when (widgetp widget
)
157 (insert (cond (wid-field "This is an editable text area")
158 (wid-button "This is an active area")
159 (wid-doc "This is documentation text")))
161 (describe-text-widget widget
)
164 (when (and button
(not (widgetp wid-button
)))
166 (insert "Here is a `" (format "%S" button-type
)
167 "' button labeled `" button-label
"'.\n\n"))
171 (if (eq (length overlays
) 1)
172 (insert "There is an overlay here:\n")
173 (insert "There are " (format "%d" (length overlays
))
174 " overlays here:\n"))
175 (dolist (overlay overlays
)
176 (insert " From " (format "%d" (overlay-start overlay
))
177 " to " (format "%d" (overlay-end overlay
)) "\n")
178 (describe-property-list (overlay-properties overlay
)))
183 (insert "There are text properties here:\n")
184 (describe-property-list properties
)))))
186 (defcustom describe-char-unicodedata-file nil
187 "Location of Unicode data file.
188 This is the UnicodeData.txt file from the Unicode consortium, used for
189 diagnostics. If it is non-nil `describe-char' will print data
190 looked up from it. This facility is mostly of use to people doing
191 multilingual development.
193 This is a fairly large file, not typically present on GNU systems. At
194 the time of writing it is at the URL
195 `http://www.unicode.org/Public/UNIDATA/UnicodeData.txt'."
198 :type
'(choice (const :tag
"None" nil
)
201 ;; We could convert the unidata file into a Lispy form once-for-all
202 ;; and distribute it for loading on demand. It might be made more
203 ;; space-efficient by splitting strings word-wise and replacing them
204 ;; with lists of symbols interned in a private obarray, e.g.
205 ;; "LATIN SMALL LETTER A" => '(LATIN SMALL LETTER A).
207 ;; Fixme: Check whether this needs updating for Unicode 4.
208 (defun describe-char-unicode-data (char)
209 "Return a list of Unicode data for unicode CHAR.
210 Each element is a list of a property description and the property value.
211 The list is null if CHAR isn't found in `describe-char-unicodedata-file'."
212 (when describe-char-unicodedata-file
213 (unless (file-exists-p describe-char-unicodedata-file
)
214 (error "`unicodedata-file' %s not found" describe-char-unicodedata-file
))
216 ;; Find file in fundamental mode to avoid, e.g. flyspell turned
217 ;; on for .txt. Don't use RAWFILE arg in case of DOS line endings.
218 (let ((auto-mode-alist))
219 (find-file-noselect describe-char-unicodedata-file
))
220 (goto-char (point-min))
221 (let ((hex (format "%04X" char
))
223 (if (re-search-forward (concat "^" hex
) nil t
)
225 ;; It's not listed explicitly. Look for ranges, e.g. CJK
226 ;; ideographs, and check whether it's in one of them.
227 (while (and (re-search-forward "^\\([^;]+\\);[^;]+First>;" nil t
)
229 (string-to-number (match-string 1) 16)))
232 (looking-at "^\\([^;]+\\);[^;]+Last>;")
235 (string-to-number (match-string 1) 16))))))
236 (if (and (>= char first
)
240 (let ((fields (mapcar (lambda (elt)
241 (if (> (length elt
) 0)
245 (line-beginning-position)
248 ;; The length depends on whether the last field was empty.
249 (unless (or (= 13 (length fields
))
250 (= 14 (length fields
)))
251 (error "Invalid contents in %s" describe-char-unicodedata-file
))
252 ;; The field names and values lists are slightly
253 ;; modified from Mule-UCS unidata.el.
255 (list "Name" (let ((name (nth 0 fields
)))
256 ;; Check for <..., First>, <..., Last>
257 (if (string-match "\\`\\(<[^,]+\\)," name
)
258 (concat (match-string 1 name
) ">")
263 '(("Lu" .
"uppercase letter")
264 ("Ll" .
"lowercase letter")
265 ("Lt" .
"titlecase letter")
266 ("Mn" .
"non-spacing mark")
267 ("Mc" .
"spacing-combining mark")
268 ("Me" .
"enclosing mark")
269 ("Nd" .
"decimal digit")
270 ("Nl" .
"letter number")
271 ("No" .
"other number")
272 ("Zs" .
"space separator")
273 ("Zl" .
"line separator")
274 ("Zp" .
"paragraph separator")
275 ("Cc" .
"other control")
276 ("Cf" .
"other format")
278 ("Co" .
"private use")
279 ("Cn" .
"not assigned")
280 ("Lm" .
"modifier letter")
281 ("Lo" .
"other letter")
282 ("Pc" .
"connector punctuation")
283 ("Pd" .
"dash punctuation")
284 ("Ps" .
"open punctuation")
285 ("Pe" .
"close punctuation")
286 ("Pi" .
"initial-quotation punctuation")
287 ("Pf" .
"final-quotation punctuation")
288 ("Po" .
"other punctuation")
289 ("Sm" .
"math symbol")
290 ("Sc" .
"currency symbol")
291 ("Sk" .
"modifier symbol")
292 ("So" .
"other symbol")))))
293 (list "Combining class"
295 (string-to-number (nth 2 fields
))
297 (1 .
"Overlays and interior")
299 (8 .
"Hiragana/Katakana voicing marks")
301 (10 .
"Start of fixed position classes")
302 (199 .
"End of fixed position classes")
303 (200 .
"Below left attached")
304 (202 .
"Below attached")
305 (204 .
"Below right attached")
306 (208 .
"Left attached (reordrant around \
307 single base character)")
308 (210 .
"Right attached")
309 (212 .
"Above left attached")
310 (214 .
"Above attached")
311 (216 .
"Above right attached")
314 (222 .
"Below right")
315 (224 .
"Left (reordrant around single base \
320 (232 .
"Above right")
321 (233 .
"Double below")
322 (234 .
"Double above")
323 (240 .
"Below (iota subscript)")))))
324 (list "Bidi category"
327 '(("L" .
"Left-to-Right")
328 ("LRE" .
"Left-to-Right Embedding")
329 ("LRO" .
"Left-to-Right Override")
330 ("R" .
"Right-to-Left")
331 ("AL" .
"Right-to-Left Arabic")
332 ("RLE" .
"Right-to-Left Embedding")
333 ("RLO" .
"Right-to-Left Override")
334 ("PDF" .
"Pop Directional Format")
335 ("EN" .
"European Number")
336 ("ES" .
"European Number Separator")
337 ("ET" .
"European Number Terminator")
338 ("AN" .
"Arabic Number")
339 ("CS" .
"Common Number Separator")
340 ("NSM" .
"Non-Spacing Mark")
341 ("BN" .
"Boundary Neutral")
342 ("B" .
"Paragraph Separator")
343 ("S" .
"Segment Separator")
344 ("WS" .
"Whitespace")
345 ("ON" .
"Other Neutrals")))))
349 (let* ((parts (split-string (nth 4 fields
)))
351 (if (string-match "\\`<\\(.+\\)>\\'" info
)
352 (setq info
(match-string 1 info
))
354 (if info
(setq parts
(cdr parts
)))
355 ;; Maybe printing ? for unrepresentable unicodes
356 ;; here and below should be changed?
357 (setq parts
(mapconcat
359 (string (or (decode-char
361 (string-to-number arg
16))
364 (concat info parts
))))
365 (list "Decimal digit value"
369 (list "Numeric value"
372 (if (equal "Y" (nth 8 fields
))
374 (list "Old name" (nth 9 fields
))
375 (list "ISO 10646 comment" (nth 10 fields
))
376 (list "Uppercase" (and (nth 11 fields
)
377 (string (or (decode-char
382 (list "Lowercase" (and (nth 12 fields
)
383 (string (or (decode-char
388 (list "Titlecase" (and (nth 13 fields
)
389 (string (or (decode-char
395 ;; Return information about how CHAR is displayed at the buffer
396 ;; position POS. If the selected frame is on a graphic display,
397 ;; return a cons (FONTNAME . GLYPH-CODE). Otherwise, return a string
398 ;; describing the terminal codes for the character.
399 (defun describe-char-display (pos char
)
400 (if (display-graphic-p (selected-frame))
401 (internal-char-font pos char
)
402 (let* ((coding (terminal-coding-system))
403 (encoded (encode-coding-char char coding
)))
405 (encoded-string-description encoded coding
)))))
409 (defun describe-char (pos)
410 "Describe the character after POS (interactively, the character after point).
411 The information includes character code, charset and code points in it,
412 syntax, category, how the character is encoded in a file,
413 character composition information (if relevant),
414 as well as widgets, buttons, overlays, and text properties."
416 (if (>= pos
(point-max))
417 (error "No character follows specified position"))
418 (let* ((char (char-after pos
))
419 (charset (char-charset char
))
420 (composition (find-composition pos nil nil t
))
421 (component-chars nil
)
422 (display-table (or (window-display-table)
424 standard-display-table
))
425 (disp-vector (and display-table
(aref display-table char
)))
426 (multibyte-p enable-multibyte-characters
)
427 (overlays (mapcar #'(lambda (o) (overlay-properties o
))
429 (char-description (if (not multibyte-p
)
430 (single-key-description char
)
432 (single-key-description char
)
434 (char-to-string char
)))))
436 (let ((tmp-buf (generate-new-buffer " *text-props*")))
439 (describe-text-properties pos tmp-buf
)
440 (with-current-buffer tmp-buf
(buffer-string)))
441 (kill-buffer tmp-buf
))))
442 item-list max-width unicode
)
445 (memq 'mule-utf-8
(find-coding-systems-region pos
(1+ pos
)))
446 (get-char-property pos
'untranslated-utf-8
))
447 (setq unicode
(or (get-char-property pos
'untranslated-utf-8
)
448 (encode-char char
'ucs
))))
451 ,(format "%s (%d, #o%o, #x%x%s)"
452 (apply 'propertize char-description
453 (text-properties-at pos
))
456 (format ", U+%04X" unicode
)
459 ,`(insert-text-button
460 ,(symbol-name charset
)
461 'type
'help-character-set
'help-args
'(,charset
))
462 ,(format "(%s)" (charset-description charset
)))
464 ,(let ((split (split-char char
)))
466 ,(if (= (charset-dimension charset
) 1)
467 (format "#x%02X" (nth 1 split
))
468 (format "#x%02X #x%02X" (nth 1 split
)
470 'action
(lambda (&rest ignore
)
471 (list-charset-chars ',charset
)
472 (with-selected-window
473 (get-buffer-window "*Character List*" 0)
474 (goto-char (point-min))
475 (forward-line 2) ;Skip the header.
476 (let ((case-fold-search nil
))
477 (search-forward ,(char-to-string char
)
480 "mouse-2, RET: show this character in its character set")))
482 ,(let ((syntax (syntax-after pos
)))
484 (internal-describe-syntax-value syntax
)
487 ,@(let ((category-set (char-category-set char
)))
488 (if (not category-set
)
490 (mapcar #'(lambda (x) (format "%c:%s"
491 x
(category-docstring x
)))
492 (category-set-mnemonics category-set
)))))
493 ,@(let ((props (aref char-code-property-table char
))
497 (push (format "%s:" (pop props
)) ps
)
498 (push (format "%s;" (pop props
)) ps
))
499 (list (cons "Properties" (nreverse ps
)))))
501 ,@(let ((key-list (and (eq input-method-function
503 (quail-find-key char
))))
506 (mapconcat #'(lambda (x) (concat "\"" x
"\""))
510 ,current-input-method
511 'type
'help-input-method
512 'help-args
'(,current-input-method
))))))
514 ,(encoded-string-description
515 (string-as-unibyte (char-to-string char
)) nil
))
517 ,@(let* ((coding buffer-file-coding-system
)
518 (encoded (encode-coding-char char coding
)))
520 (list (encoded-string-description encoded coding
)
521 (format "(encoded by coding system %S)" coding
))
522 (list "not encodable by coding system"
523 (symbol-name coding
)))))
527 (setq disp-vector
(copy-sequence disp-vector
))
528 (dotimes (i (length disp-vector
))
529 (setq char
(aref disp-vector i
))
531 (cons char
(describe-char-display
532 pos
(logand char
#x7ffff
)))))
533 (format "by display table entry [%s] (see below)"
536 (format "?%c" (logand (car x
) #x7ffff
)))
539 (let ((from (car composition
))
540 (to (nth 1 composition
))
542 (components (nth 2 composition
))
545 (and (< from pos
) (buffer-substring from pos
)))
546 (setcar (cdr composition
)
547 (and (< next to
) (buffer-substring next to
)))
548 (dotimes (i (length components
))
549 (if (integerp (setq ch
(aref components i
)))
550 (push (cons ch
(describe-char-display pos ch
))
552 (setq component-chars
(nreverse component-chars
))
553 (format "composed to form \"%s\" (see below)"
554 (buffer-substring from to
))))
556 (let ((display (describe-char-display pos char
)))
557 (if (display-graphic-p (selected-frame))
560 "by this font (glyph code)\n"
561 (format " %s (#x%02X)"
562 (car display
) (cdr display
)))
565 (format "terminal code %s" display
)
566 "not encodable for terminal"))))))
568 (if (not (or disp-vector composition
))
570 ((and show-trailing-whitespace
571 (save-excursion (goto-char pos
)
572 (looking-at "[ \t]+$")))
573 'trailing-whitespace
)
574 ((and nobreak-char-display unicode
(eq unicode
'#xa0
))
576 ((and nobreak-char-display unicode
(eq unicode
'#xad
))
578 ((and (< char
32) (not (memq char
'(9 10))))
580 (if face
(list (list "hardcoded face"
583 'type
'help-face
'help-args
'(,face
))))))
584 ,@(let ((unicodedata (and unicode
585 (describe-char-unicode-data unicode
))))
587 (cons (list "Unicode data" " ") unicodedata
)))))
588 (setq max-width
(apply #'max
(mapcar #'(lambda (x)
589 (if (cadr x
) (length (car x
)) 0))
591 (help-setup-xref nil
(interactive-p))
592 (with-output-to-temp-buffer (help-buffer)
593 (with-current-buffer standard-output
594 (set-buffer-multibyte multibyte-p
)
595 (let ((formatter (format "%%%ds:" max-width
)))
596 (dolist (elt item-list
)
598 (insert (format formatter
(car elt
)))
599 (dolist (clm (cdr elt
))
600 (if (eq (car-safe clm
) 'insert-text-button
)
601 (progn (insert " ") (eval clm
))
602 (when (>= (+ (current-column)
603 (or (string-match "\n" clm
)
608 (indent-to (1+ max-width
)))
614 (goto-char (point-min))
615 (re-search-forward "character:[ \t\n]+")
616 (let* ((end (+ (point) (length char-description
))))
617 (mapc #'(lambda (props)
618 (let ((o (make-overlay (point) end
)))
620 (overlay-put o
(car props
) (nth 1 props
))
621 (setq props
(cddr props
)))))
626 "\nThe display table entry is displayed by ")
627 (if (display-graphic-p (selected-frame))
629 (insert "these fonts (glyph codes):\n")
630 (dotimes (i (length disp-vector
))
631 (insert (logand (car (aref disp-vector i
)) #x7ffff
) ?
:
632 (propertize " " 'display
'(space :align-to
5))
633 (if (cdr (aref disp-vector i
))
634 (format "%s (#x%02X)" (cadr (aref disp-vector i
))
635 (cddr (aref disp-vector i
)))
638 (when (> (car (aref disp-vector i
)) #x7ffff
)
639 (let* ((face-id (lsh (car (aref disp-vector i
)) -
19))
640 (face (car (delq nil
(mapcar
642 (and (eq (face-id face
)
646 (insert (propertize " " 'display
'(space :align-to
5))
648 (insert (concat "`" (symbol-name face
) "'"))
650 (insert "these terminal codes:\n")
651 (dotimes (i (length disp-vector
))
652 (insert (car (aref disp-vector i
))
653 (propertize " " 'display
'(space :align-to
5))
654 (or (cdr (aref disp-vector i
)) "-- not encodable --")
658 (insert "\nComposed")
659 (if (car composition
)
660 (if (cadr composition
)
661 (insert " with the surrounding characters \""
662 (car composition
) "\" and \""
663 (cadr composition
) "\"")
664 (insert " with the preceding character(s) \""
665 (car composition
) "\""))
666 (if (cadr composition
)
667 (insert " with the following character(s) \""
668 (cadr composition
) "\"")))
669 (insert " by the rule:\n\t("
670 (mapconcat (lambda (x)
671 (format (if (consp x
) "%S" "?%c") x
))
675 (insert "\nThe component character(s) are displayed by ")
676 (if (display-graphic-p (selected-frame))
678 (insert "these fonts (glyph codes):")
679 (dolist (elt component-chars
)
680 (insert "\n " (car elt
) ?
:
681 (propertize " " 'display
'(space :align-to
5))
683 (format "%s (#x%02X)" (cadr elt
) (cddr elt
))
685 (insert "these terminal codes:")
686 (dolist (elt component-chars
)
687 (insert "\n " (car elt
) ":"
688 (propertize " " 'display
'(space :align-to
5))
689 (or (cdr elt
) "-- not encodable --"))))
690 (insert "\nSee the variable `reference-point-alist' for "
691 "the meaning of the rule.\n"))
693 (if text-props-desc
(insert text-props-desc
))
694 (setq help-xref-stack-item
(list 'help-insert-string
(buffer-string)))
696 (print-help-return-message)))))
698 (defalias 'describe-char-after
'describe-char
)
699 (make-obsolete 'describe-char-after
'describe-char
"22.1")
701 (provide 'descr-text
)
703 ;; arch-tag: fc55a498-f3e9-4312-b5bd-98cc02480af1
704 ;;; descr-text.el ends here