Skip to content

Commit

Permalink
Generalize display math regular expression to cover \\[..\\]
Browse files Browse the repository at this point in the history
Also add a test for double backslash math.

Closes GH-270
  • Loading branch information
jrblevin committed Nov 7, 2017
1 parent 2183a26 commit 01ffd71
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
Safronov for a patch. ([GH-248][])
- New user option `markdown-translate-filename-function` to translate
filenames when following file links. ([GH-268][], [GH-277][])
- Support double-backslash math delimiters. ([GH-270][])

* Bug fixes:

Expand Down Expand Up @@ -65,6 +66,7 @@
[gh-263]: https://github.com/jrblevin/markdown-mode/pull/263
[gh-266]: https://github.com/jrblevin/markdown-mode/issues/266
[gh-268]: https://github.com/jrblevin/markdown-mode/issues/268
[gh-270]: https://github.com/jrblevin/markdown-mode/issues/270
[gh-272]: https://github.com/jrblevin/markdown-mode/issues/272
[gh-273]: https://github.com/jrblevin/markdown-mode/issues/273
[gh-274]: https://github.com/jrblevin/markdown-mode/pull/274
Expand Down
22 changes: 14 additions & 8 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -1750,10 +1750,16 @@ Groups 1 and 3 match opening and closing dollar signs.
Group 2 matches the mathematical expression contained within.")

(defconst markdown-regex-math-display
"^\\(\\\\\\[\\)\\(\\(?:.\\|\n\\)*?\\)?\\(\\\\\\]\\)$"
"Regular expression for itex \[..\] display mode expressions.
Groups 1 and 3 match the opening and closing delimiters.
Group 2 matches the mathematical expression contained within.")
(rx line-start
(group (group (repeat 1 2 "\\")) "[")
(group (*? anything))
(group (backref 2) "]")
line-end)
"Regular expression for \[..\] or \\[..\\] display math.
Groups 1 and 4 match the opening and closing markup.
Group 3 matches the mathematical expression contained within.
Group 2 matches the opening slashes, and is used internally to
match the closing slashes.")

(defsubst markdown-make-tilde-fence-regex (num-tildes &optional end-of-line)
"Return regexp matching a tilde code fence at least NUM-TILDES long.
Expand Down Expand Up @@ -2886,10 +2892,10 @@ Depending on your font, some reasonable choices are:
(markdown-match-math-double . ((1 markdown-markup-face prepend)
(2 markdown-math-face append)
(3 markdown-markup-face prepend)))
;; Math mode \[..\]
;; Math mode \[..\] and \\[..\\]
(markdown-match-math-display . ((1 markdown-markup-face prepend)
(2 markdown-math-face append)
(3 markdown-markup-face prepend)))
(3 markdown-math-face append)
(4 markdown-markup-face prepend)))
(markdown-match-bold . ((1 markdown-markup-properties prepend)
(2 markdown-bold-face append)
(3 markdown-markup-properties prepend)))
Expand Down Expand Up @@ -3780,7 +3786,7 @@ $..$ or `markdown-regex-math-inline-double' for matching $$..$$."
(markdown-match-math-generic markdown-regex-math-inline-double last))

(defun markdown-match-math-display (last)
"Match single quoted \[..\] math from point to LAST."
"Match bracketed display math \[..\] and \\[..\\] from point to LAST."
(markdown-match-math-generic markdown-regex-math-display last))

(defun markdown-match-propertized-text (property last)
Expand Down
9 changes: 9 additions & 0 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -5019,6 +5019,15 @@ This includes preserving whitespace after the pipe."
(markdown-test-range-has-face 394 398 markdown-math-face)
(markdown-test-range-has-face 399 400 markdown-markup-face))))

(ert-deftest test-markdown-math/double-slash-display-math ()
"Test double slash display math font lock."
(let ((markdown-enable-math t))
(markdown-test-file "math.text"
(markdown-test-range-has-face 403 474 nil)
(markdown-test-range-has-face 475 477 markdown-markup-face)
(markdown-test-range-has-face 478 543 markdown-math-face)
(markdown-test-range-has-face 544 546 markdown-markup-face))))

(ert-deftest test-markdown-math/font-lock-italics ()
"Test markdown math mode with underscores."
(let ((markdown-enable-math t))
Expand Down
6 changes: 6 additions & 0 deletions tests/math.text
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ $**η = (-1)^{k(n-k)}sη$, where $**η$, is the Hodge star applied twice.
This is neither math nor italic
\[ a_2 \]

Support brackets with two backslashes, as do MultiMarkdown and Pandoc:

\\[
\Gamma(x) = \int_0^\infty t^{x-1}{\mathrm{e}}^{-t}\,{\mathrm{d}}
\\]

<!-- Local Variables: -->
<!-- markdown-enable-math: t -->
<!-- End: -->

0 comments on commit 01ffd71

Please sign in to comment.