2013-07-12 2 views
4

이것은 MuMaMo에 관한 질문입니다. 하나는 nxhtml/util/mumamo-fun.el 다음 코드의 끝에 추가하는 경우 :regexp에서 regexp로 모드를 설정 하시겠습니까?

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

하나는 문자열 사이 덩어리를 얻을 수 .. math::\n\n.. 라텍스 모드의 수 ~/.emacs에 있습니다.

내 질문은 - 주어진 된 두 정규 표현식 사이에 청크를 어떻게 만들 수 있습니까?

편집는 :

나는 mumamo-fun.el의 끝에 follosing를 추가했습니다 :

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

그리고 지금은이 : LaTeX의 모드 LaTeX의 모드의

.. math: 

    TEXT 

math:`TEXT`

SH 모드의 516,
.. code-block: bash 

    TEXT 

파이썬 모드
.. code-block: py 

    TEXT 

.

편집 2 : 또한

하나 AUCTeX의 font-latex.el에서 font-latex-make-user-keywords

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

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

을 추가하는 경우는, 다음 하나는 .. math::에서 수학 모드를 얻을 것입니다.

+0

허구'define-mumamo-multi-major-mode'의 정의를보고'rst-bk-latex-directive-chunk' 함수를 어떻게, 언제 호출하는지 볼 수 있습니다. – Malabarba

답변

2

키 기능은 mumamo-chunk-start-fw-remumamo-chunk-end-fw-re과 조합 된 mumamo-possible-chunk-forward입니다. 후자의 두 기능은 정규식 일치를 수행합니다. 난 당신이 제공하는 자동로드 물건을 복사 한

(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는 약간 불행합니다. 버퍼의 영역과 일치하는 기능은 3 개의 개별 기능에 뿌려 지므로 재사용하기가 어렵습니다. 패턴 일치 작업을하면 많은 정규 표현식을 정의하기를 원할 것으로 기대합니다.

그래서 여기에 마르코 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 (.)이어야합니다.

MuMaMo의 regexp 기능을 사용하는 방법에 대한 고급 예제는 nxHtml sources (in /util/mumamo-fun.el)에서 noweb2 청크의 정의를 확인하십시오.

+0

이상하지만이 솔루션은 최신 버전의 emacs에서는 작동하지 않습니다. http://emacs.stackexchange.com/q/13971/719 – Adobe

관련 문제