(struct sound_device): Function pointer field "write"
[bpt/emacs.git] / lisp / international / mule-util.el
CommitLineData
60370d40 1;;; mule-util.el --- utility functions for mulitilingual environment (mule)
4ed46869 2
4ed46869 3;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
fa526c4a 4;; Licensed to the Free Software Foundation.
4ed46869
KH
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
369314dc
KH
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.
4ed46869 24
60370d40
PJ
25;;; Commentary:
26
4ed46869
KH
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.
88d9cc1e 35TYPE should be `list' or `vector'."
f6afe80c
DL
36;;; (let ((len (length string))
37;;; (i 0)
38;;; val)
dbea6766 39 (cond ((eq type 'list)
f6afe80c
DL
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))))))
dbea6766 48 ((eq type 'vector)
f6afe80c
DL
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))))
dbea6766
KH
55 (t
56 (error "Invalid type: %s" type)))
f6afe80c
DL
57;;; val)
58)
ad0be9a1
JB
59
60;;;###autoload
48e3f3d7 61(make-obsolete 'string-to-sequence
5c92a2a6
JB
62 "use `string-to-list' or `string-to-vector'."
63 "21.4")
4ed46869
KH
64
65;;;###autoload
66(defsubst string-to-list (string)
67 "Return a list of characters in STRING."
48e3f3d7 68 (append string nil))
4ed46869
KH
69
70;;;###autoload
71(defsubst string-to-vector (string)
72 "Return a vector of characters in STRING."
48e3f3d7 73 (vconcat string))
4ed46869
KH
74
75;;;###autoload
76(defun store-substring (string idx obj)
77 "Embed OBJ (string or character) at index IDX of STRING."
dbea6766
KH
78 (if (integerp obj)
79 (aset string idx obj)
80 (let ((len1 (length obj))
81 (len2 (length string))
82 (i 0))
83 (while (< i len1)
84 (aset string (+ idx i) (aref obj i))
85 (setq i (1+ i)))))
86 string)
4ed46869
KH
87
88;;;###autoload
171a7d5d
CW
89(defun truncate-string-to-width (str end-column
90 &optional start-column padding ellipsis)
be9778f8 91 "Truncate string STR to end at column END-COLUMN.
171a7d5d
CW
92The optional 3rd arg START-COLUMN, if non-nil, specifies the starting
93column; that means to return the characters occupying columns
94START-COLUMN ... END-COLUMN of STR. Both END-COLUMN and START-COLUMN
95are specified in terms of character display width in the current
96buffer; see also `char-width'.
97
98The optional 4th arg PADDING, if non-nil, specifies a padding
99character (which should have a display width of 1) to add at the end
100of the result if STR doesn't reach column END-COLUMN, or if END-COLUMN
101comes in the middle of a character in STR. PADDING is also added at
102the beginning of the result if column START-COLUMN appears in the
103middle of a character in STR.
be9778f8
RS
104
105If PADDING is nil, no padding is added in these cases, so
171a7d5d
CW
106the resulting string may be narrower than END-COLUMN.
107
108If ELLIPSIS is non-nil, it should be a string which will replace the
109end of STR (including any padding) if it extends beyond END-COLUMN,
110unless the display width of STR is equal to or less than the display
111width of ELLIPSIS. If it is non-nil and not a string, then ELLIPSIS
112defaults to \"...\"."
4ed46869
KH
113 (or start-column
114 (setq start-column 0))
171a7d5d
CW
115 (when (and ellipsis (not (stringp ellipsis)))
116 (setq ellipsis "..."))
117 (let ((str-len (length str))
118 (str-width (string-width str))
119 (ellipsis-len (if ellipsis (length ellipsis) 0))
120 (ellipsis-width (if ellipsis (string-width ellipsis) 0))
4ed46869
KH
121 (idx 0)
122 (column 0)
123 (head-padding "") (tail-padding "")
124 ch last-column last-idx from-idx)
125 (condition-case nil
126 (while (< column start-column)
dbea6766 127 (setq ch (aref str idx)
4ed46869 128 column (+ column (char-width ch))
dbea6766 129 idx (1+ idx)))
171a7d5d 130 (args-out-of-range (setq idx str-len)))
4ed46869 131 (if (< column start-column)
be9778f8 132 (if padding (make-string end-column padding) "")
171a7d5d
CW
133 (when (and padding (> column start-column))
134 (setq head-padding (make-string (- column start-column) padding)))
4ed46869 135 (setq from-idx idx)
171a7d5d
CW
136 (when (>= end-column column)
137 (if (and (< end-column str-width)
138 (> str-width ellipsis-width))
139 (setq end-column (- end-column ellipsis-width))
140 (setq ellipsis ""))
be9778f8
RS
141 (condition-case nil
142 (while (< column end-column)
143 (setq last-column column
144 last-idx idx
dbea6766 145 ch (aref str idx)
be9778f8 146 column (+ column (char-width ch))
dbea6766 147 idx (1+ idx)))
171a7d5d
CW
148 (args-out-of-range (setq idx str-len)))
149 (when (> column end-column)
150 (setq column last-column
151 idx last-idx))
152 (when (and padding (< column end-column))
153 (setq tail-padding (make-string (- end-column column) padding))))
154 (concat head-padding (substring str from-idx idx)
155 tail-padding ellipsis))))
156
157;;; Test suite for truncate-string-to-width
158;; (dolist (test '((("" 0) . "")
159;; (("x" 1) . "x")
160;; (("xy" 1) . "x")
161;; (("xy" 2 1) . "y")
162;; (("xy" 0) . "")
163;; (("xy" 3) . "xy")
164;; (("\e$AVP\e(B" 0) . "")
165;; (("\e$AVP\e(B" 1) . "")
166;; (("\e$AVP\e(B" 2) . "\e$AVP\e(B")
167;; (("\e$AVP\e(B" 1 nil ? ) . " ")
168;; (("\e$AVPND\e(B" 3 1 ? ) . " ")
169;; (("x\e$AVP\e(Bx" 2) . "x")
170;; (("x\e$AVP\e(Bx" 3) . "x\e$AVP\e(B")
171;; (("x\e$AVP\e(Bx" 3) . "x\e$AVP\e(B")
172;; (("x\e$AVP\e(Bx" 4 1) . "\e$AVP\e(Bx")
173;; (("kor\e$(CGQ\e(Be\e$(C1[\e(Ban" 8 1 ? ) . "or\e$(CGQ\e(Be\e$(C1[\e(B")
174;; (("kor\e$(CGQ\e(Be\e$(C1[\e(Ban" 7 2 ? ) . "r\e$(CGQ\e(Be ")
175;; (("" 0 nil nil "...") . "")
176;; (("x" 3 nil nil "...") . "x")
177;; (("\e$AVP\e(B" 3 nil nil "...") . "\e$AVP\e(B")
178;; (("foo" 3 nil nil "...") . "foo")
179;; (("foo" 2 nil nil "...") . "fo") ;; XEmacs failure?
180;; (("foobar" 6 0 nil "...") . "foobar")
181;; (("foobarbaz" 6 nil nil "...") . "foo...")
182;; (("foobarbaz" 7 2 nil "...") . "ob...")
183;; (("foobarbaz" 9 3 nil "...") . "barbaz")
184;; (("\e$B$3\e(Bh\e$B$s\e(Be\e$B$K\e(Bl\e$B$A\e(Bl\e$B$O\e(Bo" 15 1 ? t) . " h\e$B$s\e(Be\e$B$K\e(Bl\e$B$A\e(Bl\e$B$O\e(Bo")
185;; (("\e$B$3\e(Bh\e$B$s\e(Be\e$B$K\e(Bl\e$B$A\e(Bl\e$B$O\e(Bo" 14 1 ? t) . " h\e$B$s\e(Be\e$B$K\e(Bl\e$B$A\e(B...")
186;; (("x" 3 nil nil "\e$(0GnM$\e(B") . "x")
187;; (("\e$AVP\e(B" 2 nil nil "\e$(0GnM$\e(B") . "\e$AVP\e(B")
188;; (("\e$AVP\e(B" 1 nil ?x "\e$(0GnM$\e(B") . "x") ;; XEmacs error
189;; (("\e$AVPND\e(B" 3 nil ? "\e$(0GnM$\e(B") . "\e$AVP\e(B ") ;; XEmacs error
190;; (("foobarbaz" 4 nil nil "\e$(0GnM$\e(B") . "\e$(0GnM$\e(B")
191;; (("foobarbaz" 5 nil nil "\e$(0GnM$\e(B") . "f\e$(0GnM$\e(B")
192;; (("foobarbaz" 6 nil nil "\e$(0GnM$\e(B") . "fo\e$(0GnM$\e(B")
193;; (("foobarbaz" 8 3 nil "\e$(0GnM$\e(B") . "b\e$(0GnM$\e(B")
194;; (("\e$B$3\e(Bh\e$B$s\e(Be\e$B$K\e(Bl\e$B$A\e(Bl\e$B$O\e(Bo" 14 4 ?x "\e$BF|K\8l\e(B") . "xe\e$B$KF|K\8l\e(B")
195;; (("\e$B$3\e(Bh\e$B$s\e(Be\e$B$K\e(Bl\e$B$A\e(Bl\e$B$O\e(Bo" 13 4 ?x "\e$BF|K\8l\e(B") . "xex\e$BF|K\8l\e(B")
196;; ))
197;; (let (ret)
198;; (condition-case e
199;; (setq ret (apply #'truncate-string-to-width (car test)))
200;; (error (setq ret e)))
201;; (unless (equal ret (cdr test))
202;; (error "%s: expected %s, got %s"
203;; (prin1-to-string (cons 'truncate-string-to-width (car test)))
204;; (prin1-to-string (cdr test))
205;; (if (consp ret)
206;; (format "error: %s: %s" (car ret)
207;; (prin1-to-string (cdr ret)))
208;; (prin1-to-string ret))))))
4ed46869 209
e8dd0160 210;;; For backward compatibility ...
4ed46869
KH
211;;;###autoload
212(defalias 'truncate-string 'truncate-string-to-width)
ad0be9a1
JB
213
214;;;###autoload
2598a293 215(make-obsolete 'truncate-string 'truncate-string-to-width "20.1")
4ed46869
KH
216\f
217;;; Nested alist handler. Nested alist is alist whose elements are
218;;; also nested alist.
219
220;;;###autoload
221(defsubst nested-alist-p (obj)
e8dd0160 222 "Return t if OBJ is a nested alist.
4ed46869
KH
223
224Nested alist is a list of the form (ENTRY . BRANCHES), where ENTRY is
225any Lisp object, and BRANCHES is a list of cons cells of the form
4a30e92e 226\(KEY-ELEMENT . NESTED-ALIST).
4ed46869
KH
227
228You can use a nested alist to store any Lisp object (ENTRY) for a key
229sequence KEYSEQ, where KEYSEQ is a sequence of KEY-ELEMENT. KEYSEQ
230can be a string, a vector, or a list."
231 (and obj (listp obj) (listp (cdr obj))))
232
233;;;###autoload
234(defun set-nested-alist (keyseq entry alist &optional len branches)
235 "Set ENTRY for KEYSEQ in a nested alist ALIST.
e8dd0160 236Optional 4th arg LEN non-nil means the first LEN elements in KEYSEQ
4ed46869
KH
237 is considered.
238Optional argument BRANCHES if non-nil is branches for a keyseq
239longer than KEYSEQ.
240See the documentation of `nested-alist-p' for more detail."
241 (or (nested-alist-p alist)
e8dd0160 242 (error "Invalid argument %s" alist))
4ed46869
KH
243 (let ((islist (listp keyseq))
244 (len (or len (length keyseq)))
245 (i 0)
246 key-elt slot)
247 (while (< i len)
248 (if (null (nested-alist-p alist))
249 (error "Keyseq %s is too long for this nested alist" keyseq))
250 (setq key-elt (if islist (nth i keyseq) (aref keyseq i)))
251 (setq slot (assoc key-elt (cdr alist)))
252 (if (null slot)
253 (progn
254 (setq slot (cons key-elt (list t)))
255 (setcdr alist (cons slot (cdr alist)))))
256 (setq alist (cdr slot))
257 (setq i (1+ i)))
258 (setcar alist entry)
259 (if branches
72594565 260 (setcdr (last alist) branches))))
4ed46869
KH
261
262;;;###autoload
263(defun lookup-nested-alist (keyseq alist &optional len start nil-for-too-long)
264 "Look up key sequence KEYSEQ in nested alist ALIST. Return the definition.
265Optional 1st argument LEN specifies the length of KEYSEQ.
266Optional 2nd argument START specifies index of the starting key.
267The returned value is normally a nested alist of which
268car part is the entry for KEYSEQ.
269If ALIST is not deep enough for KEYSEQ, return number which is
270 how many key elements at the front of KEYSEQ it takes
271 to reach a leaf in ALIST.
272Optional 3rd argument NIL-FOR-TOO-LONG non-nil means return nil
273 even if ALIST is not deep enough."
274 (or (nested-alist-p alist)
f6afe80c 275 (error "Invalid argument %s" alist))
4ed46869
KH
276 (or len
277 (setq len (length keyseq)))
278 (let ((i (or start 0)))
279 (if (catch 'lookup-nested-alist-tag
280 (if (listp keyseq)
281 (while (< i len)
282 (if (setq alist (cdr (assoc (nth i keyseq) (cdr alist))))
283 (setq i (1+ i))
284 (throw 'lookup-nested-alist-tag t))))
285 (while (< i len)
286 (if (setq alist (cdr (assoc (aref keyseq i) (cdr alist))))
287 (setq i (1+ i))
288 (throw 'lookup-nested-alist-tag t))))
289 ;; KEYSEQ is too long.
290 (if nil-for-too-long nil i)
291 alist)))
292
be1d31dc 293\f
4ed46869
KH
294;; Coding system related functions.
295
be1d31dc
KH
296;;;###autoload
297(defun coding-system-eol-type-mnemonic (coding-system)
935f6bf5
KH
298 "Return the string indicating end-of-line format of CODING-SYSTEM."
299 (let* ((eol-type (coding-system-eol-type coding-system))
300 (val (cond ((vectorp eol-type) eol-mnemonic-undecided)
301 ((eq eol-type 0) eol-mnemonic-unix)
302 ((eq eol-type 1) eol-mnemonic-dos)
303 ((eq eol-type 2) eol-mnemonic-mac)
304 (t "-"))))
305 (if (stringp val)
306 val
307 (char-to-string val))))
be1d31dc
KH
308
309;;;###autoload
310(defun coding-system-post-read-conversion (coding-system)
bc9254e2 311 "Return the value of CODING-SYSTEM's `post-read-conversion' property."
f1fd88c6 312 (coding-system-get coding-system 'post-read-conversion))
be1d31dc
KH
313
314;;;###autoload
315(defun coding-system-pre-write-conversion (coding-system)
bc9254e2 316 "Return the value of CODING-SYSTEM's `pre-write-conversion' property."
f1fd88c6 317 (coding-system-get coding-system 'pre-write-conversion))
be1d31dc
KH
318
319;;;###autoload
d2a1ee18 320(defun coding-system-translation-table-for-decode (coding-system)
bc9254e2 321 "Return the value of CODING-SYSTEM's `translation-table-for-decode' property."
f967223b 322 (coding-system-get coding-system 'translation-table-for-decode))
34761746
KH
323
324;;;###autoload
d2a1ee18 325(defun coding-system-translation-table-for-encode (coding-system)
bc9254e2 326 "Return the value of CODING-SYSTEM's `translation-table-for-encode' property."
f967223b 327 (coding-system-get coding-system 'translation-table-for-encode))
be1d31dc 328
88d9cc1e
KH
329;;;###autoload
330(defun coding-system-equal (coding-system-1 coding-system-2)
be1d31dc 331 "Return t if and only if CODING-SYSTEM-1 and CODING-SYSTEM-2 are identical.
88d9cc1e
KH
332Two coding systems are identical if two symbols are equal
333or one is an alias of the other."
be1d31dc 334 (or (eq coding-system-1 coding-system-2)
f1fd88c6
KH
335 (and (equal (coding-system-spec coding-system-1)
336 (coding-system-spec coding-system-2))
337 (let ((eol-type-1 (coding-system-eol-type coding-system-1))
338 (eol-type-2 (coding-system-eol-type coding-system-2)))
339 (or (eq eol-type-1 eol-type-2)
340 (and (vectorp eol-type-1) (vectorp eol-type-2)))))))
88d9cc1e 341
e481690d 342;;;###autoload
dbea6766
KH
343(defmacro detect-coding-with-priority (from to priority-list)
344 "Detect a coding system of the text between FROM and TO with PRIORITY-LIST.
345PRIORITY-LIST is an alist of coding categories vs the corresponding
346coding systems ordered by priority."
e74d7926
KH
347 `(unwind-protect
348 (let* ((prio-list ,priority-list)
349 (coding-category-list coding-category-list)
350 ,@(mapcar (function (lambda (x) (list x x)))
351 coding-category-list))
cb7216a7
DL
352 (mapc (function (lambda (x) (set (car x) (cdr x))))
353 prio-list)
354 (set-coding-priority (mapcar #'car prio-list))
e74d7926
KH
355 (detect-coding-region ,from ,to))
356 ;; We must restore the internal database.
357 (set-coding-priority coding-category-list)
358 (update-coding-systems-internal)))
dbea6766
KH
359
360;;;###autoload
361(defun detect-coding-with-language-environment (from to lang-env)
362 "Detect a coding system of the text between FROM and TO with LANG-ENV.
e8dd0160 363The detection takes into account the coding system priorities for the
dbea6766
KH
364language environment LANG-ENV."
365 (let ((coding-priority (get-language-info lang-env 'coding-priority)))
366 (if coding-priority
367 (detect-coding-with-priority
368 from to
369 (mapcar (function (lambda (x)
370 (cons (coding-system-get x 'coding-category) x)))
371 coding-priority))
372 (detect-coding-region from to))))
e481690d 373
4ed46869 374\f
f6afe80c 375(provide 'mule-util)
72594565 376
171a7d5d
CW
377;; Local Variables:
378;; coding: iso-2022-7bit
379;; End:
380
f6afe80c 381;;; mule-util.el ends here