descr-text.el 36 KB

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