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