(sendmail-coding-system): Doc-string modified.
[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
KH
133
134;; These are meaningless when running under X.
4ed46869 135(put 'set-terminal-coding-system 'menu-enable
7624ebb9 136 '(not window-system))
15b3e511 137(put 'set-keyboard-coding-system 'menu-enable
7624ebb9 138 '(not window-system))
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.
8efc03e1
KH
215 o default value for the command `set-terminal-coding-system'
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))
de94d711
KH
221 (setq default-terminal-coding-system coding-system)
222 (setq default-keyboard-coding-system coding-system)
223 (setq default-process-coding-system (cons coding-system coding-system)))
224
45d08cb2
KH
225(defalias 'update-iso-coding-systems 'update-coding-systems-internal)
226(make-obsolete 'update-iso-coding-systems 'update-coding-systems-internal)
227
0c3154d2
KH
228(defun prefer-coding-system (coding-system)
229 "Add CODING-SYSTEM at the front of the priority list for automatic detection.
387136f6 230This also sets the following coding systems:
0c3154d2 231 o coding system of a newly created buffer
8efc03e1
KH
232 o default coding system for subprocess I/O
233This also sets the following values:
387136f6 234 o default value used as file-name-coding-system for converting file names.
8efc03e1
KH
235 o default value for the command `set-terminal-coding-system'
236 o default value for the command `set-keyboard-coding-system'"
0c3154d2
KH
237 (interactive "zPrefer coding system: ")
238 (if (not (and coding-system (coding-system-p coding-system)))
239 (error "Invalid coding system `%s'" coding-system))
240 (let ((coding-category (coding-system-category coding-system))
8efc03e1 241 (base (coding-system-base coding-system)))
0c3154d2
KH
242 (if (not coding-category)
243 ;; CODING-SYSTEM is no-conversion or undecided.
244 (error "Can't prefer the coding system `%s'" coding-system))
8efc03e1 245 (set coding-category (or base coding-system))
45d08cb2 246 (update-coding-systems-internal)
0c3154d2
KH
247 (if (not (eq coding-category (car coding-category-list)))
248 ;; We must change the order.
249 (setq coding-category-list
250 (cons coding-category
251 (delq coding-category coding-category-list))))
8efc03e1
KH
252 (if (and base (interactive-p))
253 (message "Highest priority is set to %s (base of %s)"
254 base coding-system))
255 (set-default-coding-systems (or base coding-system))))
0c3154d2 256
2c395d56 257(defun find-coding-systems-region-subset-p (list1 list2)
54b226f7
KH
258 "Return non-nil if all elements in LIST1 are included in LIST2.
259Comparison done with EQ."
260 (catch 'tag
261 (while list1
262 (or (memq (car list1) list2)
263 (throw 'tag nil))
264 (setq list1 (cdr list1)))
265 t))
266
3fc7dfe5 267(defun find-coding-systems-region (from to)
54b226f7
KH
268 "Return a list of proper coding systems to encode a text between FROM and TO.
269All coding systems in the list can safely encode any multibyte characters
270in the text.
271
272If the text contains no multibyte charcters, return a list of a single
3fc7dfe5
KH
273element `undecided'."
274 (find-coding-systems-for-charsets (find-charset-region from to)))
54b226f7 275
3fc7dfe5
KH
276(defun find-coding-systems-string (string)
277 "Return a list of proper coding systems to encode STRING.
278All coding systems in the list can safely encode any multibyte characters
279in STRING.
280
281If STRING contains no multibyte charcters, return a list of a single
282element `undecided'."
283 (find-coding-systems-for-charsets (find-charset-string string)))
284
285(defun find-coding-systems-for-charsets (charsets)
286 "Return a list of proper coding systems to encode characters of CHARSETS.
287CHARSETS is a list of character sets."
288 (if (or (null charsets)
289 (and (= (length charsets) 1)
290 (eq 'ascii (car charsets))))
291 '(undecided)
292 (let ((l coding-system-list)
34104362 293 (charset-prefered-codings
3fc7dfe5
KH
294 (mapcar (function
295 (lambda (x)
296 (get-charset-property x 'prefered-coding-system)))
297 charsets))
34104362
KH
298 (priorities (mapcar (function (lambda (x) (symbol-value x)))
299 coding-category-list))
3fc7dfe5
KH
300 codings coding safe)
301 (while l
302 (setq coding (car l) l (cdr l))
303 (if (and (eq coding (coding-system-base coding))
304 (setq safe (coding-system-get coding 'safe-charsets))
305 (or (eq safe t)
2c395d56 306 (find-coding-systems-region-subset-p charsets safe)))
3fc7dfe5 307 ;; We put the higher priority to coding systems included
34104362
KH
308 ;; in CHARSET-PREFERED-CODINGS, and within them, put the
309 ;; higher priority to coding systems which support smaller
3fc7dfe5
KH
310 ;; number of charsets.
311 (let ((priority
34104362
KH
312 (+ (if (coding-system-get coding 'mime-charset) 4096 0)
313 (lsh (length (memq coding priorities)) 7)
314 (if (memq coding charset-prefered-codings) 64 0)
315 (if (> (coding-system-type coding) 0) 32 0)
316 (if (consp safe) (- 32 (length safe)) 0))))
3fc7dfe5
KH
317 (setq codings (cons (cons priority coding) codings)))))
318 (mapcar 'cdr
319 (sort codings (function (lambda (x y) (> (car x) (car y))))))
320 )))
54b226f7 321
51ed58ea
KH
322(defun find-multibyte-characters (from to &optional maxcount excludes)
323 "Find multibyte characters in the region specified by FROM and TO.
324If FROM is a string, find multibyte characters in the string.
325The return value is an alist of the following format:
326 ((CHARSET COUNT CHAR ...) ...)
327where
328 CHARSET is a character set,
329 COUNT is a number of characters,
330 CHARs are found characters of the character set.
331Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
332Optioanl 4th arg EXCLUDE is a list of character sets to be ignored."
333 (let ((chars nil)
334 charset char)
335 (if (stringp from)
336 (let ((idx 0))
337 (while (setq idx (string-match "[^\000-\177]" from idx))
338 (setq char (aref from idx)
339 charset (char-charset char))
340 (if (not (memq charset excludes))
341 (let ((slot (assq charset chars)))
342 (if slot
343 (if (not (memq char (nthcdr 2 slot)))
344 (let ((count (nth 1 slot)))
345 (setcar (cdr slot) (1+ count))
346 (if (or (not maxcount) (< count maxcount))
347 (nconc slot (list char)))))
348 (setq chars (cons (list charset 1 char) chars)))))
349 (setq idx (1+ idx))))
350 (save-excursion
351 (goto-char from)
352 (while (re-search-forward "[^\000-\177]" to t)
353 (setq char (preceding-char)
354 charset (char-charset char))
355 (if (not (memq charset excludes))
356 (let ((slot (assq charset chars)))
357 (if slot
358 (if (not (memq char (nthcdr 2 slot)))
359 (let ((count (nth 1 slot)))
360 (setcar (cdr slot) (1+ count))
361 (if (or (not maxcount) (< count maxcount))
362 (nconc slot (list char)))))
363 (setq chars (cons (list charset 1 char) chars))))))))
364 (nreverse chars)))
365
c83c4f60
RS
366(defvar last-coding-system-specified nil
367 "Most recent coding system explicitly specified by the user when asked.
368This variable is set whenever Emacs asks the user which coding system
369to use in order to write a file. If you set it to nil explicitly,
370then call `write-region', then afterward this variable will be non-nil
371only if the user was explicitly asked and specified a coding system.")
372
54b226f7 373(defun select-safe-coding-system (from to &optional default-coding-system)
d5266ddf
KH
374 "Ask a user to select a safe coding system from candidates.
375The candidates of coding systems which can safely encode a text
376between FROM and TO are shown in a popup window.
54b226f7
KH
377
378Optional arg DEFAULT-CODING-SYSTEM specifies a coding system to be
379checked at first. If omitted, buffer-file-coding-system of the
380current buffer is used.
381
d5266ddf
KH
382If the text can be encoded safely by DEFAULT-CODING-SYSTEM, it is
383returned without any user interaction.
54b226f7
KH
384
385Kludgy feature: if FROM is a string, the string is the target text,
386and TO is ignored."
387 (or default-coding-system
388 (setq default-coding-system buffer-file-coding-system))
51ed58ea
KH
389 (let* ((charsets (if (stringp from) (find-charset-string from)
390 (find-charset-region from to)))
391 (safe-coding-systems (find-coding-systems-for-charsets charsets)))
54b226f7 392 (if (or (eq (car safe-coding-systems) 'undecided)
902e3f77 393 (eq default-coding-system 'no-conversion)
54b226f7
KH
394 (and default-coding-system
395 (memq (coding-system-base default-coding-system)
396 safe-coding-systems)))
397 default-coding-system
398
34104362
KH
399 ;; At first, change each coding system to the corresponding
400 ;; mime-charset name if it is also a coding system.
401 (let ((l safe-coding-systems)
402 mime-charset)
403 (while l
404 (setq mime-charset (coding-system-get (car l) 'mime-charset))
405 (if (and mime-charset (coding-system-p mime-charset))
406 (setcar l mime-charset))
407 (setq l (cdr l))))
408
51ed58ea
KH
409 (let ((non-safe-chars (find-multibyte-characters
410 from to 3
411 (and default-coding-system
412 (coding-system-get default-coding-system
413 'safe-charsets))))
414 overlays)
415 (save-excursion
416 ;; Highlight characters that default-coding-system can't encode.
417 (when (integerp from)
418 (goto-char from)
419 (let ((found nil))
420 (while (and (not found)
421 (re-search-forward "[^\000-\177]" to t))
422 (setq found (assq (char-charset (preceding-char))
423 non-safe-chars))))
424 (beginning-of-line)
425 (set-window-start (selected-window) (point))
426 (save-excursion
427 (while (re-search-forward "[^\000-\177]" to t)
428 (let* ((char (preceding-char))
429 (charset (char-charset char)))
430 (when (assq charset non-safe-chars)
431 (setq overlays (cons (make-overlay (1- (point)) (point))
432 overlays))
433 (overlay-put (car overlays) 'face 'highlight))))))
434
435 ;; At last, ask a user to select a proper coding system.
436 (unwind-protect
437 (save-window-excursion
438 ;; At first, show a helpful message.
439 (with-output-to-temp-buffer "*Warning*"
440 (save-excursion
441 (set-buffer standard-output)
442 (insert "The target text contains the following non ASCII character(s):\n")
443 (let ((len (length non-safe-chars))
444 (shown 0))
445 (while (and non-safe-chars (< shown 3))
446 (when (> (length (car non-safe-chars)) 2)
447 (setq shown (1+ shown))
448 (insert (format "%25s: " (car (car non-safe-chars))))
449 (let ((l (nthcdr 2 (car non-safe-chars))))
450 (while l
451 (insert (car l))
452 (setq l (cdr l))))
453 (if (> (nth 1 (car non-safe-chars)) 3)
454 (insert "..."))
455 (insert "\n"))
456 (setq non-safe-chars (cdr non-safe-chars)))
457 (if (< shown len)
458 (insert (format "%27s\n" "..."))))
459 (insert (format "\
460These can't be encoded safely by the coding system %s.
54b226f7
KH
461
462Please select one from the following safe coding systems:\n"
51ed58ea
KH
463 default-coding-system))
464 (let ((pos (point))
465 (fill-prefix " "))
466 (mapcar (function (lambda (x) (princ " ") (princ x)))
467 safe-coding-systems)
468 (fill-region-as-paragraph pos (point)))))
469
470 ;; Read a coding system.
471 (let* ((safe-names (mapcar (lambda (x) (list (symbol-name x)))
472 safe-coding-systems))
473 (name (completing-read
474 (format "Select coding system (default %s): "
475 (car safe-coding-systems))
476 safe-names nil t nil nil
477 (car (car safe-names)))))
478 (setq last-coding-system-specified (intern name))
479 (if (integerp (coding-system-eol-type default-coding-system))
480 (setq last-coding-system-specified
481 (coding-system-change-eol-conversion
482 last-coding-system-specified
483 (coding-system-eol-type default-coding-system))))
484 last-coding-system-specified))
485 (kill-buffer "*Warning*")
486 (while overlays
487 (delete-overlay (car overlays))
488 (setq overlays (cdr overlays)))))))))
54b226f7
KH
489
490(setq select-safe-coding-system-function 'select-safe-coding-system)
491
4ed46869
KH
492\f
493;;; Language support staffs.
494
4ed46869 495(defvar language-info-alist nil
2c395d56 496 "Alist of language environment definitions.
4ed46869
KH
497Each element looks like:
498 (LANGUAGE-NAME . ((KEY . INFO) ...))
2c395d56
RS
499where LANGUAGE-NAME is a string, the name of the language environment,
500KEY is a symbol denoting the kind of information, and
501INFO is the data associated with KEY.
502Meaningful values for KEY include
503
504 documentation value is documentation of what this language environment
505 is meant for, and how to use it.
506 charset value is a list of the character sets used by this
507 language environment.
508 sample-text value is one line of text,
509 written using those character sets,
510 appropriate for this language environment.
511 setup-function value is a function to call to switch to this
512 language environment.
513 exit-function value is a function to call to leave this
514 language environment.
515 coding-system value is a list of coding systems that are good
516 for saving text written in this language environment.
517 This list serves as suggestions to the user;
518 in effect, as a kind of documentation.
519 coding-priority value is a list of coding systems for this language
520 environment, in order of decreasing priority.
521 This is used to set up the coding system priority
45d08cb2 522 list when you switch to this language environment.
ddb5c041 523 nonascii-translation
7624ebb9 524 value is a translation table to be set in the
45d08cb2 525 variable `nonascii-translation-table' in this
7624ebb9
KH
526 language environment, or a character set from
527 which `nonascii-insert-offset' is calculated.
45d08cb2 528 charset-origin-alist
7624ebb9 529 value is an alist to be set in the variable
ddb5c041
KH
530 `charset-origin-alist' in this language environment.
531 input-method value is a default input method for this language
532 environment.
7624ebb9
KH
533 features value is a list of features requested in this
534 language environment.
ddb5c041
KH
535
536The following keys take effect only when multibyte characters are
537globally disabled, i.e. the value of `default-enable-multibyte-characters'
538is nil.
539
540 unibyte-syntax value is a library name to load to set
541 unibyte 8-bit charcater syntaxes for this
542 language environment.
543
544 unibyte-display value is a coding system to encode characters
545 for the terminal. Characters in the range
546 of 160 to 255 display not as octal escapes,
547 but as non-ASCII characters in this language
548 environment.")
2c395d56
RS
549
550(defun get-language-info (lang-env key)
551 "Return information listed under KEY for language environment LANG-ENV.
552KEY is a symbol denoting the kind of information.
553For a list of useful values for KEY and their meanings,
554see `language-info-alist'."
555 (if (symbolp lang-env)
556 (setq lang-env (symbol-name lang-env)))
557 (let ((lang-slot (assoc-ignore-case lang-env language-info-alist)))
4ed46869
KH
558 (if lang-slot
559 (cdr (assq key (cdr lang-slot))))))
560
f08adf27 561(defun set-language-info (lang-env key info)
2c395d56
RS
562 "Modify part of the definition of language environment LANG-ENV.
563Specifically, this stores the information INFO under KEY
564in the definition of this language environment.
4ed46869 565KEY is a symbol denoting the kind of information.
2c395d56 566INFO is the value for that information.
281d03ec 567
2c395d56 568For a list of useful values for KEY and their meanings,
f08adf27 569see `language-info-alist'."
2c395d56
RS
570 (if (symbolp lang-env)
571 (setq lang-env (symbol-name lang-env)))
4ed46869 572 (let (lang-slot key-slot)
2c395d56 573 (setq lang-slot (assoc lang-env language-info-alist))
4ed46869 574 (if (null lang-slot) ; If no slot for the language, add it.
2c395d56 575 (setq lang-slot (list lang-env)
4ed46869
KH
576 language-info-alist (cons lang-slot language-info-alist)))
577 (setq key-slot (assq key lang-slot))
578 (if (null key-slot) ; If no slot for the key, add it.
579 (progn
580 (setq key-slot (list key))
581 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
f08adf27 582 (setcdr key-slot info)))
4ed46869 583
2c395d56
RS
584(defun set-language-info-alist (lang-env alist &optional parents)
585 "Store ALIST as the definition of language environment LANG-ENV.
586ALIST is an alist of KEY and INFO values. See the documentation of
7624ebb9 587`set-language-info' for the meanings of KEY and INFO.
54b226f7 588
2c395d56
RS
589Optional arg PARENTS is a list of parent menu names; it specifies
590where to put this language environment in the
591Describe Language Environment and Set Language Environment menus.
592For example, (\"European\") means to put this language environment
593in the European submenu in each of those two menus."
594 (if (symbolp lang-env)
595 (setq lang-env (symbol-name lang-env)))
54b226f7
KH
596 (let ((describe-map describe-language-environment-map)
597 (setup-map setup-language-environment-map))
598 (if parents
599 (let ((l parents)
600 map parent-symbol parent)
601 (while l
602 (if (symbolp (setq parent-symbol (car l)))
603 (setq parent (symbol-name parent))
604 (setq parent parent-symbol parent-symbol (intern parent)))
605 (setq map (lookup-key describe-map (vector parent-symbol)))
606 (if (not map)
607 (progn
608 (setq map (intern (format "describe-%s-environment-map"
609 (downcase parent))))
610 (define-prefix-command map)
611 (define-key-after describe-map (vector parent-symbol)
612 (cons parent map) t)))
613 (setq describe-map (symbol-value map))
614 (setq map (lookup-key setup-map (vector parent-symbol)))
615 (if (not map)
616 (progn
617 (setq map (intern (format "setup-%s-environment-map"
618 (downcase parent))))
619 (define-prefix-command map)
620 (define-key-after setup-map (vector parent-symbol)
621 (cons parent map) t)))
622 (setq setup-map (symbol-value map))
623 (setq l (cdr l)))))
f08adf27
RS
624
625 ;; Set up menu items for this language env.
7624ebb9 626 (let ((doc (assq 'documentation alist)))
f08adf27
RS
627 (when doc
628 (define-key-after describe-map (vector (intern lang-env))
7624ebb9
KH
629 (cons lang-env 'describe-specified-language-support) t)))
630 (define-key-after setup-map (vector (intern lang-env))
631 (cons lang-env 'setup-specified-language-environment) t)
f08adf27 632
54b226f7 633 (while alist
f08adf27 634 (set-language-info lang-env (car (car alist)) (cdr (car alist)))
54b226f7 635 (setq alist (cdr alist)))))
4ed46869 636
ae302641 637(defun read-language-name (key prompt &optional default)
2c395d56 638 "Read a language environment name which has information for KEY.
ddb5c041 639If KEY is nil, read any language environment.
2c395d56
RS
640Prompt with PROMPT. DEFAULT is the default choice of language environment.
641This returns a language environment name as a string."
4ed46869
KH
642 (let* ((completion-ignore-case t)
643 (name (completing-read prompt
644 language-info-alist
ddb5c041
KH
645 (and key
646 (function (lambda (elm) (assq key elm))))
ae302641 647 t nil nil default)))
13e82c04 648 (if (and (> (length name) 0)
ddb5c041
KH
649 (or (not key)
650 (get-language-info name key)))
13e82c04 651 name)))
4ed46869
KH
652\f
653;;; Multilingual input methods.
654
d0b9c3ab
KH
655(defconst leim-list-file-name "leim-list.el"
656 "Name of LEIM list file.
657This file contains a list of libraries of Emacs input methods (LEIM)
658in the format of Lisp expression for registering each input method.
659Emacs loads this file at startup time.")
660
661(defvar leim-list-header (format "\
662;;; %s -- list of LEIM (Library of Emacs Input Method)
663;;
664;; This file contains a list of LEIM (Library of Emacs Input Method)
665;; in the same directory as this file. Loading this file registeres
666;; the whole input methods in Emacs.
667;;
d33d5fbe 668;; Each entry has the form:
d0b9c3ab
KH
669;; (register-input-method
670;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
671;; TITLE DESCRIPTION
672;; ARG ...)
673;; See the function `register-input-method' for the meanings of arguments.
674;;
675;; If this directory is included in load-path, Emacs automatically
676;; loads this file at startup time.
677
678"
679 leim-list-file-name)
680 "Header to be inserted in LEIM list file.")
681
e55e92ee 682(defvar leim-list-entry-regexp "^(register-input-method"
d0b9c3ab
KH
683 "Regexp matching head of each entry in LEIM list file.
684See also the variable `leim-list-header'")
685
686(defvar update-leim-list-functions
687 '(quail-update-leim-list-file)
688 "List of functions to call to update LEIM list file.
689Each function is called with one arg, LEIM directory name.")
690
a337fe7f
RS
691(defun update-leim-list-file (&rest dirs)
692 "Update LEIM list file in directories DIRS."
d0b9c3ab
KH
693 (let ((functions update-leim-list-functions))
694 (while functions
a337fe7f 695 (apply (car functions) dirs)
d0b9c3ab
KH
696 (setq functions (cdr functions)))))
697
4ed46869
KH
698(defvar current-input-method nil
699 "The current input method for multilingual text.
96db204a 700If nil, that means no input method is activated now.")
4ed46869
KH
701(make-variable-buffer-local 'current-input-method)
702(put 'current-input-method 'permanent-local t)
703
704(defvar current-input-method-title nil
d0b9c3ab 705 "Title string of the current input method shown in mode line.")
4ed46869
KH
706(make-variable-buffer-local 'current-input-method-title)
707(put 'current-input-method-title 'permanent-local t)
708
b4fba33f 709(defcustom default-input-method nil
8861c593 710 "*Default input method for multilingual text (a string).
b4fba33f 711This is the input method activated automatically by the command
9b10b5a3 712`toggle-input-method' (\\[toggle-input-method])."
8861c593
RS
713 :group 'mule
714 :type 'string)
b4fba33f 715
723a427a
KH
716(defvar input-method-history nil
717 "History list for some commands that read input methods.")
718(make-variable-buffer-local 'input-method-history)
719(put 'input-method-history 'permanent-local t)
4ed46869
KH
720
721(defvar inactivate-current-input-method-function nil
722 "Function to call for inactivating the current input method.
723Every input method should set this to an appropriate value when activated.
f17ccaee
KH
724This function is called with no argument.
725
726This function should never change the value of `current-input-method'.
727It is set to nil by the function `inactivate-input-method'.")
4ed46869
KH
728(make-variable-buffer-local 'inactivate-current-input-method-function)
729(put 'inactivate-current-input-method-function 'permanent-local t)
730
731(defvar describe-current-input-method-function nil
732 "Function to call for describing the current input method.
733This function is called with no argument.")
734(make-variable-buffer-local 'describe-current-input-method-function)
735(put 'describe-current-input-method-function 'permanent-local t)
736
d0b9c3ab 737(defvar input-method-alist nil
2c395d56 738 "Alist of input method names vs how to use them.
d0b9c3ab 739Each element has the form:
2c395d56
RS
740 (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
741See the function `register-input-method' for the meanings of the elements.")
742
f08adf27 743(defun register-input-method (input-method lang-env &rest args)
2c395d56 744 "Register INPUT-METHOD as an input method for language environment ENV.
f08adf27 745INPUT-METHOD and LANG-ENV are symbols or strings.
d0b9c3ab 746
d0b9c3ab 747The remaining arguments are:
2c395d56
RS
748 ACTIVATE-FUNC, TITLE, DESCRIPTION, and ARGS...
749ACTIVATE-FUNC is a function to call to activate this method.
750TITLE is a string to show in the mode line when this method is active.
751DESCRIPTION is a string describing this method and what it is good for.
752The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
753All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS."
f08adf27
RS
754 (if (symbolp lang-env)
755 (setq lang-env (symbol-name lang-env)))
4ef06f75
KH
756 (if (symbolp input-method)
757 (setq input-method (symbol-name input-method)))
f08adf27 758 (let ((info (cons lang-env args))
d0b9c3ab
KH
759 (slot (assoc input-method input-method-alist)))
760 (if slot
761 (setcdr slot info)
762 (setq slot (cons input-method info))
763 (setq input-method-alist (cons slot input-method-alist)))))
764
4d5ac029 765(defun read-input-method-name (prompt &optional default inhibit-null)
d0b9c3ab 766 "Read a name of input method from a minibuffer prompting with PROMPT.
4d5ac029
RS
767If DEFAULT is non-nil, use that as the default,
768 and substitute it into PROMPT at the first `%s'.
4ef06f75
KH
769If INHIBIT-NULL is non-nil, null input signals an error.
770
771The return value is a string."
4d5ac029
RS
772 (if default
773 (setq prompt (format prompt default)))
d0b9c3ab 774 (let* ((completion-ignore-case t)
723a427a
KH
775 ;; This binding is necessary because input-method-history is
776 ;; buffer local.
d0b9c3ab 777 (input-method (completing-read prompt input-method-alist
87505a98
RS
778 nil t nil 'input-method-history
779 default)))
d0b9c3ab
KH
780 (if (> (length input-method) 0)
781 input-method
782 (if inhibit-null
43807b77 783 (error "No valid input method is specified")))))
d0b9c3ab 784
d0b9c3ab 785(defun activate-input-method (input-method)
2c395d56
RS
786 "Switch to input method INPUT-METHOD for the current buffer.
787If some other input method is already active, turn it off first.
788If INPUT-METHOD is nil, deactivate any current input method."
305a3cb6 789 (if (and input-method (symbolp input-method))
4ef06f75 790 (setq input-method (symbol-name input-method)))
723a427a
KH
791 (if (and current-input-method
792 (not (string= current-input-method input-method)))
305a3cb6 793 (inactivate-input-method))
2c395d56 794 (unless (or current-input-method (null input-method))
d0b9c3ab
KH
795 (let ((slot (assoc input-method input-method-alist)))
796 (if (null slot)
723a427a 797 (error "Can't activate input method `%s'" input-method))
8efc03e1
KH
798 (let ((func (nth 2 slot)))
799 (if (functionp func)
800 (apply (nth 2 slot) input-method (nthcdr 5 slot))
801 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
802 (progn
803 (require (cdr func))
804 (apply (car func) input-method (nthcdr 5 slot)))
805 (error "Can't activate input method `%s'" input-method))))
d0b9c3ab 806 (setq current-input-method input-method)
723a427a 807 (setq current-input-method-title (nth 3 slot))
28885c0e
KH
808 (unwind-protect
809 (run-hooks 'input-method-activate-hook)
810 (force-mode-line-update)))))
15b3e511 811
15b3e511 812(defun inactivate-input-method ()
f17ccaee 813 "Turn off the current input method."
723a427a
KH
814 (when current-input-method
815 (if input-method-history
816 (unless (string= current-input-method (car input-method-history))
817 (setq input-method-history
818 (cons current-input-method
819 (delete current-input-method input-method-history))))
820 (setq input-method-history (list current-input-method)))
821 (unwind-protect
822 (funcall inactivate-current-input-method-function)
15b3e511 823 (unwind-protect
723a427a
KH
824 (run-hooks 'input-method-inactivate-hook)
825 (setq current-input-method nil
28885c0e
KH
826 current-input-method-title nil)
827 (force-mode-line-update)))))
4ed46869 828
8b784951 829(defun set-input-method (input-method)
2c395d56
RS
830 "Select and activate input method INPUT-METHOD for the current buffer.
831This also sets the default input method to the one you specify."
d0b9c3ab 832 (interactive
723a427a 833 (let* ((default (or (car input-method-history) default-input-method)))
42395763 834 (list (read-input-method-name
87505a98 835 (if default "Select input method (default %s): " "Select input method: ")
42395763 836 default t))))
d0b9c3ab 837 (activate-input-method input-method)
42395763 838 (setq default-input-method input-method))
4ed46869
KH
839
840(defun toggle-input-method (&optional arg)
15b3e511 841 "Turn on or off a multilingual text input method for the current buffer.
723a427a 842
7ddbb5bc
RS
843With no prefix argument, if some input method is currently activated,
844turn it off. Otherwise, activate an input method--the one most recently used,
845or the one specified in `default-input-method', or one read from the
846minibuffer.
723a427a 847
7ddbb5bc
RS
848With a prefix arg, read an input method from minibuffer and turn it on.
849The default is the most recent input method specified
850\(not including the currently active input method, if any).
723a427a
KH
851
852When there's no input method to turn on, turn on what read from minibuffer."
4ed46869 853 (interactive "P")
7ddbb5bc
RS
854 (if (and current-input-method (not arg))
855 (inactivate-input-method)
856 (let ((default (or (car input-method-history) default-input-method)))
857 (if (and arg default (equal current-input-method default)
858 (> (length input-method-history) 1))
859 (setq default (nth 1 input-method-history)))
723a427a
KH
860 (activate-input-method
861 (if (or arg (not default))
7ddbb5bc
RS
862 (progn
863 (read-input-method-name
864 (if default "Input method (default %s): " "Input method: " )
865 default t))
723a427a
KH
866 default))
867 (or default-input-method
868 (setq default-input-method current-input-method)))))
d0b9c3ab
KH
869
870(defun describe-input-method (input-method)
2c395d56 871 "Describe input method INPUT-METHOD."
d0b9c3ab
KH
872 (interactive
873 (list (read-input-method-name
874 "Describe input method (default, current choice): ")))
78754934 875 (if (and input-method (symbolp input-method))
4ef06f75 876 (setq input-method (symbol-name input-method)))
d0b9c3ab
KH
877 (if (null input-method)
878 (describe-current-input-method)
879 (with-output-to-temp-buffer "*Help*"
880 (let ((elt (assoc input-method input-method-alist)))
881 (princ (format "Input method: %s (`%s' in mode line) for %s\n %s\n"
882 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt)))))))
883
884(defun describe-current-input-method ()
96db204a 885 "Describe the input method currently in use."
4ed46869
KH
886 (if current-input-method
887 (if (and (symbolp describe-current-input-method-function)
888 (fboundp describe-current-input-method-function))
889 (funcall describe-current-input-method-function)
890 (message "No way to describe the current input method `%s'"
891 (cdr current-input-method))
892 (ding))
d0b9c3ab 893 (error "No input method is activated now")))
4ed46869 894
d3459641 895(defun read-multilingual-string (prompt &optional initial-input input-method)
4ed46869
KH
896 "Read a multilingual string from minibuffer, prompting with string PROMPT.
897The input method selected last time is activated in minibuffer.
15b3e511 898If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
d0b9c3ab
KH
899initially.
900Optional 3rd argument INPUT-METHOD specifies the input method
4ef06f75
KH
901to be activated instead of the one selected last time. It is a symbol
902or a string."
88d559ec
KH
903 (setq input-method
904 (or input-method
d3459641 905 current-input-method
88d559ec
KH
906 default-input-method
907 (read-input-method-name "Input method: " nil t)))
3df60841 908 (if (and input-method (symbolp input-method))
4ef06f75 909 (setq input-method (symbol-name input-method)))
305a3cb6
KH
910 (let ((prev-input-method current-input-method))
911 (unwind-protect
912 (progn
913 (activate-input-method input-method)
914 (read-string prompt initial-input nil nil t))
915 (activate-input-method prev-input-method))))
4ed46869
KH
916
917;; Variables to control behavior of input methods. All input methods
918;; should react to these variables.
919
8efc03e1
KH
920(defcustom input-method-verbose-flag 'default
921 "*A flag to control extra guidance given by input methods.
922The value should be nil, t, `complex-only', or `default'.
4ed46869 923
cb29dfb6 924The extra guidance is done by showing list of available keys in echo
8efc03e1
KH
925area. When you use the input method in the minibuffer, the guidance
926is shown at the bottom short window (split from the existing window).
c27c4ed8 927
8efc03e1
KH
928If the value is t, extra guidance is always given, if the value is
929nil, extra guidance is always suppressed.
930
931If the value is `complex-only', only complex input methods such as
932`chinese-py' and `japanese' give extra guidance.
933
934If the value is `default', complex input methods always give extra
935guidance, but simple input methods give it only when you are not in
936the minibuffer.
937
938See also the variable `input-method-highlight-flag'."
939 :type '(choice (const t) (const nil) (const complex-only) (const default))
42395763
RS
940 :group 'mule)
941
942(defcustom input-method-highlight-flag t
943 "*If this flag is non-nil, input methods highlight partially-entered text.
944For instance, while you are in the middle of a Quail input method sequence,
945the text inserted so far is temporarily underlined.
8efc03e1
KH
946The underlining goes away when you finish or abort the input method sequence.
947See also the variable `input-method-verbose-flag'."
42395763
RS
948 :type 'boolean
949 :group 'mule)
4ed46869
KH
950
951(defvar input-method-activate-hook nil
f17ccaee
KH
952 "Normal hook run just after an input method is activated.
953
954The variable `current-input-method' keeps the input method name
955just activated.")
4ed46869
KH
956
957(defvar input-method-inactivate-hook nil
f17ccaee
KH
958 "Normal hook run just after an input method is inactivated.
959
960The variable `current-input-method' still keeps the input method name
4d0e6a11 961just inactivated.")
4ed46869
KH
962
963(defvar input-method-after-insert-chunk-hook nil
964 "Normal hook run just after an input method insert some chunk of text.")
965
dccca980
KH
966(defvar input-method-exit-on-first-char nil
967 "This flag controls a timing when an input method returns.
968Usually, the input method does not return while there's a possibility
969that it may find a different translation if a user types another key.
39e643e2
RS
970But, it this flag is non-nil, the input method returns as soon as
971the current key sequence gets long enough to have some valid translation.")
dccca980
KH
972
973(defvar input-method-use-echo-area nil
974 "This flag controls how an input method shows an intermediate key sequence.
39e643e2
RS
975Usually, the input method inserts the intermediate key sequence,
976or candidate translations corresponding to the sequence,
977at point in the current buffer.
978But, if this flag is non-nil, it displays them in echo area instead.")
dccca980 979
723a427a
KH
980(defvar input-method-exit-on-invalid-key nil
981 "This flag controls the behaviour of an input method on invalid key input.
982Usually, when a user types a key which doesn't start any character
983handled by the input method, the key is handled by turning off the
39e643e2 984input method temporarily. After that key, the input method is renabled.
723a427a
KH
985But, if this flag is non-nil, the input method is never back on.")
986
4ed46869 987\f
8efc03e1
KH
988(defvar set-language-environment-hook nil
989 "Normal hook run after some language environment is set.
990
991When you set some hook function here, that effect usually should not
992be inherited to another language environment. So, you had better set
993another function in `exit-language-environment-hook' (which see) to
994cancel the effect.")
995
996(defvar exit-language-environment-hook nil
997 "Normal hook run after exiting from some language environment.
998When this hook is run, the variable `current-language-environment'
999is still bound to the language environment being exited.
1000
1001This hook is mainly used for cancelling the effect of
1002`set-language-environment-hook' (which-see).")
1003
15b3e511 1004(defun setup-specified-language-environment ()
f08adf27 1005 "Switch to a specified language environment."
15b3e511 1006 (interactive)
f850d782 1007 (let (language-name)
15b3e511
KH
1008 (if (and (symbolp last-command-event)
1009 (or (not (eq last-command-event 'Default))
1010 (setq last-command-event 'English))
f850d782
RS
1011 (setq language-name (symbol-name last-command-event)))
1012 (set-language-environment language-name)
15b3e511 1013 (error "Bogus calling sequence"))))
4ed46869 1014
8861c593
RS
1015(defcustom current-language-environment "English"
1016 "The last language environment specified with `set-language-environment'."
1017 :set 'set-language-environment
1018 :initialize 'custom-initialize-default
1019 :group 'mule
1020 :type 'string)
f850d782 1021
ddb5c041
KH
1022(defun reset-language-environment ()
1023 "Reset multilingual environment of Emacs to the default status.
1024
1025The default status is as follows:
1026
1027 The default value of buffer-file-coding-system is nil.
1028 The default coding system for process I/O is nil.
1029 The default value for the command `set-terminal-coding-system' is nil.
1030 The default value for the command `set-keyboard-coding-system' is nil.
1031
1032 The order of priorities of coding categories and the coding system
1033 bound to each category are as follows
1034 coding category coding system
1035 --------------------------------------------------
1036 coding-category-iso-8-2 iso-latin-1
1037 coding-category-iso-8-1 iso-latin-1
1038 coding-category-iso-7-tight iso-2022-jp
1039 coding-category-iso-7 iso-2022-7bit
1040 coding-category-iso-7-else iso-2022-7bit-lock
1041 coding-category-iso-8-else iso-2022-8bit-ss2
1042 coding-category-emacs-mule emacs-mule
1043 coding-category-raw-text raw-text
1044 coding-category-sjis japanese-shift-jis
1045 coding-category-big5 chinese-big5
1046 coding-category-ccl nil
1047 coding-category-binarry no-conversion
1048"
1049 (interactive)
1050 ;; This function formerly set default-enable-multibyte-characters to t,
1051 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1052
1053 (setq coding-category-iso-7-tight 'iso-2022-jp
1054 coding-category-iso-7 'iso-2022-7bit
1055 coding-category-iso-8-1 'iso-latin-1
1056 coding-category-iso-8-2 'iso-latin-1
1057 coding-category-iso-7-else 'iso-2022-7bit-lock
1058 coding-category-iso-8-else 'iso-2022-8bit-ss2
1059 coding-category-emacs-mule 'emacs-mule
1060 coding-category-raw-text 'raw-text
1061 coding-category-sjis 'japanese-shift-jis
1062 coding-category-big5 'chinese-big5
1063 coding-category-ccl nil
1064 coding-category-binary 'no-conversion)
1065
1066 (set-coding-priority
1067 '(coding-category-iso-8-1
1068 coding-category-iso-8-2
1069 coding-category-iso-7-tight
1070 coding-category-iso-7
1071 coding-category-iso-7-else
1072 coding-category-iso-8-else
1073 coding-category-emacs-mule
1074 coding-category-raw-text
1075 coding-category-sjis
1076 coding-category-big5
1077 coding-category-ccl
1078 coding-category-binary))
1079
1080 (set-default-coding-systems nil)
1081 ;; Don't alter the terminal and keyboard coding systems here.
1082 ;; The terminal still supports the same coding system
1083 ;; that it supported a minute ago.
1084;;; (set-terminal-coding-system-internal nil)
1085;;; (set-keyboard-coding-system-internal nil)
1086
1087 (setq nonascii-translation-table nil
1088 nonascii-insert-offset 0))
1089
166246f7 1090(defun set-language-environment (language-name)
6c05d680
RS
1091 "Set up multi-lingual environment for using LANGUAGE-NAME.
1092This sets the coding system priority and the default input method
8861c593
RS
1093and sometimes other things. LANGUAGE-NAME should be a string
1094which is the name of a language environment. For example, \"Latin-1\"
1095specifies the character set for the major languages of Western Europe."
8efc03e1 1096 (interactive (list (read-language-name
ddb5c041 1097 nil
8efc03e1 1098 "Set language environment (default, English): ")))
4ef06f75
KH
1099 (if language-name
1100 (if (symbolp language-name)
1101 (setq language-name (symbol-name language-name)))
1102 (setq language-name "English"))
ddb5c041 1103 (or (assoc-ignore-case language-name language-info-alist)
f850d782 1104 (error "Language environment not defined: %S" language-name))
8efc03e1
KH
1105 (if current-language-environment
1106 (let ((func (get-language-info current-language-environment
1107 'exit-function)))
e63645c2
KH
1108 (run-hooks 'exit-language-environment-hook)
1109 (if (fboundp func) (funcall func))))
ddb5c041
KH
1110 (reset-language-environment)
1111
f850d782 1112 (setq current-language-environment language-name)
ddb5c041
KH
1113 (set-language-environment-coding-systems language-name)
1114 (let ((input-method (get-language-info language-name 'input-method)))
1115 (when input-method
1116 (setq default-input-method input-method)
1117 (if input-method-history
1118 (setq input-method-history
1119 (cons input-method
1120 (delete input-method input-method-history))))))
1121 (let ((nonascii (get-language-info language-name 'nonascii-translation)))
1122 (if (char-table-p nonascii)
1123 (setq nonascii-translation-table nonascii)
1124 (if (charsetp nonascii)
1125 (setq nonascii-insert-offset (- (make-char nonascii) 128)))))
1126
45d08cb2
KH
1127 (setq charset-origin-alist
1128 (get-language-info language-name 'charset-origin-alist))
ddb5c041 1129
63283a8f 1130 ;; Unibyte setups if necessary.
ddb5c041 1131 (unless default-enable-multibyte-characters
63283a8f 1132 ;; Syntax and case table.
ddb5c041
KH
1133 (let ((syntax (get-language-info language-name 'unibyte-syntax)))
1134 (if syntax
1135 (let ((set-case-syntax-set-multibyte nil))
63283a8f
KH
1136 (load syntax nil t))
1137 ;; No information for syntax and case. Reset to the defaults.
1138 (let ((syntax-table (standard-syntax-table))
1139 (case-table (standard-case-table))
1140 (ch 160))
1141 (while (< ch 256)
1142 (modify-syntax-entry ch " " syntax-table)
1143 (aset case-table ch ch)
1144 (setq ch (1+ ch)))
1145 (set-char-table-extra-slot case-table 0 nil)
1146 (set-char-table-extra-slot case-table 1 nil)
1147 (set-char-table-extra-slot case-table 2 nil))
1148 (set-standard-case-table (standard-case-table))
1149 (let ((list (buffer-list)))
1150 (while list
1151 (with-current-buffer (car list)
1152 (set-case-table (standard-case-table)))
1153 (setq list (cdr list))))))
1154 ;; Display table and coding system for terminal.
ddb5c041
KH
1155 (let ((coding (get-language-info language-name 'unibyte-display)))
1156 (if coding
1157 (progn
1158 (standard-display-european-internal)
1159 (set-terminal-coding-system coding))
1160 (standard-display-default 160 255)
1161 (aset standard-display-table 146 nil)
1162 (set-terminal-coding-system nil))))
1163
1164 (let ((required-features (get-language-info language-name 'features)))
1165 (while required-features
1166 (require (car required-features))
1167 (setq required-features (cdr required-features))))
1168 (let ((func (get-language-info language-name 'setup-function)))
1169 (if (fboundp func)
1170 (funcall func)))
8efc03e1 1171 (run-hooks 'set-language-environment-hook)
f850d782 1172 (force-mode-line-update t))
4ed46869 1173
51a8fc1d
RS
1174(defun standard-display-european-internal ()
1175 ;; Actually set up direct output of non-ASCII characters.
1176 (standard-display-8bit 160 255)
1177 ;; Make non-line-break space display as a plain space.
1178 ;; Most X fonts do the wrong thing for code 160.
1179 (aset standard-display-table 160 [32])
1180 ;; Most Windows programs send out apostrophe's as \222. Most X fonts
1181 ;; don't contain a character at that position. Map it to the ASCII
1182 ;; apostrophe.
1183 (aset standard-display-table 146 [39]))
1184
54b226f7
KH
1185(defun set-language-environment-coding-systems (language-name)
1186 "Do various coding system setups for language environment LANGUAGE-NAME."
1187 (let* ((priority (get-language-info language-name 'coding-priority))
1188 (default-coding (car priority)))
1189 (if priority
1190 (let ((categories (mapcar 'coding-system-category priority)))
1191 (set-default-coding-systems default-coding)
1192 (set-coding-priority categories)
1193 (while priority
1194 (set (car categories) (car priority))
1195 (setq priority (cdr priority) categories (cdr categories)))
45d08cb2 1196 (update-coding-systems-internal)))))
54b226f7 1197
4ed46869
KH
1198;; Print all arguments with `princ', then print "\n".
1199(defsubst princ-list (&rest args)
1200 (while args (princ (car args)) (setq args (cdr args)))
1201 (princ "\n"))
1202
48082651 1203;; Print a language specific information such as input methods,
13e82c04 1204;; charsets, and coding systems. This function is intended to be
48082651 1205;; called from the menu:
281d03ec 1206;; [menu-bar mule describe-language-environment LANGUAGE]
48082651
KH
1207;; and should not run it by `M-x describe-current-input-method-function'.
1208(defun describe-specified-language-support ()
96db204a 1209 "Describe how Emacs supports the specified language environment."
48082651 1210 (interactive)
281d03ec 1211 (let (language-name)
48082651 1212 (if (not (and (symbolp last-command-event)
281d03ec 1213 (setq language-name (symbol-name last-command-event))))
48082651 1214 (error "Bogus calling sequence"))
281d03ec
RS
1215 (describe-language-environment language-name)))
1216
1217(defun describe-language-environment (language-name)
1218 "Describe how Emacs supports language environment LANGUAGE-NAME."
78754934
KH
1219 (interactive
1220 (list (read-language-name
1221 'documentation
8adfa8be 1222 "Describe language environment (default, current choice): ")))
f850d782
RS
1223 (if (null language-name)
1224 (setq language-name current-language-environment))
281d03ec
RS
1225 (if (or (null language-name)
1226 (null (get-language-info language-name 'documentation)))
1227 (error "No documentation for the specified language"))
4ef06f75
KH
1228 (if (symbolp language-name)
1229 (setq language-name (symbol-name language-name)))
281d03ec 1230 (let ((doc (get-language-info language-name 'documentation)))
48082651 1231 (with-output-to-temp-buffer "*Help*"
a33c9d6f 1232 (princ-list language-name " language environment" "\n")
13e82c04 1233 (if (stringp doc)
d0b9c3ab
KH
1234 (progn
1235 (princ-list doc)
1236 (terpri)))
15b3e511
KH
1237 (let ((str (get-language-info language-name 'sample-text)))
1238 (if (stringp str)
1239 (progn
281d03ec 1240 (princ "Sample text:\n")
d0b9c3ab
KH
1241 (princ-list " " str)
1242 (terpri))))
ddb5c041
KH
1243 (let ((input-method (get-language-info language-name 'input-method))
1244 (l input-method-alist))
1245 (princ "Input methods")
1246 (when input-method
1247 (princ (format " (default, %s)" input-method))
1248 (setq input-method (assoc input-method input-method-alist))
1249 (setq l (cons input-method (delete input-method l))))
1250 (princ ":\n")
15b3e511 1251 (while l
d0b9c3ab
KH
1252 (if (string= language-name (nth 1 (car l)))
1253 (princ-list " " (car (car l))
1254 (format " (`%s' in mode line)" (nth 3 (car l)))))
15b3e511 1255 (setq l (cdr l))))
281d03ec
RS
1256 (terpri)
1257 (princ "Character sets:\n")
15b3e511
KH
1258 (let ((l (get-language-info language-name 'charset)))
1259 (if (null l)
1260 (princ-list " nothing specific to " language-name)
1261 (while l
1262 (princ-list " " (car l) ": "
1263 (charset-description (car l)))
1264 (setq l (cdr l)))))
281d03ec
RS
1265 (terpri)
1266 (princ "Coding systems:\n")
15b3e511
KH
1267 (let ((l (get-language-info language-name 'coding-system)))
1268 (if (null l)
1269 (princ-list " nothing specific to " language-name)
48082651 1270 (while l
281d03ec
RS
1271 (princ (format " %s (`%c' in mode line):\n\t%s\n"
1272 (car l)
1273 (coding-system-mnemonic (car l))
a904b20b 1274 (coding-system-doc-string (car l))))
8efc03e1
KH
1275 (let ((aliases (coding-system-get (car l) 'alias-coding-systems)))
1276 (when aliases
1277 (princ "\t")
1278 (princ (cons 'alias: (cdr aliases)))
1279 (terpri)))
15b3e511 1280 (setq l (cdr l))))))))
4ed46869
KH
1281\f
1282;;; Charset property
1283
1284(defsubst get-charset-property (charset propname)
1285 "Return the value of CHARSET's PROPNAME property.
1286This is the last value stored with
96db204a 1287 (put-charset-property CHARSET PROPNAME VALUE)."
4ed46869
KH
1288 (plist-get (charset-plist charset) propname))
1289
1290(defsubst put-charset-property (charset propname value)
1291 "Store CHARSETS's PROPNAME property with value VALUE.
1292It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
1293 (set-charset-plist charset
1294 (plist-put (charset-plist charset) propname value)))
1295
1296;;; Character code property
1297(put 'char-code-property-table 'char-table-extra-slots 0)
1298
1299(defvar char-code-property-table
1300 (make-char-table 'char-code-property-table)
1301 "Char-table containing a property list of each character code.
1302
1303See also the documentation of `get-char-code-property' and
96db204a 1304`put-char-code-property'.")
4ed46869
KH
1305
1306(defun get-char-code-property (char propname)
1307 "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
1308 (let ((plist (aref char-code-property-table char)))
1309 (if (listp plist)
1310 (car (cdr (memq propname plist))))))
1311
1312(defun put-char-code-property (char propname value)
1313 "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
1314It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
1315 (let ((plist (aref char-code-property-table char)))
1316 (if plist
1317 (let ((slot (memq propname plist)))
1318 (if slot
1319 (setcar (cdr slot) value)
1320 (nconc plist (list propname value))))
1321 (aset char-code-property-table char (list propname value)))))
1322
1323;;; mule-cmds.el ends here