descr-text.el 41 KB

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