4

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.

Wuffers
  • 19,619
dehmann
  • 2,333

2 Answers2

3

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)
Gareth
  • 19,080
2

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.