从正则表达式模式切换到正则表达式模式?

4
这是一个关于 MuMaMo 的问题。如果在 nxhtml/util/mumamo-fun.el 文件末尾添加以下代码:
(defun rst-bk-latex-directive-chunk (pos min max)
  "Find math chunks. Return range and 'latex-mode.
See `mumamo-find-possible-chunk' for POS, MIN and MAX."
  (mumamo-quick-static-chunk pos min max ".. math::\n\n" ".." nil 'latex-mode t))

;;;###autoload
(define-mumamo-multi-major-mode rst-bk-mumamo-mode
 "Turn on multiple major modes for Python with RestructuredText docstrings."
 ("ReST" rst-mode
  (
   rst-bk-latex-directive-chunk
   )))

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

还有

(load "~/.emacs.d/site-lisp/nxhtml/autostart.el")
(require 'mumamo)
(require 'mumamo-fun)

~/.emacs

通过在字符串.. math::\n\n..之间获取块,可以使用latex-mode。

我的问题是-如何使给定模式下两个正则表达式之间的块?

编辑:

我已将以下内容添加到mumamo-fun.el的末尾:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; ReST + math + bash + python + cl

;; LaTeX:

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

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

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

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


(defun rst-bk-mumamo-math-inline-chunk (pos min max)
 "Find math chunks.  Return range and 'math-mode.
See `mumamo-find-possible-chunk' for POS, MIN and MAX."
 (mumamo-quick-static-chunk pos min max ":math:`" "`" nil 'math-mode t))


;; bash:

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

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

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

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


;; python:

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

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

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

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


;; cl:

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

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

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

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


;;;###autoload
(define-mumamo-multi-major-mode rst-bk-mumamo-mode
  "Turn on multiple major modes for Python with RestructuredText docstrings."
  ("ReST" rst-mode (
                    rst-bk-mumamo-math-directive
                    rst-bk-mumamo-math-inline-chunk
                    rst-bk-mumamo-sh-directive
                    rst-bk-mumamo-py-directive
                    )))

(provide 'mumamo-fun)之前。

然后在.emacs中:

(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)
(load "mumamo-fun")

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

现在当我打开 ReST 文件时,我会看到:

math:`TEXT`

这是 LaTeX 模式的一部分。

.. math:

   TEXT
< p > LaTeX 模式

.. code-block: bash

   TEXT
< p > sh-mode的,和 < /p >
.. code-block: py

   TEXT

Python模式。

编辑2:

如果添加

("^   \\(.+\\)" 1 'font-latex-math-face)

之后

(dolist (item
         '(("\\(^\\|[^\\]\\)\\(&+\\)" 2 'font-latex-warning-face)
           ("\\$\\$\\([^$]+\\)\\$\\$" 1 'font-latex-math-face)
           ;; HERE
           ...

在AUCTeX的font-latex.el中使用font-latex-make-user-keywords,那么将会在.. math::下获得数学模式。

你可以查看虚构体 define-mumamo-multi-major-mode 的定义,了解它如何以及何时调用函数 rst-bk-latex-directive-chunk - Malabarba
1个回答

2
关键功能是 mumamo-possible-chunk-forwardmumamo-chunk-start-fw-remumamo-chunk-end-fw-re 的组合 - 后两者执行正则表达式匹配。
下面的代码可以实现这个功能:
(defun regexp-chunk-start (pos max)
  (let ((where (mumamo-chunk-start-fw-re pos max "math:\n\n")))
    (when where
      (list where 'latex-mode))))

(defun regexp-chunk-end (pos max)
  (save-match-data
    (mumamo-chunk-end-fw-re pos max "\\.\\.")))


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


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

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

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

我已经复制了你提供的autoload内容。老实说,我不理解MuMaMo主模式应该如何加载。但是,我能够通过在ReST文件中手动调用mumamo-alias-rst-bk-latex-directive来测试该功能。
MuMaMo的API有点不幸。匹配缓冲区区域的功能分散在三个单独的函数中,这使得重复使用变得困难。一旦模式匹配工作正常,我希望您可能想定义许多正则表达式。
因此,这里有另一个版本,将所有内容包装在宏define-quick-regexp-chunk中:
(defmacro define-quick-regexp-chunk (regexp-chunk-fun begin-mark end-mark mode)
  (let ((regexp-chunk-start-fun (gensym))
        (regexp-chunk-end-fun (gensym)))
    `(progn
       (defun ,regexp-chunk-start-fun (pos max)
         (let ((where (mumamo-chunk-start-fw-re pos max ,begin-mark)))
           (when where
             (list where ,mode))))

       (defun ,regexp-chunk-end-fun (pos max)
         (save-match-data
           (mumamo-chunk-end-fw-re pos max ,end-mark)))


       (defun ,regexp-chunk-fun (pos
                                 min
                                 max)
         (save-match-data
           (mumamo-possible-chunk-forward pos max ',regexp-chunk-start-fun
                                                  ',regexp-chunk-end-fun))))))

;; switch to latex-mode in between "math:\n\n" ".." 
;; defines a function named "rst-bk-latex-directive" which should be called by MuMaMo
(define-quick-regexp-chunk rst-bk-latex-directive "math:\n\n" "\\.\\." 'latex-mode)

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

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

它的功能完全相同,但现在我可以使用define-quick-regexp-chunk轻松定义多个基于正则表达式的区域。请注意,你必须双重转义点号(.
查看/util/mumamo-fun.el中的nxHtml源代码中noweb2区块的定义,了解如何更高级地使用MuMaMo的正则表达式函数。

奇怪的是,这个解决方案在最近版本的emacs中不起作用:http://emacs.stackexchange.com/q/13971/719 - Adobe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接