1 ;;; descr-text.el --- describe text mode
3 ;; Copyright (C) 1994, 1995, 1996, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 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 3, 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.
194 At 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
))
215 (with-current-buffer (get-buffer-create " *Unicode Data*")
216 (when (zerop (buffer-size))
217 ;; Don't use -literally in case of DOS line endings.
218 (insert-file-contents describe-char-unicodedata-file
))
219 (goto-char (point-min))
220 (let ((hex (format "%04X" char
))
222 (if (re-search-forward (concat "^" hex
) nil t
)
224 ;; It's not listed explicitly. Look for ranges, e.g. CJK
225 ;; ideographs, and check whether it's in one of them.
226 (while (and (re-search-forward "^\\([^;]+\\);[^;]+First>;" nil t
)
228 (string-to-number (match-string 1) 16)))
231 (looking-at "^\\([^;]+\\);[^;]+Last>;")
234 (string-to-number (match-string 1) 16))))))
235 (if (and (>= char first
)
239 (let ((fields (mapcar (lambda (elt)
240 (if (> (length elt
) 0)
244 (line-beginning-position)
247 ;; The length depends on whether the last field was empty.
248 (unless (or (= 13 (length fields
))
249 (= 14 (length fields
)))
250 (error "Invalid contents in %s" describe-char-unicodedata-file
))
251 ;; The field names and values lists are slightly
252 ;; modified from Mule-UCS unidata.el.
254 (list "Name" (let ((name (nth 0 fields
)))
255 ;; Check for <..., First>, <..., Last>
256 (if (string-match "\\`\\(<[^,]+\\)," name
)
257 (concat (match-string 1 name
) ">")
262 '(("Lu" .
"uppercase letter")
263 ("Ll" .
"lowercase letter")
264 ("Lt" .
"titlecase letter")
265 ("Mn" .
"non-spacing mark")
266 ("Mc" .
"spacing-combining mark")
267 ("Me" .
"enclosing mark")
268 ("Nd" .
"decimal digit")
269 ("Nl" .
"letter number")
270 ("No" .
"other number")
271 ("Zs" .
"space separator")
272 ("Zl" .
"line separator")
273 ("Zp" .
"paragraph separator")
274 ("Cc" .
"other control")
275 ("Cf" .
"other format")
277 ("Co" .
"private use")
278 ("Cn" .
"not assigned")
279 ("Lm" .
"modifier letter")
280 ("Lo" .
"other letter")
281 ("Pc" .
"connector punctuation")
282 ("Pd" .
"dash punctuation")
283 ("Ps" .
"open punctuation")
284 ("Pe" .
"close punctuation")
285 ("Pi" .
"initial-quotation punctuation")
286 ("Pf" .
"final-quotation punctuation")
287 ("Po" .
"other punctuation")
288 ("Sm" .
"math symbol")
289 ("Sc" .
"currency symbol")
290 ("Sk" .
"modifier symbol")
291 ("So" .
"other symbol")))))
292 (list "Combining class"
294 (string-to-number (nth 2 fields
))
296 (1 .
"Overlays and interior")
298 (8 .
"Hiragana/Katakana voicing marks")
300 (10 .
"Start of fixed position classes")
301 (199 .
"End of fixed position classes")
302 (200 .
"Below left attached")
303 (202 .
"Below attached")
304 (204 .
"Below right attached")
305 (208 .
"Left attached (reordrant around \
306 single base character)")
307 (210 .
"Right attached")
308 (212 .
"Above left attached")
309 (214 .
"Above attached")
310 (216 .
"Above right attached")
313 (222 .
"Below right")
314 (224 .
"Left (reordrant around single base \
319 (232 .
"Above right")
320 (233 .
"Double below")
321 (234 .
"Double above")
322 (240 .
"Below (iota subscript)")))))
323 (list "Bidi category"
326 '(("L" .
"Left-to-Right")
327 ("LRE" .
"Left-to-Right Embedding")
328 ("LRO" .
"Left-to-Right Override")
329 ("R" .
"Right-to-Left")
330 ("AL" .
"Right-to-Left Arabic")
331 ("RLE" .
"Right-to-Left Embedding")
332 ("RLO" .
"Right-to-Left Override")
333 ("PDF" .
"Pop Directional Format")
334 ("EN" .
"European Number")
335 ("ES" .
"European Number Separator")
336 ("ET" .
"European Number Terminator")
337 ("AN" .
"Arabic Number")
338 ("CS" .
"Common Number Separator")
339 ("NSM" .
"Non-Spacing Mark")
340 ("BN" .
"Boundary Neutral")
341 ("B" .
"Paragraph Separator")
342 ("S" .
"Segment Separator")
343 ("WS" .
"Whitespace")
344 ("ON" .
"Other Neutrals")))))
348 (let* ((parts (split-string (nth 4 fields
)))
350 (if (string-match "\\`<\\(.+\\)>\\'" info
)
351 (setq info
(match-string 1 info
))
353 (if info
(setq parts
(cdr parts
)))
354 ;; Maybe printing ? for unrepresentable unicodes
355 ;; here and below should be changed?
356 (setq parts
(mapconcat
358 (string (or (decode-char
360 (string-to-number arg
16))
363 (concat info parts
))))
364 (list "Decimal digit value"
368 (list "Numeric value"
371 (if (equal "Y" (nth 8 fields
))
373 (list "Old name" (nth 9 fields
))
374 (list "ISO 10646 comment" (nth 10 fields
))
375 (list "Uppercase" (and (nth 11 fields
)
376 (string (or (decode-char
381 (list "Lowercase" (and (nth 12 fields
)
382 (string (or (decode-char
387 (list "Titlecase" (and (nth 13 fields
)
388 (string (or (decode-char
394 ;; Return information about how CHAR is displayed at the buffer
395 ;; position POS. If the selected frame is on a graphic display,
396 ;; return a cons (FONTNAME . GLYPH-CODE). Otherwise, return a string
397 ;; describing the terminal codes for the character.
398 (defun describe-char-display (pos char
)
399 (if (display-graphic-p (selected-frame))
400 (internal-char-font pos char
)
401 (let* ((coding (terminal-coding-system))
402 (encoded (encode-coding-char char coding
)))
404 (encoded-string-description encoded coding
)))))
408 (defun describe-char (pos)
409 "Describe the character after POS (interactively, the character after point).
410 The information includes character code, charset and code points in it,
411 syntax, category, how the character is encoded in a file,
412 character composition information (if relevant),
413 as well as widgets, buttons, overlays, and text properties."
415 (if (>= pos
(point-max))
416 (error "No character follows specified position"))
417 (let* ((char (char-after pos
))
418 (charset (char-charset char
))
419 (composition (find-composition pos nil nil t
))
420 (component-chars nil
)
421 (display-table (or (window-display-table)
423 standard-display-table
))
424 (disp-vector (and display-table
(aref display-table char
)))
425 (multibyte-p enable-multibyte-characters
)
426 (overlays (mapcar #'(lambda (o) (overlay-properties o
))
428 (char-description (if (not multibyte-p
)
429 (single-key-description char
)
431 (single-key-description char
)
433 (char-to-string char
)))))
435 (let ((tmp-buf (generate-new-buffer " *text-props*")))
438 (describe-text-properties pos tmp-buf
)
439 (with-current-buffer tmp-buf
(buffer-string)))
440 (kill-buffer tmp-buf
))))
441 item-list max-width unicode
)
444 (memq 'mule-utf-8
(find-coding-systems-region pos
(1+ pos
)))
445 (get-char-property pos
'untranslated-utf-8
))
446 (setq unicode
(or (get-char-property pos
'untranslated-utf-8
)
447 (encode-char char
'ucs
))))
450 ,(format "%s (%d, #o%o, #x%x%s)"
451 (apply 'propertize char-description
452 (text-properties-at pos
))
455 (format ", U+%04X" unicode
)
458 ,`(insert-text-button
459 ,(symbol-name charset
)
460 'type
'help-character-set
'help-args
'(,charset
))
461 ,(format "(%s)" (charset-description charset
)))
463 ,(let ((split (split-char char
)))
465 ,(if (= (charset-dimension charset
) 1)
466 (format "#x%02X" (nth 1 split
))
467 (format "#x%02X #x%02X" (nth 1 split
)
469 'action
(lambda (&rest ignore
)
470 (list-charset-chars ',charset
)
471 (with-selected-window
472 (get-buffer-window "*Character List*" 0)
473 (goto-char (point-min))
474 (forward-line 2) ;Skip the header.
475 (let ((case-fold-search nil
))
476 (search-forward ,(char-to-string char
)
479 "mouse-2, RET: show this character in its character set")))
481 ,(let ((syntax (syntax-after pos
)))
483 (internal-describe-syntax-value syntax
)
486 ,@(let ((category-set (char-category-set char
)))
487 (if (not category-set
)
489 (mapcar #'(lambda (x) (format "%c:%s"
490 x
(category-docstring x
)))
491 (category-set-mnemonics category-set
)))))
492 ,@(let ((props (aref char-code-property-table char
))
496 (push (format "%s:" (pop props
)) ps
)
497 (push (format "%s;" (pop props
)) ps
))
498 (list (cons "Properties" (nreverse ps
)))))
500 ,@(let ((key-list (and (eq input-method-function
502 (quail-find-key char
))))
505 (mapconcat #'(lambda (x) (concat "\"" x
"\""))
509 ,current-input-method
510 'type
'help-input-method
511 'help-args
'(,current-input-method
))))))
513 ,(encoded-string-description
514 (string-as-unibyte (char-to-string char
)) nil
))
516 ,@(let* ((coding buffer-file-coding-system
)
517 (encoded (encode-coding-char char coding
)))
519 (list (encoded-string-description encoded coding
)
520 (format "(encoded by coding system %S)" coding
))
521 (list "not encodable by coding system"
522 (symbol-name coding
)))))
526 (setq disp-vector
(copy-sequence disp-vector
))
527 (dotimes (i (length disp-vector
))
528 (setq char
(aref disp-vector i
))
530 (cons char
(describe-char-display
531 pos
(glyph-char char
)))))
532 (format "by display table entry [%s] (see below)"
535 (format "?%c" (glyph-char (car x
))))
538 (let ((from (car composition
))
539 (to (nth 1 composition
))
541 (components (nth 2 composition
))
544 (and (< from pos
) (buffer-substring from pos
)))
545 (setcar (cdr composition
)
546 (and (< next to
) (buffer-substring next to
)))
547 (dotimes (i (length components
))
548 (if (integerp (setq ch
(aref components i
)))
549 (push (cons ch
(describe-char-display pos ch
))
551 (setq component-chars
(nreverse component-chars
))
552 (format "composed to form \"%s\" (see below)"
553 (buffer-substring from to
))))
555 (let ((display (describe-char-display pos char
)))
556 (if (display-graphic-p (selected-frame))
559 "by this font (glyph code)\n"
560 (format " %s (#x%02X)"
561 (car display
) (cdr display
)))
564 (format "terminal code %s" display
)
565 "not encodable for terminal"))))))
567 (if (not (or disp-vector composition
))
569 ((and show-trailing-whitespace
570 (save-excursion (goto-char pos
)
571 (looking-at "[ \t]+$")))
572 'trailing-whitespace
)
573 ((and nobreak-char-display unicode
(eq unicode
'#xa0
))
575 ((and nobreak-char-display unicode
(eq unicode
'#xad
))
577 ((and (< char
32) (not (memq char
'(9 10))))
579 (if face
(list (list "hardcoded face"
582 'type
'help-face
'help-args
'(,face
))))))
583 ,@(let ((unicodedata (and unicode
584 (describe-char-unicode-data unicode
))))
586 (cons (list "Unicode data" " ") unicodedata
)))))
587 (setq max-width
(apply #'max
(mapcar #'(lambda (x)
588 (if (cadr x
) (length (car x
)) 0))
590 (help-setup-xref nil
(interactive-p))
591 (with-help-window (help-buffer)
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 (if (eq (car-safe clm
) 'insert-text-button
)
600 (progn (insert " ") (eval clm
))
601 (when (>= (+ (current-column)
602 (or (string-match "\n" clm
)
607 (indent-to (1+ max-width
)))
613 (goto-char (point-min))
614 (re-search-forward "character:[ \t\n]+")
615 (let* ((end (+ (point) (length char-description
))))
616 (mapc #'(lambda (props)
617 (let ((o (make-overlay (point) end
)))
619 (overlay-put o
(car props
) (nth 1 props
))
620 (setq props
(cddr props
)))))
625 "\nThe display table entry is displayed by ")
626 (if (display-graphic-p (selected-frame))
628 (insert "these fonts (glyph codes):\n")
629 (dotimes (i (length disp-vector
))
630 (insert (glyph-char (car (aref disp-vector i
))) ?
:
631 (propertize " " 'display
'(space :align-to
5))
632 (if (cdr (aref disp-vector i
))
633 (format "%s (#x%02X)" (cadr (aref disp-vector i
))
634 (cddr (aref disp-vector i
)))
637 (let ((face (glyph-face (car (aref disp-vector i
)))))
639 (insert (propertize " " 'display
'(space :align-to
5))
641 (insert (concat "`" (symbol-name face
) "'"))
643 (insert "these terminal codes:\n")
644 (dotimes (i (length disp-vector
))
645 (insert (car (aref disp-vector i
))
646 (propertize " " 'display
'(space :align-to
5))
647 (or (cdr (aref disp-vector i
)) "-- not encodable --")
651 (insert "\nComposed")
652 (if (car composition
)
653 (if (cadr composition
)
654 (insert " with the surrounding characters \""
655 (car composition
) "\" and \""
656 (cadr composition
) "\"")
657 (insert " with the preceding character(s) \""
658 (car composition
) "\""))
659 (if (cadr composition
)
660 (insert " with the following character(s) \""
661 (cadr composition
) "\"")))
662 (insert " by the rule:\n\t("
663 (mapconcat (lambda (x)
664 (format (if (consp x
) "%S" "?%c") x
))
668 (insert "\nThe component character(s) are displayed by ")
669 (if (display-graphic-p (selected-frame))
671 (insert "these fonts (glyph codes):")
672 (dolist (elt component-chars
)
673 (insert "\n " (car elt
) ?
:
674 (propertize " " 'display
'(space :align-to
5))
676 (format "%s (#x%02X)" (cadr elt
) (cddr elt
))
678 (insert "these terminal codes:")
679 (dolist (elt component-chars
)
680 (insert "\n " (car elt
) ":"
681 (propertize " " 'display
'(space :align-to
5))
682 (or (cdr elt
) "-- not encodable --"))))
683 (insert "\nSee the variable `reference-point-alist' for "
684 "the meaning of the rule.\n"))
686 (if text-props-desc
(insert text-props-desc
))
687 (setq help-xref-stack-item
(list 'help-insert-string
(buffer-string)))
688 (toggle-read-only 1)))))
690 (defalias 'describe-char-after
'describe-char
)
691 (make-obsolete 'describe-char-after
'describe-char
"22.1")
693 (provide 'descr-text
)
695 ;; arch-tag: fc55a498-f3e9-4312-b5bd-98cc02480af1
696 ;;; descr-text.el ends here