-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_complete.vim
170 lines (155 loc) · 5.75 KB
/
word_complete.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
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
" word_complete.vim: (global plugin) automatically offer word completion
" Last Change: Wed 6 Dec 2006 12:51:42 PM EST
" Author: Benji Fisher <[email protected]>
" Version: 1.1, for Vim 7.0+
" URL: http://vim.sourceforge.net/scripts/script.php?script_id=73
" DESCRIPTION:
" Each time you type an alphabetic character, the script attempts
" to complete the current word. The suggested completion is selected
" in Select mode, so you can just type another character to keep going.
" Other options: <Tab> to accept, <BS> to get rid of the completion,
" <Esc> to leave Insert mode without accepting the completion, <C-N>
" or <C-P> to cycle through choices, <C-X> to enter <C-X> mode.
" LIMITATIONS:
" The script works by :imap'ping each alphabetic character, and uses
" Insert-mode completion (:help ins-completion). It is far from perfect.
" Since characters you type are not entered in a single round of Insert mode,
" abbreviations will usually not work. I have at least one report that
" mswin.vim interferes a little. Since Select mode uses the same mappings as
" Visual mode, the special keys mentioned above may conflict with what you
" are used to in Visual mode.
" INSTALLATION:
" :source it from your vimrc file or drop it in your plugin directory.
" To activate, choose "Word Completion" from the Tools menu, or type
" :call DoWordComplete()
" To make it stop, choose "Tools/Stop Completion", or type
" :call EndWordComplete()
" If you want to activate the script by default, add the line
" autocmd BufEnter * call DoWordComplete()
" to your vimrc file.
" USER CONFIGURATION:
" Use this section to change some of the defaults. Before upgrading to a new
" version of this file, you should copy the following section to a file
" word_complete.vimrc in your plugin directory, normally the same directory
" as this file. If your system administrator has installed this file, you
" should install word_complete.vimrc in your after/plugin/ directory so that
" your choices override the system defaults. See
" :help ftplugin-overrule
" :help 'runtimepath'
" for details.
" ==================== file word_complete.vimrc ====================
" User Configuration file for word_complete.vim .
" To use this, uncomment and change the defaults.
" Do not complete words shorter than this length:
" let g:WC_min_len = 1
" Use this key to accept the offered completion:
" let g:WC_accept_key = "<Tab>"
" ==================== end: word_complete.vimrc ====================
" source the user configuration file(s):
runtime! plugin/<sfile>:t:r.vimrc
" Use the values from the configuration file(s) or the defaults:
let s:min_len = exists("g:WC_min_len") ? g:WC_min_len : 1
let s:accept_key = exists("g:WC_accept_key") ? g:WC_accept_key : "<Tab>"
" Use Vim defaults while :source'ing this file.
let save_cpo = &cpo
set cpo&vim
if has("menu")
amenu &Tools.&Word\ Completion :call DoWordComplete()<CR>
amenu &Tools.&Stop\ Completion :call EndWordComplete()<CR>
endif
" Return the :lmap value if there is one, otherwise echo the input.
fun! s:Langmap(char)
let val = maparg(a:char, "l")
return (val != "") ? val : a:char
endfun
" The :startinsert command does not have an option for acting like "a"
" instead of "i" so this implements it.
fun! s:StartAppend()
if strlen(getline(".")) > col(".")
normal l
startinsert
else
startinsert!
endif
endfun
fun! WordComplete()
let length=strlen(expand("<cword>"))
" Do not try to complete 1- nor 2-character words.
if length < s:min_len
call s:StartAppend()
return
endif
" Save and reset the 'ignorecase' option.
let save_ic = &ignorecase
set noignorecase
" Use language maps (keymaps) if appropriate.
if &iminsert == 1
let char = getline(".")[col(".")-1]
let lchar = maparg(char, "l")
if lchar != ""
execute "normal! r" . lchar
endif
endif
" If at EOL or before a space or punctuation character, do completion.
if strlen(getline(".")) == col(".")
\ || getline(".")[col(".")] =~ '[[:punct:][:space:]]'
execute "normal a\<C-P>\<Esc>"
endif
" If a match was found, highlight the completed part in Select mode.
if strlen(expand("<cword>")) > length
execute "normal viwo" . length . "l\<C-G>"
else " ... just return to Insert mode.
if version > 505
call s:StartAppend()
else
execute "normal a*\<Esc>gh"
endif "version > 505
endif
" Restore the 'ignorecase' option.
let &ignorecase = save_ic
endfun
" Make an :imap for each alphabetic character, and define a few :smap's.
fun! DoWordComplete()
execute "snoremap <buffer>" s:accept_key "<Esc>`>a"
snoremap <buffer> <Esc> d
if has("mac")
snoremap <buffer> <Del>a
else
snoremap <buffer> <BS> <Del>a
endif "has("mac")
if version > 505
snoremap <buffer> <C-N> <Del>a<C-N>
snoremap <buffer> <C-P> <Del>a<C-P><C-P>
snoremap <buffer> <C-X> <Del>a<C-P><C-X>
endif "version > 505
" Thanks to Bohdan Vlasyuk for suggesting a loop here:
let letter = "a"
while letter <=# "z"
execute "inoremap <buffer>" letter letter . "<Esc>:call WordComplete()<CR>"
let letter = nr2char(char2nr(letter) + 1)
endwhile
endfun
" Remove all the mappings created by DoWordComplete().
" Lazy: I do not save and restore existing mappings.
fun! EndWordComplete()
execute "vunmap <buffer>" s:accept_key
vunmap <buffer> <Esc>
if has("mac")
vunmap <buffer>
else
vunmap <buffer> <BS>
endif "has("mac")
if version > 505
vunmap <buffer> <C-N>
vunmap <buffer> <C-P>
vunmap <buffer> <C-X>
endif "version > 505
" Thanks to Bohdan Vlasyuk for suggesting a loop here:
let letter = char2nr("a")
while letter <= char2nr("z")
execute "iunmap <buffer>" nr2char(letter)
let letter = letter + 1
endwhile
endfun
let &cpo = save_cpo
" vim:sts=2:sw=2:ff=unix: