From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.help Subject: Re: .cpp, switch to .h Date: Sat, 14 Feb 2004 14:38:02 +0000 Organization: muc.de e.V. -- private internet access Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: References: NNTP-Posting-Host: deer.gmane.org X-Trace: sea.gmane.org 1076798953 2923 80.91.224.253 (14 Feb 2004 22:49:13 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sat, 14 Feb 2004 22:49:13 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Feb 14 23:49:04 2004 Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1As8au-0006he-00 for ; Sat, 14 Feb 2004 23:49:04 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.24) id 1As8Y9-00075a-TM for geh-help-gnu-emacs@m.gmane.org; Sat, 14 Feb 2004 17:46:13 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed.arcor-online.net!news.belwue.de!informatik.tu-muenchen.de!news.muc.de!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 53 Original-NNTP-Posting-Host: acm.muc.de Original-X-Trace: marvin.muc.de 1076797897 64976 193.149.49.134 (14 Feb 2004 22:31:37 GMT) Original-X-Complaints-To: news-admin@muc.de Original-NNTP-Posting-Date: 14 Feb 2004 22:31:37 GMT User-Agent: tin/1.4.5-20010409 ("One More Nightmare") (UNIX) (Linux/2.0.35 (i686)) Original-Xref: shelby.stanford.edu gnu.emacs.help:120914 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.2 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:16863 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:16863 Daniel Lidstrom wrote on Sat, 14 Feb 2004 10:53:58 +0100: > On Fri, 13 Feb 2004 23:42:38 -0500, Benjamin Rutt wrote: >> Daniel Lidstrom 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").