-
Notifications
You must be signed in to change notification settings - Fork 0
/
.vimrc.functions
161 lines (137 loc) · 4.76 KB
/
.vimrc.functions
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
" http://vim.wikia.com/wiki/Restore_cursor_to_file_position_in_previous_editing_session
" Restore cursor to file position in previous editing session
function! ResCur()
if line("'\"") <= line("$")
normal! g`"
return 1
endif
endfunction
augroup resCur
autocmd!
autocmd BufWinEnter * call ResCur()
augroup END
function! StripTrailingWhitespace()
" Preparation: save last search, and cursor position.
let _s=@/
let l = line(".")
let c = col(".")
" do the business:
%s/\s\+$//e
" clean up: restore previous search history, and cursor position
let @/=_s
call cursor(l, c)
endfunction
function! ToggleErrors()
let old_last_winnr = winnr('$')
lclose
if old_last_winnr == winnr('$')
" Nothing was closed, open syntastic error location panel
Errors
endif
endfunction
function! InitializeDirectories()
let parent = $HOME
let prefix = 'vim'
let dir_list = {
\ 'backup': 'backupdir',
\ 'views': 'viewdir',
\ 'swap': 'directory' }
let common_dir = parent . '/.' . prefix
for [dirname, settingname] in items(dir_list)
let directory = common_dir . dirname . '/'
if exists("*mkdir")
if !isdirectory(directory)
call mkdir(directory)
endif
endif
if !isdirectory(directory)
echo "Warning: Unable to create backup directory: " . directory
echo "Try: mkdir -p " . directory
else
let directory = substitute(directory, " ", "\\\\ ", "g")
exec "set " . settingname . "=" . directory
endif
endfor
endfunction
call InitializeDirectories()
function! MyFoldText()
let l:lpadding = &fdc
redir => l:signs
execute 'silent sign place buffer='.bufnr('%')
redir End
let l:lpadding += l:signs =~ 'id=' ? 2 : 0
if exists("+relativenumber")
if (&number)
let l:lpadding += max([&numberwidth, strlen(line('$'))]) + 1
elseif (&relativenumber)
let l:lpadding += max([&numberwidth, strlen(v:foldstart - line('w0')), strlen(line('w$') - v:foldstart), strlen(v:foldstart)]) + 1
endif
else
if (&number)
let l:lpadding += max([&numberwidth, strlen(line('$'))]) + 1
endif
endif
" expand tabs
let l:start = substitute(getline(v:foldstart), '\t', repeat(' ', &tabstop), 'g')
let l:end = substitute(substitute(getline(v:foldend), '\t', repeat(' ', &tabstop), 'g'), '^\s*', '', 'g')
let l:info = ' (' . (v:foldend - v:foldstart) . ')'
let l:infolen = strlen(substitute(l:info, '.', 'x', 'g'))
let l:width = winwidth(0) - l:lpadding - l:infolen
let l:separator = ' … '
let l:separatorlen = strlen(substitute(l:separator, '.', 'x', 'g'))
let l:start = strpart(l:start , 0, l:width - strlen(substitute(l:end, '.', 'x', 'g')) - l:separatorlen)
let l:text = '--' . l:start . ' … ' . l:end
return l:text . repeat(' ', l:width - strlen(substitute(l:text, ".", "x", "g"))) . l:info
endfunction
function s:CheckColorScheme()
if !has('termguicolors')
let g:base16colorspace=256
endif
let s:config_file = expand('~/.vim/.base16')
if filereadable(s:config_file)
let s:config = readfile(s:config_file, '', 2)
if s:config[1] =~# '^dark\|light$'
execute 'set background=' . s:config[1]
else
echoerr 'Bad background ' . s:config[1] . ' in ' . s:config_file
endif
if filereadable(expand('~/.vim/bundle/base16-vim/colors/base16-' . s:config[0] . '.vim'))
execute 'color base16-' . s:config[0]
else
echoerr 'Bad scheme ' . s:config[0] . ' in ' . s:config_file
endif
else " default
set background=dark
color base16-tomorrow
endif
" Allow for overrides:
" - `statusline.vim` will re-set User1, User2 etc.
" - `after/plugin/loupe.vim` will override Search.
"doautocmd ColorScheme
endfunction
set foldtext=MyFoldText()
let g:ColorColumnBlacklist = ['diff', 'undotree', 'nerdtree', 'qf']
function! s:should_colorcolumn() abort
return index(g:ColorColumnBlacklist, &filetype) == -1
endfunction
let g:CursorlineBlacklist = ['command-t']
function! s:should_cursorline() abort
return index(g:CursorlineBlacklist, &filetype) == -1
endfunction
" Make current window more obvious by turning off/adjusting some features in non-current
" windows.
if exists('+colorcolumn')
autocmd BufEnter,FocusGained,VimEnter,WinEnter * if s:should_colorcolumn() | let &l:colorcolumn='+' . join(range(0, 254), ',+') | endif
autocmd FocusLost,WinLeave * if s:should_colorcolumn() | let &l:colorcolumn=join(range(1, 255), ',') | endif
endif
autocmd InsertLeave,VimEnter,WinEnter * if s:should_cursorline() | setlocal cursorline | endif
autocmd InsertEnter,WinLeave * if s:should_cursorline() | setlocal nocursorline | endif
if v:progname !=# 'vi'
if has('autocmd')
augroup WincentAutocolor
autocmd!
autocmd FocusGained * call s:CheckColorScheme()
augroup END
endif
call s:CheckColorScheme()
endif