1 ;;; descr-text.el --- describe text mode
3 ;; Copyright (C) 1994, 1995, 1996, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009 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 of the License, or
15 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Describe-Text Mode.
31 (eval-when-compile (require 'quail
))
34 ;;; Describe-Text Utilities.
36 (defun describe-text-widget (widget)
37 "Insert text to describe WIDGET in the current buffer."
39 (symbol-name (if (symbolp widget
) widget
(car widget
)))
40 'action
`(lambda (&rest ignore
)
41 (widget-browse ',widget
))
42 'help-echo
"mouse-2, RET: browse this widget")
45 "(widget)Top" 'type
'help-info
'help-args
'("(widget)Top")))
47 (defun describe-text-sexp (sexp)
48 "Insert a short description of SEXP in the current buffer."
49 (let ((pp (condition-case signal
51 (error (prin1-to-string signal
)))))
52 (when (string-match-p "\n\\'" pp
)
53 (setq pp
(substring pp
0 (1- (length pp
)))))
55 (if (and (not (string-match-p "\n" pp
))
56 (<= (length pp
) (- (window-width) (current-column))))
59 "[Show]" 'action
`(lambda (&rest ignore
)
60 (with-output-to-temp-buffer
63 'help-echo
"mouse-2, RET: pretty print value in another buffer"))))
65 (defun describe-property-list (properties)
66 "Insert a description of PROPERTIES in the current buffer.
67 PROPERTIES should be a list of overlay or text properties.
68 The `category', `face' and `font-lock-face' properties are made
69 into help buttons that call `describe-text-category' or
70 `describe-face' when pushed."
71 ;; Sort the properties by the size of their value.
72 (dolist (elt (sort (let (ret)
74 (push (list (pop properties
) (pop properties
)) ret
))
76 (lambda (a b
) (string< (prin1-to-string (nth 0 a
) t
)
77 (prin1-to-string (nth 0 b
) t
)))))
78 (let ((key (nth 0 elt
))
80 (insert (propertize (format " %-20s " key
)
81 'face
'help-argument-name
))
82 (cond ((eq key
'category
)
85 'action
`(lambda (&rest ignore
)
86 (describe-text-category ',value
))
87 'help-echo
"mouse-2, RET: describe this category"))
88 ((memq key
'(face font-lock-face mouse-face
))
91 'type
'help-face
'help-args
(list value
)))
93 (describe-text-widget value
))
95 (describe-text-sexp value
))))
98 ;;; Describe-Text Commands.
100 (defun describe-text-category (category)
101 "Describe a text property category."
102 (interactive "SCategory: ")
103 (help-setup-xref (list #'describe-text-category category
) (interactive-p))
105 (with-output-to-temp-buffer "*Help*"
106 (set-buffer standard-output
)
107 (insert "Category " (format "%S" category
) ":\n\n")
108 (describe-property-list (symbol-plist category
))
109 (goto-char (point-min)))))
112 (defun describe-text-properties (pos &optional output-buffer
)
113 "Describe widgets, buttons, overlays and text properties at POS.
114 Interactively, describe them for the character after point.
115 If optional second argument OUTPUT-BUFFER is non-nil,
116 insert the output into that buffer, and don't initialize or clear it
119 (if (>= pos
(point-max))
120 (error "No character follows specified position"))
122 (describe-text-properties-1 pos output-buffer
)
123 (if (not (or (text-properties-at pos
) (overlays-at pos
)))
124 (message "This is plain text.")
125 (let ((buffer (current-buffer))
126 (target-buffer "*Help*"))
127 (when (eq buffer
(get-buffer target-buffer
))
128 (setq target-buffer
"*Help*<2>"))
130 (with-output-to-temp-buffer target-buffer
131 (set-buffer standard-output
)
132 (setq output-buffer
(current-buffer))
133 (insert "Text content at position " (format "%d" pos
) ":\n\n")
134 (with-current-buffer buffer
135 (describe-text-properties-1 pos output-buffer
))
136 (goto-char (point-min))))))))
138 (defun describe-text-properties-1 (pos output-buffer
)
139 (let* ((properties (text-properties-at pos
))
140 (overlays (overlays-at pos
))
141 (wid-field (get-char-property pos
'field
))
142 (wid-button (get-char-property pos
'button
))
143 (wid-doc (get-char-property pos
'widget-doc
))
144 ;; If button.el is not loaded, we have no buttons in the text.
145 (button (and (fboundp 'button-at
) (button-at pos
)))
146 (button-type (and button
(button-type button
)))
147 (button-label (and button
(button-label button
)))
148 (widget (or wid-field wid-button wid-doc
)))
149 (with-current-buffer output-buffer
151 (when (widgetp widget
)
153 (insert (cond (wid-field "This is an editable text area")
154 (wid-button "This is an active area")
155 (wid-doc "This is documentation text")))
157 (describe-text-widget widget
)
160 (when (and button
(not (widgetp wid-button
)))
162 (insert "Here is a `" (format "%S" button-type
)
163 "' button labeled `" button-label
"'.\n\n"))
167 (if (eq (length overlays
) 1)
168 (insert "There is an overlay here:\n")
169 (insert "There are " (format "%d" (length overlays
))
170 " overlays here:\n"))
171 (dolist (overlay overlays
)
172 (insert " From " (format "%d" (overlay-start overlay
))
173 " to " (format "%d" (overlay-end overlay
)) "\n")
174 (describe-property-list (overlay-properties overlay
)))
179 (insert "There are text properties here:\n")
180 (describe-property-list properties
)))))
182 (defcustom describe-char-unidata-list
183 '(name old-name general-category decomposition
)
184 "List of Unicode-based character property names shown by `describe-char'."
187 :type
'(choice (const :tag
"All properties" t
)
189 (const :tag
"Unicode Name" name
)
190 (const :tag
"Unicode old name" old-name
)
191 (const :tag
"Unicode general category " general-category
)
192 (const :tag
"Unicode canonical combining class"
193 canonical-combining-class
)
194 (const :tag
"Unicode bidi class" bidi-class
)
195 (const :tag
"Unicode decomposition mapping" decomposition
)
196 (const :tag
"Unicode decimal digit value" decimal-digit-value
)
197 (const :tag
"Unicode digit value" digit-value
)
198 (const :tag
"Unicode numeric value" numeric-value
)
199 (const :tag
"Unicode mirrored" mirrored
)
200 (const :tag
"Unicode ISO 10646 comment" iso-10646-comment
)
201 (const :tag
"Unicode simple uppercase mapping" uppercase
)
202 (const :tag
"Unicode simple lowercase mapping" lowercase
)
203 (const :tag
"Unicode simple titlecase mapping" titlecase
))))
205 (defcustom describe-char-unicodedata-file nil
206 "Location of Unicode data file.
207 This is the UnicodeData.txt file from the Unicode Consortium, used for
208 diagnostics. If it is non-nil `describe-char' will print data
209 looked up from it. This facility is mostly of use to people doing
210 multilingual development.
212 This is a fairly large file, not typically present on GNU systems.
213 At the time of writing it is at the URL
214 `http://www.unicode.org/Public/UNIDATA/UnicodeData.txt'."
217 :type
'(choice (const :tag
"None" nil
)
220 (defun describe-char-unicode-data (char)
221 "Return a list of Unicode data for unicode CHAR.
222 Each element is a list of a property description and the property value.
223 The list is null if CHAR isn't found in `describe-char-unicodedata-file'.
224 This function is semi-obsolete. Use `get-char-code-property'."
225 (when describe-char-unicodedata-file
226 (unless (file-exists-p describe-char-unicodedata-file
)
227 (error "`unicodedata-file' %s not found" describe-char-unicodedata-file
))
228 (with-current-buffer (get-buffer-create " *Unicode Data*")
229 (when (zerop (buffer-size))
230 ;; Don't use -literally in case of DOS line endings.
231 (insert-file-contents describe-char-unicodedata-file
))
232 (goto-char (point-min))
233 (let ((hex (format "%04X" char
))
235 (if (re-search-forward (concat "^" hex
) nil t
)
237 ;; It's not listed explicitly. Look for ranges, e.g. CJK
238 ;; ideographs, and check whether it's in one of them.
239 (while (and (re-search-forward "^\\([^;]+\\);[^;]+First>;" nil t
)
241 (string-to-number (match-string 1) 16)))
244 (looking-at "^\\([^;]+\\);[^;]+Last>;")
247 (string-to-number (match-string 1) 16))))))
248 (if (and (>= char first
)
252 (let ((fields (mapcar (lambda (elt)
253 (if (> (length elt
) 0)
257 (line-beginning-position)
260 ;; The length depends on whether the last field was empty.
261 (unless (or (= 13 (length fields
))
262 (= 14 (length fields
)))
263 (error "Invalid contents in %s" describe-char-unicodedata-file
))
264 ;; The field names and values lists are slightly
265 ;; modified from Mule-UCS unidata.el.
267 (list "Name" (let ((name (nth 0 fields
)))
268 ;; Check for <..., First>, <..., Last>
269 (if (string-match "\\`\\(<[^,]+\\)," name
)
270 (concat (match-string 1 name
) ">")
273 (let ((val (nth 1 fields
)))
274 (or (char-code-property-description
275 'general-category
(intern val
))
277 (list "Combining class"
278 (let ((val (nth 1 fields
)))
279 (or (char-code-property-description
280 'canonical-combining-class
(intern val
))
282 (list "Bidi category"
283 (let ((val (nth 1 fields
)))
284 (or (char-code-property-description
285 'bidi-class
(intern val
))
290 (let* ((parts (split-string (nth 4 fields
)))
292 (if (string-match "\\`<\\(.+\\)>\\'" info
)
293 (setq info
(match-string 1 info
))
295 (if info
(setq parts
(cdr parts
)))
296 (setq parts
(mapconcat
298 (string (string-to-number arg
16)))
300 (concat info parts
))))
301 (list "Decimal digit value"
305 (list "Numeric value"
308 (if (equal "Y" (nth 8 fields
))
310 (list "Old name" (nth 9 fields
))
311 (list "ISO 10646 comment" (nth 10 fields
))
312 (list "Uppercase" (and (nth 11 fields
)
313 (string (string-to-number
314 (nth 11 fields
) 16))))
315 (list "Lowercase" (and (nth 12 fields
)
316 (string (string-to-number
317 (nth 12 fields
) 16))))
318 (list "Titlecase" (and (nth 13 fields
)
319 (string (string-to-number
320 (nth 13 fields
) 16)))))))))))
322 ;; Not defined on builds without X, but behind display-graphic-p.
323 (declare-function internal-char-font
"fontset.c" (position &optional ch
))
325 ;; Return information about how CHAR is displayed at the buffer
326 ;; position POS. If the selected frame is on a graphic display,
327 ;; return a string "FONT-DRIVER:FONT-NAME (GLYPH-CODE)" where:
328 ;; FONT-DRIVER is the font-driver name,
329 ;; FONT-NAME is the font name,
330 ;; GLYPH-CODE is a hexadigit string representing the glyph-ID.
331 ;; Otherwise, return a string describing the terminal codes for the
333 (defun describe-char-display (pos char
)
334 (if (display-graphic-p (selected-frame))
335 (let ((char-font-info (internal-char-font pos char
)))
337 (let ((type (font-get (car char-font-info
) :type
))
338 (name (font-xlfd-name (car char-font-info
)))
339 (code (cdr char-font-info
)))
341 (format "%s:%s (#x%02X)" type name code
)
342 (format "%s:%s (#x%04X%04X)"
343 type name
(car code
) (cdr code
))))))
344 (let* ((charset (get-text-property pos
'charset
))
345 (coding (terminal-coding-system))
346 (encoded (encode-coding-char char coding charset
)))
348 (encoded-string-description encoded coding
)))))
351 ;; Return a string of CH with composition for padding on both sides.
352 ;; It is displayed without overlapping with the left/right columns.
353 (defsubst describe-char-padded-string
(ch)
354 (compose-string (string ch
) 0 1 (format "\t%c\t" ch
)))
357 (defun describe-char (pos)
358 "Describe the character after POS (interactively, the character after point).
359 The information includes character code, charset and code points in it,
360 syntax, category, how the character is encoded in a file,
361 character composition information (if relevant),
362 as well as widgets, buttons, overlays, and text properties."
364 (if (>= pos
(point-max))
365 (error "No character follows specified position"))
366 (let* ((char (char-after pos
))
367 (charset (or (get-text-property pos
'charset
) (char-charset char
)))
368 (composition (find-composition pos nil nil t
))
369 (component-chars nil
)
370 (display-table (or (window-display-table)
372 standard-display-table
))
373 (disp-vector (and display-table
(aref display-table char
)))
374 (multibyte-p enable-multibyte-characters
)
375 (overlays (mapcar #'(lambda (o) (overlay-properties o
))
377 (char-description (if (not multibyte-p
)
378 (single-key-description char
)
380 (single-key-description char
)
382 (char-to-string char
)))))
384 (let ((tmp-buf (generate-new-buffer " *text-props*")))
387 (describe-text-properties pos tmp-buf
)
388 (with-current-buffer tmp-buf
(buffer-string)))
389 (kill-buffer tmp-buf
))))
390 item-list max-width code
)
392 (or (setq code
(encode-char char charset
))
393 (setq charset
(char-charset char
)
394 code
(encode-char char charset
)))
397 ,(format "%s (%d, #o%o, #x%x)"
398 (apply 'propertize char-description
399 (text-properties-at pos
))
402 ,`(insert-text-button
403 ,(symbol-name charset
)
404 'type
'help-character-set
'help-args
'(,charset
))
405 ,(format "(%s)" (charset-description charset
)))
407 ,(let ((str (if (integerp code
)
408 (format (if (< code
256) "0x%02X" "0x%04X") code
)
409 (format "0x%04X%04X" (car code
) (cdr code
)))))
410 (if (<= (charset-dimension charset
) 2)
413 'action
(lambda (&rest ignore
)
414 (list-charset-chars ',charset
)
415 (with-selected-window
416 (get-buffer-window "*Character List*" 0)
417 (goto-char (point-min))
418 (forward-line 2) ;Skip the header.
419 (let ((case-fold-search nil
))
420 (if (search-forward ,(char-to-string char
)
422 (goto-char (match-beginning 0))))))
424 "mouse-2, RET: show this character in its character set")
427 ,(let ((syntax (syntax-after pos
)))
429 (internal-describe-syntax-value syntax
)
432 ,@(let ((category-set (char-category-set char
)))
433 (if (not category-set
)
435 (mapcar #'(lambda (x) (format "%c:%s"
436 x
(category-docstring x
)))
437 (category-set-mnemonics category-set
)))))
439 ,@(let ((key-list (and (eq input-method-function
441 (quail-find-key char
))))
444 (mapconcat #'(lambda (x) (concat "\"" x
"\""))
448 ,current-input-method
449 'type
'help-input-method
450 'help-args
'(,current-input-method
))))))
452 ,(encoded-string-description
453 (string-as-unibyte (char-to-string char
)) nil
))
455 ,@(let* ((coding buffer-file-coding-system
)
456 (encoded (encode-coding-char char coding charset
)))
458 (list (encoded-string-description encoded coding
)
459 (format "(encoded by coding system %S)" coding
))
460 (list "not encodable by coding system"
461 (symbol-name coding
)))))
465 (setq disp-vector
(copy-sequence disp-vector
))
466 (dotimes (i (length disp-vector
))
468 (cons (aref disp-vector i
)
469 (describe-char-display
470 pos
(glyph-char (aref disp-vector i
))))))
471 (format "by display table entry [%s] (see below)"
474 (format "?%c" (glyph-char (car x
))))
477 (let ((from (car composition
))
478 (to (nth 1 composition
))
480 (components (nth 2 composition
))
483 (and (< from pos
) (buffer-substring from pos
)))
484 (setcar (cdr composition
)
485 (and (< next to
) (buffer-substring next to
)))
486 (dotimes (i (length components
))
487 (if (integerp (setq ch
(aref components i
)))
488 (push (cons ch
(describe-char-display pos ch
))
490 (setq component-chars
(nreverse component-chars
))
491 (format "composed to form \"%s\" (see below)"
492 (buffer-substring from to
))))
494 (let ((display (describe-char-display pos char
)))
495 (if (display-graphic-p (selected-frame))
497 (concat "by this font (glyph code)\n " display
)
500 (format "terminal code %s" display
)
501 "not encodable for terminal"))))))
503 (if (not (or disp-vector composition
))
505 ((and show-trailing-whitespace
506 (save-excursion (goto-char pos
)
507 (looking-at-p "[ \t]+$")))
508 'trailing-whitespace
)
509 ((and nobreak-char-display char
(eq char
'#xa0
))
511 ((and nobreak-char-display char
(eq char
'#xad
))
513 ((and (< char
32) (not (memq char
'(9 10))))
515 (if face
(list (list "hardcoded face"
518 'type
'help-face
'help-args
'(,face
))))))
519 ,@(let ((unicodedata (describe-char-unicode-data char
)))
521 (cons (list "Unicode data" " ") unicodedata
)))))
522 (setq max-width
(apply #'max
(mapcar #'(lambda (x)
523 (if (cadr x
) (length (car x
)) 0))
525 (help-setup-xref nil
(interactive-p))
526 (with-help-window (help-buffer)
527 (with-current-buffer standard-output
528 (set-buffer-multibyte multibyte-p
)
529 (let ((formatter (format "%%%ds:" max-width
)))
530 (dolist (elt item-list
)
532 (insert (format formatter
(car elt
)))
533 (dolist (clm (cdr elt
))
534 (if (eq (car-safe clm
) 'insert-text-button
)
535 (progn (insert " ") (eval clm
))
536 (when (>= (+ (current-column)
537 (or (string-match-p "\n" clm
)
542 (indent-to (1+ max-width
)))
548 (goto-char (point-min))
549 (re-search-forward "character:[ \t\n]+")
550 (let ((end (+ (point) (length char-description
))))
551 (mapc #'(lambda (props)
552 (let ((o (make-overlay (point) end
)))
554 (overlay-put o
(car props
) (nth 1 props
))
555 (setq props
(cddr props
)))))
560 "\nThe display table entry is displayed by ")
561 (if (display-graphic-p (selected-frame))
563 (insert "these fonts (glyph codes):\n")
564 (dotimes (i (length disp-vector
))
565 (insert (glyph-char (car (aref disp-vector i
))) ?
:
566 (propertize " " 'display
'(space :align-to
5))
567 (or (cdr (aref disp-vector i
)) "-- no font --")
569 (let ((face (glyph-face (car (aref disp-vector i
)))))
571 (insert (propertize " " 'display
'(space :align-to
5))
573 (insert (concat "`" (symbol-name face
) "'"))
575 (insert "these terminal codes:\n")
576 (dotimes (i (length disp-vector
))
577 (insert (car (aref disp-vector i
))
578 (propertize " " 'display
'(space :align-to
5))
579 (or (cdr (aref disp-vector i
)) "-- not encodable --")
583 (insert "\nComposed")
584 (if (car composition
)
585 (if (cadr composition
)
586 (insert " with the surrounding characters \""
587 (mapconcat 'describe-char-padded-string
588 (car composition
) "")
590 (mapconcat 'describe-char-padded-string
591 (cadr composition
) "")
593 (insert " with the preceding character(s) \""
594 (mapconcat 'describe-char-padded-string
595 (car composition
) "")
597 (if (cadr composition
)
598 (insert " with the following character(s) \""
599 (mapconcat 'describe-char-padded-string
600 (cadr composition
) "")
602 (if (and (vectorp (nth 2 composition
))
603 (vectorp (aref (nth 2 composition
) 0)))
604 (let* ((gstring (nth 2 composition
))
605 (font (lgstring-font gstring
))
606 (nglyphs (lgstring-glyph-len gstring
))
611 (insert " using this font:\n "
612 (symbol-name (font-get font
:type
))
614 (aref (query-font font
) 0)
615 "\nby these glyphs:\n")
616 (while (and (< i nglyphs
)
617 (setq glyph
(lgstring-glyph gstring i
)))
618 (insert (format " %S\n" glyph
))
620 (insert " by these characters:\n")
621 (while (and (< i nglyphs
)
622 (setq glyph
(lgstring-glyph gstring i
)))
623 (insert (format " %c (#x%d)\n"
624 (lglyph-char glyph
) (lglyph-char glyph
)))
626 (insert " by the rule:\n\t(")
629 (if first
(setq first nil
)
631 (if (consp x
) (insert (format "%S" x
))
632 (if (= x ?
\t) (insert (single-key-description x
))
634 (insert (describe-char-padded-string x
)))))
635 (nth 2 composition
)))
636 (insert ")\nThe component character(s) are displayed by ")
637 (if (display-graphic-p (selected-frame))
639 (insert "these fonts (glyph codes):")
640 (dolist (elt component-chars
)
641 (if (/= (car elt
) ?
\t)
643 (describe-char-padded-string (car elt
))
645 (propertize " " 'display
'(space :align-to
5))
646 (or (cdr elt
) "-- no font --")))))
647 (insert "these terminal codes:")
648 (dolist (elt component-chars
)
649 (insert "\n " (car elt
) ":"
650 (propertize " " 'display
'(space :align-to
4))
651 (or (cdr elt
) "-- not encodable --"))))
652 (insert "\nSee the variable `reference-point-alist' for "
653 "the meaning of the rule.\n")))
655 (insert (if (not describe-char-unidata-list
)
656 "\nCharacter code properties are not shown: "
657 "\nCharacter code properties: "))
659 "customize what to show"
660 'action
(lambda (&rest ignore
)
662 'describe-char-unidata-list
)))
664 (dolist (elt (if (eq describe-char-unidata-list t
)
665 (nreverse (mapcar 'car char-code-property-alist
))
666 describe-char-unidata-list
))
667 (let ((val (get-char-code-property char elt
))
670 (setq description
(char-code-property-description elt val
))
671 (insert (if description
672 (format " %s: %s (%s)\n" elt val description
)
673 (format " %s: %s\n" elt val
))))))
675 (if text-props-desc
(insert text-props-desc
))
676 (setq help-xref-stack-item
(list 'help-insert-string
(buffer-string)))
677 (toggle-read-only 1)))))
679 (define-obsolete-function-alias 'describe-char-after
'describe-char
"22.1")
681 (provide 'descr-text
)
683 ;; arch-tag: fc55a498-f3e9-4312-b5bd-98cc02480af1
684 ;;; descr-text.el ends here