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