File: cite.vim

package info (click to toggle)
vim-vimtex 2.17-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,844 kB
  • sloc: makefile: 360; python: 103
file content (182 lines) | stat: -rw-r--r-- 4,724 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
" VimTeX - LaTeX plugin for Vim
"
" Maintainer: Karl Yngve LervÄg
" Email:      karl.yngve@gmail.com
"

function! vimtex#context#cite#new() abort " {{{1
  return deepcopy(s:handler)
endfunction

" }}}1

let s:handler = {
      \ 'name': 'citation handler',
      \}
function! s:handler.match(cmd, word) abort dict " {{{1
  let self.selected = vimtex#cite#get_key(a:cmd, a:word)
  return !empty(self.selected)
endfunction

" }}}1
function! s:handler.get_actions() abort dict " {{{1
  let l:entry = vimtex#cite#get_entry(self.selected)

  if empty(l:entry)
    call vimtex#log#warning('Cite key not found: ' .. self.selected)
    return {}
  endif

  return s:actions.create(l:entry)
endfunction

" }}}1

let s:actions = {
      \ 'menu': [
      \   {'name': 'Edit entry',
      \    'func': 'edit'},
      \   {'name': 'Show entry',
      \    'func': 'show'},
      \ ],
      \}
function! s:actions.create(entry) abort dict " {{{1
  let l:new = deepcopy(self)
  unlet l:new.create

  let l:new.entry = deepcopy(a:entry)
  let l:new.prompt = 'Context menu for citekey ' .. a:entry.key

  if has_key(a:entry, 'file')
    let l:pdfs = filter(split(a:entry.file, ';'),
          \ {_, x -> fnamemodify(x, ':e') ==? 'pdf'})
    if !empty(l:pdfs)
      let l:new.pdfs = map(l:pdfs, {_, x -> expand(x)})
      call add(l:new.menu, {'name': 'Open PDF', 'func': 'open_pdf'})
    endif
  endif

  if has_key(a:entry, 'doi')
    call add(l:new.menu, {'name': 'Open doi', 'func': 'open_doi'})
  endif

  if has_key(a:entry, 'eprint')
        \ && (a:entry.eprint[0:4] ==# 'arXiv'
        \     || (has_key(a:entry, 'archiveprefix')
        \         && a:entry.archiveprefix ==# 'arXiv'))
    call add(l:new.menu, {'name': 'Open arXiv', 'func': 'open_arxiv'})
  endif

  if has_key(a:entry, 'url')
    call add(l:new.menu, {'name': 'Open url', 'func': 'open_url'})
  endif

  if executable('zotero')
    call add(l:new.menu, {'name': 'Open in Zotero', 'func': 'open_zotero'})
  endif

  if vimtex#util#get_os() ==# 'mac'
    let l:output = vimtex#jobs#capture(
          \ 'osascript -l JavaScript -e ''Application("BibDesk").id()''')
    if join(l:output) =~# 'edu.ucsd.cs.mmccrack.bibdesk'
      call add(l:new.menu, {'name': 'Open in BibDesk', 'func': 'open_bdsk'})
    endif
  endif

  return l:new
endfunction

" }}}1
function! s:actions.show() abort dict " {{{1
  let l:entry = deepcopy(self.entry)

  call vimtex#ui#echo([
        \ ['Normal', '@'],
        \ ['VimtexMsg', l:entry.type],
        \ ['Normal', '{'],
        \ ['Special', l:entry.key],
        \ ['Normal', ','],
        \])

  for l:x in ['key', 'type', 'source_lnum', 'source_file']
    if has_key(l:entry, l:x)
      call remove(l:entry, l:x)
    endif
  endfor

  for l:x in ['title', 'author', 'year']
    if has_key(l:entry, l:x)
      call vimtex#ui#echo([
            \ ['VimtexInfoValue', '  ' .. l:x .. ': '],
            \ ['Normal', remove(l:entry, l:x)]
            \])
    endif
  endfor

  for [l:key, l:val] in items(l:entry)
      call vimtex#ui#echo([
            \ ['VimtexInfoValue', '  ' .. l:key .. ': '],
            \ ['Normal', l:val]
            \])
  endfor
  call vimtex#ui#echo([['Normal', '}']])
endfunction

" }}}1
function! s:actions.edit() abort dict " {{{1
  execute 'edit' self.entry.source_file
  filetype detect

  call vimtex#pos#set_cursor(self.entry.source_lnum, 0)
  normal! zv
endfunction

" }}}1
function! s:actions.open_pdf() abort dict " {{{1
  let l:readable = filter(self.pdfs, {_, x -> filereadable(x)})
  if empty(l:readable)
    call vimtex#log#warning('Could not open PDF file!')
    for l:file in self.pdfs
      call vimtex#log#info('Filename: ' .. l:file)
    endfor
    return
  endif

  let l:file = vimtex#ui#select(l:readable, {
        \ 'prompt': 'Open file:',
        \})
  if empty(l:file) | return | endif

  call vimtex#jobs#start(
        \ g:vimtex_context_pdf_viewer
        \ .. ' ' .. vimtex#util#shellescape(l:file),
        \ {'detached': v:true})
endfunction

" }}}1
function! s:actions.open_arxiv() abort dict " {{{1
  let l:id = matchstr(self.entry.eprint, '\v^(arXiv:)?\zs.*')
  call vimtex#util#www('https://arxiv.org/abs/' .. l:id)
endfunction

" }}}1
function! s:actions.open_doi() abort dict " {{{1
  call vimtex#util#www('http://dx.doi.org/' .. self.entry.doi)
endfunction

" }}}1
function! s:actions.open_url() abort dict " {{{1
  call vimtex#util#www(self.entry.url)
endfunction

" }}}1
function! s:actions.open_zotero() abort dict " {{{1
  call vimtex#util#www('zotero://select/items/bbt:' .. self.entry.key)
endfunction

" }}}1
function! s:actions.open_bdsk() abort dict " {{{1
  call vimtex#util#www('x-bdsk://' .. vimtex#util#url_encode(self.entry.key))
endfunction

" }}}1