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