(ccl-decode-mule-utf-8): Check utf-subst-table-for-decode for more
[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.
8ce0e9a8 5;; Copyright (C) 2000, 2002 Free Software Foundation, Inc.
4ed46869
KH
6
7;; Keywords: mule, multilingual
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
369314dc
KH
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
4ed46869 25
60370d40
PJ
26;;; Commentary:
27
4ed46869
KH
28;;; Code:
29
30;;; String manipulations while paying attention to multibyte
31;;; characters.
32
33;;;###autoload
34(defun string-to-sequence (string type)
35 "Convert STRING to a sequence of TYPE which contains characters in STRING.
88d9cc1e 36TYPE should be `list' or `vector'."
f6afe80c
DL
37;;; (let ((len (length string))
38;;; (i 0)
39;;; val)
dbea6766 40 (cond ((eq type 'list)
f6afe80c
DL
41 ;; Applicable post-Emacs 20.2 and asymptotically ~10 times
42 ;; faster than the code below:
43 (append string nil))
44;;; (setq val (make-list len 0))
45;;; (let ((l val))
46;;; (while (< i len)
47;;; (setcar l (aref string i))
48;;; (setq l (cdr l) i (1+ i))))))
dbea6766 49 ((eq type 'vector)
f6afe80c
DL
50 ;; As above.
51 (vconcat string))
52;;; (setq val (make-vector len 0))
53;;; (while (< i len)
54;;; (aset val i (aref string i))
55;;; (setq i (1+ i))))
dbea6766
KH
56 (t
57 (error "Invalid type: %s" type)))
f6afe80c
DL
58;;; val)
59)
ad0be9a1
JB
60
61;;;###autoload
48e3f3d7 62(make-obsolete 'string-to-sequence
5c92a2a6
JB
63 "use `string-to-list' or `string-to-vector'."
64 "21.4")
4ed46869
KH
65
66;;;###autoload
67(defsubst string-to-list (string)
68 "Return a list of characters in STRING."
48e3f3d7 69 (append string nil))
4ed46869
KH
70
71;;;###autoload
72(defsubst string-to-vector (string)
73 "Return a vector of characters in STRING."
48e3f3d7 74 (vconcat string))
4ed46869
KH
75
76;;;###autoload
77(defun store-substring (string idx obj)
78 "Embed OBJ (string or character) at index IDX of STRING."
dbea6766
KH
79 (if (integerp obj)
80 (aset string idx obj)
81 (let ((len1 (length obj))
82 (len2 (length string))
83 (i 0))
84 (while (< i len1)
85 (aset string (+ idx i) (aref obj i))
86 (setq i (1+ i)))))
87 string)
4ed46869
KH
88
89;;;###autoload
171a7d5d
CW
90(defun truncate-string-to-width (str end-column
91 &optional start-column padding ellipsis)
be9778f8 92 "Truncate string STR to end at column END-COLUMN.
171a7d5d
CW
93The optional 3rd arg START-COLUMN, if non-nil, specifies the starting
94column; that means to return the characters occupying columns
95START-COLUMN ... END-COLUMN of STR. Both END-COLUMN and START-COLUMN
96are specified in terms of character display width in the current
97buffer; see also `char-width'.
98
99The optional 4th arg PADDING, if non-nil, specifies a padding
100character (which should have a display width of 1) to add at the end
101of the result if STR doesn't reach column END-COLUMN, or if END-COLUMN
102comes in the middle of a character in STR. PADDING is also added at
103the beginning of the result if column START-COLUMN appears in the
104middle of a character in STR.
be9778f8
RS
105
106If PADDING is nil, no padding is added in these cases, so
171a7d5d
CW
107the resulting string may be narrower than END-COLUMN.
108
109If ELLIPSIS is non-nil, it should be a string which will replace the
110end of STR (including any padding) if it extends beyond END-COLUMN,
111unless the display width of STR is equal to or less than the display
112width of ELLIPSIS. If it is non-nil and not a string, then ELLIPSIS
113defaults to \"...\"."
4ed46869
KH
114 (or start-column
115 (setq start-column 0))
171a7d5d
CW
116 (when (and ellipsis (not (stringp ellipsis)))
117 (setq ellipsis "..."))
118 (let ((str-len (length str))
119 (str-width (string-width str))
120 (ellipsis-len (if ellipsis (length ellipsis) 0))
121 (ellipsis-width (if ellipsis (string-width ellipsis) 0))
4ed46869
KH
122 (idx 0)
123 (column 0)
124 (head-padding "") (tail-padding "")
125 ch last-column last-idx from-idx)
126 (condition-case nil
127 (while (< column start-column)
dbea6766 128 (setq ch (aref str idx)
4ed46869 129 column (+ column (char-width ch))
dbea6766 130 idx (1+ idx)))
171a7d5d 131 (args-out-of-range (setq idx str-len)))
4ed46869 132 (if (< column start-column)
be9778f8 133 (if padding (make-string end-column padding) "")
171a7d5d
CW
134 (when (and padding (> column start-column))
135 (setq head-padding (make-string (- column start-column) padding)))
4ed46869 136 (setq from-idx idx)
171a7d5d
CW
137 (when (>= end-column column)
138 (if (and (< end-column str-width)
139 (> str-width ellipsis-width))
140 (setq end-column (- end-column ellipsis-width))
141 (setq ellipsis ""))
be9778f8
RS
142 (condition-case nil
143 (while (< column end-column)
144 (setq last-column column
145 last-idx idx
dbea6766 146 ch (aref str idx)
be9778f8 147 column (+ column (char-width ch))
dbea6766 148 idx (1+ idx)))
171a7d5d
CW
149 (args-out-of-range (setq idx str-len)))
150 (when (> column end-column)
151 (setq column last-column
152 idx last-idx))
153 (when (and padding (< column end-column))
154 (setq tail-padding (make-string (- end-column column) padding))))
155 (concat head-padding (substring str from-idx idx)
156 tail-padding ellipsis))))
157
158;;; Test suite for truncate-string-to-width
159;; (dolist (test '((("" 0) . "")
160;; (("x" 1) . "x")
161;; (("xy" 1) . "x")
162;; (("xy" 2 1) . "y")
163;; (("xy" 0) . "")
164;; (("xy" 3) . "xy")
165;; (("\e$AVP\e(B" 0) . "")
166;; (("\e$AVP\e(B" 1) . "")
167;; (("\e$AVP\e(B" 2) . "\e$AVP\e(B")
168;; (("\e$AVP\e(B" 1 nil ? ) . " ")
169;; (("\e$AVPND\e(B" 3 1 ? ) . " ")
170;; (("x\e$AVP\e(Bx" 2) . "x")
171;; (("x\e$AVP\e(Bx" 3) . "x\e$AVP\e(B")
172;; (("x\e$AVP\e(Bx" 3) . "x\e$AVP\e(B")
173;; (("x\e$AVP\e(Bx" 4 1) . "\e$AVP\e(Bx")
174;; (("kor\e$(CGQ\e(Be\e$(C1[\e(Ban" 8 1 ? ) . "or\e$(CGQ\e(Be\e$(C1[\e(B")
175;; (("kor\e$(CGQ\e(Be\e$(C1[\e(Ban" 7 2 ? ) . "r\e$(CGQ\e(Be ")
176;; (("" 0 nil nil "...") . "")
177;; (("x" 3 nil nil "...") . "x")
178;; (("\e$AVP\e(B" 3 nil nil "...") . "\e$AVP\e(B")
179;; (("foo" 3 nil nil "...") . "foo")
180;; (("foo" 2 nil nil "...") . "fo") ;; XEmacs failure?
181;; (("foobar" 6 0 nil "...") . "foobar")
182;; (("foobarbaz" 6 nil nil "...") . "foo...")
183;; (("foobarbaz" 7 2 nil "...") . "ob...")
184;; (("foobarbaz" 9 3 nil "...") . "barbaz")
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" 15 1 ? t) . " h\e$B$s\e(Be\e$B$K\e(Bl\e$B$A\e(Bl\e$B$O\e(Bo")
186;; (("\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...")
187;; (("x" 3 nil nil "\e$(0GnM$\e(B") . "x")
188;; (("\e$AVP\e(B" 2 nil nil "\e$(0GnM$\e(B") . "\e$AVP\e(B")
189;; (("\e$AVP\e(B" 1 nil ?x "\e$(0GnM$\e(B") . "x") ;; XEmacs error
190;; (("\e$AVPND\e(B" 3 nil ? "\e$(0GnM$\e(B") . "\e$AVP\e(B ") ;; XEmacs error
191;; (("foobarbaz" 4 nil nil "\e$(0GnM$\e(B") . "\e$(0GnM$\e(B")
192;; (("foobarbaz" 5 nil nil "\e$(0GnM$\e(B") . "f\e$(0GnM$\e(B")
193;; (("foobarbaz" 6 nil nil "\e$(0GnM$\e(B") . "fo\e$(0GnM$\e(B")
194;; (("foobarbaz" 8 3 nil "\e$(0GnM$\e(B") . "b\e$(0GnM$\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" 14 4 ?x "\e$BF|K\8l\e(B") . "xe\e$B$KF|K\8l\e(B")
196;; (("\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")
197;; ))
198;; (let (ret)
2fb49346 199;; (condition-case e
171a7d5d
CW
200;; (setq ret (apply #'truncate-string-to-width (car test)))
201;; (error (setq ret e)))
202;; (unless (equal ret (cdr test))
203;; (error "%s: expected %s, got %s"
204;; (prin1-to-string (cons 'truncate-string-to-width (car test)))
205;; (prin1-to-string (cdr test))
206;; (if (consp ret)
207;; (format "error: %s: %s" (car ret)
208;; (prin1-to-string (cdr ret)))
209;; (prin1-to-string ret))))))
4ed46869 210
e8dd0160 211;;; For backward compatibility ...
4ed46869
KH
212;;;###autoload
213(defalias 'truncate-string 'truncate-string-to-width)
ad0be9a1
JB
214
215;;;###autoload
2598a293 216(make-obsolete 'truncate-string 'truncate-string-to-width "20.1")
4ed46869
KH
217\f
218;;; Nested alist handler. Nested alist is alist whose elements are
219;;; also nested alist.
220
221;;;###autoload
222(defsubst nested-alist-p (obj)
e8dd0160 223 "Return t if OBJ is a nested alist.
4ed46869
KH
224
225Nested alist is a list of the form (ENTRY . BRANCHES), where ENTRY is
226any Lisp object, and BRANCHES is a list of cons cells of the form
4a30e92e 227\(KEY-ELEMENT . NESTED-ALIST).
4ed46869
KH
228
229You can use a nested alist to store any Lisp object (ENTRY) for a key
230sequence KEYSEQ, where KEYSEQ is a sequence of KEY-ELEMENT. KEYSEQ
231can be a string, a vector, or a list."
232 (and obj (listp obj) (listp (cdr obj))))
233
234;;;###autoload
235(defun set-nested-alist (keyseq entry alist &optional len branches)
236 "Set ENTRY for KEYSEQ in a nested alist ALIST.
e8dd0160 237Optional 4th arg LEN non-nil means the first LEN elements in KEYSEQ
4ed46869
KH
238 is considered.
239Optional argument BRANCHES if non-nil is branches for a keyseq
240longer than KEYSEQ.
241See the documentation of `nested-alist-p' for more detail."
242 (or (nested-alist-p alist)
e8dd0160 243 (error "Invalid argument %s" alist))
4ed46869
KH
244 (let ((islist (listp keyseq))
245 (len (or len (length keyseq)))
246 (i 0)
247 key-elt slot)
248 (while (< i len)
249 (if (null (nested-alist-p alist))
250 (error "Keyseq %s is too long for this nested alist" keyseq))
251 (setq key-elt (if islist (nth i keyseq) (aref keyseq i)))
252 (setq slot (assoc key-elt (cdr alist)))
253 (if (null slot)
254 (progn
255 (setq slot (cons key-elt (list t)))
256 (setcdr alist (cons slot (cdr alist)))))
257 (setq alist (cdr slot))
258 (setq i (1+ i)))
259 (setcar alist entry)
260 (if branches
72594565 261 (setcdr (last alist) branches))))
4ed46869
KH
262
263;;;###autoload
264(defun lookup-nested-alist (keyseq alist &optional len start nil-for-too-long)
265 "Look up key sequence KEYSEQ in nested alist ALIST. Return the definition.
266Optional 1st argument LEN specifies the length of KEYSEQ.
267Optional 2nd argument START specifies index of the starting key.
268The returned value is normally a nested alist of which
269car part is the entry for KEYSEQ.
270If ALIST is not deep enough for KEYSEQ, return number which is
271 how many key elements at the front of KEYSEQ it takes
272 to reach a leaf in ALIST.
273Optional 3rd argument NIL-FOR-TOO-LONG non-nil means return nil
274 even if ALIST is not deep enough."
275 (or (nested-alist-p alist)
f6afe80c 276 (error "Invalid argument %s" alist))
4ed46869
KH
277 (or len
278 (setq len (length keyseq)))
279 (let ((i (or start 0)))
280 (if (catch 'lookup-nested-alist-tag
281 (if (listp keyseq)
282 (while (< i len)
283 (if (setq alist (cdr (assoc (nth i keyseq) (cdr alist))))
284 (setq i (1+ i))
285 (throw 'lookup-nested-alist-tag t))))
286 (while (< i len)
287 (if (setq alist (cdr (assoc (aref keyseq i) (cdr alist))))
288 (setq i (1+ i))
289 (throw 'lookup-nested-alist-tag t))))
290 ;; KEYSEQ is too long.
291 (if nil-for-too-long nil i)
292 alist)))
293
be1d31dc 294\f
4ed46869
KH
295;; Coding system related functions.
296
be1d31dc
KH
297;;;###autoload
298(defun coding-system-post-read-conversion (coding-system)
bc9254e2 299 "Return the value of CODING-SYSTEM's `post-read-conversion' property."
f1fd88c6 300 (coding-system-get coding-system 'post-read-conversion))
be1d31dc
KH
301
302;;;###autoload
303(defun coding-system-pre-write-conversion (coding-system)
bc9254e2 304 "Return the value of CODING-SYSTEM's `pre-write-conversion' property."
f1fd88c6 305 (coding-system-get coding-system 'pre-write-conversion))
be1d31dc
KH
306
307;;;###autoload
d2a1ee18 308(defun coding-system-translation-table-for-decode (coding-system)
bc9254e2 309 "Return the value of CODING-SYSTEM's `translation-table-for-decode' property."
f967223b 310 (coding-system-get coding-system 'translation-table-for-decode))
34761746
KH
311
312;;;###autoload
d2a1ee18 313(defun coding-system-translation-table-for-encode (coding-system)
bc9254e2 314 "Return the value of CODING-SYSTEM's `translation-table-for-encode' property."
f967223b 315 (coding-system-get coding-system 'translation-table-for-encode))
be1d31dc 316
e481690d 317;;;###autoload
dbea6766
KH
318(defmacro detect-coding-with-priority (from to priority-list)
319 "Detect a coding system of the text between FROM and TO with PRIORITY-LIST.
320PRIORITY-LIST is an alist of coding categories vs the corresponding
321coding systems ordered by priority."
e74d7926
KH
322 `(unwind-protect
323 (let* ((prio-list ,priority-list)
324 (coding-category-list coding-category-list)
325 ,@(mapcar (function (lambda (x) (list x x)))
326 coding-category-list))
cb7216a7
DL
327 (mapc (function (lambda (x) (set (car x) (cdr x))))
328 prio-list)
329 (set-coding-priority (mapcar #'car prio-list))
e74d7926
KH
330 (detect-coding-region ,from ,to))
331 ;; We must restore the internal database.
332 (set-coding-priority coding-category-list)
333 (update-coding-systems-internal)))
dbea6766
KH
334
335;;;###autoload
336(defun detect-coding-with-language-environment (from to lang-env)
337 "Detect a coding system of the text between FROM and TO with LANG-ENV.
e8dd0160 338The detection takes into account the coding system priorities for the
dbea6766
KH
339language environment LANG-ENV."
340 (let ((coding-priority (get-language-info lang-env 'coding-priority)))
341 (if coding-priority
342 (detect-coding-with-priority
343 from to
344 (mapcar (function (lambda (x)
345 (cons (coding-system-get x 'coding-category) x)))
346 coding-priority))
347 (detect-coding-region from to))))
e481690d 348
8ce0e9a8
DL
349;;;###autoload
350(defun char-displayable-p (char)
351 "Return non-nil if we should be able to display CHAR.
352On a multi-font display, the test is only whether there is an
353appropriate font from the selected frame's fontset to display CHAR's
354charset in general. Since fonts may be specified on a per-character
355basis, this may not be accurate."
356 (cond ((< char 256)
357 ;; Single byte characters are always displayable.
358 t)
359 ((display-multi-font-p)
360 ;; On a window system, a character is displayable if we have
361 ;; a font for that character in the default face of the
362 ;; currently selected frame.
44416f23 363 (car (internal-char-font nil char)))
8ce0e9a8
DL
364 (t
365 (let ((coding (terminal-coding-system)))
366 (if coding
367 (let ((safe-chars (coding-system-get coding 'safe-chars))
368 (safe-charsets (coding-system-get coding 'safe-charsets)))
369 (or (and safe-chars
370 (aref safe-chars char))
371 (and safe-charsets
372 (memq (char-charset char) safe-charsets)))))))))
4ed46869 373\f
f6afe80c 374(provide 'mule-util)
72594565 375
171a7d5d
CW
376;; Local Variables:
377;; coding: iso-2022-7bit
378;; End:
379
ab5796a9 380;;; arch-tag: 5bdb52b6-a3a5-4529-b7a0-37d01b0e570b
f6afe80c 381;;; mule-util.el ends here