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,020
  • 17
  • 95
  • 126
dehmann
  • 2,243
  • 3
  • 24
  • 26

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)
Gaff
  • 18,569
  • 15
  • 57
  • 68
silversby
  • 31
  • 1
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.

Brad Payne
  • 515
  • 1
  • 5
  • 9
  • 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