1 ;;; mule-util.el --- utility functions for mulitilingual environment (mule)
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5 ;; Copyright (C) 2000, 2002 Free Software Foundation, Inc.
7 ;; Keywords: mule, multilingual
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
30 ;;; String manipulations while paying attention to multibyte
34 (defun string-to-sequence (string type
)
35 "Convert STRING to a sequence of TYPE which contains characters in STRING.
36 TYPE should be `list' or `vector'."
37 ;;; (let ((len (length string))
40 (cond ((eq type
'list
)
41 ;; Applicable post-Emacs 20.2 and asymptotically ~10 times
42 ;; faster than the code below:
44 ;;; (setq val (make-list len 0))
47 ;;; (setcar l (aref string i))
48 ;;; (setq l (cdr l) i (1+ i))))))
52 ;;; (setq val (make-vector len 0))
54 ;;; (aset val i (aref string i))
57 (error "Invalid type: %s" type
)))
62 (make-obsolete 'string-to-sequence
63 "use `string-to-list' or `string-to-vector'."
67 (defsubst string-to-list
(string)
68 "Return a list of characters in STRING."
72 (defsubst string-to-vector
(string)
73 "Return a vector of characters in STRING."
77 (defun store-substring (string idx obj
)
78 "Embed OBJ (string or character) at index IDX of STRING."
81 (let ((len1 (length obj
))
82 (len2 (length string
))
85 (aset string
(+ idx i
) (aref obj i
))
90 (defun truncate-string-to-width (str end-column
91 &optional start-column padding ellipsis
)
92 "Truncate string STR to end at column END-COLUMN.
93 The optional 3rd arg START-COLUMN, if non-nil, specifies the starting
94 column; that means to return the characters occupying columns
95 START-COLUMN ... END-COLUMN of STR. Both END-COLUMN and START-COLUMN
96 are specified in terms of character display width in the current
97 buffer; see also `char-width'.
99 The optional 4th arg PADDING, if non-nil, specifies a padding
100 character (which should have a display width of 1) to add at the end
101 of the result if STR doesn't reach column END-COLUMN, or if END-COLUMN
102 comes in the middle of a character in STR. PADDING is also added at
103 the beginning of the result if column START-COLUMN appears in the
104 middle of a character in STR.
106 If PADDING is nil, no padding is added in these cases, so
107 the resulting string may be narrower than END-COLUMN.
109 If ELLIPSIS is non-nil, it should be a string which will replace the
110 end of STR (including any padding) if it extends beyond END-COLUMN,
111 unless the display width of STR is equal to or less than the display
112 width of ELLIPSIS. If it is non-nil and not a string, then ELLIPSIS
113 defaults to \"...\"."
115 (setq start-column
0))
116 (when (and ellipsis
(not (stringp ellipsis
)))
117 (setq ellipsis
"..."))
118 (let ((str-len (length str
))
119 (str-width (string-width str
))
120 (ellipsis-len (if ellipsis
(length ellipsis
) 0))
121 (ellipsis-width (if ellipsis
(string-width ellipsis
) 0))
124 (head-padding "") (tail-padding "")
125 ch last-column last-idx from-idx
)
127 (while (< column start-column
)
128 (setq ch
(aref str idx
)
129 column
(+ column
(char-width ch
))
131 (args-out-of-range (setq idx str-len
)))
132 (if (< column start-column
)
133 (if padding
(make-string end-column padding
) "")
134 (when (and padding
(> column start-column
))
135 (setq head-padding
(make-string (- column start-column
) padding
)))
137 (when (>= end-column column
)
138 (if (and (< end-column str-width
)
139 (> str-width ellipsis-width
))
140 (setq end-column
(- end-column ellipsis-width
))
143 (while (< column end-column
)
144 (setq last-column column
147 column
(+ column
(char-width ch
))
149 (args-out-of-range (setq idx str-len
)))
150 (when (> column end-column
)
151 (setq column last-column
153 (when (and padding
(< column end-column
))
154 (setq tail-padding
(make-string (- end-column column
) padding
))))
155 (concat head-padding
(substring str from-idx idx
)
156 tail-padding ellipsis
))))
158 ;;; Test suite for truncate-string-to-width
159 ;; (dolist (test '((("" 0) . "")
162 ;; (("xy" 2 1) . "y")
165 ;; (("\e$AVP\e(B" 0) . "")
166 ;; (("\e$AVP\e(B" 1) . "")
167 ;; (("\e$AVP\e(B" 2) . "\e$AVP\e(B")
168 ;; (("\e$AVP\e(B" 1 nil ? ) . " ")
169 ;; (("\e$AVPND\e(B" 3 1 ? ) . " ")
170 ;; (("x\e$AVP\e(Bx" 2) . "x")
171 ;; (("x\e$AVP\e(Bx" 3) . "x\e$AVP\e(B")
172 ;; (("x\e$AVP\e(Bx" 3) . "x\e$AVP\e(B")
173 ;; (("x\e$AVP\e(Bx" 4 1) . "\e$AVP\e(Bx")
174 ;; (("kor\e$(CGQ\e(Be\e$(C1[\e(Ban" 8 1 ? ) . "or\e$(CGQ\e(Be\e$(C1[\e(B")
175 ;; (("kor\e$(CGQ\e(Be\e$(C1[\e(Ban" 7 2 ? ) . "r\e$(CGQ\e(Be ")
176 ;; (("" 0 nil nil "...") . "")
177 ;; (("x" 3 nil nil "...") . "x")
178 ;; (("\e$AVP\e(B" 3 nil nil "...") . "\e$AVP\e(B")
179 ;; (("foo" 3 nil nil "...") . "foo")
180 ;; (("foo" 2 nil nil "...") . "fo") ;; XEmacs failure?
181 ;; (("foobar" 6 0 nil "...") . "foobar")
182 ;; (("foobarbaz" 6 nil nil "...") . "foo...")
183 ;; (("foobarbaz" 7 2 nil "...") . "ob...")
184 ;; (("foobarbaz" 9 3 nil "...") . "barbaz")
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" 15 1 ? t) . " h\e$B$s\e(Be\e$B$K\e(Bl\e$B$A\e(Bl\e$B$O\e(Bo")
186 ;; (("\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...")
187 ;; (("x" 3 nil nil "\e$(0GnM$\e(B") . "x")
188 ;; (("\e$AVP\e(B" 2 nil nil "\e$(0GnM$\e(B") . "\e$AVP\e(B")
189 ;; (("\e$AVP\e(B" 1 nil ?x "\e$(0GnM$\e(B") . "x") ;; XEmacs error
190 ;; (("\e$AVPND\e(B" 3 nil ? "\e$(0GnM$\e(B") . "\e$AVP\e(B ") ;; XEmacs error
191 ;; (("foobarbaz" 4 nil nil "\e$(0GnM$\e(B") . "\e$(0GnM$\e(B")
192 ;; (("foobarbaz" 5 nil nil "\e$(0GnM$\e(B") . "f\e$(0GnM$\e(B")
193 ;; (("foobarbaz" 6 nil nil "\e$(0GnM$\e(B") . "fo\e$(0GnM$\e(B")
194 ;; (("foobarbaz" 8 3 nil "\e$(0GnM$\e(B") . "b\e$(0GnM$\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" 14 4 ?x "\e$BF|K\8l\e(B") . "xe\e$B$KF|K\8l\e(B")
196 ;; (("\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")
200 ;; (setq ret (apply #'truncate-string-to-width (car test)))
201 ;; (error (setq ret e)))
202 ;; (unless (equal ret (cdr test))
203 ;; (error "%s: expected %s, got %s"
204 ;; (prin1-to-string (cons 'truncate-string-to-width (car test)))
205 ;; (prin1-to-string (cdr test))
207 ;; (format "error: %s: %s" (car ret)
208 ;; (prin1-to-string (cdr ret)))
209 ;; (prin1-to-string ret))))))
211 ;;; For backward compatibility ...
213 (defalias 'truncate-string
'truncate-string-to-width
)
216 (make-obsolete 'truncate-string
'truncate-string-to-width
"20.1")
218 ;;; Nested alist handler. Nested alist is alist whose elements are
219 ;;; also nested alist.
222 (defsubst nested-alist-p
(obj)
223 "Return t if OBJ is a nested alist.
225 Nested alist is a list of the form (ENTRY . BRANCHES), where ENTRY is
226 any Lisp object, and BRANCHES is a list of cons cells of the form
227 \(KEY-ELEMENT . NESTED-ALIST).
229 You can use a nested alist to store any Lisp object (ENTRY) for a key
230 sequence KEYSEQ, where KEYSEQ is a sequence of KEY-ELEMENT. KEYSEQ
231 can be a string, a vector, or a list."
232 (and obj
(listp obj
) (listp (cdr obj
))))
235 (defun set-nested-alist (keyseq entry alist
&optional len branches
)
236 "Set ENTRY for KEYSEQ in a nested alist ALIST.
237 Optional 4th arg LEN non-nil means the first LEN elements in KEYSEQ
239 Optional argument BRANCHES if non-nil is branches for a keyseq
241 See the documentation of `nested-alist-p' for more detail."
242 (or (nested-alist-p alist
)
243 (error "Invalid argument %s" alist
))
244 (let ((islist (listp keyseq
))
245 (len (or len
(length keyseq
)))
249 (if (null (nested-alist-p alist
))
250 (error "Keyseq %s is too long for this nested alist" keyseq
))
251 (setq key-elt
(if islist
(nth i keyseq
) (aref keyseq i
)))
252 (setq slot
(assoc key-elt
(cdr alist
)))
255 (setq slot
(cons key-elt
(list t
)))
256 (setcdr alist
(cons slot
(cdr alist
)))))
257 (setq alist
(cdr slot
))
261 (setcdr (last alist
) branches
))))
264 (defun lookup-nested-alist (keyseq alist
&optional len start nil-for-too-long
)
265 "Look up key sequence KEYSEQ in nested alist ALIST. Return the definition.
266 Optional 1st argument LEN specifies the length of KEYSEQ.
267 Optional 2nd argument START specifies index of the starting key.
268 The returned value is normally a nested alist of which
269 car part is the entry for KEYSEQ.
270 If ALIST is not deep enough for KEYSEQ, return number which is
271 how many key elements at the front of KEYSEQ it takes
272 to reach a leaf in ALIST.
273 Optional 3rd argument NIL-FOR-TOO-LONG non-nil means return nil
274 even if ALIST is not deep enough."
275 (or (nested-alist-p alist
)
276 (error "Invalid argument %s" alist
))
278 (setq len
(length keyseq
)))
279 (let ((i (or start
0)))
280 (if (catch 'lookup-nested-alist-tag
283 (if (setq alist
(cdr (assoc (nth i keyseq
) (cdr alist
))))
285 (throw 'lookup-nested-alist-tag t
))))
287 (if (setq alist
(cdr (assoc (aref keyseq i
) (cdr alist
))))
289 (throw 'lookup-nested-alist-tag t
))))
290 ;; KEYSEQ is too long.
291 (if nil-for-too-long nil i
)
295 ;; Coding system related functions.
298 (defun coding-system-post-read-conversion (coding-system)
299 "Return the value of CODING-SYSTEM's `post-read-conversion' property."
300 (coding-system-get coding-system
'post-read-conversion
))
303 (defun coding-system-pre-write-conversion (coding-system)
304 "Return the value of CODING-SYSTEM's `pre-write-conversion' property."
305 (coding-system-get coding-system
'pre-write-conversion
))
308 (defun coding-system-translation-table-for-decode (coding-system)
309 "Return the value of CODING-SYSTEM's `translation-table-for-decode' property."
310 (coding-system-get coding-system
'translation-table-for-decode
))
313 (defun coding-system-translation-table-for-encode (coding-system)
314 "Return the value of CODING-SYSTEM's `translation-table-for-encode' property."
315 (coding-system-get coding-system
'translation-table-for-encode
))
318 (defun coding-system-equal (coding-system-1 coding-system-2
)
319 "Return t if and only if CODING-SYSTEM-1 and CODING-SYSTEM-2 are identical.
320 Two coding systems are identical if two symbols are equal
321 or one is an alias of the other."
322 (or (eq coding-system-1 coding-system-2
)
323 (and (equal (coding-system-spec coding-system-1
)
324 (coding-system-spec coding-system-2
))
325 (let ((eol-type-1 (coding-system-eol-type coding-system-1
))
326 (eol-type-2 (coding-system-eol-type coding-system-2
)))
327 (or (eq eol-type-1 eol-type-2
)
328 (and (vectorp eol-type-1
) (vectorp eol-type-2
)))))))
331 (defmacro detect-coding-with-priority
(from to priority-list
)
332 "Detect a coding system of the text between FROM and TO with PRIORITY-LIST.
333 PRIORITY-LIST is an alist of coding categories vs the corresponding
334 coding systems ordered by priority."
336 (let* ((prio-list ,priority-list
)
337 (coding-category-list coding-category-list
)
338 ,@(mapcar (function (lambda (x) (list x x
)))
339 coding-category-list
))
340 (mapc (function (lambda (x) (set (car x
) (cdr x
))))
342 (set-coding-priority (mapcar #'car prio-list
))
343 (detect-coding-region ,from
,to
))
344 ;; We must restore the internal database.
345 (set-coding-priority coding-category-list
)
346 (update-coding-systems-internal)))
349 (defun detect-coding-with-language-environment (from to lang-env
)
350 "Detect a coding system of the text between FROM and TO with LANG-ENV.
351 The detection takes into account the coding system priorities for the
352 language environment LANG-ENV."
353 (let ((coding-priority (get-language-info lang-env
'coding-priority
)))
355 (detect-coding-with-priority
357 (mapcar (function (lambda (x)
358 (cons (coding-system-get x
'coding-category
) x
)))
360 (detect-coding-region from to
))))
363 (defun char-displayable-p (char)
364 "Return non-nil if we should be able to display CHAR.
365 On a multi-font display, the test is only whether there is an
366 appropriate font from the selected frame's fontset to display CHAR's
367 charset in general. Since fonts may be specified on a per-character
368 basis, this may not be accurate."
370 ;; Single byte characters are always displayable.
372 ((display-multi-font-p)
373 ;; On a window system, a character is displayable if we have
374 ;; a font for that character in the default face of the
375 ;; currently selected frame.
376 (let ((fontset (frame-parameter (selected-frame) 'font
))
378 (if (query-fontset fontset
)
379 (setq font-pattern
(fontset-font fontset char
)))
381 (setq font-pattern
(fontset-font "fontset-default" char
)))
384 ;; Now FONT-PATTERN is a string or a cons of family
385 ;; field pattern and registry field pattern.
386 (or (stringp font-pattern
)
387 (setq font-pattern
(concat "-"
388 (or (car font-pattern
) "*")
390 (cdr font-pattern
))))
391 (x-list-fonts font-pattern
'default
(selected-frame) 1)))))
393 (let ((coding (terminal-coding-system)))
395 (let ((safe-chars (coding-system-get coding
'safe-chars
))
396 (safe-charsets (coding-system-get coding
'safe-charsets
)))
398 (aref safe-chars char
))
400 (memq (char-charset char
) safe-charsets
)))))))))
405 ;; coding: iso-2022-7bit
408 ;;; arch-tag: 5bdb52b6-a3a5-4529-b7a0-37d01b0e570b
409 ;;; mule-util.el ends here