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