unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Alan Mackenzie<none@example.invalid>
Subject: Re: .cpp, switch to .h
Date: Sat, 14 Feb 2004 14:38:02 +0000	[thread overview]
Message-ID: <asbl0c.ad.ln@acm.acm> (raw)
In-Reply-To: pan.2004.02.14.09.53.58.530166@microsoft.com

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

  reply	other threads:[~2004-02-14 14:38 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=asbl0c.ad.ln@acm.acm \
    --to=none@example.invalid \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).