annotate lisp/eterm/tshell.el @ 167:85ec50267440 r20-3b10

Import from CVS: tag r20-3b10
author cvs
date Mon, 13 Aug 2007 09:45:46 +0200
parents cf808b4c4290
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; tshell.el --- specialized term.el for running the shell.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;; Copyright (C) 1988, 1993, 1994 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; Author: Per Bothner <bothner@cygnus.com>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;; Original comint version author: Olin Shivers <shivers@cs.cmu.edu>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;; Comint version maintainer: Simon Marshall <s.marshall@dcs.hull.ac.uk>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;; Keywords: processes
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;; This file is part of GNU Emacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;; it under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;; GNU Emacs is distributed in the hope that it will be useful,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 ;; GNU General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 ;;; Commentary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 ;;; Please send me bug reports, bug fixes, and extensions, so that I can
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;;; merge them into the master source.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;;; - Olin Shivers (shivers@cs.cmu.edu)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;;; - Simon Marshall (s.marshall@dcs.hull.ac.uk)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 ;;; This file defines a a shell-in-a-buffer package (shell mode) built
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 ;;; on top of term mode. This is actually cmushell with things
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;;; renamed to replace its counterpart in Emacs 18. cmushell is more
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;;; featureful, robust, and uniform than the Emacs 18 version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;;; Since this mode is built on top of the general command-interpreter-in-
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 ;;; a-buffer mode (term mode), it shares a common base functionality,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 ;;; and a common set of bindings, with all modes derived from term mode.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ;;; This makes these modes easier to use.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 ;;; For documentation on the functionality provided by term mode, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 ;;; the hooks available for customising it, see the file term.el.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 ;;; For further information on shell mode, see the comments below.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 ;;; Needs fixin:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 ;;; When sending text from a source file to a subprocess, the process-mark can
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 ;;; move off the window, so you can lose sight of the process interactions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 ;;; Maybe I should ensure the process mark is in the window when I send
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 ;;; text to the process? Switch selectable?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 ;; YOUR .EMACS FILE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 ;;=============================================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 ;; Some suggestions for your .emacs file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 ;; ;; Define C-c t to run my favorite command in shell mode:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 ;; (setq tshell-mode-hook
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 ;; '((lambda ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 ;; (define-key tshell-mode-map "\C-ct" 'favorite-cmd))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 ;;; Brief Command Documentation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 ;;;============================================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 ;;; Term Mode Commands: (common to tshell and all term-derived modes)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 ;;; m-p term-previous-input Cycle backwards in input history
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 ;;; m-n term-next-input Cycle forwards
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 ;;; m-r term-previous-matching-input Previous input matching a regexp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 ;;; m-R term-previous-matching-input-from-input -"- matching input
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 ;;; m-s term-next-matching-input Next input that matches
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 ;;; m-S term-next-matching-input-from-input -"- matching input
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 ;;; m-c-l term-show-output Show last batch of process output
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 ;;; return term-send-input
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 ;;; c-c c-a term-bol Beginning of line; skip prompt
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 ;;; c-d term-delchar-or-maybe-eof Delete char unless at end of buff.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 ;;; c-c c-u term-kill-input ^u
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 ;;; c-c c-w backward-kill-word ^w
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 ;;; c-c c-c term-interrupt-subjob ^c
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 ;;; c-c c-z term-stop-subjob ^z
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 ;;; c-c c-\ term-quit-subjob ^\
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 ;;; c-c c-o term-kill-output Delete last batch of process output
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 ;;; c-c c-r term-show-output Show last batch of process output
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 ;;; c-c c-h term-dynamic-list-input-ring List input history
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 ;;; term-send-invisible Read line w/o echo & send to proc
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 ;;; term-continue-subjob Useful if you accidentally suspend
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 ;;; top-level job
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 ;;; term-mode-hook is the term mode hook.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 ;;; Tshell Mode Commands:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 ;;; tshell Fires up the shell process
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 ;;; tab term-dynamic-complete Complete filename/command/history
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 ;;; m-? term-dynamic-list-filename-completions List completions in help buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 ;;; m-c-f tshell-forward-command Forward a shell command
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 ;;; m-c-b tshell-backward-command Backward a shell command
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 ;;; dirs Resync the buffer's dir stack
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 ;;; dirtrack-toggle Turn dir tracking on/off
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 ;;; tshell-strip-ctrl-m Remove trailing ^Ms from output
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 ;;; The tshell mode hook is tshell-mode-hook
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 ;;; term-prompt-regexp is initialised to tshell-prompt-pattern, for backwards
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 ;;; compatibility.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 ;;; Read the rest of this file for more information.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 ;;; SHELL.EL COMPATIBILITY
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 ;;; Notes from when this was called cmushell, and was not the standard emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 ;;; shell package.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 ;;;============================================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 ;;; In brief: this package should have no trouble coexisting with shell.el.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 ;;; Most customising variables -- e.g., explicit-shell-file-name -- are the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 ;;; same, so the users shouldn't have much trouble. Hooks have different
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 ;;; names, however, so you can customise tshell mode differently from cmushell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 ;;; mode. You basically just have to remember to type M-x cmushell instead of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 ;;; M-x shell.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 ;;; It would be nice if this file was completely plug-compatible with the old
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 ;;; shell package -- if you could just name this file shell.el, and have it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 ;;; transparently replace the old one. But you can't. Several other packages
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 ;;; (tex-mode, background, dbx, gdb, kermit, monkey, prolog, telnet) are also
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 ;;; clients of shell mode. These packages assume detailed knowledge of shell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 ;;; mode internals in ways that are incompatible with cmushell mode (mostly
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 ;;; because of cmushell mode's greater functionality). So, unless we are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 ;;; willing to port all of these packages, we can't have this file be a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 ;;; complete replacement for shell.el -- that is, we can't name this file
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 ;;; shell.el, and its main entry point (shell), because dbx.el will break
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 ;;; when it loads it in and tries to use it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 ;;; There are two ways to fix this. One: rewrite these other modes to use the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 ;;; new package. This is a win, but can't be assumed. The other, backwards
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 ;;; compatible route, is to make this package non-conflict with shell.el, so
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 ;;; both files can be loaded in at the same time. And *that* is why some
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 ;;; functions and variables have different names: (cmushell),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 ;;; cmushell-mode-map, that sort of thing. All the names have been carefully
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 ;;; chosen so that shell.el and cmushell.el won't tromp on each other.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 ;;; Customization and Buffer Variables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 ;;; ===========================================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 ;;; Code:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 (require 'term)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 (defvar tshell-prompt-pattern "^[^#$%>\n]*[#$%>] *"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 "Regexp to match prompts in the inferior shell.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 Defaults to \"^[^#$%>\\n]*[#$%>] *\", which works pretty well.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 This variable is used to initialise `term-prompt-regexp' in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 shell buffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 The pattern should probably not match more than one line. If it does,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 tshell-mode may become confused trying to distinguish prompt from input
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 on lines which don't start with a prompt.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 This is a fine thing to set in your `.emacs' file.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 (defvar tshell-completion-fignore nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 "*List of suffixes to be disregarded during file/command completion.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 This variable is used to initialize `term-completion-fignore' in the shell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 buffer. The default is nil, for compatibility with most shells.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 Some people like (\"~\" \"#\" \"%\").
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 This is a fine thing to set in your `.emacs' file.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 (defvar tshell-delimiter-argument-list '(?\| ?& ?< ?> ?\( ?\) ?\;)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 "List of characters to recognise as separate arguments.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 This variable is used to initialize `term-delimiter-argument-list' in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 shell buffer. The default is (?\\| ?& ?< ?> ?\\( ?\\) ?\\;).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 This is a fine thing to set in your `.emacs' file.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 (defvar tshell-dynamic-complete-functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 '(term-replace-by-expanded-history
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 tshell-dynamic-complete-environment-variable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 tshell-dynamic-complete-command
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 tshell-replace-by-expanded-directory
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 term-dynamic-complete-filename)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 "List of functions called to perform completion.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 This variable is used to initialise `term-dynamic-complete-functions' in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 shell buffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 This is a fine thing to set in your `.emacs' file.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 (defvar shell-command-regexp "[^;&|\n]+"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 "*Regexp to match a single command within a pipeline.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 This is used for directory tracking and does not do a perfect job.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 (defvar shell-completion-execonly t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 "*If non-nil, use executable files only for completion candidates.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 This mirrors the optional behavior of tcsh.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 Detecting executability of files may slow command completion considerably.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 (defvar shell-popd-regexp "popd"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 "*Regexp to match subshell commands equivalent to popd.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 (defvar shell-pushd-regexp "pushd"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 "*Regexp to match subshell commands equivalent to pushd.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 (defvar shell-pushd-tohome nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 "*If non-nil, make pushd with no arg behave as \"pushd ~\" (like cd).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 This mirrors the optional behavior of tcsh.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 (defvar shell-pushd-dextract nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 "*If non-nil, make \"pushd +n\" pop the nth dir to the stack top.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 This mirrors the optional behavior of tcsh.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 (defvar shell-pushd-dunique nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 "*If non-nil, make pushd only add unique directories to the stack.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 This mirrors the optional behavior of tcsh.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 (defvar shell-cd-regexp "cd"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 "*Regexp to match subshell commands equivalent to cd.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 ;; explicit-shell-file-name nil is in term.el.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 (defvar explicit-csh-args
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 (if (eq system-type 'hpux)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 ;; -T persuades HP's csh not to think it is smarter
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 ;; than us about what terminal modes to use.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 '("-i" "-T")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 '("-i"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 "*Args passed to inferior shell by M-x tshell, if the shell is csh.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 Value is a list of strings, which may be nil.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 (defvar tshell-input-autoexpand 'history
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 "*If non-nil, expand input command history references on completion.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 This mirrors the optional behavior of tcsh (its autoexpand and histlit).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 If the value is `input', then the expansion is seen on input.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 If the value is `history', then the expansion is only when inserting
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 into the buffer's input ring. See also `term-magic-space' and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 `term-dynamic-complete'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 This variable supplies a default for `term-input-autoexpand',
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 for Tshell mode only.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 (defvar tshell-dirstack nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 "List of directories saved by pushd in this buffer's shell.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 Thus, this does not include the shell's current directory.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 (defvar tshell-dirtrackp t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 "Non-nil in a shell buffer means directory tracking is enabled.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 (defvar tshell-last-dir nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 "Keep track of last directory for ksh `cd -' command.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 (defvar tshell-dirstack-query nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 "Command used by `tshell-resync-dir' to query the shell.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 (defvar tshell-mode-map nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 (cond ((not tshell-mode-map)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 (setq tshell-mode-map (copy-keymap term-mode-map))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 (define-key tshell-mode-map "\C-c\C-f" 'tshell-forward-command)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 (define-key tshell-mode-map "\C-c\C-b" 'tshell-backward-command)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 (define-key tshell-mode-map "\t" 'term-dynamic-complete)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 (define-key tshell-mode-map "\M-?"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 'term-dynamic-list-filename-completions)
104
cf808b4c4290 Import from CVS: tag r20-1b4
cvs
parents: 70
diff changeset
261 ;;; XEmacs change [JTL]: We don't have define-key-after
cf808b4c4290 Import from CVS: tag r20-1b4
cvs
parents: 70
diff changeset
262 ;;; (and we don't need it ...)
cf808b4c4290 Import from CVS: tag r20-1b4
cvs
parents: 70
diff changeset
263 ;;; (define-key-after (lookup-key tshell-mode-map [menu-bar completion])
cf808b4c4290 Import from CVS: tag r20-1b4
cvs
parents: 70
diff changeset
264 ;;; [complete-env-variable] '("Complete Env. Variable Name" .
cf808b4c4290 Import from CVS: tag r20-1b4
cvs
parents: 70
diff changeset
265 ;;; tshell-dynamic-complete-environment-variable)
cf808b4c4290 Import from CVS: tag r20-1b4
cvs
parents: 70
diff changeset
266 ;;; 'complete-file)
cf808b4c4290 Import from CVS: tag r20-1b4
cvs
parents: 70
diff changeset
267 ;;; (define-key-after (lookup-key tshell-mode-map [menu-bar completion])
cf808b4c4290 Import from CVS: tag r20-1b4
cvs
parents: 70
diff changeset
268 ;;; [expand-directory] '("Expand Directory Reference" .
cf808b4c4290 Import from CVS: tag r20-1b4
cvs
parents: 70
diff changeset
269 ;;; tshell-replace-by-expanded-directory)
cf808b4c4290 Import from CVS: tag r20-1b4
cvs
parents: 70
diff changeset
270 ;;; 'complete-expand)
cf808b4c4290 Import from CVS: tag r20-1b4
cvs
parents: 70
diff changeset
271 ))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 (defvar tshell-mode-hook '()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 "*Hook for customising Tshell mode.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 ;;; Basic Procedures
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 ;;; ===========================================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 (defun tshell-mode ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 "Major mode for interacting with an inferior shell.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 Return after the end of the process' output sends the text from the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 end of process to the end of the current line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 Return before end of process output copies the current line (except
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 for the prompt) to the end of the buffer and sends it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 M-x term-send-invisible reads a line of text without echoing it,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 and sends it to the shell. This is useful for entering passwords.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 If you accidentally suspend your process, use \\[term-continue-subjob]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 to continue it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 cd, pushd and popd commands given to the shell are watched by Emacs to keep
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 this buffer's default directory the same as the shell's working directory.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 M-x dirs queries the shell and resyncs Emacs' idea of what the current
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 directory stack is.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 M-x dirtrack-toggle turns directory tracking on and off.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 \\{tshell-mode-map}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 Customization: Entry to this mode runs the hooks on `term-mode-hook' and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 `tshell-mode-hook' (in that order). Before each input, the hooks on
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 `term-input-filter-functions' are run.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 Variables `shell-cd-regexp', `shell-pushd-regexp' and `shell-popd-regexp'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 are used to match their respective commands, while `shell-pushd-tohome',
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 `shell-pushd-dextract' and `shell-pushd-dunique' control the behavior of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 relevant command.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 Variables `term-completion-autolist', `term-completion-addsuffix',
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 `term-completion-recexact' and `term-completion-fignore' control the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 behavior of file name, command name and variable name completion. Variable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 `shell-completion-execonly' controls the behavior of command name completion.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 Variable `tshell-completion-fignore' is used to initialise the value of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 `term-completion-fignore'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 Variables `term-input-ring-file-name' and `term-input-autoexpand' control
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 the initialisation of the input ring history, and history expansion.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 Variables `term-output-filter-functions', a hook, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 `term-scroll-to-bottom-on-input' and `term-scroll-to-bottom-on-output'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 control whether input and output cause the window to scroll to the end of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 buffer."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 (term-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 (setq major-mode 'tshell-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 (setq mode-name "Shell")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 (use-local-map tshell-mode-map)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 (setq term-prompt-regexp tshell-prompt-pattern)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 (setq term-completion-fignore tshell-completion-fignore)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 (setq term-delimiter-argument-list tshell-delimiter-argument-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 (setq term-dynamic-complete-functions tshell-dynamic-complete-functions)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 (make-local-variable 'paragraph-start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 (setq paragraph-start term-prompt-regexp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 (make-local-variable 'tshell-dirstack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 (setq tshell-dirstack nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 (setq tshell-last-dir nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 (make-local-variable 'tshell-dirtrackp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 (setq tshell-dirtrackp t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 (add-hook 'term-input-filter-functions 'tshell-directory-tracker)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 (setq term-input-autoexpand tshell-input-autoexpand)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 ;; shell-dependent assignments.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 (let ((shell (file-name-nondirectory (car
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 (process-command (get-buffer-process (current-buffer)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 (setq term-input-ring-file-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 (or (getenv "HISTFILE")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 (cond ((string-equal shell "bash") "~/.bash_history")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 ((string-equal shell "ksh") "~/.sh_history")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 (t "~/.history"))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 (if (equal term-input-ring-file-name "/dev/null")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 (setq term-input-ring-file-name nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 (setq tshell-dirstack-query
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 (if (string-match "^k?sh$" shell) "pwd" "dirs")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 (run-hooks 'tshell-mode-hook)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 (term-read-input-ring t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 (defun tshell ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 "Run an inferior shell, with I/O through buffer *shell*.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 If buffer exists but shell process is not running, make new shell.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 If buffer exists and shell process is running, just switch to buffer `*shell*'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 Program used comes from variable `explicit-shell-file-name',
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 or (if that is nil) from the ESHELL environment variable,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 or else from SHELL if there is no ESHELL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 If a file `~/.emacs_SHELLNAME' exists, it is given as initial input
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 (Note that this may lose due to a timing error if the shell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 discards input when it starts up.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 The buffer is put in Tshell mode, giving commands for sending input
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 and controlling the subjobs of the shell. See `tshell-mode'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 See also the variable `tshell-prompt-pattern'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 The shell file name (sans directories) is used to make a symbol name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 such as `explicit-csh-args'. If that symbol is a variable,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 its value is used as a list of arguments when invoking the shell.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 Otherwise, one argument `-i' is passed to the shell.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 \(Type \\[describe-mode] in the shell buffer for a list of commands.)"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 (if (not (term-check-proc "*shell*"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 (let* ((prog (or explicit-shell-file-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 (getenv "ESHELL")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 (getenv "SHELL")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 "/bin/sh"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 (name (file-name-nondirectory prog))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 (startfile (concat "~/.emacs_" name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 (xargs-name (intern-soft (concat "explicit-" name "-args"))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 (set-buffer (apply 'make-term "shell" prog
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 (if (file-exists-p startfile) startfile)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 (if (and xargs-name (boundp xargs-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 (symbol-value xargs-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 '("-i"))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 (tshell-mode)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 (switch-to-buffer "*shell*"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 ;;; Directory tracking
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 ;;; ===========================================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 ;;; This code provides the tshell mode input sentinel
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 ;;; TSHELL-DIRECTORY-TRACKER
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 ;;; that tracks cd, pushd, and popd commands issued to the tshell, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 ;;; changes the current directory of the tshell buffer accordingly.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 ;;; A better mechanism is now available:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 ;;; The standard term process filter supports a special escape command
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 ;;; \032 / <cwd> \n
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 ;;; that the inferior can use to tell emacs what the current working
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 ;;; directory is.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 ;;; All the inferior has to do is something like:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 ;;; printf("\032/%s\n", PWD);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 ;;; Most modern shells can be programmed to emit this string easily.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 ;;; Hopefully, bash (at least) will be modified to do this automatically.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 ;;; So all this horrible directory-tracking machinary is now obsolete,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 ;;; but is kept at least until the standard GNU shells are modified
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 ;;; This is basically a fragile hack, although it's more accurate than
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 ;;; the version in Emacs 18's shell.el. It has the following failings:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 ;;; 1. It doesn't know about the cdpath shell variable.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 ;;; 2. It cannot infallibly deal with command sequences, though it does well
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 ;;; with these and with ignoring commands forked in another shell with ()s.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 ;;; 3. More generally, any complex command is going to throw it. Otherwise,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 ;;; you'd have to build an entire shell interpreter in emacs lisp. Failing
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 ;;; that, there's no way to catch shell commands where cd's are buried
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 ;;; inside conditional expressions, aliases, and so forth.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 ;;; The whole approach is a crock. Shell aliases mess it up. File sourcing
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 ;;; messes it up. You run other processes under the shell; these each have
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 ;;; separate working directories, and some have commands for manipulating
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 ;;; their w.d.'s (e.g., the lcd command in ftp). Some of these programs have
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 ;;; commands that do *not* affect the current w.d. at all, but look like they
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 ;;; do (e.g., the cd command in ftp). In shells that allow you job
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 ;;; control, you can switch between jobs, all having different w.d.'s. So
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 ;;; simply saying %3 can shift your w.d..
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 ;;; The solution is to relax, not stress out about it, and settle for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 ;;; a hack that works pretty well in typical circumstances. Remember
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 ;;; that a half-assed solution is more in keeping with the spirit of Unix,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 ;;; anyway. Blech.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 (defun tshell-directory-tracker (str)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 "Tracks cd, pushd and popd commands issued to the shell.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 This function is called on each input passed to the shell.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 It watches for cd, pushd and popd commands and sets the buffer's
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 default directory to track these commands.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 You may toggle this tracking on and off with M-x dirtrack-toggle.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 If emacs gets confused, you can resync with the shell with M-x dirs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 See variables `shell-cd-regexp', `shell-pushd-regexp', and `shell-popd-regexp',
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 while `shell-pushd-tohome', `shell-pushd-dextract' and `shell-pushd-dunique'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 control the behavior of the relevant command.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 Environment variables are expanded, see function `substitute-in-file-name'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 (if tshell-dirtrackp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 ;; We fail gracefully if we think the command will fail in the shell.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 (condition-case chdir-failure
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 (let ((start (progn (string-match "^[;\\s ]*" str) ; skip whitespace
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 (match-end 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 end cmd arg1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 (while (string-match shell-command-regexp str start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 (setq end (match-end 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 cmd (term-arguments (substring str start end) 0 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 arg1 (term-arguments (substring str start end) 1 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 (cond ((eq (string-match shell-popd-regexp cmd) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 (tshell-process-popd (substitute-in-file-name arg1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 ((eq (string-match shell-pushd-regexp cmd) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 (tshell-process-pushd (substitute-in-file-name arg1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 ((eq (string-match shell-cd-regexp cmd) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 (tshell-process-cd (substitute-in-file-name arg1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 (setq start (progn (string-match "[;\\s ]*" str end) ; skip again
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 (match-end 0)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 (error "Couldn't cd"))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 ;;; popd [+n]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 (defun tshell-process-popd (arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 (let ((num (or (tshell-extract-num arg) 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 (cond ((and num (= num 0) tshell-dirstack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 (cd (car tshell-dirstack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 (setq tshell-dirstack (cdr tshell-dirstack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 (tshell-dirstack-message))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 ((and num (> num 0) (<= num (length tshell-dirstack)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 (let* ((ds (cons nil tshell-dirstack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 (cell (nthcdr (1- num) ds)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 (rplacd cell (cdr (cdr cell)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 (setq tshell-dirstack (cdr ds))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 (tshell-dirstack-message)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 (error "Couldn't popd")))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 ;; Return DIR prefixed with term-file-name-prefix as appropriate.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 (defun tshell-prefixed-directory-name (dir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 (if (= (length term-file-name-prefix) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 dir
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 (if (file-name-absolute-p dir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 ;; The name is absolute, so prepend the prefix.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 (concat term-file-name-prefix dir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 ;; For a relative name we assume default-directory already has the prefix.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 (expand-file-name dir))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 ;;; cd [dir]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 (defun tshell-process-cd (arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 (let ((new-dir (cond ((zerop (length arg)) (concat term-file-name-prefix
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 "~"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 ((string-equal "-" arg) tshell-last-dir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 (t (tshell-prefixed-directory-name arg)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 (setq tshell-last-dir default-directory)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 (cd new-dir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506 (tshell-dirstack-message)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 ;;; pushd [+n | dir]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 (defun tshell-process-pushd (arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 (let ((num (tshell-extract-num arg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 (cond ((zerop (length arg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 ;; no arg -- swap pwd and car of stack unless shell-pushd-tohome
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 (cond (shell-pushd-tohome
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 (shell-process-pushd (concat term-file-name-prefix "~")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 (tshell-dirstack
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 (let ((old default-directory))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 (cd (car tshell-dirstack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518 (setq tshell-dirstack
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 (cons old (cdr tshell-dirstack)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 (tshell-dirstack-message)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522 (message "Directory stack empty."))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 ((numberp num)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 ;; pushd +n
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 (cond ((> num (length tshell-dirstack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526 (message "Directory stack not that deep."))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 ((= num 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 (error (message "Couldn't cd.")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 (shell-pushd-dextract
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530 (let ((dir (nth (1- num) tshell-dirstack)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 (tshell-process-popd arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532 (tshell-process-pushd default-directory)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533 (cd dir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 (tshell-dirstack-message)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 (let* ((ds (cons default-directory tshell-dirstack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 (dslen (length ds))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
538 (front (nthcdr num ds))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539 (back (reverse (nthcdr (- dslen num) (reverse ds))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 (new-ds (append front back)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 (cd (car new-ds))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 (setq tshell-dirstack (cdr new-ds))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543 (tshell-dirstack-message)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 ;; pushd <dir>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546 (let ((old-wd default-directory))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 (cd (tshell-prefixed-directory-name arg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 (if (or (null shell-pushd-dunique)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549 (not (member old-wd tshell-dirstack)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550 (setq tshell-dirstack (cons old-wd tshell-dirstack)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551 (tshell-dirstack-message))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
553 ;; If STR is of the form +n, for n>0, return n. Otherwise, nil.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554 (defun tshell-extract-num (str)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555 (and (string-match "^\\+[1-9][0-9]*$" str)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556 (string-to-int str)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
559 (defun tshell-dirtrack-toggle ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 "Turn directory tracking on and off in a shell buffer."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
561 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
562 (setq tshell-dirtrackp (not tshell-dirtrackp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
563 (message "Directory tracking %s" (if tshell-dirtrackp "ON" "OFF")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
564
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565 ;;; For your typing convenience:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
566 (defalias 'dirtrack-toggle 'tshell-dirtrack-toggle)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
567
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
568
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
569 (defun tshell-resync-dirs ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570 "Resync the buffer's idea of the current directory stack.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571 This command queries the shell with the command bound to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
572 `tshell-dirstack-query' (default \"dirs\"), reads the next
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
573 line output and parses it to form the new directory stack.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
574 DON'T issue this command unless the buffer is at a shell prompt.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
575 Also, note that if some other subprocess decides to do output
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
576 immediately after the query, its output will be taken as the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
577 new directory stack -- you lose. If this happens, just do the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
578 command again."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
579 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
580 (let* ((proc (get-buffer-process (current-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
581 (pmark (process-mark proc)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
582 (goto-char pmark)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
583 (insert tshell-dirstack-query) (insert "\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
584 (sit-for 0) ; force redisplay
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
585 (term-send-string proc tshell-dirstack-query)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586 (term-send-string proc "\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
587 (set-marker pmark (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
588 (let ((pt (point))) ; wait for 1 line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
589 ;; This extra newline prevents the user's pending input from spoofing us.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
590 (insert "\n") (backward-char 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
591 (while (not (looking-at ".+\n"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592 (accept-process-output proc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593 (goto-char pt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594 (goto-char pmark) (delete-char 1) ; remove the extra newline
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 ;; That's the dirlist. grab it & parse it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
596 (let* ((dl (buffer-substring (match-beginning 0) (1- (match-end 0))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
597 (dl-len (length dl))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
598 (ds '()) ; new dir stack
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
599 (i 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
600 (while (< i dl-len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
601 ;; regexp = optional whitespace, (non-whitespace), optional whitespace
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
602 (string-match "\\s *\\(\\S +\\)\\s *" dl i) ; pick off next dir
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
603 (setq ds (cons (concat term-file-name-prefix
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
604 (substring dl (match-beginning 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
605 (match-end 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
606 ds))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
607 (setq i (match-end 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
608 (let ((ds (nreverse ds)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
609 (condition-case nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
610 (progn (cd (car ds))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
611 (setq tshell-dirstack (cdr ds))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
612 (tshell-dirstack-message))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
613 (error (message "Couldn't cd.")))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
614
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
615 ;;; For your typing convenience:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
616 (defalias 'dirs 'tshell-resync-dirs)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
617
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
618
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
619 ;;; Show the current dirstack on the message line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
620 ;;; Pretty up dirs a bit by changing "/usr/jqr/foo" to "~/foo".
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
621 ;;; (This isn't necessary if the dirlisting is generated with a simple "dirs".)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
622 ;;; All the commands that mung the buffer's dirstack finish by calling
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
623 ;;; this guy.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
624 (defun tshell-dirstack-message ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
625 (let* ((msg "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
626 (ds (cons default-directory tshell-dirstack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
627 (home (expand-file-name (concat term-file-name-prefix "~/")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
628 (homelen (length home)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
629 (while ds
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
630 (let ((dir (car ds)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
631 (and (>= (length dir) homelen) (string= home (substring dir 0 homelen))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
632 (setq dir (concat "~/" (substring dir homelen))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
633 ;; Strip off term-file-name-prefix if present.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
634 (and term-file-name-prefix
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
635 (>= (length dir) (length term-file-name-prefix))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
636 (string= term-file-name-prefix
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
637 (substring dir 0 (length term-file-name-prefix)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
638 (setq dir (substring dir (length term-file-name-prefix)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
639 (setcar ds dir))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
640 (setq msg (concat msg (directory-file-name dir) " "))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
641 (setq ds (cdr ds))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
642 (message msg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
643
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
644 (defun tshell-forward-command (&optional arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
645 "Move forward across ARG shell command(s). Does not cross lines.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
646 See `shell-command-regexp'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
647 (interactive "p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
648 (let ((limit (save-excursion (end-of-line nil) (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
649 (if (re-search-forward (concat shell-command-regexp "\\([;&|][\t ]*\\)+")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
650 limit 'move arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
651 (skip-syntax-backward " "))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
652
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
653
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
654 (defun tshell-backward-command (&optional arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
655 "Move backward across ARG shell command(s). Does not cross lines.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
656 See `shell-command-regexp'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
657 (interactive "p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
658 (let ((limit (save-excursion (term-bol nil) (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
659 (if (> limit (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
660 (save-excursion (beginning-of-line) (setq limit (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
661 (skip-syntax-backward " " limit)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
662 (if (re-search-backward
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
663 (format "[;&|]+[\t ]*\\(%s\\)" shell-command-regexp) limit 'move arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
664 (progn (goto-char (match-beginning 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
665 (skip-chars-forward ";&|")))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
666
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
667
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
668 (defun tshell-dynamic-complete-command ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
669 "Dynamically complete the command at point.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
670 This function is similar to `term-dynamic-complete-filename', except that it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
671 searches `exec-path' (minus the trailing emacs library path) for completion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
672 candidates. Note that this may not be the same as the shell's idea of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
673 path.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
674
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
675 Completion is dependent on the value of `shell-completion-execonly', plus
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
676 those that effect file completion. See `tshell-dynamic-complete-as-command'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
677
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
678 Returns t if successful."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
679 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
680 (let ((filename (term-match-partial-filename)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
681 (if (and filename
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
682 (save-match-data (not (string-match "[~/]" filename)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
683 (eq (match-beginning 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
684 (save-excursion (tshell-backward-command 1) (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
685 (prog2 (message "Completing command name...")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
686 (tshell-dynamic-complete-as-command)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
687
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
688
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
689 (defun tshell-dynamic-complete-as-command ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
690 "Dynamically complete at point as a command.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
691 See `tshell-dynamic-complete-filename'. Returns t if successful."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
692 (let* ((filename (or (term-match-partial-filename) ""))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
693 (pathnondir (file-name-nondirectory filename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
694 (paths (cdr (reverse exec-path)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
695 (cwd (file-name-as-directory (expand-file-name default-directory)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
696 (ignored-extensions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
697 (and term-completion-fignore
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
698 (mapconcat (function (lambda (x) (concat (regexp-quote x) "$")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
699 term-completion-fignore "\\|")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
700 (path "") (comps-in-path ()) (file "") (filepath "") (completions ()))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
701 ;; Go thru each path in the search path, finding completions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
702 (while paths
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
703 (setq path (file-name-as-directory (term-directory (or (car paths) ".")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
704 comps-in-path (and (file-accessible-directory-p path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
705 (file-name-all-completions pathnondir path)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
706 ;; Go thru each completion found, to see whether it should be used.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
707 (while comps-in-path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
708 (setq file (car comps-in-path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
709 filepath (concat path file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
710 (if (and (not (member file completions))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
711 (not (and ignored-extensions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
712 (string-match ignored-extensions file)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
713 (or (string-equal path cwd)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
714 (not (file-directory-p filepath)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
715 (or (null shell-completion-execonly)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
716 (file-executable-p filepath)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
717 (setq completions (cons file completions)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
718 (setq comps-in-path (cdr comps-in-path)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
719 (setq paths (cdr paths)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
720 ;; OK, we've got a list of completions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
721 (let ((success (let ((term-completion-addsuffix nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
722 (term-dynamic-simple-complete pathnondir completions))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
723 (if (and (memq success '(sole shortest)) term-completion-addsuffix
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
724 (not (file-directory-p (term-match-partial-filename))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
725 (insert " "))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
726 success)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
727
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
728
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
729 (defun tshell-match-partial-variable ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
730 "Return the variable at point, or nil if non is found."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
731 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
732 (let ((limit (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
733 (if (re-search-backward "[^A-Za-z0-9_{}]" nil 'move)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
734 (or (looking-at "\\$") (forward-char 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
735 ;; Anchor the search forwards.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
736 (if (or (eolp) (looking-at "[^A-Za-z0-9_{}$]"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
737 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
738 (re-search-forward "\\$?{?[A-Za-z0-9_]*}?" limit)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
739 (buffer-substring (match-beginning 0) (match-end 0))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
740
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
741
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
742 (defun tshell-dynamic-complete-environment-variable ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
743 "Dynamically complete the environment variable at point.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
744 Completes if after a variable, i.e., if it starts with a \"$\".
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
745 See `tshell-dynamic-complete-as-environment-variable'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
746
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
747 This function is similar to `term-dynamic-complete-filename', except that it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
748 searches `process-environment' for completion candidates. Note that this may
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
749 not be the same as the interpreter's idea of variable names. The main problem
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
750 with this type of completion is that `process-environment' is the environment
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
751 which Emacs started with. Emacs does not track changes to the environment made
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
752 by the interpreter. Perhaps it would be more accurate if this function was
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
753 called `tshell-dynamic-complete-process-environment-variable'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
754
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
755 Returns non-nil if successful."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
756 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
757 (let ((variable (tshell-match-partial-variable)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
758 (if (and variable (string-match "^\\$" variable))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
759 (prog2 (message "Completing variable name...")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
760 (tshell-dynamic-complete-as-environment-variable)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
761
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
762
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
763 (defun tshell-dynamic-complete-as-environment-variable ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
764 "Dynamically complete at point as an environment variable.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
765 Used by `tshell-dynamic-complete-environment-variable'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
766 Uses `term-dynamic-simple-complete'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
767 (let* ((var (or (tshell-match-partial-variable) ""))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
768 (variable (substring var (or (string-match "[^$({]\\|$" var) 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
769 (variables (mapcar (function (lambda (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
770 (substring x 0 (string-match "=" x))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
771 process-environment))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
772 (addsuffix term-completion-addsuffix)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
773 (term-completion-addsuffix nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
774 (success (term-dynamic-simple-complete variable variables)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
775 (if (memq success '(sole shortest))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
776 (let* ((var (tshell-match-partial-variable))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
777 (variable (substring var (string-match "[^$({]" var)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
778 (protection (cond ((string-match "{" var) "}")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
779 ((string-match "(" var) ")")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
780 (t "")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
781 (suffix (cond ((null addsuffix) "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
782 ((file-directory-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
783 (term-directory (getenv variable))) "/")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
784 (t " "))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
785 (insert protection suffix)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
786 success))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
787
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
788
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
789 (defun tshell-replace-by-expanded-directory ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
790 "Expand directory stack reference before point.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
791 Directory stack references are of the form \"=digit\" or \"=-\".
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
792 See `default-directory' and `tshell-dirstack'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
793
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
794 Returns t if successful."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
795 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
796 (if (term-match-partial-filename)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
797 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
798 (goto-char (match-beginning 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
799 (let ((stack (cons default-directory tshell-dirstack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
800 (index (cond ((looking-at "=-/?")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
801 (length tshell-dirstack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
802 ((looking-at "=\\([0-9]+\\)")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
803 (string-to-number
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
804 (buffer-substring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
805 (match-beginning 1) (match-end 1)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
806 (cond ((null index)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
807 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
808 ((>= index (length stack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
809 (error "Directory stack not that deep."))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
810 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
811 (replace-match (file-name-as-directory (nth index stack)) t t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
812 (message "Directory item: %d" index)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
813 t))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
814
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
815 (provide 'tshell)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
816
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
817 ;;; tshell.el ends here