]> code.delx.au - gnu-emacs/blob - lisp/international/mule-cmds.el
(set-language-info): If LANG-ENV is
[gnu-emacs] / lisp / international / mule-cmds.el
1 ;;; mule-cmds.el --- commands for mulitilingual environment -*-coding: iso-2022-7bit -*-
2
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 ;; Free Software Foundation, Inc.
5 ;; Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
6 ;; National Institute of Advanced Industrial Science and Technology (AIST)
7 ;; Registration Number H14PRO021
8
9 ;; Keywords: mule, multilingual
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;;; Commentary:
29
30 ;;; Code:
31
32 (eval-when-compile
33 (defvar dos-codepage)
34 (autoload 'widget-value "wid-edit"))
35
36 (defvar mac-system-coding-system)
37 (defvar mac-system-locale)
38
39 ;;; MULE related key bindings and menus.
40
41 (defvar mule-keymap (make-sparse-keymap)
42 "Keymap for Mule (Multilingual environment) specific commands.")
43
44 ;; Keep "C-x C-m ..." for mule specific commands.
45 (define-key ctl-x-map "\C-m" mule-keymap)
46
47 (define-key mule-keymap "f" 'set-buffer-file-coding-system)
48 (define-key mule-keymap "r" 'revert-buffer-with-coding-system)
49 (define-key mule-keymap "F" 'set-file-name-coding-system)
50 (define-key mule-keymap "t" 'set-terminal-coding-system)
51 (define-key mule-keymap "k" 'set-keyboard-coding-system)
52 (define-key mule-keymap "p" 'set-buffer-process-coding-system)
53 (define-key mule-keymap "x" 'set-selection-coding-system)
54 (define-key mule-keymap "X" 'set-next-selection-coding-system)
55 (define-key mule-keymap "\C-\\" 'set-input-method)
56 (define-key mule-keymap "c" 'universal-coding-system-argument)
57 (define-key mule-keymap "l" 'set-language-environment)
58
59 (defvar mule-menu-keymap
60 (make-sparse-keymap "Mule (Multilingual Environment)")
61 "Keymap for Mule (Multilingual environment) menu specific commands.")
62
63 (defvar describe-language-environment-map
64 (make-sparse-keymap "Describe Language Environment"))
65
66 (defvar setup-language-environment-map
67 (make-sparse-keymap "Set Language Environment"))
68
69 (defvar set-coding-system-map
70 (make-sparse-keymap "Set Coding System"))
71
72 (define-key-after mule-menu-keymap [set-language-environment]
73 (list 'menu-item "Set Language Environment" setup-language-environment-map))
74 (define-key-after mule-menu-keymap [separator-mule]
75 '("--")
76 t)
77 (define-key-after mule-menu-keymap [toggle-input-method]
78 '(menu-item "Toggle Input Method" toggle-input-method)
79 t)
80 (define-key-after mule-menu-keymap [set-input-method]
81 '(menu-item "Select Input Method..." set-input-method)
82 t)
83 (define-key-after mule-menu-keymap [describe-input-method]
84 '(menu-item "Describe Input Method" describe-input-method))
85 (define-key-after mule-menu-keymap [separator-input-method]
86 '("--")
87 t)
88 (define-key-after mule-menu-keymap [set-various-coding-system]
89 (list 'menu-item "Set Coding Systems" set-coding-system-map
90 :enable 'default-enable-multibyte-characters))
91 (define-key-after mule-menu-keymap [view-hello-file]
92 '(menu-item "Show Multi-lingual Text" view-hello-file
93 :enable (file-readable-p
94 (expand-file-name "HELLO" data-directory))
95 :help "Display file which says HELLO in many languages")
96 t)
97 (define-key-after mule-menu-keymap [separator-coding-system]
98 '("--")
99 t)
100 (define-key-after mule-menu-keymap [describe-language-environment]
101 (list 'menu-item "Describe Language Environment"
102 describe-language-environment-map
103 :help "Show multilingual settings for a specific language")
104 t)
105 (define-key-after mule-menu-keymap [describe-input-method]
106 '(menu-item "Describe Input Method..." describe-input-method
107 :help "Keyboard layout for a specific input method")
108 t)
109 (define-key-after mule-menu-keymap [describe-coding-system]
110 '(menu-item "Describe Coding System..." describe-coding-system)
111 t)
112 (define-key-after mule-menu-keymap [list-character-sets]
113 '(menu-item "List Character Sets" list-character-sets
114 :help "Show table of available character sets"))
115 (define-key-after mule-menu-keymap [mule-diag]
116 '(menu-item "Show All of Mule Status" mule-diag
117 :help "Display multilingual environment settings")
118 t)
119
120 (define-key-after set-coding-system-map [universal-coding-system-argument]
121 '(menu-item "For Next Command" universal-coding-system-argument
122 :help "Coding system to be used by next command")
123 t)
124 (define-key-after set-coding-system-map [separator-1]
125 '("--")
126 t)
127 (define-key-after set-coding-system-map [set-buffer-file-coding-system]
128 '(menu-item "For Saving This Buffer" set-buffer-file-coding-system
129 :help "How to encode this buffer when saved")
130 t)
131 (define-key-after set-coding-system-map [revert-buffer-with-coding-system]
132 '(menu-item "For Reverting This File Now" revert-buffer-with-coding-system
133 :enable buffer-file-name
134 :help "Revisit this file immediately using specified coding system")
135 t)
136 (define-key-after set-coding-system-map [set-file-name-coding-system]
137 '(menu-item "For File Name" set-file-name-coding-system
138 :help "How to decode/encode file names")
139 t)
140 (define-key-after set-coding-system-map [separator-2]
141 '("--")
142 t)
143
144 (define-key-after set-coding-system-map [set-keyboard-coding-system]
145 '(menu-item "For Keyboard" set-keyboard-coding-system
146 :help "How to decode keyboard input")
147 t)
148 (define-key-after set-coding-system-map [set-terminal-coding-system]
149 '(menu-item "For Terminal" set-terminal-coding-system
150 :enable (null (memq window-system '(x w32 mac)))
151 :help "How to encode terminal output")
152 t)
153 (define-key-after set-coding-system-map [separator-3]
154 '("--")
155 t)
156 (define-key-after set-coding-system-map [set-selection-coding-system]
157 '(menu-item "For X Selections/Clipboard" set-selection-coding-system
158 :visible (display-selections-p)
159 :help "How to en/decode data to/from selection/clipboard")
160 t)
161 (define-key-after set-coding-system-map [set-next-selection-coding-system]
162 '(menu-item "For Next X Selection" set-next-selection-coding-system
163 :visible (display-selections-p)
164 :help "How to en/decode next selection/clipboard operation")
165 t)
166 (define-key-after set-coding-system-map [set-buffer-process-coding-system]
167 '(menu-item "For I/O with Subprocess" set-buffer-process-coding-system
168 :visible (fboundp 'start-process)
169 :enable (get-buffer-process (current-buffer))
170 :help "How to en/decode I/O from/to subprocess connected to this buffer")
171 t)
172
173
174 (define-key setup-language-environment-map
175 [Default] '(menu-item "Default" setup-specified-language-environment))
176
177 (define-key describe-language-environment-map
178 [Default] '(menu-item "Default" describe-specified-language-support))
179
180 ;; This should be a single character key binding because users use it
181 ;; very frequently while editing multilingual text. Now we can use
182 ;; only two such keys: "\C-\\" and "\C-^", but the latter is not
183 ;; convenient because it requires shifting on most keyboards. An
184 ;; alternative is "\C-\]" which is now bound to `abort-recursive-edit'
185 ;; but it won't be used that frequently.
186 (define-key global-map "\C-\\" 'toggle-input-method)
187
188 ;; This is no good because people often type Shift-SPC
189 ;; meaning to type SPC. -- rms.
190 ;; ;; Here's an alternative key binding for X users (Shift-SPACE).
191 ;; (define-key global-map [?\S- ] 'toggle-input-method)
192
193 ;;; Mule related hyperlinks.
194 (defconst help-xref-mule-regexp-template
195 (purecopy (concat "\\(\\<\\("
196 "\\(coding system\\)\\|"
197 "\\(input method\\)\\|"
198 "\\(character set\\)\\|"
199 "\\(charset\\)"
200 "\\)\\s-+\\)?"
201 ;; Note starting with word-syntax character:
202 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")))
203
204 (defun coding-system-change-eol-conversion (coding-system eol-type)
205 "Return a coding system which differs from CODING-SYSTEM in eol conversion.
206 The returned coding system converts end-of-line by EOL-TYPE
207 but text as the same way as CODING-SYSTEM.
208 EOL-TYPE should be `unix', `dos', `mac', or nil.
209 If EOL-TYPE is nil, the returned coding system detects
210 how end-of-line is formatted automatically while decoding.
211
212 EOL-TYPE can be specified by an integer 0, 1, or 2.
213 They means `unix', `dos', and `mac' respectively."
214 (if (symbolp eol-type)
215 (setq eol-type (cond ((eq eol-type 'unix) 0)
216 ((eq eol-type 'dos) 1)
217 ((eq eol-type 'mac) 2)
218 (t eol-type))))
219 (let ((orig-eol-type (coding-system-eol-type coding-system)))
220 (if (vectorp orig-eol-type)
221 (if (not eol-type)
222 coding-system
223 (aref orig-eol-type eol-type))
224 (let ((base (coding-system-base coding-system)))
225 (if (not eol-type)
226 base
227 (if (= eol-type orig-eol-type)
228 coding-system
229 (setq orig-eol-type (coding-system-eol-type base))
230 (if (vectorp orig-eol-type)
231 (aref orig-eol-type eol-type))))))))
232
233 (defun coding-system-change-text-conversion (coding-system coding)
234 "Return a coding system which differs from CODING-SYSTEM in text conversion.
235 The returned coding system converts text by CODING
236 but end-of-line as the same way as CODING-SYSTEM.
237 If CODING is nil, the returned coding system detects
238 how text is formatted automatically while decoding."
239 (let ((eol-type (coding-system-eol-type coding-system)))
240 (coding-system-change-eol-conversion
241 (if coding coding 'undecided)
242 (if (numberp eol-type) (aref [unix dos mac] eol-type)))))
243
244 (defun toggle-enable-multibyte-characters (&optional arg)
245 "Change whether this buffer uses multibyte characters.
246 With arg, use multibyte characters if the arg is positive.
247
248 Note that this command does not convert the byte contents of
249 the buffer; it only changes the way those bytes are interpreted.
250 In general, therefore, this command *changes* the sequence of
251 characters that the current buffer contains.
252
253 We suggest you avoid using use this command unless you know what you
254 are doing. If you use it by mistake, and the buffer is now displayed
255 wrong, use this command again to toggle back to the right mode."
256 (interactive "P")
257 (let ((new-flag
258 (if (null arg) (null enable-multibyte-characters)
259 (> (prefix-numeric-value arg) 0))))
260 (set-buffer-multibyte new-flag))
261 (force-mode-line-update))
262
263 (defun view-hello-file ()
264 "Display the HELLO file which list up many languages and characters."
265 (interactive)
266 ;; We have to decode the file in any environment.
267 (let ((default-enable-multibyte-characters t)
268 (coding-system-for-read 'iso-2022-7bit))
269 (view-file (expand-file-name "HELLO" data-directory))))
270
271 (defun universal-coding-system-argument (coding-system)
272 "Execute an I/O command using the specified coding system."
273 (interactive
274 (let ((default (and buffer-file-coding-system
275 (not (eq (coding-system-type buffer-file-coding-system)
276 t))
277 buffer-file-coding-system)))
278 (list (read-coding-system
279 (if default
280 (format "Coding system for following command (default %s): " default)
281 "Coding system for following command: ")
282 default))))
283 (let* ((keyseq (read-key-sequence
284 (format "Command to execute with %s:" coding-system)))
285 (cmd (key-binding keyseq))
286 prefix)
287
288 (when (eq cmd 'universal-argument)
289 (call-interactively cmd)
290
291 ;; Process keys bound in `universal-argument-map'.
292 (while (progn
293 (setq keyseq (read-key-sequence nil t)
294 cmd (key-binding keyseq t))
295 (not (eq cmd 'universal-argument-other-key)))
296 (let ((current-prefix-arg prefix-arg)
297 ;; Have to bind `last-command-char' here so that
298 ;; `digit-argument', for instance, can compute the
299 ;; prefix arg.
300 (last-command-char (aref keyseq 0)))
301 (call-interactively cmd)))
302
303 ;; This is the final call to `universal-argument-other-key', which
304 ;; set's the final `prefix-arg.
305 (let ((current-prefix-arg prefix-arg))
306 (call-interactively cmd))
307
308 ;; Read the command to execute with the given prefix arg.
309 (setq prefix prefix-arg
310 keyseq (read-key-sequence nil t)
311 cmd (key-binding keyseq)))
312
313 (let ((coding-system-for-read coding-system)
314 (coding-system-for-write coding-system)
315 (coding-system-require-warning t)
316 (current-prefix-arg prefix))
317 (message "")
318 (call-interactively cmd))))
319
320 (defun set-default-coding-systems (coding-system)
321 "Set default value of various coding systems to CODING-SYSTEM.
322 This sets the following coding systems:
323 o coding system of a newly created buffer
324 o default coding system for subprocess I/O
325 This also sets the following values:
326 o default value used as `file-name-coding-system' for converting file names
327 if CODING-SYSTEM is ASCII-compatible.
328 o default value for the command `set-terminal-coding-system' (not on MSDOS)
329 o default value for the command `set-keyboard-coding-system'
330 if CODING-SYSTEM is ASCII-compatible.."
331 (check-coding-system coding-system)
332 (setq-default buffer-file-coding-system coding-system)
333 (if (fboundp 'ucs-set-table-for-input)
334 (dolist (buffer (buffer-list))
335 (or (local-variable-p 'buffer-file-coding-system buffer)
336 (ucs-set-table-for-input buffer))))
337
338 (if (and default-enable-multibyte-characters (not (eq system-type 'darwin))
339 (or (not coding-system)
340 (not (coding-system-get coding-system 'ascii-incompatible))))
341 ;; The file-name coding system on Darwin systems is always utf-8.
342 (setq default-file-name-coding-system coding-system))
343 ;; If coding-system is nil, honor that on MS-DOS as well, so
344 ;; that they could reset the terminal coding system.
345 (unless (and (eq window-system 'pc) coding-system)
346 (setq default-terminal-coding-system coding-system))
347 (if (or (not coding-system)
348 (not (coding-system-get coding-system 'ascii-incompatible)))
349 (setq default-keyboard-coding-system coding-system))
350 ;; Preserve eol-type from existing default-process-coding-systems.
351 ;; On non-unix-like systems in particular, these may have been set
352 ;; carefully by the user, or by the startup code, to deal with the
353 ;; users shell appropriately, so should not be altered by changing
354 ;; language environment.
355 (let ((output-coding
356 (coding-system-change-text-conversion
357 (car default-process-coding-system) coding-system))
358 (input-coding
359 (coding-system-change-text-conversion
360 (cdr default-process-coding-system) coding-system)))
361 (setq default-process-coding-system
362 (cons output-coding input-coding))))
363
364 (defun prefer-coding-system (coding-system)
365 "Add CODING-SYSTEM at the front of the priority list for automatic detection.
366 This also sets the following coding systems:
367 o coding system of a newly created buffer
368 o default coding system for subprocess I/O
369 This also sets the following values:
370 o default value used as `file-name-coding-system' for converting file names.
371 o default value for the command `set-terminal-coding-system' (not on MSDOS)
372 o default value for the command `set-keyboard-coding-system'
373
374 If CODING-SYSTEM specifies a certain type of EOL conversion, the coding
375 systems set by this function will use that type of EOL conversion.
376
377 This command does not change the default value of terminal coding system
378 for MS-DOS terminal, because DOS terminals only support a single coding
379 system, and Emacs automatically sets the default to that coding system at
380 startup.
381
382 A coding system that requires automatic detection of text
383 encoding (e.g. undecided, unix) can't be preferred.
384
385 See also `coding-category-list' and `coding-system-category'."
386 (interactive "zPrefer coding system: ")
387 (if (not (and coding-system (coding-system-p coding-system)))
388 (error "Invalid coding system `%s'" coding-system))
389 (let ((coding-category (coding-system-category coding-system))
390 (base (coding-system-base coding-system))
391 (eol-type (coding-system-eol-type coding-system)))
392 (if (not coding-category)
393 ;; CODING-SYSTEM is no-conversion or undecided.
394 (error "Can't prefer the coding system `%s'" coding-system))
395 (set coding-category (or base coding-system))
396 ;; Changing the binding of a coding category requires this call.
397 (update-coding-systems-internal)
398 (or (eq coding-category (car coding-category-list))
399 ;; We must change the order.
400 (set-coding-priority (list coding-category)))
401 (if (and base (interactive-p))
402 (message "Highest priority is set to %s (base of %s)"
403 base coding-system))
404 ;; If they asked for specific EOL conversion, honor that.
405 (if (memq eol-type '(0 1 2))
406 (setq coding-system
407 (coding-system-change-eol-conversion base eol-type))
408 (setq coding-system base))
409 (set-default-coding-systems coding-system)))
410
411 (defvar sort-coding-systems-predicate nil
412 "If non-nil, a predicate function to sort coding systems.
413
414 It is called with two coding systems, and should return t if the first
415 one is \"less\" than the second.
416
417 The function `sort-coding-systems' use it.")
418
419 (defun sort-coding-systems (codings)
420 "Sort coding system list CODINGS by a priority of each coding system.
421 Returns the sorted list. CODINGS is modified by side effects.
422
423 If a coding system is most preferred, it has the highest priority.
424 Otherwise, a coding system corresponds to some MIME charset has higher
425 priorities. Among them, a coding system included in `coding-system'
426 key of the current language environment has higher priorities. See
427 also the documentation of `language-info-alist'.
428
429 If the variable `sort-coding-systems-predicate' (which see) is
430 non-nil, it is used to sort CODINGS in the different way than above."
431 (if sort-coding-systems-predicate
432 (sort codings sort-coding-systems-predicate)
433 (let* ((from-categories (mapcar #'(lambda (x) (symbol-value x))
434 coding-category-list))
435 (most-preferred (car from-categories))
436 (lang-preferred (get-language-info current-language-environment
437 'coding-system))
438 (func (function
439 (lambda (x)
440 (let ((base (coding-system-base x)))
441 ;; We calculate the priority number 0..255 by
442 ;; using the 8 bits PMMLCEII as this:
443 ;; P: 1 iff most preferred.
444 ;; MM: greater than 0 iff mime-charset.
445 ;; L: 1 iff one of the current lang. env.'s codings.
446 ;; C: 1 iff one of codings listed in the category list.
447 ;; E: 1 iff not XXX-with-esc
448 ;; II: if iso-2022 based, 0..3, else 1.
449 (logior
450 (lsh (if (eq base most-preferred) 1 0) 7)
451 (lsh
452 (let ((mime (coding-system-get base 'mime-charset)))
453 ;; Prefer coding systems corresponding to a
454 ;; MIME charset.
455 (if mime
456 ;; Lower utf-16 priority so that we
457 ;; normally prefer utf-8 to it, and put
458 ;; x-ctext below that.
459 (cond ((string-match "utf-16"
460 (symbol-name mime))
461 2)
462 ((string-match "^x-" (symbol-name mime))
463 1)
464 (t 3))
465 0))
466 5)
467 (lsh (if (memq base lang-preferred) 1 0) 4)
468 (lsh (if (memq base from-categories) 1 0) 3)
469 (lsh (if (string-match "-with-esc\\'"
470 (symbol-name base))
471 0 1) 2)
472 (if (eq (coding-system-type base) 2)
473 ;; For ISO based coding systems, prefer
474 ;; one that doesn't use escape sequences.
475 (let ((flags (coding-system-flags base)))
476 (if (or (consp (aref flags 0))
477 (consp (aref flags 1))
478 (consp (aref flags 2))
479 (consp (aref flags 3)))
480 (if (or (aref flags 8) (aref flags 9))
481 0
482 1)
483 2))
484 1)))))))
485 (sort codings (function (lambda (x y)
486 (> (funcall func x) (funcall func y))))))))
487
488 (defun find-coding-systems-region (from to)
489 "Return a list of proper coding systems to encode a text between FROM and TO.
490 If FROM is a string, find coding systems in that instead of the buffer.
491 All coding systems in the list can safely encode any multibyte characters
492 in the text.
493
494 If the text contains no multibyte characters, return a list of a single
495 element `undecided'."
496 (let ((codings (find-coding-systems-region-internal from to)))
497 (if (eq codings t)
498 ;; The text contains only ASCII characters. Any coding
499 ;; systems are safe.
500 '(undecided)
501 ;; We need copy-sequence because sorting will alter the argument.
502 (sort-coding-systems (copy-sequence codings)))))
503
504 (defun find-coding-systems-string (string)
505 "Return a list of proper coding systems to encode STRING.
506 All coding systems in the list can safely encode any multibyte characters
507 in STRING.
508
509 If STRING contains no multibyte characters, return a list of a single
510 element `undecided'."
511 (find-coding-systems-region string nil))
512
513 (defun find-coding-systems-for-charsets (charsets)
514 "Return a list of proper coding systems to encode characters of CHARSETS.
515 CHARSETS is a list of character sets.
516 It actually checks at most the first 96 characters of each charset.
517 So, if a charset of dimension two is included in CHARSETS, the value may
518 contain a coding system that can't encode all characters of the charset."
519 (cond ((or (null charsets)
520 (and (= (length charsets) 1)
521 (eq 'ascii (car charsets))))
522 '(undecided))
523 ((or (memq 'eight-bit-control charsets)
524 (memq 'eight-bit-graphic charsets))
525 '(raw-text emacs-mule))
526 (t
527 (let ((codings t)
528 charset l str)
529 (while (and codings charsets)
530 (setq charset (car charsets) charsets (cdr charsets))
531 (unless (eq charset 'ascii)
532 (setq str (make-string 96 32))
533 (if (= (charset-dimension charset) 1)
534 (if (= (charset-chars charset) 96)
535 (dotimes (i 96)
536 (aset str i (make-char charset (+ i 32))))
537 (dotimes (i 94)
538 (aset str i (make-char charset (+ i 33)))))
539 (if (= (charset-chars charset) 96)
540 (dotimes (i 96)
541 (aset str i (make-char charset 32 (+ i 32))))
542 (dotimes (i 94)
543 (aset str i (make-char charset 33 (+ i 33))))))
544 (setq l (find-coding-systems-string str))
545 (if (eq codings t)
546 (setq codings l)
547 (let ((ll nil))
548 (dolist (elt codings)
549 (if (memq elt l)
550 (setq ll (cons elt ll))))
551 (setq codings ll)))))
552 codings))))
553
554 (defun find-multibyte-characters (from to &optional maxcount excludes)
555 "Find multibyte characters in the region specified by FROM and TO.
556 If FROM is a string, find multibyte characters in the string.
557 The return value is an alist of the following format:
558 ((CHARSET COUNT CHAR ...) ...)
559 where
560 CHARSET is a character set,
561 COUNT is a number of characters,
562 CHARs are the characters found from the character set.
563 Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
564 Optional 4th arg EXCLUDE is a list of character sets to be ignored.
565
566 For invalid characters, CHARs are actually strings."
567 (let ((chars nil)
568 charset char)
569 (if (stringp from)
570 (let ((idx 0))
571 (while (setq idx (string-match "[^\000-\177]" from idx))
572 (setq char (aref from idx)
573 charset (char-charset char))
574 (if (eq charset 'unknown)
575 (setq char (match-string 0)))
576 (if (or (memq charset '(unknown
577 eight-bit-control eight-bit-graphic))
578 (not (or (eq excludes t) (memq charset excludes))))
579 (let ((slot (assq charset chars)))
580 (if slot
581 (if (not (memq char (nthcdr 2 slot)))
582 (let ((count (nth 1 slot)))
583 (setcar (cdr slot) (1+ count))
584 (if (or (not maxcount) (< count maxcount))
585 (nconc slot (list char)))))
586 (setq chars (cons (list charset 1 char) chars)))))
587 (setq idx (1+ idx))))
588 (save-excursion
589 (goto-char from)
590 (while (re-search-forward "[^\000-\177]" to t)
591 (setq char (preceding-char)
592 charset (char-charset char))
593 (if (eq charset 'unknown)
594 (setq char (match-string 0)))
595 (if (or (memq charset '(unknown eight-bit-control eight-bit-graphic))
596 (not (or (eq excludes t) (memq charset excludes))))
597 (let ((slot (assq charset chars)))
598 (if slot
599 (if (not (member char (nthcdr 2 slot)))
600 (let ((count (nth 1 slot)))
601 (setcar (cdr slot) (1+ count))
602 (if (or (not maxcount) (< count maxcount))
603 (nconc slot (list char)))))
604 (setq chars (cons (list charset 1 char) chars))))))))
605 (nreverse chars)))
606
607
608 (defun search-unencodable-char (coding-system)
609 "Search forward from point for a character that is not encodable.
610 It asks which coding system to check.
611 If such a character is found, set point after that character.
612 Otherwise, don't move point.
613
614 When called from a program, the value is a position of the found character,
615 or nil if all characters are encodable."
616 (interactive
617 (list (let ((default (or buffer-file-coding-system 'us-ascii)))
618 (read-coding-system
619 (format "Coding-system (default %s): " default)
620 default))))
621 (let ((pos (unencodable-char-position (point) (point-max) coding-system)))
622 (if pos
623 (goto-char (1+ pos))
624 (message "All following characters are encodable by %s" coding-system))
625 pos))
626
627
628 (defvar last-coding-system-specified nil
629 "Most recent coding system explicitly specified by the user when asked.
630 This variable is set whenever Emacs asks the user which coding system
631 to use in order to write a file. If you set it to nil explicitly,
632 then call `write-region', then afterward this variable will be non-nil
633 only if the user was explicitly asked and specified a coding system.")
634
635 (defvar select-safe-coding-system-accept-default-p nil
636 "If non-nil, a function to control the behavior of coding system selection.
637 The meaning is the same as the argument ACCEPT-DEFAULT-P of the
638 function `select-safe-coding-system' (which see). This variable
639 overrides that argument.")
640
641 (defun select-safe-coding-system-interactively (from to codings unsafe
642 &optional rejected default)
643 "Select interactively a coding system for the region FROM ... TO.
644 FROM can be a string, as in `write-region'.
645 CODINGS is the list of base coding systems known to be safe for this region,
646 typically obtained with `find-coding-systems-region'.
647 UNSAFE is a list of coding systems known to be unsafe for this region.
648 REJECTED is a list of coding systems which were safe but for some reason
649 were not recommended in the particular context.
650 DEFAULT is the coding system to use by default in the query."
651 ;; At first, if some defaults are unsafe, record at most 11
652 ;; problematic characters and their positions for them by turning
653 ;; (CODING ...)
654 ;; into
655 ;; ((CODING (POS . CHAR) (POS . CHAR) ...) ...)
656 (if unsafe
657 (setq unsafe
658 (mapcar #'(lambda (coding)
659 (cons coding
660 (if (stringp from)
661 (mapcar #'(lambda (pos)
662 (cons pos (aref from pos)))
663 (unencodable-char-position
664 0 (length from) coding
665 11 from))
666 (mapcar #'(lambda (pos)
667 (cons pos (char-after pos)))
668 (unencodable-char-position
669 from to coding 11)))))
670 unsafe)))
671
672 ;; Change each safe coding system to the corresponding
673 ;; mime-charset name if it is also a coding system. Such a name
674 ;; is more friendly to users.
675 (let ((l codings)
676 mime-charset)
677 (while l
678 (setq mime-charset (coding-system-get (car l) 'mime-charset))
679 (if (and mime-charset (coding-system-p mime-charset))
680 (setcar l mime-charset))
681 (setq l (cdr l))))
682
683 ;; Don't offer variations with locking shift, which you
684 ;; basically never want.
685 (let (l)
686 (dolist (elt codings (setq codings (nreverse l)))
687 (unless (or (eq 'coding-category-iso-7-else
688 (coding-system-category elt))
689 (eq 'coding-category-iso-8-else
690 (coding-system-category elt)))
691 (push elt l))))
692
693 ;; Remove raw-text, emacs-mule and no-conversion unless nothing
694 ;; else is available.
695 (setq codings
696 (or (delq 'raw-text
697 (delq 'emacs-mule
698 (delq 'no-conversion codings)))
699 '(raw-text emacs-mule no-conversion)))
700
701 (let ((window-configuration (current-window-configuration))
702 (bufname (buffer-name))
703 coding-system)
704 (save-excursion
705 ;; If some defaults are unsafe, make sure the offending
706 ;; buffer is displayed.
707 (when (and unsafe (not (stringp from)))
708 (pop-to-buffer bufname)
709 (goto-char (apply 'min (mapcar #'(lambda (x) (car (cadr x)))
710 unsafe))))
711 ;; Then ask users to select one from CODINGS while showing
712 ;; the reason why none of the defaults are not used.
713 (with-output-to-temp-buffer "*Warning*"
714 (with-current-buffer standard-output
715 (if (and (null rejected) (null unsafe))
716 (insert "No default coding systems to try for "
717 (if (stringp from)
718 (format "string \"%s\"." from)
719 (format "buffer `%s'." bufname)))
720 (insert
721 "These default coding systems were tried to encode"
722 (if (stringp from)
723 (concat " \"" (if (> (length from) 10)
724 (concat (substring from 0 10) "...\"")
725 (concat from "\"")))
726 (format " text\nin the buffer `%s'" bufname))
727 ":\n")
728 (let ((pos (point))
729 (fill-prefix " "))
730 (dolist (x (append rejected unsafe))
731 (princ " ") (princ (car x)))
732 (insert "\n")
733 (fill-region-as-paragraph pos (point)))
734 (when rejected
735 (insert "These safely encodes the target text,
736 but it is not recommended for encoding text in this context,
737 e.g., for sending an email message.\n ")
738 (dolist (x rejected)
739 (princ " ") (princ x))
740 (insert "\n"))
741 (when unsafe
742 (insert (if rejected "And the others"
743 "However, each of them")
744 " encountered these problematic characters:\n")
745 (dolist (coding unsafe)
746 (insert (format " %s:" (car coding)))
747 (let ((i 0)
748 (func1
749 #'(lambda (bufname pos)
750 (when (buffer-live-p (get-buffer bufname))
751 (pop-to-buffer bufname)
752 (goto-char pos))))
753 (func2
754 #'(lambda (bufname pos coding)
755 (when (buffer-live-p (get-buffer bufname))
756 (pop-to-buffer bufname)
757 (if (< (point) pos)
758 (goto-char pos)
759 (forward-char 1)
760 (search-unencodable-char coding)
761 (forward-char -1))))))
762 (dolist (elt (cdr coding))
763 (insert " ")
764 (if (stringp from)
765 (insert (if (< i 10) (cdr elt) "..."))
766 (if (< i 10)
767 (insert-text-button
768 (cdr elt)
769 :type 'help-xref
770 'help-echo
771 "mouse-2, RET: jump to this character"
772 'help-function func1
773 'help-args (list bufname (car elt)))
774 (insert-text-button
775 "..."
776 :type 'help-xref
777 'help-echo
778 "mouse-2, RET: next unencodable character"
779 'help-function func2
780 'help-args (list bufname (car elt)
781 (car coding)))))
782 (setq i (1+ i))))
783 (insert "\n"))
784 (insert "\
785 The first problematic character is at point in the displayed buffer,\n"
786 (substitute-command-keys "\
787 and \\[universal-argument] \\[what-cursor-position] will give information about it.\n"))))
788 (insert "\nSelect \
789 one of the following safe coding systems, or edit the buffer:\n")
790 (let ((pos (point))
791 (fill-prefix " "))
792 (dolist (x codings)
793 (princ " ") (princ x))
794 (insert "\n")
795 (fill-region-as-paragraph pos (point)))
796 (insert "Or specify any other coding system
797 at the risk of losing the problematic characters.\n")))
798
799 ;; Read a coding system.
800 (setq coding-system
801 (read-coding-system
802 (format "Select coding system (default %s): " default)
803 default))
804 (setq last-coding-system-specified coding-system))
805
806 (kill-buffer "*Warning*")
807 (set-window-configuration window-configuration)
808 coding-system))
809
810 (defun select-safe-coding-system (from to &optional default-coding-system
811 accept-default-p file)
812 "Ask a user to select a safe coding system from candidates.
813 The candidates of coding systems which can safely encode a text
814 between FROM and TO are shown in a popup window. Among them, the most
815 proper one is suggested as the default.
816
817 The list of `buffer-file-coding-system' of the current buffer,
818 the `default-buffer-file-coding-system', and the
819 most preferred coding system (if it corresponds to a MIME charset) is
820 treated as the default coding system list. Among them, the first one
821 that safely encodes the text is normally selected silently and
822 returned without any user interaction. See also the command
823 `prefer-coding-system'.
824
825 However, the user is queried if the chosen coding system is
826 inconsistent with what would be selected by `find-auto-coding' from
827 coding cookies &c. if the contents of the region were read from a
828 file. (That could lead to data corruption in a file subsequently
829 re-visited and edited.)
830
831 Optional 3rd arg DEFAULT-CODING-SYSTEM specifies a coding system or a
832 list of coding systems to be prepended to the default coding system
833 list. However, if DEFAULT-CODING-SYSTEM is a list and the first
834 element is t, the cdr part is used as the defualt coding system list,
835 i.e. `buffer-file-coding-system', `default-buffer-file-coding-system',
836 and the most preferred coding system are not used.
837
838 Optional 4th arg ACCEPT-DEFAULT-P, if non-nil, is a function to
839 determine the acceptability of the silently selected coding system.
840 It is called with that coding system, and should return nil if it
841 should not be silently selected and thus user interaction is required.
842
843 Optional 5th arg FILE is the file name to use for this purpose.
844 That is different from `buffer-file-name' when handling `write-region'
845 \(for example).
846
847 The variable `select-safe-coding-system-accept-default-p', if
848 non-nil, overrides ACCEPT-DEFAULT-P.
849
850 Kludgy feature: if FROM is a string, the string is the target text,
851 and TO is ignored."
852 (if (and default-coding-system
853 (not (listp default-coding-system)))
854 (setq default-coding-system (list default-coding-system)))
855
856 (let ((no-other-defaults nil)
857 auto-cs)
858 (unless (or (stringp from) find-file-literally)
859 ;; Find an auto-coding that is specified for the the current
860 ;; buffer and file from the region FROM and TO.
861 (save-excursion
862 (save-restriction
863 (widen)
864 (goto-char from)
865 (setq auto-cs (find-auto-coding (or file buffer-file-name "")
866 (- to from)))
867 (if auto-cs
868 (if (coding-system-p (car auto-cs))
869 (setq auto-cs (car auto-cs))
870 (display-warning
871 :warning
872 (format "\
873 Invalid coding system `%s' is specified
874 for the current buffer/file by the %s.
875 It is highly recommended to fix it before writing to a file."
876 (car auto-cs)
877 (if (eq (cdr auto-cs) :coding) ":coding tag"
878 (format "variable `%s'" (cdr auto-cs)))))
879 (or (yes-or-no-p "Really proceed with writing? ")
880 (error "Save aborted"))
881 (setq auto-cs nil))))))
882
883 (if (eq (car default-coding-system) t)
884 (setq no-other-defaults t
885 default-coding-system (cdr default-coding-system)))
886
887 ;; Change elements of the list to (coding . base-coding).
888 (setq default-coding-system
889 (mapcar (function (lambda (x) (cons x (coding-system-base x))))
890 default-coding-system))
891
892 (if (and auto-cs (not no-other-defaults))
893 ;; If the file has a coding cookie, try to use it before anything
894 ;; else (i.e. before default-coding-system which will typically come
895 ;; from file-coding-system-alist).
896 (let ((base (coding-system-base auto-cs)))
897 (or (memq base '(nil undecided))
898 (rassq base default-coding-system)
899 (push (cons auto-cs base) default-coding-system))))
900
901 ;; From now on, the list of defaults is reversed.
902 (setq default-coding-system (nreverse default-coding-system))
903
904 (unless no-other-defaults
905 ;; If buffer-file-coding-system is not nil nor undecided, append it
906 ;; to the defaults.
907 (if buffer-file-coding-system
908 (let ((base (coding-system-base buffer-file-coding-system)))
909 (or (eq base 'undecided)
910 (rassq base default-coding-system)
911 (push (cons buffer-file-coding-system base)
912 default-coding-system))))
913
914 ;; If default-buffer-file-coding-system is not nil nor undecided,
915 ;; append it to the defaults.
916 (if default-buffer-file-coding-system
917 (let ((base (coding-system-base default-buffer-file-coding-system)))
918 (or (eq base 'undecided)
919 (rassq base default-coding-system)
920 (push (cons default-buffer-file-coding-system base)
921 default-coding-system))))
922
923 ;; If the most preferred coding system has the property mime-charset,
924 ;; append it to the defaults.
925 (let ((tail coding-category-list)
926 preferred base)
927 (while (and tail (not (setq preferred (symbol-value (car tail)))))
928 (setq tail (cdr tail)))
929 (and (coding-system-p preferred)
930 (setq base (coding-system-base preferred))
931 (coding-system-get preferred 'mime-charset)
932 (not (rassq base default-coding-system))
933 (push (cons preferred base)
934 default-coding-system))))
935
936 (if select-safe-coding-system-accept-default-p
937 (setq accept-default-p select-safe-coding-system-accept-default-p))
938
939 (let ((codings (find-coding-systems-region from to))
940 (coding-system nil)
941 safe rejected unsafe)
942 (if (eq (car codings) 'undecided)
943 ;; Any coding system is ok.
944 (setq coding-system t)
945 ;; Classify the defaults into safe, rejected, and unsafe.
946 (dolist (elt default-coding-system)
947 (if (memq (cdr elt) codings)
948 (if (and (functionp accept-default-p)
949 (not (funcall accept-default-p (cdr elt))))
950 (push (car elt) rejected)
951 (push (car elt) safe))
952 (push (car elt) unsafe)))
953 (if safe
954 (setq coding-system (car safe))))
955
956 ;; If all the defaults failed, ask a user.
957 (when (not coding-system)
958 (setq coding-system (select-safe-coding-system-interactively
959 from to codings unsafe rejected (car codings))))
960
961 (if (vectorp (coding-system-eol-type coding-system))
962 (let ((eol (coding-system-eol-type buffer-file-coding-system)))
963 (if (numberp eol)
964 (setq coding-system
965 (coding-system-change-eol-conversion coding-system eol)))))
966
967 (if (eq coding-system t)
968 (setq coding-system buffer-file-coding-system))
969 ;; Check we're not inconsistent with what `coding:' spec &c would
970 ;; give when file is re-read.
971 ;; But don't do this if we explicitly ignored the cookie
972 ;; by using `find-file-literally'.
973 (when (and auto-cs
974 (not (and
975 coding-system
976 (memq (coding-system-type coding-system) '(0 5)))))
977 ;; Merge coding-system and auto-cs as far as possible.
978 (if (not coding-system)
979 (setq coding-system auto-cs)
980 (if (not auto-cs)
981 (setq auto-cs coding-system)
982 (let ((eol-type-1 (coding-system-eol-type coding-system))
983 (eol-type-2 (coding-system-eol-type auto-cs)))
984 (if (eq (coding-system-base coding-system) 'undecided)
985 (setq coding-system (coding-system-change-text-conversion
986 coding-system auto-cs))
987 (if (eq (coding-system-base auto-cs) 'undecided)
988 (setq auto-cs (coding-system-change-text-conversion
989 auto-cs coding-system))))
990 (if (vectorp eol-type-1)
991 (or (vectorp eol-type-2)
992 (setq coding-system (coding-system-change-eol-conversion
993 coding-system eol-type-2)))
994 (if (vectorp eol-type-2)
995 (setq auto-cs (coding-system-change-eol-conversion
996 auto-cs eol-type-1)))))))
997
998 (if (and auto-cs
999 ;; Don't barf if writing a compressed file, say.
1000 ;; This check perhaps isn't ideal, but is probably
1001 ;; the best thing to do.
1002 (not (auto-coding-alist-lookup (or file buffer-file-name "")))
1003 (not (coding-system-equal coding-system auto-cs)))
1004 (unless (yes-or-no-p
1005 (format "Selected encoding %s disagrees with \
1006 %s specified by file contents. Really save (else edit coding cookies \
1007 and try again)? " coding-system auto-cs))
1008 (error "Save aborted"))))
1009 coding-system)))
1010
1011 (setq select-safe-coding-system-function 'select-safe-coding-system)
1012
1013 (defun select-message-coding-system ()
1014 "Return a coding system to encode the outgoing message of the current buffer.
1015 It at first tries the first coding system found in these variables
1016 in this order:
1017 (1) local value of `buffer-file-coding-system'
1018 (2) value of `sendmail-coding-system'
1019 (3) value of `default-sendmail-coding-system'
1020 (4) value of `default-buffer-file-coding-system'
1021 If the found coding system can't encode the current buffer,
1022 or none of them are bound to a coding system,
1023 it asks the user to select a proper coding system."
1024 (let ((coding (or (and (local-variable-p 'buffer-file-coding-system)
1025 buffer-file-coding-system)
1026 sendmail-coding-system
1027 default-sendmail-coding-system
1028 default-buffer-file-coding-system)))
1029 (if (eq coding 'no-conversion)
1030 ;; We should never use no-conversion for outgoing mail.
1031 (setq coding nil))
1032 (if (fboundp select-safe-coding-system-function)
1033 (setq coding
1034 (funcall select-safe-coding-system-function
1035 (point-min) (point-max) coding
1036 (function (lambda (x)
1037 (coding-system-get x 'mime-charset))))))
1038 (if coding
1039 ;; Be sure to use LF for end-of-line.
1040 (setq coding (coding-system-change-eol-conversion coding 'unix))
1041 ;; No coding system is decided. Usually this is the case that
1042 ;; the current buffer contains only ASCII. So, we hope
1043 ;; iso-8859-1 works.
1044 (setq coding 'iso-8859-1-unix))
1045 coding))
1046 \f
1047 ;;; Language support stuff.
1048
1049 (defvar language-info-alist nil
1050 "Alist of language environment definitions.
1051 Each element looks like:
1052 (LANGUAGE-NAME . ((KEY . INFO) ...))
1053 where LANGUAGE-NAME is a string, the name of the language environment,
1054 KEY is a symbol denoting the kind of information, and
1055 INFO is the data associated with KEY.
1056 Meaningful values for KEY include
1057
1058 documentation value is documentation of what this language environment
1059 is meant for, and how to use it.
1060 charset value is a list of the character sets used by this
1061 language environment.
1062 sample-text value is an expression which is evalled to generate
1063 a line of text written using characters appropriate
1064 for this language environment.
1065 setup-function value is a function to call to switch to this
1066 language environment.
1067 exit-function value is a function to call to leave this
1068 language environment.
1069 coding-system value is a list of coding systems that are good
1070 for saving text written in this language environment.
1071 This list serves as suggestions to the user;
1072 in effect, as a kind of documentation.
1073 coding-priority value is a list of coding systems for this language
1074 environment, in order of decreasing priority.
1075 This is used to set up the coding system priority
1076 list when you switch to this language environment.
1077 nonascii-translation
1078 value is a translation table to be set in the
1079 variable `nonascii-translation-table' in this
1080 language environment, or a character set from
1081 which `nonascii-insert-offset' is calculated.
1082 input-method value is a default input method for this language
1083 environment.
1084 features value is a list of features requested in this
1085 language environment.
1086 ctext-non-standard-encodings
1087 value is a list of non-standard encoding
1088 names used in extended segments of CTEXT.
1089 See the variable
1090 `ctext-non-standard-encodings' for more
1091 detail.
1092
1093 The following keys take effect only when multibyte characters are
1094 globally disabled, i.e. the value of `default-enable-multibyte-characters'
1095 is nil.
1096
1097 unibyte-syntax value is a library name to load to set
1098 unibyte 8-bit character syntaxes for this
1099 language environment.
1100
1101 unibyte-display value is a coding system to encode characters
1102 for the terminal. Characters in the range
1103 of 160 to 255 display not as octal escapes,
1104 but as non-ASCII characters in this language
1105 environment.")
1106
1107 (defun get-language-info (lang-env key)
1108 "Return information listed under KEY for language environment LANG-ENV.
1109 KEY is a symbol denoting the kind of information.
1110 For a list of useful values for KEY and their meanings,
1111 see `language-info-alist'."
1112 (if (symbolp lang-env)
1113 (setq lang-env (symbol-name lang-env)))
1114 (let ((lang-slot (assoc-string lang-env language-info-alist t)))
1115 (if lang-slot
1116 (cdr (assq key (cdr lang-slot))))))
1117
1118 (defun set-language-info (lang-env key info)
1119 "Modify part of the definition of language environment LANG-ENV.
1120 Specifically, this stores the information INFO under KEY
1121 in the definition of this language environment.
1122 KEY is a symbol denoting the kind of information.
1123 INFO is the value for that information.
1124
1125 For a list of useful values for KEY and their meanings,
1126 see `language-info-alist'."
1127 (if (symbolp lang-env)
1128 (setq lang-env (symbol-name lang-env)))
1129 (set-language-info-internal lang-env key info)
1130 (if (equal lang-env current-language-environment)
1131 (cond ((eq key 'coding-priority)
1132 (set-language-environment-coding-systems lang-env))
1133 ((eq key 'input-method)
1134 (set-language-environment-input-method lang-env))
1135 ((eq key 'nonascii-translation)
1136 (set-language-environment-nonascii-translation lang-env))
1137 ((eq key 'charset)
1138 (set-language-environment-charset lang-env))
1139 ((eq key 'overriding-fontspec)
1140 (set-language-environment-fontset lang-env))
1141 ((and (not default-enable-multibyte-characters)
1142 (or (eq key 'unibyte-syntax) (eq key 'unibyte-display)))
1143 (set-language-environment-unibyte lang-env)))))
1144
1145 (defun set-language-info-internal (lang-env key info)
1146 "Internal use only.
1147 Arguments are the same as `set-language-info'."
1148 (let (lang-slot key-slot)
1149 (setq lang-slot (assoc lang-env language-info-alist))
1150 (if (null lang-slot) ; If no slot for the language, add it.
1151 (setq lang-slot (list lang-env)
1152 language-info-alist (cons lang-slot language-info-alist)))
1153 (setq key-slot (assq key lang-slot))
1154 (if (null key-slot) ; If no slot for the key, add it.
1155 (progn
1156 (setq key-slot (list key))
1157 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
1158 (setcdr key-slot (purecopy info))
1159 ;; Update the custom-type of `current-language-environment'.
1160 (put 'current-language-environment 'custom-type
1161 (cons 'choice (mapcar
1162 (lambda (lang)
1163 (list 'const lang))
1164 (sort (mapcar 'car language-info-alist) 'string<))))))
1165
1166 (defun set-language-info-alist (lang-env alist &optional parents)
1167 "Store ALIST as the definition of language environment LANG-ENV.
1168 ALIST is an alist of KEY and INFO values. See the documentation of
1169 `language-info-alist' for the meanings of KEY and INFO.
1170
1171 Optional arg PARENTS is a list of parent menu names; it specifies
1172 where to put this language environment in the
1173 Describe Language Environment and Set Language Environment menus.
1174 For example, (\"European\") means to put this language environment
1175 in the European submenu in each of those two menus."
1176 (if (symbolp lang-env)
1177 (setq lang-env (symbol-name lang-env)))
1178 (let ((describe-map describe-language-environment-map)
1179 (setup-map setup-language-environment-map))
1180 (if parents
1181 (let ((l parents)
1182 map parent-symbol parent prompt)
1183 (while l
1184 (if (symbolp (setq parent-symbol (car l)))
1185 (setq parent (symbol-name parent))
1186 (setq parent parent-symbol parent-symbol (intern parent)))
1187 (setq map (lookup-key describe-map (vector parent-symbol)))
1188 ;; This prompt string is for define-prefix-command, so
1189 ;; that the map it creates will be suitable for a menu.
1190 (or map (setq prompt (format "%s Environment" parent)))
1191 (if (not map)
1192 (progn
1193 (setq map (intern (format "describe-%s-environment-map"
1194 (downcase parent))))
1195 (define-prefix-command map nil prompt)
1196 (define-key-after describe-map (vector parent-symbol)
1197 (cons parent map) t)))
1198 (setq describe-map (symbol-value map))
1199 (setq map (lookup-key setup-map (vector parent-symbol)))
1200 (if (not map)
1201 (progn
1202 (setq map (intern (format "setup-%s-environment-map"
1203 (downcase parent))))
1204 (define-prefix-command map nil prompt)
1205 (define-key-after setup-map (vector parent-symbol)
1206 (cons parent map) t)))
1207 (setq setup-map (symbol-value map))
1208 (setq l (cdr l)))))
1209
1210 ;; Set up menu items for this language env.
1211 (let ((doc (assq 'documentation alist)))
1212 (when doc
1213 (define-key-after describe-map (vector (intern lang-env))
1214 (cons lang-env 'describe-specified-language-support) t)))
1215 (define-key-after setup-map (vector (intern lang-env))
1216 (cons lang-env 'setup-specified-language-environment) t)
1217
1218 (dolist (elt alist)
1219 (set-language-info-internal lang-env (car elt) (cdr elt)))
1220
1221 (if (equal lang-env current-language-environment)
1222 (set-language-environment lang-env))))
1223
1224 (defun read-language-name (key prompt &optional default)
1225 "Read a language environment name which has information for KEY.
1226 If KEY is nil, read any language environment.
1227 Prompt with PROMPT. DEFAULT is the default choice of language environment.
1228 This returns a language environment name as a string."
1229 (let* ((completion-ignore-case t)
1230 (name (completing-read prompt
1231 language-info-alist
1232 (and key
1233 (function (lambda (elm) (and (listp elm) (assq key elm)))))
1234 t nil nil default)))
1235 (if (and (> (length name) 0)
1236 (or (not key)
1237 (get-language-info name key)))
1238 name)))
1239 \f
1240 ;;; Multilingual input methods.
1241 (defgroup leim nil
1242 "LEIM: Libraries of Emacs Input Methods."
1243 :group 'mule)
1244
1245 (defconst leim-list-file-name "leim-list.el"
1246 "Name of LEIM list file.
1247 This file contains a list of libraries of Emacs input methods (LEIM)
1248 in the format of Lisp expression for registering each input method.
1249 Emacs loads this file at startup time.")
1250
1251 (defvar leim-list-header (format
1252 ";;; %s -- list of LEIM (Library of Emacs Input Method) -*-coding: iso-2022-7bit;-*-
1253 ;;
1254 ;; This file contains a list of LEIM (Library of Emacs Input Method)
1255 ;; methods in the same directory as this file. Loading this file
1256 ;; registers all the input methods in Emacs.
1257 ;;
1258 ;; Each entry has the form:
1259 ;; (register-input-method
1260 ;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
1261 ;; TITLE DESCRIPTION
1262 ;; ARG ...)
1263 ;; See the function `register-input-method' for the meanings of the arguments.
1264 ;;
1265 ;; If this directory is included in load-path, Emacs automatically
1266 ;; loads this file at startup time.
1267
1268 "
1269 leim-list-file-name)
1270 "Header to be inserted in LEIM list file.")
1271
1272 (defvar leim-list-entry-regexp "^(register-input-method"
1273 "Regexp matching head of each entry in LEIM list file.
1274 See also the variable `leim-list-header'")
1275
1276 (defvar update-leim-list-functions
1277 '(quail-update-leim-list-file)
1278 "List of functions to call to update LEIM list file.
1279 Each function is called with one arg, LEIM directory name.")
1280
1281 (defun update-leim-list-file (&rest dirs)
1282 "Update LEIM list file in directories DIRS."
1283 (let ((functions update-leim-list-functions))
1284 (while functions
1285 (apply (car functions) dirs)
1286 (setq functions (cdr functions)))))
1287
1288 (defvar current-input-method nil
1289 "The current input method for multilingual text.
1290 If nil, that means no input method is activated now.")
1291 (make-variable-buffer-local 'current-input-method)
1292 (put 'current-input-method 'permanent-local t)
1293
1294 (defvar current-input-method-title nil
1295 "Title string of the current input method shown in mode line.")
1296 (make-variable-buffer-local 'current-input-method-title)
1297 (put 'current-input-method-title 'permanent-local t)
1298
1299 (defcustom default-input-method nil
1300 "*Default input method for multilingual text (a string).
1301 This is the input method activated automatically by the command
1302 `toggle-input-method' (\\[toggle-input-method])."
1303 :link '(custom-manual "(emacs)Input Methods")
1304 :group 'mule
1305 :type '(choice (const nil) (string
1306 :completion-ignore-case t
1307 :complete-function widget-string-complete
1308 :completion-alist input-method-alist
1309 :prompt-history input-method-history))
1310 :set-after '(current-language-environment))
1311
1312 (put 'input-method-function 'permanent-local t)
1313
1314 (defvar input-method-history nil
1315 "History list for some commands that read input methods.")
1316 (make-variable-buffer-local 'input-method-history)
1317 (put 'input-method-history 'permanent-local t)
1318
1319 (defvar inactivate-current-input-method-function nil
1320 "Function to call for inactivating the current input method.
1321 Every input method should set this to an appropriate value when activated.
1322 This function is called with no argument.
1323
1324 This function should never change the value of `current-input-method'.
1325 It is set to nil by the function `inactivate-input-method'.")
1326 (make-variable-buffer-local 'inactivate-current-input-method-function)
1327 (put 'inactivate-current-input-method-function 'permanent-local t)
1328
1329 (defvar describe-current-input-method-function nil
1330 "Function to call for describing the current input method.
1331 This function is called with no argument.")
1332 (make-variable-buffer-local 'describe-current-input-method-function)
1333 (put 'describe-current-input-method-function 'permanent-local t)
1334
1335 (defvar input-method-alist nil
1336 "Alist of input method names vs how to use them.
1337 Each element has the form:
1338 (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
1339 See the function `register-input-method' for the meanings of the elements.")
1340
1341 (defun register-input-method (input-method lang-env &rest args)
1342 "Register INPUT-METHOD as an input method for language environment LANG-ENV.
1343 INPUT-METHOD and LANG-ENV are symbols or strings.
1344
1345 The remaining arguments are:
1346 ACTIVATE-FUNC, TITLE, DESCRIPTION, and ARGS...
1347 ACTIVATE-FUNC is a function to call to activate this method.
1348 TITLE is a string to show in the mode line when this method is active.
1349 DESCRIPTION is a string describing this method and what it is good for.
1350 The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
1351 All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
1352
1353 This function is mainly used in the file \"leim-list.el\" which is
1354 created at Emacs build time, registering all Quail input methods
1355 contained in the Emacs distribution.
1356
1357 In case you want to register a new Quail input method by yourself, be
1358 careful to use the same input method title as given in the third
1359 parameter of `quail-define-package'. (If the values are different, the
1360 string specified in this function takes precedence.)
1361
1362 The commands `describe-input-method' and `list-input-methods' need
1363 these duplicated values to show some information about input methods
1364 without loading the relevant Quail packages."
1365 (if (symbolp lang-env)
1366 (setq lang-env (symbol-name lang-env)))
1367 (if (symbolp input-method)
1368 (setq input-method (symbol-name input-method)))
1369 (let ((info (cons lang-env args))
1370 (slot (assoc input-method input-method-alist)))
1371 (if slot
1372 (setcdr slot info)
1373 (setq slot (cons input-method info))
1374 (setq input-method-alist (cons slot input-method-alist)))))
1375
1376 (defun read-input-method-name (prompt &optional default inhibit-null)
1377 "Read a name of input method from a minibuffer prompting with PROMPT.
1378 If DEFAULT is non-nil, use that as the default,
1379 and substitute it into PROMPT at the first `%s'.
1380 If INHIBIT-NULL is non-nil, null input signals an error.
1381
1382 The return value is a string."
1383 (if default
1384 (setq prompt (format prompt default)))
1385 (let* ((completion-ignore-case t)
1386 ;; As it is quite normal to change input method in the
1387 ;; minibuffer, we must enable it even if
1388 ;; enable-recursive-minibuffers is currently nil.
1389 (enable-recursive-minibuffers t)
1390 ;; This binding is necessary because input-method-history is
1391 ;; buffer local.
1392 (input-method (completing-read prompt input-method-alist
1393 nil t nil 'input-method-history
1394 default)))
1395 (if (and input-method (symbolp input-method))
1396 (setq input-method (symbol-name input-method)))
1397 (if (> (length input-method) 0)
1398 input-method
1399 (if inhibit-null
1400 (error "No valid input method is specified")))))
1401
1402 (defun activate-input-method (input-method)
1403 "Switch to input method INPUT-METHOD for the current buffer.
1404 If some other input method is already active, turn it off first.
1405 If INPUT-METHOD is nil, deactivate any current input method."
1406 (if (and input-method (symbolp input-method))
1407 (setq input-method (symbol-name input-method)))
1408 (if (and current-input-method
1409 (not (string= current-input-method input-method)))
1410 (inactivate-input-method))
1411 (unless (or current-input-method (null input-method))
1412 (let ((slot (assoc input-method input-method-alist)))
1413 (if (null slot)
1414 (error "Can't activate input method `%s'" input-method))
1415 (setq current-input-method-title nil)
1416 (let ((func (nth 2 slot)))
1417 (if (functionp func)
1418 (apply (nth 2 slot) input-method (nthcdr 5 slot))
1419 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
1420 (progn
1421 (require (cdr func))
1422 (apply (car func) input-method (nthcdr 5 slot)))
1423 (error "Can't activate input method `%s'" input-method))))
1424 (setq current-input-method input-method)
1425 (or (stringp current-input-method-title)
1426 (setq current-input-method-title (nth 3 slot)))
1427 (unwind-protect
1428 (run-hooks 'input-method-activate-hook)
1429 (force-mode-line-update)))))
1430
1431 (defun inactivate-input-method ()
1432 "Turn off the current input method."
1433 (when current-input-method
1434 (if input-method-history
1435 (unless (string= current-input-method (car input-method-history))
1436 (setq input-method-history
1437 (cons current-input-method
1438 (delete current-input-method input-method-history))))
1439 (setq input-method-history (list current-input-method)))
1440 (unwind-protect
1441 (funcall inactivate-current-input-method-function)
1442 (unwind-protect
1443 (run-hooks 'input-method-inactivate-hook)
1444 (setq current-input-method nil
1445 input-method-function nil
1446 current-input-method-title nil)
1447 (force-mode-line-update)))))
1448
1449 (defun set-input-method (input-method &optional interactive)
1450 "Select and activate input method INPUT-METHOD for the current buffer.
1451 This also sets the default input method to the one you specify.
1452 If INPUT-METHOD is nil, this function turns off the input method, and
1453 also causes you to be prompted for a name of an input method the next
1454 time you invoke \\[toggle-input-method].
1455 When called interactively, the optional arg INTERACTIVE is non-nil,
1456 which marks the variable `default-input-method' as set for Custom buffers.
1457
1458 To deactivate the input method interactively, use \\[toggle-input-method].
1459 To deactivate it programmatically, use \\[inactivate-input-method]."
1460 (interactive
1461 (let* ((default (or (car input-method-history) default-input-method)))
1462 (list (read-input-method-name
1463 (if default "Select input method (default %s): " "Select input method: ")
1464 default t)
1465 t)))
1466 (activate-input-method input-method)
1467 (setq default-input-method input-method)
1468 (when interactive
1469 (customize-mark-as-set 'default-input-method))
1470 default-input-method)
1471
1472 (defun toggle-input-method (&optional arg interactive)
1473 "Enable or disable multilingual text input method for the current buffer.
1474 Only one input method can be enabled at any time in a given buffer.
1475
1476 The normal action is to enable an input method if none was
1477 enabled, and disable the current one otherwise. Which input method
1478 to enable can be determined in various ways--either the one most
1479 recently used, or the one specified by `default-input-method', or
1480 as a last resort by reading the name of an input method in the
1481 minibuffer.
1482
1483 With a prefix argument, read an input method name with the minibuffer
1484 and enable that one. The default is the most recent input method specified
1485 \(not including the currently active input method, if any).
1486
1487 When called interactively, the optional arg INTERACTIVE is non-nil,
1488 which marks the variable `default-input-method' as set for Custom buffers."
1489
1490 (interactive "P\np")
1491 (if (and current-input-method (not arg))
1492 (inactivate-input-method)
1493 (let ((default (or (car input-method-history) default-input-method)))
1494 (if (and arg default (equal current-input-method default)
1495 (> (length input-method-history) 1))
1496 (setq default (nth 1 input-method-history)))
1497 (activate-input-method
1498 (if (or arg (not default))
1499 (progn
1500 (read-input-method-name
1501 (if default "Input method (default %s): " "Input method: " )
1502 default t))
1503 default))
1504 (unless default-input-method
1505 (prog1
1506 (setq default-input-method current-input-method)
1507 (when interactive
1508 (customize-mark-as-set 'default-input-method)))))))
1509
1510 (defun describe-input-method (input-method)
1511 "Describe input method INPUT-METHOD."
1512 (interactive
1513 (list (read-input-method-name
1514 "Describe input method (default current choice): ")))
1515 (if (and input-method (symbolp input-method))
1516 (setq input-method (symbol-name input-method)))
1517 (help-setup-xref (list #'describe-input-method
1518 (or input-method current-input-method))
1519 (interactive-p))
1520
1521 (if (null input-method)
1522 (describe-current-input-method)
1523 (let ((current current-input-method))
1524 (condition-case nil
1525 (progn
1526 (save-excursion
1527 (activate-input-method input-method)
1528 (describe-current-input-method))
1529 (activate-input-method current))
1530 (error
1531 (activate-input-method current)
1532 (help-setup-xref (list #'describe-input-method input-method)
1533 (interactive-p))
1534 (with-output-to-temp-buffer (help-buffer)
1535 (let ((elt (assoc input-method input-method-alist)))
1536 (princ (format
1537 "Input method: %s (`%s' in mode line) for %s\n %s\n"
1538 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt))))))))))
1539
1540 (defun describe-current-input-method ()
1541 "Describe the input method currently in use.
1542 This is a subroutine for `describe-input-method'."
1543 (if current-input-method
1544 (if (and (symbolp describe-current-input-method-function)
1545 (fboundp describe-current-input-method-function))
1546 (funcall describe-current-input-method-function)
1547 (message "No way to describe the current input method `%s'"
1548 current-input-method)
1549 (ding))
1550 (error "No input method is activated now")))
1551
1552 (defun read-multilingual-string (prompt &optional initial-input input-method)
1553 "Read a multilingual string from minibuffer, prompting with string PROMPT.
1554 The input method selected last time is activated in minibuffer.
1555 If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
1556 initially.
1557 Optional 3rd argument INPUT-METHOD specifies the input method
1558 to be activated instead of the one selected last time. It is a symbol
1559 or a string."
1560 (setq input-method
1561 (or input-method
1562 current-input-method
1563 default-input-method
1564 (read-input-method-name "Input method: " nil t)))
1565 (if (and input-method (symbolp input-method))
1566 (setq input-method (symbol-name input-method)))
1567 (let ((prev-input-method current-input-method))
1568 (unwind-protect
1569 (progn
1570 (activate-input-method input-method)
1571 (read-string prompt initial-input nil nil t))
1572 (activate-input-method prev-input-method))))
1573
1574 ;; Variables to control behavior of input methods. All input methods
1575 ;; should react to these variables.
1576
1577 (defcustom input-method-verbose-flag 'default
1578 "*A flag to control extra guidance given by input methods.
1579 The value should be nil, t, `complex-only', or `default'.
1580
1581 The extra guidance is done by showing list of available keys in echo
1582 area. When you use the input method in the minibuffer, the guidance
1583 is shown at the bottom short window (split from the existing window).
1584
1585 If the value is t, extra guidance is always given, if the value is
1586 nil, extra guidance is always suppressed.
1587
1588 If the value is `complex-only', only complex input methods such as
1589 `chinese-py' and `japanese' give extra guidance.
1590
1591 If the value is `default', complex input methods always give extra
1592 guidance, but simple input methods give it only when you are not in
1593 the minibuffer.
1594
1595 See also the variable `input-method-highlight-flag'."
1596 :type '(choice (const :tag "Always" t) (const :tag "Never" nil)
1597 (const complex-only) (const default))
1598 :group 'mule)
1599
1600 (defcustom input-method-highlight-flag t
1601 "*If this flag is non-nil, input methods highlight partially-entered text.
1602 For instance, while you are in the middle of a Quail input method sequence,
1603 the text inserted so far is temporarily underlined.
1604 The underlining goes away when you finish or abort the input method sequence.
1605 See also the variable `input-method-verbose-flag'."
1606 :type 'boolean
1607 :group 'mule)
1608
1609 (defvar input-method-activate-hook nil
1610 "Normal hook run just after an input method is activated.
1611
1612 The variable `current-input-method' keeps the input method name
1613 just activated.")
1614
1615 (defvar input-method-inactivate-hook nil
1616 "Normal hook run just after an input method is inactivated.
1617
1618 The variable `current-input-method' still keeps the input method name
1619 just inactivated.")
1620
1621 (defvar input-method-after-insert-chunk-hook nil
1622 "Normal hook run just after an input method insert some chunk of text.")
1623
1624 (defvar input-method-exit-on-first-char nil
1625 "This flag controls when an input method returns.
1626 Usually, the input method does not return while there's a possibility
1627 that it may find a different translation if a user types another key.
1628 But, it this flag is non-nil, the input method returns as soon as
1629 the current key sequence gets long enough to have some valid translation.")
1630
1631 (defvar input-method-use-echo-area nil
1632 "This flag controls how an input method shows an intermediate key sequence.
1633 Usually, the input method inserts the intermediate key sequence,
1634 or candidate translations corresponding to the sequence,
1635 at point in the current buffer.
1636 But, if this flag is non-nil, it displays them in echo area instead.")
1637
1638 (defvar input-method-exit-on-invalid-key nil
1639 "This flag controls the behavior of an input method on invalid key input.
1640 Usually, when a user types a key which doesn't start any character
1641 handled by the input method, the key is handled by turning off the
1642 input method temporarily. After that key, the input method is re-enabled.
1643 But, if this flag is non-nil, the input method is never back on.")
1644
1645 \f
1646 (defvar set-language-environment-hook nil
1647 "Normal hook run after some language environment is set.
1648
1649 When you set some hook function here, that effect usually should not
1650 be inherited to another language environment. So, you had better set
1651 another function in `exit-language-environment-hook' (which see) to
1652 cancel the effect.")
1653
1654 (defvar exit-language-environment-hook nil
1655 "Normal hook run after exiting from some language environment.
1656 When this hook is run, the variable `current-language-environment'
1657 is still bound to the language environment being exited.
1658
1659 This hook is mainly used for canceling the effect of
1660 `set-language-environment-hook' (which-see).")
1661
1662 (put 'setup-specified-language-environment 'apropos-inhibit t)
1663
1664 (defun setup-specified-language-environment ()
1665 "Switch to a specified language environment."
1666 (interactive)
1667 (let (language-name)
1668 (if (and (symbolp last-command-event)
1669 (or (not (eq last-command-event 'Default))
1670 (setq last-command-event 'English))
1671 (setq language-name (symbol-name last-command-event)))
1672 (prog1
1673 (set-language-environment language-name)
1674 (customize-mark-as-set 'current-language-environment))
1675 (error "Bogus calling sequence"))))
1676
1677 (defcustom current-language-environment "English"
1678 "The last language environment specified with `set-language-environment'.
1679 This variable should be set only with \\[customize], which is equivalent
1680 to using the function `set-language-environment'."
1681 :link '(custom-manual "(emacs)Language Environments")
1682 :set (lambda (symbol value) (set-language-environment value))
1683 :get (lambda (x)
1684 (or (car-safe (assoc-string
1685 (if (symbolp current-language-environment)
1686 (symbol-name current-language-environment)
1687 current-language-environment)
1688 language-info-alist t))
1689 "English"))
1690 ;; custom type will be updated with `set-language-info'.
1691 :type (if language-info-alist
1692 (cons 'choice (mapcar
1693 (lambda (lang)
1694 (list 'const lang))
1695 (sort (mapcar 'car language-info-alist) 'string<)))
1696 'string)
1697 :initialize 'custom-initialize-default
1698 :group 'mule)
1699
1700 (defun reset-language-environment ()
1701 "Reset multilingual environment of Emacs to the default status.
1702
1703 The default status is as follows:
1704
1705 The default value of `buffer-file-coding-system' is nil.
1706 The default coding system for process I/O is nil.
1707 The default value for the command `set-terminal-coding-system' is nil.
1708 The default value for the command `set-keyboard-coding-system' is nil.
1709
1710 The order of priorities of coding categories and the coding system
1711 bound to each category are as follows
1712 coding category coding system
1713 --------------------------------------------------
1714 coding-category-iso-8-1 iso-latin-1
1715 coding-category-iso-8-2 iso-latin-1
1716 coding-category-utf-8 mule-utf-8
1717 coding-category-utf-16-be mule-utf-16be-with-signature
1718 coding-category-utf-16-le mule-utf-16le-with-signature
1719 coding-category-iso-7-tight iso-2022-jp
1720 coding-category-iso-7 iso-2022-7bit
1721 coding-category-iso-7-else iso-2022-7bit-lock
1722 coding-category-iso-8-else iso-2022-8bit-ss2
1723 coding-category-emacs-mule emacs-mule
1724 coding-category-raw-text raw-text
1725 coding-category-sjis japanese-shift-jis
1726 coding-category-big5 chinese-big5
1727 coding-category-ccl nil
1728 coding-category-binary no-conversion"
1729 (interactive)
1730 ;; This function formerly set default-enable-multibyte-characters to t,
1731 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1732
1733 (setq coding-category-iso-7-tight 'iso-2022-jp
1734 coding-category-iso-7 'iso-2022-7bit
1735 coding-category-iso-8-1 'iso-latin-1
1736 coding-category-iso-8-2 'iso-latin-1
1737 coding-category-iso-7-else 'iso-2022-7bit-lock
1738 coding-category-iso-8-else 'iso-2022-8bit-ss2
1739 coding-category-emacs-mule 'emacs-mule
1740 coding-category-raw-text 'raw-text
1741 coding-category-sjis 'japanese-shift-jis
1742 coding-category-big5 'chinese-big5
1743 coding-category-utf-16-be 'mule-utf-16be-with-signature
1744 coding-category-utf-16-le 'mule-utf-16le-with-signature
1745 coding-category-utf-8 'mule-utf-8
1746 coding-category-ccl nil
1747 coding-category-binary 'no-conversion)
1748
1749 (set-coding-priority
1750 '(coding-category-iso-8-1
1751 coding-category-iso-8-2
1752 coding-category-utf-8
1753 coding-category-utf-16-be
1754 coding-category-utf-16-le
1755 coding-category-iso-7-tight
1756 coding-category-iso-7
1757 coding-category-iso-7-else
1758 coding-category-iso-8-else
1759 coding-category-emacs-mule
1760 coding-category-raw-text
1761 coding-category-sjis
1762 coding-category-big5
1763 coding-category-ccl
1764 coding-category-binary))
1765
1766 ;; Changing the binding of a coding category requires this call.
1767 (update-coding-systems-internal)
1768
1769 (set-default-coding-systems nil)
1770 (setq default-sendmail-coding-system 'iso-latin-1)
1771 ;; On Darwin systems, this should be utf-8, but when this file is loaded
1772 ;; utf-8 is not yet defined, so we set it in set-locale-environment instead.
1773 (setq default-file-name-coding-system 'iso-latin-1)
1774 ;; Preserve eol-type from existing default-process-coding-systems.
1775 ;; On non-unix-like systems in particular, these may have been set
1776 ;; carefully by the user, or by the startup code, to deal with the
1777 ;; users shell appropriately, so should not be altered by changing
1778 ;; language environment.
1779 (let ((output-coding
1780 ;; When bootstrapping, coding-systems are not defined yet, so
1781 ;; we need to catch the error from check-coding-system.
1782 (condition-case nil
1783 (coding-system-change-text-conversion
1784 (car default-process-coding-system) 'undecided)
1785 (coding-system-error 'undecided)))
1786 (input-coding
1787 (condition-case nil
1788 (coding-system-change-text-conversion
1789 (cdr default-process-coding-system) 'iso-latin-1)
1790 (coding-system-error 'iso-latin-1))))
1791 (setq default-process-coding-system
1792 (cons output-coding input-coding)))
1793
1794 ;; Don't alter the terminal and keyboard coding systems here.
1795 ;; The terminal still supports the same coding system
1796 ;; that it supported a minute ago.
1797 ;; (set-terminal-coding-system-internal nil)
1798 ;; (set-keyboard-coding-system-internal nil)
1799
1800 (setq nonascii-translation-table nil
1801 nonascii-insert-offset 0)
1802
1803 ;; Don't invoke fontset-related functions if fontsets aren't
1804 ;; supported in this build of Emacs.
1805 (and (fboundp 'fontset-list)
1806 (set-overriding-fontspec-internal nil)))
1807
1808 (reset-language-environment)
1809
1810 (defun set-display-table-and-terminal-coding-system (language-name &optional coding-system)
1811 "Set up the display table and terminal coding system for LANGUAGE-NAME."
1812 (let ((coding (get-language-info language-name 'unibyte-display)))
1813 (if (and coding
1814 (or (not coding-system)
1815 (coding-system-equal coding coding-system)))
1816 (standard-display-european-internal)
1817 ;; The following 2 lines undo the 8-bit display that we set up
1818 ;; in standard-display-european-internal, which see. This is in
1819 ;; case the user has used standard-display-european earlier in
1820 ;; this session. (The MS-DOS port doesn't use that setup, so it
1821 ;; doesn't need to undo it.)
1822 (when standard-display-table
1823 (dotimes (i 128)
1824 (aset standard-display-table (+ i 128) nil))))
1825 (or (eq window-system 'pc)
1826 (set-terminal-coding-system (or coding-system coding)))))
1827
1828 (defun set-language-environment (language-name)
1829 "Set up multi-lingual environment for using LANGUAGE-NAME.
1830 This sets the coding system priority and the default input method
1831 and sometimes other things. LANGUAGE-NAME should be a string
1832 which is the name of a language environment. For example, \"Latin-1\"
1833 specifies the character set for the major languages of Western Europe."
1834 (interactive (list (read-language-name
1835 nil
1836 "Set language environment (default English): ")))
1837 (if language-name
1838 (if (symbolp language-name)
1839 (setq language-name (symbol-name language-name)))
1840 (setq language-name "English"))
1841 (let ((slot (assoc-string language-name language-info-alist t)))
1842 (unless slot
1843 (error "Language environment not defined: %S" language-name))
1844 (setq language-name (car slot)))
1845 (if current-language-environment
1846 (let ((func (get-language-info current-language-environment
1847 'exit-function)))
1848 (run-hooks 'exit-language-environment-hook)
1849 (if (functionp func) (funcall func))))
1850
1851 (reset-language-environment)
1852 ;; The features might set up coding systems.
1853 (let ((required-features (get-language-info language-name 'features)))
1854 (while required-features
1855 (require (car required-features))
1856 (setq required-features (cdr required-features))))
1857
1858 (setq current-language-environment language-name)
1859
1860 (set-language-environment-coding-systems language-name)
1861 (set-language-environment-input-method language-name)
1862 (set-language-environment-nonascii-translation language-name)
1863 (set-language-environment-charset language-name)
1864 (set-language-environment-fontset language-name)
1865 ;; Unibyte setups if necessary.
1866 (unless default-enable-multibyte-characters
1867 (set-language-environment-unibyte language-name))
1868
1869 (let ((func (get-language-info language-name 'setup-function)))
1870 (if (functionp func)
1871 (funcall func)))
1872
1873 (run-hooks 'set-language-environment-hook)
1874 (force-mode-line-update t))
1875
1876 (defun standard-display-european-internal ()
1877 ;; Actually set up direct output of non-ASCII characters.
1878 (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
1879 ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
1880 ;; the native font, and codes 160 and 146 stand for something very
1881 ;; different there.
1882 (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
1883 (progn
1884 ;; Most X fonts used to do the wrong thing for latin-1 code 160.
1885 (unless (and (eq window-system 'x)
1886 ;; XFree86 4 has fixed the fonts.
1887 (string= "The XFree86 Project, Inc" (x-server-vendor))
1888 (> (aref (number-to-string (nth 2 (x-server-version))) 0)
1889 ?3))
1890 ;; Make non-line-break space display as a plain space.
1891 (aset standard-display-table 160 [32]))
1892 ;; Most Windows programs send out apostrophes as \222. Most X fonts
1893 ;; don't contain a character at that position. Map it to the ASCII
1894 ;; apostrophe. [This is actually RIGHT SINGLE QUOTATION MARK,
1895 ;; U+2019, normally from the windows-1252 character set. XFree 4
1896 ;; fonts probably have the appropriate glyph at this position,
1897 ;; so they could use standard-display-8bit. It's better to use a
1898 ;; proper windows-1252 coding system. --fx]
1899 (aset standard-display-table 146 [39]))))
1900
1901 (defun set-language-environment-coding-systems (language-name)
1902 "Do various coding system setups for language environment LANGUAGE-NAME."
1903 (let* ((priority (get-language-info language-name 'coding-priority))
1904 (default-coding (car priority))
1905 (eol-type (coding-system-eol-type default-buffer-file-coding-system)))
1906 (if priority
1907 (let ((categories (mapcar 'coding-system-category priority)))
1908 (set-default-coding-systems
1909 (if (memq eol-type '(0 1 2 unix dos mac))
1910 (coding-system-change-eol-conversion default-coding eol-type)
1911 default-coding))
1912 (setq default-sendmail-coding-system default-coding)
1913 (set-coding-priority categories)
1914 (while priority
1915 (set (car categories) (car priority))
1916 (setq priority (cdr priority) categories (cdr categories)))
1917 ;; Changing the binding of a coding category requires this call.
1918 (update-coding-systems-internal)))))
1919
1920 (defun set-language-environment-input-method (language-name)
1921 "Do various input method setups for language environment LANGUAGE-NAME."
1922 (let ((input-method (get-language-info language-name 'input-method)))
1923 (when input-method
1924 (setq default-input-method input-method)
1925 (if input-method-history
1926 (setq input-method-history
1927 (cons input-method
1928 (delete input-method input-method-history)))))))
1929
1930 (defun set-language-environment-nonascii-translation (language-name)
1931 "Do unibyte/multibyte translation setup for language environment LANGUAGE-NAME."
1932 (let ((nonascii (get-language-info language-name 'nonascii-translation))
1933 (dos-table
1934 (if (eq window-system 'pc)
1935 (intern
1936 (format "cp%d-nonascii-translation-table" dos-codepage)))))
1937 (cond
1938 ((char-table-p nonascii)
1939 (setq nonascii-translation-table nonascii))
1940 ((and (eq window-system 'pc) (boundp dos-table))
1941 ;; DOS terminals' default is to use a special non-ASCII translation
1942 ;; table as appropriate for the installed codepage.
1943 (setq nonascii-translation-table (symbol-value dos-table)))
1944 ((charsetp nonascii)
1945 (setq nonascii-insert-offset (- (make-char nonascii) 128))))))
1946
1947 (defun set-language-environment-charset (language-name)
1948 "Do various charset setups for language environment LANGUAGE-NAME."
1949 (if (and utf-translate-cjk-mode
1950 (not (eq utf-translate-cjk-lang-env language-name))
1951 (catch 'tag
1952 (dolist (charset (get-language-info language-name 'charset))
1953 (if (memq charset utf-translate-cjk-charsets)
1954 (throw 'tag t)))
1955 nil))
1956 (utf-translate-cjk-load-tables)))
1957
1958 (defun set-language-environment-fontset (language-name)
1959 "Do various fontset setups for language environment LANGUAGE-NAME."
1960 ;; Don't invoke fontset-related functions if fontsets aren't
1961 ;; supported in this build of Emacs.
1962 (if (fboundp 'fontset-list)
1963 (set-overriding-fontspec-internal
1964 (get-language-info language-name 'overriding-fontspec))))
1965
1966 (defun set-language-environment-unibyte (language-name)
1967 "Do various unibyte-mode setups for language environment LANGUAGE-NAME."
1968 ;; Syntax and case table.
1969 (let ((syntax (get-language-info language-name 'unibyte-syntax)))
1970 (if syntax
1971 (let ((set-case-syntax-set-multibyte nil))
1972 (load syntax nil t))
1973 ;; No information for syntax and case. Reset to the defaults.
1974 (let ((syntax-table (standard-syntax-table))
1975 (standard-table (standard-case-table))
1976 (case-table (make-char-table 'case-table))
1977 (ch (if (eq window-system 'pc) 128 160)))
1978 (while (< ch 256)
1979 (modify-syntax-entry ch " " syntax-table)
1980 (setq ch (1+ ch)))
1981 (dotimes (i 128)
1982 (aset case-table i (aref standard-table i)))
1983 (set-char-table-extra-slot case-table 0 nil)
1984 (set-char-table-extra-slot case-table 1 nil)
1985 (set-char-table-extra-slot case-table 2 nil)
1986 (set-standard-case-table case-table))
1987 (let ((list (buffer-list)))
1988 (while list
1989 (with-current-buffer (car list)
1990 (set-case-table (standard-case-table)))
1991 (setq list (cdr list))))))
1992 (set-display-table-and-terminal-coding-system language-name))
1993
1994 (defsubst princ-list (&rest args)
1995 "Print all arguments with `princ', then print \"\n\"."
1996 (while args (princ (car args)) (setq args (cdr args)))
1997 (princ "\n"))
1998
1999 (put 'describe-specified-language-support 'apropos-inhibit t)
2000
2001 ;; Print language-specific information such as input methods,
2002 ;; charsets, and coding systems. This function is intended to be
2003 ;; called from the menu:
2004 ;; [menu-bar mule describe-language-environment LANGUAGE]
2005 ;; and should not run it by `M-x describe-current-input-method-function'.
2006 (defun describe-specified-language-support ()
2007 "Describe how Emacs supports the specified language environment."
2008 (interactive)
2009 (let (language-name)
2010 (if (not (and (symbolp last-command-event)
2011 (or (not (eq last-command-event 'Default))
2012 (setq last-command-event 'English))
2013 (setq language-name (symbol-name last-command-event))))
2014 (error "Bogus calling sequence"))
2015 (describe-language-environment language-name)))
2016
2017 (defun describe-language-environment (language-name)
2018 "Describe how Emacs supports language environment LANGUAGE-NAME."
2019 (interactive
2020 (list (read-language-name
2021 'documentation
2022 "Describe language environment (default current choice): ")))
2023 (if (null language-name)
2024 (setq language-name current-language-environment))
2025 (if (or (null language-name)
2026 (null (get-language-info language-name 'documentation)))
2027 (error "No documentation for the specified language"))
2028 (if (symbolp language-name)
2029 (setq language-name (symbol-name language-name)))
2030 (dolist (feature (get-language-info language-name 'features))
2031 (require feature))
2032 (let ((doc (get-language-info language-name 'documentation)))
2033 (help-setup-xref (list #'describe-language-environment language-name)
2034 (interactive-p))
2035 (with-output-to-temp-buffer (help-buffer)
2036 (save-excursion
2037 (set-buffer standard-output)
2038 (insert language-name " language environment\n\n")
2039 (if (stringp doc)
2040 (insert doc "\n\n"))
2041 (condition-case nil
2042 (let ((str (eval (get-language-info language-name 'sample-text))))
2043 (if (stringp str)
2044 (insert "Sample text:\n " str "\n\n")))
2045 (error nil))
2046 (let ((input-method (get-language-info language-name 'input-method))
2047 (l (copy-sequence input-method-alist)))
2048 (insert "Input methods")
2049 (when input-method
2050 (insert " (default " input-method ")")
2051 (setq input-method (assoc input-method input-method-alist))
2052 (setq l (cons input-method (delete input-method l))))
2053 (insert ":\n")
2054 (while l
2055 (when (string= language-name (nth 1 (car l)))
2056 (insert " " (car (car l)))
2057 (search-backward (car (car l)))
2058 (help-xref-button 0 'help-input-method (car (car l)))
2059 (goto-char (point-max))
2060 (insert " (\""
2061 (if (stringp (nth 3 (car l)))
2062 (nth 3 (car l))
2063 (car (nth 3 (car l))))
2064 "\" in mode line)\n"))
2065 (setq l (cdr l)))
2066 (insert "\n"))
2067 (insert "Character sets:\n")
2068 (let ((l (get-language-info language-name 'charset)))
2069 (if (null l)
2070 (insert " nothing specific to " language-name "\n")
2071 (while l
2072 (insert " " (symbol-name (car l)))
2073 (search-backward (symbol-name (car l)))
2074 (help-xref-button 0 'help-character-set (car l))
2075 (goto-char (point-max))
2076 (insert ": " (charset-description (car l)) "\n")
2077 (setq l (cdr l)))))
2078 (insert "\n")
2079 (insert "Coding systems:\n")
2080 (let ((l (get-language-info language-name 'coding-system)))
2081 (if (null l)
2082 (insert " nothing specific to " language-name "\n")
2083 (while l
2084 (insert " " (symbol-name (car l)))
2085 (search-backward (symbol-name (car l)))
2086 (help-xref-button 0 'help-coding-system (car l))
2087 (goto-char (point-max))
2088 (insert " (`"
2089 (coding-system-mnemonic (car l))
2090 "' in mode line):\n\t"
2091 (coding-system-doc-string (car l))
2092 "\n")
2093 (let ((aliases (coding-system-get (car l)
2094 'alias-coding-systems)))
2095 (when aliases
2096 (insert "\t(alias:")
2097 (while aliases
2098 (insert " " (symbol-name (car aliases)))
2099 (setq aliases (cdr aliases)))
2100 (insert ")\n")))
2101 (setq l (cdr l)))))))))
2102 \f
2103 ;;; Locales.
2104
2105 (defvar locale-translation-file-name nil
2106 "File name for the system's file of locale-name aliases, or nil if none.")
2107
2108 ;; The following definitions might as well be marked as constants and
2109 ;; purecopied, since they're normally used on startup, and probably
2110 ;; should reflect the facilities of the base Emacs.
2111 (defconst locale-language-names
2112 (purecopy
2113 '(
2114 ;; Locale names of the form LANGUAGE[_TERRITORY][.CODESET][@MODIFIER]
2115 ;; as specified in the Single Unix Spec, Version 2.
2116 ;; LANGUAGE is a language code taken from ISO 639:1988 (E/F)
2117 ;; with additions from ISO 639/RA Newsletter No.1/1989;
2118 ;; see Internet RFC 2165 (1997-06) and
2119 ;; http://www.evertype.com/standards/iso639/iso639-en.html
2120 ;; TERRITORY is a country code taken from ISO 3166
2121 ;; http://www.din.de/gremien/nas/nabd/iso3166ma/codlstp1/en_listp1.html.
2122 ;; CODESET and MODIFIER are implementation-dependent.
2123
2124 ;; jasonr comments: MS Windows uses three letter codes for
2125 ;; languages instead of the two letter ISO codes that POSIX
2126 ;; uses. In most cases the first two letters are the same, so
2127 ;; most of the regexps in locale-language-names work. Japanese
2128 ;; and Chinese are exceptions, which are listed in the
2129 ;; non-standard section at the bottom of locale-language-names.
2130
2131 ("aa_DJ" . "Latin-1") ; Afar
2132 ("aa" . "UTF-8")
2133 ;; ab Abkhazian
2134 ("af" . "Latin-1") ; Afrikaans
2135 ("am" "Ethiopic" utf-8) ; Amharic
2136 ("an" . "Latin-9") ; Aragonese
2137 ; ar Arabic glibc uses 8859-6
2138 ; as Assamese
2139 ; ay Aymara
2140 ("az" . "UTF-8") ; Azerbaijani
2141 ; ba Bashkir
2142 ("be" "Belarusian" cp1251) ; Belarusian [Byelorussian until early 1990s]
2143 ("bg" "Bulgarian" cp1251) ; Bulgarian
2144 ; bh Bihari
2145 ; bi Bislama
2146 ("bn" . "UTF-8") ; Bengali, Bangla
2147 ("bo" . "Tibetan")
2148 ("br" . "Latin-1") ; Breton
2149 ("bs" . "Latin-2") ; Bosnian
2150 ("byn" . "UTF-8") ; Bilin; Blin
2151 ("ca" . "Latin-1") ; Catalan
2152 ; co Corsican
2153 ("cs" "Czech" iso-8859-2)
2154 ("cy" "Welsh" iso-8859-14)
2155 ("da" . "Latin-1") ; Danish
2156 ("de" "German" iso-8859-1)
2157 ; dv Divehi
2158 ; dz Bhutani
2159 ("el" "Greek" iso-8859-7)
2160 ;; Users who specify "en" explicitly typically want Latin-1, not ASCII.
2161 ;; That's actually what the GNU locales define, modulo things like
2162 ;; en_IN -- fx.
2163 ("en_IN" "English" utf-8) ; glibc uses utf-8 for English in India
2164 ("en" "English" iso-8859-1) ; English
2165 ("eo" . "Latin-3") ; Esperanto
2166 ("es" "Spanish" iso-8859-1)
2167 ("et" . "Latin-1") ; Estonian
2168 ("eu" . "Latin-1") ; Basque
2169 ("fa" . "UTF-8") ; Persian
2170 ("fi" . "Latin-1") ; Finnish
2171 ("fj" . "Latin-1") ; Fiji
2172 ("fo" . "Latin-1") ; Faroese
2173 ("fr" "French" iso-8859-1) ; French
2174 ("fy" . "Latin-1") ; Frisian
2175 ("ga" . "Latin-1") ; Irish Gaelic (new orthography)
2176 ("gd" . "Latin-9") ; Scots Gaelic
2177 ("gez" "Ethiopic" utf-8) ; Geez
2178 ("gl" . "Latin-1") ; Gallegan; Galician
2179 ; gn Guarani
2180 ("gu" . "UTF-8") ; Gujarati
2181 ("gv" . "Latin-1") ; Manx Gaelic
2182 ; ha Hausa
2183 ("he" "Hebrew" iso-8859-8)
2184 ("hi" "Devanagari" utf-8) ; Hindi
2185 ("hr" "Croatian" iso-8859-2) ; Croatian
2186 ("hu" . "Latin-2") ; Hungarian
2187 ; hy Armenian
2188 ; ia Interlingua
2189 ("id" . "Latin-1") ; Indonesian
2190 ; ie Interlingue
2191 ; ik Inupiak
2192 ("is" . "Latin-1") ; Icelandic
2193 ("it" "Italian" iso-8859-1) ; Italian
2194 ; iu Inuktitut
2195 ("iw" "Hebrew" iso-8859-8)
2196 ("ja" "Japanese" euc-jp)
2197 ; jw Javanese
2198 ("ka" "Georgian" georgian-ps) ; Georgian
2199 ; kk Kazakh
2200 ("kl" . "Latin-1") ; Greenlandic
2201 ; km Cambodian
2202 ("kn" "Kannada" utf-8)
2203 ("ko" "Korean" euc-kr)
2204 ; ks Kashmiri
2205 ; ku Kurdish
2206 ("kw" . "Latin-1") ; Cornish
2207 ; ky Kirghiz
2208 ("la" . "Latin-1") ; Latin
2209 ("lb" . "Latin-1") ; Luxemburgish
2210 ("lg" . "Laint-6") ; Ganda
2211 ; ln Lingala
2212 ("lo" "Lao" utf-8) ; Laothian
2213 ("lt" "Lithuanian" iso-8859-13)
2214 ("lv" . "Latvian") ; Latvian, Lettish
2215 ; mg Malagasy
2216 ("mi" . "Latin-7") ; Maori
2217 ("mk" "Cyrillic-ISO" iso-8859-5) ; Macedonian
2218 ("ml" "Malayalam" utf-8)
2219 ("mn" . "UTF-8") ; Mongolian
2220 ; mo Moldavian
2221 ("mr" "Devanagari" utf-8) ; Marathi
2222 ("ms" . "Latin-1") ; Malay
2223 ("mt" . "Latin-3") ; Maltese
2224 ; my Burmese
2225 ; na Nauru
2226 ("nb" . "Latin-1") ; Norwegian
2227 ("ne" "Devanagari" utf-8) ; Nepali
2228 ("nl" "Dutch" iso-8859-1)
2229 ("no" . "Latin-1") ; Norwegian
2230 ("oc" . "Latin-1") ; Occitan
2231 ("om_ET" . "UTF-8") ; (Afan) Oromo
2232 ("om" . "Latin-1") ; (Afan) Oromo
2233 ; or Oriya
2234 ("pa" . "UTF-8") ; Punjabi
2235 ("pl" . "Latin-2") ; Polish
2236 ; ps Pashto, Pushto
2237 ("pt" . "Latin-1") ; Portuguese
2238 ; qu Quechua
2239 ("rm" . "Latin-1") ; Rhaeto-Romanic
2240 ; rn Kirundi
2241 ("ro" "Romanian" iso-8859-2)
2242 ("ru_RU" "Russian" iso-8859-5)
2243 ("ru_UA" "Russian" koi8-u)
2244 ; rw Kinyarwanda
2245 ("sa" . "Devanagari") ; Sanskrit
2246 ; sd Sindhi
2247 ("se" . "UTF-8") ; Northern Sami
2248 ; sg Sangho
2249 ("sh" . "Latin-2") ; Serbo-Croatian
2250 ; si Sinhalese
2251 ("sid" . "UTF-8") ; Sidamo
2252 ("sk" "Slovak" iso-8859-2)
2253 ("sl" "Slovenian" iso-8859-2)
2254 ; sm Samoan
2255 ; sn Shona
2256 ("so_ET" "UTF-8") ; Somali
2257 ("so" "Latin-1") ; Somali
2258 ("sq" . "Latin-1") ; Albanian
2259 ("sr_YU@cyrillic" . "Cyrillic-ISO") ; Serbian (Cyrillic alphabet)
2260 ("sr" . "Latin-2") ; Serbian (Latin alphabet)
2261 ; ss Siswati
2262 ("st" . "Latin-1") ; Sesotho
2263 ; su Sundanese
2264 ("sv" "Swedish" iso-8859-1) ; Swedish
2265 ("sw" . "Latin-1") ; Swahili
2266 ("ta" "Tamil" utf-8)
2267 ("te" . "UTF-8") ; Telugu
2268 ("tg" "Tajik" koi8-t)
2269 ("th" "Thai" tis-620)
2270 ("ti" "Ethiopic" utf-8) ; Tigrinya
2271 ("tig_ER" . "UTF-8") ; Tigre
2272 ; tk Turkmen
2273 ("tl" . "Latin-1") ; Tagalog
2274 ; tn Setswana
2275 ; to Tonga
2276 ("tr" "Turkish" iso-8859-9)
2277 ; ts Tsonga
2278 ("tt" . "UTF-8") ; Tatar
2279 ; tw Twi
2280 ; ug Uighur
2281 ("uk" "Ukrainian" koi8-u)
2282 ("ur" . "UTF-8") ; Urdu
2283 ("uz_UZ@cyrillic" . "UTF-8"); Uzbek
2284 ("uz" . "Latin-1") ; Uzbek
2285 ("vi" "Vietnamese" utf-8)
2286 ; vo Volapuk
2287 ("wa" . "Latin-1") ; Walloon
2288 ; wo Wolof
2289 ("xh" . "Latin-1") ; Xhosa
2290 ("yi" . "Windows-1255") ; Yiddish
2291 ; yo Yoruba
2292 ; za Zhuang
2293 ("zh_HK" . "Chinese-Big5")
2294 ("zh_TW" . "Chinese-Big5")
2295 ("zh_CN" . "Chinese-GB")
2296 ("zh" . "Chinese-GB")
2297 ; zh_CN.GB18030/GB18030 \
2298 ; zh_CN.GBK/GBK \
2299 ; zh_HK/BIG5-HKSCS \
2300 ("zu" . "Latin-1") ; Zulu
2301
2302 ;; ISO standard locales
2303 ("c$" . "ASCII")
2304 ("posix$" . "ASCII")
2305
2306 ;; The "IPA" Emacs language environment does not correspond
2307 ;; to any ISO 639 code, so let it stand for itself.
2308 ("ipa$" . "IPA")
2309
2310 ;; Nonstandard or obsolete language codes
2311 ("cz" . "Czech") ; e.g. Solaris 2.6
2312 ("ee" . "Latin-4") ; Estonian, e.g. X11R6.4
2313 ("iw" . "Hebrew") ; e.g. X11R6.4
2314 ("sp" . "Cyrillic-ISO") ; Serbian (Cyrillic alphabet), e.g. X11R6.4
2315 ("su" . "Latin-1") ; Finnish, e.g. Solaris 2.6
2316 ("jp" . "Japanese") ; e.g. MS Windows
2317 ("chs" . "Chinese-GB") ; MS Windows Chinese Simplified
2318 ("cht" . "Chinese-BIG5") ; MS Windows Chinese Traditional
2319 ("gbz" . "UTF-8") ; MS Windows Dari Persian
2320 ("div" . "UTF-8") ; MS Windows Divehi (Maldives)
2321 ("wee" . "Latin-2") ; MS Windows Lower Sorbian
2322 ("wen" . "Latin-2") ; MS Windows Upper Sorbian
2323 ))
2324 "Alist of locale regexps vs the corresponding languages and coding systems.
2325 Each element has these form:
2326 \(LOCALE-REGEXP LANG-ENV CODING-SYSTEM)
2327 The first element whose LOCALE-REGEXP matches the start of a
2328 downcased locale specifies the LANG-ENV \(language environtment)
2329 and CODING-SYSTEM corresponding to that locale. If there is no
2330 appropriate language environment, the element may have this form:
2331 \(LOCALE-REGEXP . LANG-ENV)
2332 In this case, LANG-ENV is one of generic language environments for an
2333 specific encoding such as \"Latin-1\" and \"UTF-8\".")
2334
2335 (defconst locale-charset-language-names
2336 (purecopy
2337 '((".*8859[-_]?1\\>" . "Latin-1")
2338 (".*8859[-_]?2\\>" . "Latin-2")
2339 (".*8859[-_]?3\\>" . "Latin-3")
2340 (".*8859[-_]?4\\>" . "Latin-4")
2341 (".*8859[-_]?9\\>" . "Latin-5")
2342 (".*8859[-_]?14\\>" . "Latin-8")
2343 (".*8859[-_]?15\\>" . "Latin-9")
2344 (".*utf\\(?:-?8\\)?\\>" . "UTF-8")
2345 ;; utf-8@euro exists, so put this last. (@euro really specifies
2346 ;; the currency, rather than the charset.)
2347 (".*@euro\\>" . "Latin-9")))
2348 "List of pairs of locale regexps and charset language names.
2349 The first element whose locale regexp matches the start of a downcased locale
2350 specifies the language name whose charset corresponds to that locale.
2351 This language name is used if the locale is not listed in
2352 `locale-language-names'")
2353
2354 (defconst locale-preferred-coding-systems
2355 (purecopy
2356 '((".*8859[-_]?1\\>" . iso-8859-1)
2357 (".*8859[-_]?2\\>" . iso-8859-2)
2358 (".*8859[-_]?3\\>" . iso-8859-3)
2359 (".*8859[-_]?4\\>" . iso-8859-4)
2360 (".*8859[-_]?9\\>" . iso-8859-9)
2361 (".*8859[-_]?14\\>" . iso-8859-14)
2362 (".*8859[-_]?15\\>" . iso-8859-15)
2363 (".*utf\\(?:-?8\\)?" . utf-8)
2364 ;; utf-8@euro exists, so put this after utf-8. (@euro really
2365 ;; specifies the currency, rather than the charset.)
2366 (".*@euro" . iso-8859-15)
2367 ("koi8-?r" . koi8-r)
2368 ("koi8-?u" . koi8-u)
2369 ("tcvn" . tcvn)
2370 ("big5" . big5)
2371 ("euc-?tw" . euc-tw)
2372 ;; We don't support GBK, but as it is upper compatible with
2373 ;; GB-2312, we setup the default coding system to gb2312.
2374 ("gbk" . gb2312)
2375 ;; We don't support BIG5-HKSCS, but as it is upper compatible with
2376 ;; BIG5, we setup the default coding system to big5.
2377 ("big5hkscs" . big5)
2378 ("ja.*[._]euc" . japanese-iso-8bit)
2379 ("ja.*[._]jis7" . iso-2022-jp)
2380 ("ja.*[._]pck" . japanese-shift-jis)
2381 ("ja.*[._]sjis" . japanese-shift-jis)
2382 ("jpn" . japanese-shift-jis) ; MS-Windows uses this.
2383 ))
2384 "List of pairs of locale regexps and preferred coding systems.
2385 The first element whose locale regexp matches the start of a downcased locale
2386 specifies the coding system to prefer when using that locale.
2387 This coding system is used if the locale specifies a specific charset.")
2388
2389 (defun locale-name-match (key alist)
2390 "Search for KEY in ALIST, which should be a list of regexp-value pairs.
2391 Return the value corresponding to the first regexp that matches the
2392 start of KEY, or nil if there is no match."
2393 (let (element)
2394 (while (and alist (not element))
2395 (if (string-match (concat "\\`\\(?:" (car (car alist)) "\\)") key)
2396 (setq element (car alist)))
2397 (setq alist (cdr alist)))
2398 (cdr element)))
2399
2400 (defun locale-charset-match-p (charset1 charset2)
2401 "Whether charset names (strings) CHARSET1 and CHARSET2 are equivalent.
2402 Matching is done ignoring case and any hyphens and underscores in the
2403 names. E.g. `ISO_8859-1' and `iso88591' both match `iso-8859-1'."
2404 (setq charset1 (replace-regexp-in-string "[-_]" "" charset1))
2405 (setq charset2 (replace-regexp-in-string "[-_]" "" charset2))
2406 (eq t (compare-strings charset1 nil nil charset2 nil nil t)))
2407
2408 (defvar locale-charset-alist nil
2409 "Coding system alist keyed on locale-style charset name.
2410 Used by `locale-charset-to-coding-system'.")
2411
2412 (defun locale-charset-to-coding-system (charset)
2413 "Find coding system corresponding to CHARSET.
2414 CHARSET is any sort of non-Emacs charset name, such as might be used
2415 in a locale codeset, or elsewhere. It is matched to a coding system
2416 first by case-insensitive lookup in `locale-charset-alist'. Then
2417 matches are looked for in the coding system list, treating case and
2418 the characters `-' and `_' as insignificant. The coding system base
2419 is returned. Thus, for instance, if charset \"ISO8859-2\",
2420 `iso-latin-2' is returned."
2421 (or (car (assoc-string charset locale-charset-alist t))
2422 (let ((cs coding-system-alist)
2423 c)
2424 (while (and (not c) cs)
2425 (if (locale-charset-match-p charset (caar cs))
2426 (setq c (intern (caar cs)))
2427 (pop cs)))
2428 (if c (coding-system-base c)))))
2429
2430 ;; Fixme: This ought to deal with the territory part of the locale
2431 ;; too, for setting things such as calendar holidays, ps-print paper
2432 ;; size, spelling dictionary.
2433
2434 (defun set-locale-environment (&optional locale-name)
2435 "Set up multi-lingual environment for using LOCALE-NAME.
2436 This sets the language environment, the coding system priority,
2437 the default input method and sometimes other things.
2438
2439 LOCALE-NAME should be a string which is the name of a locale supported
2440 by the system. Often it is of the form xx_XX.CODE, where xx is a
2441 language, XX is a country, and CODE specifies a character set and
2442 coding system. For example, the locale name \"ja_JP.EUC\" might name
2443 a locale for Japanese in Japan using the `japanese-iso-8bit'
2444 coding-system. The name may also have a modifier suffix, e.g. `@euro'
2445 or `@cyrillic'.
2446
2447 If LOCALE-NAME is nil, its value is taken from the environment
2448 variables LC_ALL, LC_CTYPE and LANG (the first one that is set).
2449
2450 The locale names supported by your system can typically be found in a
2451 directory named `/usr/share/locale' or `/usr/lib/locale'. LOCALE-NAME
2452 will be translated according to the table specified by
2453 `locale-translation-file-name'.
2454
2455 See also `locale-charset-language-names', `locale-language-names',
2456 `locale-preferred-coding-systems' and `locale-coding-system'."
2457 (interactive "sSet environment for locale: ")
2458
2459 ;; Do this at runtime for the sake of binaries possibly transported
2460 ;; to a system without X.
2461 (setq locale-translation-file-name
2462 (let ((files
2463 '("/usr/share/X11/locale/locale.alias" ; e.g. X11R7
2464 "/usr/lib/X11/locale/locale.alias" ; e.g. X11R6.4
2465 "/usr/X11R6/lib/X11/locale/locale.alias" ; XFree86, e.g. RedHat 4.2
2466 "/usr/openwin/lib/locale/locale.alias" ; e.g. Solaris 2.6
2467 ;;
2468 ;; The following name appears after the X-related names above,
2469 ;; since the X-related names are what X actually uses.
2470 "/usr/share/locale/locale.alias" ; GNU/Linux sans X
2471 )))
2472 (while (and files (not (file-exists-p (car files))))
2473 (setq files (cdr files)))
2474 (car files)))
2475
2476 (let ((locale locale-name))
2477
2478 (unless locale
2479 ;; Use the first of these three environment variables
2480 ;; that has a nonempty value.
2481 (let ((vars '("LC_ALL" "LC_CTYPE" "LANG")))
2482 (while (and vars
2483 (= 0 (length locale))) ; nil or empty string
2484 (setq locale (getenv (pop vars))))))
2485
2486 (unless locale
2487 ;; The two tests are kept separate so the byte-compiler sees
2488 ;; that mac-get-preference is only called after checking its existence.
2489 (when (fboundp 'mac-get-preference)
2490 (setq locale (mac-get-preference "AppleLocale"))
2491 (unless locale
2492 (let ((languages (mac-get-preference "AppleLanguages")))
2493 (unless (= (length languages) 0) ; nil or empty vector
2494 (setq locale (aref languages 0)))))))
2495 (unless (or locale (not (boundp 'mac-system-locale)))
2496 (setq locale mac-system-locale))
2497
2498 (when locale
2499
2500 ;; Translate "swedish" into "sv_SE.ISO8859-1", and so on,
2501 ;; using the translation file that many systems have.
2502 (when locale-translation-file-name
2503 (with-temp-buffer
2504 (insert-file-contents locale-translation-file-name)
2505 (when (re-search-forward
2506 (concat "^" (regexp-quote locale) ":?[ \t]+") nil t)
2507 (setq locale (buffer-substring (point) (line-end-position))))))
2508
2509 ;; Leave the system locales alone if the caller did not specify
2510 ;; an explicit locale name, as their defaults are set from
2511 ;; LC_MESSAGES and LC_TIME, not LC_CTYPE, and the user might not
2512 ;; want to set them to the same value as LC_CTYPE.
2513 (when locale-name
2514 (setq system-messages-locale locale)
2515 (setq system-time-locale locale))
2516
2517 (setq locale (downcase locale))
2518
2519 (let ((language-name
2520 (locale-name-match locale locale-language-names))
2521 (charset-language-name
2522 (locale-name-match locale locale-charset-language-names))
2523 (default-eol-type (coding-system-eol-type
2524 default-buffer-file-coding-system))
2525 (coding-system
2526 (or (locale-name-match locale locale-preferred-coding-systems)
2527 (when locale
2528 (if (string-match "\\.\\([^@]+\\)" locale)
2529 (locale-charset-to-coding-system
2530 (match-string 1 locale))))
2531 (and (eq system-type 'macos) mac-system-coding-system))))
2532
2533 (if (consp language-name)
2534 ;; locale-language-names specify both lang-env and coding.
2535 ;; But, what specified in locale-preferred-coding-systems
2536 ;; has higher priority.
2537 (setq coding-system (or coding-system
2538 (nth 1 language-name))
2539 language-name (car language-name))
2540 ;; Otherwise, if locale is not listed in locale-language-names,
2541 ;; use what listed in locale-charset-language-names.
2542 (if (not language-name)
2543 (setq language-name charset-language-name)))
2544
2545 ;; If a specific EOL conversion was specified in the default
2546 ;; buffer-file-coding-system, preserve it in the coding system
2547 ;; we will be using from now on.
2548 (if (and (memq default-eol-type '(0 1 2 unix dos mac))
2549 coding-system
2550 (coding-system-p coding-system))
2551 (setq coding-system (coding-system-change-eol-conversion
2552 coding-system default-eol-type)))
2553
2554 (when language-name
2555
2556 ;; Set up for this character set. This is now the right way
2557 ;; to do it for both unibyte and multibyte modes.
2558 (set-language-environment language-name)
2559
2560 ;; If default-enable-multibyte-characters is nil,
2561 ;; we are using single-byte characters,
2562 ;; so the display table and terminal coding system are irrelevant.
2563 (when default-enable-multibyte-characters
2564 (set-display-table-and-terminal-coding-system
2565 language-name coding-system))
2566
2567 ;; Set the `keyboard-coding-system' if appropriate (tty
2568 ;; only). At least X and MS Windows can generate
2569 ;; multilingual input.
2570 (unless window-system
2571 (let ((kcs (or coding-system
2572 (car (get-language-info language-name
2573 'coding-system)))))
2574 (if kcs (set-keyboard-coding-system kcs))))
2575
2576 (setq locale-coding-system
2577 (car (get-language-info language-name 'coding-priority))))
2578
2579 (when (and coding-system
2580 (not (coding-system-equal coding-system
2581 locale-coding-system)))
2582 (prefer-coding-system coding-system)
2583 (setq locale-coding-system coding-system))))
2584
2585 ;; On Windows, override locale-coding-system,
2586 ;; keyboard-coding-system with system codepage. Note:
2587 ;; selection-coding-system is already set in w32select.c.
2588 (when (boundp 'w32-ansi-code-page)
2589 (let ((code-page-coding (intern (format "cp%d" w32-ansi-code-page))))
2590 (when (coding-system-p code-page-coding)
2591 (setq locale-coding-system code-page-coding)
2592 (set-keyboard-coding-system code-page-coding)
2593 (set-terminal-coding-system code-page-coding))))
2594
2595 (when (eq system-type 'darwin)
2596 ;; On Darwin, file names are always encoded in utf-8, no matter
2597 ;; the locale.
2598 (setq default-file-name-coding-system 'utf-8)
2599 ;; Mac OS X's Terminal.app by default uses utf-8 regardless of
2600 ;; the locale.
2601 (when (and (null window-system)
2602 (equal (getenv "TERM_PROGRAM") "Apple_Terminal"))
2603 (set-terminal-coding-system 'utf-8)
2604 (set-keyboard-coding-system 'utf-8)))
2605
2606 ;; Default to A4 paper if we're not in a C, POSIX or US locale.
2607 ;; (See comments in Flocale_info.)
2608 (let ((locale locale)
2609 (paper (locale-info 'paper)))
2610 (if paper
2611 ;; This will always be null at the time of writing.
2612 (cond
2613 ((equal paper '(216 279))
2614 (setq ps-paper-type 'letter))
2615 ((equal paper '(210 297))
2616 (setq ps-paper-type 'a4)))
2617 (let ((vars '("LC_ALL" "LC_PAPER" "LANG")))
2618 (while (and vars (= 0 (length locale)))
2619 (setq locale (getenv (pop vars)))))
2620 (when locale
2621 ;; As of glibc 2.2.5, these are the only US Letter locales,
2622 ;; and the rest are A4.
2623 (setq ps-paper-type
2624 (or (locale-name-match locale '(("c$" . letter)
2625 ("posix$" . letter)
2626 (".._us" . letter)
2627 (".._pr" . letter)
2628 (".._ca" . letter)
2629 ("enu$" . letter) ; Windows
2630 ("esu$" . letter)
2631 ("enc$" . letter)
2632 ("frc$" . letter)))
2633 'a4))))))
2634 nil)
2635 \f
2636 ;;; Charset property
2637
2638 (defun get-charset-property (charset propname)
2639 "Return the value of CHARSET's PROPNAME property.
2640 This is the last value stored with
2641 (put-charset-property CHARSET PROPNAME VALUE)."
2642 (and (not (eq charset 'composition))
2643 (plist-get (charset-plist charset) propname)))
2644
2645 (defun put-charset-property (charset propname value)
2646 "Store CHARSETS's PROPNAME property with value VALUE.
2647 It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
2648 (or (eq charset 'composition)
2649 (set-charset-plist charset
2650 (plist-put (charset-plist charset) propname value))))
2651
2652 ;;; Character code property
2653 (put 'char-code-property-table 'char-table-extra-slots 0)
2654
2655 (defvar char-code-property-table
2656 (make-char-table 'char-code-property-table)
2657 "Char-table containing a property list of each character code.
2658
2659 See also the documentation of `get-char-code-property' and
2660 `put-char-code-property'.")
2661
2662 (defun get-char-code-property (char propname)
2663 "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
2664 (let ((plist (aref char-code-property-table char)))
2665 (if (listp plist)
2666 (car (cdr (memq propname plist))))))
2667
2668 (defun put-char-code-property (char propname value)
2669 "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
2670 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
2671 (let ((plist (aref char-code-property-table char)))
2672 (if plist
2673 (let ((slot (memq propname plist)))
2674 (if slot
2675 (setcar (cdr slot) value)
2676 (nconc plist (list propname value))))
2677 (aset char-code-property-table char (list propname value)))))
2678
2679 \f
2680 ;; Pretty description of encoded string
2681
2682 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
2683 (defvar iso-2022-control-alist
2684 '((?\x1b . "ESC")
2685 (?\x0e . "SO")
2686 (?\x0f . "SI")
2687 (?\x8e . "SS2")
2688 (?\x8f . "SS3")
2689 (?\x9b . "CSI")))
2690
2691 (defun encoded-string-description (str coding-system)
2692 "Return a pretty description of STR that is encoded by CODING-SYSTEM."
2693 (setq str (string-as-unibyte str))
2694 (mapconcat
2695 (if (and coding-system (eq (coding-system-type coding-system) 2))
2696 ;; Try to get a pretty description for ISO 2022 escape sequences.
2697 (function (lambda (x) (or (cdr (assq x iso-2022-control-alist))
2698 (format "#x%02X" x))))
2699 (function (lambda (x) (format "#x%02X" x))))
2700 str " "))
2701
2702 (defun encode-coding-char (char coding-system)
2703 "Encode CHAR by CODING-SYSTEM and return the resulting string.
2704 If CODING-SYSTEM can't safely encode CHAR, return nil."
2705 (let ((str1 (string-as-multibyte (char-to-string char)))
2706 (str2 (string-as-multibyte (make-string 2 char)))
2707 (safe-chars (and coding-system
2708 (coding-system-get coding-system 'safe-chars)))
2709 (charset (char-charset char))
2710 enc1 enc2 i1 i2)
2711 (when (or (eq safe-chars t)
2712 (eq charset 'ascii)
2713 (and safe-chars (aref safe-chars char)))
2714 ;; We must find the encoded string of CHAR. But, just encoding
2715 ;; CHAR will put extra control sequences (usually to designate
2716 ;; ASCII charset) at the tail if type of CODING is ISO 2022.
2717 ;; To exclude such tailing bytes, we at first encode one-char
2718 ;; string and two-char string, then check how many bytes at the
2719 ;; tail of both encoded strings are the same.
2720
2721 (setq enc1 (encode-coding-string str1 coding-system)
2722 i1 (length enc1)
2723 enc2 (encode-coding-string str2 coding-system)
2724 i2 (length enc2))
2725 (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
2726 (setq i1 (1- i1) i2 (1- i2)))
2727
2728 ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
2729 ;; and they are the extra control sequences at the tail to
2730 ;; exclude.
2731 (substring enc2 0 i2))))
2732
2733
2734 ;; arch-tag: b382c432-4b36-460e-bf4c-05efd0bb18dc
2735 ;;; mule-cmds.el ends here