[comp.emacs] Automatic cd for M-x shell

sane@cs.uiuc.edu (Aamod Sane) (11/27/90)

Whenever I pop up M-x shell from within a buffer, I would like
to cd to the current directory of the buffer. Now, when I start
up, only then the buffer directory is consulted.

But a existing shell is not sent cd to current buffer directory
while coming up using M-x shell.

How can I fix this?

Thanks

Aamod Sane

PS: something even more desirable is to do the above only if the
buffer is called *shell*, so that I can have a *fixed-shell* which is
not sent the cd. But I do not seem to use the *fixed-shell* type stuff
anyway.
-- 
sane@cs.uiuc.edu
         ==         / \  
-----    ==    *    \_/     -|||- 
         ==     

piet@cs.ruu.nl (Piet van Oostrum) (11/30/90)

>>>>> In message <1990Nov26.210601.13376@julius.cs.uiuc.edu>, sane@cs.uiuc.edu (Aamod Sane) (AS) writes:

AS> Whenever I pop up M-x shell from within a buffer, I would like
AS> to cd to the current directory of the buffer. Now, when I start
AS> up, only then the buffer directory is consulted.

AS> But a existing shell is not sent cd to current buffer directory
AS> while coming up using M-x shell.

This may not be exactly what you ask for, but it is what I use: A
shell-like command that creates a shell per directory where you call it. I
hate cd-ing all over the place because (1) I get lost, (2) Usually I am
doing unrelated things in different directories.

The following mode creates a shell buffer in the directory of the current
buffer, or it prompts for a directory if you give a prefix argument. If a
shell already is running for this directory it pops to the existing one.
The name of the shell and the directory name are remembered in the buffer
name. 

NOTE: I use this with the cmushell mode. If you want to use it with the
traditional shell mode, you will have to make some slight modifications.

Use it with (autoload 'sh "sh" "Shell mode with buffer per directory" t)
---------------------------- sh.el ----------------------------------------
(require 'cmushell)
(defun sh (dir)
  "Run an inferior shell.
The shell runs in and the buffer name reflects DIRECTORY (default current
working directory). Prompts for directory name if interactively called
with prefix arg.
The buffer is put in cmushell-mode, giving commands for sending input
and controlling the subjobs of the shell.  See cmushell-mode.
See also variable comint-prompt-regexp.
Note that many people's .cshrc files unconditionally clear the prompt.
If yours does, you will probably want to change it."
  (interactive (list
		(if current-prefix-arg
		    (read-file-name "sh in dir: " nil default-directory t))))
  (setq dir (file-name-as-directory
	     (expand-file-name (or dir default-directory))))
  (let* ((prog (or explicit-shell-file-name
		   (getenv "ESHELL")
		   (getenv "SHELL")
		   "/bin/sh"))		     
	 (name (file-name-nondirectory prog))
	 (bufname (concat name "-" dir))
	 (startfile (concat "~/.emacs_" name))
	 (xargs-name (intern-soft (concat "explicit-" name "-args"))))
    (switch-to-buffer (get-buffer-create (concat "*" bufname "*")))
    (setq default-directory dir)
    (apply 'make-comint bufname
	   name
	   (if (file-exists-p startfile) startfile)
	   (if (and xargs-name (boundp xargs-name))
	       (symbol-value xargs-name)
	     '("-i"))))
  (cmushell-mode))
------------------------------------------------------------------------
-- 
Piet* van Oostrum, Dept of Computer Science, Utrecht University,
Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands.
Telephone: +31 30 531806   Uucp:   uunet!mcsun!ruuinf!piet
Telefax:   +31 30 513791   Internet:  piet@cs.ruu.nl   (*`Pete')