annotate lisp/mule/mule-util.el @ 222:aae4c8b01452

Added tag r20-4b9 for changeset 6c0ae1f9357f
author cvs
date Mon, 13 Aug 2007 10:10:03 +0200
parents b405438285a2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
187
b405438285a2 Import from CVS: tag r20-3b20
cvs
parents: 167
diff changeset
1 ;;; mule-util.el --- Utility functions for multilingual environment (mule)
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
2
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
4 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
5 ;; Copyright (C) 1997 MORIOKA Tomohiko
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
6
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
7 ;; Keywords: mule, multilingual
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
8
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
9 ;; This file is part of XEmacs.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
10
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
11 ;; XEmacs is free software; you can redistribute it and/or modify it
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
12 ;; under the terms of the GNU General Public License as published by
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
13 ;; the Free Software Foundation; either version 2, or (at your option)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
14 ;; any later version.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
15
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
16 ;; XEmacs is distributed in the hope that it will be useful, but
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
19 ;; General Public License for more details.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
20
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
22 ;; along with XEmacs; see the file COPYING. If not, write to the Free
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
23 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
24 ;; 02111-1307, USA.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
25
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
26 ;;; Code:
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
27
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
28 ;;; String manipulations while paying attention to multibyte
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
29 ;;; characters.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
30
187
b405438285a2 Import from CVS: tag r20-3b20
cvs
parents: 167
diff changeset
31 ;; That code is pointless in XEmacs/Mule, since our multibyte
b405438285a2 Import from CVS: tag r20-3b20
cvs
parents: 167
diff changeset
32 ;; representation doesn't leak to Lisp.
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
33
187
b405438285a2 Import from CVS: tag r20-3b20
cvs
parents: 167
diff changeset
34 ;; string-to-sequence, string-to-list, string-to-vector, store-substring,
b405438285a2 Import from CVS: tag r20-3b20
cvs
parents: 167
diff changeset
35 ;; truncate-string-to-width
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
36
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
37
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
38 ;;; Nested alist handler. Nested alist is alist whose elements are
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
39 ;;; also nested alist.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
40
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
41 ;; [Was defsubst]
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
42 ;;;###autoload
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
43 (defun nested-alist-p (obj)
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
44 "Return t if OBJ is a nesetd alist.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
45
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
46 Nested alist is a list of the form (ENTRY . BRANCHES), where ENTRY is
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
47 any Lisp object, and BRANCHES is a list of cons cells of the form
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
48 (KEY-ELEMENT . NESTED-ALIST).
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
49
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
50 You can use a nested alist to store any Lisp object (ENTRY) for a key
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
51 sequence KEYSEQ, where KEYSEQ is a sequence of KEY-ELEMENT. KEYSEQ
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
52 can be a string, a vector, or a list."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
53 (and obj (listp obj) (listp (cdr obj))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
54
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
55 ;;;###autoload
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
56 (defun set-nested-alist (keyseq entry alist &optional len branches)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
57 "Set ENTRY for KEYSEQ in a nested alist ALIST.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
58 Optional 4th arg LEN non-nil means the firlst LEN elements in KEYSEQ
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
59 is considered.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
60 Optional argument BRANCHES if non-nil is branches for a keyseq
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
61 longer than KEYSEQ.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
62 See the documentation of `nested-alist-p' for more detail."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
63 (or (nested-alist-p alist)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
64 (error "Invalid arguement %s" alist))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
65 (let ((islist (listp keyseq))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
66 (len (or len (length keyseq)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
67 (i 0)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
68 key-elt slot)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
69 (while (< i len)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
70 (if (null (nested-alist-p alist))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
71 (error "Keyseq %s is too long for this nested alist" keyseq))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
72 (setq key-elt (if islist (nth i keyseq) (aref keyseq i)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
73 (setq slot (assoc key-elt (cdr alist)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
74 (if (null slot)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
75 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
76 (setq slot (cons key-elt (list t)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
77 (setcdr alist (cons slot (cdr alist)))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
78 (setq alist (cdr slot))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
79 (setq i (1+ i)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
80 (setcar alist entry)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
81 (if branches
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
82 (if (cdr alist)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
83 (error "Can't set branches for keyseq %s" keyseq)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
84 (setcdr alist branches)))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
85
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
86 ;;;###autoload
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
87 (defun lookup-nested-alist (keyseq alist &optional len start nil-for-too-long)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
88 "Look up key sequence KEYSEQ in nested alist ALIST. Return the definition.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
89 Optional 1st argument LEN specifies the length of KEYSEQ.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
90 Optional 2nd argument START specifies index of the starting key.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
91 The returned value is normally a nested alist of which
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
92 car part is the entry for KEYSEQ.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
93 If ALIST is not deep enough for KEYSEQ, return number which is
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
94 how many key elements at the front of KEYSEQ it takes
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
95 to reach a leaf in ALIST.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
96 Optional 3rd argument NIL-FOR-TOO-LONG non-nil means return nil
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
97 even if ALIST is not deep enough."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
98 (or (nested-alist-p alist)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
99 (error "invalid arguement %s" alist))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
100 (or len
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
101 (setq len (length keyseq)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
102 (let ((i (or start 0)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
103 (if (catch 'lookup-nested-alist-tag
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
104 (if (listp keyseq)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
105 (while (< i len)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
106 (if (setq alist (cdr (assoc (nth i keyseq) (cdr alist))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
107 (setq i (1+ i))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
108 (throw 'lookup-nested-alist-tag t))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
109 (while (< i len)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
110 (if (setq alist (cdr (assoc (aref keyseq i) (cdr alist))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
111 (setq i (1+ i))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
112 (throw 'lookup-nested-alist-tag t))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
113 ;; KEYSEQ is too long.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
114 (if nil-for-too-long nil i)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
115 alist)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
116
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
117 ;; Coding system related functions.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
118
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
119 ;;;###autoload
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
120 (defun set-coding-system-alist (target-type regexp coding-system
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
121 &optional operation)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
122 "Update `coding-system-alist' according to the arguments.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
123 TARGET-TYPE specifies a type of the target: `file', `process', or `network'.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
124 TARGET-TYPE tells which slots of coding-system-alist should be affected.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
125 If `file', it affects slots for insert-file-contents and write-region.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
126 If `process', it affects slots for call-process, call-process-region, and
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
127 start-process.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
128 If `network', it affects a slot for open-network-process.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
129 REGEXP is a regular expression matching a target of I/O operation.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
130 CODING-SYSTEM is a coding system to perform code conversion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
131 on the I/O operation, or a cons of coding systems for decoding and
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
132 encoding respectively, or a function symbol which returns the cons.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
133 Optional arg OPERATION if non-nil specifies directly one of slots above.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
134 The valid value is: insert-file-contents, write-region,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
135 call-process, call-process-region, start-process, or open-network-stream.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
136 If OPERATION is specified, TARGET-TYPE is ignored.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
137 See the documentation of `coding-system-alist' for more detail."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
138 (or (stringp regexp)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
139 (error "Invalid regular expression: %s" regexp))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
140 (or (memq target-type '(file process network))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
141 (error "Invalid target type: %s" target-type))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
142 (if (symbolp coding-system)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
143 (if (not (fboundp coding-system))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
144 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
145 (check-coding-system coding-system)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
146 (setq coding-system (cons coding-system coding-system))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
147 (check-coding-system (car coding-system))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
148 (check-coding-system (cdr coding-system)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
149 (let ((op-list (if operation (list operation)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
150 (cond ((eq target-type 'file)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
151 '(insert-file-contents write-region))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
152 ((eq target-type 'process)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
153 '(call-process call-process-region start-process))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
154 (t ; i.e. (eq target-type network)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
155 '(open-network-stream)))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
156 slot)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
157 (while op-list
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
158 (setq slot (assq (car op-list) coding-system-alist))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
159 (if slot
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
160 (let ((chain (cdr slot)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
161 (if (catch 'tag
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
162 (while chain
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
163 (if (string= regexp (car (car chain)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
164 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
165 (setcdr (car chain) coding-system)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
166 (throw 'tag nil)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
167 (setq chain (cdr chain)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
168 t)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
169 (setcdr slot (cons (cons regexp coding-system) (cdr slot)))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
170 (setq coding-system-alist
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
171 (cons (cons (car op-list) (list (cons regexp coding-system)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
172 coding-system-alist)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
173 (setq op-list (cdr op-list)))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
174
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
175
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
176 ;;; Composite charcater manipulations.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
177
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
178 ;;;###autoload
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
179 (defun compose-region (start end &optional buffer)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
180 "Compose characters in the current region into one composite character.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
181 From a Lisp program, pass two arguments, START to END.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
182 The composite character replaces the composed characters.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
183 BUFFER defaults to the current buffer if omitted."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
184 (interactive "r")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
185 (let ((ch (make-composite-char (buffer-substring start end buffer))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
186 (delete-region start end buffer)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
187 (insert-char ch nil nil buffer)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
188
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
189 ;;;###autoload
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
190 (defun decompose-region (start end &optional buffer)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
191 "Decompose any composite characters in the current region.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
192 From a Lisp program, pass two arguments, START to END.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
193 This converts each composite character into one or more characters,
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
194 the individual characters out of which the composite character was formed.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
195 Non-composite characters are left as-is. BUFFER defaults to the current
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
196 buffer if omitted."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
197 (interactive "r")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
198 (save-excursion
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
199 (set-buffer buffer)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
200 (save-restriction
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
201 (narrow-to-region start end)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
202 (goto-char (point-min))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
203 (let ((compcharset (get-charset 'composite)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
204 (while (< (point) (point-max))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
205 (let ((ch (char-after (point))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
206 (if (eq compcharset (char-charset ch))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
207 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
208 (delete-char 1)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
209 (insert (composite-char-string ch))))))))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
210
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
211 ;;;###autoload
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
212 (defconst reference-point-alist
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
213 '((tl . 0) (tc . 1) (tr . 2)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
214 (ml . 3) (mc . 4) (mr . 5)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
215 (bl . 6) (bc . 7) (br . 8)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
216 (top-left . 0) (top-center . 1) (top-right . 2)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
217 (mid-left . 3) (mid-center . 4) (mid-right . 5)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
218 (bottom-left . 6) (bottom-center . 7) (bottom-right . 8)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
219 (0 . 0) (1 . 1) (2 . 2)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
220 (3 . 3) (4 . 4) (5 . 5)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
221 (6 . 6) (7 . 7) (8 . 8))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
222 "Alist of reference point symbols vs reference point codes.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
223 Meanings of reference point codes are as follows:
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
224
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
225 0----1----2 <-- ascent 0:tl or top-left
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
226 | | 1:tc or top-center
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
227 | | 2:tr or top-right
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
228 | | 3:ml or mid-left
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
229 | 4 <--+---- center 4:mc or mid-center
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
230 | | 5:mr or mid-right
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
231 --- 3 5 <-- baseline 6:bl or bottom-left
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
232 | | 7:bc or bottom-center
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
233 6----7----8 <-- descent 8:br or bottom-right
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
234
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
235 Reference point symbols are to be used to specify composition rule of
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
236 the form \(GLOBAL-REF-POINT . NEW-REF-POINT), where GLOBAL-REF-POINT
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
237 is a reference point in the overall glyphs already composed, and
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
238 NEW-REF-POINT is a reference point in the new glyph to be added.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
239
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
240 For instance, if GLOBAL-REF-POINT is 8 and NEW-REF-POINT is 1, the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
241 overall glyph is updated as follows:
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
242
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
243 +-------+--+ <--- new ascent
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
244 | | |
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
245 | global| |
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
246 | glyph | |
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
247 --- | | | <--- baseline (doesn't change)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
248 +----+--+--+
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
249 | | new |
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
250 | |glyph|
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
251 +----+-----+ <--- new descent
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
252 ")
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
253
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
254 ;; Return a string for char CH to be embedded in multibyte form of
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
255 ;; composite character.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
256 (defun compose-chars-component (ch)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
257 (if (< ch 128)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
258 (format "\240%c" (+ ch 128))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
259 (let ((str (char-to-string ch)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
260 (if (cmpcharp ch)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
261 (if (/= (aref str 1) ?\xFF)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
262 (error "Char %c can't be composed" ch)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
263 (substring str 2))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
264 (aset str 0 (+ (aref str 0) ?\x20))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
265 str))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
266
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
267 ;; Return a string for composition rule RULE to be embedded in
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
268 ;; multibyte form of composite character.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
269 (defsubst compose-chars-rule (rule)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
270 (char-to-string (+ ?\xA0
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
271 (* (cdr (assq (car rule) reference-point-alist)) 9)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
272 (cdr (assq (cdr rule) reference-point-alist)))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
273
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
274 ;;;###autoload
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
275 (defun compose-chars (first-component &rest args)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
276 "Return one char string composed from the arguments.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
277 Each argument is a character (including a composite chararacter)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
278 or a composition rule.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
279 A composition rule has the form \(GLOBAL-REF-POINT . NEW-REF-POINT).
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
280 See the documentation of `reference-point-alist' for more detail."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
281 (if (= (length args) 0)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
282 (char-to-string first-component)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
283 (let* ((with-rule (consp (car args)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
284 (str (if with-rule (concat (vector leading-code-composition ?\xFF))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
285 (char-to-string leading-code-composition))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
286 (setq str (concat str (compose-chars-component first-component)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
287 (while args
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
288 (if with-rule
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
289 (progn
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
290 (if (not (consp (car args)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
291 (error "Invalid composition rule: %s" (car args)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
292 (setq str (concat str (compose-chars-rule (car args))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
293 (compose-chars-component (car (cdr args))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
294 args (cdr (cdr args))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
295 (setq str (concat str (compose-chars-component (car args)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
296 args (cdr args))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
297 str)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
298
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
299 ;;;###autoload
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
300 (defun decompose-composite-char (char &optional type with-composition-rule)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
301 "Convert composite character CHAR to a string containing components of CHAR.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
302 Optional 1st arg TYPE specifies the type of sequence returned.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
303 It should be `string' (default), `list', or `vector'.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
304 Optional 2nd arg WITH-COMPOSITION-RULE non-nil means the returned
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
305 sequence contains embedded composition rules if any. In this case, the
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
306 order of elements in the sequence is the same as arguments for
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
307 `compose-chars' to create CHAR.
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
308 If TYPE is omitted or is `string', composition rules are omitted
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
309 even if WITH-COMPOSITION-RULE is t."
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
310 (or type
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
311 (setq type 'string))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
312 (let* ((len (composite-char-component-count char))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
313 (i (1- len))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
314 l)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
315 (setq with-composition-rule (and with-composition-rule
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
316 (not (eq type 'string))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
317 (composite-char-composition-rule-p char)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
318 (while (> i 0)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
319 (setq l (cons (composite-char-component char i) l))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
320 (if with-composition-rule
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
321 (let ((rule (- (composite-char-composition-rule char i) ?\xA0)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
322 (setq l (cons (cons (/ rule 9) (% rule 9)) l))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
323 (setq i (1- i)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
324 (setq l (cons (composite-char-component char 0) l))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
325 (cond ((eq type 'string)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
326 (apply 'concat-chars l))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
327 ((eq type 'list)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
328 l)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
329 (t ; i.e. TYPE is vector
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
330 (vconcat l)))))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
331
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents:
diff changeset
332 ;;; mule-util.el ends here