2013-07-12 382 views
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..是乳膠模式。

我的問題是 - 如何在給定模式的兩個給定正則表達式之間做一個塊?

編輯:在(provide 'mumamo-fun)之前就

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
;;;; 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 
        ))) 

我已經添加了follosing到mumamo-fun.el結束。

然後在.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)) 

,現在當我打開其餘的文件,我有:

math:`TEXT`

乳膠模式

.. math: 

    TEXT 
乳膠模式

.. code-block: bash 

    TEXT 
的SH-模式

,和

.. code-block: py 

    TEXT 
的蟒模式

編輯2

此外,如果一個後

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

font-latex-make-user-keywords在AUCTeX的font-latex.el增加

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

,一會又.. math::下獲得數學模式。

+0

你可以看看小說'define-mumamo-multi-major-mode'的定義,看看它如何以及何時調用函數'rst-bk-latex-directive-chunk'。 – Malabarba

回答

2

主要功能是mumamo-possible-chunk-forward結合mumamo-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)) 

我複製你提供的自動加載的東西。坦率地說,我不明白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輕鬆地定義許多正則表達式分隔的區域。請注意,您必須double escape the dots (.)

查看nxHtml sources (in /util/mumamo-fun.el)中noweb2塊的定義,以獲得更高級的如何使用MuMaMo的正則表達式函數的示例。

+0

奇怪,但這個解決方案在emacs的最新版本中無法使用:http://emacs.stackexchange.com/q/13971/719 – Adobe