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