From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Daniel Lidstrom Newsgroups: gmane.emacs.help Subject: Re: .cpp, switch to .h Date: Sat, 14 Feb 2004 10:53:58 +0100 Organization: Home Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: References: NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: sea.gmane.org 1076752949 31003 80.91.224.253 (14 Feb 2004 10:02:29 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sat, 14 Feb 2004 10:02:29 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Feb 14 11:02:22 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 1Arwcv-0002k7-00 for ; Sat, 14 Feb 2004 11:02:22 +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 1ArwbE-000875-9a for geh-help-gnu-emacs@m.gmane.org; Sat, 14 Feb 2004 05:00:36 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!news2.telebyte.nl!newshub1.home.nl!home.nl!amsnews01.chello.com!amsnews04.chello.com!amstwist00.POSTED!not-for-mail User-Agent: Pan/0.14.2 (This is not a psychotic episode. It's a cleansing moment of clarity.) Original-Newsgroups: gnu.emacs.help Original-Lines: 52 Original-NNTP-Posting-Host: 193.150.217.25 Original-X-Complaints-To: abuse@chello.se Original-X-Trace: amstwist00 1076752261 193.150.217.25 (Sat, 14 Feb 2004 10:51:01 MET) Original-NNTP-Posting-Date: Sat, 14 Feb 2004 10:51:01 MET Original-Xref: shelby.stanford.edu gnu.emacs.help:120894 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:16843 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:16843 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) (define-key c++-mode-map (kbd "C-S-h") (quote my-goto-h-file)) eval-buffer(# 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