help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to automate file-opening (derived filename)?


From: Friedrich Dominicus
Subject: Re: How to automate file-opening (derived filename)?
Date: 07 Jan 2003 19:19:47 +0100
User-agent: Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.4 (Military Intelligence)

wildernesscat@hotmail.com (Danny Dorfman) writes:

> Hi!
> 
> I need to devise an e-lisp macro, that would do the following:
> Assume I am now looking at a file named "/a/b/c/ZZZ/d/e.cpp", I'd like
> the macro to open "/a/b/c/YYY/d/e.h" for me. The elements a,b,c,d,e
> are unknown in advance, while elements ZZZ and YYY are a constant part
> of the path.
> How do I do this? (My understanding of e-lisp is pretty basic).
I have changed the question slightly. I do not think that having a
separate source directory makes very much sense, because you wrote "I
am now looking at a file ..." That means you have an open buffer with
the contents of the file in it. Now what makes sense is having a
separate directory for header files. Anyway I do think for one project
you won't have diverse header directories but one. With that
assumptioins this is my shot

(let (hdr-dir)
  (defun load-proper-header-file (&optional buf-name)
    (interactive)
    (unless hdr-dir
      (setf hdr-dir (read-from-minibuffer "Header directory: "
                                          (default-directory))))
    (let ((file-name (file-name-sans-extension (if buf-name
                                                   buf-name 
                                                 (buffer-name)))))
      (find-file (concat hdr-dir file-name ".h")))))

Emacs Lisp is not lexically scoped therefor hdr-dir can be accesses
any time. So the base idea is that you just give it the Header
directory once. If you later decide it should be changed you just can
reset it with
(setf hdr-dir nil)

The next time you call load-proper-header-file you will get prompted
for the new Header directory, of course you could add something along
this lines

(defun set-hdr-dir (dir)
        (interactive "DHeader directory: ")
        (setf hdr-dir dir))

if you like

Now the next simplification I did was that I just drop the extension
and replace it with the ".h" prefix. dropping the extension is be done
with file-name-sans-extension building the new file name with
(concat ...

Because I was thinking the way you'll use it will work like this:
a) visit a C(PP) file
b) you than have that file in a buffer
c) you than want to load the appropriate header without getting
prompted about the name everytime you prefer just something along
M-x load-proper-header-file 
and the file-name is automatically constructed and loaded.

Now with (buffer-name) do I get the name of the current buffer, so
this is my "default" value. If that does not work you have to use
M-: (load-proper-headerfile "some-cpp-file-name")

Regards
Friedrich



reply via email to

[Prev in Thread] Current Thread [Next in Thread]