1 ;;; descr-text.el --- describe text mode
3 ;; Copyright (C) 1994, 1995, 1996, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Boris Goldowsky <boris@gnu.org>
7 ;; Keywords: faces, i18n, Unicode, multilingual
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;;; Describe-Text Mode.
32 (eval-when-compile (require 'button
) (require 'quail
))
34 (defun describe-text-done ()
35 "Delete the current window or bury the current buffer."
37 (if (> (count-windows) 1)
41 (defvar describe-text-mode-map
42 (let ((map (make-sparse-keymap)))
43 (set-keymap-parent map widget-keymap
)
45 "Keymap for `describe-text-mode'.")
47 (defcustom describe-text-mode-hook nil
48 "List of hook functions ran by `describe-text-mode'."
52 (defun describe-text-mode ()
53 "Major mode for buffers created by `describe-char'.
55 \\{describe-text-mode-map}
56 Entry to this mode calls the value of `describe-text-mode-hook'
57 if that value is non-nil."
58 (kill-all-local-variables)
59 (setq major-mode
'describe-text-mode
60 mode-name
"Describe-Text")
61 (use-local-map describe-text-mode-map
)
63 (add-hook 'change-major-mode-hook
'font-lock-defontify nil t
)
64 (run-mode-hooks 'describe-text-mode-hook
))
66 ;;; Describe-Text Utilities.
68 (defun describe-text-widget (widget)
69 "Insert text to describe WIDGET in the current buffer."
71 :notify
`(lambda (&rest ignore
)
72 (widget-browse ',widget
))
73 (format "%S" (if (symbolp widget
)
77 (widget-create 'info-link
:tag
"widget" "(widget)Top"))
79 (defun describe-text-sexp (sexp)
80 "Insert a short description of SEXP in the current buffer."
81 (let ((pp (condition-case signal
83 (error (prin1-to-string signal
)))))
84 (when (string-match "\n\\'" pp
)
85 (setq pp
(substring pp
0 (1- (length pp
)))))
86 (if (cond ((string-match "\n" pp
)
88 ((> (length pp
) (- (window-width) (current-column)))
92 (widget-create 'push-button
94 :action
(lambda (widget &optional event
)
95 (with-output-to-temp-buffer
97 (princ (widget-get widget
:value
))))
100 (defun describe-property-list (properties)
101 "Insert a description of PROPERTIES in the current buffer.
102 PROPERTIES should be a list of overlay or text properties.
103 The `category', `face' and `font-lock-face' properties are made
104 into widget buttons that call `describe-text-category' or
105 `describe-face' when pushed."
106 ;; Sort the properties by the size of their value.
107 (dolist (elt (sort (let (ret)
109 (push (list (pop properties
) (pop properties
)) ret
))
111 (lambda (a b
) (string< (prin1-to-string (nth 0 a
) t
)
112 (prin1-to-string (nth 0 b
) t
)))))
113 (let ((key (nth 0 elt
))
115 (widget-insert (propertize (format " %-20s " key
)
116 'font-lock-face
'italic
))
117 (cond ((eq key
'category
)
119 :notify
`(lambda (&rest ignore
)
120 (describe-text-category ',value
))
121 (format "%S" value
)))
122 ((memq key
'(face font-lock-face mouse-face
))
124 :notify
`(lambda (&rest ignore
)
125 (describe-face ',value
))
126 (format "%S" value
)))
128 (describe-text-widget value
))
130 (describe-text-sexp value
))))
131 (widget-insert "\n")))
133 ;;; Describe-Text Commands.
135 (defun describe-text-category (category)
136 "Describe a text property category."
139 (with-output-to-temp-buffer "*Help*"
140 (set-buffer standard-output
)
141 (widget-insert "Category " (format "%S" category
) ":\n\n")
142 (describe-property-list (symbol-plist category
))
144 (goto-char (point-min)))))
147 (defun describe-text-properties (pos &optional output-buffer
)
148 "Describe widgets, buttons, overlays and text properties at POS.
149 Interactively, describe them for the character after point.
150 If optional second argument OUTPUT-BUFFER is non-nil,
151 insert the output into that buffer, and don't initialize or clear it
154 (if (>= pos
(point-max))
155 (error "No character follows specified position"))
157 (describe-text-properties-1 pos output-buffer
)
158 (if (not (or (text-properties-at pos
) (overlays-at pos
)))
159 (message "This is plain text.")
160 (let ((buffer (current-buffer))
161 (target-buffer "*Help*"))
162 (when (eq buffer
(get-buffer target-buffer
))
163 (setq target-buffer
"*Help-2*"))
165 (with-output-to-temp-buffer target-buffer
166 (set-buffer standard-output
)
167 (setq output-buffer
(current-buffer))
168 (widget-insert "Text content at position " (format "%d" pos
) ":\n\n")
169 (with-current-buffer buffer
170 (describe-text-properties-1 pos output-buffer
))
172 (goto-char (point-min))))))))
174 (defun describe-text-properties-1 (pos output-buffer
)
175 (let* ((properties (text-properties-at pos
))
176 (overlays (overlays-at pos
))
177 (wid-field (get-char-property pos
'field
))
178 (wid-button (get-char-property pos
'button
))
179 (wid-doc (get-char-property pos
'widget-doc
))
180 ;; If button.el is not loaded, we have no buttons in the text.
181 (button (and (fboundp 'button-at
) (button-at pos
)))
182 (button-type (and button
(button-type button
)))
183 (button-label (and button
(button-label button
)))
184 (widget (or wid-field wid-button wid-doc
)))
185 (with-current-buffer output-buffer
187 (when (widgetp widget
)
189 (widget-insert (cond (wid-field "This is an editable text area")
190 (wid-button "This is an active area")
191 (wid-doc "This is documentation text")))
192 (widget-insert " of a ")
193 (describe-text-widget widget
)
194 (widget-insert ".\n\n"))
196 (when (and button
(not (widgetp wid-button
)))
198 (widget-insert "Here is a " (format "%S" button-type
)
199 " button labeled `" button-label
"'.\n\n"))
203 (if (eq (length overlays
) 1)
204 (widget-insert "There is an overlay here:\n")
205 (widget-insert "There are " (format "%d" (length overlays
))
206 " overlays here:\n"))
207 (dolist (overlay overlays
)
208 (widget-insert " From " (format "%d" (overlay-start overlay
))
209 " to " (format "%d" (overlay-end overlay
)) "\n")
210 (describe-property-list (overlay-properties overlay
)))
211 (widget-insert "\n"))
215 (widget-insert "There are text properties here:\n")
216 (describe-property-list properties
)))))
218 (defcustom describe-char-unicodedata-file nil
219 "Location of Unicode data file.
220 This is the UnicodeData.txt file from the Unicode consortium, used for
221 diagnostics. If it is non-nil `describe-char' will print data
222 looked up from it. This facility is mostly of use to people doing
223 multilingual development.
225 This is a fairly large file, not typically present on GNU systems. At
226 the time of writing it is at
227 <URL:http://www.unicode.org/Public/UNIDATA/UnicodeData.txt>."
230 :type
'(choice (const :tag
"None" nil
)
233 ;; We could convert the unidata file into a Lispy form once-for-all
234 ;; and distribute it for loading on demand. It might be made more
235 ;; space-efficient by splitting strings word-wise and replacing them
236 ;; with lists of symbols interned in a private obarray, e.g.
237 ;; "LATIN SMALL LETTER A" => '(LATIN SMALL LETTER A).
239 ;; Fixme: Check whether this needs updating for Unicode 4.
240 (defun describe-char-unicode-data (char)
241 "Return a list of Unicode data for unicode CHAR.
242 Each element is a list of a property description and the property value.
243 The list is null if CHAR isn't found in `describe-char-unicodedata-file'."
244 (when describe-char-unicodedata-file
245 (unless (file-exists-p describe-char-unicodedata-file
)
246 (error "`unicodedata-file' %s not found" describe-char-unicodedata-file
))
248 ;; Find file in fundamental mode to avoid, e.g. flyspell turned
249 ;; on for .txt. Don't use RAWFILE arg in case of DOS line endings.
250 (let ((auto-mode-alist))
251 (find-file-noselect describe-char-unicodedata-file
))
252 (goto-char (point-min))
253 (let ((hex (format "%04X" char
))
255 (if (re-search-forward (concat "^" hex
) nil t
)
257 ;; It's not listed explicitly. Look for ranges, e.g. CJK
258 ;; ideographs, and check whether it's in one of them.
259 (while (and (re-search-forward "^\\([^;]+\\);[^;]+First>;" nil t
)
261 (string-to-number (match-string 1) 16)))
264 (looking-at "^\\([^;]+\\);[^;]+Last>;")
267 (string-to-number (match-string 1) 16))))))
268 (if (and (>= char first
)
272 (let ((fields (mapcar (lambda (elt)
273 (if (> (length elt
) 0)
277 (line-beginning-position)
280 ;; The length depends on whether the last field was empty.
281 (unless (or (= 13 (length fields
))
282 (= 14 (length fields
)))
283 (error "Invalid contents in %s" describe-char-unicodedata-file
))
284 ;; The field names and values lists are slightly
285 ;; modified from Mule-UCS unidata.el.
287 (list "Name" (let ((name (nth 0 fields
)))
288 ;; Check for <..., First>, <..., Last>
289 (if (string-match "\\`\\(<[^,]+\\)," name
)
290 (concat (match-string 1 name
) ">")
295 '(("Lu" .
"uppercase letter")
296 ("Ll" .
"lowercase letter")
297 ("Lt" .
"titlecase letter")
298 ("Mn" .
"non-spacing mark")
299 ("Mc" .
"spacing-combining mark")
300 ("Me" .
"enclosing mark")
301 ("Nd" .
"decimal digit")
302 ("Nl" .
"letter number")
303 ("No" .
"other number")
304 ("Zs" .
"space separator")
305 ("Zl" .
"line separator")
306 ("Zp" .
"paragraph separator")
307 ("Cc" .
"other control")
308 ("Cf" .
"other format")
310 ("Co" .
"private use")
311 ("Cn" .
"not assigned")
312 ("Lm" .
"modifier letter")
313 ("Lo" .
"other letter")
314 ("Pc" .
"connector punctuation")
315 ("Pd" .
"dash punctuation")
316 ("Ps" .
"open punctuation")
317 ("Pe" .
"close punctuation")
318 ("Pi" .
"initial-quotation punctuation")
319 ("Pf" .
"final-quotation punctuation")
320 ("Po" .
"other punctuation")
321 ("Sm" .
"math symbol")
322 ("Sc" .
"currency symbol")
323 ("Sk" .
"modifier symbol")
324 ("So" .
"other symbol")))))
325 (list "Combining class"
327 (string-to-number (nth 2 fields
))
329 (1 .
"Overlays and interior")
331 (8 .
"Hiragana/Katakana voicing marks")
333 (10 .
"Start of fixed position classes")
334 (199 .
"End of fixed position classes")
335 (200 .
"Below left attached")
336 (202 .
"Below attached")
337 (204 .
"Below right attached")
338 (208 .
"Left attached (reordrant around \
339 single base character)")
340 (210 .
"Right attached")
341 (212 .
"Above left attached")
342 (214 .
"Above attached")
343 (216 .
"Above right attached")
346 (222 .
"Below right")
347 (224 .
"Left (reordrant around single base \
352 (232 .
"Above right")
353 (233 .
"Double below")
354 (234 .
"Double above")
355 (240 .
"Below (iota subscript)")))))
356 (list "Bidi category"
359 '(("L" .
"Left-to-Right")
360 ("LRE" .
"Left-to-Right Embedding")
361 ("LRO" .
"Left-to-Right Override")
362 ("R" .
"Right-to-Left")
363 ("AL" .
"Right-to-Left Arabic")
364 ("RLE" .
"Right-to-Left Embedding")
365 ("RLO" .
"Right-to-Left Override")
366 ("PDF" .
"Pop Directional Format")
367 ("EN" .
"European Number")
368 ("ES" .
"European Number Separator")
369 ("ET" .
"European Number Terminator")
370 ("AN" .
"Arabic Number")
371 ("CS" .
"Common Number Separator")
372 ("NSM" .
"Non-Spacing Mark")
373 ("BN" .
"Boundary Neutral")
374 ("B" .
"Paragraph Separator")
375 ("S" .
"Segment Separator")
376 ("WS" .
"Whitespace")
377 ("ON" .
"Other Neutrals")))))
381 (let* ((parts (split-string (nth 4 fields
)))
383 (if (string-match "\\`<\\(.+\\)>\\'" info
)
384 (setq info
(match-string 1 info
))
386 (if info
(setq parts
(cdr parts
)))
387 ;; Maybe printing ? for unrepresentable unicodes
388 ;; here and below should be changed?
389 (setq parts
(mapconcat
391 (string (or (decode-char
393 (string-to-number arg
16))
396 (concat info parts
))))
397 (list "Decimal digit value"
401 (list "Numeric value"
404 (if (equal "Y" (nth 8 fields
))
406 (list "Old name" (nth 9 fields
))
407 (list "ISO 10646 comment" (nth 10 fields
))
408 (list "Uppercase" (and (nth 11 fields
)
409 (string (or (decode-char
414 (list "Lowercase" (and (nth 12 fields
)
415 (string (or (decode-char
420 (list "Titlecase" (and (nth 13 fields
)
421 (string (or (decode-char
427 ;; Return information about how CHAR is displayed at the buffer
428 ;; position POS. If the selected frame is on a graphic display,
429 ;; return a cons (FONTNAME . GLYPH-CODE). Otherwise, return a string
430 ;; describing the terminal codes for the character.
431 (defun describe-char-display (pos char
)
432 (if (display-graphic-p (selected-frame))
433 (internal-char-font pos char
)
434 (let* ((coding (terminal-coding-system))
435 (encoded (encode-coding-char char coding
)))
437 (encoded-string-description encoded coding
)))))
441 (defun describe-char (pos)
442 "Describe the character after POS (interactively, the character after point).
443 The information includes character code, charset and code points in it,
444 syntax, category, how the character is encoded in a file,
445 character composition information (if relevant),
446 as well as widgets, buttons, overlays, and text properties."
448 (if (>= pos
(point-max))
449 (error "No character follows specified position"))
450 (let* ((char (char-after pos
))
451 (charset (char-charset char
))
452 (composition (find-composition pos nil nil t
))
453 (component-chars nil
)
454 (display-table (or (window-display-table)
456 standard-display-table
))
457 (disp-vector (and display-table
(aref display-table char
)))
458 (multibyte-p enable-multibyte-characters
)
459 (overlays (mapcar #'(lambda (o) (overlay-properties o
))
461 item-list max-width unicode
)
464 (memq 'mule-utf-8
(find-coding-systems-region pos
(1+ pos
)))
465 (get-char-property pos
'untranslated-utf-8
))
466 (setq unicode
(or (get-char-property pos
'untranslated-utf-8
)
467 (encode-char char
'ucs
))))
470 ,(format "%s (0%o, %d, 0x%x%s)"
471 (apply 'propertize
(if (not multibyte-p
)
472 (single-key-description char
)
474 (single-key-description char
)
476 (char-to-string char
))))
477 (text-properties-at pos
))
480 (format ", U+%04X" unicode
)
483 ,`(widget-create 'link
484 :notify
(lambda (&rest ignore
)
485 (describe-character-set ',charset
))
486 ,(symbol-name charset
))
487 ,(format "(%s)" (charset-description charset
)))
489 ,(let ((split (split-char char
)))
492 :notify
(lambda (&rest ignore
)
493 (list-charset-chars ',charset
)
494 (with-selected-window
495 (get-buffer-window "*Character List*" 0)
496 (goto-char (point-min))
497 (forward-line 2) ;Skip the header.
498 (let ((case-fold-search nil
))
499 (search-forward ,(char-to-string char
)
501 ,(if (= (charset-dimension charset
) 1)
502 (format "%d" (nth 1 split
))
503 (format "%d %d" (nth 1 split
) (nth 2 split
))))))
505 ,(let ((syntax (syntax-after pos
)))
507 (internal-describe-syntax-value syntax
)
510 ,@(let ((category-set (char-category-set char
)))
511 (if (not category-set
)
513 (mapcar #'(lambda (x) (format "%c:%s "
514 x
(category-docstring x
)))
515 (category-set-mnemonics category-set
)))))
516 ,@(let ((props (aref char-code-property-table char
))
520 (push (format "%s:" (pop props
)) ps
)
521 (push (format "%s;" (pop props
)) ps
))
522 (list (cons "Properties" (nreverse ps
)))))
524 ,@(let ((key-list (and (eq input-method-function
526 (quail-find-key char
))))
529 (mapconcat #'(lambda (x) (concat "\"" x
"\""))
534 :notify
(lambda (&rest ignore
)
535 (describe-input-method
536 ',current-input-method
))
537 ,(format "%s" current-input-method
))))))
539 ,(encoded-string-description
540 (string-as-unibyte (char-to-string char
)) nil
))
542 ,@(let* ((coding buffer-file-coding-system
)
543 (encoded (encode-coding-char char coding
)))
545 (list (encoded-string-description encoded coding
)
546 (format "(encoded by coding system %S)" coding
))
547 (list "not encodable by coding system"
548 (symbol-name coding
)))))
552 (setq disp-vector
(copy-sequence disp-vector
))
553 (dotimes (i (length disp-vector
))
554 (setq char
(aref disp-vector i
))
556 (cons char
(describe-char-display
557 pos
(logand char
#x7ffff
)))))
558 (format "by display table entry [%s] (see below)"
561 (format "?%c" (logand (car x
) #x7ffff
)))
564 (let ((from (car composition
))
565 (to (nth 1 composition
))
567 (components (nth 2 composition
))
570 (and (< from pos
) (buffer-substring from pos
)))
571 (setcar (cdr composition
)
572 (and (< next to
) (buffer-substring next to
)))
573 (dotimes (i (length components
))
574 (if (integerp (setq ch
(aref components i
)))
575 (push (cons ch
(describe-char-display pos ch
))
577 (setq component-chars
(nreverse component-chars
))
578 (format "composed to form \"%s\" (see below)"
579 (buffer-substring from to
))))
581 (let ((display (describe-char-display pos char
)))
582 (if (display-graphic-p (selected-frame))
585 "by this font (glyph code)\n"
586 (format " %s (0x%02X)"
587 (car display
) (cdr display
)))
590 (format "terminal code %s" display
)
591 "not encodable for terminal"))))))
593 (if (not (or disp-vector composition
))
595 ((and show-trailing-whitespace
596 (save-excursion (goto-char pos
)
597 (looking-at "[ \t]+$")))
598 'trailing-whitespace
)
599 ((and nobreak-char-display unicode
(eq unicode
'#xa0
))
601 ((and nobreak-char-display unicode
(eq unicode
'#xad
))
603 ((and (< char
32) (not (memq char
'(9 10))))
605 (if face
(list (list "hardcoded face"
608 :notify
(lambda (&rest ignore
)
609 (describe-face ',face
))
610 ,(format "%s" face
))))))
611 ,@(let ((unicodedata (and unicode
612 (describe-char-unicode-data unicode
))))
614 (cons (list "Unicode data" " ") unicodedata
)))))
615 (setq max-width
(apply #'max
(mapcar #'(lambda (x)
616 (if (cadr x
) (length (car x
)) 0))
618 (with-output-to-temp-buffer "*Help*"
619 (with-current-buffer standard-output
620 (set-buffer-multibyte multibyte-p
)
621 (let ((formatter (format "%%%ds:" max-width
)))
622 (dolist (elt item-list
)
624 (insert (format formatter
(car elt
)))
625 (dolist (clm (cdr elt
))
626 (if (eq (car-safe clm
) 'widget-create
)
627 (progn (insert " ") (eval clm
))
628 (when (>= (+ (current-column)
629 (or (string-match "\n" clm
)
634 (indent-to (1+ max-width
)))
639 (goto-char (point-min))
640 (re-search-forward "character:[ \t\n]+")
643 (mapc #'(lambda (props)
644 (let ((o (make-overlay pos
(1+ pos
))))
646 (overlay-put o
(car props
) (nth 1 props
))
647 (setq props
(cddr props
)))))
652 "\nThe display table entry is displayed by ")
653 (if (display-graphic-p (selected-frame))
655 (insert "these fonts (glyph codes):\n")
656 (dotimes (i (length disp-vector
))
657 (insert (logand (car (aref disp-vector i
)) #x7ffff
) ?
:
658 (propertize " " 'display
'(space :align-to
5))
659 (if (cdr (aref disp-vector i
))
660 (format "%s (0x%02X)" (cadr (aref disp-vector i
))
661 (cddr (aref disp-vector i
)))
664 (when (> (car (aref disp-vector i
)) #x7ffff
)
665 (let* ((face-id (lsh (car (aref disp-vector i
)) -
19))
666 (face (car (delq nil
(mapcar (lambda (face)
667 (and (eq (face-id face
)
671 (insert (propertize " " 'display
'(space :align-to
5))
674 :notify
`(lambda (&rest ignore
)
675 (describe-face ',face
))
678 (insert "these terminal codes:\n")
679 (dotimes (i (length disp-vector
))
680 (insert (car (aref disp-vector i
))
681 (propertize " " 'display
'(space :align-to
5))
682 (or (cdr (aref disp-vector i
)) "-- not encodable --")
686 (insert "\nComposed")
687 (if (car composition
)
688 (if (cadr composition
)
689 (insert " with the surrounding characters \""
690 (car composition
) "\" and \""
691 (cadr composition
) "\"")
692 (insert " with the preceding character(s) \""
693 (car composition
) "\""))
694 (if (cadr composition
)
695 (insert " with the following character(s) \""
696 (cadr composition
) "\"")))
697 (insert " by the rule:\n\t("
698 (mapconcat (lambda (x)
699 (format (if (consp x
) "%S" "?%c") x
))
703 (insert "\nThe component character(s) are displayed by ")
704 (if (display-graphic-p (selected-frame))
706 (insert "these fonts (glyph codes):")
707 (dolist (elt component-chars
)
708 (insert "\n " (car elt
) ?
:
709 (propertize " " 'display
'(space :align-to
5))
711 (format "%s (0x%02X)" (cadr elt
) (cddr elt
))
713 (insert "these terminal codes:")
714 (dolist (elt component-chars
)
715 (insert "\n " (car elt
) ":"
716 (propertize " " 'display
'(space :align-to
5))
717 (or (cdr elt
) "-- not encodable --"))))
718 (insert "\nSee the variable `reference-point-alist' for "
719 "the meaning of the rule.\n"))
721 (describe-text-properties pos
(current-buffer))
722 (describe-text-mode)))))
724 (defalias 'describe-char-after
'describe-char
)
725 (make-obsolete 'describe-char-after
'describe-char
"22.1")
727 (provide 'descr-text
)
729 ;; arch-tag: fc55a498-f3e9-4312-b5bd-98cc02480af1
730 ;;; descr-text.el ends here