Mercurial > emacs
annotate lisp/international/mule-util.el @ 19745:356bc22e88f6
(string-to-sequence): Work usefully when enable-multibyte-characters is nil.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Thu, 04 Sep 1997 04:48:23 +0000 |
parents | 7cf3d42a6fd7 |
children | f43513081277 |
rev | line source |
---|---|
17052 | 1 ;;; mule-util.el --- Utility functions for mulitilingual environment (mule) |
2 | |
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN. | |
18377
8b4a66c66dd6
Change copyright notice.
Richard M. Stallman <rms@gnu.org>
parents:
18313
diff
changeset
|
4 ;; Licensed to the Free Software Foundation. |
17052 | 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 | |
17071 | 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. | |
17052 | 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. | |
18200
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
33 TYPE should be `list' or `vector'." |
17052 | 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) | |
19745
356bc22e88f6
(string-to-sequence): Work usefully when enable-multibyte-characters is nil.
Richard M. Stallman <rms@gnu.org>
parents:
19455
diff
changeset
|
40 (setq ch (if enable-multibyte-characters |
356bc22e88f6
(string-to-sequence): Work usefully when enable-multibyte-characters is nil.
Richard M. Stallman <rms@gnu.org>
parents:
19455
diff
changeset
|
41 (sref string i) (aref string i))) |
17052 | 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 width &optional start-column padding) | |
77 "Truncate string STR to fit in WIDTH columns. | |
78 Optional 1st arg START-COLUMN if non-nil specifies the starting column. | |
17092
e7920fdc4948
(truncate-string-to-width): Argument PADDING can be a padding character.
Kenichi Handa <handa@m17n.org>
parents:
17071
diff
changeset
|
79 Optional 2nd arg PADDING if non-nil is a padding character to be padded at |
17052 | 80 the head and tail of the resulting string to fit in WIDTH if necessary. |
81 If PADDING is nil, the resulting string may be narrower than WIDTH." | |
82 (or start-column | |
83 (setq start-column 0)) | |
84 (let ((len (length str)) | |
85 (idx 0) | |
86 (column 0) | |
87 (head-padding "") (tail-padding "") | |
88 ch last-column last-idx from-idx) | |
89 (condition-case nil | |
90 (while (< column start-column) | |
91 (setq ch (sref str idx) | |
92 column (+ column (char-width ch)) | |
93 idx (+ idx (char-bytes ch)))) | |
94 (args-out-of-range (setq idx len))) | |
95 (if (< column start-column) | |
17092
e7920fdc4948
(truncate-string-to-width): Argument PADDING can be a padding character.
Kenichi Handa <handa@m17n.org>
parents:
17071
diff
changeset
|
96 (if padding (make-string width padding) "") |
17052 | 97 (if (and padding (> column start-column)) |
98 (setq head-padding (make-string (- column start-column) ?\ ))) | |
99 (setq from-idx idx) | |
100 (condition-case nil | |
101 (while (< column width) | |
102 (setq last-column column | |
103 last-idx idx | |
104 ch (sref str idx) | |
105 column (+ column (char-width ch)) | |
106 idx (+ idx (char-bytes ch)))) | |
107 (args-out-of-range (setq idx len))) | |
108 (if (> column width) | |
109 (setq column last-column idx last-idx)) | |
110 (if (and padding (< column width)) | |
17092
e7920fdc4948
(truncate-string-to-width): Argument PADDING can be a padding character.
Kenichi Handa <handa@m17n.org>
parents:
17071
diff
changeset
|
111 (setq tail-padding (make-string (- width column) padding))) |
17052 | 112 (setq str (substring str from-idx idx)) |
113 (if padding | |
114 (concat head-padding str tail-padding) | |
115 str)))) | |
116 | |
117 ;;; For backward compatiblity ... | |
118 ;;;###autoload | |
119 (defalias 'truncate-string 'truncate-string-to-width) | |
120 (make-obsolete 'truncate-string 'truncate-string-to-width) | |
121 | |
122 ;;; Nested alist handler. Nested alist is alist whose elements are | |
123 ;;; also nested alist. | |
124 | |
125 ;;;###autoload | |
126 (defsubst nested-alist-p (obj) | |
127 "Return t if OBJ is a nesetd alist. | |
128 | |
129 Nested alist is a list of the form (ENTRY . BRANCHES), where ENTRY is | |
130 any Lisp object, and BRANCHES is a list of cons cells of the form | |
131 (KEY-ELEMENT . NESTED-ALIST). | |
132 | |
133 You can use a nested alist to store any Lisp object (ENTRY) for a key | |
134 sequence KEYSEQ, where KEYSEQ is a sequence of KEY-ELEMENT. KEYSEQ | |
135 can be a string, a vector, or a list." | |
136 (and obj (listp obj) (listp (cdr obj)))) | |
137 | |
138 ;;;###autoload | |
139 (defun set-nested-alist (keyseq entry alist &optional len branches) | |
140 "Set ENTRY for KEYSEQ in a nested alist ALIST. | |
141 Optional 4th arg LEN non-nil means the firlst LEN elements in KEYSEQ | |
142 is considered. | |
143 Optional argument BRANCHES if non-nil is branches for a keyseq | |
144 longer than KEYSEQ. | |
145 See the documentation of `nested-alist-p' for more detail." | |
146 (or (nested-alist-p alist) | |
147 (error "Invalid arguement %s" alist)) | |
148 (let ((islist (listp keyseq)) | |
149 (len (or len (length keyseq))) | |
150 (i 0) | |
151 key-elt slot) | |
152 (while (< i len) | |
153 (if (null (nested-alist-p alist)) | |
154 (error "Keyseq %s is too long for this nested alist" keyseq)) | |
155 (setq key-elt (if islist (nth i keyseq) (aref keyseq i))) | |
156 (setq slot (assoc key-elt (cdr alist))) | |
157 (if (null slot) | |
158 (progn | |
159 (setq slot (cons key-elt (list t))) | |
160 (setcdr alist (cons slot (cdr alist))))) | |
161 (setq alist (cdr slot)) | |
162 (setq i (1+ i))) | |
163 (setcar alist entry) | |
164 (if branches | |
165 (if (cdr alist) | |
166 (error "Can't set branches for keyseq %s" keyseq) | |
167 (setcdr alist branches))))) | |
168 | |
169 ;;;###autoload | |
170 (defun lookup-nested-alist (keyseq alist &optional len start nil-for-too-long) | |
171 "Look up key sequence KEYSEQ in nested alist ALIST. Return the definition. | |
172 Optional 1st argument LEN specifies the length of KEYSEQ. | |
173 Optional 2nd argument START specifies index of the starting key. | |
174 The returned value is normally a nested alist of which | |
175 car part is the entry for KEYSEQ. | |
176 If ALIST is not deep enough for KEYSEQ, return number which is | |
177 how many key elements at the front of KEYSEQ it takes | |
178 to reach a leaf in ALIST. | |
179 Optional 3rd argument NIL-FOR-TOO-LONG non-nil means return nil | |
180 even if ALIST is not deep enough." | |
181 (or (nested-alist-p alist) | |
182 (error "invalid arguement %s" alist)) | |
183 (or len | |
184 (setq len (length keyseq))) | |
185 (let ((i (or start 0))) | |
186 (if (catch 'lookup-nested-alist-tag | |
187 (if (listp keyseq) | |
188 (while (< i len) | |
189 (if (setq alist (cdr (assoc (nth i keyseq) (cdr alist)))) | |
190 (setq i (1+ i)) | |
191 (throw 'lookup-nested-alist-tag t)))) | |
192 (while (< i len) | |
193 (if (setq alist (cdr (assoc (aref keyseq i) (cdr alist)))) | |
194 (setq i (1+ i)) | |
195 (throw 'lookup-nested-alist-tag t)))) | |
196 ;; KEYSEQ is too long. | |
197 (if nil-for-too-long nil i) | |
198 alist))) | |
199 | |
18299
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
200 |
17052 | 201 ;; Coding system related functions. |
202 | |
203 ;;;###autoload | |
18200
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
204 (defun coding-system-base (coding-system) |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
205 "Return a base of CODING-SYSTEM. |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
206 The base is a coding system of which coding-system property is a |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
207 coding-spec (see the function `make-coding-system')." |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
208 (let ((coding-spec (get coding-system 'coding-system))) |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
209 (if (vectorp coding-spec) |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
210 coding-system |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
211 (coding-system-base coding-spec)))) |
17052 | 212 |
213 ;;;###autoload | |
18200
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
214 (defun coding-system-eol-type-mnemonic (coding-system) |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
215 "Return mnemonic letter of eol-type of CODING-SYSTEM." |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
216 (let ((eol-type (coding-system-eol-type coding-system))) |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
217 (cond ((vectorp eol-type) eol-mnemonic-undecided) |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
218 ((eq eol-type 0) eol-mnemonic-unix) |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
219 ((eq eol-type 1) eol-mnemonic-unix) |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
220 ((eq eol-type 2) eol-mnemonic-unix) |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
221 (t ?-)))) |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
222 |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
223 ;;;###autoload |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
224 (defun coding-system-post-read-conversion (coding-system) |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
225 "Return post-read-conversion property of CODING-SYSTEM." |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
226 (and coding-system |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
227 (symbolp coding-system) |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
228 (or (get coding-system 'post-read-conversion) |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
229 (coding-system-post-read-conversion |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
230 (get coding-system 'coding-system))))) |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
231 |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
232 ;;;###autoload |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
233 (defun coding-system-pre-write-conversion (coding-system) |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
234 "Return pre-write-conversion property of CODING-SYSTEM." |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
235 (and coding-system |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
236 (symbolp coding-system) |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
237 (or (get coding-system 'pre-write-conversion) |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
238 (coding-system-pre-write-conversion |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
239 (get coding-system 'coding-system))))) |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
240 |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
241 ;;;###autoload |
19455
7cf3d42a6fd7
(coding-system-unification-table):
Kenichi Handa <handa@m17n.org>
parents:
19264
diff
changeset
|
242 (defun coding-system-unification-table-for-decode (coding-system) |
7cf3d42a6fd7
(coding-system-unification-table):
Kenichi Handa <handa@m17n.org>
parents:
19264
diff
changeset
|
243 "Return unification-table-for-decode property of CODING-SYSTEM." |
18200
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
244 (and coding-system |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
245 (symbolp coding-system) |
19455
7cf3d42a6fd7
(coding-system-unification-table):
Kenichi Handa <handa@m17n.org>
parents:
19264
diff
changeset
|
246 (or (get coding-system 'unification-table-for-decode) |
7cf3d42a6fd7
(coding-system-unification-table):
Kenichi Handa <handa@m17n.org>
parents:
19264
diff
changeset
|
247 (coding-system-unification-table |
7cf3d42a6fd7
(coding-system-unification-table):
Kenichi Handa <handa@m17n.org>
parents:
19264
diff
changeset
|
248 (get coding-system 'coding-system))))) |
7cf3d42a6fd7
(coding-system-unification-table):
Kenichi Handa <handa@m17n.org>
parents:
19264
diff
changeset
|
249 |
7cf3d42a6fd7
(coding-system-unification-table):
Kenichi Handa <handa@m17n.org>
parents:
19264
diff
changeset
|
250 ;;;###autoload |
7cf3d42a6fd7
(coding-system-unification-table):
Kenichi Handa <handa@m17n.org>
parents:
19264
diff
changeset
|
251 (defun coding-system-unification-table-for-encode (coding-system) |
7cf3d42a6fd7
(coding-system-unification-table):
Kenichi Handa <handa@m17n.org>
parents:
19264
diff
changeset
|
252 "Return unification-table-for-encode property of CODING-SYSTEM." |
7cf3d42a6fd7
(coding-system-unification-table):
Kenichi Handa <handa@m17n.org>
parents:
19264
diff
changeset
|
253 (and coding-system |
7cf3d42a6fd7
(coding-system-unification-table):
Kenichi Handa <handa@m17n.org>
parents:
19264
diff
changeset
|
254 (symbolp coding-system) |
7cf3d42a6fd7
(coding-system-unification-table):
Kenichi Handa <handa@m17n.org>
parents:
19264
diff
changeset
|
255 (or (get coding-system 'unification-table-for-encode) |
18200
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
256 (coding-system-unification-table |
c913160e34a7
(set-coding-system-alist): Deleted.
Kenichi Handa <handa@m17n.org>
parents:
17092
diff
changeset
|
257 (get coding-system 'coding-system))))) |
17052 | 258 |
18299
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
259 (defun coding-system-lessp (x y) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
260 (cond ((eq x 'no-conversion) t) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
261 ((eq y 'no-conversion) nil) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
262 ((eq x 'emacs-mule) t) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
263 ((eq y 'emacs-mule) nil) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
264 ((eq x 'undecided) t) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
265 ((eq y 'undecided) nil) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
266 (t (let ((c1 (coding-system-mnemonic x)) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
267 (c2 (coding-system-mnemonic y))) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
268 (or (< (downcase c1) (downcase c2)) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
269 (and (not (> (downcase c1) (downcase c2))) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
270 (< c1 c2))))))) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
271 |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
272 ;;;###autoload |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
273 (defun coding-system-list (&optional base-only) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
274 "Return a list of all existing coding systems. |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
275 If optional arg BASE-ONLY is non-nil, only base coding systems are listed." |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
276 (let (l) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
277 (mapatoms (lambda (x) (if (get x 'coding-system) (setq l (cons x l))))) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
278 (let* ((codings (sort l 'coding-system-lessp)) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
279 (tail (cons nil codings)) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
280 coding) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
281 ;; At first, remove subsidiary coding systems (eol variants) and |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
282 ;; alias coding systems (if necessary). |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
283 (while (cdr tail) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
284 (setq coding (car (cdr tail))) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
285 (if (or (get coding 'eol-variant) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
286 (and base-only (coding-system-parent coding))) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
287 (setcdr tail (cdr (cdr tail))) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
288 (setq tail (cdr tail)))) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
289 codings))) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
290 |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
291 ;;;###autoload |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
292 (defun coding-system-plist (coding-system) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
293 "Return property list of CODING-SYSTEM." |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
294 (let ((found nil) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
295 coding-spec eol-type |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
296 post-read-conversion pre-write-conversion |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
297 unification-table) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
298 (while (not found) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
299 (or eol-type |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
300 (setq eol-type (get coding-system 'eol-type))) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
301 (or post-read-conversion |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
302 (setq post-read-conversion |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
303 (get coding-system 'post-read-conversion))) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
304 (or pre-write-conversion |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
305 (setq pre-write-conversion |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
306 (get coding-system 'pre-write-conversion))) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
307 (or unification-table |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
308 (setq unification-table |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
309 (get coding-system 'unification-table))) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
310 (setq coding-spec (get coding-system 'coding-system)) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
311 (if (and coding-spec (symbolp coding-spec)) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
312 (setq coding-system coding-spec) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
313 (setq found t))) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
314 (if (not coding-spec) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
315 (error "Invalid coding system: %s" coding-system)) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
316 (list 'coding-spec coding-spec |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
317 'eol-type eol-type |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
318 'post-read-conversion post-read-conversion |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
319 'pre-write-conversion pre-write-conversion |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
320 'unification-table unification-table))) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
321 |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
322 ;;;###autoload |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
323 (defun coding-system-equal (coding-system-1 coding-system-2) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
324 "Return t if and only if CODING-SYSTEM-1 and CODING-SYSTEM-2 are identical. |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
325 Two coding systems are identical if two symbols are equal |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
326 or one is an alias of the other." |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
327 (or (eq coding-system-1 coding-system-2) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
328 (equal (coding-system-plist coding-system-1) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
329 (coding-system-plist coding-system-2)))) |
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
330 |
17052 | 331 |
332 ;;; Composite charcater manipulations. | |
333 | |
334 ;;;###autoload | |
335 (defun compose-region (start end) | |
336 "Compose all characters in the current region into one composite character. | |
337 When called from a program, expects two arguments, | |
338 positions (integers or markers) specifying the region." | |
339 (interactive "r") | |
340 (save-excursion | |
341 (let ((str (buffer-substring start end))) | |
342 (goto-char start) | |
343 (delete-region start end) | |
344 (insert (compose-string str))))) | |
345 | |
346 ;;;###autoload | |
347 (defun decompose-region (start end) | |
348 "Decompose all composite characters in the current region. | |
349 Composite characters are broken up into individual components. | |
350 When called from a program, expects two arguments, | |
351 positions (integers or markers) specifying the region." | |
352 (interactive "r") | |
353 (save-restriction | |
354 (narrow-to-region start end) | |
355 (goto-char (point-min)) | |
356 (let ((enable-multibyte-characters nil) | |
357 ;; This matches the whole bytes of single composite character. | |
358 (re-cmpchar "\200[\240-\377]+") | |
359 p ch str) | |
360 (while (re-search-forward re-cmpchar nil t) | |
361 (setq str (buffer-substring (match-beginning 0) (match-end 0))) | |
362 (delete-region (match-beginning 0) (match-end 0)) | |
363 (insert (decompose-composite-char (string-to-char str))))))) | |
364 | |
365 ;;;###autoload | |
19264
6122dbba797f
(prefer-coding-system): Moved to mule-util.el.
Kenichi Handa <handa@m17n.org>
parents:
19056
diff
changeset
|
366 (defun decompose-string (string) |
6122dbba797f
(prefer-coding-system): Moved to mule-util.el.
Kenichi Handa <handa@m17n.org>
parents:
19056
diff
changeset
|
367 "Decompose all composite characters in STRING." |
6122dbba797f
(prefer-coding-system): Moved to mule-util.el.
Kenichi Handa <handa@m17n.org>
parents:
19056
diff
changeset
|
368 (let* ((l (string-to-list string)) |
6122dbba797f
(prefer-coding-system): Moved to mule-util.el.
Kenichi Handa <handa@m17n.org>
parents:
19056
diff
changeset
|
369 (tail l) |
6122dbba797f
(prefer-coding-system): Moved to mule-util.el.
Kenichi Handa <handa@m17n.org>
parents:
19056
diff
changeset
|
370 ch) |
6122dbba797f
(prefer-coding-system): Moved to mule-util.el.
Kenichi Handa <handa@m17n.org>
parents:
19056
diff
changeset
|
371 (while tail |
6122dbba797f
(prefer-coding-system): Moved to mule-util.el.
Kenichi Handa <handa@m17n.org>
parents:
19056
diff
changeset
|
372 (setq ch (car tail)) |
6122dbba797f
(prefer-coding-system): Moved to mule-util.el.
Kenichi Handa <handa@m17n.org>
parents:
19056
diff
changeset
|
373 (setcar tail (if (cmpcharp ch) (decompose-composite-char ch) |
6122dbba797f
(prefer-coding-system): Moved to mule-util.el.
Kenichi Handa <handa@m17n.org>
parents:
19056
diff
changeset
|
374 (char-to-string ch))) |
6122dbba797f
(prefer-coding-system): Moved to mule-util.el.
Kenichi Handa <handa@m17n.org>
parents:
19056
diff
changeset
|
375 (setq tail (cdr tail))) |
6122dbba797f
(prefer-coding-system): Moved to mule-util.el.
Kenichi Handa <handa@m17n.org>
parents:
19056
diff
changeset
|
376 (apply 'concat l))) |
6122dbba797f
(prefer-coding-system): Moved to mule-util.el.
Kenichi Handa <handa@m17n.org>
parents:
19056
diff
changeset
|
377 |
6122dbba797f
(prefer-coding-system): Moved to mule-util.el.
Kenichi Handa <handa@m17n.org>
parents:
19056
diff
changeset
|
378 ;;;###autoload |
17052 | 379 (defconst reference-point-alist |
380 '((tl . 0) (tc . 1) (tr . 2) | |
381 (ml . 3) (mc . 4) (mr . 5) | |
382 (bl . 6) (bc . 7) (br . 8) | |
383 (top-left . 0) (top-center . 1) (top-right . 2) | |
384 (mid-left . 3) (mid-center . 4) (mid-right . 5) | |
385 (bottom-left . 6) (bottom-center . 7) (bottom-right . 8) | |
386 (0 . 0) (1 . 1) (2 . 2) | |
387 (3 . 3) (4 . 4) (5 . 5) | |
388 (6 . 6) (7 . 7) (8 . 8)) | |
389 "Alist of reference point symbols vs reference point codes. | |
19048
65112b3cc989
(reference-point-alist): Doc-string modified.
Kenichi Handa <handa@m17n.org>
parents:
19016
diff
changeset
|
390 A reference point symbol is to be used to specify a composition rule |
65112b3cc989
(reference-point-alist): Doc-string modified.
Kenichi Handa <handa@m17n.org>
parents:
19016
diff
changeset
|
391 while making a composite character by the function `compose-chars' |
65112b3cc989
(reference-point-alist): Doc-string modified.
Kenichi Handa <handa@m17n.org>
parents:
19016
diff
changeset
|
392 (which see). |
65112b3cc989
(reference-point-alist): Doc-string modified.
Kenichi Handa <handa@m17n.org>
parents:
19016
diff
changeset
|
393 |
17052 | 394 Meanings of reference point codes are as follows: |
395 | |
396 0----1----2 <-- ascent 0:tl or top-left | |
397 | | 1:tc or top-center | |
398 | | 2:tr or top-right | |
399 | | 3:ml or mid-left | |
400 | 4 <--+---- center 4:mc or mid-center | |
401 | | 5:mr or mid-right | |
402 --- 3 5 <-- baseline 6:bl or bottom-left | |
403 | | 7:bc or bottom-center | |
404 6----7----8 <-- descent 8:br or bottom-right | |
405 | |
406 Reference point symbols are to be used to specify composition rule of | |
407 the form \(GLOBAL-REF-POINT . NEW-REF-POINT), where GLOBAL-REF-POINT | |
408 is a reference point in the overall glyphs already composed, and | |
409 NEW-REF-POINT is a reference point in the new glyph to be added. | |
410 | |
411 For instance, if GLOBAL-REF-POINT is 8 and NEW-REF-POINT is 1, the | |
412 overall glyph is updated as follows: | |
413 | |
414 +-------+--+ <--- new ascent | |
415 | | | | |
416 | global| | | |
417 | glyph | | | |
418 --- | | | <--- baseline (doesn't change) | |
419 +----+--+--+ | |
420 | | new | | |
421 | |glyph| | |
422 +----+-----+ <--- new descent | |
423 ") | |
424 | |
425 ;; Return a string for char CH to be embedded in multibyte form of | |
426 ;; composite character. | |
427 (defun compose-chars-component (ch) | |
428 (if (< ch 128) | |
429 (format "\240%c" (+ ch 128)) | |
430 (let ((str (char-to-string ch))) | |
431 (if (cmpcharp ch) | |
18299
c6f35cac24b4
(coding-system-parent): New function.
Kenichi Handa <handa@m17n.org>
parents:
18200
diff
changeset
|
432 (substring str (if (= (aref str 1) ?\xFF) 2 1)) |
17052 | 433 (aset str 0 (+ (aref str 0) ?\x20)) |
434 str)))) | |
435 | |
436 ;; Return a string for composition rule RULE to be embedded in | |
437 ;; multibyte form of composite character. | |
438 (defsubst compose-chars-rule (rule) | |
439 (char-to-string (+ ?\xA0 | |
440 (* (cdr (assq (car rule) reference-point-alist)) 9) | |
441 (cdr (assq (cdr rule) reference-point-alist))))) | |
442 | |
443 ;;;###autoload | |
444 (defun compose-chars (first-component &rest args) | |
445 "Return one char string composed from the arguments. | |
446 Each argument is a character (including a composite chararacter) | |
447 or a composition rule. | |
448 A composition rule has the form \(GLOBAL-REF-POINT . NEW-REF-POINT). | |
449 See the documentation of `reference-point-alist' for more detail." | |
450 (if (= (length args) 0) | |
451 (char-to-string first-component) | |
452 (let* ((with-rule (consp (car args))) | |
453 (str (if with-rule (concat (vector leading-code-composition ?\xFF)) | |
454 (char-to-string leading-code-composition)))) | |
455 (setq str (concat str (compose-chars-component first-component))) | |
456 (while args | |
457 (if with-rule | |
458 (progn | |
459 (if (not (consp (car args))) | |
460 (error "Invalid composition rule: %s" (car args))) | |
461 (setq str (concat str (compose-chars-rule (car args)) | |
462 (compose-chars-component (car (cdr args)))) | |
463 args (cdr (cdr args)))) | |
464 (setq str (concat str (compose-chars-component (car args))) | |
465 args (cdr args)))) | |
466 str))) | |
467 | |
468 ;;;###autoload | |
469 (defun decompose-composite-char (char &optional type with-composition-rule) | |
470 "Convert composite character CHAR to a string containing components of CHAR. | |
471 Optional 1st arg TYPE specifies the type of sequence returned. | |
472 It should be `string' (default), `list', or `vector'. | |
473 Optional 2nd arg WITH-COMPOSITION-RULE non-nil means the returned | |
474 sequence contains embedded composition rules if any. In this case, the | |
475 order of elements in the sequence is the same as arguments for | |
476 `compose-chars' to create CHAR. | |
477 If TYPE is omitted or is `string', composition rules are omitted | |
478 even if WITH-COMPOSITION-RULE is t." | |
479 (or type | |
480 (setq type 'string)) | |
481 (let* ((len (composite-char-component-count char)) | |
482 (i (1- len)) | |
483 l) | |
484 (setq with-composition-rule (and with-composition-rule | |
485 (not (eq type 'string)) | |
486 (composite-char-composition-rule-p char))) | |
487 (while (> i 0) | |
488 (setq l (cons (composite-char-component char i) l)) | |
489 (if with-composition-rule | |
490 (let ((rule (- (composite-char-composition-rule char i) ?\xA0))) | |
491 (setq l (cons (cons (/ rule 9) (% rule 9)) l)))) | |
492 (setq i (1- i))) | |
493 (setq l (cons (composite-char-component char 0) l)) | |
494 (cond ((eq type 'string) | |
495 (apply 'concat-chars l)) | |
496 ((eq type 'list) | |
497 l) | |
498 (t ; i.e. TYPE is vector | |
499 (vconcat l))))) | |
500 | |
501 ;;; mule-util.el ends here |