Emacs:非代码文件的语法高亮

时间:2015-05-14 19:56:43

标签: file emacs syntax-highlighting

让我们假设我想创建一个文件(使用emacs)来解释有关编程的内容。例如,mylib-tutorial.txt。

有没有办法在包含代码的文件的特定部分打开语法高亮?

例如:

Tutorial
---------
This call behaves as follow:

  void foo(&resource);

This call will provoke a huge stack overflow and all of your 
personal files will be permanent lost (a copy to the police
will be sent, though).

是否有办法为代码示例启用语法高亮显示? Org-mode或许?

2 个答案:

答案 0 :(得分:3)

Enable org-src-fontify-natively

(eval-after-load "org"
  '(setq org-src-fontify-natively t))

然后使用org-mode

* Tutorial

This call behaves as follows:

#+BEGIN_SRC c
  void foo(&resource);
#+END_SRC

This call will provoke a huge stack overflow and all of your 
personal files will be permanent lost (a copy to the police
will be sent, though).

您还可以使用SRC修改c-mode中的org-edit-special块,默认情况下绑定到C-c '。再次使用C-c '关闭c-mode缓冲区并更新组织缓冲区。

答案 1 :(得分:0)

您可以做的不仅仅是区域化:使用nxhtml模式,您可以在代码区域中启用适当的模式。

这是一个基本的nxhtml设置:

;; nxhtml-mode:
(load "~/.emacs.d/site-lisp/nxhtml/autostart.el")
;; Mumamo is making emacs 23.3 freak out:
(when (and (equal emacs-major-version 23)
           (equal emacs-minor-version 3))
  (eval-after-load "bytecomp"
    '(add-to-list 'byte-compile-not-obsolete-vars
                  'font-lock-beginning-of-syntax-function))
  ;; tramp-compat.el clobbers this variable!
  (eval-after-load "tramp-compat"
    '(add-to-list 'byte-compile-not-obsolete-vars
                  'font-lock-beginning-of-syntax-function)))
(require 'mumamo)
(require 'mumamo-fun)

例如,让我们为rst-mode添加一个css代码规则。在reStructuredText css代码中,它用

标记

.. code-block :: css

p {        text-align:right;    }

现在让我们编写规则,指定这应该是css模式。对于这个开放的nxhtml/util/mumamo-fun.el并添加:

(defun rst-bk-mumamo-css-regexp-chunk-start (pos max)
  (let ((where (mumamo-chunk-start-fw-re pos max "\\.\\. code-block:: css\\(.\\|\n\\)*?\n\n")))
    (when where
      (list where 'css-mode))))

(defun rst-bk-mumamo-css-regexp-chunk-end (pos max)
  (save-match-data
    (mumamo-chunk-end-fw-re pos max "\\(^[[:blank:]]+$\\|\n\\)+[^[:blank:]\n]")))

(defun rst-bk-mumamo-css-quick-regexp-chunk (pos
                                  min
                                  max)
  (save-match-data
    (mumamo-possible-chunk-forward pos max 'rst-bk-mumamo-css-regexp-chunk-start
                                           'rst-bk-mumamo-css-regexp-chunk-end)))

(defun rst-bk-mumamo-css-directive (pos min max)
  "Find css chunks. Return range and 'css-mode.
   See `mumamo-find-possible-chunk' for POS, MIN and MAX."
  (rst-bk-mumamo-css-quick-regexp-chunk pos min max))

现在让我们认为这是一种新的rst模式:

;;;###autoload
(define-mumamo-multi-major-mode rst-bk-mumamo-mode
  "Turn on multiple major modes for RestructuredText."
  ("ReST" rst-mode (
                    rst-bk-mumamo-css-directive
                    )))

并使用这种新的rst模式关联第一个文件:

(add-to-list 'auto-mode-alist '("\\.rst\\'" . rst-bk-mumamo-mode))

现在你在第一个文件中的css代码块中有一个css-mode:

enter image description here

enter image description here