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