Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 217 lines (197 sloc) 7.563 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:
33 " title: optional title (default File Browser)
34 " ------------------------------------------------------------
35 " map <Leader>v7 :call forms#dialog#filebrowser#Make()<CR>
36 " forms#dialog#filebrowser#Make: {{{1
37 function! forms#dialog#filebrowser#Make(...)
38
39 function! V7MakeChoices(dir)
40 let files = split(globpath(a:dir, "*"))
41 let flist = []
42 let dlist = [["../", 0]]
43 let cnt = 1
44 for file in files
45 let index = strridx(file, '/')+1
46 let filename = strpart(file, index, len(file)-index)
47 if isdirectory(file)
48 call add(dlist, [filename.'/', cnt])
49 else
50 call add(flist, [filename, cnt])
51 endif
52
53 let cnt += 1
54 endfor
55 return dlist + flist
56 endfunction
57
58 let title = (a:0 == 0 ) ? 'File Browser' : a:1
59 let attrs = { 'text': title }
60 let titleLabel = forms#newLabel(attrs)
61
62 function! V7VLFAction(...) dict
63 call self.acceptbutton.doSelect()
64 endfunction
65 let fnaction = forms#newAction({ 'execute': function("V7VLFAction")})
66
67 let esize = 25
68 let fn = forms#newVariableLengthField({
69 \ 'tag': 'editor',
70 \ 'size': esize,
71 \ 'on_selection_action': fnaction
72 \ })
73 function! fn.purpose() dict
74 return [
75 \ "Use editor ot enter a filename.",
76 \ " Enter <CR> to accept the entered filename."
77 \ ]
78 endfunction
79 let fnbox = forms#newBox({'body': fn})
80
81 function! V7Action(...) dict
82 let slist = self.slist
83 let pos = slist.__selections[0][0]
84 " call forms#log("V7Action.execute: pos=".pos)
85 let [filename,_] = slist.__choices[pos]
86 if strpart(filename, len(filename)-1, 1) == '/'
87 " call forms#log("V7Action.execute: directory: ".filename)
88 call self.fn.setText('')
89
90 let dir = self.dir
91 " call forms#log("V7Action.execute: old dir: ".dir)
92 let dir = simplify(dir . '/' . filename)
93 let self.dir = dir
94 " call forms#log("V7Action.execute: new dir: ".dir)
95
96 let choices = V7MakeChoices(dir)
97 let attrs = {
98 \ 'tag': 'selection',
99 \ 'mode': 'single',
100 \ 'choices': choices,
101 \ 'size': slist.__size,
102 \ 'on_selection_action': self
103 \ }
104 " call forms#log("V7Action.execute: before reinit")
105 call slist.reinit(attrs)
106 " call forms#log("V7Action.execute: after reinit")
107
108 else
109 " call forms#log("V7Action.execute: file: ".filename)
110 call self.fn.setText(filename)
111 endif
112 endfunction
113 let action = forms#newAction({ 'execute': function("V7Action")})
114 let action.fn = fn
115
116 let dir = getcwd()
117 let choices = V7MakeChoices(dir)
118
119 " SelectList
120 let ssize = 10
121 let attrs = {
122 \ 'tag': 'selection',
123 \ 'mode': 'single',
124 \ 'choices': choices,
125 \ 'size': ssize,
126 \ 'on_selection_action': action
127 \ }
128 let slist = forms#newSelectList(attrs)
129 function! slist.purpose() dict
130 return [
131 \ "Navigate file system with select list.",
132 \ " Select ../ to move up a directory. Select a directory",
133 \ " name to enter that directory, Select a file to enter",
134 \ " the file into the editor."
135 \ ]
136 endfunction
137 let action.slist = slist
138 let action.dir = dir
139 let slistbox = forms#newBox({ 'body': slist} )
140
141 " Bottom HPoly
142 let hspaceB = forms#newHSpace({'size': 1})
143
144 let attrs = { 'text': 'Cancel'}
145 let cancellabel = forms#newLabel(attrs)
146 let cancelbutton = forms#newButton({
147 \ 'tag': 'cancel',
148 \ 'body': cancellabel,
149 \ 'action': g:forms#cancelAction})
150 function! cancelbutton.purpose() dict
151 return [
152 \ "Make no selection, cancel operation."
153 \ ]
154 endfunction
155 let attrs = { 'text': 'Accept'}
156 let acceptlabel = forms#newLabel(attrs)
157 let acceptbutton = forms#newButton({
158 \ 'tag': 'accept',
159 \ 'body': acceptlabel,
160 \ 'action': g:forms#submitAction})
161 function! acceptbutton.purpose() dict
162 return [
163 \ "Accept the filename in the editor."
164 \ ]
165 endfunction
166 let fnaction.acceptbutton = acceptbutton
167
168 let hpoly = forms#newHPoly({'children': [
169 \ cancelbutton,
170 \ hspaceB,
171 \ acceptbutton] })
172
173 let vpoly = forms#newVPoly({'children': [
174 \ fnbox,
175 \ slistbox,
176 \ hpoly],
177 \ 'alignments': [[2,'R']]
178 \ })
179 let topvpoly = forms#newVPoly({'children': [
180 \ titleLabel,
181 \ vpoly],
182 \ 'mode': 'light',
183 \ 'alignment': 'C'
184 \ })
185
186 " let b = forms#newBorder({ 'body': vpoly })
187 let bg = forms#newBackground({ 'body': topvpoly })
188 let form = forms#newForm({'body': bg })
189 function! form.purpose() dict
190 return [
191 \ "Navigate the file system and select a file.",
192 \ " Use editor to enter a filename or the select list",
193 \ " to navigate the file system and make a file selection.",
194 \ " Press Cancel to make no selection.",
195 \ " Press Open to make file selection."
196 \ ]
197 endfunction
198 let results = form.run()
199 if type(results) == g:self#DICTIONARY_TYPE
200 if results.accept
201 return action.dir . '/' . results.editor
202 endif
203 endif
204 return ''
205 endfunction
206
ee21b40 Better support for both 'light' and 'dard' backgrounds.
megaannum authored
207 " forms#dialog#filebrowser#MakeTest: {{{1
208 function! forms#dialog#filebrowser#MakeTest()
209 call forms#AppendInput({'type': 'Sleep', 'time': 5})
210 call forms#AppendInput({'type': 'Exit'})
211 call forms#dialog#filebrowser#Make()
212 endfunction
213
4c5e6d4 first commit
megaannum authored
214 " Modelines: {{{1
215 " ================
216 " vim: ts=4 fdm=marker
Something went wrong with that request. Please try again.