Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 234 lines (213 sloc) 8.043 kb
4c5e6d4 first commit
megaannum authored
1
2 " ------------------------------------------------------------
3 " forms#dialog#filebrowser#Make:
4 " A form to browse file system and select file.
5 " The form has:
6 " Label (title)
7 " VariableLengthField (file name)
8 " SelectList
9 " Cancel and Open buttons
10 " Example:
11 " ┌───────────────────────────┐
12 " │ File Browser │
13 " ├───────────────────────────┤
14 " │┌─────────────────────────┐│
15 " ││ ││
16 " │└─────────────────────────┘│
17 " │┌─────────────────┐ │
18 " ││../ │ │
19 " ││build/ │ │
20 " ││examples/ │ │
21 " ││extra/ │ │
22 " ││ivy/ │ │
23 " ││lib/ │ │
24 " ││other/ │ │
25 " ││src/ │ │
26 " ││tmp/ │ │
27 " ││EnsimeClient.log │ │
28 " │└─────────────────┘ │
29 " │ Cancel Open│
30 " └───────────────────────────┘
31 "
32 " parameters:
26c68c7 Added a directory brower dialog.
megaannum authored
33 " attrs: optional Dictionary
34 " title: title (default File Browser)
35 " dir: initial directory (default cwdir)
4c5e6d4 first commit
megaannum authored
36 " ------------------------------------------------------------
37 " map <Leader>v7 :call forms#dialog#filebrowser#Make()<CR>
38 " forms#dialog#filebrowser#Make: {{{1
39 function! forms#dialog#filebrowser#Make(...)
40
26c68c7 Added a directory brower dialog.
megaannum authored
41 function! FBMakeChoices(dir)
42edec9 Fixed directory/filename with spaces
megaannum authored
42 let files = split(globpath(a:dir, "*"), '\n')
4c5e6d4 first commit
megaannum authored
43 let flist = []
44 let dlist = [["../", 0]]
45 let cnt = 1
46 for file in files
47 let index = strridx(file, '/')+1
48 let filename = strpart(file, index, len(file)-index)
49 if isdirectory(file)
50 call add(dlist, [filename.'/', cnt])
51 else
52 call add(flist, [filename, cnt])
53 endif
54
55 let cnt += 1
56 endfor
57 return dlist + flist
58 endfunction
59
26c68c7 Added a directory brower dialog.
megaannum authored
60 let title = 'File Browser'
61 let initial_dir = getcwd()
62 if a:0 != 0
63 if ! type(a:1) == g:self#DICTIONARY_TYPE
64 throw "Error: forms#dialog#filebrowser#Make bad argument type: " .string(a:1)
65 endif
66 let dict = a:1
67 if has_key(dict, 'title')
68 let title = dict['title']
69 endif
70 if has_key(dict, 'dir')
71 let initial_dir = dict['dir']
72 endif
73 endif
74
4c5e6d4 first commit
megaannum authored
75 let attrs = { 'text': title }
76 let titleLabel = forms#newLabel(attrs)
77
26c68c7 Added a directory brower dialog.
megaannum authored
78 function! FBVLFAction(...) dict
4c5e6d4 first commit
megaannum authored
79 call self.acceptbutton.doSelect()
80 endfunction
26c68c7 Added a directory brower dialog.
megaannum authored
81 let fnaction = forms#newAction({ 'execute': function("FBVLFAction")})
4c5e6d4 first commit
megaannum authored
82
83 let esize = 25
84 let fn = forms#newVariableLengthField({
85 \ 'tag': 'editor',
86 \ 'size': esize,
87 \ 'on_selection_action': fnaction
88 \ })
89 function! fn.purpose() dict
90 return [
91 \ "Use editor ot enter a filename.",
92 \ " Enter <CR> to accept the entered filename."
93 \ ]
94 endfunction
95 let fnbox = forms#newBox({'body': fn})
96
26c68c7 Added a directory brower dialog.
megaannum authored
97 function! FBAction(...) dict
4c5e6d4 first commit
megaannum authored
98 let slist = self.slist
99 let pos = slist.__selections[0][0]
26c68c7 Added a directory brower dialog.
megaannum authored
100 " call forms#log("FBAction.execute: pos=".pos)
4c5e6d4 first commit
megaannum authored
101 let [filename,_] = slist.__choices[pos]
102 if strpart(filename, len(filename)-1, 1) == '/'
26c68c7 Added a directory brower dialog.
megaannum authored
103 " call forms#log("FBAction.execute: directory: ".filename)
4c5e6d4 first commit
megaannum authored
104 call self.fn.setText('')
105
106 let dir = self.dir
26c68c7 Added a directory brower dialog.
megaannum authored
107 " call forms#log("FBAction.execute: old dir: ".dir)
4c5e6d4 first commit
megaannum authored
108 let dir = simplify(dir . '/' . filename)
109 let self.dir = dir
26c68c7 Added a directory brower dialog.
megaannum authored
110 " call forms#log("FBAction.execute: new dir: ".dir)
4c5e6d4 first commit
megaannum authored
111
26c68c7 Added a directory brower dialog.
megaannum authored
112 let choices = FBMakeChoices(dir)
4c5e6d4 first commit
megaannum authored
113 let attrs = {
114 \ 'tag': 'selection',
115 \ 'mode': 'single',
26c68c7 Added a directory brower dialog.
megaannum authored
116 \ 'pos': 0,
4c5e6d4 first commit
megaannum authored
117 \ 'choices': choices,
118 \ 'size': slist.__size,
119 \ 'on_selection_action': self
120 \ }
26c68c7 Added a directory brower dialog.
megaannum authored
121 " call forms#log("FBAction.execute: before reinit")
4c5e6d4 first commit
megaannum authored
122 call slist.reinit(attrs)
26c68c7 Added a directory brower dialog.
megaannum authored
123 " call forms#log("FBAction.execute: after reinit")
4c5e6d4 first commit
megaannum authored
124
125 else
26c68c7 Added a directory brower dialog.
megaannum authored
126 " call forms#log("FBAction.execute: file: ".filename)
4c5e6d4 first commit
megaannum authored
127 call self.fn.setText(filename)
128 endif
129 endfunction
26c68c7 Added a directory brower dialog.
megaannum authored
130 let fbaction = forms#newAction({ 'execute': function("FBAction")})
131 let fbaction.fn = fn
4c5e6d4 first commit
megaannum authored
132
26c68c7 Added a directory brower dialog.
megaannum authored
133 let choices = FBMakeChoices(initial_dir)
4c5e6d4 first commit
megaannum authored
134
135 " SelectList
136 let ssize = 10
137 let attrs = {
138 \ 'tag': 'selection',
139 \ 'mode': 'single',
140 \ 'choices': choices,
141 \ 'size': ssize,
26c68c7 Added a directory brower dialog.
megaannum authored
142 \ 'on_selection_action': fbaction
4c5e6d4 first commit
megaannum authored
143 \ }
144 let slist = forms#newSelectList(attrs)
145 function! slist.purpose() dict
146 return [
147 \ "Navigate file system with select list.",
148 \ " Select ../ to move up a directory. Select a directory",
149 \ " name to enter that directory, Select a file to enter",
150 \ " the file into the editor."
151 \ ]
152 endfunction
26c68c7 Added a directory brower dialog.
megaannum authored
153 let fbaction.slist = slist
154 let fbaction.dir = initial_dir
4c5e6d4 first commit
megaannum authored
155 let slistbox = forms#newBox({ 'body': slist} )
156
157 " Bottom HPoly
158 let hspaceB = forms#newHSpace({'size': 1})
159
160 let attrs = { 'text': 'Cancel'}
161 let cancellabel = forms#newLabel(attrs)
162 let cancelbutton = forms#newButton({
163 \ 'tag': 'cancel',
164 \ 'body': cancellabel,
165 \ 'action': g:forms#cancelAction})
166 function! cancelbutton.purpose() dict
167 return [
168 \ "Make no selection, cancel operation."
169 \ ]
170 endfunction
171 let attrs = { 'text': 'Accept'}
172 let acceptlabel = forms#newLabel(attrs)
173 let acceptbutton = forms#newButton({
174 \ 'tag': 'accept',
175 \ 'body': acceptlabel,
176 \ 'action': g:forms#submitAction})
177 function! acceptbutton.purpose() dict
178 return [
179 \ "Accept the filename in the editor."
180 \ ]
181 endfunction
182 let fnaction.acceptbutton = acceptbutton
183
184 let hpoly = forms#newHPoly({'children': [
185 \ cancelbutton,
186 \ hspaceB,
187 \ acceptbutton] })
188
189 let vpoly = forms#newVPoly({'children': [
190 \ fnbox,
191 \ slistbox,
192 \ hpoly],
193 \ 'alignments': [[2,'R']]
194 \ })
195 let topvpoly = forms#newVPoly({'children': [
196 \ titleLabel,
197 \ vpoly],
198 \ 'mode': 'light',
199 \ 'alignment': 'C'
200 \ })
201
202 " let b = forms#newBorder({ 'body': vpoly })
203 let bg = forms#newBackground({ 'body': topvpoly })
204 let form = forms#newForm({'body': bg })
205 function! form.purpose() dict
206 return [
207 \ "Navigate the file system and select a file.",
208 \ " Use editor to enter a filename or the select list",
209 \ " to navigate the file system and make a file selection.",
210 \ " Press Cancel to make no selection.",
211 \ " Press Open to make file selection."
212 \ ]
213 endfunction
214 let results = form.run()
215 if type(results) == g:self#DICTIONARY_TYPE
216 if results.accept
26c68c7 Added a directory brower dialog.
megaannum authored
217 let rval = fbaction.dir . '/' . results.editor
218 return rval
4c5e6d4 first commit
megaannum authored
219 endif
220 endif
221 return ''
222 endfunction
223
ee21b40 Better support for both 'light' and 'dard' backgrounds.
megaannum authored
224 " forms#dialog#filebrowser#MakeTest: {{{1
225 function! forms#dialog#filebrowser#MakeTest()
226 call forms#AppendInput({'type': 'Sleep', 'time': 5})
227 call forms#AppendInput({'type': 'Exit'})
228 call forms#dialog#filebrowser#Make()
229 endfunction
230
4c5e6d4 first commit
megaannum authored
231 " Modelines: {{{1
232 " ================
233 " vim: ts=4 fdm=marker
Something went wrong with that request. Please try again.