(imenu-buffer-menubar): Add defvar.
[emacs.git] / lisp / rlogin.el
blob15ed52b393de9e8b79f6178ea8c4ccc03832037a
1 ;;; rlogin.el --- remote login interface
3 ;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: Noah Friedman
6 ;; Maintainer: Noah Friedman <friedman@prep.ai.mit.edu>
7 ;; Keywords: unix, comm
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs 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 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs 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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;; $Id: rlogin.el,v 1.30 1995/10/27 22:34:50 friedman Exp erik $
28 ;;; Commentary:
30 ;; Support for remote logins using `rlogin'.
31 ;; This program is layered on top of shell.el; the code here only accounts
32 ;; for the variations needed to handle a remote process, e.g. directory
33 ;; tracking and the sending of some special characters.
35 ;; If you wish for rlogin mode to prompt you in the minibuffer for
36 ;; passwords when a password prompt appears, just enter m-x send-invisible
37 ;; and type in your line, or add `comint-watch-for-password-prompt' to
38 ;; `comint-output-filter-functions'.
40 ;;; Code:
42 (require 'comint)
43 (require 'shell)
45 (defvar rlogin-program "rlogin"
46 "*Name of program to invoke rlogin")
48 (defvar rlogin-explicit-args nil
49 "*List of arguments to pass to rlogin on the command line.")
51 (defvar rlogin-mode-hook nil
52 "*Hooks to run after setting current buffer to rlogin-mode.")
54 (defvar rlogin-process-connection-type nil
55 "*If non-`nil', use a pty for the local rlogin process.
56 If `nil', use a pipe (if pipes are supported on the local system).
58 Generally it is better not to waste ptys on systems which have a static
59 number of them. On the other hand, some implementations of `rlogin' assume
60 a pty is being used, and errors will result from using a pipe instead.")
62 (defvar rlogin-directory-tracking-mode 'local
63 "*Control whether and how to do directory tracking in an rlogin buffer.
65 nil means don't do directory tracking.
67 t means do so using an ftp remote file name.
69 Any other value means do directory tracking using local file names.
70 This works only if the remote machine and the local one
71 share the same directories (through NFS). This is the default.
73 This variable becomes local to a buffer when set in any fashion for it.
75 It is better to use the function of the same name to change the behavior of
76 directory tracking in an rlogin session once it has begun, rather than
77 simply setting this variable, since the function does the necessary
78 re-synching of directories.")
80 (make-variable-buffer-local 'rlogin-directory-tracking-mode)
82 (defvar rlogin-host nil
83 "*The name of the remote host. This variable is buffer-local.")
85 (defvar rlogin-remote-user nil
86 "*The username used on the remote host.
87 This variable is buffer-local and defaults to your local user name.
88 If rlogin is invoked with the `-l' option to specify the remote username,
89 this variable is set from that.")
91 ;; Initialize rlogin mode map.
92 (defvar rlogin-mode-map '())
93 (cond
94 ((null rlogin-mode-map)
95 (setq rlogin-mode-map (if (consp shell-mode-map)
96 (cons 'keymap shell-mode-map)
97 (copy-keymap shell-mode-map)))
98 (define-key rlogin-mode-map "\C-c\C-c" 'rlogin-send-Ctrl-C)
99 (define-key rlogin-mode-map "\C-c\C-d" 'rlogin-send-Ctrl-D)
100 (define-key rlogin-mode-map "\C-c\C-z" 'rlogin-send-Ctrl-Z)
101 (define-key rlogin-mode-map "\C-c\C-\\" 'rlogin-send-Ctrl-backslash)
102 (define-key rlogin-mode-map "\C-d" 'rlogin-delchar-or-send-Ctrl-D)
103 (define-key rlogin-mode-map "\C-i" 'rlogin-tab-or-complete)))
106 ;;;###autoload (add-hook 'same-window-regexps "^\\*rlogin-.*\\*\\(\\|<[0-9]+>\\)")
108 (defvar rlogin-history nil)
110 ;;;###autoload
111 (defun rlogin (input-args &optional buffer)
112 "Open a network login connection to HOST via the `rlogin' program.
113 Input is sent line-at-a-time to the remote connection.
115 Communication with the remote host is recorded in a buffer `*rlogin-HOST*'
116 \(or `*rlogin-USER@HOST*' if the remote username differs\).
117 If a prefix argument is given and the buffer `*rlogin-HOST*' already exists,
118 a new buffer with a different connection will be made.
120 When called from a program, if the optional second argument is a string or
121 buffer, it names the buffer to use.
123 The variable `rlogin-program' contains the name of the actual program to
124 run. It can be a relative or absolute path.
126 The variable `rlogin-explicit-args' is a list of arguments to give to
127 the rlogin when starting. They are added after any arguments given in
128 INPUT-ARGS.
130 If the default value of `rlogin-directory-tracking-mode' is t, then the
131 default directory in that buffer is set to a remote (FTP) file name to
132 access your home directory on the remote machine. Occasionally this causes
133 an error, if you cannot access the home directory on that machine. This
134 error is harmless as long as you don't try to use that default directory.
136 If `rlogin-directory-tracking-mode' is neither t nor nil, then the default
137 directory is initially set up to your (local) home directory.
138 This is useful if the remote machine and your local machine
139 share the same files via NFS. This is the default.
141 If you wish to change directory tracking styles during a session, use the
142 function `rlogin-directory-tracking-mode' rather than simply setting the
143 variable."
144 (interactive (list
145 (read-from-minibuffer "rlogin arguments (hostname first): "
146 nil nil nil 'rlogin-history)
147 current-prefix-arg))
149 (let* ((process-connection-type rlogin-process-connection-type)
150 (args (if rlogin-explicit-args
151 (append (rlogin-parse-words input-args)
152 rlogin-explicit-args)
153 (rlogin-parse-words input-args)))
154 (host (car args))
155 (user (or (car (cdr (member "-l" args)))
156 (user-login-name)))
157 (buffer-name (if (string= user (user-login-name))
158 (format "*rlogin-%s*" host)
159 (format "*rlogin-%s@%s*" user host)))
160 proc)
162 (cond ((null buffer))
163 ((stringp buffer)
164 (setq buffer-name buffer))
165 ((bufferp buffer)
166 (setq buffer-name (buffer-name buffer)))
167 ((numberp buffer)
168 (setq buffer-name (format "%s<%d>" buffer-name buffer)))
170 (setq buffer-name (generate-new-buffer-name buffer-name))))
172 (setq buffer (get-buffer-create buffer-name))
173 (pop-to-buffer buffer-name)
175 (cond
176 ((comint-check-proc buffer-name))
178 (comint-exec buffer buffer-name rlogin-program nil args)
179 (setq proc (get-buffer-process buffer))
180 ;; Set process-mark to point-max in case there is text in the
181 ;; buffer from a previous exited process.
182 (set-marker (process-mark proc) (point-max))
183 (rlogin-mode)
185 ;; comint-output-filter-functions is just like a hook, except that the
186 ;; functions in that list are passed arguments. add-hook serves well
187 ;; enough for modifying it.
188 (add-hook 'comint-output-filter-functions 'rlogin-carriage-filter)
190 (make-local-variable 'rlogin-host)
191 (setq rlogin-host host)
192 (make-local-variable 'rlogin-remote-user)
193 (setq rlogin-remote-user user)
195 (cond
196 ((eq rlogin-directory-tracking-mode t)
197 ;; Do this here, rather than calling the tracking mode function, to
198 ;; avoid a gratuitous resync check; the default should be the
199 ;; user's home directory, be it local or remote.
200 (setq comint-file-name-prefix
201 (concat "/" rlogin-remote-user "@" rlogin-host ":"))
202 (cd-absolute comint-file-name-prefix))
203 ((null rlogin-directory-tracking-mode))
205 (cd-absolute (concat comint-file-name-prefix "~/"))))))))
207 (defun rlogin-mode ()
208 "Set major-mode for rlogin sessions.
209 If `rlogin-mode-hook' is set, run it."
210 (interactive)
211 (kill-all-local-variables)
212 (shell-mode)
213 (setq major-mode 'rlogin-mode)
214 (setq mode-name "rlogin")
215 (use-local-map rlogin-mode-map)
216 (setq shell-dirtrackp rlogin-directory-tracking-mode)
217 (make-local-variable 'comint-file-name-prefix)
218 (run-hooks 'rlogin-mode-hook))
220 (defun rlogin-directory-tracking-mode (&optional prefix)
221 "Do remote or local directory tracking, or disable entirely.
223 If called with no prefix argument or a unspecified prefix argument (just
224 ``\\[universal-argument]'' with no number) do remote directory tracking via
225 ange-ftp. If called as a function, give it no argument.
227 If called with a negative prefix argument, disable directory tracking
228 entirely.
230 If called with a positive, numeric prefix argument, e.g.
231 ``\\[universal-argument] 1 M-x rlogin-directory-tracking-mode\'',
232 then do directory tracking but assume the remote filesystem is the same as
233 the local system. This only works in general if the remote machine and the
234 local one share the same directories (through NFS)."
235 (interactive "P")
236 (cond
237 ((or (null prefix)
238 (consp prefix))
239 (setq rlogin-directory-tracking-mode t)
240 (setq shell-dirtrackp t)
241 (setq comint-file-name-prefix
242 (concat "/" rlogin-remote-user "@" rlogin-host ":")))
243 ((< prefix 0)
244 (setq rlogin-directory-tracking-mode nil)
245 (setq shell-dirtrackp nil))
247 (setq rlogin-directory-tracking-mode 'local)
248 (setq comint-file-name-prefix "")
249 (setq shell-dirtrackp t)))
250 (cond
251 (shell-dirtrackp
252 (let* ((proc (get-buffer-process (current-buffer)))
253 (proc-mark (process-mark proc))
254 (current-input (buffer-substring proc-mark (point-max)))
255 (orig-point (point))
256 (offset (and (>= orig-point proc-mark)
257 (- (point-max) orig-point))))
258 (unwind-protect
259 (progn
260 (delete-region proc-mark (point-max))
261 (goto-char (point-max))
262 (shell-resync-dirs))
263 (goto-char proc-mark)
264 (insert current-input)
265 (if offset
266 (goto-char (- (point-max) offset))
267 (goto-char orig-point)))))))
270 ;; Parse a line into its constituent parts (words separated by
271 ;; whitespace). Return a list of the words.
272 (defun rlogin-parse-words (line)
273 (let ((list nil)
274 (posn 0)
275 (match-data (match-data)))
276 (while (string-match "[^ \t\n]+" line posn)
277 (setq list (cons (substring line (match-beginning 0) (match-end 0))
278 list))
279 (setq posn (match-end 0)))
280 (store-match-data (match-data))
281 (nreverse list)))
283 (defun rlogin-carriage-filter (string)
284 (let* ((point-marker (point-marker))
285 (end (process-mark (get-buffer-process (current-buffer))))
286 (beg (or (and (boundp 'comint-last-output-start)
287 comint-last-output-start)
288 (- end (length string)))))
289 (goto-char beg)
290 (while (search-forward "\C-m" end t)
291 (delete-char -1))
292 (goto-char point-marker)))
294 (defun rlogin-send-Ctrl-C ()
295 (interactive)
296 (send-string nil "\C-c"))
298 (defun rlogin-send-Ctrl-D ()
299 (interactive)
300 (send-string nil "\C-d"))
302 (defun rlogin-send-Ctrl-Z ()
303 (interactive)
304 (send-string nil "\C-z"))
306 (defun rlogin-send-Ctrl-backslash ()
307 (interactive)
308 (send-string nil "\C-\\"))
310 (defun rlogin-delchar-or-send-Ctrl-D (arg)
312 Delete ARG characters forward, or send a C-d to process if at end of buffer."
313 (interactive "p")
314 (if (eobp)
315 (rlogin-send-Ctrl-D)
316 (delete-char arg)))
318 (defun rlogin-tab-or-complete ()
319 "Complete file name if doing directory tracking, or just insert TAB."
320 (interactive)
321 (if rlogin-directory-tracking-mode
322 (comint-dynamic-complete)
323 (insert "\C-i")))
325 ;;; rlogin.el ends here