From 7c95b229d79c089fefab89bffeb40c4add6528d2 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Wed, 9 Sep 2026 15:02:58 +0200 Subject: [PATCH] Speed up ATX heading and ordered list matching The six ATX heading regions (markdownH1..markdownH6) and the ordered list marker start with a bounded whitespace count (" \{,3}#" and "\%(\t\| \{0,4}\)\<\d\+\."), which gives the regexp engine no first character to anchor on. The NFA engine therefore re-scans these at every line start; the backtracking engine rejects non-matching lines quickly. Force the backtracking engine with \%#=1 on these patterns. Highlighting is unchanged. On a mixed 6000-line document this cuts total :syntime engine work roughly in half (each heading region ~-97%, ordered marker ~-44%). --- syntax/markdown.vim | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/syntax/markdown.vim b/syntax/markdown.vim index a069746..b5ef093 100644 --- a/syntax/markdown.vim +++ b/syntax/markdown.vim @@ -2,7 +2,7 @@ " Language: Markdown " Maintainer: Tim Pope " Filenames: *.markdown -" Last Change: 2022 Oct 13 +" Last Change: 2026 Sep 09 if exists("b:current_syntax") finish @@ -74,12 +74,12 @@ syn match markdownH2 "^.\+\n-\+$" contained contains=@markdownInline,markdownHea syn match markdownHeadingRule "^[=-]\+$" contained -syn region markdownH1 matchgroup=markdownH1Delimiter start=" \{,3}#\s" end="#*\s*$" keepend oneline contains=@markdownInline,markdownAutomaticLink contained -syn region markdownH2 matchgroup=markdownH2Delimiter start=" \{,3}##\s" end="#*\s*$" keepend oneline contains=@markdownInline,markdownAutomaticLink contained -syn region markdownH3 matchgroup=markdownH3Delimiter start=" \{,3}###\s" end="#*\s*$" keepend oneline contains=@markdownInline,markdownAutomaticLink contained -syn region markdownH4 matchgroup=markdownH4Delimiter start=" \{,3}####\s" end="#*\s*$" keepend oneline contains=@markdownInline,markdownAutomaticLink contained -syn region markdownH5 matchgroup=markdownH5Delimiter start=" \{,3}#####\s" end="#*\s*$" keepend oneline contains=@markdownInline,markdownAutomaticLink contained -syn region markdownH6 matchgroup=markdownH6Delimiter start=" \{,3}######\s" end="#*\s*$" keepend oneline contains=@markdownInline,markdownAutomaticLink contained +syn region markdownH1 matchgroup=markdownH1Delimiter start="\%#=1 \{,3}#\s" end="#*\s*$" keepend oneline contains=@markdownInline,markdownAutomaticLink contained +syn region markdownH2 matchgroup=markdownH2Delimiter start="\%#=1 \{,3}##\s" end="#*\s*$" keepend oneline contains=@markdownInline,markdownAutomaticLink contained +syn region markdownH3 matchgroup=markdownH3Delimiter start="\%#=1 \{,3}###\s" end="#*\s*$" keepend oneline contains=@markdownInline,markdownAutomaticLink contained +syn region markdownH4 matchgroup=markdownH4Delimiter start="\%#=1 \{,3}####\s" end="#*\s*$" keepend oneline contains=@markdownInline,markdownAutomaticLink contained +syn region markdownH5 matchgroup=markdownH5Delimiter start="\%#=1 \{,3}#####\s" end="#*\s*$" keepend oneline contains=@markdownInline,markdownAutomaticLink contained +syn region markdownH6 matchgroup=markdownH6Delimiter start="\%#=1 \{,3}######\s" end="#*\s*$" keepend oneline contains=@markdownInline,markdownAutomaticLink contained syn match markdownBlockquote ">\%(\s\|$\)" contained nextgroup=@markdownBlock @@ -87,7 +87,7 @@ syn region markdownCodeBlock start="^\n\( \{4,}\|\t\)" end="^\ze \{,3}\S.*$" kee " TODO: real nesting syn match markdownListMarker "\%(\t\| \{0,4\}\)[-*+]\%(\s\+\S\)\@=" contained -syn match markdownOrderedListMarker "\%(\t\| \{0,4}\)\<\d\+\.\%(\s\+\S\)\@=" contained +syn match markdownOrderedListMarker "\%#=1\%(\t\| \{0,4}\)\<\d\+\.\%(\s\+\S\)\@=" contained syn match markdownRule "\* *\* *\*[ *]*$" contained syn match markdownRule "- *- *-[ -]*$" contained