Update copyright year to 2015
[emacs.git] / lisp / international / mule-util.el
blobeae787bbeb94b6ed58554d091a8c9062b11d0d81
1 ;;; mule-util.el --- utility functions for multilingual environment (mule)
3 ;; Copyright (C) 1997-1998, 2000-2015 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
8 ;; Copyright (C) 2003
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/>.
29 ;;; Commentary:
31 ;;; Code:
33 ;;; String manipulations while paying attention to multibyte
34 ;;; characters.
36 ;;;###autoload
37 (defsubst string-to-list (string)
38 "Return a list of characters in STRING."
39 (append string nil))
41 ;;;###autoload
42 (defsubst string-to-vector (string)
43 "Return a vector of characters in STRING."
44 (vconcat string))
46 ;;;###autoload
47 (defun store-substring (string idx obj)
48 "Embed OBJ (string or character) at index IDX of STRING."
49 (if (integerp obj)
50 (aset string idx obj)
51 (let ((len1 (length obj))
52 (len2 (length string))
53 (i 0))
54 (while (< i len1)
55 (aset string (+ idx i) (aref obj i))
56 (setq i (1+ i)))))
57 string)
59 (defvar truncate-string-ellipsis "..." ;"…"
60 "String to use to indicate truncation.")
62 ;;;###autoload
63 (defun truncate-string-to-width (str end-column
64 &optional start-column padding ellipsis)
65 "Truncate string STR to end at column END-COLUMN.
66 The optional 3rd arg START-COLUMN, if non-nil, specifies the starting
67 column; that means to return the characters occupying columns
68 START-COLUMN ... END-COLUMN of STR. Both END-COLUMN and START-COLUMN
69 are specified in terms of character display width in the current
70 buffer; see also `char-width'.
72 The optional 4th arg PADDING, if non-nil, specifies a padding
73 character (which should have a display width of 1) to add at the end
74 of the result if STR doesn't reach column END-COLUMN, or if END-COLUMN
75 comes in the middle of a character in STR. PADDING is also added at
76 the beginning of the result if column START-COLUMN appears in the
77 middle of a character in STR.
79 If PADDING is nil, no padding is added in these cases, so
80 the resulting string may be narrower than END-COLUMN.
82 If ELLIPSIS is non-nil, it should be a string which will replace the
83 end of STR (including any padding) if it extends beyond END-COLUMN,
84 unless the display width of STR is equal to or less than the display
85 width of ELLIPSIS. If it is non-nil and not a string, then ELLIPSIS
86 defaults to `truncate-string-ellipsis'."
87 (or start-column
88 (setq start-column 0))
89 (when (and ellipsis (not (stringp ellipsis)))
90 (setq ellipsis truncate-string-ellipsis))
91 (let ((str-len (length str))
92 (str-width (string-width str))
93 (ellipsis-len (if ellipsis (length ellipsis) 0))
94 (ellipsis-width (if ellipsis (string-width ellipsis) 0))
95 (idx 0)
96 (column 0)
97 (head-padding "") (tail-padding "")
98 ch last-column last-idx from-idx)
99 (condition-case nil
100 (while (< column start-column)
101 (setq ch (aref str idx)
102 column (+ column (char-width ch))
103 idx (1+ idx)))
104 (args-out-of-range (setq idx str-len)))
105 (if (< column start-column)
106 (if padding (make-string end-column padding) "")
107 (when (and padding (> column start-column))
108 (setq head-padding (make-string (- column start-column) padding)))
109 (setq from-idx idx)
110 (when (>= end-column column)
111 (if (and (< end-column str-width)
112 (> str-width ellipsis-width))
113 (setq end-column (- end-column ellipsis-width))
114 (setq ellipsis ""))
115 (condition-case nil
116 (while (< column end-column)
117 (setq last-column column
118 last-idx idx
119 ch (aref str idx)
120 column (+ column (char-width ch))
121 idx (1+ idx)))
122 (args-out-of-range (setq idx str-len)))
123 (when (> column end-column)
124 (setq column last-column
125 idx last-idx))
126 (when (and padding (< column end-column))
127 (setq tail-padding (make-string (- end-column column) padding))))
128 (concat head-padding (substring str from-idx idx)
129 tail-padding ellipsis))))
132 ;;; Nested alist handler. Nested alist is alist whose elements are
133 ;;; also nested alist.
135 ;;;###autoload
136 (defsubst nested-alist-p (obj)
137 "Return t if OBJ is a nested alist.
139 Nested alist is a list of the form (ENTRY . BRANCHES), where ENTRY is
140 any Lisp object, and BRANCHES is a list of cons cells of the form
141 \(KEY-ELEMENT . NESTED-ALIST).
143 You can use a nested alist to store any Lisp object (ENTRY) for a key
144 sequence KEYSEQ, where KEYSEQ is a sequence of KEY-ELEMENT. KEYSEQ
145 can be a string, a vector, or a list."
146 (and obj (listp obj) (listp (cdr obj))))
148 ;;;###autoload
149 (defun set-nested-alist (keyseq entry alist &optional len branches)
150 "Set ENTRY for KEYSEQ in a nested alist ALIST.
151 Optional 4th arg LEN non-nil means the first LEN elements in KEYSEQ
152 are considered.
153 Optional 5th argument BRANCHES if non-nil is branches for a keyseq
154 longer than KEYSEQ.
155 See the documentation of `nested-alist-p' for more detail."
156 (or (nested-alist-p alist)
157 (error "Invalid argument %s" alist))
158 (let ((islist (listp keyseq))
159 (len (or len (length keyseq)))
160 (i 0)
161 key-elt slot)
162 (while (< i len)
163 (if (null (nested-alist-p alist))
164 (error "Keyseq %s is too long for this nested alist" keyseq))
165 (setq key-elt (if islist (nth i keyseq) (aref keyseq i)))
166 (setq slot (assoc key-elt (cdr alist)))
167 (unless slot
168 (setq slot (cons key-elt (list t)))
169 (setcdr alist (cons slot (cdr alist))))
170 (setq alist (cdr slot))
171 (setq i (1+ i)))
172 (setcar alist entry)
173 (if branches
174 (setcdr (last alist) branches))))
176 ;;;###autoload
177 (defun lookup-nested-alist (keyseq alist &optional len start nil-for-too-long)
178 "Look up key sequence KEYSEQ in nested alist ALIST. Return the definition.
179 Optional 3rd argument LEN specifies the length of KEYSEQ.
180 Optional 4th argument START specifies index of the starting key.
181 The returned value is normally a nested alist of which
182 car part is the entry for KEYSEQ.
183 If ALIST is not deep enough for KEYSEQ, return number which is
184 how many key elements at the front of KEYSEQ it takes
185 to reach a leaf in ALIST.
186 Optional 5th argument NIL-FOR-TOO-LONG non-nil means return nil
187 even if ALIST is not deep enough."
188 (or (nested-alist-p alist)
189 (error "Invalid argument %s" alist))
190 (or len
191 (setq len (length keyseq)))
192 (let ((i (or start 0)))
193 (if (catch 'lookup-nested-alist-tag
194 (if (listp keyseq)
195 (while (< i len)
196 (if (setq alist (cdr (assoc (nth i keyseq) (cdr alist))))
197 (setq i (1+ i))
198 (throw 'lookup-nested-alist-tag t))))
199 (while (< i len)
200 (if (setq alist (cdr (assoc (aref keyseq i) (cdr alist))))
201 (setq i (1+ i))
202 (throw 'lookup-nested-alist-tag t))))
203 ;; KEYSEQ is too long.
204 (if nil-for-too-long nil i)
205 alist)))
208 ;; Coding system related functions.
210 ;;;###autoload
211 (defun coding-system-post-read-conversion (coding-system)
212 "Return the value of CODING-SYSTEM's `post-read-conversion' property."
213 (coding-system-get coding-system :post-read-conversion))
215 ;;;###autoload
216 (defun coding-system-pre-write-conversion (coding-system)
217 "Return the value of CODING-SYSTEM's `pre-write-conversion' property."
218 (coding-system-get coding-system :pre-write-conversion))
220 ;;;###autoload
221 (defun coding-system-translation-table-for-decode (coding-system)
222 "Return the value of CODING-SYSTEM's `decode-translation-table' property."
223 (coding-system-get coding-system :decode-translation-table))
225 ;;;###autoload
226 (defun coding-system-translation-table-for-encode (coding-system)
227 "Return the value of CODING-SYSTEM's `encode-translation-table' property."
228 (coding-system-get coding-system :encode-translation-table))
230 ;;;###autoload
231 (defmacro with-coding-priority (coding-systems &rest body)
232 "Execute BODY like `progn' with CODING-SYSTEMS at the front of priority list.
233 CODING-SYSTEMS is a list of coding systems. See `set-coding-system-priority'.
234 This affects the implicit sorting of lists of coding systems returned by
235 operations such as `find-coding-systems-region'."
236 (let ((current (make-symbol "current")))
237 `(let ((,current (coding-system-priority-list)))
238 (apply #'set-coding-system-priority ,coding-systems)
239 (unwind-protect
240 (progn ,@body)
241 (apply #'set-coding-system-priority ,current)))))
242 ;;;###autoload(put 'with-coding-priority 'lisp-indent-function 1)
243 (put 'with-coding-priority 'edebug-form-spec t)
245 ;;;###autoload
246 (defmacro detect-coding-with-priority (from to priority-list)
247 "Detect a coding system of the text between FROM and TO with PRIORITY-LIST.
248 PRIORITY-LIST is an alist of coding categories vs the corresponding
249 coding systems ordered by priority."
250 (declare (obsolete with-coding-priority "23.1"))
251 `(with-coding-priority (mapcar #'cdr ,priority-list)
252 (detect-coding-region ,from ,to)))
254 ;;;###autoload
255 (defun detect-coding-with-language-environment (from to lang-env)
256 "Detect a coding system for the text between FROM and TO with LANG-ENV.
257 The detection takes into account the coding system priorities for the
258 language environment LANG-ENV."
259 (let ((coding-priority (get-language-info lang-env 'coding-priority)))
260 (if coding-priority
261 (with-coding-priority coding-priority
262 (detect-coding-region from to)))))
264 (declare-function internal-char-font "fontset.c" (position &optional ch))
266 ;;;###autoload
267 (defun char-displayable-p (char)
268 "Return non-nil if we should be able to display CHAR.
269 On a multi-font display, the test is only whether there is an
270 appropriate font from the selected frame's fontset to display
271 CHAR's charset in general. Since fonts may be specified on a
272 per-character basis, this may not be accurate."
273 (cond ((< char 128)
274 ;; ASCII characters are always displayable.
276 ((not enable-multibyte-characters)
277 ;; Maybe there's a font for it, but we can't put it in the buffer.
278 nil)
279 ((display-multi-font-p)
280 ;; On a window system, a character is displayable if we have
281 ;; a font for that character in the default face of the
282 ;; currently selected frame.
283 (car (internal-char-font nil char)))
285 ;; On a terminal, a character is displayable if the coding
286 ;; system for the terminal can encode it.
287 (let ((coding (terminal-coding-system)))
288 (when coding
289 (let ((cs-list (coding-system-get coding :charset-list)))
290 (cond
291 ((listp cs-list)
292 (catch 'tag
293 (mapc #'(lambda (charset)
294 (if (encode-char char charset)
295 (throw 'tag charset)))
296 cs-list)
297 nil))
298 ((eq cs-list 'iso-2022)
299 (catch 'tag2
300 (mapc #'(lambda (charset)
301 (if (and (plist-get (charset-plist charset)
302 :iso-final-char)
303 (encode-char char charset))
304 (throw 'tag2 charset)))
305 charset-list)
306 nil))
307 ((eq cs-list 'emacs-mule)
308 (catch 'tag3
309 (mapc #'(lambda (charset)
310 (if (and (plist-get (charset-plist charset)
311 :emacs-mule-id)
312 (encode-char char charset))
313 (throw 'tag3 charset)))
314 charset-list)
315 nil)))))))))
317 (provide 'mule-util)
319 ;; Local Variables:
320 ;; coding: utf-8
321 ;; End:
323 ;;; mule-util.el ends here