disp-table.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. ;;; disp-table.el --- functions for dealing with char tables
  2. ;; Copyright (C) 1987, 1994-1995, 1999, 2001-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Erik Naggum <erik@naggum.no>
  5. ;; Based on a previous version by Howard Gayle
  6. ;; Maintainer: FSF
  7. ;; Keywords: i18n
  8. ;; Package: emacs
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;;; Code:
  22. (put 'display-table 'char-table-extra-slots 6)
  23. ;;;###autoload
  24. (defun make-display-table ()
  25. "Return a new, empty display table."
  26. (make-char-table 'display-table nil))
  27. (or standard-display-table
  28. (setq standard-display-table (make-display-table)))
  29. ;;; Display-table slot names. The property value says which slot.
  30. (put 'truncation 'display-table-slot 0)
  31. (put 'wrap 'display-table-slot 1)
  32. (put 'escape 'display-table-slot 2)
  33. (put 'control 'display-table-slot 3)
  34. (put 'selective-display 'display-table-slot 4)
  35. (put 'vertical-border 'display-table-slot 5)
  36. ;;;###autoload
  37. (defun display-table-slot (display-table slot)
  38. "Return the value of the extra slot in DISPLAY-TABLE named SLOT.
  39. SLOT may be a number from 0 to 5 inclusive, or a slot name (symbol).
  40. Valid symbols are `truncation', `wrap', `escape', `control',
  41. `selective-display', and `vertical-border'."
  42. (let ((slot-number
  43. (if (numberp slot) slot
  44. (or (get slot 'display-table-slot)
  45. (error "Invalid display-table slot name: %s" slot)))))
  46. (char-table-extra-slot display-table slot-number)))
  47. ;;;###autoload
  48. (defun set-display-table-slot (display-table slot value)
  49. "Set the value of the extra slot in DISPLAY-TABLE named SLOT to VALUE.
  50. SLOT may be a number from 0 to 5 inclusive, or a name (symbol).
  51. Valid symbols are `truncation', `wrap', `escape', `control',
  52. `selective-display', and `vertical-border'."
  53. (let ((slot-number
  54. (if (numberp slot) slot
  55. (or (get slot 'display-table-slot)
  56. (error "Invalid display-table slot name: %s" slot)))))
  57. (set-char-table-extra-slot display-table slot-number value)))
  58. ;;;###autoload
  59. (defun describe-display-table (dt)
  60. "Describe the display table DT in a help buffer."
  61. (with-help-window "*Help*"
  62. (princ "\nTruncation glyph: ")
  63. (prin1 (display-table-slot dt 'truncation))
  64. (princ "\nWrap glyph: ")
  65. (prin1 (display-table-slot dt 'wrap))
  66. (princ "\nEscape glyph: ")
  67. (prin1 (display-table-slot dt 'escape))
  68. (princ "\nCtrl glyph: ")
  69. (prin1 (display-table-slot dt 'control))
  70. (princ "\nSelective display glyph sequence: ")
  71. (prin1 (display-table-slot dt 'selective-display))
  72. (princ "\nVertical window border glyph: ")
  73. (prin1 (display-table-slot dt 'vertical-border))
  74. (princ "\nCharacter display glyph sequences:\n")
  75. (with-current-buffer standard-output
  76. (let ((vector (make-vector 256 nil))
  77. (i 0))
  78. (while (< i 256)
  79. (aset vector i (aref dt i))
  80. (setq i (1+ i)))
  81. (describe-vector
  82. vector 'display-table-print-array))
  83. (help-mode))))
  84. (defun display-table-print-array (desc)
  85. (insert "[")
  86. (let ((column (current-column))
  87. (width (window-width))
  88. string)
  89. (dotimes (i (length desc))
  90. (setq string (format "%s" (aref desc i)))
  91. (cond
  92. ((>= (+ (current-column) (length string) 1)
  93. width)
  94. (insert "\n")
  95. (insert (make-string column ? )))
  96. ((> i 0)
  97. (insert " ")))
  98. (insert string)))
  99. (insert "]\n"))
  100. ;;;###autoload
  101. (defun describe-current-display-table ()
  102. "Describe the display table in use in the selected window and buffer."
  103. (interactive)
  104. (let ((disptab (or (window-display-table (selected-window))
  105. buffer-display-table
  106. standard-display-table)))
  107. (if disptab
  108. (describe-display-table disptab)
  109. (message "No display table"))))
  110. ;;;###autoload
  111. (defun standard-display-8bit (l h)
  112. "Display characters representing raw bytes in the range L to H literally.
  113. On a terminal display, each character in the range is displayed
  114. by sending the corresponding byte directly to the terminal.
  115. On a graphic display, each character in the range is displayed
  116. using the default font by a glyph whose code is the corresponding
  117. byte.
  118. Note that ASCII printable characters (SPC to TILDA) are displayed
  119. in the default way after this call."
  120. (or standard-display-table
  121. (setq standard-display-table (make-display-table)))
  122. (if (> h 255)
  123. (setq h 255))
  124. (while (<= l h)
  125. (if (< l 128)
  126. (aset standard-display-table l
  127. (if (or (< l ?\s) (= l 127)) (vector l)))
  128. (let ((c (unibyte-char-to-multibyte l)))
  129. (aset standard-display-table c (vector c))))
  130. (setq l (1+ l))))
  131. ;;;###autoload
  132. (defun standard-display-default (l h)
  133. "Display characters in the range L to H using the default notation."
  134. (or standard-display-table
  135. (setq standard-display-table (make-display-table)))
  136. (while (<= l h)
  137. (if (and (>= l ?\s) (characterp l))
  138. (aset standard-display-table l nil))
  139. (setq l (1+ l))))
  140. ;; This function does NOT take terminal-dependent escape sequences.
  141. ;; For that, you need to go through create-glyph. Use one of the
  142. ;; other functions below, or roll your own.
  143. ;;;###autoload
  144. (defun standard-display-ascii (c s)
  145. "Display character C using printable string S."
  146. (or standard-display-table
  147. (setq standard-display-table (make-display-table)))
  148. (aset standard-display-table c (vconcat s)))
  149. ;;;###autoload
  150. (defun standard-display-g1 (c sc)
  151. "Display character C as character SC in the g1 character set.
  152. This function assumes that your terminal uses the SO/SI characters;
  153. it is meaningless for an X frame."
  154. (if (memq window-system '(x w32 ns))
  155. (error "Cannot use string glyphs in a windowing system"))
  156. (or standard-display-table
  157. (setq standard-display-table (make-display-table)))
  158. (aset standard-display-table c
  159. (vector (create-glyph (concat "\016" (char-to-string sc) "\017")))))
  160. ;;;###autoload
  161. (defun standard-display-graphic (c gc)
  162. "Display character C as character GC in graphics character set.
  163. This function assumes VT100-compatible escapes; it is meaningless for an
  164. X frame."
  165. (if (memq window-system '(x w32 ns))
  166. (error "Cannot use string glyphs in a windowing system"))
  167. (or standard-display-table
  168. (setq standard-display-table (make-display-table)))
  169. (aset standard-display-table c
  170. (vector (create-glyph (concat "\e(0" (char-to-string gc) "\e(B")))))
  171. ;;;###autoload
  172. (defun standard-display-underline (c uc)
  173. "Display character C as character UC plus underlining."
  174. (or standard-display-table
  175. (setq standard-display-table (make-display-table)))
  176. (aset standard-display-table c
  177. (vector
  178. (if window-system
  179. (make-glyph-code uc 'underline)
  180. (create-glyph (concat "\e[4m" (char-to-string uc) "\e[m"))))))
  181. ;;;###autoload
  182. (defun create-glyph (string)
  183. "Allocate a glyph code to display by sending STRING to the terminal."
  184. (if (= (length glyph-table) 65536)
  185. (error "No free glyph codes remain"))
  186. ;; Don't use slots that correspond to ASCII characters.
  187. (if (= (length glyph-table) 32)
  188. (setq glyph-table (vconcat glyph-table (make-vector 224 nil))))
  189. (setq glyph-table (vconcat glyph-table (list string)))
  190. (1- (length glyph-table)))
  191. ;;;###autoload
  192. (defun make-glyph-code (char &optional face)
  193. "Return a glyph code representing char CHAR with face FACE."
  194. ;; Due to limitations on Emacs integer values, faces with
  195. ;; face id greater that 512 are silently ignored.
  196. (if (not face)
  197. char
  198. (let ((fid (face-id face)))
  199. (if (< fid 64) ; we have 32 - 3(LSB) - 1(SIGN) - 22(CHAR) = 6 bits for face id
  200. (logior char (lsh fid 22))
  201. (cons char fid)))))
  202. ;;;###autoload
  203. (defun glyph-char (glyph)
  204. "Return the character of glyph code GLYPH."
  205. (if (consp glyph)
  206. (car glyph)
  207. (logand glyph #x3fffff)))
  208. ;;;###autoload
  209. (defun glyph-face (glyph)
  210. "Return the face of glyph code GLYPH, or nil if glyph has default face."
  211. (let ((face-id (if (consp glyph) (cdr glyph) (lsh glyph -22))))
  212. (and (> face-id 0)
  213. (catch 'face
  214. (dolist (face (face-list))
  215. (when (eq (face-id face) face-id)
  216. (throw 'face face)))))))
  217. ;;;###autoload
  218. (defun standard-display-european (arg)
  219. "Semi-obsolete way to toggle display of ISO 8859 European characters.
  220. This function is semi-obsolete; you probably don't need it, or else you
  221. probably should use `set-language-environment' or `set-locale-environment'.
  222. This function enables European character display if ARG is positive,
  223. disables it if negative. Otherwise, it toggles European character display.
  224. When this mode is enabled, characters in the range of 160 to 255
  225. display not as octal escapes, but as accented characters. Codes 146
  226. and 160 display as apostrophe and space, even though they are not the
  227. ASCII codes for apostrophe and space.
  228. Enabling European character display with this command noninteractively
  229. from Lisp code also selects Latin-1 as the language environment.
  230. This provides increased compatibility for users who call this function
  231. in `.emacs'."
  232. (if (or (<= (prefix-numeric-value arg) 0)
  233. (and (null arg)
  234. (char-table-p standard-display-table)
  235. ;; Test 161, because 160 displays as a space.
  236. (equal (aref standard-display-table
  237. (unibyte-char-to-multibyte 161))
  238. (vector (unibyte-char-to-multibyte 161)))))
  239. (progn
  240. (standard-display-default
  241. (unibyte-char-to-multibyte 160) (unibyte-char-to-multibyte 255))
  242. (unless (or (memq window-system '(x w32 ns)))
  243. (and (terminal-coding-system)
  244. (set-terminal-coding-system nil))))
  245. (display-warning 'i18n
  246. "`standard-display-european' is semi-obsolete; see its doc string for details"
  247. :warning)
  248. ;; Switch to Latin-1 language environment
  249. ;; unless some other has been specified.
  250. (if (equal current-language-environment "English")
  251. (set-language-environment "latin-1"))
  252. (unless (or noninteractive (memq window-system '(x w32 ns)))
  253. ;; Send those codes literally to a character-based terminal.
  254. ;; If we are using single-byte characters,
  255. ;; it doesn't matter which coding system we use.
  256. (set-terminal-coding-system
  257. (let ((c (intern (downcase current-language-environment))))
  258. (if (coding-system-p c) c 'latin-1))))
  259. (standard-display-european-internal)))
  260. (provide 'disp-table)
  261. ;;; disp-table.el ends here