comparison lisp/mule/mule-category.el @ 70:131b0175ea99 r20-0b30

Import from CVS: tag r20-0b30
author cvs
date Mon, 13 Aug 2007 09:02:59 +0200
parents
children 54cc21c15cbb
comparison
equal deleted inserted replaced
69:804d1389bcd6 70:131b0175ea99
1 ;;; mule-category.el --- category functions for XEmacs/Mule.
2
3 ;; Copyright (C) 1992,93,94,95 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Amdahl Corporation.
5 ;; Copyright (C) 1995 Sun Microsystems.
6
7 ;; This file is part of XEmacs.
8
9 ;; XEmacs is free software; you can redistribute it and/or modify it
10 ;; under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; XEmacs is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with XEmacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; Functions for working with category tables, which are a particular
27 ;; type of char table. Some function names / arguments should be
28 ;; parallel with syntax tables.
29
30 ;; Written by Ben Wing <wing@666.com>. The initialization code
31 ;; at the end of this file comes from Mule.
32
33 ;;; Code:
34
35 (defvar defined-category-hashtable (make-hashtable 50))
36
37 (defun define-category (designator doc-string)
38 "Make a new category whose designator is DESIGNATOR.
39 DESIGNATOR should be a visible letter of ' ' thru '~'.
40 STRING is a doc string for the category.
41 Letters of 'a' thru 'z' are already used or kept for the system."
42 (check-argument-type 'category-designator-p designator)
43 (check-argument-type 'stringp doc-string)
44 (puthash designator doc-string defined-category-hashtable))
45
46 (defun undefine-category (designator)
47 "Undefine DESIGNATOR as a designator for a category."
48 (check-argument-type 'category-designator-p designator)
49 (remhash designator defined-category-hashtable))
50
51 (defun defined-category-p (designator)
52 "Return non-nil if DESIGNATOR is a designator for a defined category."
53 (and (category-designator-p designator)
54 (gethash designator defined-category-hashtable)))
55
56 (defun defined-category-list ()
57 "Return a list of the currently defined categories.
58 Categories are given by their designators."
59 (let (list)
60 (maphash #'(lambda (key value)
61 (setq list (cons key list)))
62 defined-category-hashtable)
63 (nreverse list)))
64
65 (defun undefined-category-designator ()
66 "Return an undefined category designator, or nil if there are none."
67 (let ((a 32) found)
68 (while (and (< a 127) (not found))
69 (if (gethash a defined-category-hashtable)
70 (setq found a))
71 (setq a (1+ a)))
72 found))
73
74 (defun category-doc-string (designator)
75 "Return the doc-string for the category denoted by DESIGNATOR."
76 (check-argument-type 'defined-category-p designator)
77 (gethash designator defined-category-hashtable))
78
79 (defun modify-category-entry (char-range designator &optional table reset)
80 "Add a category to the categories associated with CHAR-RANGE.
81 CHAR-RANGE is a single character or a range of characters,
82 as per `put-char-table'.
83 The category is given by a designator character.
84 The changes are made in TABLE, which defaults to the current buffer's
85 category table.
86 If optional fourth argument RESET is non-nil, previous categories associated
87 with CHAR-RANGE are removed before adding the specified category."
88 (or table (setq table (category-table)))
89 (check-argument-type 'category-table-p table)
90 (check-argument-type 'defined-category-p designator)
91 (if reset
92 ;; clear all existing stuff.
93 (put-char-table char-range nil table))
94 (map-char-table
95 #'(lambda (key value)
96 ;; make sure that this range has a bit-vector assigned to it,
97 ;; and set the appropriate bit in that vector.
98 (if (not (bit-vector-p value))
99 (progn
100 (setq value (make-bit-vector 95 0))
101 (put-char-table key value table)))
102 (aset value (- designator 32) 1))
103 table char-range))
104
105 (defun char-category-list (char &optional table)
106 "Return a list of the categories that CHAR is in.
107 TABLE defaults to the current buffer's category table.
108 The categories are given by their designators."
109 (or table (setq table (category-table)))
110 (check-argument-type 'category-table-p table)
111 (let ((vec (get-char-table char table)))
112 (if (null vec) nil
113 (let ((a 32) list)
114 (while (< a 127)
115 (if (= 1 (aref vec (- a 32)))
116 (setq list (cons a list)))
117 (setq a (1+ a)))
118 (nreverse list)))))
119
120 (defun char-in-category-p (char category &optional table)
121 "Return non-nil if CHAR is in CATEGORY.
122 TABLE defaults to the current buffer's category table.
123 Categories are specified by their designators."
124 (or table (setq table (category-table)))
125 (check-argument-type 'category-table-p table)
126 (check-argument-type 'category-designator-p category)
127 (let ((vec (get-char-table char table)))
128 (if (null vec) nil
129 (= 1 (aref vec (- category 32))))))
130
131 (defun describe-category ()
132 "Describe the category specifications in the category table.
133 The descriptions are inserted in a buffer, which is then displayed."
134 (interactive)
135 (with-output-to-temp-buffer "*Help*"
136 (describe-category-table (category-table) standard-output)))
137
138 (defun describe-category-table (table stream)
139 (let (first-char
140 last-char
141 prev-val
142 (describe-one
143 (lambda (first last value stream)
144 (if (and (bit-vector-p value)
145 (> (reduce '+ value) 0))
146 (progn
147 (if (equal first last)
148 (cond ((vectorp first)
149 (princ (format "%s, row %d"
150 (charset-name
151 (aref first 0))
152 (aref first 1))
153 stream))
154 ((charsetp first)
155 (princ (charset-name first) stream))
156 (t (princ first stream)))
157 (cond ((vectorp first)
158 (princ (format "%s, rows %d .. %d"
159 (charset-name
160 (aref first 0))
161 (aref first 1)
162 (aref last 1))
163 stream))
164 (t
165 (princ (format "%s .. %s" first last)
166 stream))))
167 (describe-category-code value stream))))))
168 (map-char-table
169 (lambda (range value)
170 (if (and (or
171 (and (characterp range)
172 (characterp first-char)
173 (eq (char-charset range) (char-charset first-char))
174 (= (char-int last-char) (1- (char-int range))))
175 (and (vectorp range)
176 (vectorp first-char)
177 (eq (aref range 0) (aref first-char 0))
178 (= (aref last-char 1) (1- (aref range 1))))
179 (equal value prev-val)))
180 (setq last-char range)
181 (if first-char
182 (progn
183 (funcall describe-one first-char last-char prev-val stream)
184 (setq first-char nil)))
185 (funcall describe-one range range value stream))
186 nil)
187 table)
188 (if first-char
189 (funcall describe-one first-char last-char prev-val stream))))
190
191 (defun describe-category-code (code stream)
192 (let ((standard-output (or stream standard-output)))
193 (princ "\tin categories: ")
194 (if (not (bit-vector-p code))
195 (princ "(none)")
196 (let ((i 0)
197 already-matched)
198 (while (< i 95)
199 (if (= 1 (aref code i))
200 (progn
201 (if (not already-matched)
202 (setq already-matched t)
203 (princ " "))
204 (princ (int-char (+ 32 i)))))
205 (setq i (1+ i)))
206 (if (not already-matched)
207 (princ "(none)")))
208 (let ((i 0))
209 (while (< i 95)
210 (if (= 1 (aref code i))
211 (princ (format "\n\t\tmeaning: %s"
212 (category-doc-string (int-char (+ 32 i))))))
213 (setq i (1+ i)))))
214 (terpri)))
215
216 (defconst predefined-category-list
217 '((latin-1 ?l "Latin-1 through Latin-5 character set")
218 (latin-2 ?l)
219 (latin-3 ?l)
220 (latin-4 ?l)
221 (latin-5 ?l)
222 (cyrillic ?y "Cyrillic character set")
223 (arabic ?b "Arabic character set")
224 (greek ?g "Greek character set")
225 (hebrew ?w "Hebrew character set")
226 (japanese-jisx0201-kana ?k "Japanese 1-byte Katakana character set")
227 (japanese-jisx0201-roman ?r "Japanese 1-byte Roman character set")
228 (japanese-jisx0208-1978 ?j "Japanese 2-byte character set (old)")
229 (japanese-jisx0208 ?j "Japanese 2-byte character set")
230 (japanese-jisx0212 ?j)
231 (chinese-gb ?c "Chinese GB (China, PRC) 2-byte character set")
232 (chinese-cns11643-1 ?t "Chinese Taiwan (CNS or Big5) 2-byte character set")
233 (chinese-cns11643-2 ?t)
234 (chinese-big5-1 ?t)
235 (chinese-big5-2 ?t)
236 (korean-ksc5601 ?h "Hangul (Korean) 2-byte character set")
237 )
238 "List of predefined categories.
239 Each element is a list of a charset, a designator, and maybe a doc string.")
240
241 (let (i l)
242 (define-category ?a "ASCII character set.")
243 (setq i 32)
244 (while (< i 127)
245 (modify-category-entry i ?a)
246 (setq i (1+ i)))
247 (setq l predefined-category-list)
248 (while l
249 (if (nth 2 (car l))
250 (define-category (nth 1 (car l)) (nth 2 (car l))))
251 (modify-category-entry (car (car l)) (nth 1 (car l)))
252 (setq l (cdr l))))
253
254 ;;; At the present, I know Japanese and Chinese text can
255 ;;; break line at any point under a restriction of 'kinsoku'.
256 (defvar word-across-newline "\\(\\cj\\|\\cc\\|\\ct\\)"
257 "Regular expression of such characters which can be a word across newline.")