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)
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.
29 ;;; String manipulations while paying attention to multibyte
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))
39 (cond ((eq type
'list
)
40 ;; Applicable post-Emacs 20.2 and asymptotically ~10 times
41 ;; faster than the code below:
43 ;;; (setq val (make-list len 0))
46 ;;; (setcar l (aref string i))
47 ;;; (setq l (cdr l) i (1+ i))))))
51 ;;; (setq val (make-vector len 0))
53 ;;; (aset val i (aref string i))
56 (error "Invalid type: %s" type
)))
61 (make-obsolete 'string-to-sequence
62 "use `string-to-list' or `string-to-vector'."
66 (defsubst string-to-list
(string)
67 "Return a list of characters in STRING."
71 (defsubst string-to-vector
(string)
72 "Return a vector of characters in STRING."
76 (defun store-substring (string idx obj
)
77 "Embed OBJ (string or character) at index IDX of STRING."
80 (let ((len1 (length obj
))
81 (len2 (length string
))
84 (aset string
(+ idx i
) (aref obj i
))
89 (defun truncate-string-to-width (str end-column
90 &optional start-column padding ellipsis
)
91 "Truncate string STR to end at column END-COLUMN.
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'.
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.
105 If PADDING is nil, no padding is added in these cases, so
106 the resulting string may be narrower than END-COLUMN.
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 \"...\"."
114 (setq start-column
0))
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))
123 (head-padding "") (tail-padding "")
124 ch last-column last-idx from-idx
)
126 (while (< column start-column
)
127 (setq ch
(aref str idx
)
128 column
(+ column
(char-width ch
))
130 (args-out-of-range (setq idx str-len
)))
131 (if (< column start-column
)
132 (if padding
(make-string end-column padding
) "")
133 (when (and padding
(> column start-column
))
134 (setq head-padding
(make-string (- column start-column
) padding
)))
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
))
142 (while (< column end-column
)
143 (setq last-column column
146 column
(+ column
(char-width ch
))
148 (args-out-of-range (setq idx str-len
)))
149 (when (> column end-column
)
150 (setq column last-column
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
))))
157 ;;; Test suite for truncate-string-to-width
158 ;; (dolist (test '((("" 0) . "")
161 ;; (("xy" 2 1) . "y")
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")
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))
206 ;; (format "error: %s: %s" (car ret)
207 ;; (prin1-to-string (cdr ret)))
208 ;; (prin1-to-string ret))))))
210 ;;; For backward compatibility ...
212 (defalias 'truncate-string
'truncate-string-to-width
)
215 (make-obsolete 'truncate-string
'truncate-string-to-width
"20.1")
217 ;;; Nested alist handler. Nested alist is alist whose elements are
218 ;;; also nested alist.
221 (defsubst nested-alist-p
(obj)
222 "Return t if OBJ is a nested alist.
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
226 \(KEY-ELEMENT . NESTED-ALIST).
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
))))
234 (defun set-nested-alist (keyseq entry alist
&optional len branches
)
235 "Set ENTRY for KEYSEQ in a nested alist ALIST.
236 Optional 4th arg LEN non-nil means the first LEN elements in KEYSEQ
238 Optional argument BRANCHES if non-nil is branches for a keyseq
240 See the documentation of `nested-alist-p' for more detail."
241 (or (nested-alist-p alist
)
242 (error "Invalid argument %s" alist
))
243 (let ((islist (listp keyseq
))
244 (len (or len
(length keyseq
)))
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
)))
254 (setq slot
(cons key-elt
(list t
)))
255 (setcdr alist
(cons slot
(cdr alist
)))))
256 (setq alist
(cdr slot
))
260 (setcdr (last alist
) branches
))))
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
)
275 (error "Invalid argument %s" alist
))
277 (setq len
(length keyseq
)))
278 (let ((i (or start
0)))
279 (if (catch 'lookup-nested-alist-tag
282 (if (setq alist
(cdr (assoc (nth i keyseq
) (cdr alist
))))
284 (throw 'lookup-nested-alist-tag t
))))
286 (if (setq alist
(cdr (assoc (aref keyseq i
) (cdr alist
))))
288 (throw 'lookup-nested-alist-tag t
))))
289 ;; KEYSEQ is too long.
290 (if nil-for-too-long nil i
)
294 ;; Coding system related functions.
297 (defun coding-system-eol-type-mnemonic (coding-system)
298 "Return the string indicating end-of-line format of CODING-SYSTEM."
299 (let* ((eol-type (coding-system-eol-type coding-system
))
300 (val (cond ((vectorp eol-type
) eol-mnemonic-undecided
)
301 ((eq eol-type
0) eol-mnemonic-unix
)
302 ((eq eol-type
1) eol-mnemonic-dos
)
303 ((eq eol-type
2) eol-mnemonic-mac
)
307 (char-to-string val
))))
310 (defun coding-system-post-read-conversion (coding-system)
311 "Return the value of CODING-SYSTEM's `post-read-conversion' property."
312 (coding-system-get coding-system
'post-read-conversion
))
315 (defun coding-system-pre-write-conversion (coding-system)
316 "Return the value of CODING-SYSTEM's `pre-write-conversion' property."
317 (coding-system-get coding-system
'pre-write-conversion
))
320 (defun coding-system-translation-table-for-decode (coding-system)
321 "Return the value of CODING-SYSTEM's `translation-table-for-decode' property."
322 (coding-system-get coding-system
'translation-table-for-decode
))
325 (defun coding-system-translation-table-for-encode (coding-system)
326 "Return the value of CODING-SYSTEM's `translation-table-for-encode' property."
327 (coding-system-get coding-system
'translation-table-for-encode
))
330 (defun coding-system-equal (coding-system-1 coding-system-2
)
331 "Return t if and only if CODING-SYSTEM-1 and CODING-SYSTEM-2 are identical.
332 Two coding systems are identical if two symbols are equal
333 or one is an alias of the other."
334 (or (eq coding-system-1 coding-system-2
)
335 (and (equal (coding-system-spec coding-system-1
)
336 (coding-system-spec coding-system-2
))
337 (let ((eol-type-1 (coding-system-eol-type coding-system-1
))
338 (eol-type-2 (coding-system-eol-type coding-system-2
)))
339 (or (eq eol-type-1 eol-type-2
)
340 (and (vectorp eol-type-1
) (vectorp eol-type-2
)))))))
343 (defmacro detect-coding-with-priority
(from to priority-list
)
344 "Detect a coding system of the text between FROM and TO with PRIORITY-LIST.
345 PRIORITY-LIST is an alist of coding categories vs the corresponding
346 coding systems ordered by priority."
348 (let* ((prio-list ,priority-list
)
349 (coding-category-list coding-category-list
)
350 ,@(mapcar (function (lambda (x) (list x x
)))
351 coding-category-list
))
352 (mapc (function (lambda (x) (set (car x
) (cdr x
))))
354 (set-coding-priority (mapcar #'car prio-list
))
355 (detect-coding-region ,from
,to
))
356 ;; We must restore the internal database.
357 (set-coding-priority coding-category-list
)
358 (update-coding-systems-internal)))
361 (defun detect-coding-with-language-environment (from to lang-env
)
362 "Detect a coding system of the text between FROM and TO with LANG-ENV.
363 The detection takes into account the coding system priorities for the
364 language environment LANG-ENV."
365 (let ((coding-priority (get-language-info lang-env
'coding-priority
)))
367 (detect-coding-with-priority
369 (mapcar (function (lambda (x)
370 (cons (coding-system-get x
'coding-category
) x
)))
372 (detect-coding-region from to
))))
378 ;; coding: iso-2022-7bit
381 ;;; mule-util.el ends here