1 ;;; mule-util.el --- utility functions for multilingual environment (mule)
3 ;; Copyright (C) 1997-1998, 2000-2014 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011
6 ;; National Institute of Advanced Industrial Science and Technology (AIST)
7 ;; Registration Number H14PRO021
9 ;; National Institute of Advanced Industrial Science and Technology (AIST)
10 ;; Registration Number H13PRO009
12 ;; Keywords: mule, multilingual
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software: you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
33 ;;; String manipulations while paying attention to multibyte
37 (defsubst string-to-list
(string)
38 "Return a list of characters in STRING."
42 (defsubst string-to-vector
(string)
43 "Return a vector of characters in STRING."
47 (defun store-substring (string idx obj
)
48 "Embed OBJ (string or character) at index IDX of STRING."
51 (let ((len1 (length obj
))
52 (len2 (length string
))
55 (aset string
(+ idx i
) (aref obj i
))
60 (defun truncate-string-to-width (str end-column
61 &optional start-column padding ellipsis
)
62 "Truncate string STR to end at column END-COLUMN.
63 The optional 3rd arg START-COLUMN, if non-nil, specifies the starting
64 column; that means to return the characters occupying columns
65 START-COLUMN ... END-COLUMN of STR. Both END-COLUMN and START-COLUMN
66 are specified in terms of character display width in the current
67 buffer; see also `char-width'.
69 The optional 4th arg PADDING, if non-nil, specifies a padding
70 character (which should have a display width of 1) to add at the end
71 of the result if STR doesn't reach column END-COLUMN, or if END-COLUMN
72 comes in the middle of a character in STR. PADDING is also added at
73 the beginning of the result if column START-COLUMN appears in the
74 middle of a character in STR.
76 If PADDING is nil, no padding is added in these cases, so
77 the resulting string may be narrower than END-COLUMN.
79 If ELLIPSIS is non-nil, it should be a string which will replace the
80 end of STR (including any padding) if it extends beyond END-COLUMN,
81 unless the display width of STR is equal to or less than the display
82 width of ELLIPSIS. If it is non-nil and not a string, then ELLIPSIS
85 (setq start-column
0))
86 (when (and ellipsis
(not (stringp ellipsis
)))
87 (setq ellipsis
"..."))
88 (let ((str-len (length str
))
89 (str-width (string-width str
))
90 (ellipsis-len (if ellipsis
(length ellipsis
) 0))
91 (ellipsis-width (if ellipsis
(string-width ellipsis
) 0))
94 (head-padding "") (tail-padding "")
95 ch last-column last-idx from-idx
)
97 (while (< column start-column
)
98 (setq ch
(aref str idx
)
99 column
(+ column
(char-width ch
))
101 (args-out-of-range (setq idx str-len
)))
102 (if (< column start-column
)
103 (if padding
(make-string end-column padding
) "")
104 (when (and padding
(> column start-column
))
105 (setq head-padding
(make-string (- column start-column
) padding
)))
107 (when (>= end-column column
)
108 (if (and (< end-column str-width
)
109 (> str-width ellipsis-width
))
110 (setq end-column
(- end-column ellipsis-width
))
113 (while (< column end-column
)
114 (setq last-column column
117 column
(+ column
(char-width ch
))
119 (args-out-of-range (setq idx str-len
)))
120 (when (> column end-column
)
121 (setq column last-column
123 (when (and padding
(< column end-column
))
124 (setq tail-padding
(make-string (- end-column column
) padding
))))
125 (concat head-padding
(substring str from-idx idx
)
126 tail-padding ellipsis
))))
129 ;;; Nested alist handler. Nested alist is alist whose elements are
130 ;;; also nested alist.
133 (defsubst nested-alist-p
(obj)
134 "Return t if OBJ is a nested alist.
136 Nested alist is a list of the form (ENTRY . BRANCHES), where ENTRY is
137 any Lisp object, and BRANCHES is a list of cons cells of the form
138 \(KEY-ELEMENT . NESTED-ALIST).
140 You can use a nested alist to store any Lisp object (ENTRY) for a key
141 sequence KEYSEQ, where KEYSEQ is a sequence of KEY-ELEMENT. KEYSEQ
142 can be a string, a vector, or a list."
143 (and obj
(listp obj
) (listp (cdr obj
))))
146 (defun set-nested-alist (keyseq entry alist
&optional len branches
)
147 "Set ENTRY for KEYSEQ in a nested alist ALIST.
148 Optional 4th arg LEN non-nil means the first LEN elements in KEYSEQ
150 Optional 5th argument BRANCHES if non-nil is branches for a keyseq
152 See the documentation of `nested-alist-p' for more detail."
153 (or (nested-alist-p alist
)
154 (error "Invalid argument %s" alist
))
155 (let ((islist (listp keyseq
))
156 (len (or len
(length keyseq
)))
160 (if (null (nested-alist-p alist
))
161 (error "Keyseq %s is too long for this nested alist" keyseq
))
162 (setq key-elt
(if islist
(nth i keyseq
) (aref keyseq i
)))
163 (setq slot
(assoc key-elt
(cdr alist
)))
165 (setq slot
(cons key-elt
(list t
)))
166 (setcdr alist
(cons slot
(cdr alist
))))
167 (setq alist
(cdr slot
))
171 (setcdr (last alist
) branches
))))
174 (defun lookup-nested-alist (keyseq alist
&optional len start nil-for-too-long
)
175 "Look up key sequence KEYSEQ in nested alist ALIST. Return the definition.
176 Optional 3rd argument LEN specifies the length of KEYSEQ.
177 Optional 4th argument START specifies index of the starting key.
178 The returned value is normally a nested alist of which
179 car part is the entry for KEYSEQ.
180 If ALIST is not deep enough for KEYSEQ, return number which is
181 how many key elements at the front of KEYSEQ it takes
182 to reach a leaf in ALIST.
183 Optional 5th argument NIL-FOR-TOO-LONG non-nil means return nil
184 even if ALIST is not deep enough."
185 (or (nested-alist-p alist
)
186 (error "Invalid argument %s" alist
))
188 (setq len
(length keyseq
)))
189 (let ((i (or start
0)))
190 (if (catch 'lookup-nested-alist-tag
193 (if (setq alist
(cdr (assoc (nth i keyseq
) (cdr alist
))))
195 (throw 'lookup-nested-alist-tag t
))))
197 (if (setq alist
(cdr (assoc (aref keyseq i
) (cdr alist
))))
199 (throw 'lookup-nested-alist-tag t
))))
200 ;; KEYSEQ is too long.
201 (if nil-for-too-long nil i
)
205 ;; Coding system related functions.
208 (defun coding-system-post-read-conversion (coding-system)
209 "Return the value of CODING-SYSTEM's `post-read-conversion' property."
210 (coding-system-get coding-system
:post-read-conversion
))
213 (defun coding-system-pre-write-conversion (coding-system)
214 "Return the value of CODING-SYSTEM's `pre-write-conversion' property."
215 (coding-system-get coding-system
:pre-write-conversion
))
218 (defun coding-system-translation-table-for-decode (coding-system)
219 "Return the value of CODING-SYSTEM's `decode-translation-table' property."
220 (coding-system-get coding-system
:decode-translation-table
))
223 (defun coding-system-translation-table-for-encode (coding-system)
224 "Return the value of CODING-SYSTEM's `encode-translation-table' property."
225 (coding-system-get coding-system
:encode-translation-table
))
228 (defmacro with-coding-priority
(coding-systems &rest body
)
229 "Execute BODY like `progn' with CODING-SYSTEMS at the front of priority list.
230 CODING-SYSTEMS is a list of coding systems. See `set-coding-system-priority'.
231 This affects the implicit sorting of lists of coding systems returned by
232 operations such as `find-coding-systems-region'."
233 (let ((current (make-symbol "current")))
234 `(let ((,current
(coding-system-priority-list)))
235 (apply #'set-coding-system-priority
,coding-systems
)
238 (apply #'set-coding-system-priority
,current
)))))
239 ;;;###autoload(put 'with-coding-priority 'lisp-indent-function 1)
240 (put 'with-coding-priority
'edebug-form-spec t
)
243 (defmacro detect-coding-with-priority
(from to priority-list
)
244 "Detect a coding system of the text between FROM and TO with PRIORITY-LIST.
245 PRIORITY-LIST is an alist of coding categories vs the corresponding
246 coding systems ordered by priority."
247 (declare (obsolete with-coding-priority
"23.1"))
248 `(with-coding-priority (mapcar #'cdr
,priority-list
)
249 (detect-coding-region ,from
,to
)))
252 (defun detect-coding-with-language-environment (from to lang-env
)
253 "Detect a coding system for the text between FROM and TO with LANG-ENV.
254 The detection takes into account the coding system priorities for the
255 language environment LANG-ENV."
256 (let ((coding-priority (get-language-info lang-env
'coding-priority
)))
258 (with-coding-priority coding-priority
259 (detect-coding-region from to
)))))
261 (declare-function internal-char-font
"fontset.c" (position &optional ch
))
264 (defun char-displayable-p (char)
265 "Return non-nil if we should be able to display CHAR.
266 On a multi-font display, the test is only whether there is an
267 appropriate font from the selected frame's fontset to display
268 CHAR's charset in general. Since fonts may be specified on a
269 per-character basis, this may not be accurate."
271 ;; ASCII characters are always displayable.
273 ((not enable-multibyte-characters
)
274 ;; Maybe there's a font for it, but we can't put it in the buffer.
276 ((display-multi-font-p)
277 ;; On a window system, a character is displayable if we have
278 ;; a font for that character in the default face of the
279 ;; currently selected frame.
280 (car (internal-char-font nil char
)))
282 ;; On a terminal, a character is displayable if the coding
283 ;; system for the terminal can encode it.
284 (let ((coding (terminal-coding-system)))
286 (let ((cs-list (coding-system-get coding
:charset-list
)))
290 (mapc #'(lambda (charset)
291 (if (encode-char char charset
)
292 (throw 'tag charset
)))
295 ((eq cs-list
'iso-2022
)
297 (mapc #'(lambda (charset)
298 (if (and (plist-get (charset-plist charset
)
300 (encode-char char charset
))
301 (throw 'tag2 charset
)))
304 ((eq cs-list
'emacs-mule
)
306 (mapc #'(lambda (charset)
307 (if (and (plist-get (charset-plist charset
)
309 (encode-char char charset
))
310 (throw 'tag3 charset
)))
320 ;;; mule-util.el ends here