* image-mode.el (image-toggle-display):
[bpt/emacs.git] / lisp / descr-text.el
CommitLineData
dcb90b5f 1;;; descr-text.el --- describe text mode
2a1e884e 2
0d30b337 3;; Copyright (C) 1994, 1995, 1996, 2001, 2002, 2003, 2004,
ae940284 4;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
2a1e884e
RS
5
6;; Author: Boris Goldowsky <boris@gnu.org>
57d79b99 7;; Maintainer: FSF
2d65673f 8;; Keywords: faces, i18n, Unicode, multilingual
2a1e884e
RS
9
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
2a1e884e 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
2a1e884e
RS
16
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.
21
22;; You should have received a copy of the GNU General Public License
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
2a1e884e
RS
24
25;;; Commentary:
26
27;;; Describe-Text Mode.
28
29;;; Code:
30
e5a5c80c
NR
31(eval-when-compile (require 'quail))
32(require 'help-fns)
2a1e884e
RS
33
34;;; Describe-Text Utilities.
35
36(defun describe-text-widget (widget)
37 "Insert text to describe WIDGET in the current buffer."
57d79b99
NR
38 (insert-text-button
39 (symbol-name (if (symbolp widget) widget (car widget)))
40 'action `(lambda (&rest ignore)
e5a5c80c
NR
41 (widget-browse ',widget))
42 'help-echo "mouse-2, RET: browse this widget")
57d79b99 43 (insert " ")
e5a5c80c
NR
44 (insert-text-button
45 "(widget)Top" 'type 'help-info 'help-args '("(widget)Top")))
2a1e884e
RS
46
47(defun describe-text-sexp (sexp)
48 "Insert a short description of SEXP in the current buffer."
49 (let ((pp (condition-case signal
50 (pp-to-string sexp)
51 (error (prin1-to-string signal)))))
70583cb5 52 (when (string-match-p "\n\\'" pp)
2a1e884e 53 (setq pp (substring pp 0 (1- (length pp)))))
70583cb5
JB
54
55 (if (and (not (string-match-p "\n" pp))
56 (<= (length pp) (- (window-width) (current-column))))
57d79b99
NR
57 (insert pp)
58 (insert-text-button
2fd54bf8 59 "[Show]" 'action `(lambda (&rest ignore)
70583cb5
JB
60 (with-output-to-temp-buffer
61 "*Pp Eval Output*"
62 (princ ',pp)))
57d79b99 63 'help-echo "mouse-2, RET: pretty print value in another buffer"))))
2a1e884e 64
4adb7c09 65(defun describe-property-list (properties)
2a1e884e
RS
66 "Insert a description of PROPERTIES in the current buffer.
67PROPERTIES should be a list of overlay or text properties.
7fb0741b 68The `category', `face' and `font-lock-face' properties are made
57d79b99 69into help buttons that call `describe-text-category' or
7fb0741b 70`describe-face' when pushed."
e2fa2f6e 71 ;; Sort the properties by the size of their value.
8b18fb8f 72 (dolist (elt (sort (let (ret)
e2fa2f6e 73 (while properties
8b18fb8f 74 (push (list (pop properties) (pop properties)) ret))
e2fa2f6e 75 ret)
d502fc06
RS
76 (lambda (a b) (string< (prin1-to-string (nth 0 a) t)
77 (prin1-to-string (nth 0 b) t)))))
e2fa2f6e
CW
78 (let ((key (nth 0 elt))
79 (value (nth 1 elt)))
57d79b99 80 (insert (propertize (format " %-20s " key)
e5a5c80c 81 'face 'help-argument-name))
2a1e884e 82 (cond ((eq key 'category)
e5a5c80c
NR
83 (insert-text-button
84 (symbol-name value)
85 'action `(lambda (&rest ignore)
86 (describe-text-category ',value))
e7621494 87 'follow-link t
e5a5c80c 88 'help-echo "mouse-2, RET: describe this category"))
8b18fb8f 89 ((memq key '(face font-lock-face mouse-face))
2fd54bf8
JL
90 (insert-text-button
91 (format "%S" value)
92 'type 'help-face 'help-args (list value)))
f13cc97d 93 ((widgetp value)
2a1e884e
RS
94 (describe-text-widget value))
95 (t
8b18fb8f 96 (describe-text-sexp value))))
57d79b99 97 (insert "\n")))
2a1e884e
RS
98\f
99;;; Describe-Text Commands.
100
101(defun describe-text-category (category)
102 "Describe a text property category."
e5a5c80c
NR
103 (interactive "SCategory: ")
104 (help-setup-xref (list #'describe-text-category category) (interactive-p))
2a1e884e 105 (save-excursion
ca9088e7
SM
106 (with-output-to-temp-buffer "*Help*"
107 (set-buffer standard-output)
57d79b99 108 (insert "Category " (format "%S" category) ":\n\n")
4adb7c09 109 (describe-property-list (symbol-plist category))
2a1e884e
RS
110 (goto-char (point-min)))))
111
112;;;###autoload
4adb7c09
RS
113(defun describe-text-properties (pos &optional output-buffer)
114 "Describe widgets, buttons, overlays and text properties at POS.
115Interactively, describe them for the character after point.
116If optional second argument OUTPUT-BUFFER is non-nil,
117insert the output into that buffer, and don't initialize or clear it
118otherwise."
2a1e884e 119 (interactive "d")
4adb7c09
RS
120 (if (>= pos (point-max))
121 (error "No character follows specified position"))
122 (if output-buffer
123 (describe-text-properties-1 pos output-buffer)
124 (if (not (or (text-properties-at pos) (overlays-at pos)))
125 (message "This is plain text.")
0911ac26
KS
126 (let ((buffer (current-buffer))
127 (target-buffer "*Help*"))
128 (when (eq buffer (get-buffer target-buffer))
e217c04e 129 (setq target-buffer "*Help*<2>"))
4adb7c09 130 (save-excursion
0911ac26 131 (with-output-to-temp-buffer target-buffer
ca9088e7 132 (set-buffer standard-output)
4adb7c09 133 (setq output-buffer (current-buffer))
57d79b99 134 (insert "Text content at position " (format "%d" pos) ":\n\n")
4adb7c09
RS
135 (with-current-buffer buffer
136 (describe-text-properties-1 pos output-buffer))
4adb7c09
RS
137 (goto-char (point-min))))))))
138
139(defun describe-text-properties-1 (pos output-buffer)
2a1e884e
RS
140 (let* ((properties (text-properties-at pos))
141 (overlays (overlays-at pos))
2a1e884e
RS
142 (wid-field (get-char-property pos 'field))
143 (wid-button (get-char-property pos 'button))
144 (wid-doc (get-char-property pos 'widget-doc))
145 ;; If button.el is not loaded, we have no buttons in the text.
146 (button (and (fboundp 'button-at) (button-at pos)))
147 (button-type (and button (button-type button)))
148 (button-label (and button (button-label button)))
149 (widget (or wid-field wid-button wid-doc)))
4adb7c09
RS
150 (with-current-buffer output-buffer
151 ;; Widgets
152 (when (widgetp widget)
153 (newline)
57d79b99
NR
154 (insert (cond (wid-field "This is an editable text area")
155 (wid-button "This is an active area")
156 (wid-doc "This is documentation text")))
157 (insert " of a ")
4adb7c09 158 (describe-text-widget widget)
57d79b99 159 (insert ".\n\n"))
4adb7c09
RS
160 ;; Buttons
161 (when (and button (not (widgetp wid-button)))
162 (newline)
2fd54bf8
JL
163 (insert "Here is a `" (format "%S" button-type)
164 "' button labeled `" button-label "'.\n\n"))
4adb7c09
RS
165 ;; Overlays
166 (when overlays
167 (newline)
168 (if (eq (length overlays) 1)
57d79b99
NR
169 (insert "There is an overlay here:\n")
170 (insert "There are " (format "%d" (length overlays))
4adb7c09
RS
171 " overlays here:\n"))
172 (dolist (overlay overlays)
57d79b99 173 (insert " From " (format "%d" (overlay-start overlay))
4adb7c09
RS
174 " to " (format "%d" (overlay-end overlay)) "\n")
175 (describe-property-list (overlay-properties overlay)))
57d79b99 176 (insert "\n"))
4adb7c09
RS
177 ;; Text properties
178 (when properties
179 (newline)
57d79b99 180 (insert "There are text properties here:\n")
4adb7c09 181 (describe-property-list properties)))))
d6c135fb 182\f
950b5859 183(defcustom describe-char-unidata-list
0e0f6cbd 184 '(name old-name general-category decomposition)
f1f194de
KH
185 "List of Unicode-based character property names shown by `describe-char'."
186 :group 'mule
8589dc17 187 :version "23.1"
3253c7c6
SM
188 :type '(choice (const :tag "All properties" t)
189 (set
190 (const :tag "Unicode Name" name)
0e0f6cbd 191 (const :tag "Unicode old name" old-name)
3253c7c6
SM
192 (const :tag "Unicode general category " general-category)
193 (const :tag "Unicode canonical combining class"
194 canonical-combining-class)
195 (const :tag "Unicode bidi class" bidi-class)
196 (const :tag "Unicode decomposition mapping" decomposition)
197 (const :tag "Unicode decimal digit value" decimal-digit-value)
198 (const :tag "Unicode digit value" digit-value)
199 (const :tag "Unicode numeric value" numeric-value)
200 (const :tag "Unicode mirrored" mirrored)
3253c7c6
SM
201 (const :tag "Unicode ISO 10646 comment" iso-10646-comment)
202 (const :tag "Unicode simple uppercase mapping" uppercase)
203 (const :tag "Unicode simple lowercase mapping" lowercase)
204 (const :tag "Unicode simple titlecase mapping" titlecase))))
f1f194de 205
49c2a2dc
SM
206(defcustom describe-char-unicodedata-file nil
207 "Location of Unicode data file.
2acb13b1 208This is the UnicodeData.txt file from the Unicode Consortium, used for
2a4960f9 209diagnostics. If it is non-nil `describe-char' will print data
49c2a2dc
SM
210looked up from it. This facility is mostly of use to people doing
211multilingual development.
4adb7c09 212
2acb13b1
JB
213This is a fairly large file, not typically present on GNU systems.
214At the time of writing it is at the URL
57d79b99 215`http://www.unicode.org/Public/UNIDATA/UnicodeData.txt'."
49c2a2dc 216 :group 'mule
bf247b6e 217 :version "22.1"
49c2a2dc
SM
218 :type '(choice (const :tag "None" nil)
219 file))
831ccfa6 220
49c2a2dc
SM
221(defun describe-char-unicode-data (char)
222 "Return a list of Unicode data for unicode CHAR.
223Each element is a list of a property description and the property value.
f1f194de
KH
224The list is null if CHAR isn't found in `describe-char-unicodedata-file'.
225This function is semi-obsolete. Use `get-char-code-property'."
49c2a2dc
SM
226 (when describe-char-unicodedata-file
227 (unless (file-exists-p describe-char-unicodedata-file)
228 (error "`unicodedata-file' %s not found" describe-char-unicodedata-file))
def7167b
JB
229 (with-current-buffer (get-buffer-create " *Unicode Data*")
230 (when (zerop (buffer-size))
231 ;; Don't use -literally in case of DOS line endings.
232 (insert-file-contents describe-char-unicodedata-file))
49c2a2dc
SM
233 (goto-char (point-min))
234 (let ((hex (format "%04X" char))
235 found first last)
236 (if (re-search-forward (concat "^" hex) nil t)
237 (setq found t)
238 ;; It's not listed explicitly. Look for ranges, e.g. CJK
239 ;; ideographs, and check whether it's in one of them.
240 (while (and (re-search-forward "^\\([^;]+\\);[^;]+First>;" nil t)
241 (>= char (setq first
242 (string-to-number (match-string 1) 16)))
243 (progn
244 (forward-line 1)
245 (looking-at "^\\([^;]+\\);[^;]+Last>;")
246 (> char
247 (setq last
248 (string-to-number (match-string 1) 16))))))
249 (if (and (>= char first)
250 (<= char last))
251 (setq found t)))
252 (if found
253 (let ((fields (mapcar (lambda (elt)
254 (if (> (length elt) 0)
255 elt))
256 (cdr (split-string
257 (buffer-substring
258 (line-beginning-position)
259 (line-end-position))
260 ";")))))
261 ;; The length depends on whether the last field was empty.
262 (unless (or (= 13 (length fields))
263 (= 14 (length fields)))
264 (error "Invalid contents in %s" describe-char-unicodedata-file))
265 ;; The field names and values lists are slightly
266 ;; modified from Mule-UCS unidata.el.
267 (list
268 (list "Name" (let ((name (nth 0 fields)))
269 ;; Check for <..., First>, <..., Last>
270 (if (string-match "\\`\\(<[^,]+\\)," name)
271 (concat (match-string 1 name) ">")
272 name)))
273 (list "Category"
f1f194de
KH
274 (let ((val (nth 1 fields)))
275 (or (char-code-property-description
276 'general-category (intern val))
277 val)))
49c2a2dc 278 (list "Combining class"
f1f194de
KH
279 (let ((val (nth 1 fields)))
280 (or (char-code-property-description
281 'canonical-combining-class (intern val))
282 val)))
49c2a2dc 283 (list "Bidi category"
f1f194de
KH
284 (let ((val (nth 1 fields)))
285 (or (char-code-property-description
286 'bidi-class (intern val))
287 val)))
49c2a2dc
SM
288 (list
289 "Decomposition"
290 (if (nth 4 fields)
291 (let* ((parts (split-string (nth 4 fields)))
292 (info (car parts)))
293 (if (string-match "\\`<\\(.+\\)>\\'" info)
294 (setq info (match-string 1 info))
295 (setq info nil))
296 (if info (setq parts (cdr parts)))
49c2a2dc
SM
297 (setq parts (mapconcat
298 (lambda (arg)
f1f194de 299 (string (string-to-number arg 16)))
49c2a2dc
SM
300 parts " "))
301 (concat info parts))))
302 (list "Decimal digit value"
303 (nth 5 fields))
304 (list "Digit value"
305 (nth 6 fields))
306 (list "Numeric value"
307 (nth 7 fields))
308 (list "Mirrored"
309 (if (equal "Y" (nth 8 fields))
310 "yes"))
311 (list "Old name" (nth 9 fields))
312 (list "ISO 10646 comment" (nth 10 fields))
313 (list "Uppercase" (and (nth 11 fields)
f1f194de
KH
314 (string (string-to-number
315 (nth 11 fields) 16))))
49c2a2dc 316 (list "Lowercase" (and (nth 12 fields)
f1f194de
KH
317 (string (string-to-number
318 (nth 12 fields) 16))))
49c2a2dc 319 (list "Titlecase" (and (nth 13 fields)
f1f194de
KH
320 (string (string-to-number
321 (nth 13 fields) 16)))))))))))
7fb0741b 322
aa360da1
GM
323;; Not defined on builds without X, but behind display-graphic-p.
324(declare-function internal-char-font "fontset.c" (position &optional ch))
325
7fb0741b
KH
326;; Return information about how CHAR is displayed at the buffer
327;; position POS. If the selected frame is on a graphic display,
7a674474
KH
328;; return a string "FONT-DRIVER:FONT-NAME (GLYPH-CODE)" where:
329;; FONT-DRIVER is the font-driver name,
330;; FONT-NAME is the font name,
331;; GLYPH-CODE is a hexadigit string representing the glyph-ID.
332;; Otherwise, return a string describing the terminal codes for the
333;; character.
7fb0741b
KH
334(defun describe-char-display (pos char)
335 (if (display-graphic-p (selected-frame))
e463f071 336 (let ((char-font-info (internal-char-font pos char)))
a6bbf9f4 337 (if char-font-info
7a674474 338 (let ((type (font-get (car char-font-info) :type))
c57b496b 339 (name (font-xlfd-name (car char-font-info)))
7a674474
KH
340 (code (cdr char-font-info)))
341 (if (integerp code)
342 (format "%s:%s (#x%02X)" type name code)
343 (format "%s:%s (#x%04X%04X)"
344 type name (car code) (cdr code))))))
dfc7a783 345 (let* ((charset (get-text-property pos 'charset))
0ba13131 346 (coding (or (terminal-coding-system) 'us-ascii))
dfc7a783 347 (encoded (encode-coding-char char coding charset)))
7fb0741b 348 (if encoded
96065fce 349 (encoded-string-description encoded coding)))))
7fb0741b 350
d6c135fb 351\f
7a674474
KH
352;; Return a string of CH with composition for padding on both sides.
353;; It is displayed without overlapping with the left/right columns.
354(defsubst describe-char-padded-string (ch)
355 (compose-string (string ch) 0 1 (format "\t%c\t" ch)))
356
d703f938
JB
357;; Return a nicely formated list of categories; extended category
358;; description is added to the category name as a tooltip
359(defsubst describe-char-categories (category-set)
360 (let ((mnemonics (category-set-mnemonics category-set)))
361 (unless (eq mnemonics "")
362 (list (mapconcat
363 #'(lambda (x)
364 (let* ((c (category-docstring x))
365 (doc (if (string-match "\\`\\(.*?\\)\n\\(.*\\)\\'" c)
366 (propertize (match-string 1 c)
367 'help-echo (match-string 2 c))
368 c)))
369 (format "%c:%s" x doc)))
370 mnemonics ", ")))))
371
4adb7c09
RS
372;;;###autoload
373(defun describe-char (pos)
374 "Describe the character after POS (interactively, the character after point).
375The information includes character code, charset and code points in it,
376syntax, category, how the character is encoded in a file,
377character composition information (if relevant),
378as well as widgets, buttons, overlays, and text properties."
379 (interactive "d")
4adb7c09
RS
380 (if (>= pos (point-max))
381 (error "No character follows specified position"))
382 (let* ((char (char-after pos))
2d3e4f02
KH
383 (eight-bit-p (and (not enable-multibyte-characters) (>= char 128)))
384 (charset (if eight-bit-p 'eight-bit
385 (or (get-text-property pos 'charset) (char-charset char))))
ca9088e7 386 (composition (find-composition pos nil nil t))
7fb0741b
KH
387 (component-chars nil)
388 (display-table (or (window-display-table)
389 buffer-display-table
390 standard-display-table))
391 (disp-vector (and display-table (aref display-table char)))
4adb7c09 392 (multibyte-p enable-multibyte-characters)
6482d093
KH
393 (overlays (mapcar #'(lambda (o) (overlay-properties o))
394 (overlays-at pos)))
6398b88f
AS
395 (char-description (if (not multibyte-p)
396 (single-key-description char)
397 (if (< char 128)
398 (single-key-description char)
399 (string-to-multibyte
400 (char-to-string char)))))
e5a5c80c
NR
401 (text-props-desc
402 (let ((tmp-buf (generate-new-buffer " *text-props*")))
403 (unwind-protect
404 (progn
405 (describe-text-properties pos tmp-buf)
406 (with-current-buffer tmp-buf (buffer-string)))
407 (kill-buffer tmp-buf))))
327719ee 408 item-list max-width code)
831ccfa6 409
2d3e4f02
KH
410 (if multibyte-p
411 (or (setq code (encode-char char charset))
412 (setq charset (char-charset char)
413 code (encode-char char charset)))
414 (setq code char))
0ba13131
KH
415 (when composition
416 ;; When the composition is trivial (i.e. composed only with the
417 ;; current character itself without any alternate characters),
418 ;; we don't show the composition information. Otherwise, store
419 ;; two descriptive strings in the first two elments of
420 ;; COMPOSITION.
421 (or (catch 'tag
422 (let ((from (car composition))
423 (to (nth 1 composition))
424 (next (1+ pos))
425 (components (nth 2 composition))
426 ch)
427 (if (and (vectorp components) (vectorp (aref components 0)))
428 (let ((idx (- pos from))
429 (nglyphs (lgstring-glyph-len components))
430 (i 0) j glyph glyph-from)
431 ;; COMPONENTS is a gstring. Find a grapheme
432 ;; cluster containing the current character.
433 (while (and (< i nglyphs)
434 (setq glyph (lgstring-glyph components i))
435 (< (lglyph-to glyph) idx))
436 (setq i (1+ i)))
437 (if (or (not glyph) (= i nglyphs))
438 ;; The composition is broken.
439 (throw 'tag nil))
440 (setq glyph-from (lglyph-from glyph)
441 to (+ from (lglyph-to glyph) 1)
442 from (+ from glyph-from)
443 j i)
444 (while (and (< j nglyphs)
445 (setq glyph (lgstring-glyph components j))
446 (= (lglyph-from glyph) glyph-from))
447 (setq j (1+ j)))
448 (if (and (= i (1- j))
449 (setq glyph (lgstring-glyph components i))
450 (= char (lglyph-char glyph)))
451 ;; The composition is trivial.
452 (throw 'tag nil))
453 (nconc composition (list i (1- j))))
454 (dotimes (i (length components))
455 (if (integerp (setq ch (aref components i)))
456 (push (cons ch (describe-char-display pos ch))
457 component-chars)))
458 (setq component-chars (nreverse component-chars)))
459 (if (< from pos)
460 (if (< (1+ pos) to)
461 (setcar composition
462 (concat
463 " with the surrounding characters \""
464 (mapconcat 'describe-char-padded-string
465 (buffer-substring from pos) "")
466 "\" and \""
467 (mapconcat 'describe-char-padded-string
468 (buffer-substring (1+ pos) to) "")
469 "\""))
470 (setcar composition
471 (concat
472 " with the preceding character(s) \""
473 (mapconcat 'describe-char-padded-string
474 (buffer-substring from pos) "")
475 "\"")))
476 (if (< (1+ pos) to)
477 (setcar composition
478 (concat
479 " with the following character(s) \""
480 (mapconcat 'describe-char-padded-string
481 (buffer-substring (1+ pos) to) "")
482 "\""))
483 (setcar composition nil)))
484 (setcar (cdr composition)
485 (format "composed to form \"%s\" (see below)"
486 (buffer-substring from to)))))
487 (setq composition nil)))
488
ed441285
KH
489 (setq item-list
490 `(("character"
41882805 491 ,(format "%s (%d, #o%o, #x%x)"
e5a5c80c
NR
492 (apply 'propertize char-description
493 (text-properties-at pos))
41882805 494 char char char))
327719ee 495 ("preferred charset"
57d79b99 496 ,`(insert-text-button
e5a5c80c
NR
497 ,(symbol-name charset)
498 'type 'help-character-set 'help-args '(,charset))
ed441285
KH
499 ,(format "(%s)" (charset-description charset)))
500 ("code point"
879dfc97
KH
501 ,(let ((str (if (integerp code)
502 (format (if (< code 256) "0x%02X" "0x%04X") code)
503 (format "0x%04X%04X" (car code) (cdr code)))))
504 (if (<= (charset-dimension charset) 2)
41882805
MB
505 `(insert-text-button
506 ,str
507 'action (lambda (&rest ignore)
879dfc97
KH
508 (list-charset-chars ',charset)
509 (with-selected-window
510 (get-buffer-window "*Character List*" 0)
511 (goto-char (point-min))
512 (forward-line 2) ;Skip the header.
513 (let ((case-fold-search nil))
514 (if (search-forward ,(char-to-string char)
515 nil t)
516 (goto-char (match-beginning 0))))))
e7621494 517 'follow-link t
41882805
MB
518 'help-echo
519 "mouse-2, RET: show this character in its character set")
879dfc97 520 str)))
ed441285 521 ("syntax"
d8ac3d27 522 ,(let ((syntax (syntax-after pos)))
ed441285
KH
523 (with-temp-buffer
524 (internal-describe-syntax-value syntax)
525 (buffer-string))))
526 ("category"
2d3e4f02
KH
527 ,@(if (not eight-bit-p)
528 (let ((category-set (char-category-set char)))
529 (if category-set
530 (describe-char-categories category-set)
531 '("-- none --")))))
309473d0 532 ("to input"
2d3e4f02
KH
533 ,@(if (not eight-bit-p)
534 (let ((key-list (and (eq input-method-function
535 'quail-input-method)
536 (quail-find-key char))))
537 (if (consp key-list)
538 (list "type"
539 (mapconcat #'(lambda (x) (concat "\"" x "\""))
540 key-list " or ")
541 "with"
542 `(insert-text-button
543 ,current-input-method
544 'type 'help-input-method
545 'help-args '(,current-input-method)))))))
ed441285 546 ("buffer code"
2d3e4f02
KH
547 ,(if multibyte-p
548 (encoded-string-description
549 (string-as-unibyte (char-to-string char)) nil)
550 (format "#x%02X" char)))
ed441285 551 ("file code"
2d3e4f02
KH
552 ,@(if multibyte-p
553 (let* ((coding buffer-file-coding-system)
554 (encoded (encode-coding-char char coding charset)))
555 (if encoded
556 (list (encoded-string-description encoded coding)
557 (format "(encoded by coding system %S)" coding))
558 (list "not encodable by coding system"
559 (symbol-name coding))))
560 (list (format "#x%02X" char))))
ed441285
KH
561 ("display"
562 ,(cond
563 (disp-vector
564 (setq disp-vector (copy-sequence disp-vector))
565 (dotimes (i (length disp-vector))
ed441285 566 (aset disp-vector i
c57b496b
JB
567 (cons (aref disp-vector i)
568 (describe-char-display
569 pos (glyph-char (aref disp-vector i))))))
ed441285 570 (format "by display table entry [%s] (see below)"
8b18fb8f
JL
571 (mapconcat
572 #'(lambda (x)
da55bb96 573 (format "?%c" (glyph-char (car x))))
8b18fb8f 574 disp-vector " ")))
ed441285 575 (composition
0ba13131 576 (cadr composition))
ed441285
KH
577 (t
578 (let ((display (describe-char-display pos char)))
579 (if (display-graphic-p (selected-frame))
7fb0741b 580 (if display
7a674474 581 (concat "by this font (glyph code)\n " display)
ed441285
KH
582 "no font available")
583 (if display
584 (format "terminal code %s" display)
585 "not encodable for terminal"))))))
879dfc97
KH
586 ,@(let ((face
587 (if (not (or disp-vector composition))
588 (cond
589 ((and show-trailing-whitespace
590 (save-excursion (goto-char pos)
70583cb5 591 (looking-at-p "[ \t]+$")))
879dfc97
KH
592 'trailing-whitespace)
593 ((and nobreak-char-display char (eq char '#xa0))
594 'nobreak-space)
595 ((and nobreak-char-display char (eq char '#xad))
596 'escape-glyph)
597 ((and (< char 32) (not (memq char '(9 10))))
598 'escape-glyph)))))
599 (if face (list (list "hardcoded face"
2fd54bf8
JL
600 `(insert-text-button
601 ,(symbol-name face)
602 'type 'help-face 'help-args '(,face))))))
2d3e4f02
KH
603 ,@(if (not eight-bit-p)
604 (let ((unicodedata (describe-char-unicode-data char)))
605 (if unicodedata
606 (cons (list "Unicode data" " ") unicodedata))))))
95eede5c 607 (setq max-width (apply #'max (mapcar #'(lambda (x)
879dfc97 608 (if (cadr x) (length (car x)) 0))
4adb7c09 609 item-list)))
bfd94110 610 (help-setup-xref nil (interactive-p))
3db0e8bf 611 (with-help-window (help-buffer)
ca9088e7 612 (with-current-buffer standard-output
4adb7c09
RS
613 (set-buffer-multibyte multibyte-p)
614 (let ((formatter (format "%%%ds:" max-width)))
615 (dolist (elt item-list)
831ccfa6
DL
616 (when (cadr elt)
617 (insert (format formatter (car elt)))
618 (dolist (clm (cdr elt))
57d79b99 619 (if (eq (car-safe clm) 'insert-text-button)
fedbc8e5
JL
620 (progn (insert " ") (eval clm))
621 (when (>= (+ (current-column)
70583cb5 622 (or (string-match-p "\n" clm)
fedbc8e5
JL
623 (string-width clm))
624 1)
625 (window-width))
626 (insert "\n")
627 (indent-to (1+ max-width)))
628 (insert " " clm)))
831ccfa6 629 (insert "\n"))))
7fb0741b 630
086c5b2b
KH
631 (when overlays
632 (save-excursion
633 (goto-char (point-min))
634 (re-search-forward "character:[ \t\n]+")
c57b496b 635 (let ((end (+ (point) (length char-description))))
6398b88f 636 (mapc #'(lambda (props)
086c5b2b 637 (let ((o (make-overlay (point) end)))
6398b88f
AS
638 (while props
639 (overlay-put o (car props) (nth 1 props))
640 (setq props (cddr props)))))
086c5b2b 641 overlays))))
ed441285 642
7fb0741b
KH
643 (when disp-vector
644 (insert
645 "\nThe display table entry is displayed by ")
646 (if (display-graphic-p (selected-frame))
647 (progn
648 (insert "these fonts (glyph codes):\n")
649 (dotimes (i (length disp-vector))
da55bb96 650 (insert (glyph-char (car (aref disp-vector i))) ?:
7fb0741b 651 (propertize " " 'display '(space :align-to 5))
c57b496b 652 (or (cdr (aref disp-vector i)) "-- no font --")
fedbc8e5 653 "\n")
da55bb96
KS
654 (let ((face (glyph-face (car (aref disp-vector i)))))
655 (when face
656 (insert (propertize " " 'display '(space :align-to 5))
657 "face: ")
658 (insert (concat "`" (symbol-name face) "'"))
659 (insert "\n")))))
7fb0741b
KH
660 (insert "these terminal codes:\n")
661 (dotimes (i (length disp-vector))
662 (insert (car (aref disp-vector i))
663 (propertize " " 'display '(space :align-to 5))
664 (or (cdr (aref disp-vector i)) "-- not encodable --")
665 "\n"))))
666
4adb7c09 667 (when composition
7fb0741b
KH
668 (insert "\nComposed")
669 (if (car composition)
0ba13131 670 (insert (car composition)))
3f8b1daa
KH
671 (if (and (vectorp (nth 2 composition))
672 (vectorp (aref (nth 2 composition) 0)))
f5a60b43
KH
673 (let* ((gstring (nth 2 composition))
674 (font (lgstring-font gstring))
0ba13131
KH
675 (from (nth 3 composition))
676 (to (nth 4 composition))
f5a60b43 677 glyph)
e47fe93b 678 (if (fontp font)
a62ac7e3
KH
679 (progn
680 (insert " using this font:\n "
681 (symbol-name (font-get font :type))
682 ?:
683 (aref (query-font font) 0)
684 "\nby these glyphs:\n")
0ba13131
KH
685 (while (and (<= from to)
686 (setq glyph (lgstring-glyph gstring from)))
a62ac7e3 687 (insert (format " %S\n" glyph))
0ba13131 688 (setq from (1+ from))))
a62ac7e3 689 (insert " by these characters:\n")
0ba13131
KH
690 (while (and (<= from to)
691 (setq glyph (lgstring-glyph gstring from)))
a62ac7e3
KH
692 (insert (format " %c (#x%d)\n"
693 (lglyph-char glyph) (lglyph-char glyph)))
0ba13131 694 (setq from (1+ from)))))
7a674474
KH
695 (insert " by the rule:\n\t(")
696 (let ((first t))
c57b496b 697 (mapc (lambda (x)
7a674474
KH
698 (if first (setq first nil)
699 (insert " "))
700 (if (consp x) (insert (format "%S" x))
701 (if (= x ?\t) (insert (single-key-description x))
702 (insert ??)
703 (insert (describe-char-padded-string x)))))
704 (nth 2 composition)))
705 (insert ")\nThe component character(s) are displayed by ")
3f8b1daa
KH
706 (if (display-graphic-p (selected-frame))
707 (progn
708 (insert "these fonts (glyph codes):")
709 (dolist (elt component-chars)
710 (if (/= (car elt) ?\t)
c57b496b 711 (insert "\n "
7a674474
KH
712 (describe-char-padded-string (car elt))
713 ?:
3f8b1daa 714 (propertize " " 'display '(space :align-to 5))
7a674474 715 (or (cdr elt) "-- no font --")))))
3f8b1daa
KH
716 (insert "these terminal codes:")
717 (dolist (elt component-chars)
718 (insert "\n " (car elt) ":"
719 (propertize " " 'display '(space :align-to 4))
720 (or (cdr elt) "-- not encodable --"))))
721 (insert "\nSee the variable `reference-point-alist' for "
722 "the meaning of the rule.\n")))
3a73f428 723
2d3e4f02
KH
724 (unless eight-bit-p
725 (insert (if (not describe-char-unidata-list)
726 "\nCharacter code properties are not shown: "
727 "\nCharacter code properties: "))
728 (insert-text-button
729 "customize what to show"
730 'action (lambda (&rest ignore)
731 (customize-variable
e7621494
CY
732 'describe-char-unidata-list))
733 'follow-link t)
2d3e4f02
KH
734 (insert "\n")
735 (dolist (elt (if (eq describe-char-unidata-list t)
736 (nreverse (mapcar 'car char-code-property-alist))
737 describe-char-unidata-list))
738 (let ((val (get-char-code-property char elt))
739 description)
740 (when val
741 (setq description (char-code-property-description elt val))
742 (insert (if description
743 (format " %s: %s (%s)\n" elt val description)
744 (format " %s: %s\n" elt val)))))))
f1f194de 745
25a3c9d1 746 (if text-props-desc (insert text-props-desc))
2fd54bf8 747 (setq help-xref-stack-item (list 'help-insert-string (buffer-string)))
3db0e8bf 748 (toggle-read-only 1)))))
4adb7c09 749
781424c2 750(define-obsolete-function-alias 'describe-char-after 'describe-char "22.1")
831ccfa6 751
288395a7
CW
752(provide 'descr-text)
753
d8ac3d27 754;; arch-tag: fc55a498-f3e9-4312-b5bd-98cc02480af1
2a1e884e 755;;; descr-text.el ends here