首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Emacs cc-mode中的自定义C预处理器格式

Emacs cc-mode中的自定义C预处理器格式
EN

Stack Overflow用户
提问于 2020-01-09 18:26:12
回答 1查看 67关注 0票数 2

我正在从一个Mako模板生成一个C源文件。除了以%而不是#开头之外,Mako模板具有类似于C预处理器指令的指令。例如:

代码语言:javascript
复制
%if some_condition:
   /* Condition is true */
%else:
   /* Condition is false */
%endif

当编辑模板源时,这些指令严重破坏了cc-mode的字体锁定和自动缩进。

有没有办法告诉cc-mode应该以与预处理器行(以#开头)相同的方式处理以%开头的行?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-10 03:12:07

可能有一种更简单的方法,但由于cc-mode在编译时准备了字体设置,我不确定如何在不声明派生cc-mode的情况下获得字体设置(而不是简单地通过font-lock-add-keywords添加它们)。

我相信,只需在c-mode-hook中设置下面的c-opt-*变量,就可以单独修改缩进,而不用担心其他的。但是,这里是一个示例派生模式,它将在从缓冲区调用mako-mode之后,对预处理器语句进行字体处理,并提供适当的缩进(希望如此)。

代码语言:javascript
复制
(eval-when-compile
  (require 'cc-langs)
  (require 'cc-fonts))
(require 'cc-mode)

;;; create inherited mako-mode from c-mode
(eval-and-compile (c-add-language 'mako-mode 'c-mode))

;;; variables to control font-locking preprocessor stuff
(c-lang-defconst c-cpp-expr-intro-re mako
                 (concat "\\s *%\\s*" (regexp-opt '("if" "else" "endif")) ":?"))
(c-lang-defconst c-opt-cpp-prefix mako "\\s *%")
(c-lang-defconst c-opt-cpp-symbol mako "%")
(c-lang-defconst c-opt-cpp-start mako "\\s *%\\s *\\([[:alnum:]:]+\\)")

(defconst mako-font-lock-keywords-1 (c-lang-const c-matchers-1 mako))
(defconst mako-font-lock-keywords-2 (c-lang-const c-matchers-2 mako))
(defconst mako-font-lock-keywords-3 (c-lang-const c-matchers-3 mako))
(defvar mako-font-lock-keywords (c-lang-const c-matchers-3 mako))
(defun mako-font-lock-keywords ()
  (c-compose-keywords-list mako-font-lock-keywords))

(defvar mako-mode-syntax-table nil)
(define-derived-mode mako-mode prog-mode "Mako"
  :after-hook (c-update-modeline)
  :syntax-table c-mode-syntax-table

  ;; initialize cc-mode stuff
  (c-initialize-cc-mode t)
  (c-init-language-vars mako-mode)
  (c-common-init 'mako-mode))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59661877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档