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