]> code.delx.au - gnu-emacs/blob - lisp/international/mule-util.el
(standard-fontset-spec): Purecopy it.
[gnu-emacs] / lisp / international / mule-util.el
1 ;;; mule-util.el --- Utility functions for mulitilingual environment (mule)
2
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5
6 ;; Keywords: mule, multilingual
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 ;;; String manipulations while paying attention to multibyte
28 ;;; characters.
29
30 ;;;###autoload
31 (defun string-to-sequence (string type)
32 "Convert STRING to a sequence of TYPE which contains characters in STRING.
33 TYPE should be `list' or `vector'."
34 (let ((len (length string))
35 (i 0)
36 val)
37 (cond ((eq type 'list)
38 (setq val (make-list len 0))
39 (let ((l val))
40 (while (< i len)
41 (setcar l (aref string i))
42 (setq l (cdr l) i (1+ i)))))
43 ((eq type 'vector)
44 (setq val (make-vector len 0))
45 (while (< i len)
46 (aset val i (aref string i))
47 (setq i (1+ i))))
48 (t
49 (error "Invalid type: %s" type)))
50 val))
51
52 ;;;###autoload
53 (defsubst string-to-list (string)
54 "Return a list of characters in STRING."
55 (string-to-sequence string 'list))
56
57 ;;;###autoload
58 (defsubst string-to-vector (string)
59 "Return a vector of characters in STRING."
60 (string-to-sequence string 'vector))
61
62 ;;;###autoload
63 (defun store-substring (string idx obj)
64 "Embed OBJ (string or character) at index IDX of STRING."
65 (if (integerp obj)
66 (aset string idx obj)
67 (let ((len1 (length obj))
68 (len2 (length string))
69 (i 0))
70 (while (< i len1)
71 (aset string (+ idx i) (aref obj i))
72 (setq i (1+ i)))))
73 string)
74
75 ;;;###autoload
76 (defun truncate-string-to-width (str end-column &optional start-column padding)
77 "Truncate string STR to end at column END-COLUMN.
78 The optional 3rd arg START-COLUMN, if non-nil, specifies
79 the starting column; that means to return the characters occupying
80 columns START-COLUMN ... END-COLUMN of STR.
81
82 The optional 4th arg PADDING, if non-nil, specifies a padding character
83 to add at the end of the result if STR doesn't reach column END-COLUMN,
84 or if END-COLUMN comes in the middle of a character in STR.
85 PADDING is also added at the beginning of the result
86 if column START-COLUMN appears in the middle of a character in STR.
87
88 If PADDING is nil, no padding is added in these cases, so
89 the resulting string may be narrower than END-COLUMN."
90 (or start-column
91 (setq start-column 0))
92 (let ((len (length str))
93 (idx 0)
94 (column 0)
95 (head-padding "") (tail-padding "")
96 ch last-column last-idx from-idx)
97 (condition-case nil
98 (while (< column start-column)
99 (setq ch (aref str idx)
100 column (+ column (char-width ch))
101 idx (1+ idx)))
102 (args-out-of-range (setq idx len)))
103 (if (< column start-column)
104 (if padding (make-string end-column padding) "")
105 (if (and padding (> column start-column))
106 (setq head-padding (make-string (- column start-column) padding)))
107 (setq from-idx idx)
108 (if (< end-column column)
109 (setq idx from-idx)
110 (condition-case nil
111 (while (< column end-column)
112 (setq last-column column
113 last-idx idx
114 ch (aref str idx)
115 column (+ column (char-width ch))
116 idx (1+ idx)))
117 (args-out-of-range (setq idx len)))
118 (if (> column end-column)
119 (setq column last-column idx last-idx))
120 (if (and padding (< column end-column))
121 (setq tail-padding (make-string (- end-column column) padding))))
122 (setq str (substring str from-idx idx))
123 (if padding
124 (concat head-padding str tail-padding)
125 str))))
126
127 ;;; For backward compatibility ...
128 ;;;###autoload
129 (defalias 'truncate-string 'truncate-string-to-width)
130 (make-obsolete 'truncate-string 'truncate-string-to-width)
131 \f
132 ;;; Nested alist handler. Nested alist is alist whose elements are
133 ;;; also nested alist.
134
135 ;;;###autoload
136 (defsubst nested-alist-p (obj)
137 "Return t if OBJ is a nested alist.
138
139 Nested alist is a list of the form (ENTRY . BRANCHES), where ENTRY is
140 any Lisp object, and BRANCHES is a list of cons cells of the form
141 (KEY-ELEMENT . NESTED-ALIST).
142
143 You can use a nested alist to store any Lisp object (ENTRY) for a key
144 sequence KEYSEQ, where KEYSEQ is a sequence of KEY-ELEMENT. KEYSEQ
145 can be a string, a vector, or a list."
146 (and obj (listp obj) (listp (cdr obj))))
147
148 ;;;###autoload
149 (defun set-nested-alist (keyseq entry alist &optional len branches)
150 "Set ENTRY for KEYSEQ in a nested alist ALIST.
151 Optional 4th arg LEN non-nil means the first LEN elements in KEYSEQ
152 is considered.
153 Optional argument BRANCHES if non-nil is branches for a keyseq
154 longer than KEYSEQ.
155 See the documentation of `nested-alist-p' for more detail."
156 (or (nested-alist-p alist)
157 (error "Invalid argument %s" alist))
158 (let ((islist (listp keyseq))
159 (len (or len (length keyseq)))
160 (i 0)
161 key-elt slot)
162 (while (< i len)
163 (if (null (nested-alist-p alist))
164 (error "Keyseq %s is too long for this nested alist" keyseq))
165 (setq key-elt (if islist (nth i keyseq) (aref keyseq i)))
166 (setq slot (assoc key-elt (cdr alist)))
167 (if (null slot)
168 (progn
169 (setq slot (cons key-elt (list t)))
170 (setcdr alist (cons slot (cdr alist)))))
171 (setq alist (cdr slot))
172 (setq i (1+ i)))
173 (setcar alist entry)
174 (if branches
175 (setcdr (last alist) branches))))
176
177 ;;;###autoload
178 (defun lookup-nested-alist (keyseq alist &optional len start nil-for-too-long)
179 "Look up key sequence KEYSEQ in nested alist ALIST. Return the definition.
180 Optional 1st argument LEN specifies the length of KEYSEQ.
181 Optional 2nd argument START specifies index of the starting key.
182 The returned value is normally a nested alist of which
183 car part is the entry for KEYSEQ.
184 If ALIST is not deep enough for KEYSEQ, return number which is
185 how many key elements at the front of KEYSEQ it takes
186 to reach a leaf in ALIST.
187 Optional 3rd argument NIL-FOR-TOO-LONG non-nil means return nil
188 even if ALIST is not deep enough."
189 (or (nested-alist-p alist)
190 (error "invalid argument %s" alist))
191 (or len
192 (setq len (length keyseq)))
193 (let ((i (or start 0)))
194 (if (catch 'lookup-nested-alist-tag
195 (if (listp keyseq)
196 (while (< i len)
197 (if (setq alist (cdr (assoc (nth i keyseq) (cdr alist))))
198 (setq i (1+ i))
199 (throw 'lookup-nested-alist-tag t))))
200 (while (< i len)
201 (if (setq alist (cdr (assoc (aref keyseq i) (cdr alist))))
202 (setq i (1+ i))
203 (throw 'lookup-nested-alist-tag t))))
204 ;; KEYSEQ is too long.
205 (if nil-for-too-long nil i)
206 alist)))
207
208 \f
209 ;; Coding system related functions.
210
211 ;;;###autoload
212 (defun coding-system-eol-type-mnemonic (coding-system)
213 "Return the string indicating end-of-line format of CODING-SYSTEM."
214 (let* ((eol-type (coding-system-eol-type coding-system))
215 (val (cond ((vectorp eol-type) eol-mnemonic-undecided)
216 ((eq eol-type 0) eol-mnemonic-unix)
217 ((eq eol-type 1) eol-mnemonic-dos)
218 ((eq eol-type 2) eol-mnemonic-mac)
219 (t "-"))))
220 (if (stringp val)
221 val
222 (char-to-string val))))
223
224 ;;;###autoload
225 (defun coding-system-post-read-conversion (coding-system)
226 "Return the value of CODING-SYSTEM's post-read-conversion property."
227 (coding-system-get coding-system 'post-read-conversion))
228
229 ;;;###autoload
230 (defun coding-system-pre-write-conversion (coding-system)
231 "Return the value of CODING-SYSTEM's pre-write-conversion property."
232 (coding-system-get coding-system 'pre-write-conversion))
233
234 ;;;###autoload
235 (defun coding-system-translation-table-for-decode (coding-system)
236 "Return the value of CODING-SYSTEM's translation-table-for-decode property."
237 (coding-system-get coding-system 'translation-table-for-decode))
238
239 ;;;###autoload
240 (defun coding-system-translation-table-for-encode (coding-system)
241 "Return the value of CODING-SYSTEM's translation-table-for-encode property."
242 (coding-system-get coding-system 'translation-table-for-encode))
243
244 ;;;###autoload
245 (defun coding-system-equal (coding-system-1 coding-system-2)
246 "Return t if and only if CODING-SYSTEM-1 and CODING-SYSTEM-2 are identical.
247 Two coding systems are identical if two symbols are equal
248 or one is an alias of the other."
249 (or (eq coding-system-1 coding-system-2)
250 (and (equal (coding-system-spec coding-system-1)
251 (coding-system-spec coding-system-2))
252 (let ((eol-type-1 (coding-system-eol-type coding-system-1))
253 (eol-type-2 (coding-system-eol-type coding-system-2)))
254 (or (eq eol-type-1 eol-type-2)
255 (and (vectorp eol-type-1) (vectorp eol-type-2)))))))
256
257 ;;;###autoload
258 (defmacro detect-coding-with-priority (from to priority-list)
259 "Detect a coding system of the text between FROM and TO with PRIORITY-LIST.
260 PRIORITY-LIST is an alist of coding categories vs the corresponding
261 coding systems ordered by priority."
262 `(unwind-protect
263 (let* ((prio-list ,priority-list)
264 (coding-category-list coding-category-list)
265 ,@(mapcar (function (lambda (x) (list x x)))
266 coding-category-list))
267 (mapcar (function (lambda (x) (set (car x) (cdr x))))
268 prio-list)
269 (set-coding-priority (mapcar (function (lambda (x) (car x)))
270 prio-list))
271 (detect-coding-region ,from ,to))
272 ;; We must restore the internal database.
273 (set-coding-priority coding-category-list)
274 (update-coding-systems-internal)))
275
276 ;;;###autoload
277 (defun detect-coding-with-language-environment (from to lang-env)
278 "Detect a coding system of the text between FROM and TO with LANG-ENV.
279 The detection takes into account the coding system priorities for the
280 language environment LANG-ENV."
281 (let ((coding-priority (get-language-info lang-env 'coding-priority)))
282 (if coding-priority
283 (detect-coding-with-priority
284 from to
285 (mapcar (function (lambda (x)
286 (cons (coding-system-get x 'coding-category) x)))
287 coding-priority))
288 (detect-coding-region from to))))
289
290 \f
291 ;;; mule-util.el ends here
292