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