Add FZF bibliography insertion

This commit is contained in:
Marty Oehme 2019-11-04 21:45:59 +01:00
parent e8e8290426
commit 570c575776
2 changed files with 66 additions and 55 deletions

View file

@ -52,7 +52,7 @@ Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' } " show a directory listin
Plug 'Xuyuanp/nerdtree-git-plugin', { 'on': 'NERDTreeToggle' } " show git status in nerdtree for files and dirs Plug 'Xuyuanp/nerdtree-git-plugin', { 'on': 'NERDTreeToggle' } " show git status in nerdtree for files and dirs
" Fuzzy matching " Fuzzy matching
Plug 'lotabout/skim.vim' Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --bin' }
Plug 'marty-oehme/notational-fzf-vim', { 'branch': 'origin/switch-fzf-to-skim' } Plug 'marty-oehme/notational-fzf-vim', { 'branch': 'origin/switch-fzf-to-skim' }
Plug 'dyng/ctrlsf.vim' Plug 'dyng/ctrlsf.vim'
@ -269,76 +269,86 @@ endfunction
autocmd! User GoyoEnter nested call <SID>goyo_enter() autocmd! User GoyoEnter nested call <SID>goyo_enter()
autocmd! User GoyoLeave nested call <SID>goyo_leave() autocmd! User GoyoLeave nested call <SID>goyo_leave()
" PLUGIN: fzf-bibtex " FZF Fuzzy Finding
" Citation and Bibtex utilities " set some fzf defaults
let g:bibtex_cache = '/home/marty/.cache/' let g:fzf_layout = { 'up': '~40%' }
let g:bibtex_source = '/home/marty/Nextcloud/Library/academia/academia.bib' let g:fzf_colors =
\ { 'fg': ['fg', 'Normal'],
\ 'bg': ['bg', 'Normal'],
\ 'hl': ['fg', 'Comment'],
\ 'fg+': ['fg', 'CursorLine', 'CursorColumn', 'Normal'],
\ 'bg+': ['bg', 'CursorLine', 'CursorColumn'],
\ 'hl+': ['fg', 'Statement'],
\ 'info': ['fg', 'PreProc'],
\ 'border': ['fg', 'Ignore'],
\ 'prompt': ['fg', 'Conditional'],
\ 'pointer': ['fg', 'Exception'],
\ 'marker': ['fg', 'Keyword'],
\ 'spinner': ['fg', 'Label'],
\ 'header': ['fg', 'Comment'] }
let g:fzf_action = {
\ 'ctrl-t': 'tab split',
\ 'ctrl-x': 'split',
\ 'ctrl-v': 'vsplit' }
function! Bibtex_ls(...) " set up fzf-bibtex
return 'bibtex-ls -cache ' . g:bibtex_cache . ' ' . g:bibtex_source . ' ' . join(a:000, ' ') let g:fzf_bibtex_cache = '~/.cache/'
let g:fzf_bibtex_sources = g:pandoc#biblio#bibs
" prepare bibtex_ls function to look for bib files in cwd/parent/subdirs,
" attach the standard bibtex source file
" return the command with standard cache directory filled
function! s:bibtex_ls(...)
let bibfiles = (
\ globpath('.', '*.bib', v:true, v:true) +
\ globpath('..', '*.bib', v:true, v:true) +
\ globpath('*/', '*.bib', v:true, v:true)
\ )
let bibfiles = join(bibfiles, ' ')
let source_cmd = 'bibtex-ls -cache ' . g:fzf_bibtex_cache . ' ' .bibfiles . ' ' . join(g:fzf_bibtex_sources)
return source_cmd
endfunction endfunction
" insert citation from insert mode at cursor " insert citation from normal mode at cursor (with brackets)
function! s:bibtex_cite_sink(lines)
let r=system("bibtex-cite ", a:lines)
execute ':normal! i[' . r . ']'
endfunction
command! -bang -nargs=* CiteRef call fzf#run(fzf#wrap({
\ 'source': <sid>bibtex_ls(<q-args>),
\ 'sink*': function('<sid>bibtex_cite_sink'),
\ 'up': '25%',
\ 'options': '--ansi --multi --prompt "Cite> "'
\ }))
" insert citation from insert mode at cursor (no brackets inserted)
function! s:bibtex_cite_sink_insert(lines) function! s:bibtex_cite_sink_insert(lines)
let r=system("bibtex-cite ", a:lines) let r=system("bibtex-cite ", a:lines)
execute ':normal! i' . r execute ':normal! i' . r
call feedkeys('a', 'n') call feedkeys('a', 'n')
endfunction endfunction
inoremap <silent> @@ <c-g>u<c-o>:call skim#run({ " insert markdown formatted reference
\ 'source': Bibtex_ls(),
\ 'sink*': function('<sid>bibtex_cite_sink_insert'),
\ 'up': '25%',
\ 'options': '--ansi --multi --prompt "Cite> "'})<CR>
" insert citation from normal mode at cursor
function! s:bibtex_cite_sink(lines)
let r=system("bibtex-cite ", a:lines)
execute ':normal! i' . r
endfunction
command! -bang -nargs=* CiteRef call skim#run({
\ 'source': Bibtex_ls(<q-args>),
\ 'sink*': function('<sid>bibtex_cite_sink'),
\ 'up': '25%',
\ 'options': '--ansi --multi --prompt "Cite> "'
\ })
" insert pretty formatted markdown reference
function! s:bibtex_markdown_sink(lines) function! s:bibtex_markdown_sink(lines)
let r=system("bibtex-markdown " . g:bibtex_source, a:lines) let r=system("bibtex-markdown -cache " . g:fzf_bibtex_cache . ' ' . g:fzf_bibtex_sources, a:lines)
echo join(a:lines, '; ') echo join(a:lines, '; ')
execute ':normal! i' . r execute ':normal! i' . r
endfunction endfunction
command! -bang -nargs=* CitePretty call skim#run({ command! -bang -nargs=* CiteMarkdown call fzf#run(fzf#wrap({
\ 'source': Bibtex_ls(<q-args>), \ 'source': <sid>bibtex_ls(),
\ 'sink*': function('<sid>bibtex_markdown_sink'), \ 'sink*': function('<sid>bibtex_markdown_sink'),
\ 'up': '25%', \ 'up': '25%',
\ 'options': '--ansi --multi --prompt "Markdown> "' \ 'options': '--ansi --multi --prompt "Markdown> "'
\ }) \ }))
" edit the document info.yaml " map @@ to automatically insert citation reference at cursor
" needs papis to function inoremap <silent> @@ <c-g>u<c-o>:call fzf#run(fzf#wrap({
" could be abstracted out to work with .bib file or the papis .yaml \ 'source': <sid>bibtex_ls(),
" TODO THESE FUNCTIONS DO NOT WORK YET. NEED TO FIND A WAY TO GO FROM fzf -> \ 'sink*': function('<sid>bibtex_cite_sink_insert'),
" papis -> editor
function! s:bibtex_edit_info_sink(lines)
echom "edit info sink: " . join(a:lines, ",")
let r=system("bibtex-cite ", a:lines)
let s=system("papis edit ref=\"" . join(a:lines,"") . "\"")
" execute ':e s'
echo s
endfunction
command! -bang -nargs=* CiteEdit call skim#run({
\ 'source': Bibtex_ls(<q-args>),
\ 'sink*': function('<sid>bibtex_edit_info_sink'),
\ 'up': '25%', \ 'up': '25%',
\ 'options': join([ \ 'options': '--ansi --multi --prompt "Cite> "'}))<CR>
\ '--ansi', " map <leader>cc to insert a complete citation at cursor
\ '--with-nth=..', nnoremap <silent> <leader>cc :CiteRef<cr>
\ '--preview-window=:wrap', " map <leader>cm to insert markdown prettified citation
\ '--prompt "Edit> "', nnoremap <silent> <leader>cm :CiteMarkdown<cr>
\ '--preview=''papis edit -e cat ref="echo {} | bibtex-cite" '' ',
\ ])
\})
" PLUGIN: vim-go " PLUGIN: vim-go
" change the tabstops for go to use tabs and a width of 4 " change the tabstops for go to use tabs and a width of 4

View file

@ -115,6 +115,7 @@ Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --bin' }
call plug#end() call plug#end()
endif endif
" FZF Fuzzy Finding
" set some fzf defaults " set some fzf defaults
let g:fzf_layout = { 'up': '~40%' } let g:fzf_layout = { 'up': '~40%' }
let g:fzf_colors = let g:fzf_colors =