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