Merge r21-4-11-chise-0_20-=ucs.
[chise/xemacs-chise.git.1] / 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 ;; Note: Some of the code here is now in code-cmds.el
27
28 ;;; Code:
29
30 ;;; MULE related key bindings and menus.
31
32 (require 'code-cmds)
33
34 ;; Preserve the old name
35 (defvaralias 'mule-keymap 'coding-keymap)
36
37 (define-key mule-keymap "x" 'set-selection-coding-system)
38 (define-key mule-keymap "X" 'set-next-selection-coding-system)
39 (define-key mule-keymap "\C-\\" 'set-input-method)
40 ;;(define-key mule-keymap "c" 'list-coding-system-briefly) ; XEmacs
41 (define-key mule-keymap "C" 'describe-coding-system)     ; XEmacs
42 (define-key mule-keymap "r" 'toggle-display-direction)   ; XEmacs
43 (define-key mule-keymap "l" 'set-language-environment)
44
45 (define-key help-map "L" 'describe-language-environment)
46 (define-key help-map "\C-\\" 'describe-input-method)
47 (define-key help-map "I" 'describe-input-method)
48 (define-key help-map "h" 'view-hello-file)
49
50 ;; Menu for XEmacs were moved to menubar-items.el.
51
52
53 ;; This should be a single character key binding because users use it
54 ;; very frequently while editing multilingual text.  Now we can use
55 ;; only two such keys: "\C-\\" and "\C-^", but the latter is not
56 ;; convenient because it requires shifting on most keyboards.  An
57 ;; alternative is "\C-\]" which is now bound to `abort-recursive-edit'
58 ;; but it won't be used that frequently.
59 (define-key global-map "\C-\\" 'toggle-input-method)
60
61 ;;; This is no good because people often type Shift-SPC
62 ;;; meaning to type SPC.  -- rms.
63 ;;; ;; Here's an alternative key binding for X users (Shift-SPACE).
64 ;;; (define-key global-map [?\S- ] 'toggle-input-method)
65
66 (defun coding-system-change-eol-conversion (coding-system eol-type)
67   "Return a coding system which differs from CODING-SYSTEM in eol conversion.
68 The returned coding system converts end-of-line by EOL-TYPE
69 but text as the same way as CODING-SYSTEM.
70 EOL-TYPE should be `lf', `crlf', `cr' or nil.
71 If EOL-TYPE is nil, the returned coding system detects
72 how end-of-line is formatted automatically while decoding.
73
74 EOL-TYPE can be specified by an symbol `unix', `dos' or `mac'.
75 They means `lf', `crlf', and `cr' respectively."
76   (if (symbolp eol-type)
77       (setq eol-type (cond ((or (eq eol-type 'unix)
78                                 (eq eol-type 'lf))
79                             'eol-lf)
80                            ((or (eq eol-type 'dos)
81                                 (eq eol-type 'crlf))
82                             'eol-crlf)
83                            ((or (eq eol-type 'mac)
84                                 (eq eol-type 'cr))
85                             'eol-cr)
86                            (t eol-type))))
87   (let ((orig-eol-type (coding-system-eol-type coding-system)))
88     (if (null orig-eol-type)
89         (if (not eol-type)
90             coding-system
91           (coding-system-property coding-system eol-type))
92       (let ((base (coding-system-base coding-system)))
93         (if (not eol-type)
94             base
95           (if (eq eol-type orig-eol-type)
96               coding-system
97             (setq orig-eol-type (coding-system-eol-type base))
98             (if (null orig-eol-type)
99                 (coding-system-property base eol-type))))))))
100
101 ;; (defun coding-system-change-text-conversion (coding-system coding)
102 ;;   "Return a coding system which differs from CODING-SYSTEM in text conversion.
103 ;; The returned coding system converts text by CODING
104 ;; but end-of-line as the same way as CODING-SYSTEM.
105 ;; If CODING is nil, the returned coding system detects
106 ;; how text is formatted automatically while decoding."
107 ;;   (if (not coding)
108 ;;       (coding-system-base coding-system)
109 ;;     (let ((eol-type (coding-system-eol-type coding-system)))
110 ;;       (coding-system-change-eol-conversion
111 ;;        coding
112 ;;        (if (numberp eol-type) (aref [unix dos mac] eol-type))))))
113
114 (defun view-hello-file ()
115   "Display the HELLO file which list up many languages and characters."
116   (interactive)
117   ;; We have to decode the file in any environment.
118   (let ((coding-system-for-read 'iso-2022-7bit))
119     (find-file-read-only (expand-file-name "HELLO" data-directory))))
120
121 \f
122 ;;; Language support stuff.
123
124 (defvar language-info-alist nil
125   "Alist of language environment definitions.
126 Each element looks like:
127         (LANGUAGE-NAME . ((KEY . INFO) ...))
128 where LANGUAGE-NAME is a string, the name of the language environment,
129 KEY is a symbol denoting the kind of information, and
130 INFO is the data associated with KEY.
131 Meaningful values for KEY include
132
133   documentation      value is documentation of what this language environment
134                         is meant for, and how to use it.
135   charset            value is a list of the character sets used by this
136                         language environment.
137   sample-text        value is one line of text,
138                         written using those character sets,
139                         appropriate for this language environment.
140   setup-function     value is a function to call to switch to this
141                         language environment.
142   exit-function      value is a function to call to leave this
143                         language environment.
144   coding-system      value is a list of coding systems that are good
145                         for saving text written in this language environment.
146                         This list serves as suggestions to the user;
147                         in effect, as a kind of documentation.
148   coding-priority    value is a list of coding systems for this language
149                         environment, in order of decreasing priority.
150                         This is used to set up the coding system priority
151                         list when you switch to this language environment.
152   input-method       value is a default input method for this language
153                         environment.
154   features           value is a list of features requested in this
155                         language environment.
156   tutorial           value is a tutorial file name written in the language.")
157
158 (defun get-language-info (lang-env key)
159   "Return information listed under KEY for language environment LANG-ENV.
160 KEY is a symbol denoting the kind of information.
161 For a list of useful values for KEY and their meanings,
162 see `language-info-alist'."
163   (if (symbolp lang-env)
164       (setq lang-env (symbol-name lang-env)))
165   (let ((lang-slot (assoc-ignore-case lang-env language-info-alist)))
166     (if lang-slot
167         (cdr (assq key (cdr lang-slot))))))
168
169 (defun set-language-info (lang-env key info)
170   "Modify part of the definition of language environment LANG-ENV.
171 Specifically, this stores the information INFO under KEY
172 in the definition of this language environment.
173 KEY is a symbol denoting the kind of information.
174 INFO is the value for that information.
175
176 For a list of useful values for KEY and their meanings,
177 see `language-info-alist'."
178   (if (symbolp lang-env)
179       (setq lang-env (symbol-name lang-env)))
180   (let (lang-slot key-slot)
181     (setq lang-slot (assoc lang-env language-info-alist))
182     (if (null lang-slot)                ; If no slot for the language, add it.
183         (setq lang-slot (list lang-env)
184               language-info-alist (cons lang-slot language-info-alist)))
185     (setq key-slot (assq key lang-slot))
186     (if (null key-slot)                 ; If no slot for the key, add it.
187         (progn
188           (setq key-slot (list key))
189           (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
190     (setcdr key-slot info)))
191
192 (defun set-language-info-alist (lang-env alist &optional parents)
193   "Store ALIST as the definition of language environment LANG-ENV.
194 ALIST is an alist of KEY and INFO values.  See the documentation of
195 `set-language-info' for the meanings of KEY and INFO."
196   (if (symbolp lang-env)
197       (setq lang-env (symbol-name lang-env)))
198   (let (; (describe-map describe-language-environment-map)
199         ; (setup-map setup-language-environment-map)
200         )
201     ;; (if parents
202     ;;     (let ((l parents)
203     ;;           map parent-symbol parent)
204     ;;       (while l
205     ;;         (if (symbolp (setq parent-symbol (car l)))
206     ;;             (setq parent (symbol-name parent))
207     ;;           (setq parent parent-symbol parent-symbol (intern parent)))
208     ;;         (setq map (lookup-key describe-map (vector parent-symbol)))
209     ;;         (if (not map)
210     ;;             (progn
211     ;;               (setq map (intern (format "describe-%s-environment-map"
212     ;;                                         (downcase parent))))
213     ;;               (define-prefix-command map)
214     ;;               (define-key-after describe-map (vector parent-symbol)
215     ;;                 (cons parent map) t)))
216     ;;         (setq describe-map (symbol-value map))
217     ;;         (setq map (lookup-key setup-map (vector parent-symbol)))
218     ;;         (if (not map)
219     ;;             (progn
220     ;;               (setq map (intern (format "setup-%s-environment-map"
221     ;;                                         (downcase parent))))
222     ;;               (define-prefix-command map)
223     ;;               (define-key-after setup-map (vector parent-symbol)
224     ;;                 (cons parent map) t)))
225     ;;         (setq setup-map (symbol-value map))
226     ;;         (setq l (cdr l)))))
227
228     ;; Set up menu items for this language env.
229     (let ((doc (assq 'documentation alist)))
230       (when doc
231         ;; (define-key-after describe-map (vector (intern lang-env))
232         ;;   (cons lang-env 'describe-specified-language-support) t)
233         (when (featurep 'menubar)
234           (eval-after-load
235               "menubar-items.elc"
236             `(add-menu-button
237               '("%_Edit" "%_Multilingual (\"Mule\")"
238                 "%_Describe Language Support")
239               (vector ,lang-env
240                       '(describe-language-environment ,lang-env)
241                       t))))
242         ))
243     ;; (define-key-after setup-map (vector (intern lang-env))
244     ;;   (cons lang-env 'setup-specified-language-environment) t)
245     (when (featurep 'menubar)
246       (eval-after-load
247           "menubar-items.elc"
248         `(add-menu-button
249           '("%_Edit" "%_Multilingual (\"Mule\")"
250             "%_Set Language Environment")
251           (vector ,lang-env
252                   '(set-language-environment ,lang-env)
253                   t))))
254     
255     (while alist
256       (set-language-info lang-env (car (car alist)) (cdr (car alist)))
257       (setq alist (cdr alist)))))
258
259 (defun read-language-name (key prompt &optional default)
260   "Read a language environment name which has information for KEY.
261 If KEY is nil, read any language environment.
262 Prompt with PROMPT.  DEFAULT is the default choice of language environment.
263 This returns a language environment name as a string."
264   (let* ((completion-ignore-case t)
265          (name (completing-read prompt
266                                 language-info-alist
267                                 (and key
268                                      (function (lambda (elm) (assq key elm))))
269                                 t nil nil default)))
270     (if (and (> (length name) 0)
271              (or (not key)
272                  (get-language-info name key)))
273         name)))
274 \f
275 ;;; Multilingual input methods.
276
277 (defconst leim-list-file-name "leim-list.el"
278   "Name of LEIM list file.
279 This file contains a list of libraries of Emacs input methods (LEIM)
280 in the format of Lisp expression for registering each input method.
281 Emacs loads this file at startup time.")
282
283 (defvar leim-list-header (format 
284 ";;; %s -- list of LEIM (Library of Emacs Input Method)
285 ;;
286 ;; This file contains a list of LEIM (Library of Emacs Input Method)
287 ;; in the same directory as this file.  Loading this file registers
288 ;; the whole input methods in Emacs.
289 ;;
290 ;; Each entry has the form:
291 ;;   (register-input-method
292 ;;    INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
293 ;;    TITLE DESCRIPTION
294 ;;    ARG ...)
295 ;; See the function `register-input-method' for the meanings of arguments.
296 ;;
297 ;; If this directory is included in load-path, Emacs automatically
298 ;; loads this file at startup time.
299
300 "
301                                  leim-list-file-name)
302   "Header to be inserted in LEIM list file.")
303
304 (defvar leim-list-entry-regexp "^(register-input-method"
305   "Regexp matching head of each entry in LEIM list file.
306 See also the variable `leim-list-header'")
307
308 (defvar update-leim-list-functions
309   '(quail-update-leim-list-file)
310   "List of functions to call to update LEIM list file.
311 Each function is called with one arg, LEIM directory name.")
312
313 (defun update-leim-list-file (&rest dirs)
314   "Update LEIM list file in directories DIRS."
315   (let ((functions update-leim-list-functions))
316     (while functions
317       (apply (car functions) dirs)
318       (setq functions (cdr functions)))))
319
320 (defvar current-input-method nil
321   "The current input method for multilingual text.
322 If nil, that means no input method is activated now.")
323 (make-variable-buffer-local 'current-input-method)
324 (put 'current-input-method 'permanent-local t)
325
326 (defvar current-input-method-title nil
327   "Title string of the current input method shown in mode line.")
328 (make-variable-buffer-local 'current-input-method-title)
329 (put 'current-input-method-title 'permanent-local t)
330
331 (defcustom default-input-method nil
332   "*Default input method for multilingual text (a string).
333 This is the input method activated automatically by the command
334 `toggle-input-method' (\\[toggle-input-method])."
335   :group 'mule
336   :type '(choice (const nil) string))
337
338 (put 'input-method-function 'permanent-local t)
339
340 (defvar input-method-history nil
341   "History list for some commands that read input methods.")
342 (make-variable-buffer-local 'input-method-history)
343 (put 'input-method-history 'permanent-local t)
344
345 (defvar inactivate-current-input-method-function nil
346   "Function to call for inactivating the current input method.
347 Every input method should set this to an appropriate value when activated.
348 This function is called with no argument.
349
350 This function should never change the value of `current-input-method'.
351 It is set to nil by the function `inactivate-input-method'.")
352 (make-variable-buffer-local 'inactivate-current-input-method-function)
353 (put 'inactivate-current-input-method-function 'permanent-local t)
354
355 (defvar describe-current-input-method-function nil
356   "Function to call for describing the current input method.
357 This function is called with no argument.")
358 (make-variable-buffer-local 'describe-current-input-method-function)
359 (put 'describe-current-input-method-function 'permanent-local t)
360
361 (defvar input-method-alist nil
362   "Alist of input method names vs how to use them.
363 Each element has the form:
364    (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
365 See the function `register-input-method' for the meanings of the elements.")
366
367 (defun register-input-method (input-method lang-env &rest args)
368   "Register INPUT-METHOD as an input method for language environment ENV.
369 INPUT-METHOD and LANG-ENV are symbols or strings.
370
371 The remaining arguments are:
372         ACTIVATE-FUNC, TITLE, DESCRIPTION, and ARGS...
373 ACTIVATE-FUNC is a function to call to activate this method.
374 TITLE is a string to show in the mode line when this method is active.
375 DESCRIPTION is a string describing this method and what it is good for.
376 The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
377 All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
378
379 This function is mainly used in the file \"leim-list.el\" which is
380 created at building time of emacs, registering all quail input methods
381 contained in the emacs distribution.
382
383 In case you want to register a new quail input method by yourself, be
384 careful to use the same input method title as given in the third
385 parameter of `quail-define-package' (if the values are different, the
386 string specified in this function takes precedence).
387
388 The commands `describe-input-method' and `list-input-methods' need
389 this duplicated values to show some information about input methods
390 without loading the affected quail packages."
391   (if (symbolp lang-env)
392       (setq lang-env (symbol-name lang-env)))
393   (if (symbolp input-method)
394       (setq input-method (symbol-name input-method)))
395   (let ((info (cons lang-env args))
396         (slot (assoc input-method input-method-alist)))
397     (if slot
398         (setcdr slot info)
399       (setq slot (cons input-method info))
400       (setq input-method-alist (cons slot input-method-alist)))))
401
402 (defun read-input-method-name (prompt &optional default inhibit-null)
403   "Read a name of input method from a minibuffer prompting with PROMPT.
404 If DEFAULT is non-nil, use that as the default,
405   and substitute it into PROMPT at the first `%s'.
406 If INHIBIT-NULL is non-nil, null input signals an error.
407
408 The return value is a string."
409   (if default
410       (setq prompt (format prompt default)))
411   (let* ((completion-ignore-case t)
412          ;; This binding is necessary because input-method-history is
413          ;; buffer local.
414          (input-method (completing-read prompt input-method-alist
415                                         nil t nil 'input-method-history
416                                         default)))
417     (if (and input-method (symbolp input-method))
418         (setq input-method (symbol-name input-method)))
419     (if (> (length input-method) 0)
420         input-method
421       (if inhibit-null
422           (error "No valid input method is specified")))))
423
424 (defun activate-input-method (input-method)
425   "Switch to input method INPUT-METHOD for the current buffer.
426 If some other input method is already active, turn it off first.
427 If INPUT-METHOD is nil, deactivate any current input method."
428   (if (and input-method (symbolp input-method))
429       (setq input-method (symbol-name input-method)))
430   (if (and current-input-method
431            (not (string= current-input-method input-method)))
432       (inactivate-input-method))
433   (unless (or current-input-method (null input-method))
434     (let ((slot (assoc input-method input-method-alist)))
435       (if (null slot)
436           (error "Can't activate input method `%s'" input-method))
437       (let ((func (nth 2 slot)))
438         (if (functionp func)
439             (apply (nth 2 slot) input-method (nthcdr 5 slot))
440           (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
441               (progn
442                 (require (cdr func))
443                 (apply (car func) input-method (nthcdr 5 slot)))
444             (error "Can't activate input method `%s'" input-method))))
445       (setq current-input-method input-method)
446       (setq current-input-method-title (nth 3 slot))
447       (unwind-protect
448           (run-hooks 'input-method-activate-hook)
449         (force-mode-line-update)))))
450
451 (defun inactivate-input-method ()
452   "Turn off the current input method."
453   (when current-input-method
454     (if input-method-history
455         (unless (string= current-input-method (car input-method-history))
456           (setq input-method-history
457                 (cons current-input-method
458                       (delete current-input-method input-method-history))))
459       (setq input-method-history (list current-input-method)))
460     (unwind-protect
461         (funcall inactivate-current-input-method-function)
462       (unwind-protect
463           (run-hooks 'input-method-inactivate-hook)
464         (setq current-input-method nil
465               current-input-method-title nil)
466         (force-mode-line-update)))))
467
468 (defun set-input-method (input-method)
469   "Select and activate input method INPUT-METHOD for the current buffer.
470 This also sets the default input method to the one you specify."
471   (interactive
472    (let* ((default (or (car input-method-history) default-input-method)))
473      (list (read-input-method-name
474             (if default "Select input method (default %s): " "Select input method: ")
475             default t))))
476   (activate-input-method input-method)
477   (setq default-input-method input-method))
478
479 (defun toggle-input-method (&optional arg)
480   "Turn on or off a multilingual text input method for the current buffer.
481
482 With no prefix argument, if an input method is currently activated,
483 turn it off.  Otherwise, activate an input method -- the one most
484 recently used, or the one specified in `default-input-method', or
485 the one read from the minibuffer.
486
487 With a prefix argument, read an input method from the minibuffer and
488 turn it on.
489
490 The default is to use the most recent input method specified
491 \(not including the currently active input method, if any)."
492   (interactive "P")
493   (if (and current-input-method (not arg))
494       (inactivate-input-method)
495     (let ((default (or (car input-method-history) default-input-method)))
496       (if (and arg default (equal current-input-method default)
497                (> (length input-method-history) 1))
498           (setq default (nth 1 input-method-history)))
499       (activate-input-method
500        (if (or arg (not default))
501            (progn
502              (read-input-method-name
503               (if default "Input method (default %s): " "Input method: " )
504               default t))
505          default))
506       (or default-input-method
507           (setq default-input-method current-input-method)))))
508
509 (defun describe-input-method (input-method)
510   "Describe input method INPUT-METHOD."
511   (interactive
512    (list (read-input-method-name
513           "Describe input method (default, current choice): ")))
514   (if (and input-method (symbolp input-method))
515       (setq input-method (symbol-name input-method)))
516   (if (null input-method)
517       (describe-current-input-method)
518     (with-output-to-temp-buffer "*Help*"
519       (let ((elt (assoc input-method input-method-alist)))
520         (princ (format "Input method: %s (`%s' in mode line) for %s\n  %s\n"
521                        input-method (nth 3 elt) (nth 1 elt) (nth 4 elt)))))))
522
523 (defun describe-current-input-method ()
524   "Describe the input method currently in use."
525   (if current-input-method
526       (if (and (symbolp describe-current-input-method-function)
527                (fboundp describe-current-input-method-function))
528           (funcall describe-current-input-method-function)
529         (message "No way to describe the current input method `%s'"
530                  current-input-method)
531         (ding))
532     (error "No input method is activated now")))
533
534 (defun read-multilingual-string (prompt &optional initial-input input-method)
535   "Read a multilingual string from minibuffer, prompting with string PROMPT.
536 The input method selected last time is activated in minibuffer.
537 If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
538 initially.
539 Optional 3rd argument INPUT-METHOD specifies the input method
540 to be activated instead of the one selected last time.  It is a symbol
541 or a string."
542   (setq input-method
543         (or input-method
544             current-input-method
545             default-input-method
546             (read-input-method-name "Input method: " nil t)))
547   (if (and input-method (symbolp input-method))
548       (setq input-method (symbol-name input-method)))
549   (let ((prev-input-method current-input-method))
550     (unwind-protect
551         (progn
552           (activate-input-method input-method)
553           ;; FSF Emacs
554           ;; (read-string prompt initial-input nil nil t)
555           (read-string prompt initial-input nil))
556       (activate-input-method prev-input-method))))
557
558 ;; Variables to control behavior of input methods.  All input methods
559 ;; should react to these variables.
560
561 (defcustom input-method-verbose-flag 'default
562   "*A flag to control extra guidance given by input methods.
563 The value should be nil, t, `complex-only', or `default'.
564
565 The extra guidance is done by showing list of available keys in echo
566 area.  When you use the input method in the minibuffer, the guidance
567 is shown at the bottom short window (split from the existing window).
568
569 If the value is t, extra guidance is always given, if the value is
570 nil, extra guidance is always suppressed.
571
572 If the value is `complex-only', only complex input methods such as
573 `chinese-py' and `japanese' give extra guidance.
574
575 If the value is `default', complex input methods always give extra
576 guidance, but simple input methods give it only when you are not in
577 the minibuffer.
578
579 See also the variable `input-method-highlight-flag'."
580   :type '(choice (const t) (const nil) (const complex-only) (const default))
581   :group 'mule)
582
583 (defcustom input-method-highlight-flag t
584   "*If this flag is non-nil, input methods highlight partially-entered text.
585 For instance, while you are in the middle of a Quail input method sequence,
586 the text inserted so far is temporarily underlined.
587 The underlining goes away when you finish or abort the input method sequence.
588 See also the variable `input-method-verbose-flag'."
589   :type 'boolean
590   :group 'mule)
591
592 (defvar input-method-activate-hook nil
593   "Normal hook run just after an input method is activated.
594
595 The variable `current-input-method' keeps the input method name
596 just activated.")
597
598 (defvar input-method-inactivate-hook nil
599   "Normal hook run just after an input method is inactivated.
600
601 The variable `current-input-method' still keeps the input method name
602 just inactivated.")
603
604 (defvar input-method-after-insert-chunk-hook nil
605   "Normal hook run just after an input method insert some chunk of text.")
606
607 (defvar input-method-exit-on-first-char nil
608   "This flag controls a timing when an input method returns.
609 Usually, the input method does not return while there's a possibility
610 that it may find a different translation if a user types another key.
611 But, it this flag is non-nil, the input method returns as soon as
612 the current key sequence gets long enough to have some valid translation.")
613
614 (defvar input-method-use-echo-area nil
615   "This flag controls how an input method shows an intermediate key sequence.
616 Usually, the input method inserts the intermediate key sequence,
617 or candidate translations corresponding to the sequence,
618 at point in the current buffer.
619 But, if this flag is non-nil, it displays them in echo area instead.")
620
621 (defvar input-method-exit-on-invalid-key nil
622   "This flag controls the behavior of an input method on invalid key input.
623 Usually, when a user types a key which doesn't start any character
624 handled by the input method, the key is handled by turning off the
625 input method temporarily.  After that key, the input method is re-enabled.
626 But, if this flag is non-nil, the input method is never back on.")
627
628 \f
629 (defvar set-language-environment-hook nil
630   "Normal hook run after some language environment is set.
631
632 When you set some hook function here, that effect usually should not
633 be inherited to another language environment.  So, you had better set
634 another function in `exit-language-environment-hook' (which see) to
635 cancel the effect.")
636
637 (defvar exit-language-environment-hook nil
638   "Normal hook run after exiting from some language environment.
639 When this hook is run, the variable `current-language-environment'
640 is still bound to the language environment being exited.
641
642 This hook is mainly used for canceling the effect of
643 `set-language-environment-hook' (which-see).")
644
645 (put 'setup-specified-language-environment 'apropos-inhibit t)
646
647 (defun setup-specified-language-environment ()
648   "Switch to a specified language environment."
649   (interactive)
650   (let (language-name)
651     (if (and (symbolp last-command-event)
652              (or (not (eq last-command-event 'Default))
653                  (setq last-command-event 'English))
654              (setq language-name (symbol-name last-command-event)))
655         (set-language-environment language-name)
656       (error "Bogus calling sequence"))))
657
658 (defcustom current-language-environment "English"
659   "The last language environment specified with `set-language-environment'.
660 This variable should be set only with \\[customize], which is equivalent
661 to using the function `set-language-environment'."
662   :link '(custom-manual "(emacs)Language Environments")
663   :set (lambda (symbol value) (set-language-environment value))
664   :get (lambda (x)
665          (or (car-safe (assoc-ignore-case
666                         (if (symbolp current-language-environment)
667                             (symbol-name current-language-environment)
668                           current-language-environment)
669                         language-info-alist))
670              "English"))
671   :type (cons 'choice (mapcar (lambda (lang)
672                                 (list 'const (car lang)))
673                               language-info-alist))
674   :initialize 'custom-initialize-default
675   :group 'mule
676   :type 'string)
677
678 (defun reset-language-environment ()
679   "Reset multilingual environment of Emacs to the default status.
680
681 The default status is as follows:
682
683   The default value of `buffer-file-coding-system' is nil.
684   The default coding system for process I/O is nil.
685   The default value for the command `set-terminal-coding-system' is nil.
686   The default value for the command `set-keyboard-coding-system' is nil.
687
688   The order of priorities of coding categories and the coding system
689   bound to each category are as follows
690         coding category         coding system
691         --------------------------------------------------
692         iso-7                   iso-2022-7bit
693         no-conversion           raw-text
694         utf-8                   utf-8
695         iso-8-1                 iso-8859-1
696         iso-8-2                 ctext (iso-8859-1 alias)
697         iso-8-designate         ctext (iso-8859-1 alias)
698         iso-lock-shift          iso-2022-lock
699         shift-jis               shift_jis
700         big5                    big5
701         ucs-4                   iso-10646-ucs-4
702 "
703 ;; The old table (from FSF synch?) was not what we use (cf mule-coding.el),
704 ;; and as documented iso-8-designate is inconsistent with iso-2022-8bit-ss2.
705 ;;  The order of priorities of coding categories and the coding system
706 ;;  bound to each category are as follows
707 ;;      coding category         coding system
708 ;;      --------------------------------------------------
709 ;;      iso-8-2                 iso-8859-1
710 ;;      iso-8-1                 iso-8859-1
711 ;;      iso-7                   iso-2022-7bit
712 ;;      iso-lock-shift          iso-2022-lock
713 ;;      iso-8-designate         iso-2022-8bit-ss2
714 ;;      no-conversion           raw-text
715 ;;      shift-jis               shift_jis
716 ;;      big5                    big5
717 ;;      ucs-4                   ----
718 ;;      utf-8                   ----
719   (interactive)
720
721   (set-coding-category-system 'iso-7            'iso-2022-7)
722   (set-coding-category-system 'iso-8-1          'iso-8859-1)
723   (set-coding-category-system 'iso-8-2          'ctext)
724   (set-coding-category-system 'iso-lock-shift   'iso-2022-lock)
725   (set-coding-category-system 'iso-8-designate  'ctext)
726   (set-coding-category-system 'no-conversion    'raw-text)
727   (set-coding-category-system 'shift-jis        'shift_jis)
728   (set-coding-category-system 'big5             'big5)
729   ;; #### Can we now assume the existence of the 10646 coding systems?
730   ;; #### These lists need to be synched with the ones in mule-coding.el.
731   (cond ((eq (coding-system-type (coding-category-system 'utf-8)) 'utf-8)
732          (set-coding-category-system 'utf-8 'utf-8)
733          (cond ((eq (coding-system-type (coding-category-system 'ucs-4))
734                     'iso-10646-ucs-4)
735                 (set-coding-category-system 'ucs-4 'iso-10646-ucs-4)
736                 (set-coding-priority-list
737                  '(iso-7
738                    no-conversion
739                    utf-8
740                    iso-8-1
741                    iso-8-2
742                    iso-8-designate
743                    iso-lock-shift
744                    shift-jis
745                    big5
746                    ucs-4))
747                 )
748                (t
749                 (set-coding-priority-list
750                  '(iso-7
751                    no-conversion
752                    utf-8
753                    iso-8-1
754                    iso-8-2
755                    iso-8-designate
756                    iso-lock-shift
757                    shift-jis
758                    big5))
759                 ))
760          )
761         (t
762          (set-coding-priority-list
763           '(iso-7
764             no-conversion
765             iso-8-1
766             iso-8-2
767             iso-8-designate
768             iso-lock-shift
769             shift-jis
770             big5))
771          ))
772
773   ;; (update-coding-systems-internal)
774
775   (set-default-coding-systems nil)
776   ;; Don't alter the terminal and keyboard coding systems here.
777   ;; The terminal still supports the same coding system
778   ;; that it supported a minute ago.
779 ;;;  (set-terminal-coding-system-internal nil)
780 ;;;  (set-keyboard-coding-system-internal nil)
781
782   ;; (setq nonascii-translation-table nil
783   ;;       nonascii-insert-offset 0)
784   )
785
786 (defun set-language-environment (language-name)
787   "Set up multi-lingual environment for using LANGUAGE-NAME.
788 This sets the coding system priority and the default input method
789 and sometimes other things.  LANGUAGE-NAME should be a string
790 which is the name of a language environment.  For example, \"Latin-1\"
791 specifies the character set for the major languages of Western Europe."
792   (interactive (list (read-language-name
793                       nil
794                       "Set language environment (default, English): ")))
795   (if language-name
796       (if (symbolp language-name)
797           (setq language-name (symbol-name language-name)))
798     (setq language-name "English"))
799   (or (assoc-ignore-case language-name language-info-alist)
800       (error "Language environment not defined: %S" language-name))
801   (if current-language-environment
802       (let ((func (get-language-info current-language-environment
803                                      'exit-function)))
804         (run-hooks 'exit-language-environment-hook)
805         (if (fboundp func) (funcall func))))
806   (let ((default-eol-type (coding-system-eol-type
807                            default-buffer-file-coding-system)))
808     (reset-language-environment)
809
810     (setq current-language-environment language-name)
811     (set-language-environment-coding-systems language-name default-eol-type))
812   (let ((input-method (get-language-info language-name 'input-method)))
813     (when input-method
814       (setq default-input-method input-method)
815       (if input-method-history
816           (setq input-method-history
817                 (cons input-method
818                       (delete input-method input-method-history))))))
819   ;; (let ((nonascii (get-language-info language-name 'nonascii-translation))
820   ;;       (dos-table
821   ;;        (if (eq window-system 'pc)
822   ;;            (intern
823   ;;             (concat "cp" dos-codepage "-nonascii-translation-table")))))
824   ;;   (cond
825   ;;    ((char-table-p nonascii)
826   ;;     (setq nonascii-translation-table nonascii))
827   ;;    ((and (eq window-system 'pc) (boundp dos-table))
828   ;;     ;; DOS terminals' default is to use a special non-ASCII translation
829   ;;     ;; table as appropriate for the installed codepage.
830   ;;     (setq nonascii-translation-table (symbol-value dos-table)))
831   ;;    ((charsetp nonascii)
832   ;;     (setq nonascii-insert-offset (- (make-char nonascii) 128)))))
833
834   ;; (setq charset-origin-alist
835   ;;       (get-language-info language-name 'charset-origin-alist))
836
837   ;; Unibyte setups if necessary.
838   ;; (unless default-enable-multibyte-characters
839   ;;   ;; Syntax and case table.
840   ;;   (let ((syntax (get-language-info language-name 'unibyte-syntax)))
841   ;;     (if syntax
842   ;;         (let ((set-case-syntax-set-multibyte nil))
843   ;;           (load syntax nil t))
844   ;;       ;; No information for syntax and case.  Reset to the defaults.
845   ;;       (let ((syntax-table (standard-syntax-table))
846   ;;             (case-table (standard-case-table))
847   ;;             (ch (if (eq window-system 'pc) 128 160)))
848   ;;         (while (< ch 256)
849   ;;           (modify-syntax-entry ch " " syntax-table)
850   ;;           (aset case-table ch ch)
851   ;;           (setq ch (1+ ch)))
852   ;;         (set-char-table-extra-slot case-table 0 nil)
853   ;;         (set-char-table-extra-slot case-table 1 nil)
854   ;;         (set-char-table-extra-slot case-table 2 nil))
855   ;;       (set-standard-case-table (standard-case-table))
856   ;;       (let ((list (buffer-list)))
857   ;;         (while list
858   ;;           (with-current-buffer (car list)
859   ;;             (set-case-table (standard-case-table)))
860   ;;           (setq list (cdr list))))))
861   ;;   ;; Display table and coding system for terminal.
862   ;;   (let ((coding (get-language-info language-name 'unibyte-display)))
863   ;;     (if coding
864   ;;         (standard-display-european-internal)
865   ;;       (standard-display-default (if (eq window-system 'pc) 128 160) 255)
866   ;;       (aset standard-display-table 146 nil))
867   ;;     (or (eq window-system 'pc)
868   ;;         (set-terminal-coding-system coding))))
869
870   (let ((required-features (get-language-info language-name 'features)))
871     (while required-features
872       (require (car required-features))
873       (setq required-features (cdr required-features))))
874   (let ((func (get-language-info language-name 'setup-function)))
875     (if (fboundp func)
876         (funcall func)))
877   (run-hooks 'set-language-environment-hook)
878   (force-mode-line-update t))
879
880 ;; (defun standard-display-european-internal ()
881 ;;   ;; Actually set up direct output of non-ASCII characters.
882 ;;   (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
883 ;;   ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
884 ;;   ;; the native font, and codes 160 and 146 stand for something very
885 ;;   ;; different there.
886 ;;   (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
887 ;;       (progn
888 ;;         ;; Make non-line-break space display as a plain space.
889 ;;         ;; Most X fonts do the wrong thing for code 160.
890 ;;         (aset standard-display-table 160 [32])
891 ;;         ;; Most Windows programs send out apostrophe's as \222.  Most X fonts
892 ;;         ;; don't contain a character at that position.  Map it to the ASCII
893 ;;         ;; apostrophe.
894 ;;         (aset standard-display-table 146 [39]))))
895
896 (defun set-language-environment-coding-systems (language-name
897                                                 &optional eol-type)
898   "Do various coding system setups for language environment LANGUAGE-NAME.
899
900 The optional arg EOL-TYPE specifies the eol-type of the default value
901 of buffer-file-coding-system set by this function.
902
903 Note that `coding-priority-list' is not reset first; thus changing language
904 environment allows recognition of coding systems from previously set language
905 environments.  (This will not work if the desired coding systems are from the
906 same category.  E.g., starting with a Hebrew language environment, ISO 8859-8
907 will be recognized.  If you shift to Russian, ISO 8859-8 will be shadowed by
908 ISO 8859-5, and cannot be automatically recognized without resetting the
909 language environment to Hebrew.  However, if you shift from Japanese to
910 Russian, ISO-2022-JP will continue to be automatically recognized, since
911 ISO-8859-5 and ISO-2022-JP are different coding categories.)"
912   (let* ((priority (get-language-info language-name 'coding-priority))
913          (default-coding (car priority)))
914     (if priority
915         (let ((categories (mapcar 'coding-system-category priority))
916               category checked-categories)
917           (set-default-coding-systems
918            (if (memq eol-type '(lf crlf cr unix dos mac))
919                (coding-system-change-eol-conversion default-coding eol-type)
920              default-coding))
921           ;; (setq default-sendmail-coding-system default-coding)
922           (while priority
923             (unless (memq (setq category (car categories)) checked-categories)
924               (set-coding-category-system category (car priority))
925               (setq checked-categories (cons category checked-categories)))
926             (setq priority (cdr priority)
927                   categories (cdr categories)))
928           (set-coding-priority-list (nreverse checked-categories))
929           ;; (update-coding-systems-internal)
930           ))))
931
932 ;; Print all arguments with `princ', then print "\n".
933 (defsubst princ-list (&rest args)
934   (while args (princ (car args)) (setq args (cdr args)))
935   (princ "\n"))
936
937 (put 'describe-specified-language-support 'apropos-inhibit t)
938
939 ;; Print a language specific information such as input methods,
940 ;; charsets, and coding systems.  This function is intended to be
941 ;; called from the menu:
942 ;;   [menu-bar mule describe-language-environment LANGUAGE]
943 ;; and should not run it by `M-x describe-current-input-method-function'.
944 (defun describe-specified-language-support ()
945   "Describe how Emacs supports the specified language environment."
946   (interactive)
947   (let (language-name)
948     (if (not (and (symbolp last-command-event)
949                   (setq language-name (symbol-name last-command-event))))
950         (error "Bogus calling sequence"))
951     (describe-language-environment language-name)))
952
953 (defun describe-language-environment (language-name)
954   "Describe how Emacs supports language environment LANGUAGE-NAME."
955   (interactive
956    (list (read-language-name
957           'documentation
958           "Describe language environment (default, current choice): ")))
959   (if (null language-name)
960       (setq language-name current-language-environment))
961   (if (or (null language-name)
962           (null (get-language-info language-name 'documentation)))
963       (error "No documentation for the specified language"))
964   (if (symbolp language-name)
965       (setq language-name (symbol-name language-name)))
966   (let ((doc (get-language-info language-name 'documentation)))
967     (with-output-to-temp-buffer "*Help*"
968       (princ-list language-name " language environment" "\n")
969       (if (stringp doc)
970           (progn
971             (princ-list doc)
972             (terpri)))
973       (let ((str (get-language-info language-name 'sample-text)))
974         (if (stringp str)
975             (progn
976               (princ "Sample text:\n")
977               (princ-list "  " str)
978               (terpri))))
979       (let ((input-method (get-language-info language-name 'input-method))
980             (l (copy-sequence input-method-alist)))
981         (princ "Input methods")
982         (when input-method
983           (princ (format " (default, %s)" input-method))
984           (setq input-method (assoc input-method input-method-alist))
985           (setq l (cons input-method (delete input-method l))))
986         (princ ":\n")
987         (while l
988           (if (string= language-name (nth 1 (car l)))
989               (princ-list "  " (car (car l))
990                           (format " (`%s' in mode line)" (nth 3 (car l)))))
991           (setq l (cdr l))))
992       (terpri)
993       (princ "Character sets:\n")
994       (let ((l (get-language-info language-name 'charset)))
995         (if (null l)
996             (princ-list "  nothing specific to " language-name)
997           (while l
998             (princ-list "  " (car l) ": "
999                         (charset-description (car l)))
1000             (setq l (cdr l)))))
1001       (terpri)
1002       (princ "Coding systems:\n")
1003       (let ((l (get-language-info language-name 'coding-system)))
1004         (if (null l)
1005             (princ-list "  nothing specific to " language-name)
1006           (while l
1007             (princ ; (format "  %s (`%c' in mode line):\n\t%s\n"
1008              ;; In XEmacs, `coding-system-mnemonic' returns string.
1009              (format "  %s (`%s' in mode line):\n\t%s\n"
1010                            (car l)
1011                            (coding-system-mnemonic (car l))
1012                            (coding-system-doc-string (car l))))
1013             ;; (let ((aliases (coding-system-get (car l) 'alias-coding-systems)))
1014             ;;   (when aliases
1015             ;;     (princ "\t")
1016             ;;     (princ (cons 'alias: (cdr aliases)))
1017             ;;     (terpri)))
1018             (setq l (cdr l))))))))
1019 \f
1020 ;;; Charset property
1021
1022 ;; (defsubst get-charset-property (charset propname)
1023 ;;   "Return the value of CHARSET's PROPNAME property.
1024 ;; This is the last value stored with
1025 ;; `(put-charset-property CHARSET PROPNAME VALUE)'."
1026 ;;   (plist-get (charset-plist charset) propname))
1027
1028 ;; (defsubst put-charset-property (charset propname value)
1029 ;;   "Store CHARSETS's PROPNAME property with value VALUE.
1030 ;; It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
1031 ;;   (set-charset-plist charset
1032 ;;                      (plist-put (charset-plist charset) propname value)))
1033
1034 (defvar char-code-property-table
1035   (make-char-table 'generic)
1036   "Char-table containing a property list of each character code.
1037
1038 See also the documentation of `get-char-code-property' and
1039 `put-char-code-property'")
1040 ;;   (let ((plist (aref char-code-property-table char)))
1041 (defun get-char-code-property (char propname)
1042   "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
1043   (let ((plist (get-char-table char char-code-property-table)))
1044     (if (listp plist)
1045         (car (cdr (memq propname plist))))))
1046
1047 (defun put-char-code-property (char propname value)
1048   "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
1049 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
1050   (let ((plist (get-char-table char char-code-property-table)))
1051     (if plist
1052         (let ((slot (memq propname plist)))
1053           (if slot
1054               (setcar (cdr slot) value)
1055             (nconc plist (list propname value))))
1056       (put-char-table char (list propname value) char-code-property-table)
1057       )))
1058
1059 \f
1060 ;; Pretty description of encoded string
1061
1062 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
1063 ;; (defvar iso-2022-control-alist
1064 ;;   '((?\x1b . "ESC")
1065 ;;     (?\x0e . "SO")
1066 ;;     (?\x0f . "SI")
1067 ;;     (?\x8e . "SS2")
1068 ;;     (?\x8f . "SS3")
1069 ;;     (?\x9b . "CSI")))
1070
1071 ;; (defun encoded-string-description (str coding-system)
1072 ;;   "Return a pretty description of STR that is encoded by CODING-SYSTEM."
1073 ;;   (setq str (string-as-unibyte str))
1074 ;;   (let ((char (aref str 0))
1075 ;;         desc)
1076 ;;     (when (< char 128)
1077 ;;       (setq desc (or (cdr (assq char iso-2022-control-alist))
1078 ;;                      (char-to-string char)))
1079 ;;       (let ((i 1)
1080 ;;             (len (length str))) 
1081 ;;         (while (< i len)
1082 ;;           (setq char (aref str i))
1083 ;;           (if (>= char 128)
1084 ;;               (setq desc nil i len)
1085 ;;             (setq desc (concat desc " "
1086 ;;                                (or (cdr (assq char iso-2022-control-alist))
1087 ;;                                    (char-to-string char)))
1088 ;;                   i (1+ i))))))
1089 ;;     (or desc
1090 ;;         (mapconcat (function (lambda (x) (format "0x%02x" x))) str " "))))
1091
1092 ;; (defun encode-coding-char (char coding-system)
1093 ;;   "Encode CHAR by CODING-SYSTEM and return the resulting string.
1094 ;; If CODING-SYSTEM can't safely encode CHAR, return nil."
1095 ;;   (if (cmpcharp char)
1096 ;;       (setq char (car (decompose-composite-char char 'list))))
1097 ;;   (let ((str1 (char-to-string char))
1098 ;;         (str2 (make-string 2 char))
1099 ;;         (safe-charsets (and coding-system
1100 ;;                             (coding-system-get coding-system 'safe-charsets)))
1101 ;;         enc1 enc2 i1 i2)
1102 ;;     (when (or (eq safe-charsets t)
1103 ;;               (memq (char-charset char) safe-charsets))
1104 ;;       ;; We must find the encoded string of CHAR.  But, just encoding
1105 ;;       ;; CHAR will put extra control sequences (usually to designate
1106 ;;       ;; ASCII charset) at the tail if type of CODING is ISO 2022.
1107 ;;       ;; To exclude such tailing bytes, we at first encode one-char
1108 ;;       ;; string and two-char string, then check how many bytes at the
1109 ;;       ;; tail of both encoded strings are the same.
1110 ;; 
1111 ;;       (setq enc1 (string-as-unibyte (encode-coding-string str1 coding-system))
1112 ;;             i1 (length enc1)
1113 ;;             enc2 (string-as-unibyte (encode-coding-string str2 coding-system))
1114 ;;             i2 (length enc2))
1115 ;;       (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
1116 ;;         (setq i1 (1- i1) i2 (1- i2)))
1117 ;; 
1118 ;;       ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
1119 ;;       ;; and they are the extra control sequences at the tail to
1120 ;;       ;; exclude.
1121 ;;       (substring enc2 0 i2))))
1122
1123
1124 ;;; mule-cmds.el ends here