forked from mutewinter/dot_vim
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.vim
More file actions
255 lines (223 loc) · 6.43 KB
/
functions.vim
File metadata and controls
255 lines (223 loc) · 6.43 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
" ----------------------------------------
" Functions
" ----------------------------------------
" ---------------
" OpenURL
" ---------------
if has('ruby')
ruby << EOF
require 'open-uri'
require 'openssl'
def extract_url(url)
re = %r{(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|
[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]
+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]\{\};:'"
.,<>?«»“”‘’]))}
url.match(re).to_s
end
def open_url
line = VIM::Buffer.current.line
if url = extract_url(line)
if RUBY_PLATFORM.downcase =~ /(win|mingw)(32|64)/
`start cmd /c chrome #{url}`
VIM::message("Opened #{url}")
else
`open #{url}`
VIM::message("Opened #{url}")
end
else
VIM::message("No URL found on this line.")
end
end
# Returns the contents of the <title> tag of a given page
def fetch_title(url)
if RUBY_VERSION < '1.9'
open(url).read.match(/<title>(.*?)<\/title>?/i)[1]
else
open(url, :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE).read.
match(/<title>(.*?)<\/title>?/i)[1]
end
end
# Paste the title and url for the url on the clipboard in markdown format:
# [Title](url)
# Note: Clobbers p register
def paste_url_and_title
clipboard = VIM::evaluate('@+')
url = extract_url(clipboard)
if url and url.strip != ""
puts "Fetching title"
title = fetch_title(url)
VIM::command "let @p = '[#{title}](#{url})'"
VIM::command 'normal! "pp'
else
VIM::message("Clipboard does not contain URL: '#{clipboard[1..10]}'...")
end
end
EOF
" Open a URL
if !exists("*OpenURL")
function! OpenURL()
:ruby open_url
endfunction
endif
command! OpenUrl call OpenURL()
nnoremap <leader>o :call OpenURL()<CR>
" ---------------
" Paste link with Title
" ---------------
" Open a URL
if !exists("*PasteURLTitle")
function! PasteURLTitle()
:ruby paste_url_and_title
endfunction
endif
command! PasteURLTitle call PasteURLTitle()
noremap <leader>pt :PasteURLTitle<CR>
endif " endif has('ruby')
" ---------------
" Quick spelling fix (first item in z= list)
" ---------------
function! QuickSpellingFix()
if &spell
normal 1z=
else
" Enable spelling mode and do the correction
set spell
normal 1z=
set nospell
endif
endfunction
command! QuickSpellingFix call QuickSpellingFix()
nnoremap <silent> <leader>z :QuickSpellingFix<CR>
" ---------------
" Convert Ruby 1.8 hash rockets to 1.9 JSON style hashes.
" From: http://git.io/cxmJDw
" Note: Defaults to the entire file unless in visual mode.
" ---------------
command! -bar -range=% NotRocket execute
\'<line1>,<line2>s/:\(\w\+\)\s*=>/\1:/e' . (&gdefault ? '' : 'g')
" ------------------------------------
" Convert .should rspec syntax to expect.
" From: https://coderwall.com/p/o2oyrg
" ------------------------------------
command! -bar -range=% Expect execute
\'<line1>,<line2>s/\(\S\+\).should\(\s\+\)==\s*\(.\+\)' .
\'/expect(\1).to\2eq(\3)/e' .
\(&gdefault ? '' : 'g')
" ---------------
" Strip Trailing White Space
" ---------------
" From http://vimbits.com/bits/377
" Preserves/Saves the state, executes a command, and returns to the saved state
function! Preserve(command)
" Preparation: save last search, and cursor position.
let _s=@/
let l = line(".")
let c = col(".")
" Do the business:
execute a:command
" Clean up: restore previous search history, and cursor position
let @/=_s
call cursor(l, c)
endfunction
function! StripTrailingWhiteSpaceAndSave()
:call Preserve("%s/\\s\\+$//e")<CR>
:write
endfunction
command! StripTrailingWhiteSpaceAndSave :call StripTrailingWhiteSpaceAndSave()<CR>
nnoremap <silent> <leader>stw :silent! StripTrailingWhiteSpaceAndSave<CR>
" ---------------
" Paste using Paste Mode
"
" Keeps indentation in source.
" ---------------
function! PasteWithPasteMode()
if &paste
normal p
else
" Enable paste mode and paste the text, then disable paste mode.
set paste
normal p
set nopaste
endif
endfunction
command! PasteWithPasteMode call PasteWithPasteMode()
nnoremap <silent> <leader>p :PasteWithPasteMode<CR>
" ---------------
" Write Buffer if Necessary
"
" Writes the current buffer if it's needed, unless we're the in QuickFix mode.
" ---------------
function WriteBufferIfNecessary()
if &modified && !&readonly
:write
endif
endfunction
command! WriteBufferIfNecessary call WriteBufferIfNecessary()
function CRWriteIfNecessary()
if &filetype == "qf"
" Execute a normal enter when in Quickfix list.
execute "normal! \<enter>"
else
WriteBufferIfNecessary
endif
endfunction
" Clear the search buffer when hitting return
" Idea for MapCR from http://git.io/pt8kjA
function! MapCR()
nnoremap <silent> <enter> :call CRWriteIfNecessary()<CR>
endfunction
call MapCR()
" ---------------
" Make a scratch buffer with all of the leader keybindings.
"
" Adapted from http://ctoomey.com/posts/an-incremental-approach-to-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
command! ListLeaders :call ListLeaders()
function! CopyMatches(reg)
let hits = []
%s//\=len(add(hits, submatch(0))) ? submatch(0) : ''/ge
let reg = empty(a:reg) ? '+' : a:reg
execute 'let @'.reg.' = join(hits, "\n") . "\n"'
endfunction
command! -register CopyMatches call CopyMatches(<q-reg>)
function! YankLineWithoutNewline()
let l = line(".")
let c = col(".")
normal ^y$
" Clean up: restore previous search history, and cursor position
call cursor(l, c)
endfunction
nnoremap <silent>yl :call YankLineWithoutNewline()<CR>
" Show the word frequency of the current buffer in a split.
" from: http://vim.wikia.com/wiki/Word_frequency_statistics_for_a_file
function! WordFrequency() range
let all = split(join(getline(a:firstline, a:lastline)), '\A\+')
let frequencies = {}
for word in all
let frequencies[word] = get(frequencies, word, 0) + 1
endfor
new
setlocal buftype=nofile bufhidden=hide noswapfile tabstop=20
for [key,value] in items(frequencies)
call append('$', key."\t".value)
endfor
sort i
endfunction
command! -range=% WordFrequency <line1>,<line2>call WordFrequency()