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