Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


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
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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .