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