comparison lisp/international/mule-util.el @ 17052:d0d7b244b1d0

Initial revision
author Karl Heuer <kwzh@gnu.org>
date Thu, 20 Feb 1997 07:02:49 +0000
parents
children 70194012fb3a
comparison
equal deleted inserted replaced
17051:fd0b17a79b07 17052:d0d7b244b1d0
1 ;;; mule-util.el --- Utility functions for mulitilingual environment (mule)
2
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Code:
25
26 ;;; String manipulations while paying attention to multibyte
27 ;;; characters.
28
29 ;;;###autoload
30 (defun string-to-sequence (string type)
31 "Convert STRING to a sequence of TYPE which contains characters in STRING.
32 TYPE should be `list' or `vector'.
33 Multibyte characters are conserned."
34 (or (eq type 'list) (eq type 'vector)
35 (error "Invalid type: %s" type))
36 (let* ((len (length string))
37 (i 0)
38 l ch)
39 (while (< i len)
40 (setq ch (sref string i))
41 (setq l (cons ch l))
42 (setq i (+ i (char-bytes ch))))
43 (setq l (nreverse l))
44 (if (eq type 'list)
45 l
46 (vconcat l))))
47
48 ;;;###autoload
49 (defsubst string-to-list (string)
50 "Return a list of characters in STRING."
51 (string-to-sequence string 'list))
52
53 ;;;###autoload
54 (defsubst string-to-vector (string)
55 "Return a vector of characters in STRING."
56 (string-to-sequence string 'vector))
57
58 ;;;###autoload
59 (defun store-substring (string idx obj)
60 "Embed OBJ (string or character) at index IDX of STRING."
61 (let* ((str (cond ((stringp obj) obj)
62 ((integerp obj) (char-to-string obj))
63 (t (error
64 "Invalid argument (should be string or character): %s"
65 obj))))
66 (string-len (length string))
67 (len (length str))
68 (i 0))
69 (while (and (< i len) (< idx string-len))
70 (aset string idx (aref str i))
71 (setq idx (1+ idx) i (1+ i)))
72 string))
73
74 ;;;###autoload
75 (defun truncate-string-to-width (str width &optional start-column padding)
76 "Truncate string STR to fit in WIDTH columns.
77 Optional 1st arg START-COLUMN if non-nil specifies the starting column.
78 Optional 2nd arg PADDING if non-nil, space characters are padded at
79 the head and tail of the resulting string to fit in WIDTH if necessary.
80 If PADDING is nil, the resulting string may be narrower than WIDTH."
81 (or start-column
82 (setq start-column 0))
83 (let ((len (length str))
84 (idx 0)
85 (column 0)
86 (head-padding "") (tail-padding "")
87 ch last-column last-idx from-idx)
88 (condition-case nil
89 (while (< column start-column)
90 (setq ch (sref str idx)
91 column (+ column (char-width ch))
92 idx (+ idx (char-bytes ch))))
93 (args-out-of-range (setq idx len)))
94 (if (< column start-column)
95 (if padding (make-string width ?\ ) "")
96 (if (and padding (> column start-column))
97 (setq head-padding (make-string (- column start-column) ?\ )))
98 (setq from-idx idx)
99 (condition-case nil
100 (while (< column width)
101 (setq last-column column
102 last-idx idx
103 ch (sref str idx)
104 column (+ column (char-width ch))
105 idx (+ idx (char-bytes ch))))
106 (args-out-of-range (setq idx len)))
107 (if (> column width)
108 (setq column last-column idx last-idx))
109 (if (and padding (< column width))
110 (setq tail-padding (make-string (- width column) ?\ )))
111 (setq str (substring str from-idx idx))
112 (if padding
113 (concat head-padding str tail-padding)
114 str))))
115
116 ;;; For backward compatiblity ...
117 ;;;###autoload
118 (defalias 'truncate-string 'truncate-string-to-width)
119 (make-obsolete 'truncate-string 'truncate-string-to-width)
120
121 ;;; Nested alist handler. Nested alist is alist whose elements are
122 ;;; also nested alist.
123
124 ;;;###autoload
125 (defsubst nested-alist-p (obj)
126 "Return t if OBJ is a nesetd alist.
127
128 Nested alist is a list of the form (ENTRY . BRANCHES), where ENTRY is
129 any Lisp object, and BRANCHES is a list of cons cells of the form
130 (KEY-ELEMENT . NESTED-ALIST).
131
132 You can use a nested alist to store any Lisp object (ENTRY) for a key
133 sequence KEYSEQ, where KEYSEQ is a sequence of KEY-ELEMENT. KEYSEQ
134 can be a string, a vector, or a list."
135 (and obj (listp obj) (listp (cdr obj))))
136
137 ;;;###autoload
138 (defun set-nested-alist (keyseq entry alist &optional len branches)
139 "Set ENTRY for KEYSEQ in a nested alist ALIST.
140 Optional 4th arg LEN non-nil means the firlst LEN elements in KEYSEQ
141 is considered.
142 Optional argument BRANCHES if non-nil is branches for a keyseq
143 longer than KEYSEQ.
144 See the documentation of `nested-alist-p' for more detail."
145 (or (nested-alist-p alist)
146 (error "Invalid arguement %s" alist))
147 (let ((islist (listp keyseq))
148 (len (or len (length keyseq)))
149 (i 0)
150 key-elt slot)
151 (while (< i len)
152 (if (null (nested-alist-p alist))
153 (error "Keyseq %s is too long for this nested alist" keyseq))
154 (setq key-elt (if islist (nth i keyseq) (aref keyseq i)))
155 (setq slot (assoc key-elt (cdr alist)))
156 (if (null slot)
157 (progn
158 (setq slot (cons key-elt (list t)))
159 (setcdr alist (cons slot (cdr alist)))))
160 (setq alist (cdr slot))
161 (setq i (1+ i)))
162 (setcar alist entry)
163 (if branches
164 (if (cdr alist)
165 (error "Can't set branches for keyseq %s" keyseq)
166 (setcdr alist branches)))))
167
168 ;;;###autoload
169 (defun lookup-nested-alist (keyseq alist &optional len start nil-for-too-long)
170 "Look up key sequence KEYSEQ in nested alist ALIST. Return the definition.
171 Optional 1st argument LEN specifies the length of KEYSEQ.
172 Optional 2nd argument START specifies index of the starting key.
173 The returned value is normally a nested alist of which
174 car part is the entry for KEYSEQ.
175 If ALIST is not deep enough for KEYSEQ, return number which is
176 how many key elements at the front of KEYSEQ it takes
177 to reach a leaf in ALIST.
178 Optional 3rd argument NIL-FOR-TOO-LONG non-nil means return nil
179 even if ALIST is not deep enough."
180 (or (nested-alist-p alist)
181 (error "invalid arguement %s" alist))
182 (or len
183 (setq len (length keyseq)))
184 (let ((i (or start 0)))
185 (if (catch 'lookup-nested-alist-tag
186 (if (listp keyseq)
187 (while (< i len)
188 (if (setq alist (cdr (assoc (nth i keyseq) (cdr alist))))
189 (setq i (1+ i))
190 (throw 'lookup-nested-alist-tag t))))
191 (while (< i len)
192 (if (setq alist (cdr (assoc (aref keyseq i) (cdr alist))))
193 (setq i (1+ i))
194 (throw 'lookup-nested-alist-tag t))))
195 ;; KEYSEQ is too long.
196 (if nil-for-too-long nil i)
197 alist)))
198
199 ;; Coding system related functions.
200
201 ;;;###autoload
202 (defun set-coding-system-alist (target-type regexp coding-system
203 &optional operation)
204 "Update `coding-system-alist' according to the arguments.
205 TARGET-TYPE specifies a type of the target: `file', `process', or `network'.
206 TARGET-TYPE tells which slots of coding-system-alist should be affected.
207 If `file', it affects slots for insert-file-contents and write-region.
208 If `process', it affects slots for call-process, call-process-region, and
209 start-process.
210 If `network', it affects a slot for open-network-process.
211 REGEXP is a regular expression matching a target of I/O operation.
212 CODING-SYSTEM is a coding system to perform code conversion
213 on the I/O operation, or a cons of coding systems for decoding and
214 encoding respectively, or a function symbol which returns the cons.
215 Optional arg OPERATION if non-nil specifies directly one of slots above.
216 The valid value is: insert-file-contents, write-region,
217 call-process, call-process-region, start-process, or open-network-stream.
218 If OPERATION is specified, TARGET-TYPE is ignored.
219 See the documentation of `coding-system-alist' for more detail."
220 (or (stringp regexp)
221 (error "Invalid regular expression: %s" regexp))
222 (or (memq target-type '(file process network))
223 (error "Invalid target type: %s" target-type))
224 (if (symbolp coding-system)
225 (if (not (fboundp coding-system))
226 (progn
227 (check-coding-system coding-system)
228 (setq coding-system (cons coding-system coding-system))))
229 (check-coding-system (car coding-system))
230 (check-coding-system (cdr coding-system)))
231 (let ((op-list (if operation (list operation)
232 (cond ((eq target-type 'file)
233 '(insert-file-contents write-region))
234 ((eq target-type 'process)
235 '(call-process call-process-region start-process))
236 (t ; i.e. (eq target-type network)
237 '(open-network-stream)))))
238 slot)
239 (while op-list
240 (setq slot (assq (car op-list) coding-system-alist))
241 (if slot
242 (let ((chain (cdr slot)))
243 (if (catch 'tag
244 (while chain
245 (if (string= regexp (car (car chain)))
246 (progn
247 (setcdr (car chain) coding-system)
248 (throw 'tag nil)))
249 (setq chain (cdr chain)))
250 t)
251 (setcdr slot (cons (cons regexp coding-system) (cdr slot)))))
252 (setq coding-system-alist
253 (cons (cons (car op-list) (list (cons regexp coding-system)))
254 coding-system-alist)))
255 (setq op-list (cdr op-list)))))
256
257 ;;;###autoload
258 (defun coding-system-list ()
259 "Return a list of all existing coding systems."
260 (let (l)
261 (mapatoms (lambda (x) (if (get x 'coding-system) (setq l (cons x l)))))
262 l))
263
264
265 ;;; Composite charcater manipulations.
266
267 ;;;###autoload
268 (defun compose-region (start end)
269 "Compose all characters in the current region into one composite character.
270 When called from a program, expects two arguments,
271 positions (integers or markers) specifying the region."
272 (interactive "r")
273 (save-excursion
274 (let ((str (buffer-substring start end)))
275 (goto-char start)
276 (delete-region start end)
277 (insert (compose-string str)))))
278
279 ;;;###autoload
280 (defun decompose-region (start end)
281 "Decompose all composite characters in the current region.
282 Composite characters are broken up into individual components.
283 When called from a program, expects two arguments,
284 positions (integers or markers) specifying the region."
285 (interactive "r")
286 (save-restriction
287 (narrow-to-region start end)
288 (goto-char (point-min))
289 (let ((enable-multibyte-characters nil)
290 ;; This matches the whole bytes of single composite character.
291 (re-cmpchar "\200[\240-\377]+")
292 p ch str)
293 (while (re-search-forward re-cmpchar nil t)
294 (setq str (buffer-substring (match-beginning 0) (match-end 0)))
295 (delete-region (match-beginning 0) (match-end 0))
296 (insert (decompose-composite-char (string-to-char str)))))))
297
298 ;;;###autoload
299 (defconst reference-point-alist
300 '((tl . 0) (tc . 1) (tr . 2)
301 (ml . 3) (mc . 4) (mr . 5)
302 (bl . 6) (bc . 7) (br . 8)
303 (top-left . 0) (top-center . 1) (top-right . 2)
304 (mid-left . 3) (mid-center . 4) (mid-right . 5)
305 (bottom-left . 6) (bottom-center . 7) (bottom-right . 8)
306 (0 . 0) (1 . 1) (2 . 2)
307 (3 . 3) (4 . 4) (5 . 5)
308 (6 . 6) (7 . 7) (8 . 8))
309 "Alist of reference point symbols vs reference point codes.
310 Meanings of reference point codes are as follows:
311
312 0----1----2 <-- ascent 0:tl or top-left
313 | | 1:tc or top-center
314 | | 2:tr or top-right
315 | | 3:ml or mid-left
316 | 4 <--+---- center 4:mc or mid-center
317 | | 5:mr or mid-right
318 --- 3 5 <-- baseline 6:bl or bottom-left
319 | | 7:bc or bottom-center
320 6----7----8 <-- descent 8:br or bottom-right
321
322 Reference point symbols are to be used to specify composition rule of
323 the form \(GLOBAL-REF-POINT . NEW-REF-POINT), where GLOBAL-REF-POINT
324 is a reference point in the overall glyphs already composed, and
325 NEW-REF-POINT is a reference point in the new glyph to be added.
326
327 For instance, if GLOBAL-REF-POINT is 8 and NEW-REF-POINT is 1, the
328 overall glyph is updated as follows:
329
330 +-------+--+ <--- new ascent
331 | | |
332 | global| |
333 | glyph | |
334 --- | | | <--- baseline (doesn't change)
335 +----+--+--+
336 | | new |
337 | |glyph|
338 +----+-----+ <--- new descent
339 ")
340
341 ;; Return a string for char CH to be embedded in multibyte form of
342 ;; composite character.
343 (defun compose-chars-component (ch)
344 (if (< ch 128)
345 (format "\240%c" (+ ch 128))
346 (let ((str (char-to-string ch)))
347 (if (cmpcharp ch)
348 (if (/= (aref str 1) ?\xFF)
349 (error "Char %c can't be composed" ch)
350 (substring str 2))
351 (aset str 0 (+ (aref str 0) ?\x20))
352 str))))
353
354 ;; Return a string for composition rule RULE to be embedded in
355 ;; multibyte form of composite character.
356 (defsubst compose-chars-rule (rule)
357 (char-to-string (+ ?\xA0
358 (* (cdr (assq (car rule) reference-point-alist)) 9)
359 (cdr (assq (cdr rule) reference-point-alist)))))
360
361 ;;;###autoload
362 (defun compose-chars (first-component &rest args)
363 "Return one char string composed from the arguments.
364 Each argument is a character (including a composite chararacter)
365 or a composition rule.
366 A composition rule has the form \(GLOBAL-REF-POINT . NEW-REF-POINT).
367 See the documentation of `reference-point-alist' for more detail."
368 (if (= (length args) 0)
369 (char-to-string first-component)
370 (let* ((with-rule (consp (car args)))
371 (str (if with-rule (concat (vector leading-code-composition ?\xFF))
372 (char-to-string leading-code-composition))))
373 (setq str (concat str (compose-chars-component first-component)))
374 (while args
375 (if with-rule
376 (progn
377 (if (not (consp (car args)))
378 (error "Invalid composition rule: %s" (car args)))
379 (setq str (concat str (compose-chars-rule (car args))
380 (compose-chars-component (car (cdr args))))
381 args (cdr (cdr args))))
382 (setq str (concat str (compose-chars-component (car args)))
383 args (cdr args))))
384 str)))
385
386 ;;;###autoload
387 (defun decompose-composite-char (char &optional type with-composition-rule)
388 "Convert composite character CHAR to a string containing components of CHAR.
389 Optional 1st arg TYPE specifies the type of sequence returned.
390 It should be `string' (default), `list', or `vector'.
391 Optional 2nd arg WITH-COMPOSITION-RULE non-nil means the returned
392 sequence contains embedded composition rules if any. In this case, the
393 order of elements in the sequence is the same as arguments for
394 `compose-chars' to create CHAR.
395 If TYPE is omitted or is `string', composition rules are omitted
396 even if WITH-COMPOSITION-RULE is t."
397 (or type
398 (setq type 'string))
399 (let* ((len (composite-char-component-count char))
400 (i (1- len))
401 l)
402 (setq with-composition-rule (and with-composition-rule
403 (not (eq type 'string))
404 (composite-char-composition-rule-p char)))
405 (while (> i 0)
406 (setq l (cons (composite-char-component char i) l))
407 (if with-composition-rule
408 (let ((rule (- (composite-char-composition-rule char i) ?\xA0)))
409 (setq l (cons (cons (/ rule 9) (% rule 9)) l))))
410 (setq i (1- i)))
411 (setq l (cons (composite-char-component char 0) l))
412 (cond ((eq type 'string)
413 (apply 'concat-chars l))
414 ((eq type 'list)
415 l)
416 (t ; i.e. TYPE is vector
417 (vconcat l)))))
418
419 ;;; mule-util.el ends here