Skip to content

Commit f7d7983

Browse files
committed
Propertize all headings in one pass for efficiency
1 parent cd7f675 commit f7d7983

File tree

1 file changed

+42
-49
lines changed

1 file changed

+42
-49
lines changed

markdown-mode.el

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,12 +1206,12 @@ Group 2 matches only the label, without the surrounding markup.
12061206
Group 3 matches the closing square bracket.")
12071207

12081208
(defconst markdown-regex-header
1209-
"^\\(?:\\(.+\\)\n\\(=+\\)\\|\\(.+\\)\n\\(-+\\)\\|\\(#+\\)\\s-*\\(.*?\\)\\s-*?\\(#*\\)\\)$"
1209+
"^\\(?:\\(.+\\)\n\\(=+\\)\\|\\(.+\\)\n\\(-+\\)\\|\\(#+\\)[ \t]*\\(.+?\\)[ \t]*\\(#*\\)\\)$"
12101210
"Regexp identifying Markdown headings.
12111211
Group 1 matches the text of a level-1 setext heading.
12121212
Group 2 matches the underline of a level-1 setext heading.
1213-
Group 3 matches the text of a level-1 setext heading.
1214-
Group 4 matches the underline of a level-1 setext heading.
1213+
Group 3 matches the text of a level-2 setext heading.
1214+
Group 4 matches the underline of a level-2 setext heading.
12151215
Group 5 matches the opening hash marks of an atx heading.
12161216
Group 6 matches the text, without surrounding whitespace, of an atx heading.
12171217
Group 7 matches the closing hash marks of an atx heading.")
@@ -1867,17 +1867,25 @@ start which was previously propertized."
18671867
(put-text-property (match-beginning 3) (match-end 3)
18681868
'markdown-metadata-value (match-data t))))))
18691869

1870-
(defun markdown-syntax-propertize-headings-generic (symbol regex start end)
1870+
(defun markdown-syntax-propertize-headings (start end)
18711871
"Match headings of type SYMBOL with REGEX from START to END."
1872-
(save-excursion
1873-
(goto-char start)
1874-
(while (re-search-forward regex end t)
1875-
(unless (or (markdown-code-block-at-pos (match-beginning 0))
1876-
(get-text-property (match-beginning 0) 'markdown-heading))
1877-
(put-text-property (match-beginning 0) (match-end 0)
1878-
'markdown-heading t)
1879-
(put-text-property (match-beginning 0) (match-end 0)
1880-
symbol (match-data t))))))
1872+
(goto-char start)
1873+
(while (re-search-forward markdown-regex-header end t)
1874+
(unless (markdown-code-block-at-pos (match-beginning 0))
1875+
(put-text-property
1876+
(match-beginning 0) (match-end 0) 'markdown-heading t)
1877+
(put-text-property
1878+
(match-beginning 0) (match-end 0)
1879+
(cond ((match-string-no-properties 1) 'markdown-heading-1-setext)
1880+
((match-string-no-properties 3) 'markdown-heading-2-setext)
1881+
(t (let ((atx-level (length (match-string-no-properties 5))))
1882+
(cond ((= atx-level 1) 'markdown-heading-1-atx)
1883+
((= atx-level 2) 'markdown-heading-2-atx)
1884+
((= atx-level 3) 'markdown-heading-3-atx)
1885+
((= atx-level 4) 'markdown-heading-4-atx)
1886+
((= atx-level 5) 'markdown-heading-5-atx)
1887+
((= atx-level 6) 'markdown-heading-6-atx)))))
1888+
(match-data t)))))
18811889

18821890
(defun markdown-syntax-propertize-comments (start end)
18831891
"Match HTML comments from the START to END."
@@ -1931,22 +1939,7 @@ start which was previously propertized."
19311939
(markdown-syntax-propertize-yaml-metadata start end)
19321940
(markdown-syntax-propertize-pre-blocks start end)
19331941
(markdown-syntax-propertize-blockquotes start end)
1934-
(markdown-syntax-propertize-headings-generic
1935-
'markdown-heading-1-setext markdown-regex-header-1-setext start end)
1936-
(markdown-syntax-propertize-headings-generic
1937-
'markdown-heading-2-setext markdown-regex-header-2-setext start end)
1938-
(markdown-syntax-propertize-headings-generic
1939-
'markdown-heading-6-atx markdown-regex-header-6-atx start end)
1940-
(markdown-syntax-propertize-headings-generic
1941-
'markdown-heading-5-atx markdown-regex-header-5-atx start end)
1942-
(markdown-syntax-propertize-headings-generic
1943-
'markdown-heading-4-atx markdown-regex-header-4-atx start end)
1944-
(markdown-syntax-propertize-headings-generic
1945-
'markdown-heading-3-atx markdown-regex-header-3-atx start end)
1946-
(markdown-syntax-propertize-headings-generic
1947-
'markdown-heading-2-atx markdown-regex-header-2-atx start end)
1948-
(markdown-syntax-propertize-headings-generic
1949-
'markdown-heading-1-atx markdown-regex-header-1-atx start end)
1942+
(markdown-syntax-propertize-headings start end)
19501943
(markdown-syntax-propertize-comments start end))
19511944

19521945

@@ -2238,26 +2231,26 @@ See `font-lock-syntactic-face-function' for details."
22382231
(2 markdown-blockquote-face)))
22392232
(cons 'markdown-match-heading-1-setext '((1 markdown-header-face-1)
22402233
(2 markdown-header-rule-face)))
2241-
(cons 'markdown-match-heading-2-setext '((1 markdown-header-face-2)
2242-
(2 markdown-header-rule-face)))
2243-
(cons 'markdown-match-heading-6-atx '((1 markdown-header-delimiter-face)
2244-
(2 markdown-header-face-6)
2245-
(3 markdown-header-delimiter-face)))
2246-
(cons 'markdown-match-heading-5-atx '((1 markdown-header-delimiter-face)
2247-
(2 markdown-header-face-5)
2248-
(3 markdown-header-delimiter-face)))
2249-
(cons 'markdown-match-heading-4-atx '((1 markdown-header-delimiter-face)
2250-
(2 markdown-header-face-4)
2251-
(3 markdown-header-delimiter-face)))
2252-
(cons 'markdown-match-heading-3-atx '((1 markdown-header-delimiter-face)
2253-
(2 markdown-header-face-3)
2254-
(3 markdown-header-delimiter-face)))
2255-
(cons 'markdown-match-heading-2-atx '((1 markdown-header-delimiter-face)
2256-
(2 markdown-header-face-2)
2257-
(3 markdown-header-delimiter-face)))
2258-
(cons 'markdown-match-heading-1-atx '((1 markdown-header-delimiter-face)
2259-
(2 markdown-header-face-1)
2260-
(3 markdown-header-delimiter-face)))
2234+
(cons 'markdown-match-heading-2-setext '((3 markdown-header-face-2)
2235+
(4 markdown-header-rule-face)))
2236+
(cons 'markdown-match-heading-6-atx '((5 markdown-header-delimiter-face)
2237+
(6 markdown-header-face-6)
2238+
(7 markdown-header-delimiter-face)))
2239+
(cons 'markdown-match-heading-5-atx '((5 markdown-header-delimiter-face)
2240+
(6 markdown-header-face-5)
2241+
(7 markdown-header-delimiter-face)))
2242+
(cons 'markdown-match-heading-4-atx '((5 markdown-header-delimiter-face)
2243+
(6 markdown-header-face-4)
2244+
(7 markdown-header-delimiter-face)))
2245+
(cons 'markdown-match-heading-3-atx '((5 markdown-header-delimiter-face)
2246+
(6 markdown-header-face-3)
2247+
(7 markdown-header-delimiter-face)))
2248+
(cons 'markdown-match-heading-2-atx '((5 markdown-header-delimiter-face)
2249+
(6 markdown-header-face-2)
2250+
(7 markdown-header-delimiter-face)))
2251+
(cons 'markdown-match-heading-1-atx '((5 markdown-header-delimiter-face)
2252+
(6 markdown-header-face-1)
2253+
(7 markdown-header-delimiter-face)))
22612254
(cons 'markdown-match-multimarkdown-metadata '((1 markdown-metadata-key-face)
22622255
(2 markdown-markup-face)
22632256
(3 markdown-metadata-value-face)))

0 commit comments

Comments
 (0)