Bind grep-highlight-matches around the rgrep call
[emacs.git] / lisp / descr-text.el
bloba0b9ddfe2c907ffc5c02bad862307f76743990cf
1 ;;; descr-text.el --- describe text mode -*- lexical-binding:t -*-
3 ;; Copyright (C) 1994-1996, 2001-2015 Free Software Foundation, Inc.
5 ;; Author: Boris Goldowsky <boris@gnu.org>
6 ;; Maintainer: emacs-devel@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 3 of the License, or
14 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Describe-Text Mode.
28 ;;; Code:
30 (eval-when-compile (require 'quail))
31 (require 'help-mode)
33 ;;; Describe-Text Utilities.
35 (defun describe-text-widget (widget)
36 "Insert text to describe WIDGET in the current buffer."
37 (insert-text-button
38 (symbol-name (if (symbolp widget) widget (car widget)))
39 'action (lambda (&rest _ignore) (widget-browse widget))
40 'help-echo "mouse-2, RET: browse this widget")
41 (insert " ")
42 (insert-text-button
43 "(widget)Top" 'type 'help-info 'help-args '("(widget)Top")))
45 (defun describe-text-sexp (sexp)
46 "Insert a short description of SEXP in the current buffer."
47 (let ((pp (condition-case signal
48 (pp-to-string sexp)
49 (error (prin1-to-string signal)))))
50 (when (string-match-p "\n\\'" pp)
51 (setq pp (substring pp 0 (1- (length pp)))))
53 (if (and (not (string-match-p "\n" pp))
54 (<= (length pp) (- (window-width) (current-column))))
55 (insert pp)
56 (insert-text-button
57 "[Show]" 'action (lambda (&rest _ignore)
58 (with-output-to-temp-buffer
59 "*Pp Eval Output*"
60 (princ pp)))
61 'help-echo "mouse-2, RET: pretty print value in another buffer"))))
63 (defun describe-property-list (properties)
64 "Insert a description of PROPERTIES in the current buffer.
65 PROPERTIES should be a list of overlay or text properties.
66 The `category', `face' and `font-lock-face' properties are made
67 into help buttons that call `describe-text-category' or
68 `describe-face' when pushed."
69 ;; Sort the properties by the size of their value.
70 (dolist (elt (sort (let (ret)
71 (while properties
72 (push (list (pop properties) (pop properties)) ret))
73 ret)
74 (lambda (a b) (string< (prin1-to-string (nth 0 a) t)
75 (prin1-to-string (nth 0 b) t)))))
76 (let ((key (nth 0 elt))
77 (value (nth 1 elt)))
78 (insert (propertize (format " %-20s " key)
79 'face 'help-argument-name))
80 (cond ((eq key 'category)
81 (insert-text-button
82 (symbol-name value)
83 'action (lambda (&rest _ignore)
84 (describe-text-category value))
85 'follow-link t
86 'help-echo "mouse-2, RET: describe this category"))
87 ((memq key '(face font-lock-face mouse-face))
88 (insert-text-button
89 (format "%S" value)
90 'type 'help-face 'help-args (list value)))
91 ((widgetp value)
92 (describe-text-widget value))
94 (describe-text-sexp value))))
95 (insert "\n")))
97 ;;; Describe-Text Commands.
99 (defun describe-text-category (category)
100 "Describe a text property category."
101 (interactive "SCategory: ")
102 (help-setup-xref (list #'describe-text-category category)
103 (called-interactively-p 'interactive))
104 (with-help-window (help-buffer)
105 (with-current-buffer standard-output
106 (insert "Category " (format "%S" category) ":\n\n")
107 (describe-property-list (symbol-plist category))
108 (goto-char (point-min)))))
110 ;;;###autoload
111 (defun describe-text-properties (pos &optional output-buffer buffer)
112 "Describe widgets, buttons, overlays, and text properties at POS.
113 POS is taken to be in BUFFER or in current buffer if nil.
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
117 otherwise."
118 (interactive "d")
119 (let ((src-buf (current-buffer)))
120 (if buffer (set-buffer buffer) (setq buffer (current-buffer)))
121 (if (>= pos (point-max))
122 (error "No character follows specified position"))
123 (if output-buffer
124 (describe-text-properties-1 pos output-buffer)
125 (if (not (or (text-properties-at pos) (overlays-at pos)))
126 (message "This is plain text.")
127 (with-temp-buffer
128 (setq output-buffer (current-buffer))
129 (insert "Text content at position " (format "%d" pos) ":\n\n")
130 (set-buffer buffer)
131 (describe-text-properties-1 pos output-buffer)
132 (set-buffer src-buf)
133 (help-setup-xref (list 'describe-text-properties pos nil buffer)
134 (called-interactively-p 'interactive))
135 (with-help-window (help-buffer)
136 (with-current-buffer standard-output
137 (buffer-swap-text output-buffer)
138 (goto-char (point-min)))))))))
140 (defun describe-text-properties-1 (pos output-buffer)
141 (let* ((properties (text-properties-at pos))
142 (overlays (overlays-in pos (1+ pos)))
143 (wid-field (get-char-property pos 'field))
144 (wid-button (get-char-property pos 'button))
145 (wid-doc (get-char-property pos 'widget-doc))
146 ;; If button.el is not loaded, we have no buttons in the text.
147 (button (and (fboundp 'button-at) (button-at pos)))
148 (button-type (and button (button-type button)))
149 (button-label (and button (button-label button)))
150 (widget (or wid-field wid-button wid-doc)))
151 (with-current-buffer output-buffer
152 ;; Widgets
153 (when (widgetp widget)
154 (newline)
155 (insert (cond (wid-field "This is an editable text area")
156 (wid-button "This is an active area")
157 (wid-doc "This is documentation text")))
158 (insert " of a ")
159 (describe-text-widget widget)
160 (insert ".\n\n"))
161 ;; Buttons
162 (when (and button (not (widgetp wid-button)))
163 (newline)
164 (insert (substitute-command-keys "Here is a ‘")
165 (format "%S" button-type)
166 (substitute-command-keys "’ button labeled ‘")
167 button-label
168 (substitute-command-keys "’.\n\n")))
169 ;; Overlays
170 (when overlays
171 (newline)
172 (if (eq (length overlays) 1)
173 (insert "There is an overlay here:\n")
174 (insert "There are " (format "%d" (length overlays))
175 " overlays here:\n"))
176 (dolist (overlay overlays)
177 (insert " From " (format "%d" (overlay-start overlay))
178 " to " (format "%d" (overlay-end overlay)) "\n")
179 (describe-property-list (overlay-properties overlay)))
180 (insert "\n"))
181 ;; Text properties
182 (when properties
183 (newline)
184 (insert "There are text properties here:\n")
185 (describe-property-list properties)))))
187 (defcustom describe-char-unidata-list
188 '(name old-name general-category decomposition)
189 "List of Unicode-based character property names shown by `describe-char'."
190 :group 'mule
191 :version "23.1"
192 :type '(choice (const :tag "All properties" t)
193 (set
194 (const :tag "Unicode name" name)
195 (const :tag "Unicode old name" old-name)
196 (const :tag "Unicode general category " general-category)
197 (const :tag "Unicode canonical combining class"
198 canonical-combining-class)
199 (const :tag "Unicode bidi class" bidi-class)
200 (const :tag "Unicode decomposition mapping" decomposition)
201 (const :tag "Unicode decimal digit value" decimal-digit-value)
202 (const :tag "Unicode digit value" digit-value)
203 (const :tag "Unicode numeric value" numeric-value)
204 (const :tag "Unicode mirrored" mirrored)
205 (const :tag "Unicode ISO 10646 comment" iso-10646-comment)
206 (const :tag "Unicode simple uppercase mapping" uppercase)
207 (const :tag "Unicode simple lowercase mapping" lowercase)
208 (const :tag "Unicode simple titlecase mapping" titlecase))))
210 (defcustom describe-char-unicodedata-file nil
211 "Location of Unicode data file.
212 This is the UnicodeData.txt file from the Unicode Consortium, used for
213 diagnostics. If it is non-nil `describe-char' will print data
214 looked up from it. This facility is mostly of use to people doing
215 multilingual development.
217 This is a fairly large file, not typically present on GNU systems.
218 At the time of writing it is at the URL
219 `http://www.unicode.org/Public/UNIDATA/UnicodeData.txt'."
220 :group 'mule
221 :version "22.1"
222 :type '(choice (const :tag "None" nil)
223 file))
225 (defun describe-char-unicode-data (char)
226 "Return a list of Unicode data for Unicode CHAR.
227 Each element is a list of a property description and the property value.
228 The list is null if CHAR isn't found in `describe-char-unicodedata-file'.
229 This function is semi-obsolete. Use `get-char-code-property'."
230 (when describe-char-unicodedata-file
231 (unless (file-exists-p describe-char-unicodedata-file)
232 (error "`unicodedata-file' %s not found" describe-char-unicodedata-file))
233 (with-current-buffer (get-buffer-create " *Unicode Data*")
234 (when (zerop (buffer-size))
235 ;; Don't use -literally in case of DOS line endings.
236 (insert-file-contents describe-char-unicodedata-file))
237 (goto-char (point-min))
238 (let ((hex (format "%04X" char))
239 found first last)
240 (if (re-search-forward (concat "^" hex) nil t)
241 (setq found t)
242 ;; It's not listed explicitly. Look for ranges, e.g. CJK
243 ;; ideographs, and check whether it's in one of them.
244 (while (and (re-search-forward "^\\([^;]+\\);[^;]+First>;" nil t)
245 (>= char (setq first
246 (string-to-number (match-string 1) 16)))
247 (progn
248 (forward-line 1)
249 (looking-at "^\\([^;]+\\);[^;]+Last>;")
250 (> char
251 (setq last
252 (string-to-number (match-string 1) 16))))))
253 (if (and (>= char first)
254 (<= char last))
255 (setq found t)))
256 (if found
257 (let ((fields (mapcar (lambda (elt)
258 (if (> (length elt) 0)
259 elt))
260 (cdr (split-string
261 (buffer-substring
262 (line-beginning-position)
263 (line-end-position))
264 ";")))))
265 ;; The length depends on whether the last field was empty.
266 (unless (or (= 13 (length fields))
267 (= 14 (length fields)))
268 (error "Invalid contents in %s" describe-char-unicodedata-file))
269 ;; The field names and values lists are slightly
270 ;; modified from Mule-UCS unidata.el.
271 (list
272 (list "Name" (let ((name (nth 0 fields)))
273 ;; Check for <..., First>, <..., Last>
274 (if (string-match "\\`\\(<[^,]+\\)," name)
275 (concat (match-string 1 name) ">")
276 name)))
277 (list "Category"
278 (let ((val (nth 1 fields)))
279 (or (char-code-property-description
280 'general-category (intern val))
281 val)))
282 (list "Combining class"
283 (let ((val (nth 1 fields)))
284 (or (char-code-property-description
285 'canonical-combining-class (intern val))
286 val)))
287 (list "Bidi category"
288 (let ((val (nth 1 fields)))
289 (or (char-code-property-description
290 'bidi-class (intern val))
291 val)))
292 (list
293 "Decomposition"
294 (if (nth 4 fields)
295 (let* ((parts (split-string (nth 4 fields)))
296 (info (car parts)))
297 (if (string-match "\\`<\\(.+\\)>\\'" info)
298 (setq info (match-string 1 info))
299 (setq info nil))
300 (if info (setq parts (cdr parts)))
301 (setq parts (mapconcat
302 (lambda (arg)
303 (string (string-to-number arg 16)))
304 parts " "))
305 (concat info (if info " ") parts))))
306 (list "Decimal digit value"
307 (nth 5 fields))
308 (list "Digit value"
309 (nth 6 fields))
310 (list "Numeric value"
311 (nth 7 fields))
312 (list "Mirrored"
313 (if (equal "Y" (nth 8 fields))
314 "yes"))
315 (list "Old name" (nth 9 fields))
316 (list "ISO 10646 comment" (nth 10 fields))
317 (list "Uppercase" (and (nth 11 fields)
318 (string (string-to-number
319 (nth 11 fields) 16))))
320 (list "Lowercase" (and (nth 12 fields)
321 (string (string-to-number
322 (nth 12 fields) 16))))
323 (list "Titlecase" (and (nth 13 fields)
324 (string (string-to-number
325 (nth 13 fields) 16)))))))))))
327 ;; Not defined on builds without X, but behind display-graphic-p.
328 (declare-function internal-char-font "fontset.c" (position &optional ch))
330 ;; Return information about how CHAR is displayed at the buffer
331 ;; position POS. If the selected frame is on a graphic display,
332 ;; return a string "FONT-DRIVER:FONT-NAME (GLYPH-CODE)" where:
333 ;; FONT-DRIVER is the font-driver name,
334 ;; FONT-NAME is the font name,
335 ;; GLYPH-CODE is a hexadigit string representing the glyph-ID.
336 ;; Otherwise, return a string describing the terminal codes for the
337 ;; character.
338 (defun describe-char-display (pos char)
339 (if (display-graphic-p (selected-frame))
340 (let ((char-font-info (internal-char-font pos char)))
341 (if char-font-info
342 (let ((type (font-get (car char-font-info) :type))
343 (name (font-xlfd-name (car char-font-info)))
344 (code (cdr char-font-info)))
345 (if (integerp code)
346 (format "%s:%s (#x%02X)" type name code)
347 (format "%s:%s (#x%04X%04X)"
348 type name (car code) (cdr code))))))
349 (let* ((charset (get-text-property pos 'charset))
350 (coding (or (terminal-coding-system) 'us-ascii))
351 (encoded (encode-coding-char char coding charset)))
352 (if encoded
353 (encoded-string-description encoded coding)))))
356 ;; Return a string of CH with composition for padding on both sides.
357 ;; It is displayed without overlapping with the left/right columns.
358 (defsubst describe-char-padded-string (ch)
359 (if (and (display-multi-font-p)
360 (internal-char-font nil ch))
361 (compose-string (string ch) 0 1 (format "\t%c\t" ch))
362 (string ch)))
364 ;; Return a nicely formatted list of categories; extended category
365 ;; description is added to the category name as a tooltip
366 (defsubst describe-char-categories (category-set)
367 (let ((mnemonics (category-set-mnemonics category-set)))
368 (unless (eq mnemonics "")
369 (list (mapconcat
370 (lambda (x)
371 (let* ((c (category-docstring x))
372 (doc (if (string-match "\\`\\(.*?\\)\n" c)
373 (propertize (match-string 1 c)
374 'help-echo
375 (substring c (1+ (match-end 1))))
376 c)))
377 (format "%c:%s" x doc)))
378 mnemonics ", ")))))
380 (declare-function quail-find-key "quail" (char))
382 ;;;###autoload
383 (defun describe-char (pos &optional buffer)
384 "Describe position POS (interactively, point) and the char after POS.
385 POS is taken to be in BUFFER, or the current buffer if BUFFER is nil.
386 The information is displayed in buffer `*Help*'.
388 The position information includes POS; the total size of BUFFER; the
389 region limits, if narrowed; the column number; and the horizontal
390 scroll amount, if the buffer is horizontally scrolled.
392 The character information includes the character code; charset and
393 code points in it; syntax; category; how the character is encoded in
394 BUFFER and in BUFFER's file; character composition information (if
395 relevant); the font and font glyphs used to display the character;
396 the character's canonical name and other properties defined by the
397 Unicode Data Base; and widgets, buttons, overlays, and text properties
398 relevant to POS."
399 (interactive "d")
400 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
401 (let ((src-buf (current-buffer)))
402 (set-buffer buffer)
403 (if (>= pos (point-max))
404 (error "No character follows specified position"))
405 (let* ((char (char-after pos))
406 (eight-bit-p (and (not enable-multibyte-characters) (>= char 128)))
407 (charset (if eight-bit-p 'eight-bit
408 (or (get-text-property pos 'charset)
409 (char-charset char))))
410 (composition (find-composition pos nil nil t))
411 (component-chars nil)
412 (display-table (or (window-display-table)
413 buffer-display-table
414 standard-display-table))
415 (disp-vector (and display-table (aref display-table char)))
416 (multibyte-p enable-multibyte-characters)
417 (overlays (mapcar (lambda (o) (overlay-properties o))
418 (overlays-at pos)))
419 (char-description (if (not multibyte-p)
420 (single-key-description char)
421 (if (< char 128)
422 (single-key-description char)
423 (string-to-multibyte
424 (char-to-string char)))))
425 (text-props-desc
426 (let ((tmp-buf (generate-new-buffer " *text-props*")))
427 (unwind-protect
428 (progn
429 (describe-text-properties pos tmp-buf)
430 (with-current-buffer tmp-buf (buffer-string)))
431 (kill-buffer tmp-buf))))
432 item-list max-width code)
434 (if multibyte-p
435 (or (setq code (encode-char char charset))
436 (setq charset (char-charset char)
437 code (encode-char char charset)))
438 (setq code char))
439 (cond
440 ;; Append a PDF character to left-to-right directional
441 ;; embeddings and overrides, to prevent potential messup of the
442 ;; following text.
443 ((memq char '(?\x202a ?\x202d))
444 (setq char-description
445 (concat char-description
446 (propertize (string ?\x202c) 'invisible t))))
447 ;; Append a PDF character followed by LRM to right-to-left
448 ;; directional embeddings and overrides, to prevent potential
449 ;; messup of the following numerical text.
450 ((memq char '(?\x202b ?\x202e))
451 (setq char-description
452 (concat char-description
453 (propertize (string ?\x202c ?\x200e) 'invisible t))))
454 ;; Append a PDI character to directional isolate initiators, to
455 ;; prevent potential messup of the following numerical text
456 ((memq char '(?\x2066 ?\x2067 ?\x2068))
457 (setq char-description
458 (concat char-description
459 (propertize (string ?\x2069) 'invisible t))))
460 ;; Append a LRM character to any strong character to avoid
461 ;; messing up the numerical codepoint.
462 ((memq (get-char-code-property char 'bidi-class) '(R AL))
463 (setq char-description
464 (concat char-description
465 (propertize (string ?\x200e) 'invisible t)))))
466 (when composition
467 ;; When the composition is trivial (i.e. composed only with the
468 ;; current character itself without any alternate characters),
469 ;; we don't show the composition information. Otherwise, store
470 ;; two descriptive strings in the first two elements of
471 ;; COMPOSITION.
472 (or (catch 'tag
473 (let ((from (car composition))
474 (to (nth 1 composition))
475 (components (nth 2 composition))
477 (if (and (vectorp components) (vectorp (aref components 0)))
478 (let ((idx (- pos from))
479 (nglyphs (lgstring-glyph-len components))
480 (i 0) j glyph glyph-from)
481 ;; COMPONENTS is a gstring. Find a grapheme
482 ;; cluster containing the current character.
483 (while (and (< i nglyphs)
484 (setq glyph (lgstring-glyph components i))
485 (< (lglyph-to glyph) idx))
486 (setq i (1+ i)))
487 (if (or (not glyph) (= i nglyphs))
488 ;; The composition is broken.
489 (throw 'tag nil))
490 (setq glyph-from (lglyph-from glyph)
491 to (+ from (lglyph-to glyph) 1)
492 from (+ from glyph-from)
493 j i)
494 (while (and (< j nglyphs)
495 (setq glyph (lgstring-glyph components j))
496 (= (lglyph-from glyph) glyph-from))
497 (setq j (1+ j)))
498 (if (and (= to (1+ from))
499 (= i (1- j))
500 (setq glyph (lgstring-glyph components i))
501 (= char (lglyph-char glyph)))
502 ;; The composition is trivial.
503 (throw 'tag nil))
504 (nconc composition (list i (1- j))))
505 (dotimes (i (length components))
506 (if (integerp (setq ch (aref components i)))
507 (push (cons ch (describe-char-display pos ch))
508 component-chars)))
509 (setq component-chars (nreverse component-chars)))
510 (if (< from pos)
511 (if (< (1+ pos) to)
512 (setcar composition
513 (concat
514 " with the surrounding characters \""
515 (mapconcat 'describe-char-padded-string
516 (buffer-substring from pos) "")
517 "\" and \""
518 (mapconcat 'describe-char-padded-string
519 (buffer-substring (1+ pos) to) "")
520 "\""))
521 (setcar composition
522 (concat
523 " with the preceding character(s) \""
524 (mapconcat 'describe-char-padded-string
525 (buffer-substring from pos) "")
526 "\"")))
527 (if (< (1+ pos) to)
528 (setcar composition
529 (concat
530 " with the following character(s) \""
531 (mapconcat 'describe-char-padded-string
532 (buffer-substring (1+ pos) to) "")
533 "\""))
534 (setcar composition nil)))
535 (setcar (cdr composition)
536 (format "composed to form \"%s\" (see below)"
537 (buffer-substring from to)))))
538 (setq composition nil)))
540 (setq item-list
541 `(("position"
542 ,(let* ((beg (point-min))
543 (end (point-max))
544 (total (buffer-size))
545 (percent (if (> total 50000) ; Avoid overflow multiplying by 100
546 (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1))
547 (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1))))
548 (hscroll (if (= (window-hscroll) 0)
550 (format ", Hscroll: %d" (window-hscroll))))
551 (col (current-column)))
552 (if (or (/= beg 1) (/= end (1+ total)))
553 (format "%d of %d (%d%%), restriction: <%d-%d>, column: %d%s"
554 pos total percent beg end col hscroll)
555 (if (= pos end)
556 (format "%d of %d (EOB), column: %d%s" pos total col hscroll)
557 (format "%d of %d (%d%%), column: %d%s"
558 pos total percent col hscroll)))))
559 ("character"
560 ,(format "%s (displayed as %s) (codepoint %d, #o%o, #x%x)"
561 char-description
562 (apply 'propertize char-description
563 (text-properties-at pos))
564 char char char))
565 ("preferred charset"
566 ,`(insert-text-button
567 ,(symbol-name charset)
568 'type 'help-character-set 'help-args '(,charset))
569 ,(format "(%s)" (charset-description charset)))
570 ("code point in charset"
571 ,(let ((str (if (integerp code)
572 (format (if (< code 256) "0x%02X" "0x%04X")
573 code)
574 (format "0x%04X%04X" (car code) (cdr code)))))
575 (if (<= (charset-dimension charset) 2)
576 `(insert-text-button
577 ,str
578 'action (lambda (&rest ignore)
579 (list-charset-chars ',charset)
580 (with-selected-window
581 (get-buffer-window "*Character List*" 0)
582 (goto-char (point-min))
583 (forward-line 2) ;Skip the header.
584 (let ((case-fold-search nil))
585 (if (search-forward
586 ,(char-to-string char) nil t)
587 (goto-char (match-beginning 0))))))
588 'follow-link t
589 'help-echo
590 "mouse-2, RET: show this character in its character set")
591 str)))
592 ,@(let ((script (aref char-script-table char)))
593 (if script
594 (list (list "script" (symbol-name script)))))
595 ("syntax"
596 ,(let ((syntax (syntax-after pos)))
597 (with-temp-buffer
598 (internal-describe-syntax-value syntax)
599 (buffer-string))))
600 ("category"
601 ,@(if (not eight-bit-p)
602 (let ((category-set (char-category-set char)))
603 (if category-set
604 (describe-char-categories category-set)
605 '("-- none --")))))
606 ("to input"
607 ,@(if (not eight-bit-p)
608 (let ((key-list (and (eq input-method-function
609 'quail-input-method)
610 (quail-find-key char))))
611 (if (consp key-list)
612 (list "type"
613 (concat "\""
614 (mapconcat 'identity
615 key-list "\" or \"")
616 "\"")
617 "with"
618 `(insert-text-button
619 ,current-input-method
620 'type 'help-input-method
621 'help-args '(,current-input-method))
622 "input method")
623 (list
624 (let ((name
625 (or (get-char-code-property char 'name)
626 (get-char-code-property char 'old-name))))
627 (if name
628 (format
629 "type \"C-x 8 RET %x\" or \"C-x 8 RET %s\""
630 char name)
631 (format "type \"C-x 8 RET %x\"" char))))))))
632 ("buffer code"
633 ,(if multibyte-p
634 (encoded-string-description
635 (string-as-unibyte (char-to-string char)) nil)
636 (format "#x%02X" char)))
637 ("file code"
638 ,@(if multibyte-p
639 (let* ((coding buffer-file-coding-system)
640 (encoded (encode-coding-char char coding charset)))
641 (if encoded
642 (list (encoded-string-description encoded coding)
643 (format "(encoded by coding system %S)"
644 coding))
645 (list "not encodable by coding system"
646 (symbol-name coding))))
647 (list (format "#x%02X" char))))
648 ("display"
649 ,(cond
650 (disp-vector
651 (setq disp-vector (copy-sequence disp-vector))
652 (dotimes (i (length disp-vector))
653 (aset disp-vector i
654 (cons (aref disp-vector i)
655 (describe-char-display
656 pos (glyph-char (aref disp-vector i))))))
657 (format "by display table entry [%s] (see below)"
658 (mapconcat
659 (lambda (x)
660 (format "?%c" (glyph-char (car x))))
661 disp-vector " ")))
662 (composition
663 (cadr composition))
665 (let ((display (describe-char-display pos char)))
666 (if (display-graphic-p (selected-frame))
667 (if display
668 (concat "by this font (glyph code)\n " display)
669 "no font available")
670 (if display
671 (format "terminal code %s" display)
672 "not encodable for terminal"))))))
673 ,@(let ((face
674 (if (not (or disp-vector composition))
675 (cond
676 ((and show-trailing-whitespace
677 (save-excursion (goto-char pos)
678 (looking-at-p "[ \t]+$")))
679 'trailing-whitespace)
680 ((and nobreak-char-display char (eq char '#xa0))
681 'nobreak-space)
682 ((and nobreak-char-display char
683 (memq char '(#xad #x2010 #x2011)))
684 'escape-glyph)
685 ((and (< char 32) (not (memq char '(9 10))))
686 'escape-glyph)))))
687 (if face (list (list "hardcoded face"
688 `(insert-text-button ;FIXME: Wrap in lambda!
689 ,(symbol-name face)
690 'type 'help-face
691 'help-args '(,face))))))
692 ,@(if (not eight-bit-p)
693 (let ((unicodedata (describe-char-unicode-data char)))
694 (if unicodedata
695 (cons (list "Unicode data" "") unicodedata))))))
696 (setq max-width (apply 'max (mapcar (lambda (x)
697 (if (cadr x) (length (car x)) 0))
698 item-list)))
699 (set-buffer src-buf)
700 (help-setup-xref (list 'describe-char pos buffer)
701 (called-interactively-p 'interactive))
702 (with-help-window (help-buffer)
703 (with-current-buffer standard-output
704 (set-buffer-multibyte multibyte-p)
705 (let ((formatter (format "%%%ds:" max-width)))
706 (dolist (elt item-list)
707 (when (cadr elt)
708 (insert (format formatter (car elt)))
709 (dolist (clm (cdr elt))
710 (cond ((eq (car-safe clm) 'insert-text-button)
711 (insert " ")
712 (eval clm))
713 ((not (zerop (length clm)))
714 (insert " " clm))))
715 (insert "\n"))))
717 (when overlays
718 (save-excursion
719 (goto-char (point-min))
720 (re-search-forward "(displayed as ")
721 (let ((end (+ (point) (length char-description))))
722 (mapc (lambda (props)
723 (let ((o (make-overlay (point) end)))
724 (while props
725 (overlay-put o (car props) (nth 1 props))
726 (setq props (cddr props)))))
727 overlays))))
729 (when disp-vector
730 (insert
731 "\nThe display table entry is displayed by ")
732 (if (display-graphic-p (selected-frame))
733 (progn
734 (insert "these fonts (glyph codes):\n")
735 (dotimes (i (length disp-vector))
736 (insert (glyph-char (car (aref disp-vector i))) ?:
737 (propertize " " 'display '(space :align-to 5))
738 (or (cdr (aref disp-vector i)) "-- no font --")
739 "\n")
740 (let ((face (glyph-face (car (aref disp-vector i)))))
741 (when face
742 (insert (propertize " " 'display '(space :align-to 5))
743 "face: ")
744 (insert (substitute-command-keys "‘")
745 (symbol-name face)
746 (substitute-command-keys "’\n"))))))
747 (insert "these terminal codes:\n")
748 (dotimes (i (length disp-vector))
749 (insert (car (aref disp-vector i))
750 (propertize " " 'display '(space :align-to 5))
751 (or (cdr (aref disp-vector i)) "-- not encodable --")
752 "\n"))))
754 (when composition
755 (insert "\nComposed")
756 (if (car composition)
757 (insert (car composition)))
758 (if (and (vectorp (nth 2 composition))
759 (vectorp (aref (nth 2 composition) 0)))
760 (let* ((gstring (nth 2 composition))
761 (font (lgstring-font gstring))
762 (from (nth 3 composition))
763 (to (nth 4 composition))
764 glyph)
765 (if (fontp font)
766 (progn
767 (insert " using this font:\n "
768 (symbol-name (font-get font :type))
770 (aref (query-font font) 0)
771 "\nby these glyphs:\n")
772 (while (and (<= from to)
773 (setq glyph (lgstring-glyph gstring from)))
774 (insert (format " %S\n" glyph))
775 (setq from (1+ from))))
776 (insert " by these characters:\n")
777 (while (and (<= from to)
778 (setq glyph (lgstring-glyph gstring from)))
779 (insert (format " %c (#x%x)\n"
780 (lglyph-char glyph) (lglyph-char glyph)))
781 (setq from (1+ from)))))
782 (insert " by the rule:\n\t(")
783 (let ((first t))
784 (mapc (lambda (x)
785 (if first (setq first nil)
786 (insert " "))
787 (if (consp x) (insert (format "%S" x))
788 (if (= x ?\t) (insert (single-key-description x))
789 (insert ??)
790 (insert (describe-char-padded-string x)))))
791 (nth 2 composition)))
792 (insert ")\nThe component character(s) are displayed by ")
793 (if (display-graphic-p (selected-frame))
794 (progn
795 (insert "these fonts (glyph codes):")
796 (dolist (elt component-chars)
797 (if (/= (car elt) ?\t)
798 (insert "\n "
799 (describe-char-padded-string (car elt))
801 (propertize " "
802 'display '(space :align-to 5))
803 (or (cdr elt) "-- no font --")))))
804 (insert "these terminal codes:")
805 (dolist (elt component-chars)
806 (insert "\n " (car elt) ":"
807 (propertize " " 'display '(space :align-to 4))
808 (or (cdr elt) "-- not encodable --"))))
809 (insert "\nSee the variable `reference-point-alist' for "
810 "the meaning of the rule.\n")))
812 (unless eight-bit-p
813 (insert (if (not describe-char-unidata-list)
814 "\nCharacter code properties are not shown: "
815 "\nCharacter code properties: "))
816 (insert-text-button
817 "customize what to show"
818 'action (lambda (&rest _ignore)
819 (customize-variable
820 'describe-char-unidata-list))
821 'follow-link t)
822 (insert "\n")
823 (dolist (elt (if (eq describe-char-unidata-list t)
824 (nreverse (mapcar 'car char-code-property-alist))
825 describe-char-unidata-list))
826 (let ((val (get-char-code-property char elt))
827 description)
828 (when val
829 (setq description (char-code-property-description elt val))
830 (insert (if description
831 (format " %s: %s (%s)\n" elt val description)
832 (format " %s: %s\n" elt val)))))))
834 (if text-props-desc (insert text-props-desc))
835 (setq buffer-read-only t))))))
837 (define-obsolete-function-alias 'describe-char-after 'describe-char "22.1")
839 ;;; Describe-Char-ElDoc
841 (defun describe-char-eldoc--truncate (name width)
842 "Truncate NAME at white spaces such that it is no longer than WIDTH.
844 Split NAME on white space character and return string with as
845 many leading words of NAME as possible without exceeding WIDTH
846 characters. If NAME consists of white space characters only,
847 return an empty string. Three dots (\"...\") are appended to
848 returned string if some of the words from NAME have been omitted.
850 NB: Function may return string longer than WIDTH if name consists
851 of a single word, or it's first word is longer than WIDTH
852 characters."
853 (let ((words (split-string name)))
854 (if words
855 (let ((last words))
856 (setq width (- width (length (car words))))
857 (while (and (cdr last)
858 (<= (+ (length (cadr last)) (if (cddr last) 4 1)) width))
859 (setq last (cdr last))
860 (setq width (- width (length (car last)) 1)))
861 (let ((ellipsis (and (cdr last) "...")))
862 (setcdr last nil)
863 (concat (mapconcat 'identity words " ") ellipsis)))
864 "")))
866 (defun describe-char-eldoc--format (ch &optional width)
867 "Format a description for character CH which is no more than WIDTH characters.
869 Full description message has a \"U+HEX: NAME (GC: GENERAL-CATEGORY)\"
870 format where:
871 - HEX is a hexadecimal codepoint of the character (zero-padded to at
872 least four digits),
873 - NAME is name of the character.
874 - GC is a two-letter abbreviation of the general-category of the
875 character, and
876 - GENERAL-CATEGORY is full name of the general-category of the
877 character.
879 If WIDTH is non-nil some elements of the description may be
880 omitted to accommodate the length restriction. Under certain
881 condition, the function may return string longer than WIDTH, see
882 `describe-char-eldoc--truncate'."
883 (let ((name (get-char-code-property ch 'name)))
884 (when name
885 (let* ((code (propertize (format "U+%04X" ch)
886 'face 'font-lock-constant-face))
887 (gc (get-char-code-property ch 'general-category))
888 (gc-desc (char-code-property-description 'general-category gc)))
890 (unless (or (not width) (<= (length name) width))
891 (setq name (describe-char-eldoc--truncate name width)))
892 (setq name (concat (substring name 0 1) (downcase (substring name 1))))
893 (setq name (propertize name 'face 'font-lock-variable-name-face))
895 (setq gc (propertize (symbol-name gc) 'face 'font-lock-comment-face))
896 (when gc-desc
897 (setq gc-desc (propertize gc-desc 'face 'font-lock-comment-face)))
899 (let ((lcode (length code))
900 (lname (length name))
901 (lgc (length gc))
902 (lgc-desc (and gc-desc (length gc-desc))))
903 (cond
904 ((and gc-desc
905 (or (not width) (<= (+ lcode lname lgc lgc-desc 7) width)))
906 (concat code ": " name " (" gc ": " gc-desc ")"))
907 ((and gc-desc (<= (+ lcode lname lgc-desc 5) width))
908 (concat code ": " name " (" gc-desc ")"))
909 ((or (not width) (<= (+ lcode lname lgc 5) width))
910 (concat code ": " name " (" gc ")"))
911 ((<= (+ lname lgc 3) width)
912 (concat name " (" gc ")"))
913 (t name)))))))
915 ;;;###autoload
916 (defun describe-char-eldoc ()
917 "Return a description of character at point for use by ElDoc mode.
919 Return nil if character at point is a printable ASCII
920 character (i.e. codepoint between 32 and 127 inclusively).
921 Otherwise return a description formatted by
922 `describe-char-eldoc--format' function taking into account value
923 of `eldoc-echo-area-use-multiline-p' variable and width of
924 minibuffer window for width limit.
926 This function is meant to be used as a value of
927 `eldoc-documentation-function' variable."
928 (let ((ch (following-char)))
929 (when (and (not (zerop ch)) (or (< ch 32) (> ch 127)))
930 (describe-char-eldoc--format
932 (unless (eq eldoc-echo-area-use-multiline-p t)
933 (1- (window-width (minibuffer-window))))))))
935 (provide 'descr-text)
937 ;;; descr-text.el ends here