-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.vim
222 lines (196 loc) · 6.05 KB
/
functions.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
" Automatically create directories on save
function! s:MkNonExDir(file, buf)
if empty(getbufvar(a:buf, '&buftype')) && a:file!~#'\v^\w+\:\/'
let dir=fnamemodify(a:file, ':h')
if !isdirectory(dir)
call mkdir(dir, 'p')
endif
endif
endfunction
augroup BWCCreateDir
autocmd!
autocmd BufWritePre * :call s:MkNonExDir(expand('<afile>'), +expand('<abuf>'))
augroup END
function! Min(number, ...)
let result = a:number
let index = a:0
while index > 0
let result = (a:{index} > result) ? result : a:{index}
let index = index - 1
endwhile
return result
endf
" Sorts numbers in ascending order.
" Examples:
" [2, 3, 1, 11, 2] --> [1, 2, 2, 3, 11]
" ['2', '1', '10','-1'] --> [-1, 1, 2, 10]
function! Sorted(list)
" Make sure the list consists of numbers (and not strings)
" This also ensures that the original list is not modified
let nrs = ToNrs(a:list)
let sortedList = sort(nrs, "NaturalOrder")
echo sortedList
return sortedList
endfunction
" Comparator function for natural ordering of numbers
function! NaturalOrder(firstNr, secondNr)
if a:firstNr < a:secondNr
return -1
elseif a:firstNr > a:secondNr
return 1
else
return 0
endif
endfunction
" Coerces every element of a list to a number. Returns a new list without
" modifying the original list.
function! ToNrs(list)
let nrs = []
for elem in a:list
let nr = 0 + elem
call add(nrs, nr)
endfor
return nrs
endfunction
function! WordFrequency() range
" Words are separated by whitespace or punctuation characters
let wordSeparators = '[[:blank:][:punct:]]\+'
let allWords = split(join(getline(a:firstline, a:lastline)), wordSeparators)
let wordToCount = {}
for word in allWords
let wordToCount[word] = get(wordToCount, word, 0) + 1
endfor
let countToWords = {}
for [word,cnt] in items(wordToCount)
let words = get(countToWords,cnt,"")
" Append this word to the other words that occur as many times in the text
let countToWords[cnt] = words . " " . word
endfor
" Create a new buffer to show the results in
new
setlocal buftype=nofile bufhidden=hide noswapfile tabstop=20
" List of word counts in ascending order
let sortedWordCounts = Sorted(keys(countToWords))
call append("$", "count \t words")
call append("$", "--------------------------")
" Show the most frequent words first -> Descending order
for cnt in reverse(sortedWordCounts)
let words = countToWords[cnt]
call append("$", cnt . "\t" . words)
endfor
endfunction
function! SortWords()
" Get the visual mark points
let StartPosition = getpos("'<")
let EndPosition = getpos("'>")
if StartPosition[0] != EndPosition[0]
echoerr "Range spans multiple buffers"
elseif StartPosition[1] != EndPosition[1]
" This is a multiple line range, probably easiest to work line wise
" This could be made a lot more complicated and sort the whole
" lot, but that would require thoughts on how many
" words/characters on each line, so that can be an exercise for
" the reader!
for LineNum in range(StartPosition[1], EndPosition[1])
call setline(LineNum, join(sort(split(getline('.'), ' ')), " "))
endfor
else
" Single line range, sort words
let CurrentLine = getline(StartPosition[1])
" Split the line into the prefix, the selected bit and the suffix
" The start bit
if StartPosition[2] > 1
let StartOfLine = CurrentLine[:StartPosition[2]-2]
else
let StartOfLine = ""
endif
" The end bit
if EndPosition[2] < len(CurrentLine)
let EndOfLine = CurrentLine[EndPosition[2]:]
else
let EndOfLine = ""
endif
" The middle bit
let BitToSort = CurrentLine[StartPosition[2]-1:EndPosition[2]-1]
" Move spaces at the start of the section to variable StartOfLine
while BitToSort[0] == ' '
let BitToSort = BitToSort[1:]
let StartOfLine .= ' '
endwhile
" Move spaces at the end of the section to variable EndOfLine
while BitToSort[len(BitToSort)-1] == ' '
let BitToSort = BitToSort[:len(BitToSort)-2]
let EndOfLine = ' ' . EndOfLine
endwhile
" Sort the middle bit
let Sorted = join(sort(split(BitToSort, ' ')), ' ')
" Reform the line
let NewLine = StartOfLine . Sorted . EndOfLine
" Write it out
call setline(StartPosition[1], NewLine)
endif
endfunction
" Add argument (can be negative, default 1) to global variable i.
" Return value of i before the change.
function! Inc(...)
let result = g:i
let g:i += a:0 > 0 ? a:1 : 1
return result
endfunction
if !exists("*ReloadVimrc")
function! ReloadVimrc()
try
if expand('%:t') == 'vimrc'
update
endif
source ~/.vimrc
catch /^Vim\%((\a\+)\)\=:E/ " catch all Vim errors
redraw | echoerr v:exception
endtry
endfunction
endif
function! InsPackage()
read !xclip -o
silent s/\("\|'\|=\)//ge
silent s/-[.0-9]\+\(-r[0-9]\+\)\?[^-]*//e
silent s/^/- /
execute 'normal! =='
endfunction
" ---------------
" Make a scratch buffer with all of the leader keybindings.
"
" Adapted from http://ctoomey.com/posts/an-incremental-approach-to-vim/
" https://github.com/gcallaghan/dot_vim/blob/master/functions.vim
" ---------------
function! ListLeaders()
silent! redir @b
silent! nmap <LEADER>
silent! redir END
silent! new
silent! set buftype=nofile
silent! set bufhidden=hide
silent! setlocal noswapfile
silent! put! b
silent! g/^s*$/d
silent! %s/^.*,//
silent! normal ggVg
silent! sort
silent! let lines = getline(1,"$")
silent! normal <esc>
endfunction
" ---------------------------
" plugin helper functions {{{
" UltiSnips - Unite integration
function! UltiSnipsCallUnite()
Unite -start-insert -winheight=100 -immediately -no-empty ultisnips
return ''
endfunction
" autosave excludes
function! AbortAutoSave()
if &ft == 'gitcommit'
let g:auto_save_abort = 1
else
let g:auto_save_abort = 0
endif
endfunction
" }}}