descr-text.el 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. ;;; descr-text.el --- describe text mode -*- lexical-binding:t -*-
  2. ;; Copyright (C) 1994-1996, 2001-2017 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 2 fields)))
  258. (or (char-code-property-description
  259. 'canonical-combining-class (intern val))
  260. val)))
  261. (list "Bidi category"
  262. (let ((val (nth 3 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 "font.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 (< char 128)
  385. (single-key-description char)
  386. (string (if (not multibyte-p)
  387. (decode-char 'eight-bit char)
  388. 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 left-to-right directional
  404. ;; embeddings and overrides, to prevent potential messup of the
  405. ;; following text.
  406. ((memq char '(?\x202a ?\x202d))
  407. (setq char-description
  408. (concat char-description
  409. (propertize (string ?\x202c) 'invisible t))))
  410. ;; Append a PDF character followed by LRM to right-to-left
  411. ;; directional embeddings and overrides, to prevent potential
  412. ;; messup of the following numerical text.
  413. ((memq char '(?\x202b ?\x202e))
  414. (setq char-description
  415. (concat char-description
  416. (propertize (string ?\x202c ?\x200e) 'invisible t))))
  417. ;; Append a PDI character to directional isolate initiators, to
  418. ;; prevent potential messup of the following numerical text
  419. ((memq char '(?\x2066 ?\x2067 ?\x2068))
  420. (setq char-description
  421. (concat char-description
  422. (propertize (string ?\x2069) 'invisible t))))
  423. ;; Append a LRM character to any strong character to avoid
  424. ;; messing up the numerical codepoint.
  425. ((memq (get-char-code-property char 'bidi-class) '(R AL))
  426. (setq char-description
  427. (concat char-description
  428. (propertize (string ?\x200e) 'invisible t)))))
  429. (when composition
  430. ;; When the composition is trivial (i.e. composed only with the
  431. ;; current character itself without any alternate characters),
  432. ;; we don't show the composition information. Otherwise, store
  433. ;; two descriptive strings in the first two elements of
  434. ;; COMPOSITION.
  435. (or (catch 'tag
  436. (let ((from (car composition))
  437. (to (nth 1 composition))
  438. (components (nth 2 composition))
  439. ch)
  440. (if (and (vectorp components) (vectorp (aref components 0)))
  441. (let ((idx (- pos from))
  442. (nglyphs (lgstring-glyph-len components))
  443. (i 0) j glyph glyph-from)
  444. ;; COMPONENTS is a gstring. Find a grapheme
  445. ;; cluster containing the current character.
  446. (while (and (< i nglyphs)
  447. (setq glyph (lgstring-glyph components i))
  448. (< (lglyph-to glyph) idx))
  449. (setq i (1+ i)))
  450. (if (or (not glyph) (= i nglyphs))
  451. ;; The composition is broken.
  452. (throw 'tag nil))
  453. (setq glyph-from (lglyph-from glyph)
  454. to (+ from (lglyph-to glyph) 1)
  455. from (+ from glyph-from)
  456. j i)
  457. (while (and (< j nglyphs)
  458. (setq glyph (lgstring-glyph components j))
  459. (= (lglyph-from glyph) glyph-from))
  460. (setq j (1+ j)))
  461. (if (and (= to (1+ from))
  462. (= i (1- j))
  463. (setq glyph (lgstring-glyph components i))
  464. (= char (lglyph-char glyph)))
  465. ;; The composition is trivial.
  466. (throw 'tag nil))
  467. (nconc composition (list i (1- j))))
  468. (dotimes (i (length components))
  469. (if (integerp (setq ch (aref components i)))
  470. (push (cons ch (describe-char-display pos ch))
  471. component-chars)))
  472. (setq component-chars (nreverse component-chars)))
  473. (if (< from pos)
  474. (if (< (1+ pos) to)
  475. (setcar composition
  476. (concat
  477. " with the surrounding characters \""
  478. (mapconcat 'describe-char-padded-string
  479. (buffer-substring from pos) "")
  480. "\" and \""
  481. (mapconcat 'describe-char-padded-string
  482. (buffer-substring (1+ pos) to) "")
  483. "\""))
  484. (setcar composition
  485. (concat
  486. " with the preceding character(s) \""
  487. (mapconcat 'describe-char-padded-string
  488. (buffer-substring from pos) "")
  489. "\"")))
  490. (if (< (1+ pos) to)
  491. (setcar composition
  492. (concat
  493. " with the following character(s) \""
  494. (mapconcat 'describe-char-padded-string
  495. (buffer-substring (1+ pos) to) "")
  496. "\""))
  497. (setcar composition nil)))
  498. (setcar (cdr composition)
  499. (format "composed to form \"%s\" (see below)"
  500. (buffer-substring from to)))))
  501. (setq composition nil)))
  502. (setq item-list
  503. `(("position"
  504. ,(let* ((beg (point-min))
  505. (end (point-max))
  506. (total (buffer-size))
  507. (percent (round (* 100.0 (1- pos)) (max total 1)))
  508. (hscroll (if (= (window-hscroll) 0)
  509. ""
  510. (format ", Hscroll: %d" (window-hscroll))))
  511. (col (current-column)))
  512. (if (or (/= beg 1) (/= end (1+ total)))
  513. (format "%d of %d (%d%%), restriction: <%d-%d>, column: %d%s"
  514. pos total percent beg end col hscroll)
  515. (if (= pos end)
  516. (format "%d of %d (EOB), column: %d%s" pos total col hscroll)
  517. (format "%d of %d (%d%%), column: %d%s"
  518. pos total percent col hscroll)))))
  519. ("character"
  520. ,(format "%s (displayed as %s) (codepoint %d, #o%o, #x%x)"
  521. char-description
  522. (apply 'propertize char-description
  523. (text-properties-at pos))
  524. char char char))
  525. ("preferred charset"
  526. ,`(insert-text-button
  527. ,(symbol-name charset)
  528. 'type 'help-character-set 'help-args '(,charset))
  529. ,(format "(%s)" (charset-description charset)))
  530. ("code point in charset"
  531. ,(let ((str (if (integerp code)
  532. (format (if (< code 256) "0x%02X" "0x%04X")
  533. code)
  534. (format "0x%04X%04X" (car code) (cdr code)))))
  535. (if (<= (charset-dimension charset) 2)
  536. `(insert-text-button
  537. ,str
  538. 'action (lambda (&rest ignore)
  539. (list-charset-chars ',charset)
  540. (with-selected-window
  541. (get-buffer-window "*Character List*" 0)
  542. (goto-char (point-min))
  543. (forward-line 2) ;Skip the header.
  544. (let ((case-fold-search nil))
  545. (if (search-forward
  546. ,(char-to-string char) nil t)
  547. (goto-char (match-beginning 0))))))
  548. 'follow-link t
  549. 'help-echo
  550. "mouse-2, RET: show this character in its character set")
  551. str)))
  552. ,@(let ((script (aref char-script-table char)))
  553. (if script
  554. (list (list "script" (symbol-name script)))))
  555. ("syntax"
  556. ,(let ((syntax (syntax-after pos)))
  557. (with-temp-buffer
  558. (internal-describe-syntax-value syntax)
  559. (buffer-string))))
  560. ("category"
  561. ,@(if (not eight-bit-p)
  562. (let ((category-set (char-category-set char)))
  563. (if category-set
  564. (describe-char-categories category-set)
  565. '("-- none --")))))
  566. ("to input"
  567. ,@(if (not eight-bit-p)
  568. (let ((key-list (and (eq input-method-function
  569. 'quail-input-method)
  570. (quail-find-key char))))
  571. (if (consp key-list)
  572. (list "type"
  573. (concat "\""
  574. (mapconcat 'identity
  575. key-list "\" or \"")
  576. "\"")
  577. "with"
  578. `(insert-text-button
  579. ,current-input-method
  580. 'type 'help-input-method
  581. 'help-args '(,current-input-method))
  582. "input method")
  583. (list
  584. (let* ((names (ucs-names))
  585. (name
  586. (or (when (= char 7)
  587. ;; Special case for "BELL" which is
  588. ;; apparently the only char which
  589. ;; doesn't have a new name and whose
  590. ;; old-name is shadowed by a newer char
  591. ;; with that name (bug#25641).
  592. (car (rassoc char names)))
  593. (get-char-code-property char 'name)
  594. (get-char-code-property char 'old-name))))
  595. (if (and name (assoc-string name names))
  596. (format
  597. "type \"C-x 8 RET %x\" or \"C-x 8 RET %s\""
  598. char name)
  599. (format "type \"C-x 8 RET %x\"" char))))))))
  600. ("buffer code"
  601. ,(if multibyte-p
  602. (encoded-string-description
  603. (encode-coding-string (char-to-string char)
  604. 'emacs-internal)
  605. nil)
  606. (format "#x%02X" char)))
  607. ("file code"
  608. ,@(if multibyte-p
  609. (let* ((coding buffer-file-coding-system)
  610. (encoded (encode-coding-char char coding charset)))
  611. (if encoded
  612. (list (encoded-string-description encoded coding)
  613. (format "(encoded by coding system %S)"
  614. coding))
  615. (list "not encodable by coding system"
  616. (symbol-name coding))))
  617. (list (format "#x%02X" char))))
  618. ("display"
  619. ,(cond
  620. (disp-vector
  621. (setq disp-vector (copy-sequence disp-vector))
  622. (dotimes (i (length disp-vector))
  623. (aset disp-vector i
  624. (cons (aref disp-vector i)
  625. (describe-char-display
  626. pos (glyph-char (aref disp-vector i))))))
  627. (format "by display table entry [%s] (see below)"
  628. (mapconcat
  629. (lambda (x)
  630. (format "?%c" (glyph-char (car x))))
  631. disp-vector " ")))
  632. (composition
  633. (cadr composition))
  634. (t
  635. (let ((display (describe-char-display pos char)))
  636. (if (display-graphic-p (selected-frame))
  637. (if display
  638. (concat "by this font (glyph code)\n " display)
  639. "no font available")
  640. (if display
  641. (format "terminal code %s" display)
  642. "not encodable for terminal"))))))
  643. ,@(let ((face
  644. (if (not (or disp-vector composition))
  645. (cond
  646. ((and show-trailing-whitespace
  647. (save-excursion (goto-char pos)
  648. (looking-at-p "[ \t]+$")))
  649. 'trailing-whitespace)
  650. ((and nobreak-char-display char (eq char '#xa0))
  651. 'nobreak-space)
  652. ((and nobreak-char-display char
  653. (memq char '(#xad #x2010 #x2011)))
  654. 'escape-glyph)
  655. ((and (< char 32) (not (memq char '(9 10))))
  656. 'escape-glyph)))))
  657. (if face (list (list "hardcoded face"
  658. `(insert-text-button ;FIXME: Wrap in lambda!
  659. ,(symbol-name face)
  660. 'type 'help-face
  661. 'help-args '(,face))))))
  662. ,@(if (not eight-bit-p)
  663. (let ((unicodedata (describe-char-unicode-data char)))
  664. (if unicodedata
  665. (cons (list "Unicode data" "") unicodedata))))))
  666. (setq max-width (apply 'max (mapcar (lambda (x)
  667. (if (cadr x) (length (car x)) 0))
  668. item-list)))
  669. (set-buffer src-buf)
  670. (help-setup-xref (list 'describe-char pos buffer)
  671. (called-interactively-p 'interactive))
  672. (with-help-window (help-buffer)
  673. (with-current-buffer standard-output
  674. (let ((formatter (format "%%%ds:" max-width)))
  675. (dolist (elt item-list)
  676. (when (cadr elt)
  677. (insert (format formatter (car elt)))
  678. (dolist (clm (cdr elt))
  679. (cond ((eq (car-safe clm) 'insert-text-button)
  680. (insert " ")
  681. (eval clm))
  682. ((not (zerop (length clm)))
  683. (insert " " clm))))
  684. (insert "\n"))))
  685. (when overlays
  686. (save-excursion
  687. (goto-char (point-min))
  688. (re-search-forward "(displayed as ")
  689. (let ((end (+ (point) (length char-description))))
  690. (mapc (lambda (props)
  691. (let ((o (make-overlay (point) end)))
  692. (while props
  693. (overlay-put o (car props) (nth 1 props))
  694. (setq props (cddr props)))))
  695. overlays))))
  696. (when disp-vector
  697. (insert
  698. "\nThe display table entry is displayed by ")
  699. (insert "these fonts (glyph codes):\n")
  700. (dotimes (i (length disp-vector))
  701. (insert (glyph-char (car (aref disp-vector i))) ?:
  702. (propertize " " 'display '(space :align-to 5))
  703. (or (cdr (aref disp-vector i)) "-- no font --")
  704. "\n")
  705. (let ((face (glyph-face (car (aref disp-vector i)))))
  706. (when face
  707. (insert (propertize " " 'display '(space :align-to 5))
  708. "face: ")
  709. (insert (format-message "`%s'\n" face))))))
  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
  780. (cond ((eq describe-char-unidata-list t)
  781. (nreverse (mapcar 'car char-code-property-alist)))
  782. ((< char 32)
  783. ;; Temporary fix (2016-05-22): The
  784. ;; decomposition item for \n corrupts the
  785. ;; display on a Linux virtual terminal.
  786. ;; (Bug #23594).
  787. (remq 'decomposition describe-char-unidata-list))
  788. (t describe-char-unidata-list)))
  789. (let ((val (get-char-code-property char elt))
  790. description)
  791. (when val
  792. (setq description (char-code-property-description elt val))
  793. (insert (if description
  794. (format " %s: %s (%s)\n" elt val description)
  795. (format " %s: %s\n" elt val)))))))
  796. (if text-props-desc (insert text-props-desc))
  797. (setq buffer-read-only t))))))
  798. (define-obsolete-function-alias 'describe-char-after 'describe-char "22.1")
  799. ;;; Describe-Char-ElDoc
  800. (defun describe-char-eldoc--truncate (name width)
  801. "Truncate NAME at white spaces such that it is no longer than WIDTH.
  802. Split NAME on white space character and return string with as
  803. many leading words of NAME as possible without exceeding WIDTH
  804. characters. If NAME consists of white space characters only,
  805. return an empty string. Three dots (\"...\") are appended to
  806. returned string if some of the words from NAME have been omitted.
  807. NB: Function may return string longer than WIDTH if name consists
  808. of a single word, or it's first word is longer than WIDTH
  809. characters."
  810. (let ((words (split-string name)))
  811. (if words
  812. (let ((last words))
  813. (setq width (- width (length (car words))))
  814. (while (and (cdr last)
  815. (<= (+ (length (cadr last)) (if (cddr last) 4 1)) width))
  816. (setq last (cdr last))
  817. (setq width (- width (length (car last)) 1)))
  818. (let ((ellipsis (and (cdr last) "...")))
  819. (setcdr last nil)
  820. (concat (mapconcat 'identity words " ") ellipsis)))
  821. "")))
  822. (defun describe-char-eldoc--format (ch &optional width)
  823. "Format a description for character CH which is no more than WIDTH characters.
  824. Full description message has a \"U+HEX: NAME (GC: GENERAL-CATEGORY)\"
  825. format where:
  826. - HEX is a hexadecimal codepoint of the character (zero-padded to at
  827. least four digits),
  828. - NAME is name of the character.
  829. - GC is a two-letter abbreviation of the general-category of the
  830. character, and
  831. - GENERAL-CATEGORY is full name of the general-category of the
  832. character.
  833. If WIDTH is non-nil some elements of the description may be
  834. omitted to accommodate the length restriction. Under certain
  835. condition, the function may return string longer than WIDTH, see
  836. `describe-char-eldoc--truncate'."
  837. (let ((name (get-char-code-property ch 'name)))
  838. (when name
  839. (let* ((code (propertize (format "U+%04X" ch)
  840. 'face 'font-lock-constant-face))
  841. (gc (get-char-code-property ch 'general-category))
  842. (gc-desc (char-code-property-description 'general-category gc)))
  843. (unless (or (not width) (<= (length name) width))
  844. (setq name (describe-char-eldoc--truncate name width)))
  845. (setq name (concat (substring name 0 1) (downcase (substring name 1))))
  846. (setq name (propertize name 'face 'font-lock-variable-name-face))
  847. (setq gc (propertize (symbol-name gc) 'face 'font-lock-comment-face))
  848. (when gc-desc
  849. (setq gc-desc (propertize gc-desc 'face 'font-lock-comment-face)))
  850. (let ((lcode (length code))
  851. (lname (length name))
  852. (lgc (length gc))
  853. (lgc-desc (and gc-desc (length gc-desc))))
  854. (cond
  855. ((and gc-desc
  856. (or (not width) (<= (+ lcode lname lgc lgc-desc 7) width)))
  857. (concat code ": " name " (" gc ": " gc-desc ")"))
  858. ((and gc-desc (<= (+ lcode lname lgc-desc 5) width))
  859. (concat code ": " name " (" gc-desc ")"))
  860. ((or (not width) (<= (+ lcode lname lgc 5) width))
  861. (concat code ": " name " (" gc ")"))
  862. ((<= (+ lname lgc 3) width)
  863. (concat name " (" gc ")"))
  864. (t name)))))))
  865. ;;;###autoload
  866. (defun describe-char-eldoc ()
  867. "Return a description of character at point for use by ElDoc mode.
  868. Return nil if character at point is a printable ASCII
  869. character (i.e. codepoint between 32 and 127 inclusively).
  870. Otherwise return a description formatted by
  871. `describe-char-eldoc--format' function taking into account value
  872. of `eldoc-echo-area-use-multiline-p' variable and width of
  873. minibuffer window for width limit.
  874. This function is meant to be used as a value of
  875. `eldoc-documentation-function' variable."
  876. (let ((ch (following-char)))
  877. (when (and (not (zerop ch)) (or (< ch 32) (> ch 127)))
  878. (describe-char-eldoc--format
  879. ch
  880. (unless (eq eldoc-echo-area-use-multiline-p t)
  881. (1- (window-width (minibuffer-window))))))))
  882. (provide 'descr-text)
  883. ;;; descr-text.el ends here