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