]> code.delx.au - gnu-emacs/blob - lisp/international/mule-util.el
(coding-system-unification-table-for-encode): Recurse properly.
[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 (or (eq type 'list) (eq type 'vector)
35 (error "Invalid type: %s" type))
36 (let* ((len (length string))
37 (i 0)
38 l ch)
39 (while (< i len)
40 (setq ch (if enable-multibyte-characters
41 (sref string i) (aref string i)))
42 (setq l (cons ch l))
43 (setq i (+ i (char-bytes ch))))
44 (setq l (nreverse l))
45 (if (eq type 'list)
46 l
47 (vconcat l))))
48
49 ;;;###autoload
50 (defsubst string-to-list (string)
51 "Return a list of characters in STRING."
52 (string-to-sequence string 'list))
53
54 ;;;###autoload
55 (defsubst string-to-vector (string)
56 "Return a vector of characters in STRING."
57 (string-to-sequence string 'vector))
58
59 ;;;###autoload
60 (defun store-substring (string idx obj)
61 "Embed OBJ (string or character) at index IDX of STRING."
62 (let* ((str (cond ((stringp obj) obj)
63 ((integerp obj) (char-to-string obj))
64 (t (error
65 "Invalid argument (should be string or character): %s"
66 obj))))
67 (string-len (length string))
68 (len (length str))
69 (i 0))
70 (while (and (< i len) (< idx string-len))
71 (aset string idx (aref str i))
72 (setq idx (1+ idx) 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 (sref str idx)
100 column (+ column (char-width ch))
101 idx (+ idx (char-bytes ch))))
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) ?\ )))
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 (sref str idx)
115 column (+ column (char-width ch))
116 idx (+ idx (char-bytes ch))))
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 compatiblity ...
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 nesetd 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 firlst 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 arguement %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 arguement %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-base (coding-system)
215 "Return a base of CODING-SYSTEM.
216 The base is a coding system of which coding-system property is a
217 coding-spec (see the function `make-coding-system')."
218 (let ((coding-spec (get coding-system 'coding-system)))
219 (if (vectorp coding-spec)
220 coding-system
221 (coding-system-base coding-spec))))
222
223 ;;;###autoload
224 (defun coding-system-eol-type-mnemonic (coding-system)
225 "Return mnemonic letter of eol-type of CODING-SYSTEM."
226 (let ((eol-type (coding-system-eol-type coding-system)))
227 (cond ((vectorp eol-type) eol-mnemonic-undecided)
228 ((eq eol-type 0) eol-mnemonic-unix)
229 ((eq eol-type 1) eol-mnemonic-unix)
230 ((eq eol-type 2) eol-mnemonic-unix)
231 (t ?-))))
232
233 ;;;###autoload
234 (defun coding-system-post-read-conversion (coding-system)
235 "Return post-read-conversion property of CODING-SYSTEM."
236 (and coding-system
237 (symbolp coding-system)
238 (or (get coding-system 'post-read-conversion)
239 (coding-system-post-read-conversion
240 (get coding-system 'coding-system)))))
241
242 ;;;###autoload
243 (defun coding-system-pre-write-conversion (coding-system)
244 "Return pre-write-conversion property of CODING-SYSTEM."
245 (and coding-system
246 (symbolp coding-system)
247 (or (get coding-system 'pre-write-conversion)
248 (coding-system-pre-write-conversion
249 (get coding-system 'coding-system)))))
250
251 ;;;###autoload
252 (defun coding-system-unification-table-for-decode (coding-system)
253 "Return unification-table-for-decode property of CODING-SYSTEM."
254 (and coding-system
255 (symbolp coding-system)
256 (or (get coding-system 'unification-table-for-decode)
257 (coding-system-unification-table-for-decode
258 (get coding-system 'coding-system)))))
259
260 ;;;###autoload
261 (defun coding-system-unification-table-for-encode (coding-system)
262 "Return unification-table-for-encode property of CODING-SYSTEM."
263 (and coding-system
264 (symbolp coding-system)
265 (or (get coding-system 'unification-table-for-encode)
266 (coding-system-unification-table-for-encode
267 (get coding-system 'coding-system)))))
268
269 (defun coding-system-lessp (x y)
270 (cond ((eq x 'no-conversion) t)
271 ((eq y 'no-conversion) nil)
272 ((eq x 'emacs-mule) t)
273 ((eq y 'emacs-mule) nil)
274 ((eq x 'undecided) t)
275 ((eq y 'undecided) nil)
276 (t (let ((c1 (coding-system-mnemonic x))
277 (c2 (coding-system-mnemonic y)))
278 (or (< (downcase c1) (downcase c2))
279 (and (not (> (downcase c1) (downcase c2)))
280 (< c1 c2)))))))
281
282 ;;;###autoload
283 (defun coding-system-list (&optional base-only)
284 "Return a list of all existing coding systems.
285 If optional arg BASE-ONLY is non-nil, only base coding systems are listed."
286 (let (l)
287 (mapatoms (lambda (x) (if (get x 'coding-system) (setq l (cons x l)))))
288 (let* ((codings (sort l 'coding-system-lessp))
289 (tail (cons nil codings))
290 coding)
291 ;; At first, remove subsidiary coding systems (eol variants) and
292 ;; alias coding systems (if necessary).
293 (while (cdr tail)
294 (setq coding (car (cdr tail)))
295 (if (or (get coding 'eol-variant)
296 (and base-only (coding-system-parent coding)))
297 (setcdr tail (cdr (cdr tail)))
298 (setq tail (cdr tail))))
299 codings)))
300
301 ;;;###autoload
302 (defun coding-system-plist (coding-system)
303 "Return property list of CODING-SYSTEM."
304 (let ((found nil)
305 coding-spec eol-type
306 post-read-conversion pre-write-conversion
307 unification-table)
308 (while (not found)
309 (or eol-type
310 (setq eol-type (get coding-system 'eol-type)))
311 (or post-read-conversion
312 (setq post-read-conversion
313 (get coding-system 'post-read-conversion)))
314 (or pre-write-conversion
315 (setq pre-write-conversion
316 (get coding-system 'pre-write-conversion)))
317 (or unification-table
318 (setq unification-table
319 (get coding-system 'unification-table)))
320 (setq coding-spec (get coding-system 'coding-system))
321 (if (and coding-spec (symbolp coding-spec))
322 (setq coding-system coding-spec)
323 (setq found t)))
324 (if (not coding-spec)
325 (error "Invalid coding system: %s" coding-system))
326 (list 'coding-spec coding-spec
327 'eol-type eol-type
328 'post-read-conversion post-read-conversion
329 'pre-write-conversion pre-write-conversion
330 'unification-table unification-table)))
331
332 ;;;###autoload
333 (defun coding-system-equal (coding-system-1 coding-system-2)
334 "Return t if and only if CODING-SYSTEM-1 and CODING-SYSTEM-2 are identical.
335 Two coding systems are identical if two symbols are equal
336 or one is an alias of the other."
337 (or (eq coding-system-1 coding-system-2)
338 (equal (coding-system-plist coding-system-1)
339 (coding-system-plist coding-system-2))))
340
341 \f
342 ;;; Composite charcater manipulations.
343
344 ;;;###autoload
345 (defun compose-region (start end)
346 "Compose all characters in the current region into one composite character.
347 When called from a program, expects two arguments,
348 positions (integers or markers) specifying the region."
349 (interactive "r")
350 (save-excursion
351 (let ((str (buffer-substring start end)))
352 (goto-char start)
353 (delete-region start end)
354 (insert (compose-string str)))))
355
356 ;;;###autoload
357 (defun decompose-region (start end)
358 "Decompose all composite characters in the current region.
359 Composite characters are broken up into individual components.
360 When called from a program, expects two arguments,
361 positions (integers or markers) specifying the region."
362 (interactive "r")
363 (save-restriction
364 (narrow-to-region start end)
365 (goto-char (point-min))
366 (let ((enable-multibyte-characters nil)
367 ;; This matches the whole bytes of single composite character.
368 (re-cmpchar "\200[\240-\377]+")
369 p ch str)
370 (while (re-search-forward re-cmpchar nil t)
371 (setq str (buffer-substring (match-beginning 0) (match-end 0)))
372 (delete-region (match-beginning 0) (match-end 0))
373 (insert (decompose-composite-char (string-to-char str)))))))
374
375 ;;;###autoload
376 (defun decompose-string (string)
377 "Decompose all composite characters in STRING."
378 (let* ((l (string-to-list string))
379 (tail l)
380 ch)
381 (while tail
382 (setq ch (car tail))
383 (setcar tail (if (cmpcharp ch) (decompose-composite-char ch)
384 (char-to-string ch)))
385 (setq tail (cdr tail)))
386 (apply 'concat l)))
387
388 ;;;###autoload
389 (defconst reference-point-alist
390 '((tl . 0) (tc . 1) (tr . 2)
391 (ml . 3) (mc . 4) (mr . 5)
392 (bl . 6) (bc . 7) (br . 8)
393 (top-left . 0) (top-center . 1) (top-right . 2)
394 (mid-left . 3) (mid-center . 4) (mid-right . 5)
395 (bottom-left . 6) (bottom-center . 7) (bottom-right . 8)
396 (0 . 0) (1 . 1) (2 . 2)
397 (3 . 3) (4 . 4) (5 . 5)
398 (6 . 6) (7 . 7) (8 . 8))
399 "Alist of reference point symbols vs reference point codes.
400 A reference point symbol is to be used to specify a composition rule
401 while making a composite character by the function `compose-chars'
402 (which see).
403
404 Meanings of reference point codes are as follows:
405
406 0----1----2 <-- ascent 0:tl or top-left
407 | | 1:tc or top-center
408 | | 2:tr or top-right
409 | | 3:ml or mid-left
410 | 4 <--+---- center 4:mc or mid-center
411 | | 5:mr or mid-right
412 --- 3 5 <-- baseline 6:bl or bottom-left
413 | | 7:bc or bottom-center
414 6----7----8 <-- descent 8:br or bottom-right
415
416 Reference point symbols are to be used to specify composition rule of
417 the form \(GLOBAL-REF-POINT . NEW-REF-POINT), where GLOBAL-REF-POINT
418 is a reference point in the overall glyphs already composed, and
419 NEW-REF-POINT is a reference point in the new glyph to be added.
420
421 For instance, if GLOBAL-REF-POINT is 8 and NEW-REF-POINT is 1, the
422 overall glyph is updated as follows:
423
424 +-------+--+ <--- new ascent
425 | | |
426 | global| |
427 | glyph | |
428 --- | | | <--- baseline (doesn't change)
429 +----+--+--+
430 | | new |
431 | |glyph|
432 +----+-----+ <--- new descent
433 ")
434
435 ;; Return a string for char CH to be embedded in multibyte form of
436 ;; composite character.
437 (defun compose-chars-component (ch)
438 (if (< ch 128)
439 (format "\240%c" (+ ch 128))
440 (let ((str (char-to-string ch)))
441 (if (cmpcharp ch)
442 (substring str (if (= (aref str 1) ?\xFF) 2 1))
443 (aset str 0 (+ (aref str 0) ?\x20))
444 str))))
445
446 ;; Return a string for composition rule RULE to be embedded in
447 ;; multibyte form of composite character.
448 (defsubst compose-chars-rule (rule)
449 (char-to-string (+ ?\xA0
450 (* (cdr (assq (car rule) reference-point-alist)) 9)
451 (cdr (assq (cdr rule) reference-point-alist)))))
452
453 ;;;###autoload
454 (defun compose-chars (first-component &rest args)
455 "Return one char string composed from the arguments.
456 Each argument is a character (including a composite chararacter)
457 or a composition rule.
458 A composition rule has the form \(GLOBAL-REF-POINT . NEW-REF-POINT).
459 See the documentation of `reference-point-alist' for more detail."
460 (if (= (length args) 0)
461 (char-to-string first-component)
462 (let* ((with-rule (consp (car args)))
463 (str (if with-rule (concat (vector leading-code-composition ?\xFF))
464 (char-to-string leading-code-composition))))
465 (setq str (concat str (compose-chars-component first-component)))
466 (while args
467 (if with-rule
468 (progn
469 (if (not (consp (car args)))
470 (error "Invalid composition rule: %s" (car args)))
471 (setq str (concat str (compose-chars-rule (car args))
472 (compose-chars-component (car (cdr args))))
473 args (cdr (cdr args))))
474 (setq str (concat str (compose-chars-component (car args)))
475 args (cdr args))))
476 str)))
477
478 ;;;###autoload
479 (defun decompose-composite-char (char &optional type with-composition-rule)
480 "Convert composite character CHAR to a string containing components of CHAR.
481 Optional 1st arg TYPE specifies the type of sequence returned.
482 It should be `string' (default), `list', or `vector'.
483 Optional 2nd arg WITH-COMPOSITION-RULE non-nil means the returned
484 sequence contains embedded composition rules if any. In this case, the
485 order of elements in the sequence is the same as arguments for
486 `compose-chars' to create CHAR.
487 If TYPE is omitted or is `string', composition rules are omitted
488 even if WITH-COMPOSITION-RULE is t."
489 (or type
490 (setq type 'string))
491 (let* ((len (composite-char-component-count char))
492 (i (1- len))
493 l)
494 (setq with-composition-rule (and with-composition-rule
495 (not (eq type 'string))
496 (composite-char-composition-rule-p char)))
497 (while (> i 0)
498 (setq l (cons (composite-char-component char i) l))
499 (if with-composition-rule
500 (let ((rule (- (composite-char-composition-rule char i) ?\xA0)))
501 (setq l (cons (cons (/ rule 9) (% rule 9)) l))))
502 (setq i (1- i)))
503 (setq l (cons (composite-char-component char 0) l))
504 (cond ((eq type 'string)
505 (apply 'concat-chars l))
506 ((eq type 'list)
507 l)
508 (t ; i.e. TYPE is vector
509 (vconcat l)))))
510
511 ;;; mule-util.el ends here