annotate lisp/=old-shell.el @ 6402:33dec33f382d

(telnet-maximum-count): Doc fix.
author Karl Heuer <kwzh@gnu.org>
date Fri, 18 Mar 1994 05:54:56 +0000
parents 2c7997f249eb
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2247
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1 ;;; old-shell.el --- run a shell in an Emacs window
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
2
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
3 ;; Copyright (C) 1985, 1986, 1987, 1990 Free Software Foundation, Inc.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
4
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
5 ;; Keywords: processes
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
6
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
7 ;; This file is part of GNU Emacs.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
8
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
10 ;; it under the terms of the GNU General Public License as published by
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
11 ;; the Free Software Foundation; either version 2, or (at your option)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
12 ;; any later version.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
13
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
14 ;; GNU Emacs is distributed in the hope that it will be useful,
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
17 ;; GNU General Public License for more details.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
18
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
19 ;; You should have received a copy of the GNU General Public License
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
22
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
23 ;;; Hacked from tea.el and shell.el by Olin Shivers (shivers@cs.cmu.edu). 8/88
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
24
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
25 ;;; Since this mode is built on top of the general command-interpreter-in-
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
26 ;;; a-buffer mode (comint mode), it shares a common base functionality,
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
27 ;;; and a common set of bindings, with all modes derived from comint mode.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
28
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
29 ;;; For documentation on the functionality provided by comint mode, and
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
30 ;;; the hooks available for customising it, see the file comint.el.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
31
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
32 ;;; Needs fixin:
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
33 ;;; When sending text from a source file to a subprocess, the process-mark can
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
34 ;;; move off the window, so you can lose sight of the process interactions.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
35 ;;; Maybe I should ensure the process mark is in the window when I send
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
36 ;;; text to the process? Switch selectable?
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
37
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
38 ;;; Code:
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
39
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
40 (require 'comint)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
41 (defvar shell-popd-regexp "popd"
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
42 "*Regexp to match subshell commands equivalent to popd.")
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
43
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
44 (defvar shell-pushd-regexp "pushd"
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
45 "*Regexp to match subshell commands equivalent to pushd.")
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
46
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
47 (defvar shell-cd-regexp "cd"
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
48 "*Regexp to match subshell commands equivalent to cd.")
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
49
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
50 (defvar explicit-shell-file-name nil
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
51 "*If non-nil, is file name to use for explicitly requested inferior shell.")
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
52
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
53 (defvar explicit-csh-args
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
54 (if (eq system-type 'hpux)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
55 ;; -T persuades HP's csh not to think it is smarter
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
56 ;; than us about what terminal modes to use.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
57 '("-i" "-T")
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
58 '("-i"))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
59 "*Args passed to inferior shell by M-x shell, if the shell is csh.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
60 Value is a list of strings, which may be nil.")
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
61
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
62 (defvar shell-dirstack nil
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
63 "List of directories saved by pushd in this buffer's shell.")
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
64
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
65 (defvar shell-dirstack-query "dirs"
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
66 "Command used by shell-resync-dirlist to query shell.")
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
67
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
68 (defvar shell-mode-map ())
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
69 (cond ((not shell-mode-map)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
70 (setq shell-mode-map (copy-keymap comint-mode-map))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
71 (define-key shell-mode-map "\t" 'comint-dynamic-complete)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
72 (define-key shell-mode-map "\M-?" 'comint-dynamic-list-completions)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
73
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
74 (defvar shell-mode-hook '()
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
75 "*Hook for customising shell mode")
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
76
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
77
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
78 ;;; Basic Procedures
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
79 ;;; ===========================================================================
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
80 ;;;
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
81
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
82 (defun shell-mode ()
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
83 "Major mode for interacting with an inferior shell.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
84 Return after the end of the process' output sends the text from the
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
85 end of process to the end of the current line.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
86 Return before end of process output copies rest of line to end (skipping
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
87 the prompt) and sends it.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
88 M-x send-invisible reads a line of text without echoing it, and sends it to
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
89 the shell.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
90
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
91 If you accidentally suspend your process, use \\[comint-continue-subjob]
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
92 to continue it.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
93
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
94 cd, pushd and popd commands given to the shell are watched by Emacs to keep
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
95 this buffer's default directory the same as the shell's working directory.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
96 M-x dirs queries the shell and resyncs Emacs' idea of what the current
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
97 directory stack is.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
98 M-x dirtrack-toggle turns directory tracking on and off.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
99
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
100 \\{shell-mode-map}
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
101 Customisation: Entry to this mode runs the hooks on comint-mode-hook and
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
102 shell-mode-hook (in that order).
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
103
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
104 Variables shell-cd-regexp, shell-pushd-regexp and shell-popd-regexp are used
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
105 to match their respective commands."
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
106 (interactive)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
107 (comint-mode)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
108 (setq major-mode 'shell-mode
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
109 mode-name "Shell"
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
110 comint-prompt-regexp shell-prompt-pattern
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
111 comint-input-sentinel 'shell-directory-tracker)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
112 (use-local-map shell-mode-map)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
113 (make-local-variable 'shell-dirstack)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
114 (set (make-local-variable 'shell-dirtrackp) t)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
115 (run-hooks 'shell-mode-hook))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
116
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
117
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
118 (defun shell ()
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
119 "Run an inferior shell, with I/O through buffer *shell*.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
120 If buffer exists but shell process is not running, make new shell.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
121 If buffer exists and shell process is running, just switch to buffer *shell*.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
122
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
123 The shell to use comes from the first non-nil variable found from these:
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
124 explicit-shell-file-name in Emacs, ESHELL in the environment or SHELL in the
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
125 environment. If none is found, /bin/sh is used.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
126
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
127 If a file ~/.emacs_SHELLNAME exists, it is given as initial input, simulating
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
128 a start-up file for the shell like .profile or .cshrc. Note that this may
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
129 lose due to a timing error if the shell discards input when it starts up.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
130
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
131 The buffer is put in shell-mode, giving commands for sending input
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
132 and controlling the subjobs of the shell.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
133
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
134 The shell file name, sans directories, is used to make a symbol name
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
135 such as `explicit-csh-arguments'. If that symbol is a variable,
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
136 its value is used as a list of arguments when invoking the shell.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
137 Otherwise, one argument `-i' is passed to the shell.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
138
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
139 \(Type \\[describe-mode] in the shell buffer for a list of commands.)"
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
140 (interactive)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
141 (if (not (comint-check-proc "*shell*"))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
142 (let* ((prog (or explicit-shell-file-name
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
143 (getenv "ESHELL")
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
144 (getenv "SHELL")
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
145 "/bin/sh"))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
146 (name (file-name-nondirectory prog))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
147 (startfile (concat "~/.emacs_" name))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
148 (xargs-name (intern-soft (concat "explicit-" name "-args"))))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
149 (set-buffer (apply 'make-comint "shell" prog
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
150 (if (file-exists-p startfile) startfile)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
151 (if (and xargs-name (boundp xargs-name))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
152 (symbol-value xargs-name)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
153 '("-i"))))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
154 (shell-mode)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
155 (switch-to-buffer "*shell*"))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
156
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
157
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
158 ;;; Directory tracking
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
159 ;;; ===========================================================================
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
160 ;;; This code provides the shell mode input sentinel
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
161 ;;; SHELL-DIRECTORY-TRACKER
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
162 ;;; that tracks cd, pushd, and popd commands issued to the shell, and
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
163 ;;; changes the current directory of the shell buffer accordingly.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
164 ;;;
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
165 ;;; This is basically a fragile hack, although it's more accurate than
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
166 ;;; the original version in shell.el. It has the following failings:
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
167 ;;; 1. It doesn't know about the cdpath shell variable.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
168 ;;; 2. It only spots the first command in a command sequence. E.g., it will
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
169 ;;; miss the cd in "ls; cd foo"
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
170 ;;; 3. More generally, any complex command (like ";" sequencing) is going to
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
171 ;;; throw it. Otherwise, you'd have to build an entire shell interpreter in
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
172 ;;; emacs lisp. Failing that, there's no way to catch shell commands where
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
173 ;;; cd's are buried inside conditional expressions, aliases, and so forth.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
174 ;;;
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
175 ;;; The whole approach is a crock. Shell aliases mess it up. File sourcing
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
176 ;;; messes it up. You run other processes under the shell; these each have
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
177 ;;; separate working directories, and some have commands for manipulating
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
178 ;;; their w.d.'s (e.g., the lcd command in ftp). Some of these programs have
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
179 ;;; commands that do *not* effect the current w.d. at all, but look like they
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
180 ;;; do (e.g., the cd command in ftp). In shells that allow you job
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
181 ;;; control, you can switch between jobs, all having different w.d.'s. So
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
182 ;;; simply saying %3 can shift your w.d..
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
183 ;;;
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
184 ;;; The solution is to relax, not stress out about it, and settle for
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
185 ;;; a hack that works pretty well in typical circumstances. Remember
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
186 ;;; that a half-assed solution is more in keeping with the spirit of Unix,
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
187 ;;; anyway. Blech.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
188 ;;;
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
189 ;;; One good hack not implemented here for users of programmable shells
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
190 ;;; is to program up the shell w.d. manipulation commands to output
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
191 ;;; a coded command sequence to the tty. Something like
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
192 ;;; ESC | <cwd> |
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
193 ;;; where <cwd> is the new current working directory. Then trash the
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
194 ;;; directory tracking machinery currently used in this package, and
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
195 ;;; replace it with a process filter that watches for and strips out
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
196 ;;; these messages.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
197
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
198 ;;; REGEXP is a regular expression. STR is a string. START is a fixnum.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
199 ;;; Returns T if REGEXP matches STR where the match is anchored to start
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
200 ;;; at position START in STR. Sort of like LOOKING-AT for strings.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
201 (defun shell-front-match (regexp str start)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
202 (eq start (string-match regexp str start)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
203
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
204 (defun shell-directory-tracker (str)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
205 "Tracks cd, pushd and popd commands issued to the shell.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
206 This function is called on each input passed to the shell.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
207 It watches for cd, pushd and popd commands and sets the buffer's
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
208 default directory to track these commands.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
209
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
210 You may toggle this tracking on and off with M-x dirtrack-toggle.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
211 If emacs gets confused, you can resync with the shell with M-x dirs.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
212
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
213 See variables shell-cd-regexp, shell-pushd-regexp, and shell-popd-regexp.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
214 Environment variables are expanded, see function substitute-in-file-name."
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
215 (condition-case err
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
216 (cond (shell-dirtrackp
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
217 (string-match "^\\s *" str) ; skip whitespace
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
218 (let ((bos (match-end 0))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
219 (x nil))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
220 (cond ((setq x (shell-match-cmd-w/optional-arg shell-popd-regexp
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
221 str bos))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
222 (shell-process-popd (substitute-in-file-name x)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
223 ((setq x (shell-match-cmd-w/optional-arg shell-pushd-regexp
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
224 str bos))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
225 (shell-process-pushd (substitute-in-file-name x)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
226 ((setq x (shell-match-cmd-w/optional-arg shell-cd-regexp
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
227 str bos))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
228 (shell-process-cd (substitute-in-file-name x)))))))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
229 (error (message (car (cdr err))))))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
230
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
231
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
232 ;;; Try to match regexp CMD to string, anchored at position START.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
233 ;;; CMD may be followed by a single argument. If a match, then return
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
234 ;;; the argument, if there is one, or the empty string if not. If
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
235 ;;; no match, return nil.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
236
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
237 (defun shell-match-cmd-w/optional-arg (cmd str start)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
238 (and (shell-front-match cmd str start)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
239 (let ((eoc (match-end 0))) ; end of command
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
240 (cond ((shell-front-match "\\s *\\(\;\\|$\\)" str eoc)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
241 "") ; no arg
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
242 ((shell-front-match "\\s +\\([^ \t\;]+\\)\\s *\\(\;\\|$\\)"
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
243 str eoc)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
244 (substring str (match-beginning 1) (match-end 1))) ; arg
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
245 (t nil))))) ; something else.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
246 ;;; The first regexp is [optional whitespace, (";" or the end of string)].
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
247 ;;; The second regexp is [whitespace, (an arg), optional whitespace,
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
248 ;;; (";" or end of string)].
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
249
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
250
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
251 ;;; popd [+n]
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
252 (defun shell-process-popd (arg)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
253 (let ((num (if (zerop (length arg)) 0 ; no arg means +0
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
254 (shell-extract-num arg))))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
255 (if (and num (< num (length shell-dirstack)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
256 (if (= num 0) ; condition-case because the CD could lose.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
257 (condition-case nil (progn (cd (car shell-dirstack))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
258 (setq shell-dirstack
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
259 (cdr shell-dirstack))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
260 (shell-dirstack-message))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
261 (error (message "Couldn't cd.")))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
262 (let* ((ds (cons nil shell-dirstack))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
263 (cell (nthcdr (- num 1) ds)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
264 (rplacd cell (cdr (cdr cell)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
265 (setq shell-dirstack (cdr ds))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
266 (shell-dirstack-message)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
267 (message "Bad popd."))))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
268
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
269
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
270 ;;; cd [dir]
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
271 (defun shell-process-cd (arg)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
272 (condition-case nil (progn (cd (if (zerop (length arg)) (getenv "HOME")
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
273 arg))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
274 (shell-dirstack-message))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
275 (error (message "Couldn't cd."))))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
276
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
277
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
278 ;;; pushd [+n | dir]
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
279 (defun shell-process-pushd (arg)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
280 (if (zerop (length arg))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
281 ;; no arg -- swap pwd and car of shell stack
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
282 (condition-case nil (if shell-dirstack
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
283 (let ((old default-directory))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
284 (cd (car shell-dirstack))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
285 (setq shell-dirstack
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
286 (cons old (cdr shell-dirstack)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
287 (shell-dirstack-message))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
288 (message "Directory stack empty."))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
289 (message "Couldn't cd."))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
290
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
291 (let ((num (shell-extract-num arg)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
292 (if num ; pushd +n
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
293 (if (> num (length shell-dirstack))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
294 (message "Directory stack not that deep.")
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
295 (let* ((ds (cons default-directory shell-dirstack))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
296 (dslen (length ds))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
297 (front (nthcdr num ds))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
298 (back (reverse (nthcdr (- dslen num) (reverse ds))))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
299 (new-ds (append front back)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
300 (condition-case nil
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
301 (progn (cd (car new-ds))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
302 (setq shell-dirstack (cdr new-ds))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
303 (shell-dirstack-message))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
304 (error (message "Couldn't cd.")))))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
305
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
306 ;; pushd <dir>
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
307 (let ((old-wd default-directory))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
308 (condition-case nil
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
309 (progn (cd arg)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
310 (setq shell-dirstack
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
311 (cons old-wd shell-dirstack))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
312 (shell-dirstack-message))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
313 (error (message "Couldn't cd."))))))))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
314
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
315 ;; If STR is of the form +n, for n>0, return n. Otherwise, nil.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
316 (defun shell-extract-num (str)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
317 (and (string-match "^\\+[1-9][0-9]*$" str)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
318 (string-to-int str)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
319
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
320
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
321 (defun shell-dirtrack-toggle ()
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
322 "Turn directory tracking on and off in a shell buffer."
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
323 (interactive)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
324 (setq shell-dirtrackp (not shell-dirtrackp))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
325 (message "directory tracking %s."
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
326 (if shell-dirtrackp "ON" "OFF")))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
327
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
328 ;;; For your typing convenience:
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
329 (fset 'dirtrack-toggle 'shell-dirtrack-toggle)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
330
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
331
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
332 (defun shell-resync-dirs ()
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
333 "Resync the buffer's idea of the current directory stack.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
334 This command queries the shell with the command bound to
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
335 shell-dirstack-query (default \"dirs\"), reads the next
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
336 line output and parses it to form the new directory stack.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
337 DON'T issue this command unless the buffer is at a shell prompt.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
338 Also, note that if some other subprocess decides to do output
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
339 immediately after the query, its output will be taken as the
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
340 new directory stack -- you lose. If this happens, just do the
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
341 command again."
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
342 (interactive)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
343 (let* ((proc (get-buffer-process (current-buffer)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
344 (pmark (process-mark proc)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
345 (goto-char pmark)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
346 (insert shell-dirstack-query) (insert "\n")
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
347 (sit-for 0) ; force redisplay
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
348 (comint-send-string proc shell-dirstack-query)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
349 (comint-send-string proc "\n")
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
350 (set-marker pmark (point))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
351 (let ((pt (point))) ; wait for 1 line
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
352 ;; This extra newline prevents the user's pending input from spoofing us.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
353 (insert "\n") (backward-char 1)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
354 (while (not (looking-at ".+\n"))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
355 (accept-process-output proc)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
356 (goto-char pt)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
357 (goto-char pmark) (delete-char 1) ; remove the extra newline
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
358 ;; That's the dirlist. grab it & parse it.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
359 (let* ((dl (buffer-substring (match-beginning 0) (- (match-end 0) 1)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
360 (dl-len (length dl))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
361 (ds '()) ; new dir stack
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
362 (i 0))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
363 (while (< i dl-len)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
364 ;; regexp = optional whitespace, (non-whitespace), optional whitespace
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
365 (string-match "\\s *\\(\\S +\\)\\s *" dl i) ; pick off next dir
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
366 (setq ds (cons (substring dl (match-beginning 1) (match-end 1))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
367 ds))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
368 (setq i (match-end 0)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
369 (let ((ds (reverse ds)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
370 (condition-case nil
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
371 (progn (cd (car ds))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
372 (setq shell-dirstack (cdr ds))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
373 (shell-dirstack-message))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
374 (error (message "Couldn't cd.")))))))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
375
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
376 ;;; For your typing convenience:
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
377 (fset 'dirs 'shell-resync-dirs)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
378
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
379
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
380 ;;; Show the current dirstack on the message line.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
381 ;;; Pretty up dirs a bit by changing "/usr/jqr/foo" to "~/foo".
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
382 ;;; (This isn't necessary if the dirlisting is generated with a simple "dirs".)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
383 ;;; All the commands that mung the buffer's dirstack finish by calling
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
384 ;;; this guy.
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
385 (defun shell-dirstack-message ()
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
386 (let ((msg "")
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
387 (ds (cons default-directory shell-dirstack)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
388 (while ds
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
389 (let ((dir (car ds)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
390 (if (string-match (format "^%s\\(/\\|$\\)" (getenv "HOME")) dir)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
391 (setq dir (concat "~/" (substring dir (match-end 0)))))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
392 (if (string-equal dir "~/") (setq dir "~"))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
393 (setq msg (concat msg dir " "))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
394 (setq ds (cdr ds))))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
395 (message msg)))
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
396
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
397 (provide 'shell)
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
398
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
399 ;;; old-shell.el ends here