138
|
1 ;; mule-misc.el --- Miscellaneous Mule functions.
|
70
|
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 ;;;
|
|
25 ;;; protect specified local variables from kill-all-local-variables
|
|
26 ;;;
|
|
27
|
|
28 (defvar self-insert-after-hook nil
|
|
29 "Hook to run when extended self insertion command exits. Should take
|
|
30 two arguments START and END corresponding to character position.")
|
|
31
|
|
32 (make-variable-buffer-local 'self-insert-after-hook)
|
|
33
|
|
34 (defun toggle-display-direction ()
|
|
35 (interactive)
|
|
36 (setq display-direction (not display-direction))
|
|
37 (if (interactive-p) (redraw-display)))
|
|
38
|
|
39 ;;;
|
|
40 ;;; Utility functions for Mule
|
|
41 ;;;
|
|
42
|
|
43 ;(defun string-to-char-list (str)
|
|
44 ; (let ((len (length str))
|
|
45 ; (idx 0)
|
|
46 ; c l)
|
|
47 ; (while (< idx len)
|
|
48 ; (setq c (sref str idx))
|
|
49 ; (setq idx (+ idx (charset-dimension (char-charset c))))
|
|
50 ; (setq l (cons c l)))
|
|
51 ; (nreverse l)))
|
|
52
|
|
53 (defun string-to-char-list (str)
|
|
54 (mapcar 'identity str))
|
|
55
|
82
|
56 (defun string-width (string)
|
70
|
57 "Return number of columns STRING occupies when displayed.
|
|
58 Uses the charset-columns attribute of the characters in STRING,
|
|
59 which may not accurately represent the actual display width when
|
|
60 using a window system."
|
|
61 (let ((col 0)
|
|
62 (len (length string))
|
|
63 (i 0))
|
|
64 (while (< i len)
|
|
65 (setq col (+ col (charset-columns (char-charset (aref string i)))))
|
|
66 (setq i (1+ i)))
|
|
67 col))
|
|
68
|
82
|
69 (defalias 'string-columns 'string-width)
|
|
70 (make-obsolete 'string-columns 'string-width)
|
70
|
71
|
|
72 (defun delete-text-in-column (from to)
|
|
73 "Delete the text between column FROM and TO (exclusive) of the current line.
|
|
74 Nil of FORM or TO means the current column.
|
108
|
75
|
|
76 If there's a character across the borders, the character is replaced
|
|
77 with the same width of spaces before deleting."
|
70
|
78 (save-excursion
|
|
79 (let (p1 p2)
|
|
80 (if from
|
|
81 (progn
|
|
82 (setq p1 (move-to-column from))
|
|
83 (if (> p1 from)
|
|
84 (progn
|
|
85 (delete-char -1)
|
|
86 (insert-char ? (- p1 (current-column)))
|
|
87 (forward-char (- from p1))))))
|
|
88 (setq p1 (point))
|
|
89 (if to
|
|
90 (progn
|
|
91 (setq p2 (move-to-column to))
|
|
92 (if (> p2 to)
|
|
93 (progn
|
|
94 (delete-char -1)
|
|
95 (insert-char ? (- p2 (current-column)))
|
|
96 (forward-char (- to p2))))))
|
|
97 (setq p2 (point))
|
|
98 (delete-region p1 p2))))
|
|
99
|
|
100 ;; #### Someone translate this!!
|
|
101
|
|
102 (defun mc-normal-form-string (str)
|
|
103 "文字列 STR の漢字標準形文字列を返す."
|
|
104 (let ((i 0))
|
|
105 (while (setq i (string-match "\n" str i))
|
|
106 (if (and (<= 1 i) (< i (1- (length str)))
|
|
107 (< (aref str (1- i)) 128)
|
|
108 (< (aref str (1+ i)) 128))
|
|
109 (aset str i ? ))
|
|
110 (setq i (1+ i)))
|
|
111 (if (string-match "\n" str 0)
|
|
112 (let ((c 0) (i 0) new)
|
|
113 (while (setq i (string-match "\n" str i))
|
|
114 (setq i (1+ i))
|
|
115 (setq c (1+ c)))
|
|
116 (setq new (make-string (- (length str) c) 0))
|
|
117 (setq i 0 c 0)
|
|
118 (while (< i (length str))
|
|
119 (cond((not (= (aref str i) ?\n ))
|
|
120 (aset new c (aref str i))
|
|
121 (setq c (1+ c))))
|
|
122
|
|
123 (setq i (1+ i))
|
|
124 )
|
|
125 new)
|
|
126 str)))
|
|
127
|
|
128
|
|
129 (defun string-memq (str list)
|
|
130 "Returns non-nil if STR is an element of LIST. Comparison done with string=.
|
|
131 The value is actually the tail of LIST whose car is STR.
|
|
132 If each element of LIST is not a string, it is converted to string
|
|
133 before comparison."
|
|
134 (let (find elm)
|
|
135 (while (and (not find) list)
|
|
136 (setq elm (car list))
|
|
137 (if (numberp elm) (setq elm (char-to-string elm)))
|
|
138 (if (string= str elm)
|
|
139 (setq find list)
|
|
140 (setq list (cdr list))))
|
|
141 find))
|
|
142
|
|
143 (defun cancel-undo-boundary ()
|
|
144 "Cancel undo boundary."
|
|
145 (if (and (consp buffer-undo-list)
|
|
146 ;; if car is nil.
|
|
147 (null (car buffer-undo-list)) )
|
|
148 (setq buffer-undo-list (cdr buffer-undo-list)) ))
|
|
149
|
82
|
150
|
70
|
151 ;;; Common API emulation functions for GNU Emacs-merged Mule.
|
|
152 ;;; As suggested by MORIOKA Tomohiko
|
82
|
153
|
|
154 ;; Following definition were imported from Emacs/mule-delta.
|
|
155
|
|
156 (defun truncate-string-to-width (str width &optional start-column padding)
|
|
157 "Truncate string STR to fit in WIDTH columns.
|
|
158 Optional 1st arg START-COLUMN if non-nil specifies the starting column.
|
|
159 Optional 2nd arg PADDING if non-nil, space characters are padded at
|
|
160 the head and tail of the resulting string to fit in WIDTH if necessary.
|
|
161 If PADDING is nil, the resulting string may be narrower than WIDTH."
|
|
162 (or start-column
|
|
163 (setq start-column 0))
|
|
164 (let ((len (length str))
|
|
165 (idx 0)
|
|
166 (column 0)
|
|
167 (head-padding "") (tail-padding "")
|
|
168 ch last-column last-idx from-idx)
|
|
169 (condition-case nil
|
|
170 (while (< column start-column)
|
|
171 (setq ch (sref str idx)
|
|
172 column (+ column (char-width ch))
|
|
173 idx (+ idx (char-bytes ch))))
|
|
174 (args-out-of-range (setq idx len)))
|
|
175 (if (< column start-column)
|
|
176 (if padding (make-string width ?\ ) "")
|
|
177 (if (and padding (> column start-column))
|
|
178 (setq head-padding (make-string (- column start-column) ?\ )))
|
|
179 (setq from-idx idx)
|
|
180 (condition-case nil
|
|
181 (while (< column width)
|
|
182 (setq last-column column
|
|
183 last-idx idx
|
|
184 ch (sref str idx)
|
|
185 column (+ column (char-width ch))
|
|
186 idx (+ idx (char-bytes ch))))
|
|
187 (args-out-of-range (setq idx len)))
|
|
188 (if (> column width)
|
|
189 (setq column last-column idx last-idx))
|
|
190 (if (and padding (< column width))
|
|
191 (setq tail-padding (make-string (- width column) ?\ )))
|
|
192 (setq str (substring str from-idx idx))
|
|
193 (if padding
|
|
194 (concat head-padding str tail-padding)
|
|
195 str))))
|
|
196
|
|
197 ;;; For backward compatiblity ...
|
|
198 ;;;###autoload
|
|
199 (defalias 'truncate-string 'truncate-string-to-width)
|
|
200 (make-obsolete 'truncate-string 'truncate-string-to-width)
|
|
201
|
|
202 ;; end of imported definition
|
|
203
|
70
|
204
|
|
205 (defalias 'sref 'aref)
|
|
206 (defalias 'map-char-concat 'mapcar)
|
82
|
207 (defun char-bytes (character)
|
|
208 "Return number of length a CHARACTER occupies in a string or buffer.
|
|
209 It returns only 1 in XEmacs. It is for compatibility with MULE 2.3."
|
|
210 1)
|
|
211 (defalias 'char-length 'char-bytes)
|
70
|
212
|
82
|
213 (defun char-width (character)
|
70
|
214 "Return number of columns a CHARACTER occupies when displayed."
|
|
215 (charset-columns (char-charset character)))
|
|
216
|
82
|
217 (defalias 'char-columns 'char-width)
|
|
218 (make-obsolete 'char-columns 'char-width)
|
|
219
|
70
|
220 (defalias 'charset-description 'charset-doc-string)
|
|
221
|
|
222 (defalias 'find-charset-string 'charsets-in-string)
|
|
223 (defalias 'find-charset-region 'charsets-in-region)
|
|
224
|
|
225 (defun find-non-ascii-charset-string (string)
|
|
226 "Return a list of charsets in the STRING except ascii.
|
82
|
227 It might be available for compatibility with Mule 2.3,
|
|
228 because its `find-charset-string' ignores ASCII charset."
|
70
|
229 (delq 'ascii (charsets-in-string string)))
|
|
230
|
|
231 (defun find-non-ascii-charset-region (start end)
|
82
|
232 "Return a list of charsets except ascii in the region between START and END.
|
|
233 It might be available for compatibility with Mule 2.3,
|
|
234 because its `find-charset-string' ignores ASCII charset."
|
70
|
235 (delq 'ascii (charsets-in-region start end)))
|
|
236
|
|
237
|
|
238 ;;; Language environments
|
|
239
|
|
240 (defvar current-language-environment nil)
|
|
241
|
|
242 (defvar language-environment-list nil)
|
|
243
|
|
244 (defun current-language-environment ()
|
|
245 "Return the current language environment as a symbol.
|
|
246 Returns nil if `set-language-environment' has not been called."
|
|
247 current-language-environment)
|
|
248
|
|
249 (defun language-environment-list ()
|
|
250 "Return a list of all currently defined language environments."
|
|
251 language-environment-list)
|
|
252
|
|
253 (defun language-environment-p (sym)
|
|
254 "True if SYM names a defined language environment."
|
|
255 (memq sym (language-environment-list)))
|
|
256
|
|
257 (defun set-language-environment (env)
|
|
258 "Set the current language environment to ENV."
|
|
259 (interactive
|
|
260 (list (intern (completing-read "Language environment: "
|
|
261 obarray 'language-environment-p
|
|
262 'require-match))))
|
|
263 (when (not (string= (charset-registry 'ascii) "ISO8859-1"))
|
|
264 (set-charset-registry 'ascii "ISO8859-1"))
|
|
265 (let ((func (get env 'set-lang-environ)))
|
|
266 (if (not (null func))
|
|
267 (funcall func)))
|
138
|
268 (setq current-language-environment env)
|
|
269 (if (fboundp 'egg)
|
|
270 (egg-lang-switch-callback))
|
|
271 ;; (if (fboundp 'quail)
|
|
272 ;; (quail-lang-switch-callback))
|
|
273 )
|
70
|
274
|
|
275 (defun define-language-environment (env-sym doc-string enable-function)
|
|
276 "Define a new language environment, named by ENV-SYM.
|
|
277 DOC-STRING should be a string describing the environment.
|
|
278 ENABLE-FUNCTION should be a function of no arguments that will be called
|
|
279 when the language environment is made current."
|
|
280 (put env-sym 'lang-environ-doc-string doc-string)
|
|
281 (put env-sym 'set-lang-environ enable-function)
|
|
282 (setq language-environment-list (cons env-sym language-environment-list)))
|
138
|
283
|
|
284 (defun define-egg-environment (env-sym doc-string enable-function)
|
|
285 "Define a new language environment for egg, named by ENV-SYM.
|
|
286 DOC-STRING should be a string describing the environment.
|
|
287 ENABLE-FUNCTION should be a function of no arguments that will be called
|
|
288 when the language environment is made current."
|
|
289 (put env-sym 'egg-environ-doc-string doc-string)
|
|
290 (put env-sym 'set-egg-environ enable-function))
|
|
291
|
|
292 (defun define-quail-environment (env-sym doc-string enable-function)
|
|
293 "Define a new language environment for quail, named by ENV-SYM.
|
|
294 DOC-STRING should be a string describing the environment.
|
|
295 ENABLE-FUNCTION should be a function of no arguments that will be called
|
|
296 when the language environment is made current."
|
|
297 (put env-sym 'quail-environ-doc-string doc-string)
|
|
298 (put env-sym 'set-quail-environ enable-function))
|
|
299
|