commit
e869cddff1
14
README.md
14
README.md
@ -20,6 +20,20 @@ Add these lines to the `.vimrc`:
|
||||
|
||||
The `:Papis` command will open a search window for your bibliographic database. `Enter` command will insert citation for the selected record in the current buffer.
|
||||
|
||||
The `:PapisView` command will open the pdf file of the citation currently under your cursor with the same pdf-reader used by papis.
|
||||
This currently only works when using whoosh as backend for the papis database. Add the following to your `config` file of papis:
|
||||
|
||||
database-backend = whoosh
|
||||
whoosh-schema-fields = ['ref']
|
||||
|
||||
The second is needed to enable whoosh for searches through the ref field.
|
||||
|
||||
|
||||
Add the following to your `tex.vim` file for useful keyboard shortcuts:
|
||||
|
||||
nnoremap <buffer> <localleader>pc :Papis<cr>
|
||||
nnoremap <buffer> <localleader>pv :PapisView<cr>
|
||||
|
||||
## Documentation
|
||||
|
||||
For more information, execute `:help papis` in Vim.
|
||||
|
122
plugin/papis.vim
122
plugin/papis.vim
@ -4,39 +4,115 @@ function! s:yank_to_register(data)
|
||||
silent! let @+ = a:data
|
||||
endfunction
|
||||
|
||||
function! s:get_reference(line)
|
||||
let l:pat='@\v(.*)$'
|
||||
return substitute(matchlist(a:line, l:pat)[1], "/", "", "g")
|
||||
endfunction
|
||||
|
||||
function! s:inside_cite_body()
|
||||
execute "silent! normal! mq?\\\\cite\\|}\r"
|
||||
let l:inbody = getline('.')[col('.')-1: col('.') + 3] ==# '\cite'
|
||||
execute "normal! `q"
|
||||
return l:inbody
|
||||
endfunction
|
||||
|
||||
function! s:handler(a)
|
||||
let lines = a:a
|
||||
if lines == [] || lines == ['','','']
|
||||
let l:lines = a:a
|
||||
if l:lines == [] || l:lines == ['','','']
|
||||
return
|
||||
endif
|
||||
" Expect at least 2 elements, `query` and `keypress`, which may be empty
|
||||
" strings.
|
||||
let query = lines[0]
|
||||
let keypress = lines[1]
|
||||
let cmd = "normal a"
|
||||
let pat = '@\v(.{-})$'
|
||||
" it is possible to yank the doc id using the ctrl-y keypress
|
||||
if keypress ==? "ctrl-y"
|
||||
let hashes = join(filter(map(lines[2:], 'matchlist(v:val, pat)[1]'), 'len(v:val)'), "\n")
|
||||
return s:yank_to_register(hashes)
|
||||
" this will insert \cite{id} command for all selected citations
|
||||
else
|
||||
let citations = lines[2:]
|
||||
let candidates = []
|
||||
for line in citations
|
||||
let id = matchlist(line, pat)[1]
|
||||
call add(candidates, "\\cite{". id . "}")
|
||||
endfor
|
||||
endif
|
||||
let l:keypress = l:lines[1]
|
||||
let l:citations = l:lines[2:]
|
||||
let l:candidates = []
|
||||
|
||||
for candidate in candidates
|
||||
execute join([cmd, candidate])
|
||||
" Making list of the things to cite
|
||||
for l:line in l:citations
|
||||
call add(l:candidates, s:get_reference(l:line))
|
||||
endfor
|
||||
|
||||
" it is possible to yank the doc id using the ctrl-y keypress
|
||||
if l:keypress ==? "ctrl-y"
|
||||
execute s:yank_to_register(join(l:candidates, "\n"))
|
||||
" this will insert \cite{id} command for all selected citations
|
||||
else
|
||||
" If you are already in a \cite{} body
|
||||
if s:inside_cite_body()
|
||||
execute "normal! \/\}\rgea, " . join(l:candidates, ", ") ."\e"
|
||||
" start a fresh \cite{} body
|
||||
else
|
||||
execute "normal! a\\cite{" . join(l:candidates, ", ") . "}\e"
|
||||
endif
|
||||
endif
|
||||
endfunction
|
||||
|
||||
let g:PapisFormat = '"{doc[author]}: {doc[title]}'
|
||||
let g:PapisBackend = ''
|
||||
|
||||
command! -bang -nargs=* Papis
|
||||
\ call fzf#run(fzf#wrap({'source': 'papis list <args> --format "{doc[author]}: {doc[title]} @{doc[ref]}"', 'sink*': function('<sid>handler'), 'options': '--multi --expect=ctrl-y --print-query'}))
|
||||
function! s:getPapisBackend()
|
||||
if g:PapisBackend ==# ''
|
||||
let g:PapisBackend = systemlist('papis config database-backend')[0]
|
||||
endif
|
||||
return g:PapisBackend
|
||||
endfunction
|
||||
|
||||
function! s:Papis(searchline)
|
||||
let l:searchinp = a:searchline
|
||||
if l:searchinp ==# ''
|
||||
if s:getPapisBackend() ==# "whoosh"
|
||||
let l:searchinp = '"*"'
|
||||
endif
|
||||
endif
|
||||
call fzf#run(fzf#wrap({'source': 'papis list ' . l:searchinp . ' --format ' . g:PapisFormat . ' @{doc[ref]}"', 'sink*': function('s:handler'), 'options': '--multi --expect=ctrl-y --print-query'}))
|
||||
endfunction
|
||||
|
||||
command! -bang -nargs=* Papis call s:Papis('<args>')
|
||||
|
||||
function! s:get_citeref(cite, full_list)
|
||||
for l:ref in a:full_list
|
||||
if a:cite ==# substitute(l:ref, "/", "", "g")
|
||||
return l:ref
|
||||
endif
|
||||
endfor
|
||||
endfunction
|
||||
|
||||
function! s:get_all_citerefs()
|
||||
return systemlist('papis list "ref:*" --format "{doc[ref]}"')
|
||||
endfunction
|
||||
|
||||
function! s:get_cite_under_cursor()
|
||||
if s:inside_cite_body()
|
||||
if getline('.')[col('.') -1] ==# ','
|
||||
return
|
||||
endif
|
||||
|
||||
execute "silent! normal! mq/[}{]\r"
|
||||
if getline('.')[col('.') -1] ==# '{'
|
||||
execute "silent! normal! `q"
|
||||
return
|
||||
endif
|
||||
|
||||
execute "silent! normal! `q"
|
||||
execute "silent! normal! ?[{,]\rwv/[,}]\rge\"qy`q"
|
||||
return @q
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:PapisView()
|
||||
if s:getPapisBackend() !=# "whoosh"
|
||||
echom "PapisView only works for Papis with whoosh as database-backend at the moment"
|
||||
return
|
||||
endif
|
||||
|
||||
let l:cite = s:get_cite_under_cursor()
|
||||
if l:cite ==# ""
|
||||
return
|
||||
endif
|
||||
|
||||
let l:full_list = s:get_all_citerefs()
|
||||
let l:ref = s:get_citeref(l:cite, l:full_list)
|
||||
call system('papis open "ref:' . l:ref . '"')
|
||||
endfunction
|
||||
|
||||
command! -bang PapisView call s:PapisView()
|
||||
|
Loading…
Reference in New Issue
Block a user