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