* How to automate file-opening (derived filename)?
@ 2003-01-07 14:52 Danny Dorfman
2003-01-07 18:19 ` Friedrich Dominicus
0 siblings, 1 reply; 9+ messages in thread
From: Danny Dorfman @ 2003-01-07 14:52 UTC (permalink / raw)
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 am using GNU Emacs 21.2.1.
Regards,
Danny
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: How to automate file-opening (derived filename)?
2003-01-07 14:52 How to automate file-opening (derived filename)? Danny Dorfman
@ 2003-01-07 18:19 ` Friedrich Dominicus
2003-01-08 6:03 ` Alfred M. Szmidt
` (3 more replies)
0 siblings, 4 replies; 9+ messages in thread
From: Friedrich Dominicus @ 2003-01-07 18:19 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: How to automate file-opening (derived filename)?
2003-01-07 18:19 ` Friedrich Dominicus
@ 2003-01-08 6:03 ` Alfred M. Szmidt
[not found] ` <mailman.53.1042007441.21513.help-gnu-emacs@gnu.org>
` (2 subsequent siblings)
3 siblings, 0 replies; 9+ messages in thread
From: Alfred M. Szmidt @ 2003-01-08 6:03 UTC (permalink / raw)
Cc: help-gnu-emacs
Maybe you are looking for ff-find-other-file?
(ff-find-other-file &optional IN-OTHER-WINDOW IGNORE-INCLUDE)
Find the header or source file corresponding to this file.
Being on a `#include' line pulls in that file.
If optional IN-OTHER-WINDOW is non-nil, find the file in the other window.
If optional IGNORE-INCLUDE is non-nil, ignore being on `#include' lines.
...
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: How to automate file-opening (derived filename)?
[not found] ` <mailman.53.1042007441.21513.help-gnu-emacs@gnu.org>
@ 2003-01-08 11:54 ` Danny Dorfman
2003-01-11 12:49 ` Kai Großjohann
0 siblings, 1 reply; 9+ messages in thread
From: Danny Dorfman @ 2003-01-08 11:54 UTC (permalink / raw)
This is not suitable, since my #include files are located using a
different path, than the one suggested by Emacs. Our tree structure
looks like this:
/a/b/c/src/module/file.cpp +
/a/b/c/inc/module/file.h
Maybe I _do_ need some filename manipulation after all?
Danny
"Alfred M. Szmidt" <ams@kemisten.nu> wrote in message news:<mailman.53.1042007441.21513.help-gnu-emacs@gnu.org>...
> Maybe you are looking for ff-find-other-file?
>
> (ff-find-other-file &optional IN-OTHER-WINDOW IGNORE-INCLUDE)
>
> Find the header or source file corresponding to this file.
> Being on a `#include' line pulls in that file.
>
> If optional IN-OTHER-WINDOW is non-nil, find the file in the other window.
> If optional IGNORE-INCLUDE is non-nil, ignore being on `#include' lines.
> ...
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: How to automate file-opening (derived filename)?
2003-01-07 18:19 ` Friedrich Dominicus
2003-01-08 6:03 ` Alfred M. Szmidt
[not found] ` <mailman.53.1042007441.21513.help-gnu-emacs@gnu.org>
@ 2003-01-08 13:05 ` Danny Dorfman
2003-01-09 6:39 ` Friedrich Dominicus
2003-01-09 6:44 ` Danny Dorfman
3 siblings, 1 reply; 9+ messages in thread
From: Danny Dorfman @ 2003-01-08 13:05 UTC (permalink / raw)
The thing is, there is no ONE header directory. There are several
header directories, and it would be impractical to feed the directory
every time I switch from one module to another. The macro must find
the path automatically by doing some sort of "search & replace" on the
full pathname (e.g. */src/* -> */inc/*) . I'm sorry, but I'm really
bad at e-lisp, otherwise I would have changed your macro somehow to
make it work for me...
Danny
> 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)
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: How to automate file-opening (derived filename)?
2003-01-08 13:05 ` Danny Dorfman
@ 2003-01-09 6:39 ` Friedrich Dominicus
0 siblings, 0 replies; 9+ messages in thread
From: Friedrich Dominicus @ 2003-01-09 6:39 UTC (permalink / raw)
wildernesscat@hotmail.com (Danny Dorfman) writes:
> The thing is, there is no ONE header directory. There are several
> header directories, and it would be impractical to feed the directory
> every time I switch from one module to another. The macro must find
> the path automatically by doing some sort of "search & replace" on the
> full pathname (e.g. */src/* -> */inc/*) . I'm sorry, but I'm really
> bad at e-lisp, otherwise I would have changed your macro somehow to
> make it work for me...
Ok I suggest using an association list than.
(defvar *dir-match-alist*
'(("~/programming/C/"
. "~programming/C/")
("/some/path/" . "/some/path/include"))
"Asscociation lisp which maps source pathes to
appropriate header paths.")
(defun load-proper-header-file (&optional buf-name)
(interactive)
(let* ((src-dir (default-directory))
(hdr-dir (cdr (assoc src-dir *dir-match-alist*)))
(file-name (file-name-sans-extension
(if buf-name buf-name (buffer-name)))))
(find-file (concat hdr-dir file-name ".h"))))
This works as follows the first variable is a mapping from one path to
another (please do not forget to append the trailing / it should be a
dirctory. And you can read it like this:
All files in HOME/programming/C will find their header files in the
same directory all files in /some/path/ have their header files in
/some/path/include
The function load-proper-header-file works like this:
It tries to figure our in which directory it was called after that
it looks through the *dir-match-alist* association list for a matching
directory. assoc yield the whole line therefor we take the second
element (cdr), which it the directory path to the header files. After
that the file-name is build from the buffer name and in the end we
load the file which is the concatenation of the header directory part
the file name and ".h".
This works as intented, but is not very "user-friendly", but it's a
starting ground ...
Adding elements to the association list can be done with
push or acons
removing elements with remassoc
Regards
Friedrich
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: How to automate file-opening (derived filename)?
2003-01-07 18:19 ` Friedrich Dominicus
` (2 preceding siblings ...)
2003-01-08 13:05 ` Danny Dorfman
@ 2003-01-09 6:44 ` Danny Dorfman
2003-01-09 8:06 ` Friedrich Dominicus
3 siblings, 1 reply; 9+ messages in thread
From: Danny Dorfman @ 2003-01-09 6:44 UTC (permalink / raw)
Here is what I got from posting a question at http://www.experts-exchange.com
This actually does the trick! (from user "itonju01")
> This works for me. You can rename it to whatever you want
> instead of open-x-header.
>
> Hopefully the backslashes aren't munged by the expert-exchange posting system.
>
> Enjoy.
>
>
> (defun open-x-header ()
> (interactive)
> (if
> ;; replace /a/ with /ZZZ/
> (string-match "^\\(.*\\)/a/\\(.*\\)\\.c$" buffer-file-name)
>
> ;; replace find-file-other-window with find-file
> ;; or find-file-other-frame if you wish
> (find-file-other-window
> (concat (match-string 1 buffer-file-name)
> "/"
> "b" ;; replace with YYY
> "/"
> (match-string 2 buffer-file-name)
> ".h"))
> (message "No match.")))
>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: How to automate file-opening (derived filename)?
2003-01-09 6:44 ` Danny Dorfman
@ 2003-01-09 8:06 ` Friedrich Dominicus
0 siblings, 0 replies; 9+ messages in thread
From: Friedrich Dominicus @ 2003-01-09 8:06 UTC (permalink / raw)
wildernesscat@hotmail.com (Danny Dorfman) writes:
> Here is what I got from posting a question at http://www.experts-exchange.com
> This actually does the trick! (from user "itonju01")
Well it won't work for different source directories and header
directories. It just works in your special case.
Regards
Friedrich
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: How to automate file-opening (derived filename)?
2003-01-08 11:54 ` Danny Dorfman
@ 2003-01-11 12:49 ` Kai Großjohann
0 siblings, 0 replies; 9+ messages in thread
From: Kai Großjohann @ 2003-01-11 12:49 UTC (permalink / raw)
wildernesscat@hotmail.com (Danny Dorfman) writes:
> This is not suitable, since my #include files are located using a
> different path, than the one suggested by Emacs. Our tree structure
> looks like this:
> /a/b/c/src/module/file.cpp +
> /a/b/c/inc/module/file.h
> Maybe I _do_ need some filename manipulation after all?
Maybe it works to tell ff-other-file to look in ../../inc and its
subdirs?
See the variable ff-search-directories.
--
Ambibibentists unite!
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2003-01-11 12:49 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-01-07 14:52 How to automate file-opening (derived filename)? Danny Dorfman
2003-01-07 18:19 ` Friedrich Dominicus
2003-01-08 6:03 ` Alfred M. Szmidt
[not found] ` <mailman.53.1042007441.21513.help-gnu-emacs@gnu.org>
2003-01-08 11:54 ` Danny Dorfman
2003-01-11 12:49 ` Kai Großjohann
2003-01-08 13:05 ` Danny Dorfman
2003-01-09 6:39 ` Friedrich Dominicus
2003-01-09 6:44 ` Danny Dorfman
2003-01-09 8:06 ` Friedrich Dominicus
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).