The key functions are mumamo-possible-chunk-forward
in combination with mumamo-chunk-start-fw-re
andmumamo-chunk-end-fw-re
- the latter two do the regexp matching.
The following does the trick:
(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))
I've copied the autoload stuff you provided. Frankly, I don't understand how MuMaMo major modes are supposed to be loaded. However, I was able to test the feature by manually invoking mumamo-alias-rst-bk-latex-directive
in a ReST file.
MuMaMo's API is a bit unfortunate. The functionality to match a region of the buffer is sprinkled over three separate functions, which makes it difficult to reuse. I expect that you probably want to define many regexps once you have pattern-matching working.
So here's another version which wraps everything in the marco 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))
It does exactly the same thing, but now I can use define-quick-regexp-chunk
to easily define many regexp-delimited regions. Note that you have to double escape the dots (.
).
Check out the definition of noweb2 chunk in the nxHtml sources (in /util/mumamo-fun.el) for a more advanced example of how to use MuMaMo's regexp functions.