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: Daniel Lidstrom
Subject: Re: .cpp, switch to .h
Date: Sat, 14 Feb 2004 10:53:58 +0100
User-agent: Pan/0.14.2 (This is not a psychotic episode. It's a cleansing moment of clarity.)

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



reply via email to

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