import xemacs-21.2.37
[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-8-2                 iso-8859-1
693         iso-8-1                 iso-8859-1
694         iso-7                   iso-2022-7bit
695         iso-lock-shift          iso-2022-lock
696         iso-8-designate         iso-2022-8bit-ss2
697         no-conversion           raw-text
698         shift-jis               shift_jis
699         big5                    big5
700         ucs-4                   ----
701         utf-8                   ----
702 "
703   (interactive)
704   ;; This function formerly set default-enable-multibyte-characters to t,
705   ;; but that is incorrect.  It should not alter the unibyte/multibyte choice.
706
707   (set-coding-category-system 'iso-7            'iso-2022-7bit)
708   (set-coding-category-system 'iso-8-1          'iso-8859-1)
709   (set-coding-category-system 'iso-8-2          'iso-8859-1)
710   (set-coding-category-system 'iso-lock-shift   'iso-2022-lock)
711   (set-coding-category-system 'iso-8-designate  'iso-2022-8bit-ss2)
712   (set-coding-category-system 'no-conversion    'raw-text)
713   (set-coding-category-system 'shift-jis        'shift_jis)
714   (set-coding-category-system 'big5             'big5)
715   (cond ((eq (coding-system-type (coding-category-system 'utf-8)) 'utf-8)
716          (set-coding-category-system 'ucs-4 'iso-10646-ucs-4)
717          (set-coding-category-system 'utf-8 'utf-8)
718          (set-coding-priority-list
719           '(iso-8-1
720             iso-8-2
721             iso-7
722             iso-lock-shift
723             iso-8-designate
724             utf-8
725             ucs-4
726             no-conversion
727             shift-jis
728             big5))
729          )
730         (t
731          (set-coding-priority-list
732           '(iso-8-1
733             iso-8-2
734             iso-7
735             iso-lock-shift
736             iso-8-designate
737             no-conversion
738             shift-jis
739             big5))
740          ))
741
742   ;; (update-coding-systems-internal)
743
744   (set-default-coding-systems nil)
745   ;; Don't alter the terminal and keyboard coding systems here.
746   ;; The terminal still supports the same coding system
747   ;; that it supported a minute ago.
748 ;;;  (set-terminal-coding-system-internal nil)
749 ;;;  (set-keyboard-coding-system-internal nil)
750
751   ;; (setq nonascii-translation-table nil
752   ;;       nonascii-insert-offset 0)
753   )
754
755 (defun set-language-environment (language-name)
756   "Set up multi-lingual environment for using LANGUAGE-NAME.
757 This sets the coding system priority and the default input method
758 and sometimes other things.  LANGUAGE-NAME should be a string
759 which is the name of a language environment.  For example, \"Latin-1\"
760 specifies the character set for the major languages of Western Europe."
761   (interactive (list (read-language-name
762                       nil
763                       "Set language environment (default, English): ")))
764   (if language-name
765       (if (symbolp language-name)
766           (setq language-name (symbol-name language-name)))
767     (setq language-name "English"))
768   (or (assoc-ignore-case language-name language-info-alist)
769       (error "Language environment not defined: %S" language-name))
770   (if current-language-environment
771       (let ((func (get-language-info current-language-environment
772                                      'exit-function)))
773         (run-hooks 'exit-language-environment-hook)
774         (if (fboundp func) (funcall func))))
775   (let ((default-eol-type (coding-system-eol-type
776                            default-buffer-file-coding-system)))
777     (reset-language-environment)
778
779     (setq current-language-environment language-name)
780     (set-language-environment-coding-systems language-name default-eol-type))
781   (let ((input-method (get-language-info language-name 'input-method)))
782     (when input-method
783       (setq default-input-method input-method)
784       (if input-method-history
785           (setq input-method-history
786                 (cons input-method
787                       (delete input-method input-method-history))))))
788   ;; (let ((nonascii (get-language-info language-name 'nonascii-translation))
789   ;;       (dos-table
790   ;;        (if (eq window-system 'pc)
791   ;;            (intern
792   ;;             (concat "cp" dos-codepage "-nonascii-translation-table")))))
793   ;;   (cond
794   ;;    ((char-table-p nonascii)
795   ;;     (setq nonascii-translation-table nonascii))
796   ;;    ((and (eq window-system 'pc) (boundp dos-table))
797   ;;     ;; DOS terminals' default is to use a special non-ASCII translation
798   ;;     ;; table as appropriate for the installed codepage.
799   ;;     (setq nonascii-translation-table (symbol-value dos-table)))
800   ;;    ((charsetp nonascii)
801   ;;     (setq nonascii-insert-offset (- (make-char nonascii) 128)))))
802
803   ;; (setq charset-origin-alist
804   ;;       (get-language-info language-name 'charset-origin-alist))
805
806   ;; Unibyte setups if necessary.
807   ;; (unless default-enable-multibyte-characters
808   ;;   ;; Syntax and case table.
809   ;;   (let ((syntax (get-language-info language-name 'unibyte-syntax)))
810   ;;     (if syntax
811   ;;         (let ((set-case-syntax-set-multibyte nil))
812   ;;           (load syntax nil t))
813   ;;       ;; No information for syntax and case.  Reset to the defaults.
814   ;;       (let ((syntax-table (standard-syntax-table))
815   ;;             (case-table (standard-case-table))
816   ;;             (ch (if (eq window-system 'pc) 128 160)))
817   ;;         (while (< ch 256)
818   ;;           (modify-syntax-entry ch " " syntax-table)
819   ;;           (aset case-table ch ch)
820   ;;           (setq ch (1+ ch)))
821   ;;         (set-char-table-extra-slot case-table 0 nil)
822   ;;         (set-char-table-extra-slot case-table 1 nil)
823   ;;         (set-char-table-extra-slot case-table 2 nil))
824   ;;       (set-standard-case-table (standard-case-table))
825   ;;       (let ((list (buffer-list)))
826   ;;         (while list
827   ;;           (with-current-buffer (car list)
828   ;;             (set-case-table (standard-case-table)))
829   ;;           (setq list (cdr list))))))
830   ;;   ;; Display table and coding system for terminal.
831   ;;   (let ((coding (get-language-info language-name 'unibyte-display)))
832   ;;     (if coding
833   ;;         (standard-display-european-internal)
834   ;;       (standard-display-default (if (eq window-system 'pc) 128 160) 255)
835   ;;       (aset standard-display-table 146 nil))
836   ;;     (or (eq window-system 'pc)
837   ;;         (set-terminal-coding-system coding))))
838
839   (let ((required-features (get-language-info language-name 'features)))
840     (while required-features
841       (require (car required-features))
842       (setq required-features (cdr required-features))))
843   (let ((func (get-language-info language-name 'setup-function)))
844     (if (fboundp func)
845         (funcall func)))
846   (run-hooks 'set-language-environment-hook)
847   (force-mode-line-update t))
848
849 ;; (defun standard-display-european-internal ()
850 ;;   ;; Actually set up direct output of non-ASCII characters.
851 ;;   (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
852 ;;   ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
853 ;;   ;; the native font, and codes 160 and 146 stand for something very
854 ;;   ;; different there.
855 ;;   (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
856 ;;       (progn
857 ;;         ;; Make non-line-break space display as a plain space.
858 ;;         ;; Most X fonts do the wrong thing for code 160.
859 ;;         (aset standard-display-table 160 [32])
860 ;;         ;; Most Windows programs send out apostrophe's as \222.  Most X fonts
861 ;;         ;; don't contain a character at that position.  Map it to the ASCII
862 ;;         ;; apostrophe.
863 ;;         (aset standard-display-table 146 [39]))))
864
865 (defun set-language-environment-coding-systems (language-name
866                                                 &optional eol-type)
867   "Do various coding system setups for language environment LANGUAGE-NAME.
868
869 The optional arg EOL-TYPE specifies the eol-type of the default value
870 of buffer-file-coding-system set by this function."
871   (let* ((priority (get-language-info language-name 'coding-priority))
872          (default-coding (car priority)))
873     (if priority
874         (let ((categories (mapcar 'coding-system-category priority))
875               category checked-categories)
876           (set-default-coding-systems
877            (if (memq eol-type '(lf crlf cr unix dos mac))
878                (coding-system-change-eol-conversion default-coding eol-type)
879              default-coding))
880           ;; (setq default-sendmail-coding-system default-coding)
881           (while priority
882             (unless (memq (setq category (car categories)) checked-categories)
883               (set-coding-category-system category (car priority))
884               (setq checked-categories (cons category checked-categories)))
885             (setq priority (cdr priority)
886                   categories (cdr categories)))
887           (set-coding-priority-list (nreverse checked-categories))
888           ;; (update-coding-systems-internal)
889           ))))
890
891 ;; Print all arguments with `princ', then print "\n".
892 (defsubst princ-list (&rest args)
893   (while args (princ (car args)) (setq args (cdr args)))
894   (princ "\n"))
895
896 (put 'describe-specified-language-support 'apropos-inhibit t)
897
898 ;; Print a language specific information such as input methods,
899 ;; charsets, and coding systems.  This function is intended to be
900 ;; called from the menu:
901 ;;   [menu-bar mule describe-language-environment LANGUAGE]
902 ;; and should not run it by `M-x describe-current-input-method-function'.
903 (defun describe-specified-language-support ()
904   "Describe how Emacs supports the specified language environment."
905   (interactive)
906   (let (language-name)
907     (if (not (and (symbolp last-command-event)
908                   (setq language-name (symbol-name last-command-event))))
909         (error "Bogus calling sequence"))
910     (describe-language-environment language-name)))
911
912 (defun describe-language-environment (language-name)
913   "Describe how Emacs supports language environment LANGUAGE-NAME."
914   (interactive
915    (list (read-language-name
916           'documentation
917           "Describe language environment (default, current choice): ")))
918   (if (null language-name)
919       (setq language-name current-language-environment))
920   (if (or (null language-name)
921           (null (get-language-info language-name 'documentation)))
922       (error "No documentation for the specified language"))
923   (if (symbolp language-name)
924       (setq language-name (symbol-name language-name)))
925   (let ((doc (get-language-info language-name 'documentation)))
926     (with-output-to-temp-buffer "*Help*"
927       (princ-list language-name " language environment" "\n")
928       (if (stringp doc)
929           (progn
930             (princ-list doc)
931             (terpri)))
932       (let ((str (get-language-info language-name 'sample-text)))
933         (if (stringp str)
934             (progn
935               (princ "Sample text:\n")
936               (princ-list "  " str)
937               (terpri))))
938       (let ((input-method (get-language-info language-name 'input-method))
939             (l (copy-sequence input-method-alist)))
940         (princ "Input methods")
941         (when input-method
942           (princ (format " (default, %s)" input-method))
943           (setq input-method (assoc input-method input-method-alist))
944           (setq l (cons input-method (delete input-method l))))
945         (princ ":\n")
946         (while l
947           (if (string= language-name (nth 1 (car l)))
948               (princ-list "  " (car (car l))
949                           (format " (`%s' in mode line)" (nth 3 (car l)))))
950           (setq l (cdr l))))
951       (terpri)
952       (princ "Character sets:\n")
953       (let ((l (get-language-info language-name 'charset)))
954         (if (null l)
955             (princ-list "  nothing specific to " language-name)
956           (while l
957             (princ-list "  " (car l) ": "
958                         (charset-description (car l)))
959             (setq l (cdr l)))))
960       (terpri)
961       (princ "Coding systems:\n")
962       (let ((l (get-language-info language-name 'coding-system)))
963         (if (null l)
964             (princ-list "  nothing specific to " language-name)
965           (while l
966             (princ ; (format "  %s (`%c' in mode line):\n\t%s\n"
967              ;; In XEmacs, `coding-system-mnemonic' returns string.
968              (format "  %s (`%s' in mode line):\n\t%s\n"
969                            (car l)
970                            (coding-system-mnemonic (car l))
971                            (coding-system-doc-string (car l))))
972             ;; (let ((aliases (coding-system-get (car l) 'alias-coding-systems)))
973             ;;   (when aliases
974             ;;     (princ "\t")
975             ;;     (princ (cons 'alias: (cdr aliases)))
976             ;;     (terpri)))
977             (setq l (cdr l))))))))
978 \f
979 ;;; Charset property
980
981 ;; (defsubst get-charset-property (charset propname)
982 ;;   "Return the value of CHARSET's PROPNAME property.
983 ;; This is the last value stored with
984 ;; `(put-charset-property CHARSET PROPNAME VALUE)'."
985 ;;   (plist-get (charset-plist charset) propname))
986
987 ;; (defsubst put-charset-property (charset propname value)
988 ;;   "Store CHARSETS's PROPNAME property with value VALUE.
989 ;; It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
990 ;;   (set-charset-plist charset
991 ;;                      (plist-put (charset-plist charset) propname value)))
992
993 (defvar char-code-property-table
994   (make-char-table 'generic)
995   "Char-table containing a property list of each character code.
996
997 See also the documentation of `get-char-code-property' and
998 `put-char-code-property'")
999 ;;   (let ((plist (aref char-code-property-table char)))
1000 (defun get-char-code-property (char propname)
1001   "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
1002   (let ((plist (get-char-table char char-code-property-table)))
1003     (if (listp plist)
1004         (car (cdr (memq propname plist))))))
1005
1006 (defun put-char-code-property (char propname value)
1007   "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
1008 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
1009   (let ((plist (get-char-table char char-code-property-table)))
1010     (if plist
1011         (let ((slot (memq propname plist)))
1012           (if slot
1013               (setcar (cdr slot) value)
1014             (nconc plist (list propname value))))
1015       (put-char-table char (list propname value) char-code-property-table)
1016       )))
1017
1018 \f
1019 ;; Pretty description of encoded string
1020
1021 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
1022 ;; (defvar iso-2022-control-alist
1023 ;;   '((?\x1b . "ESC")
1024 ;;     (?\x0e . "SO")
1025 ;;     (?\x0f . "SI")
1026 ;;     (?\x8e . "SS2")
1027 ;;     (?\x8f . "SS3")
1028 ;;     (?\x9b . "CSI")))
1029
1030 ;; (defun encoded-string-description (str coding-system)
1031 ;;   "Return a pretty description of STR that is encoded by CODING-SYSTEM."
1032 ;;   (setq str (string-as-unibyte str))
1033 ;;   (let ((char (aref str 0))
1034 ;;         desc)
1035 ;;     (when (< char 128)
1036 ;;       (setq desc (or (cdr (assq char iso-2022-control-alist))
1037 ;;                      (char-to-string char)))
1038 ;;       (let ((i 1)
1039 ;;             (len (length str))) 
1040 ;;         (while (< i len)
1041 ;;           (setq char (aref str i))
1042 ;;           (if (>= char 128)
1043 ;;               (setq desc nil i len)
1044 ;;             (setq desc (concat desc " "
1045 ;;                                (or (cdr (assq char iso-2022-control-alist))
1046 ;;                                    (char-to-string char)))
1047 ;;                   i (1+ i))))))
1048 ;;     (or desc
1049 ;;         (mapconcat (function (lambda (x) (format "0x%02x" x))) str " "))))
1050
1051 ;; (defun encode-coding-char (char coding-system)
1052 ;;   "Encode CHAR by CODING-SYSTEM and return the resulting string.
1053 ;; If CODING-SYSTEM can't safely encode CHAR, return nil."
1054 ;;   (if (cmpcharp char)
1055 ;;       (setq char (car (decompose-composite-char char 'list))))
1056 ;;   (let ((str1 (char-to-string char))
1057 ;;         (str2 (make-string 2 char))
1058 ;;         (safe-charsets (and coding-system
1059 ;;                             (coding-system-get coding-system 'safe-charsets)))
1060 ;;         enc1 enc2 i1 i2)
1061 ;;     (when (or (eq safe-charsets t)
1062 ;;               (memq (char-charset char) safe-charsets))
1063 ;;       ;; We must find the encoded string of CHAR.  But, just encoding
1064 ;;       ;; CHAR will put extra control sequences (usually to designate
1065 ;;       ;; ASCII charset) at the tail if type of CODING is ISO 2022.
1066 ;;       ;; To exclude such tailing bytes, we at first encode one-char
1067 ;;       ;; string and two-char string, then check how many bytes at the
1068 ;;       ;; tail of both encoded strings are the same.
1069 ;; 
1070 ;;       (setq enc1 (string-as-unibyte (encode-coding-string str1 coding-system))
1071 ;;             i1 (length enc1)
1072 ;;             enc2 (string-as-unibyte (encode-coding-string str2 coding-system))
1073 ;;             i2 (length enc2))
1074 ;;       (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
1075 ;;         (setq i1 (1- i1) i2 (1- i2)))
1076 ;; 
1077 ;;       ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
1078 ;;       ;; and they are the extra control sequences at the tail to
1079 ;;       ;; exclude.
1080 ;;       (substring enc2 0 i2))))
1081
1082
1083 ;;; mule-cmds.el ends here