]> code.delx.au - gnu-emacs/blob - lisp/international/mule-cmds.el
(quail-install-map): New optional arg
[gnu-emacs] / lisp / international / mule-cmds.el
1 ;;; mule-cmds.el --- Commands for mulitilingual environment
2
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 ;;; MULE related key bindings and menus.
28
29 (defvar mule-keymap (make-sparse-keymap)
30 "Keymap for Mule (Multilingual environment) specific commands.")
31
32 ;; Keep "C-x C-m ..." for mule specific commands.
33 (define-key ctl-x-map "\C-m" mule-keymap)
34
35 (define-key mule-keymap "f" 'set-buffer-file-coding-system)
36 (define-key mule-keymap "t" 'set-terminal-coding-system)
37 (define-key mule-keymap "k" 'set-keyboard-coding-system)
38 (define-key mule-keymap "p" 'set-buffer-process-coding-system)
39 (define-key mule-keymap "x" 'set-selection-coding-system)
40 (define-key mule-keymap "X" 'set-next-selection-coding-system)
41 (define-key mule-keymap "\C-\\" 'set-input-method)
42 (define-key mule-keymap "c" 'universal-coding-system-argument)
43 (define-key mule-keymap "l" 'set-language-environment)
44
45 (define-key help-map "\C-L" 'describe-language-environment)
46 (define-key help-map "L" 'describe-language-environment)
47 (define-key help-map "\C-\\" 'describe-input-method)
48 (define-key help-map "I" 'describe-input-method)
49 (define-key help-map "C" 'describe-coding-system)
50 (define-key help-map "h" 'view-hello-file)
51
52 (defvar mule-menu-keymap (make-sparse-keymap "Mule")
53 "Keymap for Mule (Multilingual environment) menu specific commands.")
54
55 (define-key global-map [menu-bar mule]
56 ;; It is better not to use backquote here,
57 ;; because that makes a bootstrapping problem
58 ;; if you need to recompile all the Lisp files using interpreted code.
59 (list 'menu-item "Mule" mule-menu-keymap
60 ':visible 'default-enable-multibyte-characters))
61
62 (setq menu-bar-final-items (cons 'mule menu-bar-final-items))
63
64 (defvar describe-language-environment-map
65 (make-sparse-keymap "Describe Language Environment"))
66
67 (defvar setup-language-environment-map
68 (make-sparse-keymap "Set Language Environment"))
69
70 (defvar set-coding-system-map
71 (make-sparse-keymap "Set Coding System"))
72
73 (define-key-after mule-menu-keymap [describe-language-environment]
74 (cons "Describe Language Environment" describe-language-environment-map)
75 t)
76 (define-key-after mule-menu-keymap [set-language-environment]
77 (cons "Set Language Environment" setup-language-environment-map)
78 t)
79 (define-key-after mule-menu-keymap [mouse-set-font]
80 '("Set Font/Fontset" . mouse-set-font)
81 t)
82 (define-key-after mule-menu-keymap [separator-mule]
83 '("--")
84 t)
85 (define-key-after mule-menu-keymap [toggle-input-method]
86 '("Toggle Input Method" . toggle-input-method)
87 t)
88 (define-key-after mule-menu-keymap [set-input-method]
89 '("Select Input Method" . set-input-method)
90 t)
91 (define-key-after mule-menu-keymap [describe-input-method]
92 '("Describe Input Method" . describe-input-method)
93 t)
94 (define-key-after mule-menu-keymap [separator-input-method]
95 '("--")
96 t)
97 (define-key-after mule-menu-keymap [describe-coding-system]
98 '("Describe Coding Systems" . describe-coding-system)
99 t)
100 (define-key-after mule-menu-keymap [set-various-coding-system]
101 (cons "Set Coding System" set-coding-system-map)
102 t)
103 (define-key-after mule-menu-keymap [separator-coding-system]
104 '("--")
105 t)
106 (define-key-after mule-menu-keymap [mule-diag]
107 '("Show All of Mule Status" . mule-diag)
108 t)
109 (define-key-after mule-menu-keymap [view-hello-file]
110 '("Show Script Examples" . view-hello-file)
111 t)
112
113 (define-key-after set-coding-system-map [set-buffer-file-coding-system]
114 '("Buffer File" . set-buffer-file-coding-system)
115 t)
116 (define-key-after set-coding-system-map [universal-coding-system-argument]
117 '("Next Command" . universal-coding-system-argument)
118 t)
119 (define-key-after set-coding-system-map [set-terminal-coding-system]
120 '("Terminal" . set-terminal-coding-system)
121 t)
122 (define-key-after set-coding-system-map [set-keyboard-coding-system]
123 '("Keyboard" . set-keyboard-coding-system)
124 t)
125 (define-key-after set-coding-system-map [set-buffer-process-coding-system]
126 '("Buffer Process" . set-buffer-process-coding-system)
127 t)
128 (define-key-after set-coding-system-map [set-selection-coding-system]
129 '("X Selection" . set-selection-coding-system)
130 t)
131 (define-key-after set-coding-system-map [set-next-selection-coding-system]
132 '("Next X Selection" . set-next-selection-coding-system)
133 t)
134 (define-key setup-language-environment-map
135 [Default] '("Default" . setup-specified-language-environment))
136
137 ;; These are meaningless when running under X and W32.
138 (put 'set-terminal-coding-system 'menu-enable
139 '(or (not window-system) (eq window-system 'pc)))
140 (put 'set-keyboard-coding-system 'menu-enable
141 '(or (not window-system) (eq window-system 'pc)))
142 ;; This is meaningless when the current buffer has no process.
143 (put 'set-buffer-process-coding-system 'menu-enable
144 '(get-buffer-process (current-buffer)))
145 ;; These are meaningless when running under terminal.
146 (put 'set-selection-coding-system 'menu-enable
147 'window-system)
148 (put 'set-next-selection-coding-system 'menu-enable
149 'window-system)
150
151 ;; This should be a single character key binding because users use it
152 ;; very frequently while editing multilingual text. Now we can use
153 ;; only two such keys: "\C-\\" and "\C-^", but the latter is not
154 ;; convenient because it requires shifting on most keyboards. An
155 ;; alternative is "\C-\]" which is now bound to `abort-recursive-edit'
156 ;; but it won't be used that frequently.
157 (define-key global-map "\C-\\" 'toggle-input-method)
158
159 ;;; This is no good because people often type Shift-SPC
160 ;;; meaning to type SPC. -- rms.
161 ;;; ;; Here's an alternative key binding for X users (Shift-SPACE).
162 ;;; (define-key global-map [?\S- ] 'toggle-input-method)
163
164 (defun coding-system-change-eol-conversion (coding-system eol-type)
165 "Return a coding system which differs from CODING-SYSTEM in eol conversion.
166 The returned coding system converts end-of-line by EOL-TYPE
167 but text as the same way as CODING-SYSTEM.
168 EOL-TYPE should be `unix', `dos', `mac', or nil.
169 If EOL-TYPE is nil, the returned coding system detects
170 how end-of-line is formatted automatically while decoding.
171
172 EOL-TYPE can be specified by an integer 0, 1, or 2.
173 They means `unix', `dos', and `mac' respectively."
174 (if (symbolp eol-type)
175 (setq eol-type (cond ((eq eol-type 'unix) 0)
176 ((eq eol-type 'dos) 1)
177 ((eq eol-type 'mac) 2)
178 (t eol-type))))
179 (let ((orig-eol-type (coding-system-eol-type coding-system)))
180 (if (vectorp orig-eol-type)
181 (if (not eol-type)
182 coding-system
183 (aref orig-eol-type eol-type))
184 (let ((base (coding-system-base coding-system)))
185 (if (not eol-type)
186 base
187 (if (= eol-type orig-eol-type)
188 coding-system
189 (setq orig-eol-type (coding-system-eol-type base))
190 (if (vectorp orig-eol-type)
191 (aref orig-eol-type eol-type))))))))
192
193 (defun coding-system-change-text-conversion (coding-system coding)
194 "Return a coding system which differs from CODING-SYSTEM in text conversion.
195 The returned coding system converts text by CODING
196 but end-of-line as the same way as CODING-SYSTEM.
197 If CODING is nil, the returned coding system detects
198 how text is formatted automatically while decoding."
199 (if (not coding)
200 (coding-system-base coding-system)
201 (let ((eol-type (coding-system-eol-type coding-system)))
202 (coding-system-change-eol-conversion
203 coding
204 (if (numberp eol-type) (aref [unix dos mac] eol-type))))))
205
206 (defun toggle-enable-multibyte-characters (&optional arg)
207 "Change whether this buffer uses multibyte characters.
208 With arg, use multibyte characters if the arg is positive.
209
210 Note that this command does not convert the byte contents of
211 the buffer; it only changes the way those bytes are interpreted.
212 In general, therefore, this command *changes* the sequence of
213 characters that the current buffer contains.
214
215 We suggest you avoid using use this command unless you know what you
216 are doing. If you use it by mistake, and the buffer is now displayed
217 wrong, use this command again to toggle back to the right mode."
218 (interactive "P")
219 (let ((new-flag
220 (if (null arg) (null enable-multibyte-characters)
221 (> (prefix-numeric-value arg) 0))))
222 (set-buffer-multibyte new-flag))
223 (force-mode-line-update))
224
225 (defun view-hello-file ()
226 "Display the HELLO file which list up many languages and characters."
227 (interactive)
228 ;; We have to decode the file in any environment.
229 (let ((default-enable-multibyte-characters t)
230 (coding-system-for-read 'iso-2022-7bit))
231 (find-file-read-only (expand-file-name "HELLO" data-directory))))
232
233 (defun universal-coding-system-argument ()
234 "Execute an I/O command using the specified coding system."
235 (interactive)
236 (let* ((default (and buffer-file-coding-system
237 (not (eq (coding-system-type buffer-file-coding-system)
238 t))
239 buffer-file-coding-system))
240 (coding-system (read-coding-system
241 (if default
242 (format "Coding system for following command (default, %s): " default)
243 "Coding system for following command: ")
244 default))
245 (keyseq (read-key-sequence
246 (format "Command to execute with %s:" coding-system)))
247 (cmd (key-binding keyseq)))
248 (let ((coding-system-for-read coding-system)
249 (coding-system-for-write coding-system))
250 (message "")
251 (call-interactively cmd))))
252
253 (defun set-default-coding-systems (coding-system)
254 "Set default value of various coding systems to CODING-SYSTEM.
255 This sets the following coding systems:
256 o coding system of a newly created buffer
257 o default coding system for subprocess I/O
258 This also sets the following values:
259 o default value used as file-name-coding-system for converting file names.
260 o default value for the command `set-terminal-coding-system' (not on MSDOS)
261 o default value for the command `set-keyboard-coding-system'."
262 (check-coding-system coding-system)
263 (setq-default buffer-file-coding-system coding-system)
264 (if default-enable-multibyte-characters
265 (setq default-file-name-coding-system coding-system))
266 ;; If coding-system is nil, honor that on MS-DOS as well, so
267 ;; that they could reset the terminal coding system.
268 (unless (and (eq window-system 'pc) coding-system)
269 (setq default-terminal-coding-system coding-system))
270 (setq default-keyboard-coding-system coding-system)
271 (setq default-process-coding-system (cons coding-system coding-system)))
272
273 (defalias 'update-iso-coding-systems 'update-coding-systems-internal)
274 (make-obsolete 'update-iso-coding-systems 'update-coding-systems-internal)
275
276 (defun prefer-coding-system (coding-system)
277 "Add CODING-SYSTEM at the front of the priority list for automatic detection.
278 This also sets the following coding systems:
279 o coding system of a newly created buffer
280 o default coding system for subprocess I/O
281 This also sets the following values:
282 o default value used as file-name-coding-system for converting file names.
283 o default value for the command `set-terminal-coding-system' (not on MSDOS)
284 o default value for the command `set-keyboard-coding-system'
285
286 If CODING-SYSTEM specifies a certain type of EOL conversion, the coding
287 systems set by this function will use that type of EOL conversion.
288
289 This command does not change the default value of terminal coding system
290 for MS-DOS terminal, because DOS terminals only support a single coding
291 system, and Emacs automatically sets the default to that coding system at
292 startup."
293 (interactive "zPrefer coding system: ")
294 (if (not (and coding-system (coding-system-p coding-system)))
295 (error "Invalid coding system `%s'" coding-system))
296 (let ((coding-category (coding-system-category coding-system))
297 (base (coding-system-base coding-system))
298 (eol-type (coding-system-eol-type coding-system)))
299 (if (not coding-category)
300 ;; CODING-SYSTEM is no-conversion or undecided.
301 (error "Can't prefer the coding system `%s'" coding-system))
302 (set coding-category (or base coding-system))
303 (update-coding-systems-internal)
304 (or (eq coding-category (car coding-category-list))
305 ;; We must change the order.
306 (set-coding-priority (list coding-category)))
307 (if (and base (interactive-p))
308 (message "Highest priority is set to %s (base of %s)"
309 base coding-system))
310 ;; If they asked for specific EOL conversion, honor that.
311 (if (memq eol-type '(0 1 2))
312 (setq coding-system
313 (coding-system-change-eol-conversion base eol-type))
314 (setq coding-system base))
315 (set-default-coding-systems coding-system)))
316
317 (defun find-coding-systems-region-subset-p (list1 list2)
318 "Return non-nil if all elements in LIST1 are included in LIST2.
319 Comparison done with EQ."
320 (catch 'tag
321 (while list1
322 (or (memq (car list1) list2)
323 (throw 'tag nil))
324 (setq list1 (cdr list1)))
325 t))
326
327 (defun find-coding-systems-region (from to)
328 "Return a list of proper coding systems to encode a text between FROM and TO.
329 All coding systems in the list can safely encode any multibyte characters
330 in the text.
331
332 If the text contains no multibyte characters, return a list of a single
333 element `undecided'."
334 (find-coding-systems-for-charsets (find-charset-region from to)))
335
336 (defun find-coding-systems-string (string)
337 "Return a list of proper coding systems to encode STRING.
338 All coding systems in the list can safely encode any multibyte characters
339 in STRING.
340
341 If STRING contains no multibyte characters, return a list of a single
342 element `undecided'."
343 (find-coding-systems-for-charsets (find-charset-string string)))
344
345 (defun find-coding-systems-for-charsets (charsets)
346 "Return a list of proper coding systems to encode characters of CHARSETS.
347 CHARSETS is a list of character sets."
348 (if (or (null charsets)
349 (and (= (length charsets) 1)
350 (eq 'ascii (car charsets))))
351 '(undecided)
352 (setq charsets (delq 'composition charsets))
353 (let ((l (coding-system-list 'base-only))
354 (charset-preferred-codings
355 (mapcar (function
356 (lambda (x)
357 (if (eq x 'unknown)
358 'raw-text
359 (get-charset-property x 'preferred-coding-system))))
360 charsets))
361 (priorities (mapcar (function (lambda (x) (symbol-value x)))
362 coding-category-list))
363 codings coding safe)
364 (if (memq 'unknown charsets)
365 ;; The region contains invalid multibyte characters.
366 (setq l '(raw-text)))
367 (while l
368 (setq coding (car l) l (cdr l))
369 (if (and (setq safe (coding-system-get coding 'safe-charsets))
370 (or (eq safe t)
371 (find-coding-systems-region-subset-p charsets safe)))
372 ;; We put the higher priority to coding systems included
373 ;; in CHARSET-PREFERRED-CODINGS, and within them, put the
374 ;; higher priority to coding systems which support smaller
375 ;; number of charsets.
376 (let ((priority
377 (+ (if (coding-system-get coding 'mime-charset) 4096 0)
378 (lsh (length (memq coding priorities)) 7)
379 (if (memq coding charset-preferred-codings) 64 0)
380 (if (> (coding-system-type coding) 0) 32 0)
381 (if (consp safe) (- 32 (length safe)) 0))))
382 (setq codings (cons (cons priority coding) codings)))))
383 (mapcar 'cdr
384 (sort codings (function (lambda (x y) (> (car x) (car y))))))
385 )))
386
387 (defun find-multibyte-characters (from to &optional maxcount excludes)
388 "Find multibyte characters in the region specified by FROM and TO.
389 If FROM is a string, find multibyte characters in the string.
390 The return value is an alist of the following format:
391 ((CHARSET COUNT CHAR ...) ...)
392 where
393 CHARSET is a character set,
394 COUNT is a number of characters,
395 CHARs are found characters of the character set.
396 Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
397 Optional 4th arg EXCLUDE is a list of character sets to be ignored.
398
399 For invalid characters, CHARs are actually strings."
400 (let ((chars nil)
401 charset char)
402 (if (stringp from)
403 (let ((idx 0))
404 (while (setq idx (string-match "[^\000-\177]" from idx))
405 (setq char (aref from idx)
406 charset (char-charset char))
407 (if (eq charset 'unknown)
408 (setq char (match-string 0)))
409 (if (or (eq charset 'unknown)
410 (not (or (eq excludes t) (memq charset excludes))))
411 (let ((slot (assq charset chars)))
412 (if slot
413 (if (not (memq char (nthcdr 2 slot)))
414 (let ((count (nth 1 slot)))
415 (setcar (cdr slot) (1+ count))
416 (if (or (not maxcount) (< count maxcount))
417 (nconc slot (list char)))))
418 (setq chars (cons (list charset 1 char) chars)))))
419 (setq idx (1+ idx))))
420 (save-excursion
421 (goto-char from)
422 (while (re-search-forward "[^\000-\177]" to t)
423 (setq char (preceding-char)
424 charset (char-charset char))
425 (if (eq charset 'unknown)
426 (setq char (match-string 0)))
427 (if (or (eq charset 'unknown)
428 (not (or (eq excludes t) (memq charset excludes))))
429 (let ((slot (assq charset chars)))
430 (if slot
431 (if (not (member char (nthcdr 2 slot)))
432 (let ((count (nth 1 slot)))
433 (setcar (cdr slot) (1+ count))
434 (if (or (not maxcount) (< count maxcount))
435 (nconc slot (list char)))))
436 (setq chars (cons (list charset 1 char) chars))))))))
437 (nreverse chars)))
438
439 (defvar last-coding-system-specified nil
440 "Most recent coding system explicitly specified by the user when asked.
441 This variable is set whenever Emacs asks the user which coding system
442 to use in order to write a file. If you set it to nil explicitly,
443 then call `write-region', then afterward this variable will be non-nil
444 only if the user was explicitly asked and specified a coding system.")
445
446 (defun select-safe-coding-system (from to &optional default-coding-system)
447 "Ask a user to select a safe coding system from candidates.
448 The candidates of coding systems which can safely encode a text
449 between FROM and TO are shown in a popup window.
450
451 Optional arg DEFAULT-CODING-SYSTEM specifies a coding system to be
452 checked at first. If omitted, buffer-file-coding-system of the
453 current buffer is used.
454
455 If the text can be encoded safely by DEFAULT-CODING-SYSTEM, it is
456 returned without any user interaction.
457
458 Kludgy feature: if FROM is a string, the string is the target text,
459 and TO is ignored."
460 (or default-coding-system
461 (setq default-coding-system buffer-file-coding-system))
462 (let* ((charsets (if (stringp from) (find-charset-string from)
463 (find-charset-region from to)))
464 (safe-coding-systems (find-coding-systems-for-charsets charsets)))
465 (if (or (not enable-multibyte-characters)
466 (eq (car safe-coding-systems) 'undecided)
467 (eq default-coding-system 'no-conversion)
468 (and default-coding-system
469 (memq (coding-system-base default-coding-system)
470 safe-coding-systems)))
471 default-coding-system
472
473 ;; At first, change each coding system to the corresponding
474 ;; mime-charset name if it is also a coding system.
475 (let ((l safe-coding-systems)
476 mime-charset)
477 (while l
478 (setq mime-charset (coding-system-get (car l) 'mime-charset))
479 (if (and mime-charset (coding-system-p mime-charset))
480 (setcar l mime-charset))
481 (setq l (cdr l))))
482
483 (let ((non-safe-chars (find-multibyte-characters
484 from to 3
485 (and default-coding-system
486 (coding-system-get default-coding-system
487 'safe-charsets))))
488 show-position overlays)
489 (save-excursion
490 ;; Highlight characters that default-coding-system can't encode.
491 (when (integerp from)
492 (goto-char from)
493 (let ((found nil))
494 (while (and (not found)
495 (re-search-forward "[^\000-\177]" to t))
496 (setq found (assq (char-charset (preceding-char))
497 non-safe-chars))))
498 (forward-line -1)
499 (setq show-position (point))
500 (save-excursion
501 (while (and (< (length overlays) 256)
502 (re-search-forward "[^\000-\177]" to t))
503 (let* ((char (preceding-char))
504 (charset (char-charset char)))
505 (when (assq charset non-safe-chars)
506 (setq overlays (cons (make-overlay (1- (point)) (point))
507 overlays))
508 (overlay-put (car overlays) 'face 'highlight))))))
509
510 ;; At last, ask a user to select a proper coding system.
511 (unwind-protect
512 (save-window-excursion
513 (when show-position
514 ;; At first, be sure to show the current buffer.
515 (set-window-buffer (selected-window) (current-buffer))
516 (set-window-start (selected-window) show-position))
517 ;; Then, show a helpful message.
518 (with-output-to-temp-buffer "*Warning*"
519 (save-excursion
520 (set-buffer standard-output)
521 (insert "The target text contains the following non ASCII character(s):\n")
522 (let ((len (length non-safe-chars))
523 (shown 0))
524 (while (and non-safe-chars (< shown 3))
525 (when (> (length (car non-safe-chars)) 2)
526 (setq shown (1+ shown))
527 (insert (format "%25s: " (car (car non-safe-chars))))
528 (let ((l (nthcdr 2 (car non-safe-chars))))
529 (while l
530 (if (or (stringp (car l)) (char-valid-p (car l)))
531 (insert (car l)))
532 (setq l (cdr l))))
533 (if (> (nth 1 (car non-safe-chars)) 3)
534 (insert "..."))
535 (insert "\n"))
536 (setq non-safe-chars (cdr non-safe-chars)))
537 (if (< shown len)
538 (insert (format "%27s\n" "..."))))
539 (insert (format
540 "These can't be encoded safely by the coding system %s.
541
542 Please select one from the following safe coding systems:\n"
543 default-coding-system))
544 (let ((pos (point))
545 (fill-prefix " "))
546 (mapcar (function (lambda (x) (princ " ") (princ x)))
547 safe-coding-systems)
548 (fill-region-as-paragraph pos (point)))))
549
550 ;; Read a coding system.
551 (let* ((safe-names (mapcar (lambda (x) (list (symbol-name x)))
552 safe-coding-systems))
553 (name (completing-read
554 (format "Select coding system (default %s): "
555 (car safe-coding-systems))
556 safe-names nil t nil nil
557 (car (car safe-names)))))
558 (setq last-coding-system-specified (intern name))
559 (if (integerp (coding-system-eol-type default-coding-system))
560 (setq last-coding-system-specified
561 (coding-system-change-eol-conversion
562 last-coding-system-specified
563 (coding-system-eol-type default-coding-system))))
564 last-coding-system-specified))
565 (kill-buffer "*Warning*")
566 (while overlays
567 (delete-overlay (car overlays))
568 (setq overlays (cdr overlays)))))))))
569
570 (setq select-safe-coding-system-function 'select-safe-coding-system)
571
572 (defun select-message-coding-system ()
573 "Return a coding system to encode the outgoing message of the current buffer.
574 It at first tries the first coding system found in these variables
575 in this order:
576 (1) local value of `buffer-file-coding-system'
577 (2) value of `sendmail-coding-system'
578 (3) value of `default-buffer-file-coding-system'
579 (4) value of `default-sendmail-coding-system'
580 If the found coding system can't encode the current buffer,
581 or none of them are bound to a coding system,
582 it asks the user to select a proper coding system."
583 (let ((coding (or (and (local-variable-p 'buffer-file-coding-system)
584 buffer-file-coding-system)
585 sendmail-coding-system
586 default-buffer-file-coding-system
587 default-sendmail-coding-system)))
588 (if (eq coding 'no-conversion)
589 ;; We should never use no-conversion for outgoing mails.
590 (setq coding nil))
591 (if (fboundp select-safe-coding-system-function)
592 (funcall select-safe-coding-system-function
593 (point-min) (point-max) coding)
594 coding)))
595 \f
596 ;;; Language support stuff.
597
598 (defvar language-info-alist nil
599 "Alist of language environment definitions.
600 Each element looks like:
601 (LANGUAGE-NAME . ((KEY . INFO) ...))
602 where LANGUAGE-NAME is a string, the name of the language environment,
603 KEY is a symbol denoting the kind of information, and
604 INFO is the data associated with KEY.
605 Meaningful values for KEY include
606
607 documentation value is documentation of what this language environment
608 is meant for, and how to use it.
609 charset value is a list of the character sets used by this
610 language environment.
611 sample-text value is one line of text,
612 written using those character sets,
613 appropriate for this language environment.
614 setup-function value is a function to call to switch to this
615 language environment.
616 exit-function value is a function to call to leave this
617 language environment.
618 coding-system value is a list of coding systems that are good
619 for saving text written in this language environment.
620 This list serves as suggestions to the user;
621 in effect, as a kind of documentation.
622 coding-priority value is a list of coding systems for this language
623 environment, in order of decreasing priority.
624 This is used to set up the coding system priority
625 list when you switch to this language environment.
626 nonascii-translation
627 value is a translation table to be set in the
628 variable `nonascii-translation-table' in this
629 language environment, or a character set from
630 which `nonascii-insert-offset' is calculated.
631 input-method value is a default input method for this language
632 environment.
633 features value is a list of features requested in this
634 language environment.
635
636 The following keys take effect only when multibyte characters are
637 globally disabled, i.e. the value of `default-enable-multibyte-characters'
638 is nil.
639
640 unibyte-syntax value is a library name to load to set
641 unibyte 8-bit character syntaxes for this
642 language environment.
643
644 unibyte-display value is a coding system to encode characters
645 for the terminal. Characters in the range
646 of 160 to 255 display not as octal escapes,
647 but as non-ASCII characters in this language
648 environment.")
649
650 (defun get-language-info (lang-env key)
651 "Return information listed under KEY for language environment LANG-ENV.
652 KEY is a symbol denoting the kind of information.
653 For a list of useful values for KEY and their meanings,
654 see `language-info-alist'."
655 (if (symbolp lang-env)
656 (setq lang-env (symbol-name lang-env)))
657 (let ((lang-slot (assoc-ignore-case lang-env language-info-alist)))
658 (if lang-slot
659 (cdr (assq key (cdr lang-slot))))))
660
661 (defun set-language-info (lang-env key info)
662 "Modify part of the definition of language environment LANG-ENV.
663 Specifically, this stores the information INFO under KEY
664 in the definition of this language environment.
665 KEY is a symbol denoting the kind of information.
666 INFO is the value for that information.
667
668 For a list of useful values for KEY and their meanings,
669 see `language-info-alist'."
670 (if (symbolp lang-env)
671 (setq lang-env (symbol-name lang-env)))
672 (let (lang-slot key-slot)
673 (setq lang-slot (assoc lang-env language-info-alist))
674 (if (null lang-slot) ; If no slot for the language, add it.
675 (setq lang-slot (list lang-env)
676 language-info-alist (cons lang-slot language-info-alist)))
677 (setq key-slot (assq key lang-slot))
678 (if (null key-slot) ; If no slot for the key, add it.
679 (progn
680 (setq key-slot (list key))
681 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
682 (setcdr key-slot info)))
683
684 (defun set-language-info-alist (lang-env alist &optional parents)
685 "Store ALIST as the definition of language environment LANG-ENV.
686 ALIST is an alist of KEY and INFO values. See the documentation of
687 `set-language-info' for the meanings of KEY and INFO.
688
689 Optional arg PARENTS is a list of parent menu names; it specifies
690 where to put this language environment in the
691 Describe Language Environment and Set Language Environment menus.
692 For example, (\"European\") means to put this language environment
693 in the European submenu in each of those two menus."
694 (if (symbolp lang-env)
695 (setq lang-env (symbol-name lang-env)))
696 (let ((describe-map describe-language-environment-map)
697 (setup-map setup-language-environment-map))
698 (if parents
699 (let ((l parents)
700 map parent-symbol parent)
701 (while l
702 (if (symbolp (setq parent-symbol (car l)))
703 (setq parent (symbol-name parent))
704 (setq parent parent-symbol parent-symbol (intern parent)))
705 (setq map (lookup-key describe-map (vector parent-symbol)))
706 (if (not map)
707 (progn
708 (setq map (intern (format "describe-%s-environment-map"
709 (downcase parent))))
710 (define-prefix-command map)
711 (define-key-after describe-map (vector parent-symbol)
712 (cons parent map) t)))
713 (setq describe-map (symbol-value map))
714 (setq map (lookup-key setup-map (vector parent-symbol)))
715 (if (not map)
716 (progn
717 (setq map (intern (format "setup-%s-environment-map"
718 (downcase parent))))
719 (define-prefix-command map)
720 (define-key-after setup-map (vector parent-symbol)
721 (cons parent map) t)))
722 (setq setup-map (symbol-value map))
723 (setq l (cdr l)))))
724
725 ;; Set up menu items for this language env.
726 (let ((doc (assq 'documentation alist)))
727 (when doc
728 (define-key-after describe-map (vector (intern lang-env))
729 (cons lang-env 'describe-specified-language-support) t)))
730 (define-key-after setup-map (vector (intern lang-env))
731 (cons lang-env 'setup-specified-language-environment) t)
732
733 (while alist
734 (set-language-info lang-env (car (car alist)) (cdr (car alist)))
735 (setq alist (cdr alist)))))
736
737 (defun read-language-name (key prompt &optional default)
738 "Read a language environment name which has information for KEY.
739 If KEY is nil, read any language environment.
740 Prompt with PROMPT. DEFAULT is the default choice of language environment.
741 This returns a language environment name as a string."
742 (let* ((completion-ignore-case t)
743 (name (completing-read prompt
744 language-info-alist
745 (and key
746 (function (lambda (elm) (assq key elm))))
747 t nil nil default)))
748 (if (and (> (length name) 0)
749 (or (not key)
750 (get-language-info name key)))
751 name)))
752 \f
753 ;;; Multilingual input methods.
754
755 (defconst leim-list-file-name "leim-list.el"
756 "Name of LEIM list file.
757 This file contains a list of libraries of Emacs input methods (LEIM)
758 in the format of Lisp expression for registering each input method.
759 Emacs loads this file at startup time.")
760
761 (defvar leim-list-header (format
762 ";;; %s -- list of LEIM (Library of Emacs Input Method)
763 ;;
764 ;; This file contains a list of LEIM (Library of Emacs Input Method)
765 ;; in the same directory as this file. Loading this file registers
766 ;; the whole input methods in Emacs.
767 ;;
768 ;; Each entry has the form:
769 ;; (register-input-method
770 ;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
771 ;; TITLE DESCRIPTION
772 ;; ARG ...)
773 ;; See the function `register-input-method' for the meanings of arguments.
774 ;;
775 ;; If this directory is included in load-path, Emacs automatically
776 ;; loads this file at startup time.
777
778 "
779 leim-list-file-name)
780 "Header to be inserted in LEIM list file.")
781
782 (defvar leim-list-entry-regexp "^(register-input-method"
783 "Regexp matching head of each entry in LEIM list file.
784 See also the variable `leim-list-header'")
785
786 (defvar update-leim-list-functions
787 '(quail-update-leim-list-file)
788 "List of functions to call to update LEIM list file.
789 Each function is called with one arg, LEIM directory name.")
790
791 (defun update-leim-list-file (&rest dirs)
792 "Update LEIM list file in directories DIRS."
793 (let ((functions update-leim-list-functions))
794 (while functions
795 (apply (car functions) dirs)
796 (setq functions (cdr functions)))))
797
798 (defvar current-input-method nil
799 "The current input method for multilingual text.
800 If nil, that means no input method is activated now.")
801 (make-variable-buffer-local 'current-input-method)
802 (put 'current-input-method 'permanent-local t)
803
804 (defvar current-input-method-title nil
805 "Title string of the current input method shown in mode line.")
806 (make-variable-buffer-local 'current-input-method-title)
807 (put 'current-input-method-title 'permanent-local t)
808
809 (defcustom default-input-method nil
810 "*Default input method for multilingual text (a string).
811 This is the input method activated automatically by the command
812 `toggle-input-method' (\\[toggle-input-method])."
813 :group 'mule
814 :type '(choice (const nil) string)
815 :set-after '(current-language-environment))
816
817 (put 'input-method-function 'permanent-local t)
818
819 (defvar input-method-history nil
820 "History list for some commands that read input methods.")
821 (make-variable-buffer-local 'input-method-history)
822 (put 'input-method-history 'permanent-local t)
823
824 (defvar inactivate-current-input-method-function nil
825 "Function to call for inactivating the current input method.
826 Every input method should set this to an appropriate value when activated.
827 This function is called with no argument.
828
829 This function should never change the value of `current-input-method'.
830 It is set to nil by the function `inactivate-input-method'.")
831 (make-variable-buffer-local 'inactivate-current-input-method-function)
832 (put 'inactivate-current-input-method-function 'permanent-local t)
833
834 (defvar describe-current-input-method-function nil
835 "Function to call for describing the current input method.
836 This function is called with no argument.")
837 (make-variable-buffer-local 'describe-current-input-method-function)
838 (put 'describe-current-input-method-function 'permanent-local t)
839
840 (defvar input-method-alist nil
841 "Alist of input method names vs how to use them.
842 Each element has the form:
843 (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
844 See the function `register-input-method' for the meanings of the elements.")
845
846 (defun register-input-method (input-method lang-env &rest args)
847 "Register INPUT-METHOD as an input method for language environment ENV.
848 INPUT-METHOD and LANG-ENV are symbols or strings.
849
850 The remaining arguments are:
851 ACTIVATE-FUNC, TITLE, DESCRIPTION, and ARGS...
852 ACTIVATE-FUNC is a function to call to activate this method.
853 TITLE is a string to show in the mode line when this method is active.
854 DESCRIPTION is a string describing this method and what it is good for.
855 The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
856 All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
857
858 This function is mainly used in the file \"leim-list.el\" which is
859 created at building time of emacs, registering all quail input methods
860 contained in the emacs distribution.
861
862 In case you want to register a new quail input method by yourself, be
863 careful to use the same input method title as given in the third
864 parameter of `quail-define-package' (if the values are different, the
865 string specified in this function takes precedence).
866
867 The commands `describe-input-method' and `list-input-methods' need
868 this duplicated values to show some information about input methods
869 without loading the affected quail packages."
870 (if (symbolp lang-env)
871 (setq lang-env (symbol-name lang-env)))
872 (if (symbolp input-method)
873 (setq input-method (symbol-name input-method)))
874 (let ((info (cons lang-env args))
875 (slot (assoc input-method input-method-alist)))
876 (if slot
877 (setcdr slot info)
878 (setq slot (cons input-method info))
879 (setq input-method-alist (cons slot input-method-alist)))))
880
881 (defun read-input-method-name (prompt &optional default inhibit-null)
882 "Read a name of input method from a minibuffer prompting with PROMPT.
883 If DEFAULT is non-nil, use that as the default,
884 and substitute it into PROMPT at the first `%s'.
885 If INHIBIT-NULL is non-nil, null input signals an error.
886
887 The return value is a string."
888 (if default
889 (setq prompt (format prompt default)))
890 (let* ((completion-ignore-case t)
891 ;; This binding is necessary because input-method-history is
892 ;; buffer local.
893 (input-method (completing-read prompt input-method-alist
894 nil t nil 'input-method-history
895 default)))
896 (if (and input-method (symbolp input-method))
897 (setq input-method (symbol-name input-method)))
898 (if (> (length input-method) 0)
899 input-method
900 (if inhibit-null
901 (error "No valid input method is specified")))))
902
903 (defun activate-input-method (input-method)
904 "Switch to input method INPUT-METHOD for the current buffer.
905 If some other input method is already active, turn it off first.
906 If INPUT-METHOD is nil, deactivate any current input method."
907 (if (and input-method (symbolp input-method))
908 (setq input-method (symbol-name input-method)))
909 (if (and current-input-method
910 (not (string= current-input-method input-method)))
911 (inactivate-input-method))
912 (unless (or current-input-method (null input-method))
913 (let ((slot (assoc input-method input-method-alist)))
914 (if (null slot)
915 (error "Can't activate input method `%s'" input-method))
916 (let ((func (nth 2 slot)))
917 (if (functionp func)
918 (apply (nth 2 slot) input-method (nthcdr 5 slot))
919 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
920 (progn
921 (require (cdr func))
922 (apply (car func) input-method (nthcdr 5 slot)))
923 (error "Can't activate input method `%s'" input-method))))
924 (setq current-input-method input-method)
925 (setq current-input-method-title (nth 3 slot))
926 (unwind-protect
927 (run-hooks 'input-method-activate-hook)
928 (force-mode-line-update)))))
929
930 (defun inactivate-input-method ()
931 "Turn off the current input method."
932 (when current-input-method
933 (if input-method-history
934 (unless (string= current-input-method (car input-method-history))
935 (setq input-method-history
936 (cons current-input-method
937 (delete current-input-method input-method-history))))
938 (setq input-method-history (list current-input-method)))
939 (unwind-protect
940 (funcall inactivate-current-input-method-function)
941 (unwind-protect
942 (run-hooks 'input-method-inactivate-hook)
943 (setq current-input-method nil
944 current-input-method-title nil)
945 (force-mode-line-update)))))
946
947 (defun set-input-method (input-method)
948 "Select and activate input method INPUT-METHOD for the current buffer.
949 This also sets the default input method to the one you specify."
950 (interactive
951 (let* ((default (or (car input-method-history) default-input-method)))
952 (list (read-input-method-name
953 (if default "Select input method (default %s): " "Select input method: ")
954 default t))))
955 (activate-input-method input-method)
956 (setq default-input-method input-method))
957
958 (defun toggle-input-method (&optional arg)
959 "Turn on or off a multilingual text input method for the current buffer.
960
961 With no prefix argument, if an input method is currently activated,
962 turn it off. Otherwise, activate an input method -- the one most
963 recently used, or the one specified in `default-input-method', or
964 the one read from the minibuffer.
965
966 With a prefix argument, read an input method from the minibuffer and
967 turn it on.
968
969 The default is to use the most recent input method specified
970 \(not including the currently active input method, if any)."
971 (interactive "P")
972 (if (and current-input-method (not arg))
973 (inactivate-input-method)
974 (let ((default (or (car input-method-history) default-input-method)))
975 (if (and arg default (equal current-input-method default)
976 (> (length input-method-history) 1))
977 (setq default (nth 1 input-method-history)))
978 (activate-input-method
979 (if (or arg (not default))
980 (progn
981 (read-input-method-name
982 (if default "Input method (default %s): " "Input method: " )
983 default t))
984 default))
985 (or default-input-method
986 (setq default-input-method current-input-method)))))
987
988 (defun describe-input-method (input-method)
989 "Describe input method INPUT-METHOD."
990 (interactive
991 (list (read-input-method-name
992 "Describe input method (default, current choice): ")))
993 (if (and input-method (symbolp input-method))
994 (setq input-method (symbol-name input-method)))
995 (if (null input-method)
996 (describe-current-input-method)
997 (with-output-to-temp-buffer "*Help*"
998 (let ((elt (assoc input-method input-method-alist)))
999 (princ (format "Input method: %s (`%s' in mode line) for %s\n %s\n"
1000 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt)))))))
1001
1002 (defun describe-current-input-method ()
1003 "Describe the input method currently in use."
1004 (if current-input-method
1005 (if (and (symbolp describe-current-input-method-function)
1006 (fboundp describe-current-input-method-function))
1007 (funcall describe-current-input-method-function)
1008 (message "No way to describe the current input method `%s'"
1009 current-input-method)
1010 (ding))
1011 (error "No input method is activated now")))
1012
1013 (defun read-multilingual-string (prompt &optional initial-input input-method)
1014 "Read a multilingual string from minibuffer, prompting with string PROMPT.
1015 The input method selected last time is activated in minibuffer.
1016 If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
1017 initially.
1018 Optional 3rd argument INPUT-METHOD specifies the input method
1019 to be activated instead of the one selected last time. It is a symbol
1020 or a string."
1021 (setq input-method
1022 (or input-method
1023 current-input-method
1024 default-input-method
1025 (read-input-method-name "Input method: " nil t)))
1026 (if (and input-method (symbolp input-method))
1027 (setq input-method (symbol-name input-method)))
1028 (let ((prev-input-method current-input-method))
1029 (unwind-protect
1030 (progn
1031 (activate-input-method input-method)
1032 (read-string prompt initial-input nil nil t))
1033 (activate-input-method prev-input-method))))
1034
1035 ;; Variables to control behavior of input methods. All input methods
1036 ;; should react to these variables.
1037
1038 (defcustom input-method-verbose-flag 'default
1039 "*A flag to control extra guidance given by input methods.
1040 The value should be nil, t, `complex-only', or `default'.
1041
1042 The extra guidance is done by showing list of available keys in echo
1043 area. When you use the input method in the minibuffer, the guidance
1044 is shown at the bottom short window (split from the existing window).
1045
1046 If the value is t, extra guidance is always given, if the value is
1047 nil, extra guidance is always suppressed.
1048
1049 If the value is `complex-only', only complex input methods such as
1050 `chinese-py' and `japanese' give extra guidance.
1051
1052 If the value is `default', complex input methods always give extra
1053 guidance, but simple input methods give it only when you are not in
1054 the minibuffer.
1055
1056 See also the variable `input-method-highlight-flag'."
1057 :type '(choice (const t) (const nil) (const complex-only) (const default))
1058 :group 'mule)
1059
1060 (defcustom input-method-highlight-flag t
1061 "*If this flag is non-nil, input methods highlight partially-entered text.
1062 For instance, while you are in the middle of a Quail input method sequence,
1063 the text inserted so far is temporarily underlined.
1064 The underlining goes away when you finish or abort the input method sequence.
1065 See also the variable `input-method-verbose-flag'."
1066 :type 'boolean
1067 :group 'mule)
1068
1069 (defvar input-method-activate-hook nil
1070 "Normal hook run just after an input method is activated.
1071
1072 The variable `current-input-method' keeps the input method name
1073 just activated.")
1074
1075 (defvar input-method-inactivate-hook nil
1076 "Normal hook run just after an input method is inactivated.
1077
1078 The variable `current-input-method' still keeps the input method name
1079 just inactivated.")
1080
1081 (defvar input-method-after-insert-chunk-hook nil
1082 "Normal hook run just after an input method insert some chunk of text.")
1083
1084 (defvar input-method-exit-on-first-char nil
1085 "This flag controls a timing when an input method returns.
1086 Usually, the input method does not return while there's a possibility
1087 that it may find a different translation if a user types another key.
1088 But, it this flag is non-nil, the input method returns as soon as
1089 the current key sequence gets long enough to have some valid translation.")
1090
1091 (defvar input-method-use-echo-area nil
1092 "This flag controls how an input method shows an intermediate key sequence.
1093 Usually, the input method inserts the intermediate key sequence,
1094 or candidate translations corresponding to the sequence,
1095 at point in the current buffer.
1096 But, if this flag is non-nil, it displays them in echo area instead.")
1097
1098 (defvar input-method-exit-on-invalid-key nil
1099 "This flag controls the behaviour of an input method on invalid key input.
1100 Usually, when a user types a key which doesn't start any character
1101 handled by the input method, the key is handled by turning off the
1102 input method temporarily. After that key, the input method is re-enabled.
1103 But, if this flag is non-nil, the input method is never back on.")
1104
1105 \f
1106 (defvar set-language-environment-hook nil
1107 "Normal hook run after some language environment is set.
1108
1109 When you set some hook function here, that effect usually should not
1110 be inherited to another language environment. So, you had better set
1111 another function in `exit-language-environment-hook' (which see) to
1112 cancel the effect.")
1113
1114 (defvar exit-language-environment-hook nil
1115 "Normal hook run after exiting from some language environment.
1116 When this hook is run, the variable `current-language-environment'
1117 is still bound to the language environment being exited.
1118
1119 This hook is mainly used for canceling the effect of
1120 `set-language-environment-hook' (which-see).")
1121
1122 (put 'setup-specified-language-environment 'apropos-inhibit t)
1123
1124 (defun setup-specified-language-environment ()
1125 "Switch to a specified language environment."
1126 (interactive)
1127 (let (language-name)
1128 (if (and (symbolp last-command-event)
1129 (or (not (eq last-command-event 'Default))
1130 (setq last-command-event 'English))
1131 (setq language-name (symbol-name last-command-event)))
1132 (set-language-environment language-name)
1133 (error "Bogus calling sequence"))))
1134
1135 (defcustom current-language-environment "English"
1136 "The last language environment specified with `set-language-environment'.
1137 This variable should be set only with \\[customize], which is equivalent
1138 to using the function `set-language-environment'."
1139 :link '(custom-manual "(emacs)Language Environments")
1140 :set (lambda (symbol value) (set-language-environment value))
1141 :get (lambda (x)
1142 (or (car-safe (assoc-ignore-case
1143 (if (symbolp current-language-environment)
1144 (symbol-name current-language-environment)
1145 current-language-environment)
1146 language-info-alist))
1147 "English"))
1148 :type (cons 'choice (mapcar (lambda (lang)
1149 (list 'const (car lang)))
1150 language-info-alist))
1151 :initialize 'custom-initialize-default
1152 :group 'mule
1153 :type 'string)
1154
1155 (defun reset-language-environment ()
1156 "Reset multilingual environment of Emacs to the default status.
1157
1158 The default status is as follows:
1159
1160 The default value of buffer-file-coding-system is nil.
1161 The default coding system for process I/O is nil.
1162 The default value for the command `set-terminal-coding-system' is nil.
1163 The default value for the command `set-keyboard-coding-system' is nil.
1164
1165 The order of priorities of coding categories and the coding system
1166 bound to each category are as follows
1167 coding category coding system
1168 --------------------------------------------------
1169 coding-category-iso-8-2 iso-latin-1
1170 coding-category-iso-8-1 iso-latin-1
1171 coding-category-iso-7-tight iso-2022-jp
1172 coding-category-iso-7 iso-2022-7bit
1173 coding-category-iso-7-else iso-2022-7bit-lock
1174 coding-category-iso-8-else iso-2022-8bit-ss2
1175 coding-category-emacs-mule emacs-mule
1176 coding-category-raw-text raw-text
1177 coding-category-sjis japanese-shift-jis
1178 coding-category-big5 chinese-big5
1179 coding-category-ccl nil
1180 coding-category-binary no-conversion
1181 "
1182 (interactive)
1183 ;; This function formerly set default-enable-multibyte-characters to t,
1184 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1185
1186 (setq coding-category-iso-7-tight 'iso-2022-jp
1187 coding-category-iso-7 'iso-2022-7bit
1188 coding-category-iso-8-1 'iso-latin-1
1189 coding-category-iso-8-2 'iso-latin-1
1190 coding-category-iso-7-else 'iso-2022-7bit-lock
1191 coding-category-iso-8-else 'iso-2022-8bit-ss2
1192 coding-category-emacs-mule 'emacs-mule
1193 coding-category-raw-text 'raw-text
1194 coding-category-sjis 'japanese-shift-jis
1195 coding-category-big5 'chinese-big5
1196 coding-category-ccl nil
1197 coding-category-binary 'no-conversion)
1198
1199 (set-coding-priority
1200 '(coding-category-iso-8-1
1201 coding-category-iso-8-2
1202 coding-category-iso-7-tight
1203 coding-category-iso-7
1204 coding-category-iso-7-else
1205 coding-category-iso-8-else
1206 coding-category-emacs-mule
1207 coding-category-raw-text
1208 coding-category-sjis
1209 coding-category-big5
1210 coding-category-ccl
1211 coding-category-binary))
1212
1213 (update-coding-systems-internal)
1214
1215 (set-default-coding-systems nil)
1216 ;; Don't alter the terminal and keyboard coding systems here.
1217 ;; The terminal still supports the same coding system
1218 ;; that it supported a minute ago.
1219 ;;; (set-terminal-coding-system-internal nil)
1220 ;;; (set-keyboard-coding-system-internal nil)
1221
1222 (setq nonascii-translation-table nil
1223 nonascii-insert-offset 0))
1224
1225 (defun set-display-table-and-terminal-coding-system (language-name)
1226 "Set up the display table and terminal coding system for LANGUAGE-NAME."
1227 (let ((coding (get-language-info language-name 'unibyte-display)))
1228 (if coding
1229 (standard-display-european-internal)
1230 (standard-display-default (if (eq window-system 'pc) 128 160) 255)
1231 (aset standard-display-table 146 nil))
1232 (or (eq window-system 'pc)
1233 (set-terminal-coding-system coding))))
1234
1235 (defun set-language-environment (language-name)
1236 "Set up multi-lingual environment for using LANGUAGE-NAME.
1237 This sets the coding system priority and the default input method
1238 and sometimes other things. LANGUAGE-NAME should be a string
1239 which is the name of a language environment. For example, \"Latin-1\"
1240 specifies the character set for the major languages of Western Europe."
1241 (interactive (list (read-language-name
1242 nil
1243 "Set language environment (default, English): ")))
1244 (if language-name
1245 (if (symbolp language-name)
1246 (setq language-name (symbol-name language-name)))
1247 (setq language-name "English"))
1248 (or (assoc-ignore-case language-name language-info-alist)
1249 (error "Language environment not defined: %S" language-name))
1250 (if current-language-environment
1251 (let ((func (get-language-info current-language-environment
1252 'exit-function)))
1253 (run-hooks 'exit-language-environment-hook)
1254 (if (fboundp func) (funcall func))))
1255 (let ((default-eol-type (coding-system-eol-type
1256 default-buffer-file-coding-system)))
1257 (reset-language-environment)
1258
1259 (setq current-language-environment language-name)
1260 (set-language-environment-coding-systems language-name default-eol-type))
1261 (let ((input-method (get-language-info language-name 'input-method)))
1262 (when input-method
1263 (setq default-input-method input-method)
1264 (if input-method-history
1265 (setq input-method-history
1266 (cons input-method
1267 (delete input-method input-method-history))))))
1268 (let ((nonascii (get-language-info language-name 'nonascii-translation))
1269 (dos-table
1270 (if (eq window-system 'pc)
1271 (intern
1272 (concat "cp" dos-codepage "-nonascii-translation-table")))))
1273 (cond
1274 ((char-table-p nonascii)
1275 (setq nonascii-translation-table nonascii))
1276 ((and (eq window-system 'pc) (boundp dos-table))
1277 ;; DOS terminals' default is to use a special non-ASCII translation
1278 ;; table as appropriate for the installed codepage.
1279 (setq nonascii-translation-table (symbol-value dos-table)))
1280 ((charsetp nonascii)
1281 (setq nonascii-insert-offset (- (make-char nonascii) 128)))))
1282
1283 (setq charset-origin-alist
1284 (get-language-info language-name 'charset-origin-alist))
1285
1286 ;; Unibyte setups if necessary.
1287 (unless default-enable-multibyte-characters
1288 ;; Syntax and case table.
1289 (let ((syntax (get-language-info language-name 'unibyte-syntax)))
1290 (if syntax
1291 (let ((set-case-syntax-set-multibyte nil))
1292 (load syntax nil t))
1293 ;; No information for syntax and case. Reset to the defaults.
1294 (let ((syntax-table (standard-syntax-table))
1295 (case-table (standard-case-table))
1296 (ch (if (eq window-system 'pc) 128 160)))
1297 (while (< ch 256)
1298 (modify-syntax-entry ch " " syntax-table)
1299 (aset case-table ch ch)
1300 (setq ch (1+ ch)))
1301 (set-char-table-extra-slot case-table 0 nil)
1302 (set-char-table-extra-slot case-table 1 nil)
1303 (set-char-table-extra-slot case-table 2 nil))
1304 (set-standard-case-table (standard-case-table))
1305 (let ((list (buffer-list)))
1306 (while list
1307 (with-current-buffer (car list)
1308 (set-case-table (standard-case-table)))
1309 (setq list (cdr list))))))
1310 (set-display-table-and-terminal-coding-system language-name))
1311
1312 (let ((required-features (get-language-info language-name 'features)))
1313 (while required-features
1314 (require (car required-features))
1315 (setq required-features (cdr required-features))))
1316 (let ((func (get-language-info language-name 'setup-function)))
1317 (if (fboundp func)
1318 (funcall func)))
1319 (run-hooks 'set-language-environment-hook)
1320 (force-mode-line-update t))
1321
1322 (defun standard-display-european-internal ()
1323 ;; Actually set up direct output of non-ASCII characters.
1324 (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
1325 ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
1326 ;; the native font, and codes 160 and 146 stand for something very
1327 ;; different there.
1328 (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
1329 (progn
1330 ;; Make non-line-break space display as a plain space.
1331 ;; Most X fonts do the wrong thing for code 160.
1332 (aset standard-display-table 160 [32])
1333 ;; Most Windows programs send out apostrophe's as \222. Most X fonts
1334 ;; don't contain a character at that position. Map it to the ASCII
1335 ;; apostrophe.
1336 (aset standard-display-table 146 [39]))))
1337
1338 (defun set-language-environment-coding-systems (language-name
1339 &optional eol-type)
1340 "Do various coding system setups for language environment LANGUAGE-NAME.
1341
1342 The optional arg EOL-TYPE specifies the eol-type of the default value
1343 of buffer-file-coding-system set by this function."
1344 (let* ((priority (get-language-info language-name 'coding-priority))
1345 (default-coding (car priority)))
1346 (if priority
1347 (let ((categories (mapcar 'coding-system-category priority)))
1348 (set-default-coding-systems
1349 (if (memq eol-type '(0 1 2 unix dos mac))
1350 (coding-system-change-eol-conversion default-coding eol-type)
1351 default-coding))
1352 (setq default-sendmail-coding-system default-coding)
1353 (set-coding-priority categories)
1354 (while priority
1355 (set (car categories) (car priority))
1356 (setq priority (cdr priority) categories (cdr categories)))
1357 (update-coding-systems-internal)))))
1358
1359 ;; Print all arguments with `princ', then print "\n".
1360 (defsubst princ-list (&rest args)
1361 (while args (princ (car args)) (setq args (cdr args)))
1362 (princ "\n"))
1363
1364 (put 'describe-specified-language-support 'apropos-inhibit t)
1365
1366 ;; Print a language specific information such as input methods,
1367 ;; charsets, and coding systems. This function is intended to be
1368 ;; called from the menu:
1369 ;; [menu-bar mule describe-language-environment LANGUAGE]
1370 ;; and should not run it by `M-x describe-current-input-method-function'.
1371 (defun describe-specified-language-support ()
1372 "Describe how Emacs supports the specified language environment."
1373 (interactive)
1374 (let (language-name)
1375 (if (not (and (symbolp last-command-event)
1376 (setq language-name (symbol-name last-command-event))))
1377 (error "Bogus calling sequence"))
1378 (describe-language-environment language-name)))
1379
1380 (defun describe-language-environment (language-name)
1381 "Describe how Emacs supports language environment LANGUAGE-NAME."
1382 (interactive
1383 (list (read-language-name
1384 'documentation
1385 "Describe language environment (default, current choice): ")))
1386 (if (null language-name)
1387 (setq language-name current-language-environment))
1388 (if (or (null language-name)
1389 (null (get-language-info language-name 'documentation)))
1390 (error "No documentation for the specified language"))
1391 (if (symbolp language-name)
1392 (setq language-name (symbol-name language-name)))
1393 (let ((doc (get-language-info language-name 'documentation)))
1394 (with-output-to-temp-buffer "*Help*"
1395 (princ-list language-name " language environment" "\n")
1396 (if (stringp doc)
1397 (progn
1398 (princ-list doc)
1399 (terpri)))
1400 (let ((str (get-language-info language-name 'sample-text)))
1401 (if (stringp str)
1402 (progn
1403 (princ "Sample text:\n")
1404 (princ-list " " str)
1405 (terpri))))
1406 (let ((input-method (get-language-info language-name 'input-method))
1407 (l (copy-sequence input-method-alist)))
1408 (princ "Input methods")
1409 (when input-method
1410 (princ (format " (default, %s)" input-method))
1411 (setq input-method (assoc input-method input-method-alist))
1412 (setq l (cons input-method (delete input-method l))))
1413 (princ ":\n")
1414 (while l
1415 (if (string= language-name (nth 1 (car l)))
1416 (princ-list " " (car (car l))
1417 (format " (`%s' in mode line)" (nth 3 (car l)))))
1418 (setq l (cdr l))))
1419 (terpri)
1420 (princ "Character sets:\n")
1421 (let ((l (get-language-info language-name 'charset)))
1422 (if (null l)
1423 (princ-list " nothing specific to " language-name)
1424 (while l
1425 (princ-list " " (car l) ": "
1426 (charset-description (car l)))
1427 (setq l (cdr l)))))
1428 (terpri)
1429 (princ "Coding systems:\n")
1430 (let ((l (get-language-info language-name 'coding-system)))
1431 (if (null l)
1432 (princ-list " nothing specific to " language-name)
1433 (while l
1434 (princ (format " %s (`%c' in mode line):\n\t%s\n"
1435 (car l)
1436 (coding-system-mnemonic (car l))
1437 (coding-system-doc-string (car l))))
1438 (let ((aliases (coding-system-get (car l) 'alias-coding-systems)))
1439 (when aliases
1440 (princ "\t")
1441 (princ (cons 'alias: (cdr aliases)))
1442 (terpri)))
1443 (setq l (cdr l))))))))
1444 \f
1445 ;;; Locales.
1446
1447 (defvar locale-translation-file-name
1448 (let ((files '("/usr/lib/X11/locale/locale.alias" ; e.g. X11R6.4
1449 "/usr/X11R6/lib/X11/locale/locale.alias" ; e.g. RedHat 4.2
1450 "/usr/openwin/lib/locale/locale.alias" ; e.g. Solaris 2.6
1451 ;;
1452 ;; The following name appears after the X-related names above,
1453 ;; since the X-related names are what X actually uses.
1454 "/usr/share/locale/locale.alias" ; GNU/Linux sans X
1455 )))
1456 (while (and files (not (file-exists-p (car files))))
1457 (setq files (cdr files)))
1458 (car files))
1459 "*File name for the system's file of locale-name aliases, or nil if none.")
1460
1461 (defvar locale-language-names
1462 '(
1463 ;; UTF-8 is not yet implemented.
1464 ;; Put this first, so that e.g. "ko.UTF-8" does not match "ko" below.
1465 (".*[._]utf" . nil)
1466
1467 ;; Locale names of the form LANGUAGE[_TERRITORY][.CODESET][@MODIFIER]
1468 ;; as specified in the Single Unix Spec, Version 2.
1469 ;; LANGUAGE is a language code taken from ISO 639:1988 (E/F)
1470 ;; with additions from ISO 639/RA Newsletter No.1/1989;
1471 ;; see Internet RFC 2165 (1997-06).
1472 ;; TERRITORY is a country code taken from ISO 3166.
1473 ;; CODESET and MODIFIER are implementation-dependent.
1474 ;;
1475 ; aa Afar
1476 ; ab Abkhazian
1477 ("af" . "Latin-1") ; Afrikaans
1478 ("am" . "Ethiopic") ; Amharic
1479 ; ar Arabic
1480 ; as Assamese
1481 ; ay Aymara
1482 ; az Azerbaijani
1483 ; ba Bashkir
1484 ("be" . "Latin-5") ; Byelorussian
1485 ("bg" . "Latin-5") ; Bulgarian
1486 ; bh Bihari
1487 ; bi Bislama
1488 ; bn Bengali, Bangla
1489 ("bo" . "Tibetan")
1490 ("br" . "Latin-1") ; Breton
1491 ("ca" . "Latin-1") ; Catalan
1492 ; co Corsican
1493 ("cs" . "Czech")
1494 ("cy" . "Latin-8") ; Welsh
1495 ("da" . "Latin-1") ; Danish
1496 ("de" . "German")
1497 ; dz Bhutani
1498 ("el" . "Greek")
1499 ;; Users who specify "en" explicitly typically want Latin-1, not ASCII.
1500 ("en" . "Latin-1") ; English
1501 ("eo" . "Latin-3") ; Esperanto
1502 ("es" . "Latin-1") ; Spanish
1503 ("et" . "Latin-4") ; Estonian
1504 ("eu" . "Latin-1") ; Basque
1505 ; fa Persian
1506 ("fi" . "Latin-1") ; Finnish
1507 ; fj Fiji
1508 ("fo" . "Latin-1") ; Faroese
1509 ("fr" . "Latin-1") ; French
1510 ("fy" . "Latin-1") ; Frisian
1511 ("ga" . "Latin-1") ; Irish Gaelic (new orthography)
1512 ("gd" . "Latin-1") ; Scots Gaelic
1513 ("gl" . "Latin-1") ; Galician
1514 ; gn Guarani
1515 ; gu Gujarati
1516 ; ha Hausa
1517 ("he" . "Hebrew")
1518 ("hi" . "Devanagari") ; Hindi
1519 ("hr" . "Latin-2") ; Croatian
1520 ("hu" . "Latin-2") ; Hungarian
1521 ; hy Armenian
1522 ; ia Interlingua
1523 ("id" . "Latin-1") ; Indonesian
1524 ; ie Interlingue
1525 ; ik Inupiak
1526 ("is" . "Latin-1") ; Icelandic
1527 ("it" . "Latin-1") ; Italian
1528 ; iu Inuktitut
1529 ("ja" . "Japanese")
1530 ; jw Javanese
1531 ; ka Georgian
1532 ; kk Kazakh
1533 ("kl" . "Latin-1") ; Greenlandic
1534 ; km Cambodian
1535 ; kn Kannada
1536 ("ko" . "Korean")
1537 ; ks Kashmiri
1538 ; ku Kurdish
1539 ; ky Kirghiz
1540 ("la" . "Latin-1") ; Latin
1541 ; ln Lingala
1542 ("lo" . "Lao") ; Laothian
1543 ("lt" . "Latin-4") ; Lithuanian
1544 ("lv" . "Latin-4") ; Latvian, Lettish
1545 ; mg Malagasy
1546 ; mi Maori
1547 ("mk" . "Latin-5") ; Macedonian
1548 ; ml Malayalam
1549 ; mn Mongolian
1550 ; mo Moldavian
1551 ("mr" . "Devanagari") ; Marathi
1552 ; ms Malay
1553 ("mt" . "Latin-3") ; Maltese
1554 ; my Burmese
1555 ; na Nauru
1556 ("ne" . "Devanagari") ; Nepali
1557 ("nl" . "Latin-1") ; Dutch
1558 ("no" . "Latin-1") ; Norwegian
1559 ; oc Occitan
1560 ; om (Afan) Oromo
1561 ; or Oriya
1562 ; pa Punjabi
1563 ("pl" . "Latin-2") ; Polish
1564 ; ps Pashto, Pushto
1565 ("pt" . "Latin-1") ; Portuguese
1566 ; qu Quechua
1567 ("rm" . "Latin-1") ; Rhaeto-Romanic
1568 ; rn Kirundi
1569 ("ro" . "Romanian")
1570 ("ru.*[_.]koi8" . "Cyrillic-KOI8") ; Russian
1571 ("ru" . "Latin-5") ; Russian
1572 ; rw Kinyarwanda
1573 ("sa" . "Devanagari") ; Sanskrit
1574 ; sd Sindhi
1575 ; sg Sangho
1576 ("sh" . "Latin-2") ; Serbo-Croatian
1577 ; si Sinhalese
1578 ("sk" . "Slovak")
1579 ("sl" . "Slovenian")
1580 ; sm Samoan
1581 ; sn Shona
1582 ; so Somali
1583 ("sq" . "Latin-1") ; Albanian
1584 ("sr" . "Latin-2") ; Serbian (Latin alphabet)
1585 ; ss Siswati
1586 ; st Sesotho
1587 ; su Sundanese
1588 ("sv" . "Latin-1") ; Swedish
1589 ("sw" . "Latin-1") ; Swahili
1590 ; ta Tamil
1591 ; te Telugu
1592 ; tg Tajik
1593 ("th" . "Thai")
1594 ; ti Tigrinya
1595 ; tk Turkmen
1596 ("tl" . "Latin-1") ; Tagalog
1597 ; tn Setswana
1598 ; to Tonga
1599 ("tr" . "Latin-5") ; Turkish
1600 ; ts Tsonga
1601 ; tt Tatar
1602 ; tw Twi
1603 ; ug Uighur
1604 ("uk" . "Latin-5") ; Ukrainian
1605 ; ur Urdu
1606 ; uz Uzbek
1607 ("vi" . "Vietnamese")
1608 ; vo Volapuk
1609 ; wo Wolof
1610 ; xh Xhosa
1611 ; yi Yiddish
1612 ; yo Yoruba
1613 ; za Zhuang
1614 ("zh.*[._]big5" . "Chinese-BIG5")
1615 ("zh.*[._]gbk" . nil) ; Solaris 2.7; has gbk-0 as well as GB 2312.1980-0
1616 ("zh_tw" . "Chinese-CNS")
1617 ("zh" . "Chinese-GB")
1618 ; zu Zulu
1619
1620 ;; ISO standard locales
1621 ("c$" . "ASCII")
1622 ("posix$" . "ASCII")
1623
1624 ;; The "IPA" Emacs language environment does not correspond
1625 ;; to any ISO 639 code, so let it stand for itself.
1626 ("ipa$" . "IPA")
1627
1628 ;; Nonstandard or obsolete language codes
1629 ("cz" . "Czech") ; e.g. Solaris 2.6
1630 ("ee" . "Latin-4") ; Estonian, e.g. X11R6.4
1631 ("iw" . "Hebrew") ; e.g. X11R6.4
1632 ("sp" . "Latin-5") ; Serbian (Cyrillic alphabet), e.g. X11R6.4
1633 ("su" . "Latin-1") ; Finnish, e.g. Solaris 2.6
1634 )
1635 "List of pairs of locale regexps and language names.
1636 The first element whose locale regexp matches the start of a downcased locale
1637 specifies the language name corresponding to that locale.
1638 If the language name is nil, there is no corresponding language environment.")
1639
1640 (defvar locale-charset-language-names
1641 '((".*8859[-_]?1\\>" . "Latin-1")
1642 (".*8859[-_]?2\\>" . "Latin-2")
1643 (".*8859[-_]?3\\>" . "Latin-3")
1644 (".*8859[-_]?4\\>" . "Latin-4")
1645 (".*8859[-_]?9\\>" . "Latin-5")
1646 (".*8859[-_]?14\\>" . "Latin-8")
1647 (".*8859[-_]?15\\>" . "Latin-9")
1648 )
1649 "List of pairs of locale regexps and charset language names.
1650 The first element whose locale regexp matches the start of a downcased locale
1651 specifies the language name whose charsets corresponds to that locale.
1652 This language name is used if its charsets disagree with the charsets of
1653 the language name that would otherwise be used for this locale.")
1654
1655 (defvar locale-preferred-coding-systems
1656 '(("ja.*[._]euc" . japanese-iso-8bit)
1657 ("ja.*[._]jis7" . iso-2022-jp)
1658 ("ja.*[._]pck" . japanese-shift-jis)
1659 ("ja.*[._]sjis" . japanese-shift-jis)
1660 )
1661 "List of pairs of locale regexps and preferred coding systems.
1662 The first element whose locale regexp matches the start of a downcased locale
1663 specifies the coding system to prefer when using that locale.")
1664
1665 (defun locale-name-match (key alist)
1666 "Search for KEY in ALIST, which should be a list of regexp-value pairs.
1667 Return the value corresponding to the first regexp that matches the
1668 start of KEY, or nil if there is no match."
1669 (let (element)
1670 (while (and alist (not element))
1671 (if (string-match (concat "^\\(" (car (car alist)) "\\)") key)
1672 (setq element (car alist)))
1673 (setq alist (cdr alist)))
1674 (cdr element)))
1675
1676 (defun set-locale-environment (locale-name)
1677 "Set up multi-lingual environment for using LOCALE-NAME.
1678 This sets the coding system priority and the default input method
1679 and sometimes other things. LOCALE-NAME should be a string
1680 which is the name of a locale supported by the system;
1681 often it is of the form xx_XX.CODE, where xx is a language,
1682 XX is a country, and CODE specifies a character set and coding system.
1683 For example, the locale name \"ja_JP.EUC\" might name a locale
1684 for Japanese in Japan using the `japanese-iso-8bit' coding-system.
1685
1686 If LOCALE-NAME is nil, its value is taken from the environment.
1687
1688 The locale names supported by your system can typically be found in a
1689 directory named `/usr/share/locale' or `/usr/lib/locale'."
1690
1691 (unless locale-name
1692 ;; Use the first of these three environment variables
1693 ;; that has a nonempty value.
1694 (let ((vars '("LC_ALL" "LC_CTYPE" "LANG")))
1695 (while (and vars (not (setq locale-name (getenv (car vars)))))
1696 (setq vars (cdr vars)))))
1697
1698 (when locale-name
1699
1700 ;; Translate "swedish" into "sv_SE.ISO8859-1", and so on,
1701 ;; using the translation file that many systems have.
1702 (when locale-translation-file-name
1703 (with-temp-buffer
1704 (insert-file-contents locale-translation-file-name)
1705 (when (re-search-forward
1706 (concat "^" (regexp-quote locale-name) ":?[ \t]+") nil t)
1707 (setq locale-name (buffer-substring (point) (line-end-position))))))
1708
1709 (setq locale-name (downcase locale-name))
1710
1711 (let ((language-name
1712 (locale-name-match locale-name locale-language-names))
1713 (charset-language-name
1714 (locale-name-match locale-name locale-charset-language-names))
1715 (coding-system
1716 (locale-name-match locale-name locale-preferred-coding-systems)))
1717
1718 (if (and charset-language-name
1719 (not
1720 (equal (get-language-info language-name 'charset)
1721 (get-language-info charset-language-name 'charset))))
1722 (setq language-name charset-language-name))
1723
1724 (when language-name
1725
1726 ;; Set up for this character set. This is now the right way
1727 ;; to do it for both unibyte and multibyte modes.
1728 (set-language-environment language-name)
1729
1730 ;; If default-enable-multibyte-characters is nil,
1731 ;; we are using single-byte characters,
1732 ;; so the display table and terminal coding system are irrelevant.
1733 (when default-enable-multibyte-characters
1734 (set-display-table-and-terminal-coding-system language-name))
1735
1736 (setq locale-coding-system
1737 (car (get-language-info language-name 'coding-priority))))
1738
1739 (when coding-system
1740 (prefer-coding-system coding-system)
1741 (setq locale-coding-system coding-system)))))
1742 \f
1743 ;;; Charset property
1744
1745 (defun get-charset-property (charset propname)
1746 "Return the value of CHARSET's PROPNAME property.
1747 This is the last value stored with
1748 (put-charset-property CHARSET PROPNAME VALUE)."
1749 (and (not (eq charset 'composition))
1750 (plist-get (charset-plist charset) propname)))
1751
1752 (defun put-charset-property (charset propname value)
1753 "Store CHARSETS's PROPNAME property with value VALUE.
1754 It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
1755 (or (eq charset 'composition)
1756 (set-charset-plist charset
1757 (plist-put (charset-plist charset) propname value))))
1758
1759 ;;; Character code property
1760 (put 'char-code-property-table 'char-table-extra-slots 0)
1761
1762 (defvar char-code-property-table
1763 (make-char-table 'char-code-property-table)
1764 "Char-table containing a property list of each character code.
1765
1766 See also the documentation of `get-char-code-property' and
1767 `put-char-code-property'.")
1768
1769 (defun get-char-code-property (char propname)
1770 "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
1771 (let ((plist (aref char-code-property-table char)))
1772 (if (listp plist)
1773 (car (cdr (memq propname plist))))))
1774
1775 (defun put-char-code-property (char propname value)
1776 "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
1777 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
1778 (let ((plist (aref char-code-property-table char)))
1779 (if plist
1780 (let ((slot (memq propname plist)))
1781 (if slot
1782 (setcar (cdr slot) value)
1783 (nconc plist (list propname value))))
1784 (aset char-code-property-table char (list propname value)))))
1785
1786 \f
1787 ;; Pretty description of encoded string
1788
1789 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
1790 (defvar iso-2022-control-alist
1791 '((?\x1b . "ESC")
1792 (?\x0e . "SO")
1793 (?\x0f . "SI")
1794 (?\x8e . "SS2")
1795 (?\x8f . "SS3")
1796 (?\x9b . "CSI")))
1797
1798 (defun encoded-string-description (str coding-system)
1799 "Return a pretty description of STR that is encoded by CODING-SYSTEM."
1800 (setq str (string-as-unibyte str))
1801 (let ((char (aref str 0))
1802 desc)
1803 (when (< char 128)
1804 (setq desc (or (cdr (assq char iso-2022-control-alist))
1805 (char-to-string char)))
1806 (let ((i 1)
1807 (len (length str)))
1808 (while (< i len)
1809 (setq char (aref str i))
1810 (if (>= char 128)
1811 (setq desc nil i len)
1812 (setq desc (concat desc " "
1813 (or (cdr (assq char iso-2022-control-alist))
1814 (char-to-string char)))
1815 i (1+ i))))))
1816 (or desc
1817 (mapconcat (function (lambda (x) (format "0x%02x" x))) str " "))))
1818
1819 (defun encode-coding-char (char coding-system)
1820 "Encode CHAR by CODING-SYSTEM and return the resulting string.
1821 If CODING-SYSTEM can't safely encode CHAR, return nil."
1822 (let ((str1 (char-to-string char))
1823 (str2 (make-string 2 char))
1824 (safe-charsets (and coding-system
1825 (coding-system-get coding-system 'safe-charsets)))
1826 enc1 enc2 i1 i2)
1827 (when (or (eq safe-charsets t)
1828 (memq (char-charset char) safe-charsets))
1829 ;; We must find the encoded string of CHAR. But, just encoding
1830 ;; CHAR will put extra control sequences (usually to designate
1831 ;; ASCII charaset) at the tail if type of CODING is ISO 2022.
1832 ;; To exclude such tailing bytes, we at first encode one-char
1833 ;; string and two-char string, then check how many bytes at the
1834 ;; tail of both encoded strings are the same.
1835
1836 (setq enc1 (string-as-unibyte (encode-coding-string str1 coding-system))
1837 i1 (length enc1)
1838 enc2 (string-as-unibyte (encode-coding-string str2 coding-system))
1839 i2 (length enc2))
1840 (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
1841 (setq i1 (1- i1) i2 (1- i2)))
1842
1843 ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
1844 ;; and they are the extra control sequences at the tail to
1845 ;; exclude.
1846 (substring enc2 0 i2))))
1847
1848
1849 ;;; mule-cmds.el ends here