c++emacsemacs-semanticemacs-speedbar

Emacs imenu and speedbar+semantic fails because of indentation in c++ mode


My problem is that imenu or speedbar/semantic fails because of indentation. For this simple file, it is ok:

#include <iostream>
void bar() {
   std::cout << "bar" << std::endl;
}

But if I want to put function bar in a namespace and indent its code:

Exemple code that fails:

#include <iostream>

namespace foo {
    void bar() {
    std::cout << "bar" << std::endl;
    }
}

It is not the namespace that makes it fail, but the identation. The following fails too:

#include <iostream>
    void bar() {
       std::cout << "bar" << std::endl;
    }

Any idea why and how to have it to work?

Thanks!!

EDIT: Ok the solution is indeed speedbar+sementics. It actually works (I had something wrong in my init.el...)


Solution

  • Maybe, the example regexp from imenu.el is used together with imenu-example--create-c-index:

    (defvar imenu-example--function-name-regexp-c
      (concat
       "^[a-zA-Z0-9]+[ \t]?"        ; type specs; there can be no
       "\\([a-zA-Z0-9_*]+[ \t]+\\)?"    ; more than 3 tokens, right?
       "\\([a-zA-Z0-9_*]+[ \t]+\\)?"
       "\\([*&]+[ \t]*\\)?"         ; pointer
       "\\([a-zA-Z0-9_*]+\\)[ \t]*("    ; name
       ))
    

    The caret ^ at the beginning means beginning of line. If you insert [[:blank:]]* behind it also function definitions with leading spaces are indexed.

    I do not know whether stuff like

    else if(...) {
    ...
    }
    

    gives false positives in this case. (You have to try.)

    Actually, if I had sufficient time I would try to use semantic or ctags for the indexing. That would be much more robust.

    Note, I did not try this. I just had a look at imenu.el. (Currently, I do not have much spare time. Sorry.)