]> code.delx.au - gnu-emacs/blob - lisp/international/mule-util.el
(find-new-buffer-file-coding-system): Doc fix.
[gnu-emacs] / lisp / international / mule-util.el
1 ;;; mule-util.el --- Utility functions for mulitilingual environment (mule)
2
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5
6 ;; Keywords: mule, multilingual
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Code:
26
27 ;;; String manipulations while paying attention to multibyte
28 ;;; characters.
29
30 ;;;###autoload
31 (defun string-to-sequence (string type)
32 "Convert STRING to a sequence of TYPE which contains characters in STRING.
33 TYPE should be `list' or `vector'."
34 (let ((len (length string))
35 (i 0)
36 val)
37 (cond ((eq type 'list)
38 (setq val (make-list len 0))
39 (let ((l val))
40 (while (< i len)
41 (setcar l (aref string i))
42 (setq l (cdr l) i (1+ i)))))
43 ((eq type 'vector)
44 (setq val (make-vector len 0))
45 (while (< i len)
46 (aset val i (aref string i))
47 (setq i (1+ i))))
48 (t
49 (error "Invalid type: %s" type)))
50 val))
51
52 ;;;###autoload
53 (defsubst string-to-list (string)
54 "Return a list of characters in STRING."
55 (string-to-sequence string 'list))
56
57 ;;;###autoload
58 (defsubst string-to-vector (string)
59 "Return a vector of characters in STRING."
60 (string-to-sequence string 'vector))
61
62 ;;;###autoload
63 (defun store-substring (string idx obj)
64 "Embed OBJ (string or character) at index IDX of STRING."
65 (if (integerp obj)
66 (aset string idx obj)
67 (let ((len1 (length obj))
68 (len2 (length string))
69 (i 0))
70 (while (< i len1)
71 (aset string (+ idx i) (aref obj i))
72 (setq i (1+ i)))))
73 string)
74
75 ;;;###autoload
76 (defun truncate-string-to-width (str end-column &optional start-column padding)
77 "Truncate string STR to end at column END-COLUMN.
78 The optional 2nd arg START-COLUMN, if non-nil, specifies
79 the starting column; that means to return the characters occupying
80 columns START-COLUMN ... END-COLUMN of STR.
81
82 The optional 3rd arg PADDING, if non-nil, specifies a padding character
83 to add at the end of the result if STR doesn't reach column END-COLUMN,
84 or if END-COLUMN comes in the middle of a character in STR.
85 PADDING is also added at the beginning of the result
86 if column START-COLUMN appears in the middle of a character in STR.
87
88 If PADDING is nil, no padding is added in these cases, so
89 the resulting string may be narrower than END-COLUMN."
90 (or start-column
91 (setq start-column 0))
92 (let ((len (length str))
93 (idx 0)
94 (column 0)
95 (head-padding "") (tail-padding "")
96 ch last-column last-idx from-idx)
97 (condition-case nil
98 (while (< column start-column)
99 (setq ch (aref str idx)
100 column (+ column (char-width ch))
101 idx (1+ idx)))
102 (args-out-of-range (setq idx len)))
103 (if (< column start-column)
104 (if padding (make-string end-column padding) "")
105 (if (and padding (> column start-column))
106 (setq head-padding (make-string (- column start-column) padding)))
107 (setq from-idx idx)
108 (if (< end-column column)
109 (setq idx from-idx)
110 (condition-case nil
111 (while (< column end-column)
112 (setq last-column column
113 last-idx idx
114 ch (aref str idx)
115 column (+ column (char-width ch))
116 idx (1+ idx)))
117 (args-out-of-range (setq idx len)))
118 (if (> column end-column)
119 (setq column last-column idx last-idx))
120 (if (and padding (< column end-column))
121 (setq tail-padding (make-string (- end-column column) padding))))
122 (setq str (substring str from-idx idx))
123 (if padding
124 (concat head-padding str tail-padding)
125 str))))
126
127 ;;; For backward compatibility ...
128 ;;;###autoload
129 (defalias 'truncate-string 'truncate-string-to-width)
130 (make-obsolete 'truncate-string 'truncate-string-to-width)
131 \f
132 ;;; Nested alist handler. Nested alist is alist whose elements are
133 ;;; also nested alist.
134
135 ;;;###autoload
136 (defsubst nested-alist-p (obj)
137 "Return t if OBJ is a nested alist.
138
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).
142
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))))
147
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 is considered.
153 Optional 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 (if (null slot)
168 (progn
169 (setq slot (cons key-elt (list t)))
170 (setcdr alist (cons slot (cdr alist)))))
171 (setq alist (cdr slot))
172 (setq i (1+ i)))
173 (setcar alist entry)
174 (if branches
175 (if (cdr alist)
176 (error "Can't set branches for keyseq %s" keyseq)
177 (setcdr alist branches)))))
178
179 ;;;###autoload
180 (defun lookup-nested-alist (keyseq alist &optional len start nil-for-too-long)
181 "Look up key sequence KEYSEQ in nested alist ALIST. Return the definition.
182 Optional 1st argument LEN specifies the length of KEYSEQ.
183 Optional 2nd argument START specifies index of the starting key.
184 The returned value is normally a nested alist of which
185 car part is the entry for KEYSEQ.
186 If ALIST is not deep enough for KEYSEQ, return number which is
187 how many key elements at the front of KEYSEQ it takes
188 to reach a leaf in ALIST.
189 Optional 3rd argument NIL-FOR-TOO-LONG non-nil means return nil
190 even if ALIST is not deep enough."
191 (or (nested-alist-p alist)
192 (error "invalid argument %s" alist))
193 (or len
194 (setq len (length keyseq)))
195 (let ((i (or start 0)))
196 (if (catch 'lookup-nested-alist-tag
197 (if (listp keyseq)
198 (while (< i len)
199 (if (setq alist (cdr (assoc (nth i keyseq) (cdr alist))))
200 (setq i (1+ i))
201 (throw 'lookup-nested-alist-tag t))))
202 (while (< i len)
203 (if (setq alist (cdr (assoc (aref keyseq i) (cdr alist))))
204 (setq i (1+ i))
205 (throw 'lookup-nested-alist-tag t))))
206 ;; KEYSEQ is too long.
207 (if nil-for-too-long nil i)
208 alist)))
209
210 \f
211 ;; Coding system related functions.
212
213 ;;;###autoload
214 (defun coding-system-eol-type-mnemonic (coding-system)
215 "Return mnemonic letter of eol-type of CODING-SYSTEM."
216 (let ((eol-type (coding-system-eol-type coding-system)))
217 (cond ((vectorp eol-type) eol-mnemonic-undecided)
218 ((eq eol-type 0) eol-mnemonic-unix)
219 ((eq eol-type 1) eol-mnemonic-dos)
220 ((eq eol-type 2) eol-mnemonic-mac)
221 (t ?-))))
222
223 ;;;###autoload
224 (defun coding-system-post-read-conversion (coding-system)
225 "Return the value of CODING-SYSTEM's post-read-conversion property."
226 (coding-system-get coding-system 'post-read-conversion))
227
228 ;;;###autoload
229 (defun coding-system-pre-write-conversion (coding-system)
230 "Return the value of CODING-SYSTEM's pre-write-conversion property."
231 (coding-system-get coding-system 'pre-write-conversion))
232
233 ;;;###autoload
234 (defun coding-system-translation-table-for-decode (coding-system)
235 "Return the value of CODING-SYSTEM's translation-table-for-decode property."
236 (coding-system-get coding-system 'translation-table-for-decode))
237
238 ;;;###autoload
239 (defun coding-system-translation-table-for-encode (coding-system)
240 "Return the value of CODING-SYSTEM's translation-table-for-encode property."
241 (coding-system-get coding-system 'translation-table-for-encode))
242
243 ;;;###autoload
244 (defun coding-system-equal (coding-system-1 coding-system-2)
245 "Return t if and only if CODING-SYSTEM-1 and CODING-SYSTEM-2 are identical.
246 Two coding systems are identical if two symbols are equal
247 or one is an alias of the other."
248 (or (eq coding-system-1 coding-system-2)
249 (and (equal (coding-system-spec coding-system-1)
250 (coding-system-spec coding-system-2))
251 (let ((eol-type-1 (coding-system-eol-type coding-system-1))
252 (eol-type-2 (coding-system-eol-type coding-system-2)))
253 (or (eq eol-type-1 eol-type-2)
254 (and (vectorp eol-type-1) (vectorp eol-type-2)))))))
255
256 ;;;###autoload
257 (defmacro detect-coding-with-priority (from to priority-list)
258 "Detect a coding system of the text between FROM and TO with PRIORITY-LIST.
259 PRIORITY-LIST is an alist of coding categories vs the corresponding
260 coding systems ordered by priority."
261 `(unwind-protect
262 (let* ((prio-list ,priority-list)
263 (coding-category-list coding-category-list)
264 ,@(mapcar (function (lambda (x) (list x x)))
265 coding-category-list))
266 (mapcar (function (lambda (x) (set (car x) (cdr x))))
267 prio-list)
268 (set-coding-priority (mapcar (function (lambda (x) (car x)))
269 prio-list))
270 (detect-coding-region ,from ,to))
271 ;; We must restore the internal database.
272 (set-coding-priority coding-category-list)
273 (update-coding-systems-internal)))
274
275 ;;;###autoload
276 (defun detect-coding-with-language-environment (from to lang-env)
277 "Detect a coding system of the text between FROM and TO with LANG-ENV.
278 The detection takes into account the coding system priorities for the
279 language environment LANG-ENV."
280 (let ((coding-priority (get-language-info lang-env 'coding-priority)))
281 (if coding-priority
282 (detect-coding-with-priority
283 from to
284 (mapcar (function (lambda (x)
285 (cons (coding-system-get x 'coding-category) x)))
286 coding-priority))
287 (detect-coding-region from to))))
288
289 \f
290 ;;; Composite character manipulations.
291
292 ;;;###autoload
293 (defun compose-region (start end)
294 "Compose all characters in the current region into one composite character.
295 When called from a program, expects two arguments,
296 positions (integers or markers) specifying the region."
297 (interactive "r")
298 (save-excursion
299 (let ((str (buffer-substring start end)))
300 (goto-char start)
301 (insert (compose-string str))
302 (delete-char (- end start)))))
303
304 ;;;###autoload
305 (defun decompose-region (start end)
306 "Decompose all composite characters in the current region.
307 Composite characters are broken up into individual components.
308 When called from a program, expects two arguments,
309 positions (integers or markers) specifying the region."
310 (interactive "r")
311 (let ((buf (current-buffer))
312 (cmpchar-head (char-to-string leading-code-composition)))
313 (with-temp-buffer
314 (insert-buffer-substring buf start end)
315 (set-buffer-multibyte nil)
316 (goto-char (point-min))
317 (while (search-forward cmpchar-head nil t)
318 (if (looking-at "[\240-\377][\240-\377][\240-\377][\240-\377]+")
319 (let* ((from (1- (point)))
320 (to (match-end 0))
321 (str (string-as-multibyte (buffer-substring from to))))
322 (if (cmpcharp (string-to-char str))
323 (progn
324 (delete-region from to)
325 (insert (string-as-unibyte (decompose-string str))))
326 (goto-char to)))))
327 (set-buffer-multibyte t)
328 (let ((tempbuf (current-buffer)))
329 (save-excursion
330 (set-buffer buf)
331 (goto-char start)
332 (delete-region start end)
333 (insert-buffer-substring tempbuf))))))
334
335 ;;;###autoload
336 (defun decompose-string (string)
337 "Decompose all composite characters in STRING."
338 (let ((len (length string))
339 (idx 0)
340 (i 0)
341 (str-list nil)
342 ch)
343 (while (< idx len)
344 (setq ch (aref string idx))
345 (if (>= ch min-composite-char)
346 (progn
347 (if (> idx i)
348 (setq str-list (cons (substring string i idx) str-list)))
349 (setq str-list (cons (decompose-composite-char ch) str-list))
350 (setq i (1+ idx))))
351 (setq idx (1+ idx)))
352 (if (not str-list)
353 (copy-sequence string)
354 (if (> idx i)
355 (setq str-list (cons (substring string i idx) str-list)))
356 (apply 'concat (nreverse str-list)))))
357
358 ;;;###autoload
359 (defconst reference-point-alist
360 '((tl . 0) (tc . 1) (tr . 2)
361 (ml . 3) (mc . 4) (mr . 5)
362 (bl . 6) (bc . 7) (br . 8)
363 (top-left . 0) (top-center . 1) (top-right . 2)
364 (mid-left . 3) (mid-center . 4) (mid-right . 5)
365 (bottom-left . 6) (bottom-center . 7) (bottom-right . 8)
366 (0 . 0) (1 . 1) (2 . 2)
367 (3 . 3) (4 . 4) (5 . 5)
368 (6 . 6) (7 . 7) (8 . 8))
369 "Alist of reference point symbols vs reference point codes.
370 A reference point symbol is to be used to specify a composition rule
371 while making a composite character by the function `compose-chars'
372 (which see).
373
374 Meanings of reference point codes are as follows:
375
376 0----1----2 <-- ascent 0:tl or top-left
377 | | 1:tc or top-center
378 | | 2:tr or top-right
379 | | 3:ml or mid-left
380 | 4 <--+---- center 4:mc or mid-center
381 | | 5:mr or mid-right
382 --- 3 5 <-- baseline 6:bl or bottom-left
383 | | 7:bc or bottom-center
384 6----7----8 <-- descent 8:br or bottom-right
385
386 Reference point symbols are to be used to specify composition rule of
387 the form \(GLOBAL-REF-POINT . NEW-REF-POINT), where GLOBAL-REF-POINT
388 is a reference point in the overall glyphs already composed, and
389 NEW-REF-POINT is a reference point in the new glyph to be added.
390
391 For instance, if GLOBAL-REF-POINT is 8 and NEW-REF-POINT is 1, the
392 overall glyph is updated as follows:
393
394 +-------+--+ <--- new ascent
395 | | |
396 | global| |
397 | glyph | |
398 --- | | | <--- baseline (doesn't change)
399 +----+--+--+
400 | | new |
401 | |glyph|
402 +----+-----+ <--- new descent
403 ")
404
405 ;; Return a string for char CH to be embedded in multibyte form of
406 ;; composite character.
407 ;;;###autoload
408 (defun compose-chars-component (ch)
409 (if (< ch 128)
410 (format "\240%c" (+ ch 128))
411 (let ((str (string-as-unibyte (char-to-string ch))))
412 (if (cmpcharp ch)
413 (if (= (aref str 1) ?\xFF)
414 (error "Can't compose a rule-based composition character")
415 (substring str (if (= (aref str 1) ?\xFF) 2 1)))
416 (aset str 0 (+ (aref str 0) ?\x20))
417 str))))
418
419 ;; Return a string for composition rule RULE to be embedded in
420 ;; multibyte form of composite character.
421 (defsubst compose-chars-rule (rule)
422 (char-to-string (+ ?\xA0
423 (* (cdr (assq (car rule) reference-point-alist)) 9)
424 (cdr (assq (cdr rule) reference-point-alist)))))
425
426 ;;;###autoload
427 (defun compose-chars (first-component &rest args)
428 "Return one char string composed from the arguments.
429 For relative composition, each argument should be a non-composition character
430 or a relative-composition character.
431 For rule-based composition, Nth (where N is odd) argument should be
432 a non-composition character or a rule-based-composition character,
433 and Mth (where M is even) argument should be a composition rule.
434 A composition rule has the form \(GLOBAL-REF-POINT . NEW-REF-POINT).
435 See the documentation of `reference-point-alist' for more detail."
436 (if (= (length args) 0)
437 (char-to-string first-component)
438 (let* ((with-rule (consp (car args)))
439 (str (if (cmpcharp first-component)
440 (string-as-unibyte (char-to-string first-component))
441 (if with-rule
442 (concat (vector leading-code-composition ?\xFF)
443 (compose-chars-component first-component))
444 (concat (char-to-string leading-code-composition)
445 (compose-chars-component first-component))))))
446 (if (and (cmpcharp first-component)
447 (eq with-rule (/= (aref str 1) ?\xFF)))
448 (error "%s-compostion-character is not allowed in %s composition: %c"
449 (if with-rule "relative" "rule-based")
450 (if with-rule "rule-based" "relative")
451 first-component))
452 (while args
453 (if with-rule
454 (setq str (concat str (compose-chars-rule (car args)))
455 args (cdr args)))
456 (if (cmpcharp (car args))
457 (let ((cmp-str (string-as-unibyte (char-to-string (car args)))))
458 (if (eq with-rule (/= (aref cmp-str 1) ?\xFF))
459 (error "%s-compostion-character is not allowed in %s composition: %c"
460 (if with-rule "relative" "rule-based")
461 (if with-rule "rule-based" "relative")
462 (car args)))
463 (setq str (concat str (substring cmp-str
464 (if with-rule 2 1)))))
465 (setq str (concat str (compose-chars-component (car args)))))
466 (setq args (cdr args)))
467 (string-as-multibyte str))))
468
469 ;;;###autoload
470 (defun decompose-composite-char (char &optional type with-composition-rule)
471 "Convert composite character CHAR to a sequence of the components.
472 Optional 1st arg TYPE specifies the type of sequence returned.
473 It should be `string' (default), `list', or `vector'.
474 Optional 2nd arg WITH-COMPOSITION-RULE non-nil means the returned
475 sequence contains embedded composition rules if any. In this case, the
476 order of elements in the sequence is the same as arguments for
477 `compose-chars' to create CHAR.
478 If TYPE is omitted or is `string', composition rules are omitted
479 even if WITH-COMPOSITION-RULE is t."
480 (or type
481 (setq type 'string))
482 (let* ((len (composite-char-component-count char))
483 (i (1- len))
484 l)
485 (setq with-composition-rule (and with-composition-rule
486 (not (eq type 'string))
487 (composite-char-composition-rule-p char)))
488 (while (> i 0)
489 (setq l (cons (composite-char-component char i) l))
490 (if with-composition-rule
491 (let ((rule (- (composite-char-composition-rule char i) ?\xA0)))
492 (setq l (cons (cons (/ rule 9) (% rule 9)) l))))
493 (setq i (1- i)))
494 (setq l (cons (composite-char-component char 0) l))
495 (cond ((eq type 'string)
496 (apply 'string l))
497 ((eq type 'list)
498 l)
499 (t ; i.e. TYPE is vector
500 (vconcat l)))))
501
502 ;;; mule-util.el ends here