vim9script if exists('b:undo_ftplugin') b:undo_ftplugin ..= "|setl tw< sw< fo<" else b:undo_ftplugin = "setl tw< sw< fo<" endif setlocal textwidth=80 setlocal formatoptions=tnc setlocal shiftwidth=2 compiler rst2html inorea k: :kbd:``=misc#Eatchar('\s') inorea no: .. note::=misc#Eatchar('\s') inorea wa: .. warning::=misc#Eatchar('\s') inorea ad: .. admonition::=misc#Eatchar('\s') inorea at: .. attention::=misc#Eatchar('\s') inorea ca: .. caution::=misc#Eatchar('\s') inorea da: .. DANGER::=misc#Eatchar('\s') inorea er: .. error::=misc#Eatchar('\s') inorea hi: .. hint::=misc#Eatchar('\s') inorea im: .. important::=misc#Eatchar('\s') inorea ti: .. tip::=misc#Eatchar('\s') inorea si: .. sidebar::=misc#Eatchar('\s') inorea to: .. topic::=misc#Eatchar('\s') inorea ep: .. epigraph::=misc#Eatchar('\s') inorea hl: .. highlights::=misc#Eatchar('\s') inorea co: .. code::=misc#Eatchar('\s') inorea fi: .. figure::=misc#Eatchar('\s') inorea i: .. image:: import autoload 'popup.vim' def Toc() var toc: list> = [] var lvl_ch: list = [] var toc_num: list = [] var plvl = 0 var pnr = 0 for nr in range(1, line('$')) var line = getline(nr) var pline = getline(nr - 1) var ppline = getline(nr - 2) if line =~ '^\([-="#*~`.]\)\1*\s*$' if pline =~ '\S' && ppline == line && nr - 2 != pnr var lvl = lvl_ch->index(line[0] .. line[0]) if lvl == -1 lvl_ch->add(line[0] .. line[0]) toc_num->add(1) lvl = lvl_ch->len() - 1 else if lvl > plvl toc_num[lvl] = 1 else toc_num[lvl] += 1 endif endif plvl = lvl pnr = nr toc->add({lvl: lvl, toc_num: toc_num[: lvl], text: $'{pline->trim()} ({nr - 1})', linenr: nr - 1}) elseif pline =~ '^\S' && pline !~ '^\([-"=#*~`.]\)\1*\s*$' var lvl = lvl_ch->index(line[0]) if lvl == -1 lvl_ch->add(line[0]) toc_num->add(1) lvl = lvl_ch->len() - 1 else if lvl > plvl toc_num[lvl] = 1 else toc_num[lvl] += 1 endif endif plvl = lvl pnr = nr toc->add({lvl: lvl, toc_num: toc_num[: lvl], text: $'{pline->trim()} ({nr - 1})', linenr: nr - 1}) endif endif endfor var title = toc->reduce((acc, v) => v.lvl == 0 ? acc + 1 : acc, 0) == 1 ? 1 : 0 var subtitle = toc->reduce((acc, v) => v.lvl == 1 ? acc + 1 : acc, 0) == 1 ? 1 : 0 g:toc = toc for t in toc var toc_num_str = t.toc_num[title + subtitle : ]->join('.') t.text = repeat(" ", t.lvl - title - subtitle) .. $"{toc_num_str} {t.text}" endfor popup.FilterMenu("TOC", toc, (res, key) => { exe $":{res.linenr}" normal! zz }, (winid) => { win_execute(winid, 'syn match FilterMenuLineNr "(\d\+)$"') win_execute(winid, 'syn match FilterMenuSecNum "^\s*\(\d\+\.\)*\(\d\+\)"') hi def link FilterMenuLineNr Comment hi def link FilterMenuSecNum Title }) enddef nnoremap z Toc() nnoremap oh RstViewHtml nnoremap op RstViewPdf nnoremap cp Rst2Pdf \wincmd p nnoremap ch Rst2Html \wincmd p nnoremap exe "Sh" &makeprg \wincmd p def Rst2Html(locale: string = "") if !empty(locale) b:rst2html_opts = g:rst2html_opts .. " --language=" .. locale else b:rst2html_opts = g:rst2html_opts endif # workaround: convert all code-block to include + :code: :%s/^\(\s*\)\.\. code-block:: \(\S\+\)\s*\n\(\s*\):include: \(\S\+\)/\1.. include:: \4\r\3:code: \2/e :update compiler rst2html if exists(":Sh") == 2 exe "Sh" &l:makeprg else make endif enddef command -buffer -nargs=? -complete=locale Rst2Html Rst2Html() import autoload 'os.vim' def Rst2Pdf() # need to be in file's directory otherwise code-block include fails lcd %:p:h # workaround: convert all include + :code: to code-block :%s/^\(\s*\)\.\. include:: \(\S\+\)\s*\n\(\s*\):code: \(\S\+\)/\1.. code-block:: \4\r\3:include: \2/e :update compiler rst2pdf if exists(":Sh") == 2 exe "Sh" &l:makeprg else make endif lcd - enddef command -buffer Rst2Pdf Rst2Pdf() command -buffer RstViewHtml :call os.Open(expand("%:p:r") .. '.html') command -buffer RstViewPdf :call os.Open(expand("%:p:r") .. '.pdf') def HlCheckmark() exe 'syn match rstCheckDone /\%(' .. &l:formatlistpat .. '\)\@<=✓/ containedin=TOP' exe 'syn match rstCheckReject /\%(' .. &l:formatlistpat .. '\)\@<=✗/ containedin=TOP' hi link rstCheckDone Added hi link rstCheckReject Removed enddef augroup checkmark | au! au Syntax rst call HlCheckmark() au Colorscheme * call HlCheckmark() augroup END