Commit | Line | Data |
---|---|---|
60370d40 | 1 | ;;; mule-util.el --- utility functions for mulitilingual environment (mule) |
4ed46869 | 2 | |
2fd125a3 KH |
3 | ;; Copyright (C) 1997, 1998, 2000, 2001, 2002, 2003, 2004 |
4 | ;; Free Software Foundation, Inc. | |
5 | ;; Copyright (C) 1995, 1997, 1998, 1999, 2004 | |
6 | ;; National Institute of Advanced Industrial Science and Technology (AIST) | |
7 | ;; Registration Number H14PRO021 | |
4ed46869 KH |
8 | |
9 | ;; Keywords: mule, multilingual | |
10 | ||
11 | ;; This file is part of GNU Emacs. | |
12 | ||
13 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
14 | ;; it under the terms of the GNU General Public License as published by | |
15 | ;; the Free Software Foundation; either version 2, or (at your option) | |
16 | ;; any later version. | |
17 | ||
18 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
19 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | ;; GNU General Public License for more details. | |
22 | ||
23 | ;; You should have received a copy of the GNU General Public License | |
369314dc KH |
24 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
25 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
26 | ;; Boston, MA 02111-1307, USA. | |
4ed46869 | 27 | |
60370d40 PJ |
28 | ;;; Commentary: |
29 | ||
4ed46869 KH |
30 | ;;; Code: |
31 | ||
32 | ;;; String manipulations while paying attention to multibyte | |
33 | ;;; characters. | |
34 | ||
35 | ;;;###autoload | |
36 | (defun string-to-sequence (string type) | |
37 | "Convert STRING to a sequence of TYPE which contains characters in STRING. | |
88d9cc1e | 38 | TYPE should be `list' or `vector'." |
f6afe80c DL |
39 | ;;; (let ((len (length string)) |
40 | ;;; (i 0) | |
41 | ;;; val) | |
dbea6766 | 42 | (cond ((eq type 'list) |
f6afe80c DL |
43 | ;; Applicable post-Emacs 20.2 and asymptotically ~10 times |
44 | ;; faster than the code below: | |
45 | (append string nil)) | |
46 | ;;; (setq val (make-list len 0)) | |
47 | ;;; (let ((l val)) | |
48 | ;;; (while (< i len) | |
49 | ;;; (setcar l (aref string i)) | |
50 | ;;; (setq l (cdr l) i (1+ i)))))) | |
dbea6766 | 51 | ((eq type 'vector) |
f6afe80c DL |
52 | ;; As above. |
53 | (vconcat string)) | |
54 | ;;; (setq val (make-vector len 0)) | |
55 | ;;; (while (< i len) | |
56 | ;;; (aset val i (aref string i)) | |
57 | ;;; (setq i (1+ i)))) | |
dbea6766 KH |
58 | (t |
59 | (error "Invalid type: %s" type))) | |
f6afe80c DL |
60 | ;;; val) |
61 | ) | |
ad0be9a1 JB |
62 | |
63 | ;;;###autoload | |
48e3f3d7 | 64 | (make-obsolete 'string-to-sequence |
5c92a2a6 | 65 | "use `string-to-list' or `string-to-vector'." |
bf247b6e | 66 | "22.1") |
4ed46869 KH |
67 | |
68 | ;;;###autoload | |
69 | (defsubst string-to-list (string) | |
70 | "Return a list of characters in STRING." | |
48e3f3d7 | 71 | (append string nil)) |
4ed46869 KH |
72 | |
73 | ;;;###autoload | |
74 | (defsubst string-to-vector (string) | |
75 | "Return a vector of characters in STRING." | |
48e3f3d7 | 76 | (vconcat string)) |
4ed46869 KH |
77 | |
78 | ;;;###autoload | |
79 | (defun store-substring (string idx obj) | |
80 | "Embed OBJ (string or character) at index IDX of STRING." | |
dbea6766 KH |
81 | (if (integerp obj) |
82 | (aset string idx obj) | |
83 | (let ((len1 (length obj)) | |
84 | (len2 (length string)) | |
85 | (i 0)) | |
86 | (while (< i len1) | |
87 | (aset string (+ idx i) (aref obj i)) | |
88 | (setq i (1+ i))))) | |
89 | string) | |
4ed46869 KH |
90 | |
91 | ;;;###autoload | |
171a7d5d CW |
92 | (defun truncate-string-to-width (str end-column |
93 | &optional start-column padding ellipsis) | |
be9778f8 | 94 | "Truncate string STR to end at column END-COLUMN. |
171a7d5d CW |
95 | The optional 3rd arg START-COLUMN, if non-nil, specifies the starting |
96 | column; that means to return the characters occupying columns | |
97 | START-COLUMN ... END-COLUMN of STR. Both END-COLUMN and START-COLUMN | |
98 | are specified in terms of character display width in the current | |
99 | buffer; see also `char-width'. | |
100 | ||
101 | The optional 4th arg PADDING, if non-nil, specifies a padding | |
102 | character (which should have a display width of 1) to add at the end | |
103 | of the result if STR doesn't reach column END-COLUMN, or if END-COLUMN | |
104 | comes in the middle of a character in STR. PADDING is also added at | |
105 | the beginning of the result if column START-COLUMN appears in the | |
106 | middle of a character in STR. | |
be9778f8 RS |
107 | |
108 | If PADDING is nil, no padding is added in these cases, so | |
171a7d5d CW |
109 | the resulting string may be narrower than END-COLUMN. |
110 | ||
111 | If ELLIPSIS is non-nil, it should be a string which will replace the | |
112 | end of STR (including any padding) if it extends beyond END-COLUMN, | |
113 | unless the display width of STR is equal to or less than the display | |
114 | width of ELLIPSIS. If it is non-nil and not a string, then ELLIPSIS | |
115 | defaults to \"...\"." | |
4ed46869 KH |
116 | (or start-column |
117 | (setq start-column 0)) | |
171a7d5d CW |
118 | (when (and ellipsis (not (stringp ellipsis))) |
119 | (setq ellipsis "...")) | |
120 | (let ((str-len (length str)) | |
121 | (str-width (string-width str)) | |
122 | (ellipsis-len (if ellipsis (length ellipsis) 0)) | |
123 | (ellipsis-width (if ellipsis (string-width ellipsis) 0)) | |
4ed46869 KH |
124 | (idx 0) |
125 | (column 0) | |
126 | (head-padding "") (tail-padding "") | |
127 | ch last-column last-idx from-idx) | |
128 | (condition-case nil | |
129 | (while (< column start-column) | |
dbea6766 | 130 | (setq ch (aref str idx) |
4ed46869 | 131 | column (+ column (char-width ch)) |
dbea6766 | 132 | idx (1+ idx))) |
171a7d5d | 133 | (args-out-of-range (setq idx str-len))) |
4ed46869 | 134 | (if (< column start-column) |
be9778f8 | 135 | (if padding (make-string end-column padding) "") |
171a7d5d CW |
136 | (when (and padding (> column start-column)) |
137 | (setq head-padding (make-string (- column start-column) padding))) | |
4ed46869 | 138 | (setq from-idx idx) |
171a7d5d CW |
139 | (when (>= end-column column) |
140 | (if (and (< end-column str-width) | |
141 | (> str-width ellipsis-width)) | |
142 | (setq end-column (- end-column ellipsis-width)) | |
143 | (setq ellipsis "")) | |
be9778f8 RS |
144 | (condition-case nil |
145 | (while (< column end-column) | |
146 | (setq last-column column | |
147 | last-idx idx | |
dbea6766 | 148 | ch (aref str idx) |
be9778f8 | 149 | column (+ column (char-width ch)) |
dbea6766 | 150 | idx (1+ idx))) |
171a7d5d CW |
151 | (args-out-of-range (setq idx str-len))) |
152 | (when (> column end-column) | |
153 | (setq column last-column | |
154 | idx last-idx)) | |
155 | (when (and padding (< column end-column)) | |
156 | (setq tail-padding (make-string (- end-column column) padding)))) | |
157 | (concat head-padding (substring str from-idx idx) | |
158 | tail-padding ellipsis)))) | |
159 | ||
160 | ;;; Test suite for truncate-string-to-width | |
161 | ;; (dolist (test '((("" 0) . "") | |
162 | ;; (("x" 1) . "x") | |
163 | ;; (("xy" 1) . "x") | |
164 | ;; (("xy" 2 1) . "y") | |
165 | ;; (("xy" 0) . "") | |
166 | ;; (("xy" 3) . "xy") | |
167 | ;; (("\e$AVP\e(B" 0) . "") | |
168 | ;; (("\e$AVP\e(B" 1) . "") | |
169 | ;; (("\e$AVP\e(B" 2) . "\e$AVP\e(B") | |
170 | ;; (("\e$AVP\e(B" 1 nil ? ) . " ") | |
171 | ;; (("\e$AVPND\e(B" 3 1 ? ) . " ") | |
172 | ;; (("x\e$AVP\e(Bx" 2) . "x") | |
173 | ;; (("x\e$AVP\e(Bx" 3) . "x\e$AVP\e(B") | |
174 | ;; (("x\e$AVP\e(Bx" 3) . "x\e$AVP\e(B") | |
175 | ;; (("x\e$AVP\e(Bx" 4 1) . "\e$AVP\e(Bx") | |
176 | ;; (("kor\e$(CGQ\e(Be\e$(C1[\e(Ban" 8 1 ? ) . "or\e$(CGQ\e(Be\e$(C1[\e(B") | |
177 | ;; (("kor\e$(CGQ\e(Be\e$(C1[\e(Ban" 7 2 ? ) . "r\e$(CGQ\e(Be ") | |
178 | ;; (("" 0 nil nil "...") . "") | |
179 | ;; (("x" 3 nil nil "...") . "x") | |
180 | ;; (("\e$AVP\e(B" 3 nil nil "...") . "\e$AVP\e(B") | |
181 | ;; (("foo" 3 nil nil "...") . "foo") | |
182 | ;; (("foo" 2 nil nil "...") . "fo") ;; XEmacs failure? | |
183 | ;; (("foobar" 6 0 nil "...") . "foobar") | |
184 | ;; (("foobarbaz" 6 nil nil "...") . "foo...") | |
185 | ;; (("foobarbaz" 7 2 nil "...") . "ob...") | |
186 | ;; (("foobarbaz" 9 3 nil "...") . "barbaz") | |
187 | ;; (("\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") | |
188 | ;; (("\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...") | |
189 | ;; (("x" 3 nil nil "\e$(0GnM$\e(B") . "x") | |
190 | ;; (("\e$AVP\e(B" 2 nil nil "\e$(0GnM$\e(B") . "\e$AVP\e(B") | |
191 | ;; (("\e$AVP\e(B" 1 nil ?x "\e$(0GnM$\e(B") . "x") ;; XEmacs error | |
192 | ;; (("\e$AVPND\e(B" 3 nil ? "\e$(0GnM$\e(B") . "\e$AVP\e(B ") ;; XEmacs error | |
193 | ;; (("foobarbaz" 4 nil nil "\e$(0GnM$\e(B") . "\e$(0GnM$\e(B") | |
194 | ;; (("foobarbaz" 5 nil nil "\e$(0GnM$\e(B") . "f\e$(0GnM$\e(B") | |
195 | ;; (("foobarbaz" 6 nil nil "\e$(0GnM$\e(B") . "fo\e$(0GnM$\e(B") | |
196 | ;; (("foobarbaz" 8 3 nil "\e$(0GnM$\e(B") . "b\e$(0GnM$\e(B") | |
197 | ;; (("\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") | |
198 | ;; (("\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") | |
199 | ;; )) | |
200 | ;; (let (ret) | |
2fb49346 | 201 | ;; (condition-case e |
171a7d5d CW |
202 | ;; (setq ret (apply #'truncate-string-to-width (car test))) |
203 | ;; (error (setq ret e))) | |
204 | ;; (unless (equal ret (cdr test)) | |
205 | ;; (error "%s: expected %s, got %s" | |
206 | ;; (prin1-to-string (cons 'truncate-string-to-width (car test))) | |
207 | ;; (prin1-to-string (cdr test)) | |
208 | ;; (if (consp ret) | |
209 | ;; (format "error: %s: %s" (car ret) | |
210 | ;; (prin1-to-string (cdr ret))) | |
211 | ;; (prin1-to-string ret)))))) | |
4ed46869 | 212 | |
4ed46869 KH |
213 | \f |
214 | ;;; Nested alist handler. Nested alist is alist whose elements are | |
215 | ;;; also nested alist. | |
216 | ||
217 | ;;;###autoload | |
218 | (defsubst nested-alist-p (obj) | |
e8dd0160 | 219 | "Return t if OBJ is a nested alist. |
4ed46869 KH |
220 | |
221 | Nested alist is a list of the form (ENTRY . BRANCHES), where ENTRY is | |
222 | any Lisp object, and BRANCHES is a list of cons cells of the form | |
4a30e92e | 223 | \(KEY-ELEMENT . NESTED-ALIST). |
4ed46869 KH |
224 | |
225 | You can use a nested alist to store any Lisp object (ENTRY) for a key | |
226 | sequence KEYSEQ, where KEYSEQ is a sequence of KEY-ELEMENT. KEYSEQ | |
227 | can be a string, a vector, or a list." | |
228 | (and obj (listp obj) (listp (cdr obj)))) | |
229 | ||
230 | ;;;###autoload | |
231 | (defun set-nested-alist (keyseq entry alist &optional len branches) | |
232 | "Set ENTRY for KEYSEQ in a nested alist ALIST. | |
e8dd0160 | 233 | Optional 4th arg LEN non-nil means the first LEN elements in KEYSEQ |
4ed46869 KH |
234 | is considered. |
235 | Optional argument BRANCHES if non-nil is branches for a keyseq | |
236 | longer than KEYSEQ. | |
237 | See the documentation of `nested-alist-p' for more detail." | |
238 | (or (nested-alist-p alist) | |
e8dd0160 | 239 | (error "Invalid argument %s" alist)) |
4ed46869 KH |
240 | (let ((islist (listp keyseq)) |
241 | (len (or len (length keyseq))) | |
242 | (i 0) | |
243 | key-elt slot) | |
244 | (while (< i len) | |
245 | (if (null (nested-alist-p alist)) | |
246 | (error "Keyseq %s is too long for this nested alist" keyseq)) | |
247 | (setq key-elt (if islist (nth i keyseq) (aref keyseq i))) | |
248 | (setq slot (assoc key-elt (cdr alist))) | |
249 | (if (null slot) | |
250 | (progn | |
251 | (setq slot (cons key-elt (list t))) | |
252 | (setcdr alist (cons slot (cdr alist))))) | |
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. | |
262 | Optional 1st argument LEN specifies the length of KEYSEQ. | |
263 | Optional 2nd argument START specifies index of the starting key. | |
264 | The returned value is normally a nested alist of which | |
265 | car part is the entry for KEYSEQ. | |
266 | If 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. | |
269 | Optional 3rd argument NIL-FOR-TOO-LONG non-nil means return nil | |
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." |
f1fd88c6 | 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." |
f1fd88c6 | 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) |
bc9254e2 | 305 | "Return the value of CODING-SYSTEM's `translation-table-for-decode' property." |
f967223b | 306 | (coding-system-get coding-system 'translation-table-for-decode)) |
34761746 KH |
307 | |
308 | ;;;###autoload | |
d2a1ee18 | 309 | (defun coding-system-translation-table-for-encode (coding-system) |
bc9254e2 | 310 | "Return the value of CODING-SYSTEM's `translation-table-for-encode' property." |
f967223b | 311 | (coding-system-get coding-system 'translation-table-for-encode)) |
be1d31dc | 312 | |
e481690d | 313 | ;;;###autoload |
dbea6766 KH |
314 | (defmacro detect-coding-with-priority (from to priority-list) |
315 | "Detect a coding system of the text between FROM and TO with PRIORITY-LIST. | |
316 | PRIORITY-LIST is an alist of coding categories vs the corresponding | |
317 | coding systems ordered by priority." | |
e74d7926 KH |
318 | `(unwind-protect |
319 | (let* ((prio-list ,priority-list) | |
320 | (coding-category-list coding-category-list) | |
321 | ,@(mapcar (function (lambda (x) (list x x))) | |
322 | coding-category-list)) | |
cb7216a7 DL |
323 | (mapc (function (lambda (x) (set (car x) (cdr x)))) |
324 | prio-list) | |
325 | (set-coding-priority (mapcar #'car prio-list)) | |
0f58da4c | 326 | ;; Changing the binding of a coding category requires this call. |
d49a52e4 | 327 | (update-coding-systems-internal) |
e74d7926 KH |
328 | (detect-coding-region ,from ,to)) |
329 | ;; We must restore the internal database. | |
330 | (set-coding-priority coding-category-list) | |
331 | (update-coding-systems-internal))) | |
dbea6766 KH |
332 | |
333 | ;;;###autoload | |
334 | (defun detect-coding-with-language-environment (from to lang-env) | |
335 | "Detect a coding system of the text between FROM and TO with LANG-ENV. | |
e8dd0160 | 336 | The detection takes into account the coding system priorities for the |
dbea6766 KH |
337 | language environment LANG-ENV." |
338 | (let ((coding-priority (get-language-info lang-env 'coding-priority))) | |
339 | (if coding-priority | |
340 | (detect-coding-with-priority | |
341 | from to | |
342 | (mapcar (function (lambda (x) | |
343 | (cons (coding-system-get x 'coding-category) x))) | |
344 | coding-priority)) | |
345 | (detect-coding-region from to)))) | |
e481690d | 346 | |
8ce0e9a8 DL |
347 | ;;;###autoload |
348 | (defun char-displayable-p (char) | |
349 | "Return non-nil if we should be able to display CHAR. | |
350 | On a multi-font display, the test is only whether there is an | |
351 | appropriate font from the selected frame's fontset to display CHAR's | |
352 | charset in general. Since fonts may be specified on a per-character | |
353 | basis, this may not be accurate." | |
354 | (cond ((< char 256) | |
355 | ;; Single byte characters are always displayable. | |
356 | t) | |
e7a4820c SM |
357 | ((not enable-multibyte-characters) |
358 | ;; Maybe there's a font for it, but we can't put it in the buffer. | |
359 | nil) | |
8ce0e9a8 DL |
360 | ((display-multi-font-p) |
361 | ;; On a window system, a character is displayable if we have | |
362 | ;; a font for that character in the default face of the | |
363 | ;; currently selected frame. | |
44416f23 | 364 | (car (internal-char-font nil char))) |
8ce0e9a8 DL |
365 | (t |
366 | (let ((coding (terminal-coding-system))) | |
367 | (if coding | |
368 | (let ((safe-chars (coding-system-get coding 'safe-chars)) | |
369 | (safe-charsets (coding-system-get coding 'safe-charsets))) | |
370 | (or (and safe-chars | |
371 | (aref safe-chars char)) | |
372 | (and safe-charsets | |
373 | (memq (char-charset char) safe-charsets))))))))) | |
4ed46869 | 374 | \f |
f6afe80c | 375 | (provide 'mule-util) |
72594565 | 376 | |
171a7d5d CW |
377 | ;; Local Variables: |
378 | ;; coding: iso-2022-7bit | |
379 | ;; End: | |
380 | ||
e7a4820c | 381 | ;; arch-tag: 5bdb52b6-a3a5-4529-b7a0-37d01b0e570b |
f6afe80c | 382 | ;;; mule-util.el ends here |