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