annotate lisp/shell.el @ 244:d6439d1d5d02

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