]> code.delx.au - gnu-emacs/blob - lisp/international/mule-cmds.el
Revision: miles@gnu.org--gnu-2005/emacs--unicode--0--patch-13
[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 "22.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 Afar
2016 ; ab Abkhazian
2017 ("af" . "Latin-1") ; Afrikaans
2018 ("am" . "Ethiopic") ; Amharic
2019 ; ar Arabic glibc uses 8859-6
2020 ; as Assamese
2021 ; ay Aymara
2022 ; az Azerbaijani
2023 ; ba Bashkir
2024 ("be" . "Belarusian") ; Belarusian [Byelorussian until early 1990s]
2025 ("bg" . "Bulgarian") ; Bulgarian
2026 ; bh Bihari
2027 ; bi Bislama
2028 ; bn Bengali, Bangla
2029 ("bo" . "Tibetan")
2030 ("br" . "Latin-1") ; Breton
2031 ("bs" . "Latin-2") ; Bosnian
2032 ("ca" . "Latin-1") ; Catalan
2033 ; co Corsican
2034 ("cs" . "Czech")
2035 ("cy" . "Welsh") ; Welsh [glibc uses Latin-8. Did this change?]
2036 ("da" . "Latin-1") ; Danish
2037 ("de" . "German")
2038 ; dz Bhutani
2039 ("el" . "Greek")
2040 ;; Users who specify "en" explicitly typically want Latin-1, not ASCII.
2041 ;; That's actually what the GNU locales define, modulo things like
2042 ;; en_IN -- fx.
2043 ("en" . "Latin-1") ; English
2044 ("eo" . "Latin-3") ; Esperanto
2045 ("es" . "Spanish")
2046 ("et" . "Latin-4") ; Estonian
2047 ("eu" . "Latin-1") ; Basque
2048 ; fa Persian glibc uses utf-8
2049 ("fi" . "Latin-1") ; Finnish
2050 ; fj Fiji
2051 ("fo" . "Latin-1") ; Faroese
2052 ("fr" . "French") ; French
2053 ("fy" . "Latin-1") ; Frisian
2054 ("ga" . "Latin-1") ; Irish Gaelic (new orthography)
2055 ("gd" . "Latin-1") ; Scots Gaelic
2056 ("gl" . "Latin-1") ; Galician
2057 ; gn Guarani
2058 ; gu Gujarati
2059 ("gv" . "Latin-8") ; Manx Gaelic glibc uses 8859-1
2060 ; ha Hausa
2061 ("he" . "Hebrew")
2062 ("hi" . "Devanagari") ; Hindi glibc uses utf-8
2063 ("hr" . "Croatian") ; Croatian
2064 ("hu" . "Latin-2") ; Hungarian
2065 ; hy Armenian
2066 ; ia Interlingua
2067 ("id" . "Latin-1") ; Indonesian
2068 ; ie Interlingue
2069 ; ik Inupiak
2070 ("is" . "Latin-1") ; Icelandic
2071 ("it" . "Italian") ; Italian
2072 ; iu Inuktitut
2073 ("ja" . "Japanese")
2074 ; jw Javanese
2075 ("ka" . "Georgian") ; Georgian
2076 ; kk Kazakh
2077 ("kl" . "Latin-1") ; Greenlandic
2078 ; km Cambodian
2079 ; kn Kannada
2080 ("ko" . "Korean")
2081 ; ks Kashmiri
2082 ; ku Kurdish
2083 ("kw" . "Latin-1") ; Cornish
2084 ; ky Kirghiz
2085 ("la" . "Latin-1") ; Latin
2086 ("lb" . "Latin-1") ; Luxemburgish
2087 ; ln Lingala
2088 ("lo" . "Lao") ; Laothian
2089 ("lt" . "Lithuanian")
2090 ("lv" . "Latvian") ; Latvian, Lettish
2091 ; mg Malagasy
2092 ("mi" . "Latin-7") ; Maori
2093 ("mk" . "Cyrillic-ISO") ; Macedonian
2094 ; ml Malayalam
2095 ; mn Mongolian
2096 ; mo Moldavian
2097 ("mr" . "Devanagari") ; Marathi glibc uses utf-8
2098 ("ms" . "Latin-1") ; Malay
2099 ("mt" . "Latin-3") ; Maltese
2100 ; my Burmese
2101 ; na Nauru
2102 ("ne" . "Devanagari") ; Nepali
2103 ("nl" . "Dutch")
2104 ("no" . "Latin-1") ; Norwegian
2105 ("oc" . "Latin-1") ; Occitan
2106 ; om (Afan) Oromo
2107 ; or Oriya
2108 ; pa Punjabi
2109 ("pl" . "Latin-2") ; Polish
2110 ; ps Pashto, Pushto
2111 ("pt" . "Latin-1") ; Portuguese
2112 ; qu Quechua
2113 ("rm" . "Latin-1") ; Rhaeto-Romanic
2114 ; rn Kirundi
2115 ("ro" . "Romanian")
2116 ("ru.*[_.]koi8\\(?:-r\\)?\\'" . "Cyrillic-KOI8") ; Russian
2117 ("ru" . "Cyrillic-ISO") ; Russian
2118 ; rw Kinyarwanda
2119 ("sa" . "Devanagari") ; Sanskrit
2120 ; sd Sindhi
2121 ; se Northern Sami
2122 ; sg Sangho
2123 ("sh" . "Latin-2") ; Serbo-Croatian
2124 ; si Sinhalese
2125 ("sk" . "Slovak")
2126 ("sl" . "Slovenian")
2127 ; sm Samoan
2128 ; sn Shona
2129 ; so Somali
2130 ("sq" . "Latin-1") ; Albanian
2131 ("sr" . "Latin-2") ; Serbian (Latin alphabet)
2132 ("sr.*@cyrillic" . "Cyrillic-ISO") ; per glibc
2133 ; ss Siswati
2134 ; st Sesotho
2135 ; su Sundanese
2136 ("sv" . "Swedish") ; Swedish
2137 ("sw" . "Latin-1") ; Swahili
2138 ; ta Tamil glibc uses utf-8
2139 ; te Telugu glibc uses utf-8
2140 ("tg" . "Tajik")
2141 ("th" . "Thai")
2142 ; ti Tigrinya
2143 ; tk Turkmen
2144 ("tl" . "Latin-1") ; Tagalog
2145 ; tn Setswana
2146 ; to Tonga
2147 ("tr" . "Turkish")
2148 ; ts Tsonga
2149 ; tt Tatar
2150 ; tw Twi
2151 ; ug Uighur
2152 ("uk" . "Ukrainian") ; Ukrainian
2153 ; ur Urdu glibc uses utf-8
2154 ("uz" . "Latin-1") ; Uzbek
2155 ("vi" . "Vietnamese") ; glibc uses utf-8
2156 ; vo Volapuk
2157 ("wa" . "Latin-1") ; Walloon
2158 ; wo Wolof
2159 ; xh Xhosa
2160 ("yi" . "Windows-1255") ; Yiddish
2161 ; yo Yoruba
2162 ; za Zhuang
2163
2164 ; glibc:
2165 ; zh_HK/BIG5-HKSCS \
2166
2167 ("zh.*[._]big5" . "Chinese-BIG5")
2168 ("zh.*[._]gb18030" . "Chinese-GB18030") ; zh_CN.GB18030/GB18030 in glibc
2169 ("zh.*[._]gbk" . "Chinese-GBK")
2170 ;; glibc has zh_TW.EUC-TW, with zh_TW defaulting to Big5
2171 ("zh_tw" . "Chinese-CNS") ; glibc uses big5
2172 ("zh_tw[._]euc-tw" . "Chinese-EUC-TW")
2173 ("zh" . "Chinese-GB")
2174 ; zu Zulu
2175
2176 ;; ISO standard locales
2177 ("c$" . "ASCII")
2178 ("posix$" . "ASCII")
2179
2180 ;; The "IPA" Emacs language environment does not correspond
2181 ;; to any ISO 639 code, so let it stand for itself.
2182 ("ipa$" . "IPA")
2183
2184 ;; Nonstandard or obsolete language codes
2185 ("cz" . "Czech") ; e.g. Solaris 2.6
2186 ("ee" . "Latin-4") ; Estonian, e.g. X11R6.4
2187 ("iw" . "Hebrew") ; e.g. X11R6.4
2188 ("sp" . "Cyrillic-ISO") ; Serbian (Cyrillic alphabet), e.g. X11R6.4
2189 ("su" . "Latin-1") ; Finnish, e.g. Solaris 2.6
2190 ("jp" . "Japanese") ; e.g. MS Windows
2191 ("chs" . "Chinese-GB") ; MS Windows Chinese Simplified
2192 ("cht" . "Chinese-BIG5") ; MS Windows Chinese Traditional
2193 ))
2194 "List of pairs of locale regexps and language names.
2195 The first element whose locale regexp matches the start of a downcased locale
2196 specifies the language name corresponding to that locale.
2197 If the language name is nil, there is no corresponding language environment.")
2198
2199 (defconst locale-charset-language-names
2200 (purecopy
2201 '((".*8859[-_]?1\\>" . "Latin-1")
2202 (".*8859[-_]?2\\>" . "Latin-2")
2203 (".*8859[-_]?3\\>" . "Latin-3")
2204 (".*8859[-_]?4\\>" . "Latin-4")
2205 (".*8859[-_]?9\\>" . "Latin-5")
2206 (".*8859[-_]?14\\>" . "Latin-8")
2207 (".*8859[-_]?15\\>" . "Latin-9")
2208 (".*utf\\(?:-?8\\)?\\>" . "UTF-8")
2209 ;; utf-8@euro exists, so put this last. (@euro really specifies
2210 ;; the currency, rather than the charset.)
2211 (".*@euro\\>" . "Latin-9")))
2212 "List of pairs of locale regexps and charset language names.
2213 The first element whose locale regexp matches the start of a downcased locale
2214 specifies the language name whose charset corresponds to that locale.
2215 This language name is used if its charsets disagree with the charsets of
2216 the language name that would otherwise be used for this locale.")
2217
2218 (defconst locale-preferred-coding-systems
2219 (purecopy
2220 '(("ja.*[._]euc" . japanese-iso-8bit)
2221 ("ja.*[._]jis7" . iso-2022-jp)
2222 ("ja.*[._]pck" . japanese-shift-jis)
2223 ("ja.*[._]sjis" . japanese-shift-jis)
2224 ("jpn" . japanese-shift-jis) ; MS-Windows uses this.
2225 (".*[._]utf" . utf-8)))
2226 "List of pairs of locale regexps and preferred coding systems.
2227 The first element whose locale regexp matches the start of a downcased locale
2228 specifies the coding system to prefer when using that locale.")
2229
2230 (defun locale-name-match (key alist)
2231 "Search for KEY in ALIST, which should be a list of regexp-value pairs.
2232 Return the value corresponding to the first regexp that matches the
2233 start of KEY, or nil if there is no match."
2234 (let (element)
2235 (while (and alist (not element))
2236 (if (string-match (concat "\\`\\(?:" (car (car alist)) "\\)") key)
2237 (setq element (car alist)))
2238 (setq alist (cdr alist)))
2239 (cdr element)))
2240
2241 (defun locale-charset-match-p (charset1 charset2)
2242 "Whether charset names (strings) CHARSET1 and CHARSET2 are equivalent.
2243 Matching is done ignoring case and any hyphens and underscores in the
2244 names. E.g. `ISO_8859-1' and `iso88591' both match `iso-8859-1'."
2245 (setq charset1 (replace-regexp-in-string "[-_]" "" charset1))
2246 (setq charset2 (replace-regexp-in-string "[-_]" "" charset2))
2247 (eq t (compare-strings charset1 nil nil charset2 nil nil t)))
2248
2249 (defvar locale-charset-alist nil
2250 "Coding system alist keyed on locale-style charset name.
2251 Used by `locale-charset-to-coding-system'.")
2252
2253 (defun locale-charset-to-coding-system (charset)
2254 "Find coding system corresponding to CHARSET.
2255 CHARSET is any sort of non-Emacs charset name, such as might be used
2256 in a locale codeset, or elsewhere. It is matched to a coding system
2257 first by case-insensitive lookup in `locale-charset-alist'. Then
2258 matches are looked for in the coding system list, treating case and
2259 the characters `-' and `_' as insignificant. The coding system base
2260 is returned. Thus, for instance, if charset \"ISO8859-2\",
2261 `iso-latin-2' is returned."
2262 (or (car (assoc-string charset locale-charset-alist t))
2263 (let ((cs coding-system-alist)
2264 c)
2265 (while (and (not c) cs)
2266 (if (locale-charset-match-p charset (caar cs))
2267 (setq c (intern (caar cs)))
2268 (pop cs)))
2269 (if c (coding-system-base c)))))
2270
2271 ;; Fixme: This ought to deal with the territory part of the locale
2272 ;; too, for setting things such as calendar holidays, ps-print paper
2273 ;; size, spelling dictionary.
2274
2275 (defun set-locale-environment (&optional locale-name)
2276 "Set up multi-lingual environment for using LOCALE-NAME.
2277 This sets the language environment, the coding system priority,
2278 the default input method and sometimes other things.
2279
2280 LOCALE-NAME should be a string which is the name of a locale supported
2281 by the system. Often it is of the form xx_XX.CODE, where xx is a
2282 language, XX is a country, and CODE specifies a character set and
2283 coding system. For example, the locale name \"ja_JP.EUC\" might name
2284 a locale for Japanese in Japan using the `japanese-iso-8bit'
2285 coding-system. The name may also have a modifier suffix, e.g. `@euro'
2286 or `@cyrillic'.
2287
2288 If LOCALE-NAME is nil, its value is taken from the environment
2289 variables LC_ALL, LC_CTYPE and LANG (the first one that is set).
2290
2291 The locale names supported by your system can typically be found in a
2292 directory named `/usr/share/locale' or `/usr/lib/locale'. LOCALE-NAME
2293 will be translated according to the table specified by
2294 `locale-translation-file-name'.
2295
2296 See also `locale-charset-language-names', `locale-language-names',
2297 `locale-preferred-coding-systems' and `locale-coding-system'."
2298 (interactive "sSet environment for locale: ")
2299
2300 ;; Do this at runtime for the sake of binaries possibly transported
2301 ;; to a system without X.
2302 (setq locale-translation-file-name
2303 (let ((files
2304 '("/usr/lib/X11/locale/locale.alias" ; e.g. X11R6.4
2305 "/usr/X11R6/lib/X11/locale/locale.alias" ; XFree86, e.g. RedHat 4.2
2306 "/usr/openwin/lib/locale/locale.alias" ; e.g. Solaris 2.6
2307 ;;
2308 ;; The following name appears after the X-related names above,
2309 ;; since the X-related names are what X actually uses.
2310 "/usr/share/locale/locale.alias" ; GNU/Linux sans X
2311 )))
2312 (while (and files (not (file-exists-p (car files))))
2313 (setq files (cdr files)))
2314 (car files)))
2315
2316 (let ((locale locale-name))
2317
2318 (unless locale
2319 ;; Use the first of these three environment variables
2320 ;; that has a nonempty value.
2321 (let ((vars '("LC_ALL" "LC_CTYPE" "LANG")))
2322 (while (and vars
2323 (= 0 (length locale))) ; nil or empty string
2324 (setq locale (getenv (pop vars))))))
2325
2326 (when locale
2327
2328 ;; Translate "swedish" into "sv_SE.ISO8859-1", and so on,
2329 ;; using the translation file that many systems have.
2330 (when locale-translation-file-name
2331 (with-temp-buffer
2332 (set-buffer-multibyte nil)
2333 (insert-file-contents locale-translation-file-name)
2334 (when (re-search-forward
2335 (concat "^" (regexp-quote locale) ":?[ \t]+") nil t)
2336 (setq locale (buffer-substring (point) (line-end-position))))))
2337
2338 ;; Leave the system locales alone if the caller did not specify
2339 ;; an explicit locale name, as their defaults are set from
2340 ;; LC_MESSAGES and LC_TIME, not LC_CTYPE, and the user might not
2341 ;; want to set them to the same value as LC_CTYPE.
2342 (when locale-name
2343 (setq system-messages-locale locale)
2344 (setq system-time-locale locale))
2345
2346 (setq locale (downcase locale))
2347
2348 (let ((language-name
2349 (locale-name-match locale locale-language-names))
2350 (charset-language-name
2351 (locale-name-match locale locale-charset-language-names))
2352 (coding-system
2353 (or (locale-name-match locale locale-preferred-coding-systems)
2354 (when locale
2355 (if (string-match "\\.\\([^@]+\\)" locale)
2356 (locale-charset-to-coding-system
2357 (match-string 1 locale)))))))
2358
2359 ;; Give preference to charset-language-name over language-name.
2360 (if (and charset-language-name
2361 (not
2362 (equal (get-language-info language-name 'charset)
2363 (get-language-info charset-language-name 'charset))))
2364 (setq language-name charset-language-name))
2365
2366 (when language-name
2367
2368 ;; Set up for this character set. This is now the right way
2369 ;; to do it for both unibyte and multibyte modes.
2370 (set-language-environment language-name)
2371
2372 ;; If default-enable-multibyte-characters is nil,
2373 ;; we are using single-byte characters,
2374 ;; so the display table and terminal coding system are irrelevant.
2375 (when default-enable-multibyte-characters
2376 (set-display-table-and-terminal-coding-system language-name))
2377
2378 ;; Set the `keyboard-coding-system' if appropriate (tty
2379 ;; only). At least X and MS Windows can generate
2380 ;; multilingual input.
2381 (unless window-system
2382 (let ((kcs (or coding-system
2383 (car (get-language-info language-name
2384 'coding-system)))))
2385 (if kcs (set-keyboard-coding-system kcs))))
2386
2387 (setq locale-coding-system
2388 (car (get-language-info language-name 'coding-priority))))
2389
2390 (when coding-system
2391 (prefer-coding-system coding-system)
2392 (setq locale-coding-system coding-system))
2393 (when (get-language-info current-language-environment 'coding-priority)
2394 (let ((codeset (locale-info 'codeset))
2395 (coding-system (car (coding-system-priority-list))))
2396 (when codeset
2397 (let ((cs (coding-system-aliases coding-system))
2398 result)
2399 (while (and cs (not result))
2400 (setq result
2401 (locale-charset-match-p (symbol-name (pop cs))
2402 (locale-info 'codeset))))
2403 (unless result
2404 (message "Warning: Default coding system `%s' disagrees with
2405 system codeset `%s' for this locale." coding-system codeset))))))))
2406
2407 ;; On Windows, override locale-coding-system, keyboard-coding-system,
2408 ;; selection-coding-system with system codepage.
2409 (when (boundp 'w32-ansi-code-page)
2410 (let ((code-page-coding (intern (format "cp%d" w32-ansi-code-page))))
2411 (when (coding-system-p code-page-coding)
2412 (setq locale-coding-system code-page-coding)
2413 (set-selection-coding-system code-page-coding)
2414 (set-keyboard-coding-system code-page-coding)
2415 (set-terminal-coding-system code-page-coding))))
2416
2417 ;; On Darwin, file names are always encoded in utf-8, no matter the locale.
2418 (when (eq system-type 'darwin)
2419 (setq default-file-name-coding-system 'utf-8))
2420
2421 ;; Default to A4 paper if we're not in a C, POSIX or US locale.
2422 ;; (See comments in Flocale_info.)
2423 (let ((locale locale)
2424 (paper (locale-info 'paper)))
2425 (if paper
2426 ;; This will always be null at the time of writing.
2427 (cond
2428 ((equal paper '(216 279))
2429 (setq ps-paper-type 'letter))
2430 ((equal paper '(210 297))
2431 (setq ps-paper-type 'a4)))
2432 (let ((vars '("LC_ALL" "LC_PAPER" "LANG")))
2433 (while (and vars (= 0 (length locale)))
2434 (setq locale (getenv (pop vars)))))
2435 (when locale
2436 ;; As of glibc 2.2.5, these are the only US Letter locales,
2437 ;; and the rest are A4.
2438 (setq ps-paper-type
2439 (or (locale-name-match locale '(("c$" . letter)
2440 ("posix$" . letter)
2441 (".._us" . letter)
2442 (".._pr" . letter)
2443 (".._ca" . letter)
2444 ("enu$" . letter) ; Windows
2445 ("esu$" . letter)
2446 ("enc$" . letter)
2447 ("frc$" . letter)))
2448 'a4))))))
2449 nil)
2450 \f
2451 ;;; Character property
2452
2453 ;; Each element has the form (PROP . TABLE).
2454 ;; PROP is a symbol representing a character property.
2455 ;; TABLE is a char-table containing the property value for each character.
2456 ;; TABLE may be a name of file to load to build a char-table.
2457 ;; Don't modify this variable directly but use `define-char-code-property'.
2458
2459 (defvar char-code-property-alist nil
2460 "Alist of character property name vs char-table containing property values.
2461 Internal use only.")
2462
2463 (put 'char-code-property-table 'char-table-extra-slots 5)
2464
2465 (defun define-char-code-property (name table &optional docstring)
2466 "Define NAME as a character code property given by TABLE.
2467 TABLE is a char-table of purpose `char-code-property-table' with
2468 these extra slots:
2469 1st: NAME.
2470 2nd: Function to call to get a property value of a character.
2471 It is called with three arugments CHAR, VAL, and TABLE, where
2472 CHAR is a character, VAL is the value of (aref TABLE CHAR).
2473 3rd: Function to call to put a property value of a character.
2474 It is called with the same arguments as above.
2475 4th: Function to call to get a description string of a property value.
2476 It is called with one argument VALUE, a property value.
2477 5th: Data used by the above functions.
2478
2479 TABLE may be a name of file to load to build a char-table. The
2480 file should contain a call of `define-char-code-property' with a
2481 char-table of the above format as the argument TABLE.
2482
2483 TABLE may also be nil, in which case no property value is pre-assigned.
2484
2485 Optional 3rd argment DOCSTRING is a documentation string of the property.
2486
2487 See also the documentation of `get-char-code-property' and
2488 `put-char-code-property'."
2489 (or (symbolp name)
2490 (error "Not a symbol: %s" name))
2491 (if (char-table-p table)
2492 (or (and (eq (char-table-subtype table) 'char-code-property-table)
2493 (eq (char-table-extra-slot table 0) name))
2494 (error "Invalid char-table: %s" table))
2495 (or (stringp table)
2496 (error "Not a char-table nor a file name: %s" table)))
2497 (let ((slot (assq name char-code-property-alist)))
2498 (if slot
2499 (setcdr slot table)
2500 (setq char-code-property-alist
2501 (cons (cons name table) char-code-property-alist))))
2502 (put name 'char-code-property-documentation docstring))
2503
2504 (defvar char-code-property-table
2505 (make-char-table 'char-code-property-table)
2506 "Char-table containing a property list of each character code.
2507 This table is used for properties not listed in `char-code-property-alist'.
2508 See also the documentation of `get-char-code-property' and
2509 `put-char-code-property'.")
2510
2511 (defun get-char-code-property (char propname)
2512 "Return the value of CHAR's PROPNAME property."
2513 (let ((slot (assq propname char-code-property-alist)))
2514 (if slot
2515 (let (table value func)
2516 (if (stringp (cdr slot))
2517 (load (cdr slot)))
2518 (setq table (cdr slot)
2519 value (aref table char)
2520 func (char-table-extra-slot table 1))
2521 (if (functionp func)
2522 (setq value (funcall func char value table)))
2523 value)
2524 (plist-get (aref char-code-property-table char) propname))))
2525
2526 (defun put-char-code-property (char propname value)
2527 "Store CHAR's PROPNAME property with VALUE.
2528 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
2529 (let ((slot (assq propname char-code-property-alist)))
2530 (if slot
2531 (let (table func)
2532 (if (stringp (cdr slot))
2533 (load (cdr slot)))
2534 (setq table (cdr slot)
2535 func (char-table-extra-slot table 2))
2536 (if (functionp func)
2537 (funcall func char value table)
2538 (aset table char value)))
2539 (let* ((plist (aref char-code-property-table char))
2540 (x (plist-put plist propname value)))
2541 (or (eq x plist)
2542 (aset char-code-property-table char x))))
2543 value))
2544
2545 (defun char-code-property-description (prop value)
2546 "Return a description string of character property PROP's value VALUE.
2547 If there's no description string for VALUE, return nil."
2548 (let ((slot (assq prop char-code-property-alist)))
2549 (if slot
2550 (let (table func)
2551 (if (stringp (cdr slot))
2552 (load (cdr slot)))
2553 (setq table (cdr slot)
2554 func (char-table-extra-slot table 3))
2555 (if (functionp func)
2556 (funcall func value))))))
2557
2558 \f
2559 ;; Pretty description of encoded string
2560
2561 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
2562 (defvar iso-2022-control-alist
2563 '((?\x1b . "ESC")
2564 (?\x0e . "SO")
2565 (?\x0f . "SI")
2566 (?\x8e . "SS2")
2567 (?\x8f . "SS3")
2568 (?\x9b . "CSI")))
2569
2570 (defun encoded-string-description (str coding-system)
2571 "Return a pretty description of STR that is encoded by CODING-SYSTEM."
2572 (setq str (string-as-unibyte str))
2573 (mapconcat
2574 (if (and coding-system (eq (coding-system-type coding-system) 'iso-2022))
2575 ;; Try to get a pretty description for ISO 2022 escape sequences.
2576 (function (lambda (x) (or (cdr (assq x iso-2022-control-alist))
2577 (format "0x%02X" x))))
2578 (function (lambda (x) (format "0x%02X" x))))
2579 str " "))
2580
2581 (defun encode-coding-char (char coding-system)
2582 "Encode CHAR by CODING-SYSTEM and return the resulting string.
2583 If CODING-SYSTEM can't safely encode CHAR, return nil."
2584 (let ((str1 (string-as-multibyte (string char)))
2585 (str2 (string-as-multibyte (string char char)))
2586 enc1 enc2 i1 i2)
2587 (when (memq (coding-system-base coding-system)
2588 (find-coding-systems-string str1))
2589 ;; We must find the encoded string of CHAR. But, just encoding
2590 ;; CHAR will put extra control sequences (usually to designate
2591 ;; ASCII charset) at the tail if type of CODING is ISO 2022.
2592 ;; To exclude such tailing bytes, we at first encode one-char
2593 ;; string and two-char string, then check how many bytes at the
2594 ;; tail of both encoded strings are the same.
2595
2596 (setq enc1 (encode-coding-string str1 coding-system)
2597 i1 (length enc1)
2598 enc2 (encode-coding-string str2 coding-system)
2599 i2 (length enc2))
2600 (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
2601 (setq i1 (1- i1) i2 (1- i2)))
2602
2603 ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
2604 ;; and they are the extra control sequences at the tail to
2605 ;; exclude.
2606 (substring enc2 0 i2))))
2607
2608 ;; Backwards compatibility. These might be better with :init-value t,
2609 ;; but that breaks loadup.
2610 (define-minor-mode unify-8859-on-encoding-mode
2611 "Obsolete."
2612 :group 'mule
2613 :global t)
2614 (define-minor-mode unify-8859-on-decoding-mode
2615 "Obsolete."
2616 :group 'mule
2617 :global t)
2618
2619 (defvar nonascii-insert-offset 0 "This variable is obsolete.")
2620 (defvar nonascii-translation-table nil "This variable is obsolete.")
2621
2622
2623 ;; arch-tag: b382c432-4b36-460e-bf4c-05efd0bb18dc
2624 ;;; mule-cmds.el ends here