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