-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathutil.vim
More file actions
148 lines (121 loc) · 4.25 KB
/
util.vim
File metadata and controls
148 lines (121 loc) · 4.25 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
function! ctrlspace#util#system(cmd, ...) abort
if has('win32') && &shell !~? 'cmd'
let saved_shell = [
\ &shell,
\ &shellcmdflag,
\ &shellxquote,
\ &shellxescape,
\ &shellquote,
\ &shellpipe,
\ &shellredir,
\ &shellslash
\]
set shell& shellcmdflag& shellxquote& shellxescape&
set shellquote& shellpipe& shellredir& shellslash&
endif
let output = a:0 > 0 ? system(a:cmd, a:1) : system(a:cmd)
if exists('saved_shell')
let [ &shell,
\ &shellcmdflag,
\ &shellxquote,
\ &shellxescape,
\ &shellquote,
\ &shellpipe,
\ &shellredir,
\ &shellslash ] = saved_shell
endif
return has('win32') ? substitute(output, "\r", '', 'g') : output
endfunction
function! ctrlspace#util#NormalizeDirectory(directory) abort
let directory = resolve(expand(a:directory))
while directory[strlen(directory) - 1] ==# "/" || directory[strlen(directory) - 1] ==# "\\"
let directory = directory[0:-2]
endwhile
return directory
endfunction
function! ctrlspace#util#HandleVimSettings(switch) abort
call s:handleSwitchbuf(a:switch)
call s:handleAutochdir(a:switch)
endfunction
function! s:handleSwitchbuf(switch) abort
if (a:switch ==# "start") && !empty(&swb)
let s:swbSave = &swb
set swb=
elseif (a:switch ==# "stop") && exists("s:swbSave")
let &swb = s:swbSave
unlet s:swbSave
endif
endfunction
function! s:handleAutochdir(switch) abort
if (a:switch ==# "start") && &acd
let s:acdWasOn = 1
set noacd
elseif (a:switch ==# "stop") && exists("s:acdWasOn")
set acd
unlet s:acdWasOn
endif
endfunction
function! ctrlspace#util#WorkspaceFile() abort
let config = ctrlspace#context#Configuration()
return filereadable(config.WorkspaceFile) ? config.WorkspaceFile : s:internalFilePath("cs_workspaces")
endfunction
function! ctrlspace#util#FilesCache() abort
return s:internalFilePath("cs_files")
endfunction
function! ctrlspace#util#ChDir(dir) abort
let dir = fnameescape(a:dir)
let curtab = tabpagenr()
let curwin = winnr()
silent! exe "cd " . dir
for t in range(1, tabpagenr("$"))
silent! exe "noautocmd tabnext " . t
for w in range(1, winnr("$"))
silent! exe "noautocmd " . w . "wincmd w"
if haslocaldir()
silent! exe "lcd " . dir
endif
endfor
endfor
silent! exe "noautocmd tabnext " . curtab
silent! exe "noautocmd " . curwin . "wincmd w"
endfunction
function! s:internalFilePath(name) abort
let config = ctrlspace#context#Configuration()
let root = ctrlspace#roots#CurrentProjectRoot()
let fullPart = empty(root) ? "" : (root . "/")
if !empty(config.ProjectRootMarkers)
for candidate in config.ProjectRootMarkers
let candidatePath = fullPart . candidate
if isdirectory(candidatePath)
return candidatePath . "/" . a:name
endif
endfor
endif
return fullPart . "." . a:name
endfunction
" Workaround for a Vim bug after :only and e.g. help window:
" for the first time after :only gettabvar cannot properly ready any tab variable
" More info: https://github.com/vim/vim/issues/394
" TODO Remove when decided to drop support for Vim 7.3
function! ctrlspace#util#Gettabvar(nr, name) abort
let value = gettabvar(a:nr, a:name)
if type(value) == 1 && empty(value)
unlet value
let value = gettabvar(a:nr, a:name)
endif
return value
endfunction
function! ctrlspace#util#GettabvarWithDefault(nr, name, default) abort
let value = ctrlspace#util#Gettabvar(a:nr, a:name)
return type(value) == 1 && empty(value) ? a:default : value
endfunction
function! ctrlspace#util#GetbufvarWithDefault(nr, name, default) abort
let value = getbufvar(a:nr, a:name)
return type(value) == 1 && empty(value) ? a:default : value
endfunction
function! ctrlspace#util#SetStatusline() abort
if has("statusline")
let config = ctrlspace#context#Configuration()
silent! exe "let &l:statusline = " . config.StatuslineFunction
endif
endfunction