Skip to content

Commit

Permalink
Font lock with markup hiding for sub/superscripts
Browse files Browse the repository at this point in the history
Based in large part on a patch by Syohei YOSHIDA:
https://gist.github.com/syohex/30b336799510128df8bcf3d387b170b4

Closes GH-134
  • Loading branch information
jrblevin committed Jun 12, 2017
1 parent e0c4412 commit da85589
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@
display, <kbd>C-c C-x C-i</kbd>.
- Support Leanpub blocks (asides, info blocks, warnings, etc.).
These are simple extensions of the usual blockquote syntax.
- Font lock, with markup hiding, for subscripts (e.g., `H~2~0`)
and superscripts (e.g., `334^10^`). Thanks to Syohei Yoshida
for a patch on which this is based. ([GH-134][])

* Improvements:

Expand All @@ -115,6 +118,7 @@

[gh-123]: https://github.com/jrblevin/markdown-mode/issues/123
[gh-130]: https://github.com/jrblevin/markdown-mode/issues/130
[gh-134]: https://github.com/jrblevin/markdown-mode/issues/134
[gh-185]: https://github.com/jrblevin/markdown-mode/issues/185
[gh-191]: https://github.com/jrblevin/markdown-mode/issues/191

Expand Down
36 changes: 36 additions & 0 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,11 @@
(defconst markdown-output-buffer-name "*markdown-output*"
"Name of temporary buffer for markdown command output.")

(defconst markdown-sub-superscript-display
'(((raise -0.3) (height 0.7)) ; subscript
((raise 0.3) (height 0.7))) ; superscript
"Parameters for sub- and superscript formatting.")


;;; Global Variables ==========================================================

Expand Down Expand Up @@ -1623,6 +1628,17 @@ or
markdown-regex-yaml-pandoc-metadata-end-border
markdown-regex-yaml-metadata-border))

(defconst markdown-regex-sub-superscript
"\\(?:^\\|[^\\~^]\\)\\(\\([~^]\\)\\([[:alnum:]]+\\)\\(\\2\\)\\)"
"The regular expression matching a sub- or superscript.
The leading un-numbered group matches the character before the
opening tilde or carat, if any, ensuring that it is not a
backslash escape, carat, or tilde.
Group 1 matches the entire expression, including markup.
Group 2 matches the opening markup--a tilde or carat.
Group 3 matches the text inside the delimiters.
Group 4 matches the closing markup--a tilde or carat.")


;;; Syntax ====================================================================

Expand Down Expand Up @@ -2619,6 +2635,7 @@ Depending on your font, some reasonable choices are:
(markdown-fontify-plain-uris)
(,markdown-regex-email . markdown-link-face)
(,markdown-regex-line-break . (1 markdown-line-break-face prepend))
(markdown-fontify-sub-superscripts)
(markdown-fontify-blockquotes))
"Syntax highlighting for Markdown files.")

Expand Down Expand Up @@ -3667,6 +3684,25 @@ is \"\n\n\""
(window-body-width) markdown-hr-display-char)))))
t))

(defun markdown-fontify-sub-superscripts (last)
"Apply text properties to sub- and superscripts from point to LAST."
(when (markdown-search-until-condition
(lambda () (and (not (markdown-code-block-at-point-p))
(not (markdown-inline-code-at-point-p))
(not (markdown-in-comment-p))))
markdown-regex-sub-superscript last t)
(let* ((subscript-p (string= (match-string 2) "~"))
(index (if subscript-p 0 1))
(mp (list 'face 'markdown-markup-face
'invisible 'markdown-markup)))
(when markdown-hide-markup
(put-text-property (match-beginning 3) (match-end 3)
'display
(nth index markdown-sub-superscript-display)))
(add-text-properties (match-beginning 2) (match-end 2) mp)
(add-text-properties (match-beginning 4) (match-end 4) mp)
t)))


;;; Syntax Table ==============================================================

Expand Down
15 changes: 15 additions & 0 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -2822,6 +2822,21 @@ takes precedence)."
(markdown-test-range-has-face 20 20 markdown-reference-face) ; 2
(markdown-test-range-has-face 22 49 nil)))) ; [ ]and[ ]

(ert-deftest test-markdown-font-lock/subscripts ()
"Test font lock for subscripts."
(markdown-test-string "H~2~0"
(markdown-test-range-has-face 2 2 'markdown-markup-face) ; First ~
(markdown-test-range-has-face 3 3 nil) ; 2
(markdown-test-range-has-face 4 4 'markdown-markup-face))) ; Second ~

(ert-deftest test-markdown-font-lock/superscripts ()
"Test font lock for subscripts."
(markdown-test-string "334^10^"
(markdown-test-range-has-face 1 3 nil) ; 334
(markdown-test-range-has-face 4 4 'markdown-markup-face) ; First ^
(markdown-test-range-has-face 5 6 nil) ; 10
(markdown-test-range-has-face 7 7 'markdown-markup-face))) ; Second ^

(ert-deftest test-markdown-font-lock/hidden-urls-inline ()
"Test URL hiding and toggling."
(markdown-test-file "inline.text"
Expand Down

0 comments on commit da85589

Please sign in to comment.