From 71f0b6b3f136178a94eb9b775c18dfc2e5984492 Mon Sep 17 00:00:00 2001 From: Jinzhou Zhang Date: Thu, 2 Sep 2021 23:50:03 +0800 Subject: [PATCH] fix #544 use `sed` for improving highting performance --- doc/vim-markdown.txt | 13 +++++++++++++ ftplugin/markdown.vim | 17 +++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/doc/vim-markdown.txt b/doc/vim-markdown.txt index 4d731293..b029795f 100644 --- a/doc/vim-markdown.txt +++ b/doc/vim-markdown.txt @@ -287,6 +287,19 @@ Fenced code block languages ~ < Default is "['c++=cpp', 'viml=vim', 'bash=sh', 'ini=dosini']". + *g:vim_markdown_highlight_with_sed* +- 'g:vim_markdown_highlight_with_sed' + + Vim-markdown needs to search all fenced code blocks for the filetypes to + highlight. The default code uses vim's builtin regex engine which is slow + dealing with large markdown documents. Enabling this option would use + command line utility 'sed' for searching. It's expected to improve performance + around 10x. +> + let g:vim_markdown_highlight_with_sed = 1 +< + Default is '0'. + ------------------------------------------------------------------------------- *vim-markdown-follow-named-anchors* Follow named anchors ~ diff --git a/ftplugin/markdown.vim b/ftplugin/markdown.vim index d6e51c21..c8939fb9 100644 --- a/ftplugin/markdown.vim +++ b/ftplugin/markdown.vim @@ -787,10 +787,19 @@ function! s:MarkdownHighlightSources(force) " Syntax highlight source code embedded in notes. " Look for code blocks in the current file let filetypes = {} - for line in getline(1, '$') - let ft = matchstr(line, '```\s*\zs[0-9A-Za-z_+-]*\ze.*') - if !empty(ft) && ft !~ '^\d*$' | let filetypes[ft] = 1 | endif - endfor + + if get(g:, 'vim_markdown_highlight_with_sed', 0) + let l:ft_lines = systemlist('sed -n ' . "'" . '/^[[:space:]]*```[[:space:]]*\([0-9A-Za-z_+-]*\).*$/s//\1/p' . "'", bufnr()) + for ft in l:ft_lines + if !empty(ft) && ft !~ '^\d*$' | let filetypes[ft] = 1 | endif + endfor + else + for line in getline(1, '$') + let ft = matchstr(line, '```\s*\zs[0-9A-Za-z_+-]*\ze.*') + if !empty(ft) && ft !~ '^\d*$' | let filetypes[ft] = 1 | endif + endfor + endif + if !exists('b:mkd_known_filetypes') let b:mkd_known_filetypes = {} endif