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 (defsubst string-to-list
(string)
62 "Return a list of characters in STRING."
63 (string-to-sequence string
'list
))
66 (defsubst string-to-vector
(string)
67 "Return a vector of characters in STRING."
68 (string-to-sequence string
'vector
))
71 (defun store-substring (string idx obj
)
72 "Embed OBJ (string or character) at index IDX of STRING."
75 (let ((len1 (length obj
))
76 (len2 (length string
))
79 (aset string
(+ idx i
) (aref obj i
))
84 (defun truncate-string-to-width (str end-column
&optional start-column padding
)
85 "Truncate string STR to end at column END-COLUMN.
86 The optional 3rd arg START-COLUMN, if non-nil, specifies
87 the starting column; that means to return the characters occupying
88 columns START-COLUMN ... END-COLUMN of STR.
90 The optional 4th arg PADDING, if non-nil, specifies a padding character
91 to add at the end of the result if STR doesn't reach column END-COLUMN,
92 or if END-COLUMN comes in the middle of a character in STR.
93 PADDING is also added at the beginning of the result
94 if column START-COLUMN appears in the middle of a character in STR.
96 If PADDING is nil, no padding is added in these cases, so
97 the resulting string may be narrower than END-COLUMN."
99 (setq start-column
0))
100 (let ((len (length str
))
103 (head-padding "") (tail-padding "")
104 ch last-column last-idx from-idx
)
106 (while (< column start-column
)
107 (setq ch
(aref str idx
)
108 column
(+ column
(char-width ch
))
110 (args-out-of-range (setq idx len
)))
111 (if (< column start-column
)
112 (if padding
(make-string end-column padding
) "")
113 (if (and padding
(> column start-column
))
114 (setq head-padding
(make-string (- column start-column
) padding
)))
116 (if (< end-column column
)
119 (while (< column end-column
)
120 (setq last-column column
123 column
(+ column
(char-width ch
))
125 (args-out-of-range (setq idx len
)))
126 (if (> column end-column
)
127 (setq column last-column idx last-idx
))
128 (if (and padding
(< column end-column
))
129 (setq tail-padding
(make-string (- end-column column
) padding
))))
130 (setq str
(substring str from-idx idx
))
132 (concat head-padding str tail-padding
)
135 ;;; For backward compatibility ...
137 (defalias 'truncate-string
'truncate-string-to-width
)
138 (make-obsolete 'truncate-string
'truncate-string-to-width
"20.1")
140 ;;; Nested alist handler. Nested alist is alist whose elements are
141 ;;; also nested alist.
144 (defsubst nested-alist-p
(obj)
145 "Return t if OBJ is a nested alist.
147 Nested alist is a list of the form (ENTRY . BRANCHES), where ENTRY is
148 any Lisp object, and BRANCHES is a list of cons cells of the form
149 (KEY-ELEMENT . NESTED-ALIST).
151 You can use a nested alist to store any Lisp object (ENTRY) for a key
152 sequence KEYSEQ, where KEYSEQ is a sequence of KEY-ELEMENT. KEYSEQ
153 can be a string, a vector, or a list."
154 (and obj
(listp obj
) (listp (cdr obj
))))
157 (defun set-nested-alist (keyseq entry alist
&optional len branches
)
158 "Set ENTRY for KEYSEQ in a nested alist ALIST.
159 Optional 4th arg LEN non-nil means the first LEN elements in KEYSEQ
161 Optional argument BRANCHES if non-nil is branches for a keyseq
163 See the documentation of `nested-alist-p' for more detail."
164 (or (nested-alist-p alist
)
165 (error "Invalid argument %s" alist
))
166 (let ((islist (listp keyseq
))
167 (len (or len
(length keyseq
)))
171 (if (null (nested-alist-p alist
))
172 (error "Keyseq %s is too long for this nested alist" keyseq
))
173 (setq key-elt
(if islist
(nth i keyseq
) (aref keyseq i
)))
174 (setq slot
(assoc key-elt
(cdr alist
)))
177 (setq slot
(cons key-elt
(list t
)))
178 (setcdr alist
(cons slot
(cdr alist
)))))
179 (setq alist
(cdr slot
))
183 (setcdr (last alist
) branches
))))
186 (defun lookup-nested-alist (keyseq alist
&optional len start nil-for-too-long
)
187 "Look up key sequence KEYSEQ in nested alist ALIST. Return the definition.
188 Optional 1st argument LEN specifies the length of KEYSEQ.
189 Optional 2nd argument START specifies index of the starting key.
190 The returned value is normally a nested alist of which
191 car part is the entry for KEYSEQ.
192 If ALIST is not deep enough for KEYSEQ, return number which is
193 how many key elements at the front of KEYSEQ it takes
194 to reach a leaf in ALIST.
195 Optional 3rd argument NIL-FOR-TOO-LONG non-nil means return nil
196 even if ALIST is not deep enough."
197 (or (nested-alist-p alist
)
198 (error "Invalid argument %s" alist
))
200 (setq len
(length keyseq
)))
201 (let ((i (or start
0)))
202 (if (catch 'lookup-nested-alist-tag
205 (if (setq alist
(cdr (assoc (nth i keyseq
) (cdr alist
))))
207 (throw 'lookup-nested-alist-tag t
))))
209 (if (setq alist
(cdr (assoc (aref keyseq i
) (cdr alist
))))
211 (throw 'lookup-nested-alist-tag t
))))
212 ;; KEYSEQ is too long.
213 (if nil-for-too-long nil i
)
217 ;; Coding system related functions.
220 (defun coding-system-eol-type-mnemonic (coding-system)
221 "Return the string indicating end-of-line format of CODING-SYSTEM."
222 (let* ((eol-type (coding-system-eol-type coding-system
))
223 (val (cond ((vectorp eol-type
) eol-mnemonic-undecided
)
224 ((eq eol-type
0) eol-mnemonic-unix
)
225 ((eq eol-type
1) eol-mnemonic-dos
)
226 ((eq eol-type
2) eol-mnemonic-mac
)
230 (char-to-string val
))))
233 (defun coding-system-post-read-conversion (coding-system)
234 "Return the value of CODING-SYSTEM's post-read-conversion property."
235 (coding-system-get coding-system
'post-read-conversion
))
238 (defun coding-system-pre-write-conversion (coding-system)
239 "Return the value of CODING-SYSTEM's pre-write-conversion property."
240 (coding-system-get coding-system
'pre-write-conversion
))
243 (defun coding-system-translation-table-for-decode (coding-system)
244 "Return the value of CODING-SYSTEM's translation-table-for-decode property."
245 (coding-system-get coding-system
'translation-table-for-decode
))
248 (defun coding-system-translation-table-for-encode (coding-system)
249 "Return the value of CODING-SYSTEM's translation-table-for-encode property."
250 (coding-system-get coding-system
'translation-table-for-encode
))
253 (defun coding-system-equal (coding-system-1 coding-system-2
)
254 "Return t if and only if CODING-SYSTEM-1 and CODING-SYSTEM-2 are identical.
255 Two coding systems are identical if two symbols are equal
256 or one is an alias of the other."
257 (or (eq coding-system-1 coding-system-2
)
258 (and (equal (coding-system-spec coding-system-1
)
259 (coding-system-spec coding-system-2
))
260 (let ((eol-type-1 (coding-system-eol-type coding-system-1
))
261 (eol-type-2 (coding-system-eol-type coding-system-2
)))
262 (or (eq eol-type-1 eol-type-2
)
263 (and (vectorp eol-type-1
) (vectorp eol-type-2
)))))))
266 (defmacro detect-coding-with-priority
(from to priority-list
)
267 "Detect a coding system of the text between FROM and TO with PRIORITY-LIST.
268 PRIORITY-LIST is an alist of coding categories vs the corresponding
269 coding systems ordered by priority."
271 (let* ((prio-list ,priority-list
)
272 (coding-category-list coding-category-list
)
273 ,@(mapcar (function (lambda (x) (list x x
)))
274 coding-category-list
))
275 (mapc (function (lambda (x) (set (car x
) (cdr x
))))
277 (set-coding-priority (mapcar #'car prio-list
))
278 (detect-coding-region ,from
,to
))
279 ;; We must restore the internal database.
280 (set-coding-priority coding-category-list
)
281 (update-coding-systems-internal)))
284 (defun detect-coding-with-language-environment (from to lang-env
)
285 "Detect a coding system of the text between FROM and TO with LANG-ENV.
286 The detection takes into account the coding system priorities for the
287 language environment LANG-ENV."
288 (let ((coding-priority (get-language-info lang-env
'coding-priority
)))
290 (detect-coding-with-priority
292 (mapcar (function (lambda (x)
293 (cons (coding-system-get x
'coding-category
) x
)))
295 (detect-coding-region from to
))))
300 ;;; mule-util.el ends here