Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show enclosing #ifdef blocks in Emacs

Is it possible to show current #ifdef blocks in Emacs (eg in mode-line)?

For example:

#ifdef A
... | #cursor position num. 1
#ifdef !B & C
...
#else /* !B & C */
foo(); | #cursor position num. 2
#endif /* !B & C */
#endif /* A */

It should show A in mode line, if I put cursor in position 1 and show A & !(!B & C) in position 2. I am already using hide-if-def mode. But sometimes i need to work with all #ifdef blocks.

like image 943
folq Avatar asked Aug 08 '15 11:08

folq


1 Answers

I've not always had success with WhichFunction, but it should work with C/C++ code nicely. This customization will add a new function to the recognition scheme which will tell you when you're inside an #ifdef block.

(require 'which-func)
(which-function-mode 1)
(defun name-of-current-conditional ()
  "rather inelegant coding, but it works"
  (interactive)
  (let (outer)
    (condition-case nil
        (dotimes (myv 10)
          (save-excursion
            (c-up-conditional (1+ myv))
            (setq outer (buffer-substring-no-properties 
                              (line-beginning-position)
                              (line-end-position)))))
      (error nil))
    outer))
(setq which-func-functions '(name-of-current-conditional))
like image 65
Trey Jackson Avatar answered Sep 20 '22 19:09

Trey Jackson