unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* .cpp, switch to .h
@ 2004-02-13 23:20 Daniel Lidstrom
  2004-02-14  4:42 ` Benjamin Rutt
  2004-02-14 10:02 ` Felix
  0 siblings, 2 replies; 9+ messages in thread
From: Daniel Lidstrom @ 2004-02-13 23:20 UTC (permalink / raw)


Hello,

I would like a script that I can bind to Ctrl-Shift-h that will switch
from a .cpp file to the associated .h file, if there is one. For example,
if I have Hash.cpp opened and press the selected keys, emacs switches to
Hash.h. If there is no Hash.h nothing is done. Any help appreciated.
Thanks!

-- 
Daniel

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: .cpp, switch to .h
  2004-02-13 23:20 .cpp, switch to .h Daniel Lidstrom
@ 2004-02-14  4:42 ` Benjamin Rutt
  2004-02-14  9:53   ` Daniel Lidstrom
  2004-02-14 10:02 ` Felix
  1 sibling, 1 reply; 9+ messages in thread
From: Benjamin Rutt @ 2004-02-14  4:42 UTC (permalink / raw)


Daniel Lidstrom <someone@microsoft.com> writes:

> Hello,
>
> I would like a script that I can bind to Ctrl-Shift-h that will switch
> from a .cpp file to the associated .h file, if there is one. For example,
> if I have Hash.cpp opened and press the selected keys, emacs switches to
> Hash.h. If there is no Hash.h nothing is done. Any help appreciated.
> Thanks!

Looks only in the same directory as the .cpp file:

(defun my-goto-h-file ()
  (interactive)
  (when (string-match "\\.cpp$" (buffer-file-name))
    (let* ((h-file (format "%s.h"
			   (file-name-sans-extension (buffer-file-name)))))
      (when (file-exists-p h-file)
        (find-file h-file)))))
(define-key c++-mode-map (kbd "C-S-h") 'my-goto-h-file)
-- 
Benjamin

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: .cpp, switch to .h
  2004-02-14  4:42 ` Benjamin Rutt
@ 2004-02-14  9:53   ` Daniel Lidstrom
  2004-02-14 14:38     ` Alan Mackenzie
  0 siblings, 1 reply; 9+ messages in thread
From: Daniel Lidstrom @ 2004-02-14  9:53 UTC (permalink / raw)


On Fri, 13 Feb 2004 23:42:38 -0500, Benjamin Rutt wrote:

