From bac5ea6b8581cdb113dab16a8fd73ca7fe4848cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Mon, 22 Mar 2021 22:20:15 +0100 Subject: [PATCH 1/2] feat(syntax): conceal for \cite refer: #1965 --- autoload/vimtex/options.vim | 2 ++ autoload/vimtex/syntax/core.vim | 15 +++++++++++++++ autoload/vimtex/syntax/p/biblatex.vim | 8 ++++++++ test/test-syntax/test-biblatex.tex | 3 +++ 4 files changed, 28 insertions(+) diff --git a/autoload/vimtex/options.vim b/autoload/vimtex/options.vim index 40f5df4906..02da3ac8b7 100644 --- a/autoload/vimtex/options.vim +++ b/autoload/vimtex/options.vim @@ -278,7 +278,9 @@ function! vimtex#options#init() abort " {{{1 \ 'math_super_sub': g:vimtex_syntax_conceal_default, \ 'math_symbols': g:vimtex_syntax_conceal_default, \ 'styles': g:vimtex_syntax_conceal_default, + \ 'citations': g:vimtex_syntax_conceal_default, \}) + call s:init_option('vimtex_syntax_conceal_citesign', '📖') call s:init_option('vimtex_syntax_nested', { \ 'aliases' : { \ 'C' : 'c', diff --git a/autoload/vimtex/syntax/core.vim b/autoload/vimtex/syntax/core.vim index deca5163bc..104b273355 100644 --- a/autoload/vimtex/syntax/core.vim +++ b/autoload/vimtex/syntax/core.vim @@ -464,6 +464,11 @@ function! vimtex#syntax#core#init() abort " {{{1 if g:vimtex_syntax_conceal.accents call s:match_conceal_accents() endif + + " Conceal cite commands + if g:vimtex_syntax_conceal.citations + call s:match_conceal_citations() + endif endif " }}}2 @@ -531,6 +536,7 @@ function! vimtex#syntax#core#init_highlights() abort " {{{1 highlight def link texCmdPackage texCmd highlight def link texCmdPart texCmd highlight def link texCmdRef texCmd + highlight def link texCmdRefConcealed texRefArg highlight def link texCmdSize texCmdType highlight def link texCmdSpaceCode texCmd highlight def link texCmdStyle texCmd @@ -1505,3 +1511,12 @@ function! s:match_conceal_greek() abort " {{{1 endfunction " }}}1 +function! s:match_conceal_citations() abort " {{{1 + if empty(g:vimtex_syntax_conceal_citesign) | return | endif + + execute 'syntax match texCmdRefConcealed' + \ '"\\cite[tp]\?\*\?\%(\[[^]]*\]\)\{,2}{[^}]*}"' + \ 'conceal cchar=' . g:vimtex_syntax_conceal_citesign +endfunction + +" }}}1 diff --git a/autoload/vimtex/syntax/p/biblatex.vim b/autoload/vimtex/syntax/p/biblatex.vim index 6f593c4caa..754bf86680 100644 --- a/autoload/vimtex/syntax/p/biblatex.vim +++ b/autoload/vimtex/syntax/p/biblatex.vim @@ -33,6 +33,14 @@ function! vimtex#syntax#p#biblatex#load(cfg) abort " {{{1 syntax match texCmdRef nextgroup=texRefOpts,texRefArgs skipwhite skipnl "\\cite\%(field\|list\|name\)>" call vimtex#syntax#core#new_arg('texRefArgs', {'next': 'texRefOpts,texRefArgs', 'contains': 'texComment,@NoSpell'}) call vimtex#syntax#core#new_opt('texRefOpts', {'next': 'texRefOpt,texRefArg'}) + + if g:vimtex_syntax_conceal.citations + \ && !empty(g:vimtex_syntax_conceal_citesign) + execute 'syntax match texCmdRefConcealed' + \ '"\v\\%(cite[tp]?\*?|%([Tt]ext|[Ss]mart|[Aa]uto)cite)' + \ . '%(\[[^]]*\]){,2}\{[^}]*\}"' + \ 'conceal cchar=' . g:vimtex_syntax_conceal_citesign + endif endfunction " }}}1 diff --git a/test/test-syntax/test-biblatex.tex b/test/test-syntax/test-biblatex.tex index 2f99bf3297..852a8f6e3b 100644 --- a/test/test-syntax/test-biblatex.tex +++ b/test/test-syntax/test-biblatex.tex @@ -23,4 +23,7 @@ \citeyearpar{} \bibentry{} +% Conceal example +Bla bla bla, see Ref.~\cite{John2012Measurement,Anne2017Bifurcation}. For more info, see also \cite{Bob2025Mystery}. + \end{document} From 980476430589430e2100115cdd658dbb07410f1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Wed, 24 Mar 2021 22:25:10 +0100 Subject: [PATCH 2/2] feat: improve cite conceal - allow bracket style refer: #2002, #1965 --- autoload/vimtex/options.vim | 7 +++-- autoload/vimtex/syntax/core.vim | 34 +++++++++++++++++++----- autoload/vimtex/syntax/p/biblatex.vim | 26 +++++++++++++----- doc/vimtex.txt | 38 +++++++++++++++++++++++---- test/test-syntax/test-biblatex.vim | 16 +++++++++++ 5 files changed, 102 insertions(+), 19 deletions(-) create mode 100644 test/test-syntax/test-biblatex.vim diff --git a/autoload/vimtex/options.vim b/autoload/vimtex/options.vim index 02da3ac8b7..5c74538cfb 100644 --- a/autoload/vimtex/options.vim +++ b/autoload/vimtex/options.vim @@ -278,9 +278,12 @@ function! vimtex#options#init() abort " {{{1 \ 'math_super_sub': g:vimtex_syntax_conceal_default, \ 'math_symbols': g:vimtex_syntax_conceal_default, \ 'styles': g:vimtex_syntax_conceal_default, - \ 'citations': g:vimtex_syntax_conceal_default, + \ 'cites': g:vimtex_syntax_conceal_default, + \}) + call s:init_option('vimtex_syntax_conceal_cites', { + \ 'type': 'icon', + \ 'icon': '📖', \}) - call s:init_option('vimtex_syntax_conceal_citesign', '📖') call s:init_option('vimtex_syntax_nested', { \ 'aliases' : { \ 'C' : 'c', diff --git a/autoload/vimtex/syntax/core.vim b/autoload/vimtex/syntax/core.vim index 104b273355..261aa97549 100644 --- a/autoload/vimtex/syntax/core.vim +++ b/autoload/vimtex/syntax/core.vim @@ -466,8 +466,8 @@ function! vimtex#syntax#core#init() abort " {{{1 endif " Conceal cite commands - if g:vimtex_syntax_conceal.citations - call s:match_conceal_citations() + if g:vimtex_syntax_conceal.cites + call s:match_conceal_cites_{g:vimtex_syntax_conceal_cites.type}() endif endif @@ -536,7 +536,7 @@ function! vimtex#syntax#core#init_highlights() abort " {{{1 highlight def link texCmdPackage texCmd highlight def link texCmdPart texCmd highlight def link texCmdRef texCmd - highlight def link texCmdRefConcealed texRefArg + highlight def link texCmdRefConcealed texCmdRef highlight def link texCmdSize texCmdType highlight def link texCmdSpaceCode texCmd highlight def link texCmdStyle texCmd @@ -596,6 +596,10 @@ function! vimtex#syntax#core#init_highlights() abort " {{{1 highlight def link texNewenvParm texParm highlight def link texOptEqual texSymbol highlight def link texRefOpt texOpt + highlight def link texRefConcealedOpt texRefOpt + highlight def link texRefConcealedArg texRefArg + highlight def link texRefConcealedPre texDelim + highlight def link texRefConcealedPost texDelim highlight def link texTabularArg texOpt highlight def link texTabularAtSep texMathDelim highlight def link texTabularChar texSymbol @@ -1511,12 +1515,30 @@ function! s:match_conceal_greek() abort " {{{1 endfunction " }}}1 -function! s:match_conceal_citations() abort " {{{1 - if empty(g:vimtex_syntax_conceal_citesign) | return | endif +function! s:match_conceal_cites_brackets() abort " {{{1 + syntax match texCmdRefConcealed + \ "\\cite[tp]\?\>\*\?" + \ conceal skipwhite nextgroup=texRefConcealedOpt,texRefConcealedPre + call vimtex#syntax#core#new_opt('texRefConcealedOpt', { + \ 'opts': 'conceal', + \ 'next': 'texRefConcealedOpt,texRefConcealedPre', + \}) + syntax match texRefConcealedPre contained "{" + \ conceal cchar=[ + \ skipwhite nextgroup=texRefConcealedArg + syntax match texRefConcealedArg contained "[^}]*" + \ skipwhite nextgroup=texRefConcealedPost + syntax match texRefConcealedPost contained "}" + \ conceal cchar=] +endfunction + +" }}}1 +function! s:match_conceal_cites_icon() abort " {{{1 + if empty(g:vimtex_syntax_conceal_cites.icon) | return | endif execute 'syntax match texCmdRefConcealed' \ '"\\cite[tp]\?\*\?\%(\[[^]]*\]\)\{,2}{[^}]*}"' - \ 'conceal cchar=' . g:vimtex_syntax_conceal_citesign + \ 'conceal cchar=' . g:vimtex_syntax_conceal_cites.icon endfunction " }}}1 diff --git a/autoload/vimtex/syntax/p/biblatex.vim b/autoload/vimtex/syntax/p/biblatex.vim index 754bf86680..fe3211000a 100644 --- a/autoload/vimtex/syntax/p/biblatex.vim +++ b/autoload/vimtex/syntax/p/biblatex.vim @@ -34,13 +34,27 @@ function! vimtex#syntax#p#biblatex#load(cfg) abort " {{{1 call vimtex#syntax#core#new_arg('texRefArgs', {'next': 'texRefOpts,texRefArgs', 'contains': 'texComment,@NoSpell'}) call vimtex#syntax#core#new_opt('texRefOpts', {'next': 'texRefOpt,texRefArg'}) - if g:vimtex_syntax_conceal.citations - \ && !empty(g:vimtex_syntax_conceal_citesign) - execute 'syntax match texCmdRefConcealed' - \ '"\v\\%(cite[tp]?\*?|%([Tt]ext|[Ss]mart|[Aa]uto)cite)' - \ . '%(\[[^]]*\]){,2}\{[^}]*\}"' - \ 'conceal cchar=' . g:vimtex_syntax_conceal_citesign + if g:vimtex_syntax_conceal.cites + call s:match_conceal_cites_{g:vimtex_syntax_conceal_cites.type}() endif endfunction " }}}1 + +function! s:match_conceal_cites_brackets() abort " {{{1 + syntax match texCmdRefConcealed + \ "\v\\%(cite[tp]?>\*?|%([Tt]ext|[Ss]mart|[Aa]uto)cite>)" + \ conceal skipwhite nextgroup=texRefConcealedOpt,texRefConcealedPre +endfunction + +" }}}1 +function! s:match_conceal_cites_icon() abort " {{{1 + if empty(g:vimtex_syntax_conceal_cites.icon) | return | endif + + execute 'syntax match texCmdRefConcealed' + \ '"\v\\%(cite[tp]?\*?|%([Tt]ext|[Ss]mart|[Aa]uto)cite)' + \ . '%(\[[^]]*\]){,2}\{[^}]*\}"' + \ 'conceal cchar=' . g:vimtex_syntax_conceal_cites.icon +endfunction + +" }}}1 diff --git a/doc/vimtex.txt b/doc/vimtex.txt index af91ec944e..3e5894b516 100644 --- a/doc/vimtex.txt +++ b/doc/vimtex.txt @@ -2025,6 +2025,13 @@ OPTIONS *vimtex-options* Default value: 1. +*g:vimtex_syntax_conceal_default* + Specify the default flag for the conceal feature. This is used e.g. in + |vimtex_syntax_conceal| and for package specific options in + |vimtex_syntax_packages|. + + Default value: 1 + *g:vimtex_syntax_conceal* A dictionary for specifying which core conceal features to activate. This mostly implies concealing particular elements with a replacement unicode @@ -2070,18 +2077,39 @@ OPTIONS *vimtex-options* `\emph{text here}` --> `text here` + cites~ + Conceal LaTeX cite commands such as `\citet[...]{ref00}`. The conceal + style is specified by |g:vimtex_syntax_conceal_cites|. + Note: Not all fonts have all the characters used for concealing! So, for this feature to work properly, you need to install and use a font that includes the unicode characters. Default value: All keys set to |g:vimtex_syntax_conceal_default|. -*g:vimtex_syntax_conceal_default* - Specify the default flag for the conceal feature. This is used e.g. in - |vimtex_syntax_conceal| and for package specific options in - |vimtex_syntax_packages|. +*g:vimtex_syntax_conceal_cites* + A simple dictionary to control how citation conceal should work. It has two + keys: - Default value: 1 + type~ + Specify the type of concealment. There are two options, and the + difference is best explained by example: + + Value LaTeX Concealed + ----- ----- --------- + 'icon' `\cite{x}` `📖` + 'brackets' `\cite{x}` `[x]` + + icon~ + Specify an icon for `icon` conceal. This must be a single (possibly + multibyte) character. + + Default value: > + + let g:vimtex_syntax_conceal_cites = { + \ 'type': 'icon', + \ 'icon': '📖', + \} *g:vimtex_syntax_nested* A dictionary for configuring nested syntaxes. The following keys are diff --git a/test/test-syntax/test-biblatex.vim b/test/test-syntax/test-biblatex.vim new file mode 100644 index 0000000000..b1761fd1bb --- /dev/null +++ b/test/test-syntax/test-biblatex.vim @@ -0,0 +1,16 @@ +source common.vim + +highlight Conceal ctermfg=4 ctermbg=7 guibg=NONE guifg=blue + +let g:vimtex_syntax_conceal_cites = { + \ 'type': 'brackets', + \} + +silent edit test-biblatex.tex + +split +silent wincmd w +set conceallevel=2 + +if empty($INMAKE) | finish | endif +quit!