Revision: miles@gnu.org--gnu-2004/emacs--unicode--0--patch-15
[bpt/emacs.git] / 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 ;; Copyright (C) 2000, 2002 Free Software Foundation, Inc.
6 ;; Copyright (C) 2003
7 ;; National Institute of Advanced Industrial Science and Technology (AIST)
8 ;; Registration Number H13PRO009
9
10 ;; Keywords: mule, multilingual
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28
29 ;;; Commentary:
30
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.
39 TYPE should be `list' or `vector'."
40 ;;; (let ((len (length string))
41 ;;; (i 0)
42 ;;; val)
43 (cond ((eq type 'list)
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))))))
52 ((eq type 'vector)
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))))
59 (t
60 (error "Invalid type: %s" type)))
61 ;;; val)
62 )
63
64 ;;;###autoload
65 (make-obsolete 'string-to-sequence
66 "use `string-to-list' or `string-to-vector'."
67 "21.4")
68
69 ;;;###autoload
70 (defsubst string-to-list (string)
71 "Return a list of characters in STRING."
72 (append string nil))
73
74 ;;;###autoload
75 (defsubst string-to-vector (string)
76 "Return a vector of characters in STRING."
77 (vconcat string))
78
79 ;;;###autoload
80 (defun store-substring (string idx obj)
81 "Embed OBJ (string or character) at index IDX of STRING."
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)
91
92 ;;;###autoload
93 (defun truncate-string-to-width (str end-column
94 &optional start-column padding ellipsis)
95 "Truncate string STR to end at column END-COLUMN.
96 The optional 3rd arg START-COLUMN, if non-nil, specifies the starting
97 column; that means to return the characters occupying columns
98 START-COLUMN ... END-COLUMN of STR. Both END-COLUMN and START-COLUMN
99 are specified in terms of character display width in the current
100 buffer; see also `char-width'.
101
102 The optional 4th arg PADDING, if non-nil, specifies a padding
103 character (which should have a display width of 1) to add at the end
104 of the result if STR doesn't reach column END-COLUMN, or if END-COLUMN
105 comes in the middle of a character in STR. PADDING is also added at
106 the beginning of the result if column START-COLUMN appears in the
107 middle of a character in STR.
108
109 If PADDING is nil, no padding is added in these cases, so
110 the resulting string may be narrower than END-COLUMN.
111
112 If ELLIPSIS is non-nil, it should be a string which will replace the
113 end of STR (including any padding) if it extends beyond END-COLUMN,
114 unless the display width of STR is equal to or less than the display
115 width of ELLIPSIS. If it is non-nil and not a string, then ELLIPSIS
116 defaults to \"...\"."
117 (or start-column
118 (setq start-column 0))
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))
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)
131 (setq ch (aref str idx)
132 column (+ column (char-width ch))
133 idx (1+ idx)))
134 (args-out-of-range (setq idx str-len)))
135 (if (< column start-column)
136 (if padding (make-string end-column padding) "")
137 (when (and padding (> column start-column))
138 (setq head-padding (make-string (- column start-column) padding)))
139 (setq from-idx idx)
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 ""))
145 (condition-case nil
146 (while (< column end-column)
147 (setq last-column column
148 last-idx idx
149 ch (aref str idx)
150 column (+ column (char-width ch))
151 idx (1+ idx)))
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")
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")
200 ;; ))
201 ;; (let (ret)
202 ;; (condition-case e
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))))))
213
214 ;;; For backward compatibility ...
215 ;;;###autoload
216 (defalias 'truncate-string 'truncate-string-to-width)
217
218 ;;;###autoload
219 (make-obsolete 'truncate-string 'truncate-string-to-width "20.1")
220 \f
221 ;;; Nested alist handler. Nested alist is alist whose elements are
222 ;;; also nested alist.
223
224 ;;;###autoload
225 (defsubst nested-alist-p (obj)
226 "Return t if OBJ is a nested alist.
227
228 Nested alist is a list of the form (ENTRY . BRANCHES), where ENTRY is
229 any Lisp object, and BRANCHES is a list of cons cells of the form
230 \(KEY-ELEMENT . NESTED-ALIST).
231
232 You can use a nested alist to store any Lisp object (ENTRY) for a key
233 sequence KEYSEQ, where KEYSEQ is a sequence of KEY-ELEMENT. KEYSEQ
234 can be a string, a vector, or a list."
235 (and obj (listp obj) (listp (cdr obj))))
236
237 ;;;###autoload
238 (defun set-nested-alist (keyseq entry alist &optional len branches)
239 "Set ENTRY for KEYSEQ in a nested alist ALIST.
240 Optional 4th arg LEN non-nil means the first LEN elements in KEYSEQ
241 is considered.
242 Optional argument BRANCHES if non-nil is branches for a keyseq
243 longer than KEYSEQ.
244 See the documentation of `nested-alist-p' for more detail."
245 (or (nested-alist-p alist)
246 (error "Invalid argument %s" alist))
247 (let ((islist (listp keyseq))
248 (len (or len (length keyseq)))
249 (i 0)
250 key-elt slot)
251 (while (< i len)
252 (if (null (nested-alist-p alist))
253 (error "Keyseq %s is too long for this nested alist" keyseq))
254 (setq key-elt (if islist (nth i keyseq) (aref keyseq i)))
255 (setq slot (assoc key-elt (cdr alist)))
256 (if (null slot)
257 (progn
258 (setq slot (cons key-elt (list t)))
259 (setcdr alist (cons slot (cdr alist)))))
260 (setq alist (cdr slot))
261 (setq i (1+ i)))
262 (setcar alist entry)
263 (if branches
264 (setcdr (last alist) branches))))
265
266 ;;;###autoload
267 (defun lookup-nested-alist (keyseq alist &optional len start nil-for-too-long)
268 "Look up key sequence KEYSEQ in nested alist ALIST. Return the definition.
269 Optional 1st argument LEN specifies the length of KEYSEQ.
270 Optional 2nd argument START specifies index of the starting key.
271 The returned value is normally a nested alist of which
272 car part is the entry for KEYSEQ.
273 If ALIST is not deep enough for KEYSEQ, return number which is
274 how many key elements at the front of KEYSEQ it takes
275 to reach a leaf in ALIST.
276 Optional 3rd argument NIL-FOR-TOO-LONG non-nil means return nil
277 even if ALIST is not deep enough."
278 (or (nested-alist-p alist)
279 (error "Invalid argument %s" alist))
280 (or len
281 (setq len (length keyseq)))
282 (let ((i (or start 0)))
283 (if (catch 'lookup-nested-alist-tag
284 (if (listp keyseq)
285 (while (< i len)
286 (if (setq alist (cdr (assoc (nth i keyseq) (cdr alist))))
287 (setq i (1+ i))
288 (throw 'lookup-nested-alist-tag t))))
289 (while (< i len)
290 (if (setq alist (cdr (assoc (aref keyseq i) (cdr alist))))
291 (setq i (1+ i))
292 (throw 'lookup-nested-alist-tag t))))
293 ;; KEYSEQ is too long.
294 (if nil-for-too-long nil i)
295 alist)))
296
297 \f
298 ;; Coding system related functions.
299
300 ;;;###autoload
301 (defun coding-system-post-read-conversion (coding-system)
302 "Return the value of CODING-SYSTEM's `post-read-conversion' property."
303 (coding-system-get coding-system :post-read-conversion))
304
305 ;;;###autoload
306 (defun coding-system-pre-write-conversion (coding-system)
307 "Return the value of CODING-SYSTEM's `pre-write-conversion' property."
308 (coding-system-get coding-system :pre-write-conversion))
309
310 ;;;###autoload
311 (defun coding-system-translation-table-for-decode (coding-system)
312 "Return the value of CODING-SYSTEM's `decode-translation-table' property."
313 (coding-system-get coding-system :decode-translation-table))
314
315 ;;;###autoload
316 (defun coding-system-translation-table-for-encode (coding-system)
317 "Return the value of CODING-SYSTEM's `encode-translation-table' property."
318 (coding-system-get coding-system :encode-translation-table))
319
320 ;;;###autoload
321 (defun coding-system-equal (coding-system-1 coding-system-2)
322 "Return t if and only if CODING-SYSTEM-1 and CODING-SYSTEM-2 are identical.
323 Two coding systems are identical if two symbols are equal
324 or one is an alias of the other."
325 (or (eq coding-system-1 coding-system-2)
326 (and (equal (coding-system-plist coding-system-1)
327 (coding-system-plist coding-system-2))
328 (let ((eol-type-1 (coding-system-eol-type coding-system-1))
329 (eol-type-2 (coding-system-eol-type coding-system-2)))
330 (or (eq eol-type-1 eol-type-2)
331 (and (vectorp eol-type-1) (vectorp eol-type-2)))))))
332
333 ;;;###autoload
334 (defmacro with-coding-priority (coding-systems &rest body)
335 "Execute BODY like `progn' with CODING-SYSTEMS at the front of priority list.
336 CODING-SYSTEMS is a list of coding systems. See
337 `set-coding-priority'. This affects the implicit sorting of lists of
338 coding sysems returned by operations such as `find-coding-systems-region'."
339 (let ((current (make-symbol "current")))
340 `(let ((,current (coding-system-priority-list)))
341 (apply #'set-coding-system-priority ,coding-systems)
342 (unwind-protect
343 (progn ,@body)
344 (apply #'set-coding-system-priority ,current)))))
345 (put 'with-coding-priority 'lisp-indent-function 1)
346 (put 'with-coding-priority 'edebug-form-spec t)
347
348 ;;;###autoload
349 (defmacro detect-coding-with-priority (from to priority-list)
350 "Detect a coding system of the text between FROM and TO with PRIORITY-LIST.
351 PRIORITY-LIST is an alist of coding categories vs the corresponding
352 coding systems ordered by priority."
353 `(with-coding-priority (mapcar #'cdr ,priority-list)
354 (detect-coding-region ,from ,to)))
355 (make-obsolete 'detect-coding-with-priority
356 "Use with-coding-priority and detect-coding-region" "22.1")
357
358 ;;;###autoload
359 (defun detect-coding-with-language-environment (from to lang-env)
360 "Detect a coding system for the text between FROM and TO with LANG-ENV.
361 The detection takes into account the coding system priorities for the
362 language environment LANG-ENV."
363 (let ((coding-priority (get-language-info lang-env 'coding-priority)))
364 (if coding-priority
365 (with-coding-priority coding-priority
366 (detect-coding-region from to)))))
367
368 ;;;###autoload
369 (defun char-displayable-p (char)
370 "Return non-nil if we should be able to display CHAR.
371 On a multi-font display, the test is only whether there is an
372 appropriate font from the selected frame's fontset to display CHAR's
373 charset in general. Since fonts may be specified on a per-character
374 basis, this may not be accurate."
375 (cond ((< char 256)
376 ;; Single byte characters are always displayable.
377 t)
378 ((display-multi-font-p)
379 ;; On a window system, a character is displayable if we have
380 ;; a font for that character in the default face of the
381 ;; currently selected frame.
382 (car (internal-char-font nil char)))
383 (t
384 (let ((coding (terminal-coding-system)))
385 (if coding
386 (let ((safe-chars (coding-system-get coding 'safe-chars))
387 (safe-charsets (coding-system-get coding 'safe-charsets)))
388 (or (and safe-chars
389 (aref safe-chars char))
390 (and safe-charsets
391 (memq (char-charset char) safe-charsets)))))))))
392 \f
393 (provide 'mule-util)
394
395 ;; Local Variables:
396 ;; coding: iso-2022-7bit
397 ;; End:
398
399 ;;; arch-tag: 5bdb52b6-a3a5-4529-b7a0-37d01b0e570b
400 ;;; mule-util.el ends here