Skip to content

Commit

Permalink
Improve bold and italic font-lock stacking
Browse files Browse the repository at this point in the history
Closes GH-176.
  • Loading branch information
jrblevin committed Jun 13, 2017
1 parent 8b365fc commit e664c42
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 33 deletions.
66 changes: 37 additions & 29 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -3179,6 +3179,11 @@ Group 3 matches the closing backquotes."
(<= (match-beginning 0) old-point) ; match contains old-point
(>= (match-end 0) old-point)))))

(defun markdown-inline-code-at-pos-p (pos)
"Return non-nil if there is an inline code fragment at POS.
Like `markdown-inline-code-at-pos`, but preserves match data."
(save-match-data (markdown-inline-code-at-pos pos)))

(defun markdown-inline-code-at-point ()
"Return non-nil if the point is at an inline code fragment.
See `markdown-inline-code-at-pos' for details."
Expand Down Expand Up @@ -3282,50 +3287,53 @@ When FACELESS is non-nil, do not return matches where faces have been applied."
(match-beginning 2) (match-end 2)
(match-beginning 3) (match-end 3)
(match-beginning 4) (match-end 4)))
(goto-char (1+ (match-end 0)))))
t))

(defun markdown-match-bold (last)
"Match inline bold from the point to LAST."
(when (markdown-match-inline-generic markdown-regex-bold last)
(let ((begin (match-beginning 2)) (end (match-end 2)))
(cond
((markdown-range-property-any
begin end 'face (list markdown-inline-code-face
markdown-math-face))
(goto-char (1+ (match-end 0)))
(markdown-match-bold last))
(t
(let ((begin (match-beginning 2))
(end (match-end 2)))
(if (or (markdown-inline-code-at-pos-p begin)
(markdown-inline-code-at-pos-p end)
(markdown-range-property-any
begin begin 'face '(markdown-url-face))
(markdown-range-property-any
begin end 'face '(markdown-inline-code-face
markdown-math-face)))
(progn (goto-char (min (1+ begin) last))
(when (< (point) last)
(markdown-match-italic last)))
(set-match-data (list (match-beginning 2) (match-end 2)
(match-beginning 3) (match-end 3)
(match-beginning 4) (match-end 4)
(match-beginning 5) (match-end 5)))
(goto-char (1+ (match-end 0))))))))
(match-beginning 3) (match-end 3)
(match-beginning 4) (match-end 4)
(match-beginning 5) (match-end 5)))
t))))

(defun markdown-match-italic (last)
"Match inline italics from the point to LAST."
(let ((regex (if (eq major-mode 'gfm-mode)
markdown-regex-gfm-italic markdown-regex-italic)))
(when (markdown-match-inline-generic regex last)
(let ((begin (match-beginning 1)) (end (match-end 1)))
(cond
((markdown-range-property-any
begin begin 'face (list markdown-url-face))
;; Italics shouldn't begin inside a URL due to an underscore
(goto-char (min (1+ (match-end 0)) last))
(markdown-match-italic last))
((markdown-range-property-any
begin end 'face (list markdown-inline-code-face
markdown-bold-face
markdown-list-face
markdown-math-face))
(goto-char (1+ (match-end 0)))
(markdown-match-italic last))
(t
(let ((begin (match-beginning 1))
(end (match-end 1)))
(if (or (markdown-inline-code-at-pos-p begin)
(markdown-inline-code-at-pos-p end)
(markdown-range-property-any
begin begin 'face '(markdown-url-face))
(markdown-range-property-any
begin end 'face '(markdown-inline-code-face
markdown-bold-face
markdown-list-face
markdown-math-face)))
(progn (goto-char (min (1+ begin) last))
(when (< (point) last)
(markdown-match-italic last)))
(set-match-data (list (match-beginning 1) (match-end 1)
(match-beginning 2) (match-end 2)
(match-beginning 3) (match-end 3)
(match-beginning 4) (match-end 4)))
(goto-char (1+ (match-end 0)))))))))
t)))))

(defun markdown-match-math-generic (regex last)
"Match REGEX from point to LAST.
Expand Down
7 changes: 3 additions & 4 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -2870,13 +2870,11 @@ takes precedence)."

(ert-deftest test-markdown-font-lock/snake-case-code-in-heading ()
"Test underscores in inline code in headings."
:expected-result :failed
(markdown-test-string "# Title with `snake_case_code`"
(should-not (markdown-range-property-any 21 24 'face '(markdown-italic-face)))))

(ert-deftest test-markdown-font-lock/stars-in-code-in-heading ()
"Test asterisks in inline code in headings."
:expected-result :failed
(markdown-test-string "# Title with `char** foo, int* bar`"
(should-not (markdown-range-property-any 20 29 'face '(markdown-italic-face)))))

Expand All @@ -2887,7 +2885,6 @@ takes precedence)."

(ert-deftest test-markdown-font-lock/two-bold-words-after-list ()
"Test two bold words after a list marker."
:expected-result :failed
(markdown-test-string "- **foo** **bar**"
(should-not (markdown-range-property-any
(point-min) (point-max) 'face '(markdown-italic-face)))))
Expand Down Expand Up @@ -3330,7 +3327,9 @@ x: x
---
`x`
"
(should (= (markdown-match-code (point-max)) (point-max)))))
(should (markdown-match-code (point-max)))
(should (= (point) (point-max)))
(should (equal (match-data t) '(14 17 14 15 15 16 16 17)))))

(ert-deftest test-markdown-parsing/list-item-at-point ()
"Test `markdown-list-item-at-point-p'."
Expand Down

0 comments on commit e664c42

Please sign in to comment.