summaryrefslogtreecommitdiff
path: root/lisp/disp-table.el
blob: ac813f02946483facb809bdd08283c17875f409d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
;;; disp-table.el --- functions for dealing with char tables.

;; Copyright (C) 1987, 1994, 1995 Free Software Foundation, Inc.

;; Author: Erik Naggum <erik@naggum.no>
;; Based on a previous version by Howard Gayle
;; Maintainer: FSF
;; Keywords: i18n

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

;;; Code:

(defconst display-table-extras 6
  "The number of extra slots in a display table.")

;;;###autoload
(defun make-display-table ()
  "Return a new, empty display table."
  (make-char-table display-table-extras nil))

(or standard-display-table
    (setq standard-display-table (make-display-table)))

(defconst display-table-slot-name-alist
  '((truncation		0 display-table-char-p)
    (wrap		1 display-table-char-p)
    (escape		2 display-table-char-p)
    (control		3 display-table-char-p)
    (selective-display	4 display-table-vector-p)
    (vertical-border	5 display-table-char-p))
  "Association list of display-table slot names.
Each element contains the slot name, slot number, and a predicate
function to test the validity of values for the setter function.")

(defun display-table-char-p (c)
  "Test whether c is a valid character for display-tables."
  (and (integerp c) (<= 0 c) (<= c 256)))

(defun display-table-vector-p (cv)
  "Test whether CV is a valid character vector for display-tables."
  (and (vectorp cv)
       ;; (every 'display-table-char-p cv)
       (let ((i (1- (length cv))))
	 (while (and (<= 0 i) (display-table-char-p (aref cv i)))
	   (setq i (1- i)))
	 (> 0 i))))

;;;###autoload
(defun display-table-slot (display-table slot)
  "Return the value of the extra slot in DISPLAY-TABLE named SLOT.
SLOT may be a number from 0 to 5 inclusive, or a name (symbol).
See `display-table-slot-name-alist' for the names and numbers."
  (let ((slot-number
	 (if (numberp slot) slot
	   (or (car (cdr (assoc slot display-table-slot-name-alist)))
	       (error "Invalid display-table slot name: %s" slot)))))
    (char-table-extra-slot display-table slot-number)))

;;;###autoload
(defun set-display-table-slot (display-table slot value)
  "Set the value of the extra slot in DISPLAY-TABLE named SLOT to VALUE.
SLOT may be a number from 0 to 5 inclusive, or a name (symbol).
See `display-table-slot-name-alist' for the names and numbers."
  (let* ((slot-entry
	  (or (if (numberp slot)
		  (cdr (nth slot display-table-slot-name-alist))
		(cdr (assoc slot display-table-slot-name-alist)))
	      (error "Invalid display-table slot: %s" slot)))
	 (slot-number (car slot-entry))
	 (slot-predicate (car (cdr slot-entry))))
    (if (funcall slot-predicate value)
	(set-char-table-extra-slot display-table slot-number value)
      (signal 'wrong-type-argument (list slot-predicate value)))))

;;;###autoload
(defun describe-display-table (dt)
  "Describe the display table DT in a help buffer."
  (with-output-to-temp-buffer "*Help*"
    (princ "\nTruncation glyph: ")
    (prin1 (char-table-extra-slot dt 0)) ;direct access is faster
    (princ "\nWrap glyph: ")
    (prin1 (char-table-extra-slot dt 1))
    (princ "\nEscape glyph: ")
    (prin1 (char-table-extra-slot dt 2))
    (princ "\nCtrl glyph: ")
    (prin1 (char-table-extra-slot dt 3))
    (princ "\nSelective display glyph sequence: ")
    (prin1 (char-table-extra-slot dt 4))
    (princ "\nVertical window border glyph: ")
    (prin1 (char-table-extra-slot dt 5))
    (princ "\nCharacter display glyph sequences:\n")
    (save-excursion
      (set-buffer standard-output)
      (let ((vector (make-vector 256 nil))
	    (i 0))
	(while (< i 256)
	  (aset vector i (aref dt i))
	  (setq i (1+ i)))
	(describe-vector vector))
      (help-mode))
    (print-help-return-message)))

;;;###autoload
(defun describe-current-display-table ()
  "Describe the display table in use in the selected window and buffer."
  (interactive)
  (let ((disptab (or (window-display-table (selected-window))
		     buffer-display-table
		     standard-display-table)))
    (if disptab
	(describe-display-table disptab)
      (message "No display table"))))

;;;###autoload
(defun standard-display-8bit (l h)
  "Display characters in the range L to H literally."
  (while (<= l h)
    (if (and (>= l ?\ ) (< l 127))
	(aset standard-display-table l nil)
      (aset standard-display-table l (vector l)))
    (setq l (1+ l))))

;;;###autoload
(defun standard-display-default (l h)
  "Display characters in the range L to H using the default notation."
  (while (<= l h)
    (if (and (>= l ?\ ) (< l 127))
	(aset standard-display-table l nil)
      (aset standard-display-table l nil))
    (setq l (1+ l))))

;; This function does NOT take terminal-dependent escape sequences.
;; For that, you need to go through create-glyph.  Use one of the
;; other functions below, or roll your own.
;;;###autoload
(defun standard-display-ascii (c s)
  "Display character C using printable string S."
  (aset standard-display-table c (vconcat s)))

;;;###autoload
(defun standard-display-g1 (c sc)
  "Display character C as character SC in the g1 character set.
This function assumes that your terminal uses the SO/SI characters;
it is meaningless for an X frame."
  (if window-system
      (error "Cannot use string glyphs in a windowing system"))
  (aset standard-display-table c
	(vector (create-glyph (concat "\016" (char-to-string sc) "\017")))))

;;;###autoload
(defun standard-display-graphic (c gc)
  "Display character C as character GC in graphics character set.
This function assumes VT100-compatible escapes; it is meaningless for an
X frame."
  (if window-system
      (error "Cannot use string glyphs in a windowing system"))
  (aset standard-display-table c
	(vector (create-glyph (concat "\e(0" (char-to-string gc) "\e(B")))))

;;;###autoload
(defun standard-display-underline (c uc)
  "Display character C as character UC plus underlining."
  (if window-system (require 'faces))
  (aset standard-display-table c
	(vector 
	 (if window-system
	     (logior uc (lsh (face-id (internal-find-face 'underline)) 8))
	   (create-glyph (concat "\e[4m" (char-to-string uc) "\e[m"))))))

;; Allocate a glyph code to display by sending STRING to the terminal.
;;;###autoload
(defun create-glyph (string)
  (if (= (length glyph-table) 65536)
      (error "No free glyph codes remain"))
  ;; Don't use slots that correspond to ASCII characters.
  (if (= (length glyph-table) 32)
      (setq glyph-table (vconcat glyph-table (make-vector 224 nil))))
  (setq glyph-table (vconcat glyph-table (list string)))
  (1- (length glyph-table)))

;;;###autoload
(defun standard-display-european (arg)
  "Toggle display of European characters encoded with ISO 8859.
When enabled, characters in the range of 160 to 255 display not
as octal escapes, but as accented characters.
With prefix argument, enable European character display iff arg is positive."
  (interactive "P")
  (if (or (<= (prefix-numeric-value arg) 0)
	  (and (null arg)
	       (char-table-p standard-display-table)
	       (equal (aref standard-display-table 160) [160])))
      (standard-display-default 160 255)
    (standard-display-8bit 160 255)))

(provide 'disp-table)

;;; disp-table.el ends here