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