Fix typo on include guard for term.h
[sxemacs] / lisp / code-cmds.el
1 ;;; code-cmds.el --- Commands for manipulating coding systems..
2
3 ;; Copyright (C) 1995,1999 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5 ;; Copyright (C) 2000 Free Software Foundation
6 ;; Copyright (C) 1997 MORIOKA Tomohiko
7
8
9 ;; This file is part of SXEmacs.
10
11 ;; SXEmacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; SXEmacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;
25 ;; This code defines the keybindings and utility commands for the
26 ;; user to manipulate coding systems.
27 ;; This code used to be in mule-cmds.el which now only needs the
28 ;; additional bindings/commands that are avaible on the real mule.
29
30
31 ;;; Code:
32
33 ;;; Coding related key bindings and menus.
34
35 (defvar coding-keymap (make-sparse-keymap "Mule/Conding")
36   "Keymap for Mule and Coding cystem specific commands.")
37
38 ;; Keep "C-x C-m ..." for mule specific commands.
39 (define-key ctl-x-map "\C-m" coding-keymap)
40
41 (define-key coding-keymap "f" 'set-buffer-file-coding-system)
42 (define-key coding-keymap "F" 'set-default-buffer-file-coding-system) ; XEmacs
43 (define-key coding-keymap "t" 'set-terminal-coding-system)
44 (define-key coding-keymap "p" 'set-buffer-process-coding-system)
45 ;(define-key coding-keymap "x" 'set-selection-coding-system)
46 ;(define-key coding-keymap "X" 'set-next-selection-coding-system)
47 (define-key coding-keymap "c" 'universal-coding-system-argument)
48 ;;(define-key coding-keymap "c" 'list-coding-system-briefly) ; XEmacs
49 ;;(define-key coding-keymap "C" 'describe-coding-system)         ; XEmacs
50
51
52 (defun coding-system-change-eol-conversion (coding-system eol-type)
53   "Return a coding system which differs from CODING-SYSTEM in eol conversion.
54 The returned coding system converts end-of-line by EOL-TYPE
55 but text as the same way as CODING-SYSTEM.
56 EOL-TYPE should be `lf', `crlf', `cr' or nil.
57 If EOL-TYPE is nil, the returned coding system detects
58 how end-of-line is formatted automatically while decoding.
59
60 EOL-TYPE can be specified by an symbol `unix', `dos' or `mac'.
61 They means `lf', `crlf', and `cr' respectively."
62   (if (symbolp eol-type)
63       (setq eol-type (cond ((or (eq eol-type 'unix)
64                                 (eq eol-type 'lf))
65                             'eol-lf)
66                            ((or (eq eol-type 'dos)
67                                 (eq eol-type 'crlf))
68                             'eol-crlf)
69                            ((or (eq eol-type 'mac)
70                                 (eq eol-type 'cr))
71                             'eol-cr)
72                            (t eol-type))))
73   (let ((orig-eol-type (coding-system-eol-type coding-system)))
74     (if (null orig-eol-type)
75         (if (not eol-type)
76             coding-system
77           (coding-system-property coding-system eol-type))
78       (let ((base (coding-system-base coding-system)))
79         (if (not eol-type)
80             base
81           (if (= eol-type orig-eol-type)
82               coding-system
83             (setq orig-eol-type (coding-system-eol-type base))
84             (if (null orig-eol-type)
85                 (coding-system-property base eol-type))))))))
86
87
88 (defun universal-coding-system-argument ()
89   "Execute an I/O command using the specified coding system."
90   (interactive)
91   (let* ((default (and buffer-file-coding-system
92                        (not (eq (coding-system-type buffer-file-coding-system)
93                                 t))
94                        (coding-system-name buffer-file-coding-system)))
95          (coding-system
96           (read-coding-system
97            (if default
98                (format "Coding system for following command (default, %s): "
99                        default)
100              "Coding system for following command: ")
101            default))
102          (keyseq (read-key-sequence
103                   (format "Command to execute with %s:" coding-system)))
104          (cmd (key-binding keyseq)))
105     (let ((coding-system-for-read coding-system)
106           (coding-system-for-write coding-system))
107       (message "")
108       (call-interactively cmd))))
109
110 (defun set-default-coding-systems (coding-system)
111   "Set default value of various coding systems to CODING-SYSTEM.
112 This sets the following coding systems:
113   o coding system of a newly created buffer
114   o default coding system for terminal output
115   o default coding system for keyboard input
116   o default coding system for subprocess I/O
117   o default coding system for converting file names."
118   (check-coding-system coding-system)
119   ;;(setq-default buffer-file-coding-system coding-system)
120   (set-default-buffer-file-coding-system coding-system)
121   ;; (if default-enable-multibyte-characters
122   ;;     (setq default-file-name-coding-system coding-system))
123   ;; If coding-system is nil, honor that on MS-DOS as well, so
124   ;; that they could reset the terminal coding system.
125   ;; (unless (and (eq window-system 'pc) coding-system)
126   ;;   (setq default-terminal-coding-system coding-system))
127   (set-terminal-coding-system coding-system)
128   ;;(setq default-keyboard-coding-system coding-system)
129   (set-keyboard-coding-system coding-system)
130   ;;(setq default-process-coding-system (cons coding-system coding-system))
131   ;; Refer to coding-system-for-read and coding-system-for-write
132   ;; so that C-x RET c works.
133   (add-hook 'comint-exec-hook
134             `(lambda ()
135                (let ((proc (get-buffer-process (current-buffer))))
136                  (set-process-input-coding-system
137                   proc (or coding-system-for-read ',coding-system))
138                  (set-process-output-coding-system
139                   proc (or coding-system-for-write ',coding-system))))
140             'append)
141   (setq file-name-coding-system coding-system))
142
143 (defun prefer-coding-system (coding-system)
144   "Add CODING-SYSTEM at the front of the priority list for automatic detection.
145 This also sets the following coding systems:
146   o coding system of a newly created buffer
147   o default coding system for terminal output
148   o default coding system for keyboard input
149   o default coding system for converting file names.
150
151 If CODING-SYSTEM specifies a certain type of EOL conversion, the coding
152 systems set by this function will use that type of EOL conversion.
153
154 This command does not change the default value of terminal coding system
155 for MS-DOS terminal, because DOS terminals only support a single coding
156 system, and Emacs automatically sets the default to that coding system at
157 startup."
158   (interactive "zPrefer coding system: ")
159   (if (not (and coding-system (find-coding-system coding-system)))
160       (error "Invalid coding system `%s'" coding-system))
161   (let ((coding-category (coding-system-category coding-system))
162         (base (coding-system-base coding-system))
163         (eol-type (coding-system-eol-type coding-system)))
164     (if (not coding-category)
165         ;; CODING-SYSTEM is no-conversion or undecided.
166         (error "Can't prefer the coding system `%s'" coding-system))
167     (set-coding-category-system coding-category (or base coding-system))
168     ;; (update-coding-systems-internal)
169     (or (eq coding-category (car (coding-category-list)))
170         ;; We must change the order.
171         (set-coding-priority-list (list coding-category)))
172     (if (and base (interactive-p))
173         (message "Highest priority is set to %s (base of %s)"
174                  base coding-system))
175     ;; If they asked for specific EOL conversion, honor that.
176     (if (memq eol-type '(lf crlf mac))
177         (setq coding-system
178               (coding-system-change-eol-conversion base eol-type))
179       (setq coding-system base))
180     (set-default-coding-systems coding-system)))
181
182 ;;; Commands
183
184 (defun set-buffer-process-coding-system (decoding encoding)
185   "Set coding systems for the process associated with the current buffer.
186 DECODING is the coding system to be used to decode input from the process,
187 ENCODING is the coding system to be used to encode output to the process.
188
189 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]."
190   (interactive
191    "zCoding-system for process input: \nzCoding-system for process output: ")
192   (let ((proc (get-buffer-process (current-buffer))))
193     (if (null proc)
194         (error "no process")
195       (check-coding-system decoding)
196       (check-coding-system encoding)
197       (set-process-coding-system proc decoding encoding)))
198   (force-mode-line-update))
199
200 (provide 'code-cmds)
201
202 ;;; code-cmds.el ends here