Emacs cc模式下的自定义C预处理程序格式

问题描述 投票:1回答:1

我正在从Mako模板生成C源文件。 Mako模板的指令类似于C预处理程序指令,除了它们以%而不是#开头。例如:

%if some_condition:
   /* Condition is true */
%else:
   /* Condition is false */
%endif

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

是否有一种方法可以告诉cc-mode它应该以与预处理器行(以%开头的线)相同的方式处理以#开头的线?

emacs mako cc-mode
1个回答
0
投票

可能有一种更简单的方法,但是由于cc-mode在编译时准备字体化内容,所以我不确定在不声明派生cc-mode的情况下如何获得字体化。

我相信,仅通过在c-opt-*中设置下面的c-mode-hook变量,就可以单独修改缩进,而不会打扰其余部分。但是,这是一个示例派生模式,它将在从缓冲区调用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))
© www.soinside.com 2019 - 2024. All rights reserved.