* Need help writing file-visiting macro
@ 2005-07-25 13:58 Roy Smith
2005-07-25 15:33 ` Thien-Thi Nguyen
` (4 more replies)
0 siblings, 5 replies; 12+ messages in thread
From: Roy Smith @ 2005-07-25 13:58 UTC (permalink / raw)
I'm working in a software development system which has classes defined
in foo.mdl files, with the corresponding implementation code in
foo_Impl.c files I want to write a macro or function which lets you
flip back and forth between the two. If I'm looking at a .mdl file, I
want it to construct the corresponding _Impl.c filename and visit that
file. Is that possible?
I'm using GNU Emacs 21.3.1.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Need help writing file-visiting macro
2005-07-25 13:58 Need help writing file-visiting macro Roy Smith
@ 2005-07-25 15:33 ` Thien-Thi Nguyen
2005-07-25 16:45 ` Roy Smith
2005-07-25 16:50 ` Sergei Organov
` (3 subsequent siblings)
4 siblings, 1 reply; 12+ messages in thread
From: Thien-Thi Nguyen @ 2005-07-25 15:33 UTC (permalink / raw)
roy@panix.com (Roy Smith) writes:
> I want it to construct the corresponding _Impl.c filename and
> visit that file. Is that possible?
it is possible if the correspondance is at least heuristical.
thi
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Need help writing file-visiting macro
2005-07-25 15:33 ` Thien-Thi Nguyen
@ 2005-07-25 16:45 ` Roy Smith
2005-07-25 17:44 ` drkm
0 siblings, 1 reply; 12+ messages in thread
From: Roy Smith @ 2005-07-25 16:45 UTC (permalink / raw)
In article <7e7jfegat6.fsf@ada2.unipv.it>,
Thien-Thi Nguyen <ttn@glug.org> wrote:
>roy@panix.com (Roy Smith) writes:
>
>> I want it to construct the corresponding _Impl.c filename and
>> visit that file. Is that possible?
>
>it is possible if the correspondance is at least heuristical.
I'm not sure what you mean by "heuristical", but it certainly is
deterministic. For a given file name X.mdl, the corresponding
implementation file will be X_Impl.c.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Need help writing file-visiting macro
2005-07-25 16:45 ` Roy Smith
@ 2005-07-25 17:44 ` drkm
0 siblings, 0 replies; 12+ messages in thread
From: drkm @ 2005-07-25 17:44 UTC (permalink / raw)
Roy Smith writes:
> In article <7e7jfegat6.fsf@ada2.unipv.it>,
> Thien-Thi Nguyen <ttn@glug.org> wrote:
>>roy@panix.com (Roy Smith) writes:
>>> I want it to construct the corresponding _Impl.c filename and
>>> visit that file. Is that possible?
>>it is possible if the correspondance is at least heuristical.
> I'm not sure what you mean by "heuristical", but it certainly is
> deterministic. For a given file name X.mdl, the corresponding
> implementation file will be X_Impl.c.
So you just want the correspondance between the file *name*?
Ok:
(defun rs:get-impl-filename (mdl-name)
(unless (string-match "^\\(.+\\)\\.mdl$" mdl-name)
(error "Not a MDL file name" mdl-name))
(concat (match-string 1 mdl-name) "_Impl.c"))
--drkm
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Need help writing file-visiting macro
2005-07-25 13:58 Need help writing file-visiting macro Roy Smith
2005-07-25 15:33 ` Thien-Thi Nguyen
@ 2005-07-25 16:50 ` Sergei Organov
2005-07-25 17:52 ` rgb
` (2 subsequent siblings)
4 siblings, 0 replies; 12+ messages in thread
From: Sergei Organov @ 2005-07-25 16:50 UTC (permalink / raw)
roy@panix.com (Roy Smith) writes:
> I'm working in a software development system which has classes defined
> in foo.mdl files, with the corresponding implementation code in
> foo_Impl.c files I want to write a macro or function which lets you
> flip back and forth between the two. If I'm looking at a .mdl file, I
> want it to construct the corresponding _Impl.c filename and visit that
> file. Is that possible?
Yes.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Need help writing file-visiting macro
2005-07-25 13:58 Need help writing file-visiting macro Roy Smith
2005-07-25 15:33 ` Thien-Thi Nguyen
2005-07-25 16:50 ` Sergei Organov
@ 2005-07-25 17:52 ` rgb
2005-07-25 21:02 ` drkm
2005-07-25 18:05 ` Kevin Rodgers
2005-07-25 21:38 ` Martin Slouf
4 siblings, 1 reply; 12+ messages in thread
From: rgb @ 2005-07-25 17:52 UTC (permalink / raw)
Roy Smith wrote:
> I'm working in a software development system which has classes defined
> in foo.mdl files, with the corresponding implementation code in
> foo_Impl.c files I want to write a macro or function which lets you
> flip back and forth between the two. If I'm looking at a .mdl file, I
> want it to construct the corresponding _Impl.c filename and visit that
> file. Is that possible?
>
> I'm using GNU Emacs 21.3.1.
(defun my-open-complementary-file ()
"If buffer-file-name ends in .h open .mdl and vise versa."
(interactive)
(if buffer-file-name
(if (string-match "\\(\\`.*\\.\\)mdl\\'" (buffer-file-name))
(find-file (concat (match-string 1 (buffer-file-name))"h"))
(if (string-match ".*\\.h\\'" (buffer-file-name))
(find-file (concat (match-string 1
(buffer-file-name))"mdl"))
(message "Buffer's filename doesn't end in .mdl or .h")))
(message "Buffer is not visiting a file")))
Don't expect to get this kind of service all the time!
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Need help writing file-visiting macro
2005-07-25 17:52 ` rgb
@ 2005-07-25 21:02 ` drkm
2005-07-26 22:49 ` rgb
0 siblings, 1 reply; 12+ messages in thread
From: drkm @ 2005-07-25 21:02 UTC (permalink / raw)
rgb writes:
> (defun my-open-complementary-file ()
> "If buffer-file-name ends in .h open .mdl and vise versa."
> (interactive)
> (if buffer-file-name
> (if (string-match "\\(\\`.*\\.\\)mdl\\'" (buffer-file-name))
> (find-file (concat (match-string 1 (buffer-file-name))"h"))
> (if (string-match ".*\\.h\\'" (buffer-file-name))
> (find-file (concat (match-string 1
> (buffer-file-name))"mdl"))
> (message "Buffer's filename doesn't end in .mdl or .h")))
> (message "Buffer is not visiting a file")))
Or more precisely like this (to use '_Impl.c' instead of '.h'):
(defun my-open-complementary-file ()
"If buffer-file-name ends in _Impl.c open .mdl and vise versa."
(interactive)
(let ((buf (buffer-file-name))
(mdl-re "\\`\\(.+\\)\\.mdl\\'")
(c-re "\\`\\(.+\\)_Impl\\.c\\'"))
(if buf
(if (string-match mdl-re buf)
(find-file (concat (match-string 1 buf) "_Impl.c"))
(if (string-match c-re buf)
(find-file (concat (match-string 1 buf) ".mdl"))
(error "Filename doesn't end in .mdl or _Impl.c: %s" buf)))
(error "Buffer is not visiting a file"))))
--drkm
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Need help writing file-visiting macro
2005-07-25 21:02 ` drkm
@ 2005-07-26 22:49 ` rgb
0 siblings, 0 replies; 12+ messages in thread
From: rgb @ 2005-07-26 22:49 UTC (permalink / raw)
drkm wrote:
> rgb writes:
>
> > (defun my-open-complementary-file ()
> > "If buffer-file-name ends in .h open .mdl and vise versa."
> > (interactive)
> > (if buffer-file-name
> > (if (string-match "\\(\\`.*\\.\\)mdl\\'" (buffer-file-name))
> > (find-file (concat (match-string 1 (buffer-file-name))"h"))
> > (if (string-match ".*\\.h\\'" (buffer-file-name))
> > (find-file (concat (match-string 1
> > (buffer-file-name))"mdl"))
> > (message "Buffer's filename doesn't end in .mdl or .h")))
> > (message "Buffer is not visiting a file")))
>
> Or more precisely like this (to use '_Impl.c' instead of '.h'):
>
> (defun my-open-complementary-file ()
> "If buffer-file-name ends in _Impl.c open .mdl and vise versa."
> (interactive)
> (let ((buf (buffer-file-name))
> (mdl-re "\\`\\(.+\\)\\.mdl\\'")
> (c-re "\\`\\(.+\\)_Impl\\.c\\'"))
> (if buf
> (if (string-match mdl-re buf)
> (find-file (concat (match-string 1 buf) "_Impl.c"))
> (if (string-match c-re buf)
> (find-file (concat (match-string 1 buf) ".mdl"))
> (error "Filename doesn't end in .mdl or _Impl.c: %s" buf)))
> (error "Buffer is not visiting a file"))))
>
> --drkm
I've really been throwing out some slop lately. It's embarrassing.
(defun my-open-complementary-file ()
"If buffer-file-name ends in _Impl.c open .mdl and vise versa."
(interactive)
(cond
((not buffer-file-name)
(message "Buffer is not visiting a file"))
((string-match "\\(\\`.*\\)\\.mdl\\'" buffer-file-name)
(find-file (concat (match-string 1 buffer-file-name)"_Impl.c")))
((string-match "\\(\\`.*\\)_Impl\\.c\\'" buffer-file-name)
(find-file (concat (match-string 1 buffer-file-name)".mdl")))
(t (message "Buffer's filename doesn't end in .mdl or _Impl.c"))))
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Need help writing file-visiting macro
2005-07-25 13:58 Need help writing file-visiting macro Roy Smith
` (2 preceding siblings ...)
2005-07-25 17:52 ` rgb
@ 2005-07-25 18:05 ` Kevin Rodgers
2005-07-25 21:38 ` Martin Slouf
4 siblings, 0 replies; 12+ messages in thread
From: Kevin Rodgers @ 2005-07-25 18:05 UTC (permalink / raw)
Roy Smith wrote:
> I'm working in a software development system which has classes defined
> in foo.mdl files, with the corresponding implementation code in
> foo_Impl.c files I want to write a macro or function which lets you
> flip back and forth between the two. If I'm looking at a .mdl file, I
> want it to construct the corresponding _Impl.c filename and visit that
> file. Is that possible?
Of course.
(defun find-impl-file (&optional mdl-file)
"When visiting a \".mdl\", visit the corresponding \"_Impl.c\" file."
(interactive)
(when (null mdl-file)
(setq mdl-file buffer-file-name))
(unless (and mdl-file (equal (file-name-extension mdl-file) "mdl"))
(error "%s is not a \".mdl\" file" mdl-file))
(find-file (concat (file-name-sans-extension mdl-file) "_Impl.c")))
--
Kevin Rodgers
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Need help writing file-visiting macro
2005-07-25 13:58 Need help writing file-visiting macro Roy Smith
` (3 preceding siblings ...)
2005-07-25 18:05 ` Kevin Rodgers
@ 2005-07-25 21:38 ` Martin Slouf
2005-07-26 15:52 ` Kevin Rodgers
4 siblings, 1 reply; 12+ messages in thread
From: Martin Slouf @ 2005-07-25 21:38 UTC (permalink / raw)
Indeed a great thread!
I found inforamtion here very usefull and i am surprised how easy it is.
thanks.
In fact i do a lot of Java editting (jakarta struts) and there is a lot of
files whith deterministic filename (thanks to the naming conventions we use).
The files are unfortunately in different directories. I know very little of
elisp, though i use Emacs a lot.
Is there an easy modification of this piece of code that can help me to find
files in different directories? i guess these are the string manipulation
functions.
The situation is like this:
For each business level class (BankAccount.java) (located somewhere under the
'src' directory structure) there are at least two jsp pages:
bank_account_edit_.jsp and bank_account_list_.jsp under the 'web' directory
structure), ie:
top dir
+
|
+-- src (Java source in packages)
| |
| +-- somewhere
| |
| +-- BankAccount.java
|
+---web (JSP pages using jakarta-struts)
|
+-- somewhere
|
+-- bank_account_edit_.jsp
|
+-- bak_account_list_.jsp
i get those macros (proposed in thi sthread) open the same buffer with modified
name in the same directory. My questions are like this:
1. what string function can be used to make the string BankAccount to transform
it into bank_account_edit_ and bank_account_list_
2. what functions should be used to open those jsp buffers (java buffer
respectively)?, when each of this file is / can be in different directory?
thanks a lot for an answer.
Nowadays i use file-cache to search for a file.
m.
On Mon, Jul 25, 2005 at 09:58:24AM -0400, Roy Smith wrote:
> I'm working in a software development system which has classes defined
> in foo.mdl files, with the corresponding implementation code in
> foo_Impl.c files I want to write a macro or function which lets you
> flip back and forth between the two. If I'm looking at a .mdl file, I
> want it to construct the corresponding _Impl.c filename and visit that
> file. Is that possible?
>
> I'm using GNU Emacs 21.3.1.
> _______________________________________________
> Help-gnu-emacs mailing list
> Help-gnu-emacs@gnu.org
> http://lists.gnu.org/mailman/listinfo/help-gnu-emacs
----- End forwarded message -----
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Need help writing file-visiting macro
2005-07-25 21:38 ` Martin Slouf
@ 2005-07-26 15:52 ` Kevin Rodgers
2005-07-26 18:24 ` Martin Slouf
0 siblings, 1 reply; 12+ messages in thread
From: Kevin Rodgers @ 2005-07-26 15:52 UTC (permalink / raw)
Martin Slouf wrote:
> Is there an easy modification of this piece of code that can help me
to find
> files in different directories? i guess these are the string
manipulation
> functions.
There are also file name manipulation functions, which are generally
preferable to the lower-level string manipulation functions:
file-name-directory
file-name-nondirectory
file-name-extension
file-name-sans-extension
file-name-sans-versions
file-name-as-directory
directory-file-name
expand-file-name
> The situation is like this:
>
> For each business level class (BankAccount.java) (located somewhere
under the
> 'src' directory structure) there are at least two jsp pages:
> bank_account_edit_.jsp and bank_account_list_.jsp under the 'web'
directory
> structure), ie:
>
> top dir
> +
> |
> +-- src (Java source in packages)
> | |
> | +-- somewhere
> | |
> | +-- BankAccount.java
> |
> +---web (JSP pages using jakarta-struts)
> |
> +-- somewhere
> |
> +-- bank_account_edit_.jsp
> |
> +-- bak_account_list_.jsp
>
>
> i get those macros (proposed in thi sthread) open the same buffer with
> modified name in the same directory. My questions are like this:
>
> 1. what string function can be used to make the string BankAccount to
> transform it into bank_account_edit_ and bank_account_list_
Hmmm, CamelCase to lower_case.
http://www.emacswiki.org/cgi-bin/wiki/CamelCase refers to glasses-mode,
which unfortunately doesn't provide a low level utility function to
convert strings. But you can write your own:
(defun CamelCase-to-lower_case (string)
(let ((i 0)
(result "")
(case-fold-search nil))
(while (string-match "[[:upper:]][[:lower:]]*" string i)
(setq result
(concat result
(substring string i (match-beginning 0))
(if (> (match-beginning 0) 0) "_")
(downcase (match-string 0 string))))
(setq i (match-end 0)))
(setq result
(concat result (substring string i)))))
So (concat (CamelCase-to-lower_case "BankAccount") "_edit_") returns
"bank_account_edit_".
> 2. what functions should be used to open those jsp buffers (java
> buffer respectively)?, when each of this file is / can be in different
> directory?
Just stick with find-file, or perhaps
(switch-to-buffer (find-file-noselect FILE)) or
(pop-to-buffer (find-file-noselect FILE)).
--
Kevin Rodgers
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Need help writing file-visiting macro
2005-07-26 15:52 ` Kevin Rodgers
@ 2005-07-26 18:24 ` Martin Slouf
0 siblings, 0 replies; 12+ messages in thread
From: Martin Slouf @ 2005-07-26 18:24 UTC (permalink / raw)
thanks a lot for the hint, Kevin. i spend some time watching those functions
and a finally read some basic chapters from elisp manual to understand it a bit
more. It is not so bad as I thought it would be.
martin
On Tue, Jul 26, 2005 at 09:52:13AM -0600, Kevin Rodgers wrote:
> Martin Slouf wrote:
> > Is there an easy modification of this piece of code that can help me
> to find
> > files in different directories? i guess these are the string
> manipulation
> > functions.
>
> There are also file name manipulation functions, which are generally
> preferable to the lower-level string manipulation functions:
>
> file-name-directory
> file-name-nondirectory
> file-name-extension
> file-name-sans-extension
> file-name-sans-versions
> file-name-as-directory
> directory-file-name
> expand-file-name
>
> > The situation is like this:
> >
> > For each business level class (BankAccount.java) (located somewhere
> under the
> > 'src' directory structure) there are at least two jsp pages:
> > bank_account_edit_.jsp and bank_account_list_.jsp under the 'web'
> directory
> > structure), ie:
> >
> > top dir
> > +
> > |
> > +-- src (Java source in packages)
> > | |
> > | +-- somewhere
> > | |
> > | +-- BankAccount.java
> > |
> > +---web (JSP pages using jakarta-struts)
> > |
> > +-- somewhere
> > |
> > +-- bank_account_edit_.jsp
> > |
> > +-- bak_account_list_.jsp
> >
> >
> > i get those macros (proposed in thi sthread) open the same buffer with
> > modified name in the same directory. My questions are like this:
> >
> > 1. what string function can be used to make the string BankAccount to
> > transform it into bank_account_edit_ and bank_account_list_
>
> Hmmm, CamelCase to lower_case.
> http://www.emacswiki.org/cgi-bin/wiki/CamelCase refers to glasses-mode,
> which unfortunately doesn't provide a low level utility function to
> convert strings. But you can write your own:
>
> (defun CamelCase-to-lower_case (string)
> (let ((i 0)
> (result "")
> (case-fold-search nil))
> (while (string-match "[[:upper:]][[:lower:]]*" string i)
> (setq result
> (concat result
> (substring string i (match-beginning 0))
> (if (> (match-beginning 0) 0) "_")
> (downcase (match-string 0 string))))
> (setq i (match-end 0)))
> (setq result
> (concat result (substring string i)))))
>
> So (concat (CamelCase-to-lower_case "BankAccount") "_edit_") returns
> "bank_account_edit_".
>
> > 2. what functions should be used to open those jsp buffers (java
> > buffer respectively)?, when each of this file is / can be in different
> > directory?
>
> Just stick with find-file, or perhaps
> (switch-to-buffer (find-file-noselect FILE)) or
> (pop-to-buffer (find-file-noselect FILE)).
>
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2005-07-26 22:49 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-07-25 13:58 Need help writing file-visiting macro Roy Smith
2005-07-25 15:33 ` Thien-Thi Nguyen
2005-07-25 16:45 ` Roy Smith
2005-07-25 17:44 ` drkm
2005-07-25 16:50 ` Sergei Organov
2005-07-25 17:52 ` rgb
2005-07-25 21:02 ` drkm
2005-07-26 22:49 ` rgb
2005-07-25 18:05 ` Kevin Rodgers
2005-07-25 21:38 ` Martin Slouf
2005-07-26 15:52 ` Kevin Rodgers
2005-07-26 18:24 ` Martin Slouf
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).