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