Skip to content

Commit

Permalink
Merge pull request #768 from jimporter/literal-no-escape
Browse files Browse the repository at this point in the history
Don't treat backslashes as escapes inside literal blocks
  • Loading branch information
syohex authored May 22, 2023
2 parents af2c1a2 + 7c751d8 commit 0031a92
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
37 changes: 36 additions & 1 deletion markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,29 @@ Group 4 matches the text inside the delimiters.")
'markdown-metadata-markup nil)
"Property list of all Markdown syntactic properties.")

(defvar markdown-literal-faces
'(markdown-inline-code-face
markdown-pre-face
markdown-math-face
markdown-url-face
markdown-plain-url-face
markdown-language-keyword-face
markdown-language-info-face
markdown-metadata-key-face
markdown-metadata-value-face
markdown-html-entity-face
markdown-html-tag-name-face
markdown-html-tag-delimiter-face
markdown-html-attr-name-face
markdown-html-attr-value-face
markdown-reference-face
markdown-footnote-marker-face
markdown-line-break-face
markdown-comment-face)
"A list of markdown-mode faces that contain literal text.
Literal text treats backslashes literally, rather than as an
escape character (see `markdown-match-escape').")

(defsubst markdown-in-comment-p (&optional pos)
"Return non-nil if POS is in a comment.
If POS is not given, use point instead."
Expand Down Expand Up @@ -2220,7 +2243,7 @@ Depending on your font, some reasonable choices are:
(4 'markdown-highlighting-face)
(5 markdown-markup-properties)))
(,markdown-regex-line-break . (1 'markdown-line-break-face prepend))
(,markdown-regex-escape . ((1 markdown-markup-properties prepend)))
(markdown-match-escape . ((1 markdown-markup-properties prepend)))
(markdown-fontify-sub-superscripts)
(markdown-match-inline-attributes . ((0 markdown-markup-properties prepend)))
(markdown-match-leanpub-sections . ((0 markdown-markup-properties)))
Expand Down Expand Up @@ -2952,6 +2975,18 @@ When FACELESS is non-nil, do not return matches where faces have been applied."
(when markdown-enable-highlighting-syntax
(re-search-forward markdown-regex-highlighting last t)))

(defun markdown-match-escape (last)
"Match escape characters (backslashes) from point to LAST.
Backlashes only count as escape characters outside of literal
regions (e.g. code blocks). See `markdown-literal-faces'."
(catch 'found
(while (search-forward-regexp markdown-regex-escape last t)
(let* ((face (get-text-property (match-beginning 1) 'face))
(face-list (if (listp face) face (list face))))
;; Ignore any backslashes with a literal face.
(unless (cl-intersection face-list markdown-literal-faces)
(throw 'found t))))))

(defun markdown-match-math-generic (regex last)
"Match REGEX from point to LAST.
REGEX is either `markdown-regex-math-inline-single' for matching
Expand Down
16 changes: 16 additions & 0 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -2144,6 +2144,22 @@ See GH-245."
(should (invisible-p (point)))
(should-not (invisible-p (1+ (point))))))

(ert-deftest test-markdown-markup-hiding/code-2 ()
"Test hiding markup for inline code with backslashes."
(markdown-test-file "inline.text"
(goto-char 460)
(should (looking-at "`C-h C-\\\\`"))
(markdown-test-range-has-property (point) (point) 'invisible 'markdown-markup)
(should-not (invisible-p (point)))
(should-not (invisible-p (+ 1 (point))))
(should-not (invisible-p (+ 7 (point))))
(should-not (invisible-p (+ 8 (point))))
(markdown-toggle-markup-hiding t)
(should (invisible-p (point)))
(should-not (invisible-p (+ 1 (point))))
(should-not (invisible-p (+ 7 (point))))
(should (invisible-p (+ 8 (point))))))

(ert-deftest test-markdown-markup-hiding/kbd-1 ()
"Test hiding markup for <kbd> tags."
(markdown-test-string "<kbd>C-c C-x C-m</kbd>"
Expand Down

0 comments on commit 0031a92

Please sign in to comment.