> Daniel Lidstrom <someone@microsoft.com> writes:
> 
>> Hello,
>>
>> I would like a script that I can bind to Ctrl-Shift-h that will switch
>> from a .cpp file to the associated .h file, if there is one. For example,
>> if I have Hash.cpp opened and press the selected keys, emacs switches to
>> Hash.h. If there is no Hash.h nothing is done. Any help appreciated.
>> Thanks!
> 
> Looks only in the same directory as the .cpp file:
> 
> (defun my-goto-h-file ()
>   (interactive)
>   (when (string-match "\\.cpp$" (buffer-file-name))
>     (let* ((h-file (format "%s.h"
> 			   (file-name-sans-extension (buffer-file-name)))))
>       (when (file-exists-p h-file)
>         (find-file h-file)))))
> (define-key c++-mode-map (kbd "C-S-h") 'my-goto-h-file)

I tried to add your script into my .emacs file, but it seems I made a
mistake somewhere. This is what emacs reports in backtrace:
Debugger entered--Lisp error: (void-variable c++-mode-map)
  (define-key c++-mode-map (kbd "C-S-h") (quote my-goto-h-file))
  eval-buffer(#<buffer  *load*> nil "~/.emacs" nil t)
  load-with-code-conversion("/home/daniel/.emacs" "~/.emacs" t t)
  load("~/.emacs" t t)
[...]

What did I do wrong?
P.S. I want to be able to go back and forth: .cpp -> .h -> .cpp. Would
this do it?

(defun my-goto-h-file ()
  (interactive)
  (when (string-match "\\.cpp$" (buffer-file-name))
    (let* ((h-file (format "%s.h"
			   (file-name-sans-extension (buffer-file-name)))))
      (when (file-exists-p h-file)
        (find-file h-file))))
  (when (string-match "\\.h$" (buffer-file-name))
    (let* ((h-file (format "%s.cpp"
			   (file-name-sans-extension (buffer-file-name)))))
      (when (file-exists-p h-file)
        (find-file h-file)))))

-- 
Daniel

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: .cpp, switch to .h
  2004-02-13 23:20 .cpp, switch to .h Daniel Lidstrom
  2004-02-14  4:42 ` Benjamin Rutt
@ 2004-02-14 10:02 ` Felix
  2004-02-14 10:27   ` Daniel Lidstrom
  1 sibling, 1 reply; 9+ messages in thread
From: Felix @ 2004-02-14 10:02 UTC (permalink / raw)


>>>>> "Daniel" == Daniel Lidstrom <someone@microsoft.com> writes:

    Daniel> Hello, I would like a script that I can bind to Ctrl-Shift-h
    Daniel> that will switch from a .cpp file to the associated .h file, if
    Daniel> there is one. For example, if I have Hash.cpp opened and press
    Daniel> the selected keys, emacs switches to Hash.h. If there is no
    Daniel> Hash.h nothing is done. Any help appreciated.  Thanks!

    Daniel> -- Daniel

ff-find-other-file

-- 
Felix

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: .cpp, switch to .h
  2004-02-14 10:02 ` Felix
@ 2004-02-14 10:27   ` Daniel Lidstrom
  2004-02-14 12:09     ` Eli Zaretskii
       [not found]     ` <mailman.2385.1076760554.928.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 9+ messages in thread
From: Daniel Lidstrom @ 2004-02-14 10:27 UTC (permalink / raw)


On Sat, 14 Feb 2004 18:02:55 +0800, Felix wrote:

>>>>>> "Daniel" == Daniel Lidstrom <someone@microsoft.com> writes:
> 
>     Daniel> Hello, I would like a script that I can bind to Ctrl-Shift-h
>     Daniel> that will switch from a .cpp file to the associated .h file, if
>     Daniel> there is one. For example, if I have Hash.cpp opened and press
>     Daniel> the selected keys, emacs switches to Hash.h. If there is no
>     Daniel> Hash.h nothing is done. Any help appreciated.  Thanks!
> 
>     Daniel> -- Daniel
> 
> ff-find-other-file

This is great. Is there a way to go the other way too? From .h to .cpp.
Perhaps even assignable to the same key.
Thanks!

-- 
Daniel

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: .cpp, switch to .h
  2004-02-14 10:27   ` Daniel Lidstrom
@ 2004-02-14 12:09     ` Eli Zaretskii
       [not found]     ` <mailman.2385.1076760554.928.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 9+ messages in thread
From: Eli Zaretskii @ 2004-02-14 12:09 UTC (permalink / raw)


> From: Daniel Lidstrom <someone@microsoft.com>
> Newsgroups: gnu.emacs.help
> Date: Sat, 14 Feb 2004 11:27:08 +0100
> > 
> > ff-find-other-file
> 
> This is great. Is there a way to go the other way too? From .h to .cpp.

Read the doc string of ff-find-other-file: it works both ways.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: .cpp, switch to .h
       [not found]     ` <mailman.2385.1076760554.928.help-gnu-emacs@gnu.org>
@ 2004-02-14 13:09       ` Daniel Lidstrom
  2004-02-14 16:59         ` Eli Zaretskii
  0 siblings, 1 reply; 9+ messages in thread
From: Daniel Lidstrom @ 2004-02-14 13:09 UTC (permalink / raw)


On Sat, 14 Feb 2004 14:09:08 +0200, Eli Zaretskii wrote:

>> From: Daniel Lidstrom <someone@microsoft.com>
>> Newsgroups: gnu.emacs.help
>> Date: Sat, 14 Feb 2004 11:27:08 +0100
>> > 
>> > ff-find-other-file
>> 
>> This is great. Is there a way to go the other way too? From .h to .cpp.
> 
> Read the doc string of ff-find-other-file: it works both ways.

Thanks, it surely works both ways. Now only have to find out how to make
it work from a custom keybindind :-)
Thank you all for your patience with me.

-- 
Daniel

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: .cpp, switch to .h
  2004-02-14  9:53   ` Daniel Lidstrom
@ 2004-02-14 14:38     ` Alan Mackenzie
  0 siblings, 0 replies; 9+ messages in thread
From: Alan Mackenzie @ 2004-02-14 14:38 UTC (permalink / raw)


Daniel Lidstrom <someone@microsoft.com> wrote on Sat, 14 Feb 2004
10:53:58 +0100:
> On Fri, 13 Feb 2004 23:42:38 -0500, Benjamin Rutt wrote:

>> Daniel Lidstrom <someone@microsoft.com> writes:

>>> Hello,

>>> I would like a script that I can bind to Ctrl-Shift-h that will
>>> switch from a .cpp file to the associated .h file, if there is one.
>>> For example, if I have Hash.cpp opened and press the selected keys,
>>> emacs switches to Hash.h. If there is no Hash.h nothing is done. Any
>>> help appreciated.  Thanks!

>> Looks only in the same directory as the .cpp file:

>> (defun my-goto-h-file ()
>>   (interactive)
>>   (when (string-match "\\.cpp$" (buffer-file-name))
>>     (let* ((h-file (format "%s.h"
>> 			   (file-name-sans-extension (buffer-file-name)))))
>>       (when (file-exists-p h-file)
>>         (find-file h-file)))))
>> (define-key c++-mode-map (kbd "C-S-h") 'my-goto-h-file)

> I tried to add your script into my .emacs file, but it seems I made a
> mistake somewhere. This is what emacs reports in backtrace:
> Debugger entered--Lisp error: (void-variable c++-mode-map)

> What did I do wrong?

You didn't have CC Mode loaded yet, so c++-mode-map hadn't yet been
defined.  A good way to solve this is explicitly to tell the system to
make the binding _after_ it's loaded CC Mode, like this:

(defun my-goto-h-file ()
      .....
        (find-file h-file))
(eval-after-load "cc-mode"
  '(define-key c++-mode-map (kbd "C-S-h") 'my-goto-h-file))

DON'T forget the little tick which quotes the define-key form!  ;-)

[ .... ]

> -- 
> Daniel

-- 
Alan Mackenzie (Munich, Germany)
Email: aacm@muuc.dee; to decode, wherever there is a repeated letter
(like "aa"), remove half of them (leaving, say, "a").

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: .cpp, switch to .h
  2004-02-14 13:09       ` Daniel Lidstrom
@ 2004-02-14 16:59         ` Eli Zaretskii
  0 siblings, 0 replies; 9+ messages in thread
From: Eli Zaretskii @ 2004-02-14 16:59 UTC (permalink / raw)


> From: Daniel Lidstrom <someone@microsoft.com>
> Newsgroups: gnu.emacs.help
> Date: Sat, 14 Feb 2004 14:09:58 +0100
> 
> Thanks, it surely works both ways. Now only have to find out how to make
> it work from a custom keybindind :-)

I think reading the relevant chapters of the manual will give you all
the information you need.  The following nodes in the manual seem to
be especially on-topic: "Rebinding", "Init Rebinding", "Function
Keys", and "Mouse Buttons".

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2004-02-14 16:59 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-02-13 23:20 .cpp, switch to .h Daniel Lidstrom
2004-02-14  4:42 ` Benjamin Rutt
2004-02-14  9:53   ` Daniel Lidstrom
2004-02-14 14:38     ` Alan Mackenzie
2004-02-14 10:02 ` Felix
2004-02-14 10:27   ` Daniel Lidstrom
2004-02-14 12:09     ` Eli Zaretskii
     [not found]     ` <mailman.2385.1076760554.928.help-gnu-emacs@gnu.org>
2004-02-14 13:09       ` Daniel Lidstrom
2004-02-14 16:59         ` Eli Zaretskii

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).