Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there an Emacs function that toggles between .cpp and .hpp files that are not in the same directories?

I know there is toggle-source.el, but it apparently does not handle the case where .cpp and .hpp are in different directories. But my directory structure is like this:

project1/src/foo.cpp
project1/include/foo.hpp
project2/src/bar.cpp
project2/include/bar.hpp

It shouldn't be hard to toggle between src/foo.cpp and include/foo.hpp but I don't speak Lisp.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Take a look at ff-find-other-file, in find-file.el. If the .h and .cpp files are in the same directory, this will just work, but you should be able to use it with different locations by modifying the value of ff-search-directories.

share|improve this answer
    
I tested this with the type of directory structure you mentioned, and ff-find-other-file found the corresponding files successfully. You should be able to just use that function without needing to customize anything. If you have a more complicated directory structure, cc-search-directories can be modified; ff-search-directories gets its contents from cc-search-directories by default. –  Brad Payne Mar 11 '11 at 15:24
    
It didn't work without customization for me; I had to modify cc-search-directories, but it works great! –  dehmann Mar 17 '11 at 16:24
add comment

Thanks for the hint Brad! In case more people find this question, here is one way that worked for me (in emacs-23.2.1):

(setq cc-other-file-alist
      '(("\\.c"   (".h"))
       ("\\.cpp"   (".h"))
       ("\\.h"   (".c"".cpp"))))

(setq ff-search-directories
      '("." "../src" "../include"))

;;; Bind the toggle function to a global key
(global-set-key "\M-t" 'ff-find-other-file)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.