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