【问题标题】:Custom C preprocessor format in Emacs cc-modeEmacs cc-mode 中的自定义 C 预处理器格式
【发布时间】:2020-04-26 22:26:21
【问题描述】:
我正在从 Mako 模板生成 C 源文件。 Mako 模板具有类似于 C 预处理器指令的指令,不同之处在于它们以 % 而不是 # 开头。例如:
%if some_condition:
/* Condition is true */
%else:
/* Condition is false */
%endif
在编辑模板源时,这些指令会对cc-mode 的字体锁定和自动缩进造成严重破坏。
有没有办法告诉cc-mode 它应该以与预处理器行(以# 开头)相同的方式处理以% 开头的行?
【问题讨论】:
标签:
emacs
indentation
mako
font-lock
cc-mode
【解决方案1】:
可能有一种更简单的方法,但由于 cc-mode 在编译时准备字体化内容,我不确定在不声明派生 cc-mode 的情况下如何获得字体化(而不是简单地通过 @987654321 添加它们@)。
我相信,只需在您的c-mode-hook 中设置下面的c-opt-* 变量,就可以单独修改缩进,而无需打扰其余部分。但是,这是一个示例派生模式,它将在从缓冲区调用 mako-mode 后,为您的预处理器语句提供字体并提供正确的缩进(希望如此)。
(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))