• Emacs+hideif.el 隐藏预编译代码(或彩色显示预编译代码)


    什么是hideif

    介绍一个Emacs自带的插件,位于emacs\lisp\progmodes\hideif.el,它将代码中被#if 0…#else…#endif注释的无效代码,标记为自定义的格式(font face),下图中灰色斜体的代码就是被标记的部分:

    image

    对于含大量预编译指令的代码,便于区分。

    开启

    在.emacs中添加:

    (require 'hideif)
    (setq hide-ifdef-initially t)
    (add-hook 'c-mode-common-hook
              (lambda ()
                (setq hide-ifdef-shadow t)
                (setq hide-ifdef-mode t)
                (hide-ifdefs)
                ))

    (setq hide-ifdef-shadow t)时,无效代码变为灰色。

    (setq hide-ifdef-shadow nil)时,无效代码被折叠隐藏变为...,如下图:

    image

    if条件

    以设置A=1,C=2为例:

    (eval-after-load "hideif"
      '(progn
         (setq hide-ifdef-env
               '((A . 1)
                 (C . 2)))))

    如果表达式很多,可以单独用一个文件来管理,下面是hideif-table.el文件的内容

    (defconst project1 t)
    (defconst project2 nil)
    (eval-after-load "hideif"
      '(progn
         (setq hide-ifdef-env
               '((A . 1)
                 (C . 2)))))
    (if project1
      (setq hide-ifdef-env (append '(
        (B . 3)
        (D . 4)
      ) hide-ifdef-env)))
    (if project2
      (setq hide-ifdef-env (append '(
        (B . 6)
        (D . 7)
      ) hide-ifdef-env)))

    在.emacs中(load-file “the path of hideif-table.el”)来加载,更改defconst定义可以切换多组定义。

    自定义格式

    设置Hide Ifdef Shadow face的属性即可。

    可是它在哪?我比较水,是用鼠标点出来的,Emacs菜单“Options”->“Customize Emacs”->“All Settings Matching…”输入Hide Ifdef,在匹配结果的最后一项

    建议

    在使用hideif时,Emacs常常自动弹出*Messages*栏,显示hideif遇到无法解析的代码,Bad #if expression警告,还跟着一大堆代码,同时会造成Emacs卡死。感觉没什么用,如果你也想屏蔽这个警告,可以这样:将emacs\lisp\progmodes\hideif.el的line407的

    (t (error "Bad #if expression: %s" (buffer-string))

     改为

    (t (error "Bad #if expression")

    然后在Emacs中用

    byte-compile-file

    对hideif.el重新编译生成hideif.elc即可。

  • 相关阅读:
    import pymongo exceptions.ImportError: No module named pymongo
    单个回调函数中返回多个Request以及Item
    linux下将jpg,jpeg格式转为PDF
    thinkphp中SQLSTATE[42S02]: Base table or view not found: 1146 Table错误解决方法
    Example015实现html中checkbox的全选和反选(2)
    Exameple014实现html中checkbox的全选,反选和全不选(1)
    关于DOM中的model(将元素转成对象进行操作)
    Example013操作样式
    Example012点击修改属性
    Example011表单中修改内容
  • 原文地址:https://www.cnblogs.com/gamesun/p/2858074.html
Copyright © 2020-2023  润新知