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