]> code.delx.au - gnu-emacs/blob - lisp/international/mule-util.el
*** empty log message ***
[gnu-emacs] / lisp / international / mule-util.el
1 ;;; mule-util.el --- utility functions for mulitilingual environment (mule)
2
3 ;; Copyright (C) 1997, 1998, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
5 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
6 ;; 2005, 2006, 2007
7 ;; National Institute of Advanced Industrial Science and Technology (AIST)
8 ;; Registration Number H14PRO021
9 ;; Copyright (C) 2003
10 ;; National Institute of Advanced Industrial Science and Technology (AIST)
11 ;; Registration Number H13PRO009
12
13 ;; Keywords: mule, multilingual
14
15 ;; This file is part of GNU Emacs.
16
17 ;; GNU Emacs is free software; you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation; either version 3, or (at your option)
20 ;; any later version.
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
28 ;; along with GNU Emacs; see the file COPYING. If not, write to the
29 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
30 ;; Boston, MA 02110-1301, USA.
31
32 ;;; Commentary:
33
34 ;;; Code:
35
36 ;;; String manipulations while paying attention to multibyte
37 ;;; characters.
38
39 ;;;###autoload
40 (defun string-to-sequence (string type)
41 "Convert STRING to a sequence of TYPE which contains characters in STRING.
42 TYPE should be `list' or `vector'."
43 ;;; (let ((len (length string))
44 ;;; (i 0)
45 ;;; val)
46 (cond ((eq type 'list)
47 ;; Applicable post-Emacs 20.2 and asymptotically ~10 times
48 ;; faster than the code below:
49 (append string nil))
50 ;;; (setq val (make-list len 0))
51 ;;; (let ((l val))
52 ;;; (while (< i len)
53 ;;; (setcar l (aref string i))
54 ;;; (setq l (cdr l) i (1+ i))))))
55 ((eq type 'vector)
56 ;; As above.
57 (vconcat string))
58 ;;; (setq val (make-vector len 0))
59 ;;; (while (< i len)
60 ;;; (aset val i (aref string i))
61 ;;; (setq i (1+ i))))
62 (t
63 (error "Invalid type: %s" type)))
64 ;;; val)
65 )
66
67 ;;;###autoload
68 (make-obsolete 'string-to-sequence
69 "use `string-to-list' or `string-to-vector'."
70 "22.1")
71
72 ;;;###autoload
73 (defsubst string-to-list (string)
74 "Return a list of characters in STRING."
75 (append string nil))
76
77 ;;;###autoload
78 (defsubst string-to-vector (string)
79 "Return a vector of characters in STRING."
80 (vconcat string))
81
82 ;;;###autoload
83 (defun store-substring (string idx obj)
84 "Embed OBJ (string or character) at index IDX of STRING."
85 (if (integerp obj)
86 (aset string idx obj)
87 (let ((len1 (length obj))
88 (len2 (length string))
89 (i 0))
90 (while (< i len1)
91 (aset string (+ idx i) (aref obj i))
92 (setq i (1+ i)))))
93 string)
94
95 ;;;###autoload
96 (defun truncate-string-to-width (str end-column
97 &optional start-column padding ellipsis)
98 "Truncate string STR to end at column END-COLUMN.
99 The optional 3rd arg START-COLUMN, if non-nil, specifies the starting
100 column; that means to return the characters occupying columns
101 START-COLUMN ... END-COLUMN of STR. Both END-COLUMN and START-COLUMN
102 are specified in terms of character display width in the current
103 buffer; see also `char-width'.
104
105 The optional 4th arg PADDING, if non-nil, specifies a padding
106 character (which should have a display width of 1) to add at the end
107 of the result if STR doesn't reach column END-COLUMN, or if END-COLUMN
108 comes in the middle of a character in STR. PADDING is also added at
109 the beginning of the result if column START-COLUMN appears in the
110 middle of a character in STR.
111
112 If PADDING is nil, no padding is added in these cases, so
113 the resulting string may be narrower than END-COLUMN.
114
115 If ELLIPSIS is non-nil, it should be a string which will replace the
116 end of STR (including any padding) if it extends beyond END-COLUMN,
117 unless the display width of STR is equal to or less than the display
118 width of ELLIPSIS. If it is non-nil and not a string, then ELLIPSIS
119 defaults to \"...\"."
120 (or start-column
121 (setq start-column 0))
122 (when (and ellipsis (not (stringp ellipsis)))
123 (setq ellipsis "..."))
124 (let ((str-len (length str))
125 (str-width (string-width str))
126 (ellipsis-len (if ellipsis (length ellipsis) 0))
127 (ellipsis-width (if ellipsis (string-width ellipsis) 0))
128 (idx 0)
129 (column 0)
130 (head-padding "") (tail-padding "")
131 ch last-column last-idx from-idx)
132 (condition-case nil
133 (while (< column start-column)
134 (setq ch (aref str idx)
135 column (+ column (char-width ch))
136 idx (1+ idx)))
137 (args-out-of-range (setq idx str-len)))
138 (if (< column start-column)
139 (if padding (make-string end-column padding) "")
140 (when (and padding (> column start-column))
141 (setq head-padding (make-string (- column start-column) padding)))
142 (setq from-idx idx)
143 (when (>= end-column column)
144 (if (and (< end-column str-width)
145 (> str-width ellipsis-width))
146 (setq end-column (- end-column ellipsis-width))
147 (setq ellipsis ""))
148 (condition-case nil
149 (while (< column end-column)
150 (setq last-column column
151 last-idx idx
152 ch (aref str idx)
153 column (+ column (char-width ch))
154 idx (1+ idx)))
155 (args-out-of-range (setq idx str-len)))
156 (when (> column end-column)
157 (setq column last-column
158 idx last-idx))
159 (when (and padding (< column end-column))
160 (setq tail-padding (make-string (- end-column column) padding))))
161 (concat head-padding (substring str from-idx idx)
162 tail-padding ellipsis))))
163
164 ;;; Test suite for truncate-string-to-width
165 ;; (dolist (test '((("" 0) . "")
166 ;; (("x" 1) . "x")
167 ;; (("xy" 1) . "x")
168 ;; (("xy" 2 1) . "y")
169 ;; (("xy" 0) . "")
170 ;; (("xy" 3) . "xy")
171 ;; (("\e$AVP\e(B" 0) . "")
172 ;; (("\e$AVP\e(B" 1) . "")
173 ;; (("\e$AVP\e(B" 2) . "\e$AVP\e(B")
174 ;; (("\e$AVP\e(B" 1 nil ? ) . " ")
175 ;; (("\e$AVPND\e(B" 3 1 ? ) . " ")
176 ;; (("x\e$AVP\e(Bx" 2) . "x")
177 ;; (("x\e$AVP\e(Bx" 3) . "x\e$AVP\e(B")
178 ;; (("x\e$AVP\e(Bx" 3) . "x\e$AVP\e(B")
179 ;; (("x\e$AVP\e(Bx" 4 1) . "\e$AVP\e(Bx")
180 ;; (("kor\e$(CGQ\e(Be\e$(C1[\e(Ban" 8 1 ? ) . "or\e$(CGQ\e(Be\e$(C1[\e(B")
181 ;; (("kor\e$(CGQ\e(Be\e$(C1[\e(Ban" 7 2 ? ) . "r\e$(CGQ\e(Be ")
182 ;; (("" 0 nil nil "...") . "")
183 ;; (("x" 3 nil nil "...") . "x")
184 ;; (("\e$AVP\e(B" 3 nil nil "...") . "\e$AVP\e(B")
185 ;; (("foo" 3 nil nil "...") . "foo")
186 ;; (("foo" 2 nil nil "...") . "fo") ;; XEmacs failure?
187 ;; (("foobar" 6 0 nil "...") . "foobar")
188 ;; (("foobarbaz" 6 nil nil "...") . "foo...")
189 ;; (("foobarbaz" 7 2 nil "...") . "ob...")
190 ;; (("foobarbaz" 9 3 nil "...") . "barbaz")
191 ;; (("\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")
192 ;; (("\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...")
193 ;; (("x" 3 nil nil "\e$(Gemk#\e(B") . "x")
194 ;; (("\e$AVP\e(B" 2 nil nil "\e$(Gemk#\e(B") . "\e$AVP\e(B")
195 ;; (("\e$AVP\e(B" 1 nil ?x "\e$(Gemk#\e(B") . "x") ;; XEmacs error
196 ;; (("\e$AVPND\e(B" 3 nil ? "\e$(Gemk#\e(B") . "\e$AVP\e(B ") ;; XEmacs error
197 ;; (("foobarbaz" 4 nil nil "\e$(Gemk#\e(B") . "\e$(Gemk#\e(B")
198 ;; (("foobarbaz" 5 nil nil "\e$(Gemk#\e(B") . "f\e$(Gemk#\e(B")
199 ;; (("foobarbaz" 6 nil nil "\e$(Gemk#\e(B") . "fo\e$(Gemk#\e(B")
200 ;; (("foobarbaz" 8 3 nil "\e$(Gemk#\e(B") . "b\e$(Gemk#\e(B")
201 ;; (("\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")
202 ;; (("\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")
203 ;; ))
204 ;; (let (ret)
205 ;; (condition-case e
206 ;; (setq ret (apply #'truncate-string-to-width (car test)))
207 ;; (error (setq ret e)))
208 ;; (unless (equal ret (cdr test))
209 ;; (error "%s: expected %s, got %s"
210 ;; (prin1-to-string (cons 'truncate-string-to-width (car test)))
211 ;; (prin1-to-string (cdr test))
212 ;; (if (consp ret)
213 ;; (format "error: %s: %s" (car ret)
214 ;; (prin1-to-string (cdr ret)))
215 ;; (prin1-to-string ret))))))
216
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)
223 "Return t if OBJ is a nested alist.
224
225 Nested alist is a list of the form (ENTRY . BRANCHES), where ENTRY is
226 any Lisp object, and BRANCHES is a list of cons cells of the form
227 \(KEY-ELEMENT . NESTED-ALIST).
228
229 You can use a nested alist to store any Lisp object (ENTRY) for a key
230 sequence KEYSEQ, where KEYSEQ is a sequence of KEY-ELEMENT. KEYSEQ
231 can 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.
237 Optional 4th arg LEN non-nil means the first LEN elements in KEYSEQ
238 is considered.
239 Optional argument BRANCHES if non-nil is branches for a keyseq
240 longer than KEYSEQ.
241 See the documentation of `nested-alist-p' for more detail."
242 (or (nested-alist-p alist)
243 (error "Invalid argument %s" alist))
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
261 (setcdr (last alist) branches))))
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.
266 Optional 1st argument LEN specifies the length of KEYSEQ.
267 Optional 2nd argument START specifies index of the starting key.
268 The returned value is normally a nested alist of which
269 car part is the entry for KEYSEQ.
270 If 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.
273 Optional 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)
276 (error "Invalid argument %s" alist))
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
294 \f
295 ;; Coding system related functions.
296
297 ;;;###autoload
298 (defun coding-system-post-read-conversion (coding-system)
299 "Return the value of CODING-SYSTEM's `post-read-conversion' property."
300 (coding-system-get coding-system :post-read-conversion))
301
302 ;;;###autoload
303 (defun coding-system-pre-write-conversion (coding-system)
304 "Return the value of CODING-SYSTEM's `pre-write-conversion' property."
305 (coding-system-get coding-system :pre-write-conversion))
306
307 ;;;###autoload
308 (defun coding-system-translation-table-for-decode (coding-system)
309 "Return the value of CODING-SYSTEM's `decode-translation-table' property."
310 (coding-system-get coding-system :decode-translation-table))
311
312 ;;;###autoload
313 (defun coding-system-translation-table-for-encode (coding-system)
314 "Return the value of CODING-SYSTEM's `encode-translation-table' property."
315 (coding-system-get coding-system :encode-translation-table))
316
317 ;;;###autoload
318 (defmacro with-coding-priority (coding-systems &rest body)
319 "Execute BODY like `progn' with CODING-SYSTEMS at the front of priority list.
320 CODING-SYSTEMS is a list of coding systems. See
321 `set-coding-priority'. This affects the implicit sorting of lists of
322 coding sysems returned by operations such as `find-coding-systems-region'."
323 (let ((current (make-symbol "current")))
324 `(let ((,current (coding-system-priority-list)))
325 (apply #'set-coding-system-priority ,coding-systems)
326 (unwind-protect
327 (progn ,@body)
328 (apply #'set-coding-system-priority ,current)))))
329 (put 'with-coding-priority 'lisp-indent-function 1)
330 (put 'with-coding-priority 'edebug-form-spec t)
331
332 ;;;###autoload
333 (defmacro detect-coding-with-priority (from to priority-list)
334 "Detect a coding system of the text between FROM and TO with PRIORITY-LIST.
335 PRIORITY-LIST is an alist of coding categories vs the corresponding
336 coding systems ordered by priority."
337 `(with-coding-priority (mapcar #'cdr ,priority-list)
338 (detect-coding-region ,from ,to)))
339 (make-obsolete 'detect-coding-with-priority
340 "Use with-coding-priority and detect-coding-region" "23.1")
341
342 ;;;###autoload
343 (defun detect-coding-with-language-environment (from to lang-env)
344 "Detect a coding system for the text between FROM and TO with LANG-ENV.
345 The detection takes into account the coding system priorities for the
346 language environment LANG-ENV."
347 (let ((coding-priority (get-language-info lang-env 'coding-priority)))
348 (if coding-priority
349 (with-coding-priority coding-priority
350 (detect-coding-region from to)))))
351
352 ;;;###autoload
353 (defun char-displayable-p (char)
354 "Return non-nil if we should be able to display CHAR.
355 On a multi-font display, the test is only whether there is an
356 appropriate font from the selected frame's fontset to display CHAR's
357 charset in general. Since fonts may be specified on a per-character
358 basis, this may not be accurate."
359 (cond ((< char 256)
360 ;; Single byte characters are always displayable.
361 t)
362 ((not enable-multibyte-characters)
363 ;; Maybe there's a font for it, but we can't put it in the buffer.
364 nil)
365 ((display-multi-font-p)
366 ;; On a window system, a character is displayable if we have
367 ;; a font for that character in the default face of the
368 ;; currently selected frame.
369 (car (internal-char-font nil char)))
370 (t
371 (let ((coding 'iso-2022-7bit))
372 (if 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)))))))))
400 \f
401 (provide 'mule-util)
402
403 ;; Local Variables:
404 ;; coding: iso-2022-7bit
405 ;; End:
406
407 ;; arch-tag: 5bdb52b6-a3a5-4529-b7a0-37d01b0e570b
408 ;;; mule-util.el ends here