]> code.delx.au - gnu-emacs/blob - lisp/international/mule-util.el
682b850c2d8252bc16bf645985a3bcae49cad682
[gnu-emacs] / 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, 2004 Free Software Foundation, Inc.
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
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.
25
26 ;;; Commentary:
27
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.
36 TYPE should be `list' or `vector'."
37 ;;; (let ((len (length string))
38 ;;; (i 0)
39 ;;; val)
40 (cond ((eq type 'list)
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))))))
49 ((eq type 'vector)
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))))
56 (t
57 (error "Invalid type: %s" type)))
58 ;;; val)
59 )
60
61 ;;;###autoload
62 (make-obsolete 'string-to-sequence
63 "use `string-to-list' or `string-to-vector'."
64 "21.4")
65
66 ;;;###autoload
67 (defsubst string-to-list (string)
68 "Return a list of characters in STRING."
69 (append string nil))
70
71 ;;;###autoload
72 (defsubst string-to-vector (string)
73 "Return a vector of characters in STRING."
74 (vconcat string))
75
76 ;;;###autoload
77 (defun store-substring (string idx obj)
78 "Embed OBJ (string or character) at index IDX of STRING."
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)
88
89 ;;;###autoload
90 (defun truncate-string-to-width (str end-column
91 &optional start-column padding ellipsis)
92 "Truncate string STR to end at column END-COLUMN.
93 The optional 3rd arg START-COLUMN, if non-nil, specifies the starting
94 column; that means to return the characters occupying columns
95 START-COLUMN ... END-COLUMN of STR. Both END-COLUMN and START-COLUMN
96 are specified in terms of character display width in the current
97 buffer; see also `char-width'.
98
99 The optional 4th arg PADDING, if non-nil, specifies a padding
100 character (which should have a display width of 1) to add at the end
101 of the result if STR doesn't reach column END-COLUMN, or if END-COLUMN
102 comes in the middle of a character in STR. PADDING is also added at
103 the beginning of the result if column START-COLUMN appears in the
104 middle of a character in STR.
105
106 If PADDING is nil, no padding is added in these cases, so
107 the resulting string may be narrower than END-COLUMN.
108
109 If ELLIPSIS is non-nil, it should be a string which will replace the
110 end of STR (including any padding) if it extends beyond END-COLUMN,
111 unless the display width of STR is equal to or less than the display
112 width of ELLIPSIS. If it is non-nil and not a string, then ELLIPSIS
113 defaults to \"...\"."
114 (or start-column
115 (setq start-column 0))
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))
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)
128 (setq ch (aref str idx)
129 column (+ column (char-width ch))
130 idx (1+ idx)))
131 (args-out-of-range (setq idx str-len)))
132 (if (< column start-column)
133 (if padding (make-string end-column padding) "")
134 (when (and padding (> column start-column))
135 (setq head-padding (make-string (- column start-column) padding)))
136 (setq from-idx idx)
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 ""))
142 (condition-case nil
143 (while (< column end-column)
144 (setq last-column column
145 last-idx idx
146 ch (aref str idx)
147 column (+ column (char-width ch))
148 idx (1+ idx)))
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)
199 ;; (condition-case e
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))))))
210
211 ;;; For backward compatibility ...
212 ;;;###autoload
213 (defalias 'truncate-string 'truncate-string-to-width)
214
215 ;;;###autoload
216 (make-obsolete 'truncate-string 'truncate-string-to-width "20.1")
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 `translation-table-for-decode' property."
310 (coding-system-get coding-system 'translation-table-for-decode))
311
312 ;;;###autoload
313 (defun coding-system-translation-table-for-encode (coding-system)
314 "Return the value of CODING-SYSTEM's `translation-table-for-encode' property."
315 (coding-system-get coding-system 'translation-table-for-encode))
316
317 ;;;###autoload
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.
320 PRIORITY-LIST is an alist of coding categories vs the corresponding
321 coding systems ordered by priority."
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))
327 (mapc (function (lambda (x) (set (car x) (cdr x))))
328 prio-list)
329 (set-coding-priority (mapcar #'car prio-list))
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)))
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.
338 The detection takes into account the coding system priorities for the
339 language 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))))
348
349 ;;;###autoload
350 (defun char-displayable-p (char)
351 "Return non-nil if we should be able to display CHAR.
352 On a multi-font display, the test is only whether there is an
353 appropriate font from the selected frame's fontset to display CHAR's
354 charset in general. Since fonts may be specified on a per-character
355 basis, this may not be accurate."
356 (cond ((< char 256)
357 ;; Single byte characters are always displayable.
358 t)
359 ((not enable-multibyte-characters)
360 ;; Maybe there's a font for it, but we can't put it in the buffer.
361 nil)
362 ((display-multi-font-p)
363 ;; On a window system, a character is displayable if we have
364 ;; a font for that character in the default face of the
365 ;; currently selected frame.
366 (car (internal-char-font nil char)))
367 (t
368 (let ((coding (terminal-coding-system)))
369 (if coding
370 (let ((safe-chars (coding-system-get coding 'safe-chars))
371 (safe-charsets (coding-system-get coding 'safe-charsets)))
372 (or (and safe-chars
373 (aref safe-chars char))
374 (and safe-charsets
375 (memq (char-charset char) safe-charsets)))))))))
376 \f
377 (provide 'mule-util)
378
379 ;; Local Variables:
380 ;; coding: iso-2022-7bit
381 ;; End:
382
383 ;; arch-tag: 5bdb52b6-a3a5-4529-b7a0-37d01b0e570b
384 ;;; mule-util.el ends here