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