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

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

Re: .cpp, switch to .h


From: Alan Mackenzie
Subject: Re: .cpp, switch to .h
Date: Sat, 14 Feb 2004 14:38:02 +0000
User-agent: tin/1.4.5-20010409 ("One More Nightmare") (UNIX) (Linux/2.0.35 (i686))

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

>> Daniel Lidstrom <address@hidden> 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: address@hidden; to decode, wherever there is a repeated letter
(like "aa"), remove half of them (leaving, say, "a").



reply via email to

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