comparison .vim/plugin/filebrowser.vim @ 3:19d1235ce229

More stigg.
author samer
date Sun, 18 Jan 2015 18:35:47 +0000
parents
children
comparison
equal deleted inserted replaced
2:3255717f4e6b 3:19d1235ce229
1 " filebrowser.vim: utility file for vim 6.2+
2 "
3 " Copyright: Srinath Avadhanula <srinath AT fastmail DOT fm>
4 " Parts of this file are taken from explorer.vim which is a plugin file
5 " distributed with vim under the Vim charityware license.
6 " License: distributed under the Vim charityware license.
7 "
8 " Settings:
9 " FB_CallBackFunction: the function name which gets called when the user
10 " presses <cr> on a file-name in the file browser.
11 " FB_AllowRegexp: A filename has to match this regexp to be displayed.
12 " FB_RejectRegexp: If a filename matches this regexp, then its not displayed.
13 " (Both these regexps are '' by default which means no filtering is
14 " done).
15
16 " line continuation used here.
17 let s:save_cpo = &cpo
18 set cpo&vim
19
20 "======================================================================
21 " Globally visible functions (API)
22 "======================================================================
23 " FB_OpenFileBrowser: opens a new buffer and displays the file list {{{
24 " Description:
25 function! FB_OpenFileBrowser(dir)
26 if !isdirectory(a:dir)
27 return
28 endif
29 if exists('s:FB_BufferNumber')
30 if bufwinnr(s:FB_BufferNumber) != -1
31 execute bufwinnr(s:FB_BufferNumber).' wincmd w'
32 return
33 endif
34 execute 'aboveleft split #'.s:FB_BufferNumber
35 else
36 aboveleft split __Choose_File__
37 let s:FB_BufferNumber = bufnr('%')
38 endif
39 call FB_DisplayFiles(a:dir)
40 endfunction " }}}
41 " FB_DisplayFiles: displays the files in a given directory {{{
42 " Description:
43 " Call this function only when the cursor is in a temporary buffer
44 function! FB_DisplayFiles(dir)
45 if !isdirectory(a:dir)
46 return
47 endif
48 call s:FB_SetSilentSettings()
49 " make this a "scratch" buffer
50 call s:FB_SetScratchSettings()
51
52 let allowRegexp = s:FB_GetVar('FB_AllowRegexp', '')
53 let rejectRegexp = s:FB_GetVar('FB_RejectRegexp', '')
54
55 " change to the directory to make processing simpler.
56 execute "lcd ".a:dir
57 " delete everything in the buffer.
58 " IMPORTANT: we need to be in a scratch buffer
59 0,$ d_
60
61 let allFilenames = glob('*')
62 let dispFiles = ""
63 let subDirs = "../\n"
64
65 let allFilenames = allFilenames."\n"
66 let start = 0
67 while 1
68 let next = stridx(allFilenames, "\n", start)
69 let filename = strpart(allFilenames, start, next-start)
70 if filename == ""
71 break
72 endif
73
74 if isdirectory(filename)
75 let subDirs = subDirs.filename."/\n"
76 else
77 if allowRegexp != '' && filename !~ allowRegexp
78 elseif rejectRegexp != '' && filename =~ rejectRegexp
79 else
80 let dispFiles = dispFiles.filename."\n"
81 endif
82 endif
83
84 let start = next + 1
85 endwhile
86
87 0put!=dispFiles
88 0put!=subDirs
89 " delte the last empty line resulting from the put
90 $ d_
91
92 call s:FB_SetHighlighting()
93 call s:FB_DisplayHelp()
94 call s:FB_SetMaps()
95
96 " goto the first file/directory
97 0
98 call search('^"=', 'w')
99 normal! j:<bs>
100
101 set nomodified nomodifiable
102
103 call s:FB_ResetSilentSettings()
104 endfunction " }}}
105 " FB_SetVar: sets script local variables from outside this script {{{
106 " Description:
107 function! FB_SetVar(varname, value)
108 let s:{a:varname} = a:value
109 endfunction " }}}
110
111 " ==============================================================================
112 " Script local functions below this
113 " ==============================================================================
114 " FB_SetHighlighting: sets syntax highlighting for the buffer {{{
115 " Description:
116 " Origin: from explorer.vim in vim
117 function! <SID>FB_SetHighlighting()
118 " Set up syntax highlighting
119 " Something wrong with the evaluation of the conditional though...
120 if has("syntax") && exists("g:syntax_on") && !has("syntax_items")
121 syn match browseSynopsis "^\"[ -].*"
122 syn match browseDirectory "[^\"].*/ "
123 syn match browseDirectory "[^\"].*/$"
124 syn match browseCurDir "^\"= .*$"
125 syn match browseSortBy "^\" Sorted by .*$" contains=browseSuffixInfo
126 syn match browseSuffixInfo "(.*)$" contained
127 syn match browseFilter "^\" Not Showing:.*$"
128 syn match browseFiletime "«\d\+$"
129
130 "hi def link browseSynopsis PreProc
131 hi def link browseSynopsis Special
132 hi def link browseDirectory Directory
133 hi def link browseCurDir Statement
134 hi def link browseSortBy String
135 hi def link browseSuffixInfo Type
136 hi def link browseFilter String
137 hi def link browseFiletime Ignore
138 hi def link browseSuffixes Type
139 endif
140 endfunction " }}}
141 " FB_SetMaps: sets buffer local maps {{{
142 " Description:
143 function! <SID>FB_SetMaps()
144 nnoremap <buffer> <silent> q :bdelete<cr>
145 nnoremap <buffer> <silent> C :call FB_DisplayFiles(getcwd())<CR>
146 nnoremap <buffer> <silent> <esc> :bdelete<cr>
147 nnoremap <buffer> <silent> <CR> :call <SID>FB_EditEntry()<CR>
148 nnoremap <buffer> <silent> ? :call <SID>FB_ToggleHelp()<CR>
149
150 " lock the user in this window
151 nnoremap <buffer> <C-w> <nop>
152 endfunction " }}}
153 " FB_SetSilentSettings: some settings which make things silent {{{
154 " Description:
155 " Origin: from explorer.vim distributed with vim.
156 function! <SID>FB_SetSilentSettings()
157 let s:save_report=&report
158 let s:save_showcmd = &sc
159 set report=10000 noshowcmd
160 endfunction
161 " FB_ResetSilentSettings: reset settings set by FB_SetSilentSettings
162 " Description:
163 function! <SID>FB_ResetSilentSettings()
164 let &report=s:save_report
165 let &showcmd = s:save_showcmd
166 endfunction " }}}
167 " FB_SetScratchSettings: makes the present buffer a scratch buffer {{{
168 " Description:
169 function! <SID>FB_SetScratchSettings()
170 " Turn off the swapfile, set the buffer type so that it won't get
171 " written, and so that it will get deleted when it gets hidden.
172 setlocal noreadonly modifiable
173 setlocal noswapfile
174 setlocal buftype=nowrite
175 setlocal bufhidden=delete
176 " Don't wrap around long lines
177 setlocal nowrap
178 endfunction
179
180 " }}}
181 " FB_ToggleHelp: toggles verbosity of help {{{
182 " Description:
183 function! <SID>FB_ToggleHelp()
184 let s:FB_VerboseHelp = 1 - s:FB_GetVar('FB_VerboseHelp', 0)
185
186 call FB_DisplayFiles('.')
187 endfunction " }}}
188 " FB_DisplayHelp: displays a helpful header {{{
189 " Description:
190 function! <SID>FB_DisplayHelp()
191 let verboseHelp = s:FB_GetVar('FB_VerboseHelp', 0)
192 if verboseHelp
193 let txt =
194 \ "\" <cr>: on file, choose the file and quit\n"
195 \ ."\" on dir, enter directory\n"
196 \ ."\" q/<esc>: quit without choosing\n"
197 \ ."\" C: change directory to getcwd()\n"
198 \ ."\" ?: toggle help verbosity\n"
199 \ ."\"= ".getcwd()
200 else
201 let txt = "\" ?: toggle help verbosity\n"
202 \ ."\"= ".getcwd()
203 endif
204 0put!=txt
205 endfunction " }}}
206 " FB_EditEntry: handles the user pressing <enter> on a line {{{
207 " Description:
208 function! <SID>FB_EditEntry()
209 let line = getline('.')
210
211 if isdirectory(line)
212 call FB_DisplayFiles(line)
213 endif
214
215 " If the user has a call back function defined on choosing a file, handle
216 " it.
217 let cbf = s:FB_GetVar('FB_CallBackFunction', '')
218 if cbf != '' && line !~ '^" ' && filereadable(line)
219 let fname = fnamemodify(line, ':p')
220 bdelete
221
222 let arguments = s:FB_GetVar('FB_CallBackFunctionArgs', '')
223 if arguments != ''
224 let arguments = ','.arguments
225 endif
226 call Tex_Debug('arguments = '.arguments, 'fb')
227 call Tex_Debug("call ".cbf."('".fname."'".arguments.')', 'fb')
228 exec "call ".cbf."('".fname."'".arguments.')'
229 endif
230 endfunction " }}}
231 " FB_GetVar: gets the most local value of a variable {{{
232 function! <SID>FB_GetVar(name, default)
233 if exists('s:'.a:name)
234 return s:{a:name}
235 elseif exists('w:'.a:name)
236 return w:{a:name}
237 elseif exists('b:'.a:name)
238 return b:{a:name}
239 elseif exists('g:'.a:name)
240 return g:{a:name}
241 else
242 return a:default
243 endif
244 endfunction
245
246 " }}}
247
248 let &cpo = s:save_cpo
249
250 " vim:fdm=marker:ff=unix:noet:ts=4:sw=4:nowrap