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