(Info-node-at-bob-matching): New function.
[bpt/emacs.git] / lisp / international / mule-cmds.el
CommitLineData
4ed46869
KH
1;;; mule-cmds.el --- Commands for mulitilingual environment
2
4ed46869 3;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
fa526c4a 4;; Licensed to the Free Software Foundation.
4ed46869
KH
5
6;; Keywords: mule, multilingual
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
369314dc
KH
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
4ed46869
KH
24
25;;; Code:
26
27;;; MULE related key bindings and menus.
28
0709d285 29(defvar mule-keymap (make-sparse-keymap)
4ed46869 30 "Keymap for MULE (Multilingual environment) specific commands.")
4ed46869 31
8f81f784 32;; Keep "C-x C-m ..." for mule specific commands.
0709d285 33(define-key ctl-x-map "\C-m" mule-keymap)
ef8a8c8c 34
4ed46869
KH
35(define-key mule-keymap "f" 'set-buffer-file-coding-system)
36(define-key mule-keymap "t" 'set-terminal-coding-system)
15b3e511
KH
37(define-key mule-keymap "k" 'set-keyboard-coding-system)
38(define-key mule-keymap "p" 'set-buffer-process-coding-system)
7624ebb9
KH
39(define-key mule-keymap "x" 'set-selection-coding-system)
40(define-key mule-keymap "X" 'set-next-selection-coding-system)
8b784951 41(define-key mule-keymap "\C-\\" 'set-input-method)
15b3e511 42(define-key mule-keymap "c" 'universal-coding-system-argument)
b4fba33f 43(define-key mule-keymap "l" 'set-language-environment)
4ed46869 44
281d03ec 45(define-key help-map "\C-L" 'describe-language-environment)
ac4a3a2d 46(define-key help-map "L" 'describe-language-environment)
4ed46869 47(define-key help-map "\C-\\" 'describe-input-method)
ac4a3a2d 48(define-key help-map "I" 'describe-input-method)
d0b9c3ab 49(define-key help-map "C" 'describe-coding-system)
4ed46869
KH
50(define-key help-map "h" 'view-hello-file)
51
0709d285 52(defvar mule-menu-keymap (make-sparse-keymap "Mule")
15b3e511 53 "Keymap for MULE (Multilingual environment) menu specific commands.")
15b3e511 54
ef6675c8
RS
55(define-key global-map [menu-bar mule]
56 `(menu-item "Mule" ,mule-menu-keymap
57 :visible default-enable-multibyte-characters))
15b3e511
KH
58
59(setq menu-bar-final-items (cons 'mule menu-bar-final-items))
60
281d03ec
RS
61(defvar describe-language-environment-map nil)
62(define-prefix-command 'describe-language-environment-map)
15b3e511
KH
63
64(defvar setup-language-environment-map nil)
65(define-prefix-command 'setup-language-environment-map)
66
67(defvar set-coding-system-map nil)
68(define-prefix-command 'set-coding-system-map)
69
281d03ec 70(define-key-after mule-menu-keymap [describe-language-environment]
8e02924a 71 '("Describe Language Environment" . describe-language-environment-map)
15b3e511
KH
72 t)
73(define-key-after mule-menu-keymap [set-language-environment]
8e02924a 74 '("Set Language Environment" . setup-language-environment-map)
15b3e511 75 t)
a61f401d 76(define-key-after mule-menu-keymap [mouse-set-font]
8e02924a 77 '("Set Font/Fontset" . mouse-set-font)
a61f401d 78 t)
15b3e511
KH
79(define-key-after mule-menu-keymap [separator-mule]
80 '("--")
81 t)
82(define-key-after mule-menu-keymap [toggle-input-method]
8e02924a 83 '("Toggle Input Method" . toggle-input-method)
15b3e511 84 t)
8b784951
KH
85(define-key-after mule-menu-keymap [set-input-method]
86 '("Select Input Method" . set-input-method)
15b3e511
KH
87 t)
88(define-key-after mule-menu-keymap [describe-input-method]
8e02924a 89 '("Describe Input Method" . describe-input-method)
15b3e511
KH
90 t)
91(define-key-after mule-menu-keymap [separator-input-method]
92 '("--")
93 t)
d0b9c3ab 94(define-key-after mule-menu-keymap [describe-coding-system]
8e02924a 95 '("Describe Coding Systems" . describe-coding-system)
15b3e511
KH
96 t)
97(define-key-after mule-menu-keymap [set-various-coding-system]
8e02924a 98 '("Set Coding System" . set-coding-system-map)
15b3e511
KH
99 t)
100(define-key-after mule-menu-keymap [separator-coding-system]
101 '("--")
102 t)
103(define-key-after mule-menu-keymap [mule-diag]
8e02924a 104 '("Show All of MULE Status" . mule-diag)
15b3e511
KH
105 t)
106(define-key-after mule-menu-keymap [view-hello-file]
8e02924a 107 '("Show Script Examples" . view-hello-file)
15b3e511
KH
108 t)
109
110(define-key-after set-coding-system-map [set-buffer-file-coding-system]
8e02924a 111 '("Buffer File" . set-buffer-file-coding-system)
15b3e511 112 t)
3a151e98
RS
113(define-key-after set-coding-system-map [universal-coding-system-argument]
114 '("Next Command" . universal-coding-system-argument)
115 t)
15b3e511
KH
116(define-key-after set-coding-system-map [set-terminal-coding-system]
117 '("Terminal" . set-terminal-coding-system)
118 t)
119(define-key-after set-coding-system-map [set-keyboard-coding-system]
120 '("Keyboard" . set-keyboard-coding-system)
121 t)
122(define-key-after set-coding-system-map [set-buffer-process-coding-system]
8e02924a 123 '("Buffer Process" . set-buffer-process-coding-system)
15b3e511 124 t)
7624ebb9
KH
125(define-key-after set-coding-system-map [set-selection-coding-system]
126 '("X Selection" . set-selection-coding-system)
127 t)
128(define-key-after set-coding-system-map [set-next-selection-coding-system]
129 '("Next X Selection" . set-next-selection-coding-system)
130 t)
15b3e511
KH
131(define-key setup-language-environment-map
132 [Default] '("Default" . setup-specified-language-environment))
4ed46869 133
03c35c83 134;; These are meaningless when running under X and W32.
4ed46869 135(put 'set-terminal-coding-system 'menu-enable
03c35c83 136 '(or (not window-system) (eq window-system 'pc)))
15b3e511 137(put 'set-keyboard-coding-system 'menu-enable
03c35c83 138 '(or (not window-system) (eq window-system 'pc)))
d0b9c3ab
KH
139;; This is meaningless when the current buffer has no process.
140(put 'set-buffer-process-coding-system 'menu-enable
141 '(get-buffer-process (current-buffer)))
7624ebb9
KH
142;; These are meaningless when running under terminal.
143(put 'set-selection-coding-system 'menu-enable
144 'window-system)
145(put 'set-next-selection-coding-system 'menu-enable
146 'window-system)
4ed46869 147
4ed46869
KH
148;; This should be a single character key binding because users use it
149;; very frequently while editing multilingual text. Now we can use
150;; only two such keys: "\C-\\" and "\C-^", but the latter is not
151;; convenient because it requires shifting on most keyboards. An
152;; alternative is "\C-\]" which is now bound to `abort-recursive-edit'
153;; but it won't be used that frequently.
154(define-key global-map "\C-\\" 'toggle-input-method)
155
a2ad45b9
RS
156;;; This is no good because people often type Shift-SPC
157;;; meaning to type SPC. -- rms.
158;;; ;; Here's an alternative key binding for X users (Shift-SPACE).
159;;; (define-key global-map [?\S- ] 'toggle-input-method)
b4fba33f 160
4ed46869 161(defun toggle-enable-multibyte-characters (&optional arg)
6998e1a1
RS
162 "Change whether this buffer uses multibyte characters.
163With arg, use multibyte characters if the arg is positive.
164
165Note that this command does not convert the byte contents of
166the buffer; it only changes the way those bytes are interpreted.
167In general, therefore, this command *changes* the sequence of
168characters that the current buffer contains.
169
170We suggest you avoid using use this command unless you know what you
171are doing. If you use it by mistake, and the buffer is now displayed
172wrong, use this command again to toggle back to the right mode."
4ed46869 173 (interactive "P")
b7079457
RS
174 (let ((new-flag
175 (if (null arg) (null enable-multibyte-characters)
176 (> (prefix-numeric-value arg) 0))))
177 (set-buffer-multibyte new-flag))
4ed46869
KH
178 (force-mode-line-update))
179
180(defun view-hello-file ()
181 "Display the HELLO file which list up many languages and characters."
182 (interactive)
8f81f784
KH
183 ;; We have to decode the file in any environment.
184 (let ((default-enable-multibyte-characters t)
95fa03b4 185 (coding-system-for-read 'iso-2022-7bit))
8f81f784 186 (find-file-read-only (expand-file-name "HELLO" data-directory))))
4ed46869 187
15b3e511
KH
188(defun universal-coding-system-argument ()
189 "Execute an I/O command using the specified coding system."
190 (interactive)
34104362
KH
191 (let* ((default (and buffer-file-coding-system
192 (not (eq (coding-system-type buffer-file-coding-system)
193 t))
194 buffer-file-coding-system))
195 (coding-system (read-coding-system
196 (if default
197 (format "Coding system for following command (default, %s): " default)
198 "Coding system for following command: ")
199 default))
15b3e511 200 (keyseq (read-key-sequence
e14a8f4c 201 (format "Command to execute with %s:" coding-system)))
15b3e511
KH
202 (cmd (key-binding keyseq)))
203 (let ((coding-system-for-read coding-system)
204 (coding-system-for-write coding-system))
205 (message "")
206 (call-interactively cmd))))
207
de94d711 208(defun set-default-coding-systems (coding-system)
0c3154d2 209 "Set default value of various coding systems to CODING-SYSTEM.
387136f6 210This sets the following coding systems:
0c3154d2 211 o coding system of a newly created buffer
8efc03e1
KH
212 o default coding system for subprocess I/O
213This also sets the following values:
387136f6 214 o default value used as file-name-coding-system for converting file names.
03c35c83
EZ
215 o default value for the command `set-terminal-coding-system' (not on MSDOS)
216 o default value for the command `set-keyboard-coding-system'."
de94d711
KH
217 (check-coding-system coding-system)
218 (setq-default buffer-file-coding-system coding-system)
716184d4
RS
219 (if default-enable-multibyte-characters
220 (setq default-file-name-coding-system coding-system))
03c35c83
EZ
221 ;; If coding-system is nil, honor that on MS-DOS as well, so
222 ;; that they could reset the terminal coding system.
223 (unless (and (eq window-system 'pc) coding-system)
224 (setq default-terminal-coding-system coding-system))
de94d711
KH
225 (setq default-keyboard-coding-system coding-system)
226 (setq default-process-coding-system (cons coding-system coding-system)))
227
45d08cb2
KH
228(defalias 'update-iso-coding-systems 'update-coding-systems-internal)
229(make-obsolete 'update-iso-coding-systems 'update-coding-systems-internal)
230
0c3154d2
KH
231(defun prefer-coding-system (coding-system)
232 "Add CODING-SYSTEM at the front of the priority list for automatic detection.
387136f6 233This also sets the following coding systems:
0c3154d2 234 o coding system of a newly created buffer
8efc03e1
KH
235 o default coding system for subprocess I/O
236This also sets the following values:
387136f6 237 o default value used as file-name-coding-system for converting file names.
03c35c83
EZ
238 o default value for the command `set-terminal-coding-system' (not on MSDOS)
239 o default value for the command `set-keyboard-coding-system'
240
241This command does not change the default value of terminal coding system
242for MS-DOS terminal, because DOS terminals only support a single coding
243system, and Emacs automatically sets the default to that coding system at
244startup."
0c3154d2
KH
245 (interactive "zPrefer coding system: ")
246 (if (not (and coding-system (coding-system-p coding-system)))
247 (error "Invalid coding system `%s'" coding-system))
248 (let ((coding-category (coding-system-category coding-system))
8efc03e1 249 (base (coding-system-base coding-system)))
0c3154d2
KH
250 (if (not coding-category)
251 ;; CODING-SYSTEM is no-conversion or undecided.
252 (error "Can't prefer the coding system `%s'" coding-system))
8efc03e1 253 (set coding-category (or base coding-system))
45d08cb2 254 (update-coding-systems-internal)
0c3154d2
KH
255 (if (not (eq coding-category (car coding-category-list)))
256 ;; We must change the order.
257 (setq coding-category-list
258 (cons coding-category
259 (delq coding-category coding-category-list))))
8efc03e1
KH
260 (if (and base (interactive-p))
261 (message "Highest priority is set to %s (base of %s)"
262 base coding-system))
263 (set-default-coding-systems (or base coding-system))))
0c3154d2 264
2c395d56 265(defun find-coding-systems-region-subset-p (list1 list2)
54b226f7
KH
266 "Return non-nil if all elements in LIST1 are included in LIST2.
267Comparison done with EQ."
268 (catch 'tag
269 (while list1
270 (or (memq (car list1) list2)
271 (throw 'tag nil))
272 (setq list1 (cdr list1)))
273 t))
274
3fc7dfe5 275(defun find-coding-systems-region (from to)
54b226f7
KH
276 "Return a list of proper coding systems to encode a text between FROM and TO.
277All coding systems in the list can safely encode any multibyte characters
278in the text.
279
e8dd0160 280If the text contains no multibyte characters, return a list of a single
3fc7dfe5
KH
281element `undecided'."
282 (find-coding-systems-for-charsets (find-charset-region from to)))
54b226f7 283
3fc7dfe5
KH
284(defun find-coding-systems-string (string)
285 "Return a list of proper coding systems to encode STRING.
286All coding systems in the list can safely encode any multibyte characters
287in STRING.
288
e8dd0160 289If STRING contains no multibyte characters, return a list of a single
3fc7dfe5
KH
290element `undecided'."
291 (find-coding-systems-for-charsets (find-charset-string string)))
292
293(defun find-coding-systems-for-charsets (charsets)
294 "Return a list of proper coding systems to encode characters of CHARSETS.
295CHARSETS is a list of character sets."
296 (if (or (null charsets)
297 (and (= (length charsets) 1)
298 (eq 'ascii (car charsets))))
299 '(undecided)
2a17b638 300 (setq charsets (delq 'composition charsets))
251d4f4b 301 (let ((l (coding-system-list 'base-only))
34104362 302 (charset-prefered-codings
3fc7dfe5
KH
303 (mapcar (function
304 (lambda (x)
251d4f4b
KH
305 (if (eq x 'unknown)
306 'raw-text
307 (get-charset-property x 'prefered-coding-system))))
3fc7dfe5 308 charsets))
34104362
KH
309 (priorities (mapcar (function (lambda (x) (symbol-value x)))
310 coding-category-list))
3fc7dfe5 311 codings coding safe)
251d4f4b
KH
312 (if (memq 'unknown charsets)
313 ;; The region contains invalid multibyte characters.
314 (setq l '(raw-text)))
3fc7dfe5
KH
315 (while l
316 (setq coding (car l) l (cdr l))
251d4f4b 317 (if (and (setq safe (coding-system-get coding 'safe-charsets))
3fc7dfe5 318 (or (eq safe t)
2c395d56 319 (find-coding-systems-region-subset-p charsets safe)))
3fc7dfe5 320 ;; We put the higher priority to coding systems included
34104362
KH
321 ;; in CHARSET-PREFERED-CODINGS, and within them, put the
322 ;; higher priority to coding systems which support smaller
3fc7dfe5
KH
323 ;; number of charsets.
324 (let ((priority
34104362
KH
325 (+ (if (coding-system-get coding 'mime-charset) 4096 0)
326 (lsh (length (memq coding priorities)) 7)
327 (if (memq coding charset-prefered-codings) 64 0)
328 (if (> (coding-system-type coding) 0) 32 0)
329 (if (consp safe) (- 32 (length safe)) 0))))
3fc7dfe5
KH
330 (setq codings (cons (cons priority coding) codings)))))
331 (mapcar 'cdr
332 (sort codings (function (lambda (x y) (> (car x) (car y))))))
333 )))
54b226f7 334
51ed58ea
KH
335(defun find-multibyte-characters (from to &optional maxcount excludes)
336 "Find multibyte characters in the region specified by FROM and TO.
337If FROM is a string, find multibyte characters in the string.
338The return value is an alist of the following format:
339 ((CHARSET COUNT CHAR ...) ...)
340where
341 CHARSET is a character set,
342 COUNT is a number of characters,
343 CHARs are found characters of the character set.
344Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
251d4f4b
KH
345Optional 4th arg EXCLUDE is a list of character sets to be ignored.
346
347For invalid characters, CHARs are actually strings."
51ed58ea
KH
348 (let ((chars nil)
349 charset char)
350 (if (stringp from)
351 (let ((idx 0))
352 (while (setq idx (string-match "[^\000-\177]" from idx))
353 (setq char (aref from idx)
354 charset (char-charset char))
251d4f4b
KH
355 (if (eq charset 'unknown)
356 (setq char (match-string 0)))
357 (if (or (eq charset 'unknown)
358 (not (or (eq excludes t) (memq charset excludes))))
51ed58ea
KH
359 (let ((slot (assq charset chars)))
360 (if slot
361 (if (not (memq char (nthcdr 2 slot)))
362 (let ((count (nth 1 slot)))
363 (setcar (cdr slot) (1+ count))
364 (if (or (not maxcount) (< count maxcount))
365 (nconc slot (list char)))))
366 (setq chars (cons (list charset 1 char) chars)))))
367 (setq idx (1+ idx))))
368 (save-excursion
369 (goto-char from)
370 (while (re-search-forward "[^\000-\177]" to t)
371 (setq char (preceding-char)
372 charset (char-charset char))
251d4f4b
KH
373 (if (eq charset 'unknown)
374 (setq char (match-string 0)))
375 (if (or (eq charset 'unknown)
376 (not (or (eq excludes t) (memq charset excludes))))
51ed58ea
KH
377 (let ((slot (assq charset chars)))
378 (if slot
251d4f4b 379 (if (not (member char (nthcdr 2 slot)))
51ed58ea
KH
380 (let ((count (nth 1 slot)))
381 (setcar (cdr slot) (1+ count))
382 (if (or (not maxcount) (< count maxcount))
383 (nconc slot (list char)))))
384 (setq chars (cons (list charset 1 char) chars))))))))
385 (nreverse chars)))
386
c83c4f60
RS
387(defvar last-coding-system-specified nil
388 "Most recent coding system explicitly specified by the user when asked.
389This variable is set whenever Emacs asks the user which coding system
390to use in order to write a file. If you set it to nil explicitly,
391then call `write-region', then afterward this variable will be non-nil
392only if the user was explicitly asked and specified a coding system.")
393
54b226f7 394(defun select-safe-coding-system (from to &optional default-coding-system)
d5266ddf
KH
395 "Ask a user to select a safe coding system from candidates.
396The candidates of coding systems which can safely encode a text
397between FROM and TO are shown in a popup window.
54b226f7
KH
398
399Optional arg DEFAULT-CODING-SYSTEM specifies a coding system to be
400checked at first. If omitted, buffer-file-coding-system of the
401current buffer is used.
402
d5266ddf
KH
403If the text can be encoded safely by DEFAULT-CODING-SYSTEM, it is
404returned without any user interaction.
54b226f7
KH
405
406Kludgy feature: if FROM is a string, the string is the target text,
407and TO is ignored."
408 (or default-coding-system
409 (setq default-coding-system buffer-file-coding-system))
51ed58ea
KH
410 (let* ((charsets (if (stringp from) (find-charset-string from)
411 (find-charset-region from to)))
412 (safe-coding-systems (find-coding-systems-for-charsets charsets)))
251d4f4b
KH
413 (if (or (not enable-multibyte-characters)
414 (eq (car safe-coding-systems) 'undecided)
902e3f77 415 (eq default-coding-system 'no-conversion)
54b226f7
KH
416 (and default-coding-system
417 (memq (coding-system-base default-coding-system)
418 safe-coding-systems)))
419 default-coding-system
420
34104362
KH
421 ;; At first, change each coding system to the corresponding
422 ;; mime-charset name if it is also a coding system.
423 (let ((l safe-coding-systems)
424 mime-charset)
425 (while l
426 (setq mime-charset (coding-system-get (car l) 'mime-charset))
427 (if (and mime-charset (coding-system-p mime-charset))
428 (setcar l mime-charset))
429 (setq l (cdr l))))
430
51ed58ea
KH
431 (let ((non-safe-chars (find-multibyte-characters
432 from to 3
433 (and default-coding-system
434 (coding-system-get default-coding-system
435 'safe-charsets))))
436 overlays)
437 (save-excursion
438 ;; Highlight characters that default-coding-system can't encode.
439 (when (integerp from)
440 (goto-char from)
441 (let ((found nil))
442 (while (and (not found)
443 (re-search-forward "[^\000-\177]" to t))
444 (setq found (assq (char-charset (preceding-char))
445 non-safe-chars))))
446 (beginning-of-line)
447 (set-window-start (selected-window) (point))
448 (save-excursion
449 (while (re-search-forward "[^\000-\177]" to t)
450 (let* ((char (preceding-char))
451 (charset (char-charset char)))
452 (when (assq charset non-safe-chars)
453 (setq overlays (cons (make-overlay (1- (point)) (point))
454 overlays))
455 (overlay-put (car overlays) 'face 'highlight))))))
456
457 ;; At last, ask a user to select a proper coding system.
458 (unwind-protect
459 (save-window-excursion
460 ;; At first, show a helpful message.
461 (with-output-to-temp-buffer "*Warning*"
462 (save-excursion
463 (set-buffer standard-output)
464 (insert "The target text contains the following non ASCII character(s):\n")
465 (let ((len (length non-safe-chars))
466 (shown 0))
467 (while (and non-safe-chars (< shown 3))
468 (when (> (length (car non-safe-chars)) 2)
469 (setq shown (1+ shown))
470 (insert (format "%25s: " (car (car non-safe-chars))))
471 (let ((l (nthcdr 2 (car non-safe-chars))))
472 (while l
251d4f4b
KH
473 (if (or (stringp (car l)) (char-valid-p (car l)))
474 (insert (car l)))
51ed58ea
KH
475 (setq l (cdr l))))
476 (if (> (nth 1 (car non-safe-chars)) 3)
477 (insert "..."))
478 (insert "\n"))
479 (setq non-safe-chars (cdr non-safe-chars)))
480 (if (< shown len)
481 (insert (format "%27s\n" "..."))))
482 (insert (format "\
483These can't be encoded safely by the coding system %s.
54b226f7
KH
484
485Please select one from the following safe coding systems:\n"
51ed58ea
KH
486 default-coding-system))
487 (let ((pos (point))
488 (fill-prefix " "))
489 (mapcar (function (lambda (x) (princ " ") (princ x)))
490 safe-coding-systems)
491 (fill-region-as-paragraph pos (point)))))
492
493 ;; Read a coding system.
494 (let* ((safe-names (mapcar (lambda (x) (list (symbol-name x)))
495 safe-coding-systems))
496 (name (completing-read
497 (format "Select coding system (default %s): "
498 (car safe-coding-systems))
499 safe-names nil t nil nil
500 (car (car safe-names)))))
501 (setq last-coding-system-specified (intern name))
502 (if (integerp (coding-system-eol-type default-coding-system))
503 (setq last-coding-system-specified
504 (coding-system-change-eol-conversion
505 last-coding-system-specified
506 (coding-system-eol-type default-coding-system))))
507 last-coding-system-specified))
508 (kill-buffer "*Warning*")
509 (while overlays
510 (delete-overlay (car overlays))
511 (setq overlays (cdr overlays)))))))))
54b226f7
KH
512
513(setq select-safe-coding-system-function 'select-safe-coding-system)
514
46babb23
KH
515(defun select-message-coding-system ()
516 "Return a coding system to encode the outgoing message of the current buffer.
517It at first tries the first coding system found in these variables
518in this order:
519 (1) local value of `buffer-file-coding-system'
520 (2) value of `sendmail-coding-system'
521 (3) value of `default-buffer-file-coding-system'
522 (4) value of `default-sendmail-coding-system'
523If the found coding system can't encode the current buffer,
524or none of them are bound to a coding system,
48e41165 525it asks the user to select a proper coding system."
46babb23
KH
526 (let ((coding (or (and (local-variable-p 'buffer-file-coding-system)
527 buffer-file-coding-system)
528 sendmail-coding-system
529 default-buffer-file-coding-system
530 default-sendmail-coding-system)))
531 (if (eq coding 'no-conversion)
532 ;; We should never use no-conversion for outgoing mails.
533 (setq coding nil))
534 (if (fboundp select-safe-coding-system-function)
535 (funcall select-safe-coding-system-function
536 (point-min) (point-max) coding)
537 coding)))
4ed46869 538\f
03c35c83 539;;; Language support stuff.
4ed46869 540
4ed46869 541(defvar language-info-alist nil
2c395d56 542 "Alist of language environment definitions.
4ed46869
KH
543Each element looks like:
544 (LANGUAGE-NAME . ((KEY . INFO) ...))
2c395d56
RS
545where LANGUAGE-NAME is a string, the name of the language environment,
546KEY is a symbol denoting the kind of information, and
547INFO is the data associated with KEY.
548Meaningful values for KEY include
549
550 documentation value is documentation of what this language environment
551 is meant for, and how to use it.
552 charset value is a list of the character sets used by this
553 language environment.
554 sample-text value is one line of text,
555 written using those character sets,
556 appropriate for this language environment.
557 setup-function value is a function to call to switch to this
558 language environment.
559 exit-function value is a function to call to leave this
560 language environment.
561 coding-system value is a list of coding systems that are good
562 for saving text written in this language environment.
563 This list serves as suggestions to the user;
564 in effect, as a kind of documentation.
565 coding-priority value is a list of coding systems for this language
566 environment, in order of decreasing priority.
567 This is used to set up the coding system priority
45d08cb2 568 list when you switch to this language environment.
ddb5c041 569 nonascii-translation
7624ebb9 570 value is a translation table to be set in the
45d08cb2 571 variable `nonascii-translation-table' in this
7624ebb9
KH
572 language environment, or a character set from
573 which `nonascii-insert-offset' is calculated.
45d08cb2 574 charset-origin-alist
7624ebb9 575 value is an alist to be set in the variable
ddb5c041
KH
576 `charset-origin-alist' in this language environment.
577 input-method value is a default input method for this language
578 environment.
7624ebb9
KH
579 features value is a list of features requested in this
580 language environment.
ddb5c041
KH
581
582The following keys take effect only when multibyte characters are
583globally disabled, i.e. the value of `default-enable-multibyte-characters'
584is nil.
585
586 unibyte-syntax value is a library name to load to set
e8dd0160 587 unibyte 8-bit character syntaxes for this
ddb5c041
KH
588 language environment.
589
590 unibyte-display value is a coding system to encode characters
591 for the terminal. Characters in the range
592 of 160 to 255 display not as octal escapes,
593 but as non-ASCII characters in this language
594 environment.")
2c395d56
RS
595
596(defun get-language-info (lang-env key)
597 "Return information listed under KEY for language environment LANG-ENV.
598KEY is a symbol denoting the kind of information.
599For a list of useful values for KEY and their meanings,
600see `language-info-alist'."
601 (if (symbolp lang-env)
602 (setq lang-env (symbol-name lang-env)))
603 (let ((lang-slot (assoc-ignore-case lang-env language-info-alist)))
4ed46869
KH
604 (if lang-slot
605 (cdr (assq key (cdr lang-slot))))))
606
f08adf27 607(defun set-language-info (lang-env key info)
2c395d56
RS
608 "Modify part of the definition of language environment LANG-ENV.
609Specifically, this stores the information INFO under KEY
610in the definition of this language environment.
4ed46869 611KEY is a symbol denoting the kind of information.
2c395d56 612INFO is the value for that information.
281d03ec 613
2c395d56 614For a list of useful values for KEY and their meanings,
f08adf27 615see `language-info-alist'."
2c395d56
RS
616 (if (symbolp lang-env)
617 (setq lang-env (symbol-name lang-env)))
4ed46869 618 (let (lang-slot key-slot)
2c395d56 619 (setq lang-slot (assoc lang-env language-info-alist))
4ed46869 620 (if (null lang-slot) ; If no slot for the language, add it.
2c395d56 621 (setq lang-slot (list lang-env)
4ed46869
KH
622 language-info-alist (cons lang-slot language-info-alist)))
623 (setq key-slot (assq key lang-slot))
624 (if (null key-slot) ; If no slot for the key, add it.
625 (progn
626 (setq key-slot (list key))
627 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
f08adf27 628 (setcdr key-slot info)))
4ed46869 629
2c395d56
RS
630(defun set-language-info-alist (lang-env alist &optional parents)
631 "Store ALIST as the definition of language environment LANG-ENV.
632ALIST is an alist of KEY and INFO values. See the documentation of
7624ebb9 633`set-language-info' for the meanings of KEY and INFO.
54b226f7 634
2c395d56
RS
635Optional arg PARENTS is a list of parent menu names; it specifies
636where to put this language environment in the
637Describe Language Environment and Set Language Environment menus.
638For example, (\"European\") means to put this language environment
639in the European submenu in each of those two menus."
640 (if (symbolp lang-env)
641 (setq lang-env (symbol-name lang-env)))
54b226f7
KH
642 (let ((describe-map describe-language-environment-map)
643 (setup-map setup-language-environment-map))
644 (if parents
645 (let ((l parents)
646 map parent-symbol parent)
647 (while l
648 (if (symbolp (setq parent-symbol (car l)))
649 (setq parent (symbol-name parent))
650 (setq parent parent-symbol parent-symbol (intern parent)))
651 (setq map (lookup-key describe-map (vector parent-symbol)))
652 (if (not map)
653 (progn
654 (setq map (intern (format "describe-%s-environment-map"
655 (downcase parent))))
656 (define-prefix-command map)
657 (define-key-after describe-map (vector parent-symbol)
658 (cons parent map) t)))
659 (setq describe-map (symbol-value map))
660 (setq map (lookup-key setup-map (vector parent-symbol)))
661 (if (not map)
662 (progn
663 (setq map (intern (format "setup-%s-environment-map"
664 (downcase parent))))
665 (define-prefix-command map)
666 (define-key-after setup-map (vector parent-symbol)
667 (cons parent map) t)))
668 (setq setup-map (symbol-value map))
669 (setq l (cdr l)))))
f08adf27
RS
670
671 ;; Set up menu items for this language env.
7624ebb9 672 (let ((doc (assq 'documentation alist)))
f08adf27
RS
673 (when doc
674 (define-key-after describe-map (vector (intern lang-env))
7624ebb9
KH
675 (cons lang-env 'describe-specified-language-support) t)))
676 (define-key-after setup-map (vector (intern lang-env))
677 (cons lang-env 'setup-specified-language-environment) t)
f08adf27 678
54b226f7 679 (while alist
f08adf27 680 (set-language-info lang-env (car (car alist)) (cdr (car alist)))
54b226f7 681 (setq alist (cdr alist)))))
4ed46869 682
ae302641 683(defun read-language-name (key prompt &optional default)
2c395d56 684 "Read a language environment name which has information for KEY.
ddb5c041 685If KEY is nil, read any language environment.
2c395d56
RS
686Prompt with PROMPT. DEFAULT is the default choice of language environment.
687This returns a language environment name as a string."
4ed46869
KH
688 (let* ((completion-ignore-case t)
689 (name (completing-read prompt
690 language-info-alist
ddb5c041
KH
691 (and key
692 (function (lambda (elm) (assq key elm))))
ae302641 693 t nil nil default)))
13e82c04 694 (if (and (> (length name) 0)
ddb5c041
KH
695 (or (not key)
696 (get-language-info name key)))
13e82c04 697 name)))
4ed46869
KH
698\f
699;;; Multilingual input methods.
700
d0b9c3ab
KH
701(defconst leim-list-file-name "leim-list.el"
702 "Name of LEIM list file.
703This file contains a list of libraries of Emacs input methods (LEIM)
704in the format of Lisp expression for registering each input method.
705Emacs loads this file at startup time.")
706
707(defvar leim-list-header (format "\
708;;; %s -- list of LEIM (Library of Emacs Input Method)
709;;
710;; This file contains a list of LEIM (Library of Emacs Input Method)
e8dd0160 711;; in the same directory as this file. Loading this file registers
d0b9c3ab
KH
712;; the whole input methods in Emacs.
713;;
d33d5fbe 714;; Each entry has the form:
d0b9c3ab
KH
715;; (register-input-method
716;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
717;; TITLE DESCRIPTION
718;; ARG ...)
719;; See the function `register-input-method' for the meanings of arguments.
720;;
721;; If this directory is included in load-path, Emacs automatically
722;; loads this file at startup time.
723
724"
725 leim-list-file-name)
726 "Header to be inserted in LEIM list file.")
727
e55e92ee 728(defvar leim-list-entry-regexp "^(register-input-method"
d0b9c3ab
KH
729 "Regexp matching head of each entry in LEIM list file.
730See also the variable `leim-list-header'")
731
732(defvar update-leim-list-functions
733 '(quail-update-leim-list-file)
734 "List of functions to call to update LEIM list file.
735Each function is called with one arg, LEIM directory name.")
736
a337fe7f
RS
737(defun update-leim-list-file (&rest dirs)
738 "Update LEIM list file in directories DIRS."
d0b9c3ab
KH
739 (let ((functions update-leim-list-functions))
740 (while functions
a337fe7f 741 (apply (car functions) dirs)
d0b9c3ab
KH
742 (setq functions (cdr functions)))))
743
4ed46869
KH
744(defvar current-input-method nil
745 "The current input method for multilingual text.
96db204a 746If nil, that means no input method is activated now.")
4ed46869
KH
747(make-variable-buffer-local 'current-input-method)
748(put 'current-input-method 'permanent-local t)
749
750(defvar current-input-method-title nil
d0b9c3ab 751 "Title string of the current input method shown in mode line.")
4ed46869
KH
752(make-variable-buffer-local 'current-input-method-title)
753(put 'current-input-method-title 'permanent-local t)
754
b4fba33f 755(defcustom default-input-method nil
8861c593 756 "*Default input method for multilingual text (a string).
b4fba33f 757This is the input method activated automatically by the command
9b10b5a3 758`toggle-input-method' (\\[toggle-input-method])."
8861c593 759 :group 'mule
83a03f4d 760 :type '(choice (const nil) string))
b4fba33f 761
723a427a
KH
762(defvar input-method-history nil
763 "History list for some commands that read input methods.")
764(make-variable-buffer-local 'input-method-history)
765(put 'input-method-history 'permanent-local t)
4ed46869
KH
766
767(defvar inactivate-current-input-method-function nil
768 "Function to call for inactivating the current input method.
769Every input method should set this to an appropriate value when activated.
f17ccaee
KH
770This function is called with no argument.
771
772This function should never change the value of `current-input-method'.
773It is set to nil by the function `inactivate-input-method'.")
4ed46869
KH
774(make-variable-buffer-local 'inactivate-current-input-method-function)
775(put 'inactivate-current-input-method-function 'permanent-local t)
776
777(defvar describe-current-input-method-function nil
778 "Function to call for describing the current input method.
779This function is called with no argument.")
780(make-variable-buffer-local 'describe-current-input-method-function)
781(put 'describe-current-input-method-function 'permanent-local t)
782
d0b9c3ab 783(defvar input-method-alist nil
2c395d56 784 "Alist of input method names vs how to use them.
d0b9c3ab 785Each element has the form:
2c395d56
RS
786 (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
787See the function `register-input-method' for the meanings of the elements.")
788
f08adf27 789(defun register-input-method (input-method lang-env &rest args)
2c395d56 790 "Register INPUT-METHOD as an input method for language environment ENV.
f08adf27 791INPUT-METHOD and LANG-ENV are symbols or strings.
d0b9c3ab 792
d0b9c3ab 793The remaining arguments are:
2c395d56
RS
794 ACTIVATE-FUNC, TITLE, DESCRIPTION, and ARGS...
795ACTIVATE-FUNC is a function to call to activate this method.
796TITLE is a string to show in the mode line when this method is active.
797DESCRIPTION is a string describing this method and what it is good for.
798The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
205814ee
KH
799All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
800
801This function is mainly used in the file \"leim-list.el\" which is
802created at building time of emacs, registering all quail input methods
803contained in the emacs distribution.
804
805In case you want to register a new quail input method by yourself, be
806careful to use the same input method title as given in the third
807parameter of `quail-define-package' (if the values are different, the
808string specified in this function takes precedence).
809
810The commands `describe-input-method' and `list-input-methods' need
811this duplicated values to show some information about input methods
812without loading the affected quail packages."
f08adf27
RS
813 (if (symbolp lang-env)
814 (setq lang-env (symbol-name lang-env)))
4ef06f75
KH
815 (if (symbolp input-method)
816 (setq input-method (symbol-name input-method)))
f08adf27 817 (let ((info (cons lang-env args))
d0b9c3ab
KH
818 (slot (assoc input-method input-method-alist)))
819 (if slot
820 (setcdr slot info)
821 (setq slot (cons input-method info))
822 (setq input-method-alist (cons slot input-method-alist)))))
823
4d5ac029 824(defun read-input-method-name (prompt &optional default inhibit-null)
d0b9c3ab 825 "Read a name of input method from a minibuffer prompting with PROMPT.
4d5ac029
RS
826If DEFAULT is non-nil, use that as the default,
827 and substitute it into PROMPT at the first `%s'.
4ef06f75
KH
828If INHIBIT-NULL is non-nil, null input signals an error.
829
830The return value is a string."
4d5ac029
RS
831 (if default
832 (setq prompt (format prompt default)))
d0b9c3ab 833 (let* ((completion-ignore-case t)
723a427a
KH
834 ;; This binding is necessary because input-method-history is
835 ;; buffer local.
d0b9c3ab 836 (input-method (completing-read prompt input-method-alist
87505a98
RS
837 nil t nil 'input-method-history
838 default)))
d0b9c3ab
KH
839 (if (> (length input-method) 0)
840 input-method
841 (if inhibit-null
43807b77 842 (error "No valid input method is specified")))))
d0b9c3ab 843
d0b9c3ab 844(defun activate-input-method (input-method)
2c395d56
RS
845 "Switch to input method INPUT-METHOD for the current buffer.
846If some other input method is already active, turn it off first.
847If INPUT-METHOD is nil, deactivate any current input method."
305a3cb6 848 (if (and input-method (symbolp input-method))
4ef06f75 849 (setq input-method (symbol-name input-method)))
723a427a
KH
850 (if (and current-input-method
851 (not (string= current-input-method input-method)))
305a3cb6 852 (inactivate-input-method))
2c395d56 853 (unless (or current-input-method (null input-method))
d0b9c3ab
KH
854 (let ((slot (assoc input-method input-method-alist)))
855 (if (null slot)
723a427a 856 (error "Can't activate input method `%s'" input-method))
8efc03e1
KH
857 (let ((func (nth 2 slot)))
858 (if (functionp func)
859 (apply (nth 2 slot) input-method (nthcdr 5 slot))
860 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
861 (progn
862 (require (cdr func))
863 (apply (car func) input-method (nthcdr 5 slot)))
864 (error "Can't activate input method `%s'" input-method))))
d0b9c3ab 865 (setq current-input-method input-method)
723a427a 866 (setq current-input-method-title (nth 3 slot))
28885c0e
KH
867 (unwind-protect
868 (run-hooks 'input-method-activate-hook)
869 (force-mode-line-update)))))
15b3e511 870
15b3e511 871(defun inactivate-input-method ()
f17ccaee 872 "Turn off the current input method."
723a427a
KH
873 (when current-input-method
874 (if input-method-history
875 (unless (string= current-input-method (car input-method-history))
876 (setq input-method-history
877 (cons current-input-method
878 (delete current-input-method input-method-history))))
879 (setq input-method-history (list current-input-method)))
880 (unwind-protect
881 (funcall inactivate-current-input-method-function)
15b3e511 882 (unwind-protect
723a427a
KH
883 (run-hooks 'input-method-inactivate-hook)
884 (setq current-input-method nil
28885c0e
KH
885 current-input-method-title nil)
886 (force-mode-line-update)))))
4ed46869 887
8b784951 888(defun set-input-method (input-method)
2c395d56
RS
889 "Select and activate input method INPUT-METHOD for the current buffer.
890This also sets the default input method to the one you specify."
d0b9c3ab 891 (interactive
723a427a 892 (let* ((default (or (car input-method-history) default-input-method)))
42395763 893 (list (read-input-method-name
87505a98 894 (if default "Select input method (default %s): " "Select input method: ")
42395763 895 default t))))
d0b9c3ab 896 (activate-input-method input-method)
42395763 897 (setq default-input-method input-method))
4ed46869
KH
898
899(defun toggle-input-method (&optional arg)
15b3e511 900 "Turn on or off a multilingual text input method for the current buffer.
723a427a 901
7ddbb5bc
RS
902With no prefix argument, if some input method is currently activated,
903turn it off. Otherwise, activate an input method--the one most recently used,
904or the one specified in `default-input-method', or one read from the
905minibuffer.
723a427a 906
7ddbb5bc
RS
907With a prefix arg, read an input method from minibuffer and turn it on.
908The default is the most recent input method specified
909\(not including the currently active input method, if any).
723a427a
KH
910
911When there's no input method to turn on, turn on what read from minibuffer."
4ed46869 912 (interactive "P")
7ddbb5bc
RS
913 (if (and current-input-method (not arg))
914 (inactivate-input-method)
915 (let ((default (or (car input-method-history) default-input-method)))
916 (if (and arg default (equal current-input-method default)
917 (> (length input-method-history) 1))
918 (setq default (nth 1 input-method-history)))
723a427a
KH
919 (activate-input-method
920 (if (or arg (not default))
7ddbb5bc
RS
921 (progn
922 (read-input-method-name
923 (if default "Input method (default %s): " "Input method: " )
924 default t))
723a427a
KH
925 default))
926 (or default-input-method
927 (setq default-input-method current-input-method)))))
d0b9c3ab
KH
928
929(defun describe-input-method (input-method)
2c395d56 930 "Describe input method INPUT-METHOD."
d0b9c3ab
KH
931 (interactive
932 (list (read-input-method-name
933 "Describe input method (default, current choice): ")))
78754934 934 (if (and input-method (symbolp input-method))
4ef06f75 935 (setq input-method (symbol-name input-method)))
d0b9c3ab
KH
936 (if (null input-method)
937 (describe-current-input-method)
938 (with-output-to-temp-buffer "*Help*"
939 (let ((elt (assoc input-method input-method-alist)))
940 (princ (format "Input method: %s (`%s' in mode line) for %s\n %s\n"
941 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt)))))))
942
943(defun describe-current-input-method ()
96db204a 944 "Describe the input method currently in use."
4ed46869
KH
945 (if current-input-method
946 (if (and (symbolp describe-current-input-method-function)
947 (fboundp describe-current-input-method-function))
948 (funcall describe-current-input-method-function)
949 (message "No way to describe the current input method `%s'"
950 (cdr current-input-method))
951 (ding))
d0b9c3ab 952 (error "No input method is activated now")))
4ed46869 953
d3459641 954(defun read-multilingual-string (prompt &optional initial-input input-method)
4ed46869
KH
955 "Read a multilingual string from minibuffer, prompting with string PROMPT.
956The input method selected last time is activated in minibuffer.
15b3e511 957If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
d0b9c3ab
KH
958initially.
959Optional 3rd argument INPUT-METHOD specifies the input method
4ef06f75
KH
960to be activated instead of the one selected last time. It is a symbol
961or a string."
88d559ec
KH
962 (setq input-method
963 (or input-method
d3459641 964 current-input-method
88d559ec
KH
965 default-input-method
966 (read-input-method-name "Input method: " nil t)))
3df60841 967 (if (and input-method (symbolp input-method))
4ef06f75 968 (setq input-method (symbol-name input-method)))
305a3cb6
KH
969 (let ((prev-input-method current-input-method))
970 (unwind-protect
971 (progn
972 (activate-input-method input-method)
973 (read-string prompt initial-input nil nil t))
974 (activate-input-method prev-input-method))))
4ed46869
KH
975
976;; Variables to control behavior of input methods. All input methods
977;; should react to these variables.
978
8efc03e1
KH
979(defcustom input-method-verbose-flag 'default
980 "*A flag to control extra guidance given by input methods.
981The value should be nil, t, `complex-only', or `default'.
4ed46869 982
cb29dfb6 983The extra guidance is done by showing list of available keys in echo
8efc03e1
KH
984area. When you use the input method in the minibuffer, the guidance
985is shown at the bottom short window (split from the existing window).
c27c4ed8 986
8efc03e1
KH
987If the value is t, extra guidance is always given, if the value is
988nil, extra guidance is always suppressed.
989
990If the value is `complex-only', only complex input methods such as
991`chinese-py' and `japanese' give extra guidance.
992
993If the value is `default', complex input methods always give extra
994guidance, but simple input methods give it only when you are not in
995the minibuffer.
996
997See also the variable `input-method-highlight-flag'."
998 :type '(choice (const t) (const nil) (const complex-only) (const default))
42395763
RS
999 :group 'mule)
1000
1001(defcustom input-method-highlight-flag t
1002 "*If this flag is non-nil, input methods highlight partially-entered text.
1003For instance, while you are in the middle of a Quail input method sequence,
1004the text inserted so far is temporarily underlined.
8efc03e1
KH
1005The underlining goes away when you finish or abort the input method sequence.
1006See also the variable `input-method-verbose-flag'."
42395763
RS
1007 :type 'boolean
1008 :group 'mule)
4ed46869
KH
1009
1010(defvar input-method-activate-hook nil
f17ccaee
KH
1011 "Normal hook run just after an input method is activated.
1012
1013The variable `current-input-method' keeps the input method name
1014just activated.")
4ed46869
KH
1015
1016(defvar input-method-inactivate-hook nil
f17ccaee
KH
1017 "Normal hook run just after an input method is inactivated.
1018
1019The variable `current-input-method' still keeps the input method name
4d0e6a11 1020just inactivated.")
4ed46869
KH
1021
1022(defvar input-method-after-insert-chunk-hook nil
1023 "Normal hook run just after an input method insert some chunk of text.")
1024
dccca980
KH
1025(defvar input-method-exit-on-first-char nil
1026 "This flag controls a timing when an input method returns.
1027Usually, the input method does not return while there's a possibility
1028that it may find a different translation if a user types another key.
39e643e2
RS
1029But, it this flag is non-nil, the input method returns as soon as
1030the current key sequence gets long enough to have some valid translation.")
dccca980
KH
1031
1032(defvar input-method-use-echo-area nil
1033 "This flag controls how an input method shows an intermediate key sequence.
39e643e2
RS
1034Usually, the input method inserts the intermediate key sequence,
1035or candidate translations corresponding to the sequence,
1036at point in the current buffer.
1037But, if this flag is non-nil, it displays them in echo area instead.")
dccca980 1038
723a427a
KH
1039(defvar input-method-exit-on-invalid-key nil
1040 "This flag controls the behaviour of an input method on invalid key input.
1041Usually, when a user types a key which doesn't start any character
1042handled by the input method, the key is handled by turning off the
e8dd0160 1043input method temporarily. After that key, the input method is re-enabled.
723a427a
KH
1044But, if this flag is non-nil, the input method is never back on.")
1045
4ed46869 1046\f
8efc03e1
KH
1047(defvar set-language-environment-hook nil
1048 "Normal hook run after some language environment is set.
1049
1050When you set some hook function here, that effect usually should not
1051be inherited to another language environment. So, you had better set
1052another function in `exit-language-environment-hook' (which see) to
1053cancel the effect.")
1054
1055(defvar exit-language-environment-hook nil
1056 "Normal hook run after exiting from some language environment.
1057When this hook is run, the variable `current-language-environment'
1058is still bound to the language environment being exited.
1059
e8dd0160 1060This hook is mainly used for canceling the effect of
8efc03e1
KH
1061`set-language-environment-hook' (which-see).")
1062
b0648a00
RS
1063(put 'setup-specified-language-environment 'apropos-inhibit t)
1064
15b3e511 1065(defun setup-specified-language-environment ()
f08adf27 1066 "Switch to a specified language environment."
15b3e511 1067 (interactive)
f850d782 1068 (let (language-name)
15b3e511
KH
1069 (if (and (symbolp last-command-event)
1070 (or (not (eq last-command-event 'Default))
1071 (setq last-command-event 'English))
f850d782
RS
1072 (setq language-name (symbol-name last-command-event)))
1073 (set-language-environment language-name)
15b3e511 1074 (error "Bogus calling sequence"))))
4ed46869 1075
8861c593
RS
1076(defcustom current-language-environment "English"
1077 "The last language environment specified with `set-language-environment'."
dff1aa24 1078 :set (lambda (symbol value) (set-language-environment value))
8861c593
RS
1079 :initialize 'custom-initialize-default
1080 :group 'mule
1081 :type 'string)
f850d782 1082
ddb5c041
KH
1083(defun reset-language-environment ()
1084 "Reset multilingual environment of Emacs to the default status.
1085
1086The default status is as follows:
1087
1088 The default value of buffer-file-coding-system is nil.
1089 The default coding system for process I/O is nil.
1090 The default value for the command `set-terminal-coding-system' is nil.
1091 The default value for the command `set-keyboard-coding-system' is nil.
1092
1093 The order of priorities of coding categories and the coding system
1094 bound to each category are as follows
1095 coding category coding system
1096 --------------------------------------------------
1097 coding-category-iso-8-2 iso-latin-1
1098 coding-category-iso-8-1 iso-latin-1
1099 coding-category-iso-7-tight iso-2022-jp
1100 coding-category-iso-7 iso-2022-7bit
1101 coding-category-iso-7-else iso-2022-7bit-lock
1102 coding-category-iso-8-else iso-2022-8bit-ss2
1103 coding-category-emacs-mule emacs-mule
1104 coding-category-raw-text raw-text
1105 coding-category-sjis japanese-shift-jis
1106 coding-category-big5 chinese-big5
1107 coding-category-ccl nil
e8dd0160 1108 coding-category-binary no-conversion
ddb5c041
KH
1109"
1110 (interactive)
1111 ;; This function formerly set default-enable-multibyte-characters to t,
1112 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1113
1114 (setq coding-category-iso-7-tight 'iso-2022-jp
1115 coding-category-iso-7 'iso-2022-7bit
1116 coding-category-iso-8-1 'iso-latin-1
1117 coding-category-iso-8-2 'iso-latin-1
1118 coding-category-iso-7-else 'iso-2022-7bit-lock
1119 coding-category-iso-8-else 'iso-2022-8bit-ss2
1120 coding-category-emacs-mule 'emacs-mule
1121 coding-category-raw-text 'raw-text
1122 coding-category-sjis 'japanese-shift-jis
1123 coding-category-big5 'chinese-big5
1124 coding-category-ccl nil
1125 coding-category-binary 'no-conversion)
1126
1127 (set-coding-priority
1128 '(coding-category-iso-8-1
1129 coding-category-iso-8-2
1130 coding-category-iso-7-tight
1131 coding-category-iso-7
1132 coding-category-iso-7-else
1133 coding-category-iso-8-else
1134 coding-category-emacs-mule
1135 coding-category-raw-text
1136 coding-category-sjis
1137 coding-category-big5
1138 coding-category-ccl
1139 coding-category-binary))
1140
91693d18
KH
1141 (update-coding-systems-internal)
1142
ddb5c041
KH
1143 (set-default-coding-systems nil)
1144 ;; Don't alter the terminal and keyboard coding systems here.
1145 ;; The terminal still supports the same coding system
1146 ;; that it supported a minute ago.
1147;;; (set-terminal-coding-system-internal nil)
1148;;; (set-keyboard-coding-system-internal nil)
1149
1150 (setq nonascii-translation-table nil
1151 nonascii-insert-offset 0))
1152
166246f7 1153(defun set-language-environment (language-name)
6c05d680
RS
1154 "Set up multi-lingual environment for using LANGUAGE-NAME.
1155This sets the coding system priority and the default input method
8861c593
RS
1156and sometimes other things. LANGUAGE-NAME should be a string
1157which is the name of a language environment. For example, \"Latin-1\"
1158specifies the character set for the major languages of Western Europe."
8efc03e1 1159 (interactive (list (read-language-name
ddb5c041 1160 nil
8efc03e1 1161 "Set language environment (default, English): ")))
4ef06f75
KH
1162 (if language-name
1163 (if (symbolp language-name)
1164 (setq language-name (symbol-name language-name)))
1165 (setq language-name "English"))
ddb5c041 1166 (or (assoc-ignore-case language-name language-info-alist)
f850d782 1167 (error "Language environment not defined: %S" language-name))
8efc03e1
KH
1168 (if current-language-environment
1169 (let ((func (get-language-info current-language-environment
1170 'exit-function)))
e63645c2
KH
1171 (run-hooks 'exit-language-environment-hook)
1172 (if (fboundp func) (funcall func))))
03c35c83
EZ
1173 (let ((default-eol-type (coding-system-eol-type
1174 default-buffer-file-coding-system)))
1175 (reset-language-environment)
ddb5c041 1176
03c35c83
EZ
1177 (setq current-language-environment language-name)
1178 (set-language-environment-coding-systems language-name default-eol-type))
ddb5c041
KH
1179 (let ((input-method (get-language-info language-name 'input-method)))
1180 (when input-method
1181 (setq default-input-method input-method)
1182 (if input-method-history
1183 (setq input-method-history
1184 (cons input-method
1185 (delete input-method input-method-history))))))
1186 (let ((nonascii (get-language-info language-name 'nonascii-translation)))
03c35c83
EZ
1187 (cond
1188 ((char-table-p nonascii)
1189 (setq nonascii-translation-table nonascii))
1190 ((eq window-system 'pc)
1191 ;; DOS terminals' default is to use a special non-ASCII translation
1192 ;; table as appropriate for the installed codepage.
1193 (setq
1194 nonascii-translation-table (symbol-value
1195 (intern
1196 (concat "cp"
1197 dos-codepage
1198 "-nonascii-translation-table")))))
1199 ((charsetp nonascii)
1200 (setq nonascii-insert-offset (- (make-char nonascii) 128)))))
ddb5c041 1201
45d08cb2
KH
1202 (setq charset-origin-alist
1203 (get-language-info language-name 'charset-origin-alist))
ddb5c041 1204
63283a8f 1205 ;; Unibyte setups if necessary.
ddb5c041 1206 (unless default-enable-multibyte-characters
63283a8f 1207 ;; Syntax and case table.
ddb5c041
KH
1208 (let ((syntax (get-language-info language-name 'unibyte-syntax)))
1209 (if syntax
1210 (let ((set-case-syntax-set-multibyte nil))
63283a8f
KH
1211 (load syntax nil t))
1212 ;; No information for syntax and case. Reset to the defaults.
1213 (let ((syntax-table (standard-syntax-table))
1214 (case-table (standard-case-table))
03c35c83 1215 (ch (if (eq window-system 'pc) 128 160)))
63283a8f
KH
1216 (while (< ch 256)
1217 (modify-syntax-entry ch " " syntax-table)
1218 (aset case-table ch ch)
1219 (setq ch (1+ ch)))
1220 (set-char-table-extra-slot case-table 0 nil)
1221 (set-char-table-extra-slot case-table 1 nil)
1222 (set-char-table-extra-slot case-table 2 nil))
1223 (set-standard-case-table (standard-case-table))
1224 (let ((list (buffer-list)))
1225 (while list
1226 (with-current-buffer (car list)
1227 (set-case-table (standard-case-table)))
1228 (setq list (cdr list))))))
1229 ;; Display table and coding system for terminal.
ddb5c041
KH
1230 (let ((coding (get-language-info language-name 'unibyte-display)))
1231 (if coding
03c35c83
EZ
1232 (standard-display-european-internal)
1233 (standard-display-default (if (eq window-system 'pc) 128 160) 255)
1234 (aset standard-display-table 146 nil))
1235 (or (eq window-system 'pc)
1236 (set-terminal-coding-system coding))))
ddb5c041
KH
1237
1238 (let ((required-features (get-language-info language-name 'features)))
1239 (while required-features
1240 (require (car required-features))
1241 (setq required-features (cdr required-features))))
1242 (let ((func (get-language-info language-name 'setup-function)))
1243 (if (fboundp func)
1244 (funcall func)))
8efc03e1 1245 (run-hooks 'set-language-environment-hook)
f850d782 1246 (force-mode-line-update t))
4ed46869 1247
51a8fc1d
RS
1248(defun standard-display-european-internal ()
1249 ;; Actually set up direct output of non-ASCII characters.
03c35c83
EZ
1250 (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
1251 ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
1252 ;; the native font, and codes 160 and 146 stand for something very
1253 ;; different there.
1254 (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
1255 (progn
1256 ;; Make non-line-break space display as a plain space.
1257 ;; Most X fonts do the wrong thing for code 160.
1258 (aset standard-display-table 160 [32])
1259 ;; Most Windows programs send out apostrophe's as \222. Most X fonts
1260 ;; don't contain a character at that position. Map it to the ASCII
1261 ;; apostrophe.
1262 (aset standard-display-table 146 [39]))))
1263
1264(defun set-language-environment-coding-systems (language-name
1265 &optional eol-type)
1266 "Do various coding system setups for language environment LANGUAGE-NAME.
1267
1268The optional arg EOL-TYPE specifies the eol-type of the default value
1269of buffer-file-coding-system set by this function."
54b226f7
KH
1270 (let* ((priority (get-language-info language-name 'coding-priority))
1271 (default-coding (car priority)))
1272 (if priority
1273 (let ((categories (mapcar 'coding-system-category priority)))
03c35c83
EZ
1274 (set-default-coding-systems
1275 (if (memq eol-type '(0 1 2 unix dos mac))
1276 (coding-system-change-eol-conversion default-coding eol-type)
1277 default-coding))
46babb23 1278 (setq default-sendmail-coding-system default-coding)
54b226f7
KH
1279 (set-coding-priority categories)
1280 (while priority
1281 (set (car categories) (car priority))
1282 (setq priority (cdr priority) categories (cdr categories)))
45d08cb2 1283 (update-coding-systems-internal)))))
54b226f7 1284
4ed46869
KH
1285;; Print all arguments with `princ', then print "\n".
1286(defsubst princ-list (&rest args)
1287 (while args (princ (car args)) (setq args (cdr args)))
1288 (princ "\n"))
1289
b0648a00
RS
1290(put 'describe-specified-language-support 'apropos-inhibit t)
1291
48082651 1292;; Print a language specific information such as input methods,
13e82c04 1293;; charsets, and coding systems. This function is intended to be
48082651 1294;; called from the menu:
281d03ec 1295;; [menu-bar mule describe-language-environment LANGUAGE]
48082651
KH
1296;; and should not run it by `M-x describe-current-input-method-function'.
1297(defun describe-specified-language-support ()
96db204a 1298 "Describe how Emacs supports the specified language environment."
48082651 1299 (interactive)
281d03ec 1300 (let (language-name)
48082651 1301 (if (not (and (symbolp last-command-event)
281d03ec 1302 (setq language-name (symbol-name last-command-event))))
48082651 1303 (error "Bogus calling sequence"))
281d03ec
RS
1304 (describe-language-environment language-name)))
1305
1306(defun describe-language-environment (language-name)
1307 "Describe how Emacs supports language environment LANGUAGE-NAME."
78754934
KH
1308 (interactive
1309 (list (read-language-name
1310 'documentation
8adfa8be 1311 "Describe language environment (default, current choice): ")))
f850d782
RS
1312 (if (null language-name)
1313 (setq language-name current-language-environment))
281d03ec
RS
1314 (if (or (null language-name)
1315 (null (get-language-info language-name 'documentation)))
1316 (error "No documentation for the specified language"))
4ef06f75
KH
1317 (if (symbolp language-name)
1318 (setq language-name (symbol-name language-name)))
281d03ec 1319 (let ((doc (get-language-info language-name 'documentation)))
48082651 1320 (with-output-to-temp-buffer "*Help*"
a33c9d6f 1321 (princ-list language-name " language environment" "\n")
13e82c04 1322 (if (stringp doc)
d0b9c3ab
KH
1323 (progn
1324 (princ-list doc)
1325 (terpri)))
15b3e511
KH
1326 (let ((str (get-language-info language-name 'sample-text)))
1327 (if (stringp str)
1328 (progn
281d03ec 1329 (princ "Sample text:\n")
d0b9c3ab
KH
1330 (princ-list " " str)
1331 (terpri))))
ddb5c041
KH
1332 (let ((input-method (get-language-info language-name 'input-method))
1333 (l input-method-alist))
1334 (princ "Input methods")
1335 (when input-method
1336 (princ (format " (default, %s)" input-method))
1337 (setq input-method (assoc input-method input-method-alist))
1338 (setq l (cons input-method (delete input-method l))))
1339 (princ ":\n")
15b3e511 1340 (while l
d0b9c3ab
KH
1341 (if (string= language-name (nth 1 (car l)))
1342 (princ-list " " (car (car l))
1343 (format " (`%s' in mode line)" (nth 3 (car l)))))
15b3e511 1344 (setq l (cdr l))))
281d03ec
RS
1345 (terpri)
1346 (princ "Character sets:\n")
15b3e511
KH
1347 (let ((l (get-language-info language-name 'charset)))
1348 (if (null l)
1349 (princ-list " nothing specific to " language-name)
1350 (while l
1351 (princ-list " " (car l) ": "
1352 (charset-description (car l)))
1353 (setq l (cdr l)))))
281d03ec
RS
1354 (terpri)
1355 (princ "Coding systems:\n")
15b3e511
KH
1356 (let ((l (get-language-info language-name 'coding-system)))
1357 (if (null l)
1358 (princ-list " nothing specific to " language-name)
48082651 1359 (while l
281d03ec
RS
1360 (princ (format " %s (`%c' in mode line):\n\t%s\n"
1361 (car l)
1362 (coding-system-mnemonic (car l))
a904b20b 1363 (coding-system-doc-string (car l))))
8efc03e1
KH
1364 (let ((aliases (coding-system-get (car l) 'alias-coding-systems)))
1365 (when aliases
1366 (princ "\t")
1367 (princ (cons 'alias: (cdr aliases)))
1368 (terpri)))
15b3e511 1369 (setq l (cdr l))))))))
4ed46869
KH
1370\f
1371;;; Charset property
1372
251d4f4b 1373(defun get-charset-property (charset propname)
4ed46869
KH
1374 "Return the value of CHARSET's PROPNAME property.
1375This is the last value stored with
96db204a 1376 (put-charset-property CHARSET PROPNAME VALUE)."
251d4f4b
KH
1377 (and (not (eq charset 'composition))
1378 (plist-get (charset-plist charset) propname)))
4ed46869 1379
251d4f4b 1380(defun put-charset-property (charset propname value)
4ed46869
KH
1381 "Store CHARSETS's PROPNAME property with value VALUE.
1382It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
1300d43f
KH
1383 (or (eq charset 'composition)
1384 (set-charset-plist charset
1385 (plist-put (charset-plist charset) propname value))))
4ed46869
KH
1386
1387;;; Character code property
1388(put 'char-code-property-table 'char-table-extra-slots 0)
1389
1390(defvar char-code-property-table
1391 (make-char-table 'char-code-property-table)
1392 "Char-table containing a property list of each character code.
1393
1394See also the documentation of `get-char-code-property' and
96db204a 1395`put-char-code-property'.")
4ed46869
KH
1396
1397(defun get-char-code-property (char propname)
1398 "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
1399 (let ((plist (aref char-code-property-table char)))
1400 (if (listp plist)
1401 (car (cdr (memq propname plist))))))
1402
1403(defun put-char-code-property (char propname value)
1404 "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
1405It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
1406 (let ((plist (aref char-code-property-table char)))
1407 (if plist
1408 (let ((slot (memq propname plist)))
1409 (if slot
1410 (setcar (cdr slot) value)
1411 (nconc plist (list propname value))))
1412 (aset char-code-property-table char (list propname value)))))
1413
1414;;; mule-cmds.el ends here