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