]> code.delx.au - gnu-emacs/blob - lisp/international/mule-util.el
(byte-compile-inline-expand):
[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 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-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 (defun coding-system-lessp (x y)
244 (cond ((eq x 'no-conversion) t)
245 ((eq y 'no-conversion) nil)
246 ((eq x 'emacs-mule) t)
247 ((eq y 'emacs-mule) nil)
248 ((eq x 'undecided) t)
249 ((eq y 'undecided) nil)
250 (t (let ((c1 (coding-system-mnemonic x))
251 (c2 (coding-system-mnemonic y)))
252 (or (< (downcase c1) (downcase c2))
253 (and (not (> (downcase c1) (downcase c2)))
254 (< c1 c2)))))))
255
256 ;;;###autoload
257 (defun coding-system-list (&optional base-only)
258 "Return a list of all existing coding systems.
259 If optional arg BASE-ONLY is non-nil, only base coding systems are listed."
260 (let* ((codings (sort (copy-sequence coding-system-list)
261 'coding-system-lessp))
262 (tail (cons nil codings)))
263 ;; Remove subsidiary coding systems (eol variants) and alias
264 ;; coding systems (if necessary).
265 (while (cdr tail)
266 (let* ((coding (car (cdr tail)))
267 (aliases (coding-system-get coding 'alias-coding-systems)))
268 (if (or
269 ;; CODING is an eol varinant if not in ALIASES.
270 (not (memq coding aliases))
271 ;; CODING is an alias if it is not car of ALISES.
272 (and base-only (not (eq coding (car aliases)))))
273 (setcdr tail (cdr (cdr tail)))
274 (setq tail (cdr tail)))))
275 codings))
276
277 ;;;###autoload
278 (defun coding-system-equal (coding-system-1 coding-system-2)
279 "Return t if and only if CODING-SYSTEM-1 and CODING-SYSTEM-2 are identical.
280 Two coding systems are identical if two symbols are equal
281 or one is an alias of the other."
282 (or (eq coding-system-1 coding-system-2)
283 (and (equal (coding-system-spec coding-system-1)
284 (coding-system-spec coding-system-2))
285 (let ((eol-type-1 (coding-system-eol-type coding-system-1))
286 (eol-type-2 (coding-system-eol-type coding-system-2)))
287 (or (eq eol-type-1 eol-type-2)
288 (and (vectorp eol-type-1) (vectorp eol-type-2)))))))
289
290 ;;;###autoload
291 (defun coding-system-change-eol-conversion (coding-system eol-type)
292 "Return a coding system which differs from CODING-SYSTEM in eol conversion.
293 The returned coding system converts end-of-line by EOL-TYPE
294 but text as the same way as CODING-SYSTEM.
295 EOL-TYPE should be `unix', `dos', `mac', or nil.
296 If EOL-TYPE is nil, the returned coding system detects
297 how end-of-line is formatted automatically while decoding.
298
299 EOL-TYPE can be specified by an integer 0, 1, or 2.
300 They means `unix', `dos', and `mac' respectively."
301 (if (symbolp eol-type)
302 (setq eol-type (cond ((eq eol-type 'unix) 0)
303 ((eq eol-type 'dos) 1)
304 ((eq eol-type 'mac) 2)
305 (t eol-type))))
306 (let ((orig-eol-type (coding-system-eol-type coding-system)))
307 (if (vectorp orig-eol-type)
308 (if (not eol-type)
309 coding-system
310 (aref orig-eol-type eol-type))
311 (let ((base (coding-system-base coding-system)))
312 (if (not eol-type)
313 base
314 (if (= eol-type orig-eol-type)
315 coding-system
316 (setq orig-eol-type (coding-system-eol-type base))
317 (if (vectorp orig-eol-type)
318 (aref orig-eol-type eol-type))))))))
319
320 ;;;###autoload
321 (defun coding-system-change-text-conversion (coding-system coding)
322 "Return a coding system which differs from CODING-SYSTEM in text conversion.
323 The returned coding system converts text by CODING
324 but end-of-line as the same way as CODING-SYSTEM.
325 If CODING is nil, the returned coding system detects
326 how text is formatted automatically while decoding."
327 (if (not coding)
328 (coding-system-base coding-system)
329 (let ((eol-type (coding-system-eol-type coding-system)))
330 (coding-system-change-eol-conversion
331 coding
332 (if (numberp eol-type) (aref [unix dos mac] eol-type))))))
333
334 ;;;###autoload
335 (defmacro detect-coding-with-priority (from to priority-list)
336 "Detect a coding system of the text between FROM and TO with PRIORITY-LIST.
337 PRIORITY-LIST is an alist of coding categories vs the corresponding
338 coding systems ordered by priority."
339 `(let* ((prio-list ,priority-list)
340 (coding-category-list coding-category-list)
341 ,@(mapcar (function (lambda (x) (list x x))) coding-category-list))
342 (mapcar (function (lambda (x) (set (car x) (cdr x))))
343 prio-list)
344 (set-coding-priority (mapcar (function (lambda (x) (car x))) prio-list))
345 (detect-coding-region ,from ,to)))
346
347 ;;;###autoload
348 (defun detect-coding-with-language-environment (from to lang-env)
349 "Detect a coding system of the text between FROM and TO with LANG-ENV.
350 The detection takes into accont the coding system priorities for the
351 language environment LANG-ENV."
352 (let ((coding-priority (get-language-info lang-env 'coding-priority)))
353 (if coding-priority
354 (detect-coding-with-priority
355 from to
356 (mapcar (function (lambda (x)
357 (cons (coding-system-get x 'coding-category) x)))
358 coding-priority))
359 (detect-coding-region from to))))
360
361 \f
362 ;;; Composite charcater manipulations.
363
364 ;;;###autoload
365 (defun compose-region (start end)
366 "Compose all characters in the current region into one composite character.
367 When called from a program, expects two arguments,
368 positions (integers or markers) specifying the region."
369 (interactive "r")
370 (save-excursion
371 (let ((str (buffer-substring start end)))
372 (goto-char start)
373 (delete-region start end)
374 (insert (compose-string str)))))
375
376 ;;;###autoload
377 (defun decompose-region (start end)
378 "Decompose all composite characters in the current region.
379 Composite characters are broken up into individual components.
380 When called from a program, expects two arguments,
381 positions (integers or markers) specifying the region."
382 (interactive "r")
383 (save-excursion
384 (save-restriction
385 (narrow-to-region start end)
386 (goto-char (point-min))
387 (while (not (eobp))
388 (let ((ch (following-char)))
389 (if (>= ch min-composite-char)
390 (progn
391 (delete-char 1)
392 (insert (decompose-composite-char ch)))
393 (forward-char 1)))))))
394
395 ;;;###autoload
396 (defun decompose-string (string)
397 "Decompose all composite characters in STRING."
398 (let ((len (length string))
399 (idx 0)
400 (i 0)
401 (str-list nil)
402 ch)
403 (while (< idx len)
404 (setq ch (aref string idx))
405 (if (>= ch min-composite-char)
406 (progn
407 (if (> idx i)
408 (setq str-list (cons (substring string i idx) str-list)))
409 (setq str-list (cons (decompose-composite-char ch) str-list))
410 (setq i (1+ idx))))
411 (setq idx (1+ idx)))
412 (if (not str-list)
413 (copy-sequence string)
414 (if (> idx i)
415 (setq str-list (cons (substring string i idx) str-list)))
416 (apply 'concat (nreverse str-list)))))
417
418 ;;;###autoload
419 (defconst reference-point-alist
420 '((tl . 0) (tc . 1) (tr . 2)
421 (ml . 3) (mc . 4) (mr . 5)
422 (bl . 6) (bc . 7) (br . 8)
423 (top-left . 0) (top-center . 1) (top-right . 2)
424 (mid-left . 3) (mid-center . 4) (mid-right . 5)
425 (bottom-left . 6) (bottom-center . 7) (bottom-right . 8)
426 (0 . 0) (1 . 1) (2 . 2)
427 (3 . 3) (4 . 4) (5 . 5)
428 (6 . 6) (7 . 7) (8 . 8))
429 "Alist of reference point symbols vs reference point codes.
430 A reference point symbol is to be used to specify a composition rule
431 while making a composite character by the function `compose-chars'
432 (which see).
433
434 Meanings of reference point codes are as follows:
435
436 0----1----2 <-- ascent 0:tl or top-left
437 | | 1:tc or top-center
438 | | 2:tr or top-right
439 | | 3:ml or mid-left
440 | 4 <--+---- center 4:mc or mid-center
441 | | 5:mr or mid-right
442 --- 3 5 <-- baseline 6:bl or bottom-left
443 | | 7:bc or bottom-center
444 6----7----8 <-- descent 8:br or bottom-right
445
446 Reference point symbols are to be used to specify composition rule of
447 the form \(GLOBAL-REF-POINT . NEW-REF-POINT), where GLOBAL-REF-POINT
448 is a reference point in the overall glyphs already composed, and
449 NEW-REF-POINT is a reference point in the new glyph to be added.
450
451 For instance, if GLOBAL-REF-POINT is 8 and NEW-REF-POINT is 1, the
452 overall glyph is updated as follows:
453
454 +-------+--+ <--- new ascent
455 | | |
456 | global| |
457 | glyph | |
458 --- | | | <--- baseline (doesn't change)
459 +----+--+--+
460 | | new |
461 | |glyph|
462 +----+-----+ <--- new descent
463 ")
464
465 ;; Return a string for char CH to be embedded in multibyte form of
466 ;; composite character.
467 (defun compose-chars-component (ch)
468 (if (< ch 128)
469 (format "\240%c" (+ ch 128))
470 (let ((str (string-as-unibyte (char-to-string ch))))
471 (if (cmpcharp ch)
472 (substring str (if (= (aref str 1) ?\xFF) 2 1))
473 (aset str 0 (+ (aref str 0) ?\x20))
474 str))))
475
476 ;; Return a string for composition rule RULE to be embedded in
477 ;; multibyte form of composite character.
478 (defsubst compose-chars-rule (rule)
479 (char-to-string (+ ?\xA0
480 (* (cdr (assq (car rule) reference-point-alist)) 9)
481 (cdr (assq (cdr rule) reference-point-alist)))))
482
483 ;;;###autoload
484 (defun compose-chars (first-component &rest args)
485 "Return one char string composed from the arguments.
486 Each argument is a character (including a composite chararacter)
487 or a composition rule.
488 A composition rule has the form \(GLOBAL-REF-POINT . NEW-REF-POINT).
489 See the documentation of `reference-point-alist' for more detail."
490 (if (= (length args) 0)
491 (char-to-string first-component)
492 (let* ((with-rule (consp (car args)))
493 (str (if with-rule (concat (vector leading-code-composition ?\xFF))
494 (char-to-string leading-code-composition))))
495 (setq str (concat str (compose-chars-component first-component)))
496 (while args
497 (if with-rule
498 (progn
499 (if (not (consp (car args)))
500 (error "Invalid composition rule: %s" (car args)))
501 (setq str (concat str (compose-chars-rule (car args))
502 (compose-chars-component (car (cdr args))))
503 args (cdr (cdr args))))
504 (setq str (concat str (compose-chars-component (car args)))
505 args (cdr args))))
506 (string-as-multibyte str))))
507
508 ;;;###autoload
509 (defun decompose-composite-char (char &optional type with-composition-rule)
510 "Convert composite character CHAR to a sequence of the components.
511 Optional 1st arg TYPE specifies the type of sequence returned.
512 It should be `string' (default), `list', or `vector'.
513 Optional 2nd arg WITH-COMPOSITION-RULE non-nil means the returned
514 sequence contains embedded composition rules if any. In this case, the
515 order of elements in the sequence is the same as arguments for
516 `compose-chars' to create CHAR.
517 If TYPE is omitted or is `string', composition rules are omitted
518 even if WITH-COMPOSITION-RULE is t."
519 (or type
520 (setq type 'string))
521 (let* ((len (composite-char-component-count char))
522 (i (1- len))
523 l)
524 (setq with-composition-rule (and with-composition-rule
525 (not (eq type 'string))
526 (composite-char-composition-rule-p char)))
527 (while (> i 0)
528 (setq l (cons (composite-char-component char i) l))
529 (if with-composition-rule
530 (let ((rule (- (composite-char-composition-rule char i) ?\xA0)))
531 (setq l (cons (cons (/ rule 9) (% rule 9)) l))))
532 (setq i (1- i)))
533 (setq l (cons (composite-char-component char 0) l))
534 (cond ((eq type 'string)
535 (apply 'string l))
536 ((eq type 'list)
537 l)
538 (t ; i.e. TYPE is vector
539 (vconcat l)))))
540
541 ;;; mule-util.el ends here