[comp.emacs] notion of current directory

jpayne@rochester.ARPA (Jonathan Payne) (01/07/87)

Is the notion of current directory in GNU emacs PER BUFFER?  And is that
what gets changed when you do a cd?  Lots of times I want to find a file
in the current directory, namely the one in which I fired up emacs in the
first place.  But unless the file currently being visited is in that
directory, you have to retype the entire pathname (or whatever's
necessary).  You can't just say "./" and you can't delete the entire
supplied path name (say in find-file) and say "./" either.  What I really
want to do is find out what the current directory is - I don't care about
find-file.  But as far as I can tell, there's no way to find out what the
current directory is, short of running /bin/pwd into a buffer.  And I'm
not even sure if THAT will work (but it probably would).

Am I confused, or is there really no other way to do this?

mct@briar.UUCP (Mark C. Tucker) (01/07/87)

The notion of "current directory" *is* PER BUFFER.
The easiest way to find out what a buffer thinks is current is to
do "find-file", and look at EMACS's prompt.
If you don't like EMACS's idea, don't forget:
    You can just start typing a full path name without deleting what is
	already in the prompt buffer, and
    You can edit what's there (to get a near neighbor), or
    Start 'relativizing' the name with "../"

-- Mark Tucker

    

jr@ALEXANDER.BBN.COM (John Robinson) (01/07/87)

Some things that might help.

Therre is a getenv function; if your cwd is in your environment, it
would be easy to snarf in the .emacs.  But of course (pwd) tells you
this info anyway.

Another way is to use environment variables ($HOME, e.g.) in the
prompt for find file.  Since it expands to a directory name, you can
type, e.g., ^X ^F $ H O M E SPC, at which point the Find File: prompt
will show you your home abs path and let you type more.  If emacs is
given $CWD on startup, this should work to get the directory it was
started in.

/jr
jr@bbn.com or jr@bbnccv.uucp

Without life, there wouldn't be chemical companies.

nz@hotlg.UUCP (01/08/87)

In article <23621@rochester.ARPA> jpayne@rochester.ARPA (Jonathan Payne) writes:
 > 
 > Is the notion of current directory in GNU emacs PER BUFFER?  And is that
 > what gets changed when you do a cd?  

In the version of GNU I have (17.65) and in all the other version I have
used, it is definitely on a per-buffer basis.  If you look in the source
file ``buffer.h'', and look at the members of struct buffer, you will see
that there is an entry for the directory.  

 > Lots of times I want to find a file
 > in the current directory, namely the one in which I fired up emacs in the
 > first place.  But unless the file currently being visited is in that
 > directory, you have to retype the entire pathname (or whatever's
 > necessary). 

If you never delete the buffer '*scratch*' it will have the same current
directory as the one you started GNU from.  You can then define an Elisp
function to get the old cwd.  Actually, maintaining a shell or env variable
might be easier, but this way stays in Elisp.  On SysV, the variable PWD
always points to the shell's notion of your current /bin/pwd.  Anyhow,
this function returns a string ...

(defun old-default-directory ()
	"get the old . assuming the *scratch* still exists"
	(let ((scrtch (get-buffer "*scratch*")))
	     (if (null scrtch)
		(message "sorry, no *scratch* buffer")
		(set-buffer scrtch)
		default-directory)))
	
... that you can use as follows

	ESC ESC (find-file (concat (old-default-directory) "foo.c"))


 >  You can't just say "./" and you can't delete the entire
 > supplied path name (say in find-file) and say "./" either.  

Deleting the supplied path and saying ./ seems to work fine for me.
However, if you do a M-X cd or any other such command, ./ then points
to that new directory.

 > What I really
 > want to do is find out what the current directory is - I don't care about
 > find-file.  But as far as I can tell, there's no way to find out what the
 > current directory is, short of running /bin/pwd into a buffer.  And I'm
 > not even sure if THAT will work (but it probably would).

Actually, it wouldn't.  When you start up a shell buffer, it inherits
the default directory from whatever buffer you were in when you started
it up.  

 > Am I confused, or is there really no other way to do this?

Now that you have read my who article, the reward is an easy way to do
this.  In your .emacs file, put the following lines of Elisp code.

	(defvar startup-default-directory default-directory
		"default directory GNU invocation time.")

Now that you have an Elisp variable set to the directory
you want, the possibilities are BOUNDLESS!

There are lots of other ways to approach this.  For instance, you
must know that a leading ~ always expands to the rooted full pathname 
of your home directory.  There are other tricks, too, like recursive 
use of the minibuffer, in-line Elisp code evaluation, etc...


-- 
...nz  (Neal Ziring  @  ATT-BL Holmdel, x9229, 2E-315a)
	"You can fit an infinite number of wires into this junction box,
	but we usually don't go that far in practice."
					London Electric Co. Worker, 1880s

ardis@wanginst.EDU (Mark A. Ardis) (01/08/87)

>... Lots of times I want to find a file
>in the current directory, namely the one in which I fired up emacs in the
>first place...
>...What I really
>want to do is find out what the current directory is - I don't care about
>find-file....

You might find it useful to have a shell window active, so that you
can list the files in *its* current directory.  Try using some of the
following when appropriate:

M-x shell          (To create a shell window.)
^X 4 f file-name   (To visit the file in another window.)
^X 4 b *shell*     (To get the shell window back if it is not present.)
^X 1               (To delete other windows.)
^X 0               (To delete the current window.)

I usually fire up a shell in the morning and keep it active all day
long.  I change directory from the *shell* buffer with "cd" whenever
my focus of attention changes.  That way, I can use "ls" and "grep",
etc. to look for files.  Invoking "find-file" from the *shell* buffer
will give you the prefix you expect.
-- 
Mark A. Ardis                           ardis%wanginst@CSNet-Relay (CSNet)
Wang Institute of Graduate Studies       ...!decvax!wanginst!ardis (UUCP)
Tyng Road, Tyngsboro, MA 01879          (617) 649-9731