From 860f6364b954cc710d3773c5299479e6099cbb9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Sun, 4 Apr 2021 18:14:37 +0200 Subject: [PATCH] fix: cite context on \*cites{x}{y} refer: #2016 --- autoload/vimtex/cmd.vim | 29 +++++++++++++++------------ autoload/vimtex/context.vim | 27 +++++++++++++++++++++++++ autoload/vimtex/context/cite.vim | 9 +++++++-- test/test-context-cite/Makefile | 14 +++++++++++++ test/test-context-cite/test-cites.bib | 13 ++++++++++++ test/test-context-cite/test-cites.tex | 13 ++++++++++++ test/test-context-cite/test-cites.vim | 19 ++++++++++++++++++ 7 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 test/test-context-cite/Makefile create mode 100644 test/test-context-cite/test-cites.bib create mode 100644 test/test-context-cite/test-cites.tex create mode 100644 test/test-context-cite/test-cites.vim diff --git a/autoload/vimtex/cmd.vim b/autoload/vimtex/cmd.vim index f81842a70c..e589704219 100644 --- a/autoload/vimtex/cmd.vim +++ b/autoload/vimtex/cmd.vim @@ -623,22 +623,25 @@ function! s:get_cmd(direction) abort " {{{1 let res.pos_end.cnum = res.overlay.close.cnum endif - " Get options - let opt = s:get_cmd_part('[', res.pos_end) - while !empty(opt) - call add(res.opts, opt) - let res.pos_end.lnum = opt.close.lnum - let res.pos_end.cnum = opt.close.cnum + " Get options and arguments + while v:true let opt = s:get_cmd_part('[', res.pos_end) - endwhile + if !empty(opt) + call add(res.opts, opt) + let res.pos_end.lnum = opt.close.lnum + let res.pos_end.cnum = opt.close.cnum + continue + endif - " Get arguments - let arg = s:get_cmd_part('{', res.pos_end) - while !empty(arg) - call add(res.args, arg) - let res.pos_end.lnum = arg.close.lnum - let res.pos_end.cnum = arg.close.cnum let arg = s:get_cmd_part('{', res.pos_end) + if !empty(arg) + call add(res.args, arg) + let res.pos_end.lnum = arg.close.lnum + let res.pos_end.cnum = arg.close.cnum + continue + endif + + break endwhile " Include entire cmd text diff --git a/autoload/vimtex/context.vim b/autoload/vimtex/context.vim index b6cec35f2a..a2d07183e9 100644 --- a/autoload/vimtex/context.vim +++ b/autoload/vimtex/context.vim @@ -39,3 +39,30 @@ function! vimtex#context#menu() abort " {{{1 endfunction " }}}1 +function! vimtex#context#get(...) abort " {{{1 + if a:0 > 0 + let l:pos_saved = vimtex#pos#get_cursor() + call vimtex#pos#set_cursor(a:000) + endif + + let l:cmd = vimtex#cmd#get_current() + let l:word = expand('') + + if a:0 > 0 + call vimtex#pos#set_cursor(l:pos_saved) + endif + + if empty(l:cmd) | return | endif + + for l:handler in b:vimtex.context_menu + if l:handler.match(l:cmd, l:word) + return { + \ 'cmd': l:cmd, + \ 'word': l:word, + \ 'handler': l:handler, + \} + endif + endfor +endfunction + +" }}}1 diff --git a/autoload/vimtex/context/cite.vim b/autoload/vimtex/context/cite.vim index 4fc4549d80..cfc998cd91 100644 --- a/autoload/vimtex/context/cite.vim +++ b/autoload/vimtex/context/cite.vim @@ -19,11 +19,16 @@ function! s:handler.match(cmd, word) abort dict " {{{1 return v:false endif - if len(a:cmd.args) != 1 + if len(a:cmd.args) < 1 || len(a:cmd.args) > 2 return v:false endif - let self.cites = split(a:cmd.args[0].text, ',\s*') + let l:text = a:cmd.args[0].text + if len(a:cmd.args) == 2 + let l:text .= ',' . a:cmd.args[1].text + endif + + let self.cites = split(l:text, ',\s*') if index(self.cites, a:word) >= 0 let self.selected = a:word else diff --git a/test/test-context-cite/Makefile b/test/test-context-cite/Makefile new file mode 100644 index 0000000000..c18c1db586 --- /dev/null +++ b/test/test-context-cite/Makefile @@ -0,0 +1,14 @@ +MYVIM ?= nvim --headless + +INMAKE := 1 +export INMAKE + +TESTS := $(wildcard test*.vim) +TESTS := $(TESTS:.vim=) + +.PHONY: test $(TESTS) + +test: $(TESTS) + +$(TESTS): + @$(MYVIM) -u $@.vim diff --git a/test/test-context-cite/test-cites.bib b/test/test-context-cite/test-cites.bib new file mode 100644 index 0000000000..7e730347fd --- /dev/null +++ b/test/test-context-cite/test-cites.bib @@ -0,0 +1,13 @@ +@book{Hemingway1940, + author = {Earnest Hemingway}, + title = {For Whom the Bell Tolls}, + date = {1940}, + publisher = {Simon \& Schuster}, +} + +@book{JiM2020, + author = {{Justice in Mexico}}, + title = {Organized Crime and Violence in Mexico: 2020 Special Report}, + date = {2019-07}, + publisher = {Justice in Mexico}, +} diff --git a/test/test-context-cite/test-cites.tex b/test/test-context-cite/test-cites.tex new file mode 100644 index 0000000000..d0bfda7a86 --- /dev/null +++ b/test/test-context-cite/test-cites.tex @@ -0,0 +1,13 @@ +\documentclass{article} +\usepackage{biblatex} +\addbibresource{test-cites.bib} + +\begin{document} +\begin{enumerate} + \item \autocite[10]{Hemingway1940} + \item \autocites{Hemingway1940}{JiM2020} + \item \autocites[10]{JiM2020}[20]{Hemingway1940} + \item \autocites[10]{Hemingway1940}{JiM2020} + \item \autocites{Hemingway1940}[20]{JiM2020} +\end{enumerate} +\end{document} diff --git a/test/test-context-cite/test-cites.vim b/test/test-context-cite/test-cites.vim new file mode 100644 index 0000000000..04a430d088 --- /dev/null +++ b/test/test-context-cite/test-cites.vim @@ -0,0 +1,19 @@ +set nocompatible +let &rtp = '../..,' . &rtp +filetype plugin indent on +syntax enable + +set nomore + +nnoremap q :qall! + +silent edit test-cites.tex + +if empty($INMAKE) | finish | endif + +call vimtex#test#assert_equal('Hemingway1940', + \ vimtex#context#get(9, 49).handler.selected) +call vimtex#test#assert_equal('JiM2020', + \ vimtex#context#get(11, 39).handler.selected) + +quit!