This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
forked from mutewinter/dot_vim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.vim
170 lines (146 loc) · 3.95 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
" ----------------------------------------
" 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()
map <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()
nmap <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')
" ---------------
" 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
"strip all trailing white space
command! StripTrailingWhiteSpace :call Preserve("%s/\\s\\+$//e")<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()
nmap <silent> <leader>p :PasteWithPasteMode<CR>
" ---------------
" Write Buffer
"
" Writes the current buffer unless we're the in QuickFix mode.
" ---------------
function WriteBuffer()
if &filetype == "qf"
execute "normal! \<enter>"
else
:write
endif
endfunction
noremap <silent> <enter> :call WriteBuffer()<CR>