3

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

#define ENABLE 1
#if ENABLE
    bool hide = false;
#else
    bool hide = true;
#endif

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.

6
  • 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
    Commented Mar 22, 2017 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
    Commented Mar 23, 2017 at 15:40
  • You have to run M-x hide-ifdefs You didn't understand Willy's answer.
    – Daniel
    Commented Mar 23, 2017 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
    Commented Mar 23, 2017 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
    Commented Mar 23, 2017 at 20:53

2 Answers 2

3

Sorry that I didn't see this earlier. This should only happen in older hide-ifdef-mode in Emacs v24 or earlier. For any Emacs version newer than 25.1 (released 2016/09/17), the aforementioned Hide If Def that I rewrote is built-in and replace the old implementation, which did have the problem of not being able to evaluate preprocessor macros properly. In newer Emacs, you should see something like this by performing C-c @ h (M-x hide-ifdefs):

enter image description here

By marking the "ENABLE" and press C-c @ e (M-x hif-evaluate-macro) you can see the value of ENABLE shown in the echo area.

However, I am not sure why Hide If Def was not working at the time this question was asked (2017/3/22). Before I submitted it into Emacs source trunk (2014/07/07) it has been working for Emacs v24 or even v23. Now I can no longer access Emacs24 or 23 therefore can't reproduce the issue. Anyway, please be sure to try newer (above v25) Emacs.

(By the way, if you really like the texts to be "grayed" out, try C-c @ C-w (M-x hide-ifdef-toggle-shadowing), then the hidden texts will really be "grayed" out.)

Hope this helps!

2

'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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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