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