-
Notifications
You must be signed in to change notification settings - Fork 0
/
abbrev.vim
55 lines (42 loc) · 1.38 KB
/
abbrev.vim
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
" Vim plugin for abbreviating titles in ISO4 standard
" Maintainer: André C. Silva <[email protected]>
let s:save_cpo = &cpo
set cpo&vim
if exists("g:loaded_iso4abbrev")
finish
endif
let s:path = expand('<sfile>:p:h' )
let s:path = s:path . '/../python3'
py3 import vim
py3 import os
py3 from abbr import Trie
py3 import abbr
"Building the Tries is quite expensive.
py3 abbr_prefixTrie, abbr_suffixTrie, abbr_lastWordTrie = abbr.getTries(vim.eval('s:path'))
let g:loaded_iso4abbrev = 1
if !hasmapto(':set operatorfunc=<SID>Abbreviate<cr>g@')
nnoremap <unique> <leader>a :set operatorfunc=<SID>Abbreviate<cr>g@
vnoremap <unique> <leader>a :<c-u>call <SID>Abbreviate(visualmode())<cr>
endif
function! s:Abbreviate(type)
if a:type ==# 'v' || a:type ==# 'char'
let saved = @@
"TODO make 'v' respect linebreaks
if a:type ==# 'v'
execute "normal! `<v`>d"
elseif a:type ==# 'char'
execute "normal! `[v`]d"
endif
py3 string_to_abbreviate = vim.eval("@@")
py3 vim.command('let l:abbreviation="{}"'.format(
\ abbr.cleanAndAbbreviate(vim.eval('@@'),abbr_prefixTrie,abbr_suffixTrie,abbr_lastWordTrie)))
execute "normal! i\<C-r>\<C-r>=l:abbreviation\<CR>\<Esc>"
let @@ = saved
elseif a:type ==# 'V'
'<,'>py3do return abbr.cleanAndAbbreviate(line,abbr_prefixTrie,abbr_suffixTrie,abbr_lastWordTrie)
else
return
endif
endfunction
let &cpo = s:save_cpo
unlet s:save_cpo