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