Is there a package that will gray out or hide the false portion of a c preprocessor macro?

#define ENABLE 1
    bool hide = false;
    bool hide = true;

In the above example, I want bool hide = true; to be grayed out or hidden.

I've looked at Hide If Def, but it doesn't produce my desired effect. Executing M-x hide-ifdefs hides bool hide = false;, but I would expect bool hide = true; to be hidden.

The functionality I am looking for would be similar to what NetBeans does.

  • Can you explain what you mean about hide-ifdef-mode having no effect? It certainly works for me. Did you run hide-ifdefs? – Willy Lee Mar 22 '17 at 20:35
  • I modified my question to reflect what I am experiencing. hide-ifdef-mode appears to only hide the #if part of #if - #else - #endif blocks. – tomcat Mar 23 '17 at 15:40
  • You have to run M-x hide-ifdefs You didn't understand Willy's answer. – dangom Mar 23 '17 at 18:28
  • I added more clarity to my question. M-x hide-ifdefs will hide the correct block when I use an integer (ie 1 or 0). But if I wrap that integer into a macro called ENABLE then M-x hide-ifdefs doesn't appear to unwrap the macro. Instead it always hides #if ENABLE block. – tomcat Mar 23 '17 at 20:12
  • You would have to build all of the preprocessor functionality into an elisp program: not impossible, but unlikely - I certainly don't know of any such implementation. – NickD Mar 23 '17 at 20:53

'hide-ifdef-mode' hides, but it doesn't do the preprocessor evaluation, since that can come from multiple places, most of which aren't available to an Emacs mode which is just looking at a single file.

You enter hide-ifdef-mode, then tell the mode what things are defined and are not, using hide-ifdef-define/hide-ifdef-undef. Then hide-ifdefs will do what you want. You can save lists of these definitions and toggle them (hide-ifdef-set-define-alist/hide-ifdef-use-define-alist). hif-evaluate-macro might be pretty valuable for you to handle more involved macrology.

It is simple to use but tedious if you have a lot of complex preprocessor defs that depend on others etc.

Might be some more project-aware packages that wrap this to make this easier, not sure.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.