Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / disp-table.el
1 ;;; disp-table.el --- functions for dealing with char tables
2
3 ;; Copyright (C) 1987, 1994, 1995, 1999, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Erik Naggum <erik@naggum.no>
7 ;; Based on a previous version by Howard Gayle
8 ;; Maintainer: FSF
9 ;; Keywords: i18n
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 (save-excursion
91 (set-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))))
99
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
111 ;;;###autoload
112 (defun standard-display-8bit (l h)
113 "Display characters in the range L to H literally."
114 (or standard-display-table
115 (setq standard-display-table (make-display-table)))
116 (while (<= l h)
117 (aset standard-display-table l (if (or (< l ?\s) (>= l 127)) (vector l)))
118 (setq l (1+ l))))
119
120 ;;;###autoload
121 (defun standard-display-default (l h)
122 "Display characters in the range L to H using the default notation."
123 (or standard-display-table
124 (setq standard-display-table (make-display-table)))
125 (while (<= l h)
126 (if (and (>= l ?\s) (characterp l))
127 (aset standard-display-table l nil))
128 (setq l (1+ l))))
129
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.
133 ;;;###autoload
134 (defun standard-display-ascii (c s)
135 "Display character C using printable string S."
136 (or standard-display-table
137 (setq standard-display-table (make-display-table)))
138 (aset standard-display-table c (vconcat s)))
139
140 ;;;###autoload
141 (defun standard-display-g1 (c sc)
142 "Display character C as character SC in the g1 character set.
143 This function assumes that your terminal uses the SO/SI characters;
144 it is meaningless for an X frame."
145 (if (memq window-system '(x w32 mac))
146 (error "Cannot use string glyphs in a windowing system"))
147 (or standard-display-table
148 (setq standard-display-table (make-display-table)))
149 (aset standard-display-table c
150 (vector (create-glyph (concat "\016" (char-to-string sc) "\017")))))
151
152 ;;;###autoload
153 (defun standard-display-graphic (c gc)
154 "Display character C as character GC in graphics character set.
155 This function assumes VT100-compatible escapes; it is meaningless for an
156 X frame."
157 (if (memq window-system '(x w32 mac))
158 (error "Cannot use string glyphs in a windowing system"))
159 (or standard-display-table
160 (setq standard-display-table (make-display-table)))
161 (aset standard-display-table c
162 (vector (create-glyph (concat "\e(0" (char-to-string gc) "\e(B")))))
163
164 ;;;###autoload
165 (defun standard-display-underline (c uc)
166 "Display character C as character UC plus underlining."
167 (or standard-display-table
168 (setq standard-display-table (make-display-table)))
169 (aset standard-display-table c
170 (vector
171 (if window-system
172 (make-glyph-code uc 'underline)
173 (create-glyph (concat "\e[4m" (char-to-string uc) "\e[m"))))))
174
175 ;;;###autoload
176 (defun create-glyph (string)
177 "Allocate a glyph code to display by sending STRING to the terminal."
178 (if (= (length glyph-table) 65536)
179 (error "No free glyph codes remain"))
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))))
183 (setq glyph-table (vconcat glyph-table (list string)))
184 (1- (length glyph-table)))
185
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
190 ;; face id greater that 512 are silently ignored.
191 (if (not face)
192 char
193 (let ((fid (face-id face)))
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)))))
197
198 ;;;###autoload
199 (defun glyph-char (glyph)
200 "Return the character of glyph code GLYPH."
201 (if (consp glyph)
202 (car glyph)
203 (logand glyph #x3fffff)))
204
205 ;;;###autoload
206 (defun glyph-face (glyph)
207 "Return the face of glyph code GLYPH, or nil if glyph has default face."
208 (let ((face-id (if (consp glyph) (cdr glyph) (lsh glyph -22))))
209 (and (> face-id 0)
210 (catch 'face
211 (dolist (face (face-list))
212 (when (eq (face-id face) face-id)
213 (throw 'face face)))))))
214
215 ;;;###autoload
216 (defun standard-display-european (arg)
217 "Semi-obsolete way to toggle display of ISO 8859 European characters.
218
219 This function is semi-obsolete; if you want to do your editing with
220 unibyte characters, it is better to `set-language-environment' coupled
221 with either the `--unibyte' option or the EMACS_UNIBYTE environment
222 variable, or else customize `enable-multibyte-characters'.
223
224 With prefix argument, this command enables European character display
225 if ARG is positive, disables it otherwise. Otherwise, it toggles
226 European character display.
227
228 When this mode is enabled, characters in the range of 160 to 255
229 display not as octal escapes, but as accented characters. Codes 146
230 and 160 display as apostrophe and space, even though they are not the
231 ASCII codes for apostrophe and space.
232
233 Enabling European character display with this command noninteractively
234 from Lisp code also selects Latin-1 as the language environment, and
235 selects unibyte mode for all Emacs buffers \(both existing buffers and
236 those created subsequently). This provides increased compatibility
237 for users who call this function in `.emacs'."
238
239 (if (or (<= (prefix-numeric-value arg) 0)
240 (and (null arg)
241 (char-table-p standard-display-table)
242 ;; Test 161, because 160 displays as a space.
243 (equal (aref standard-display-table 161) [161])))
244 (progn
245 (standard-display-default 160 255)
246 (unless (or (memq window-system '(x w32 mac)))
247 (and (terminal-coding-system)
248 (set-terminal-coding-system nil))))
249
250 (display-warning 'i18n
251 "`standard-display-european' is semi-obsolete; see its doc string for details"
252 :warning)
253
254 ;; Switch to Latin-1 language environment
255 ;; unless some other has been specified.
256 (if (equal current-language-environment "English")
257 (set-language-environment "latin-1"))
258 (unless (or noninteractive (memq window-system '(x w32 mac)))
259 ;; Send those codes literally to a character-based terminal.
260 ;; If we are using single-byte characters,
261 ;; it doesn't matter which coding system we use.
262 (set-terminal-coding-system
263 (let ((c (intern (downcase current-language-environment))))
264 (if (coding-system-p c) c 'latin-1))))
265 (standard-display-european-internal)))
266
267 (provide 'disp-table)
268
269 ;; arch-tag: ffe4c28c-960c-47aa-b8a8-ae89d371ffc7
270 ;;; disp-table.el ends here