Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 292 lines (256 sloc) 9.677 kb
4c5e6d4 first commit
megaannum authored
1
2 " ------------------------------------------------------------
3 " forms#dialog#textsearch#Make:
4 " A form to search or search-and-replace text.
5 " The form has:
6 " Label (title)
7 " VariableLengthField (find and replace)
8 " ToggleButtons (match)
9 " RadioButtons (Up/Down)
10 " Find, Replace, and Close buttons
11 " Example:
12 " ┌──────────────────────────────────────────────┐
13 " │ Text Search │
14 " ├──────────────────────────────────────────────┤
15 " │ Find what:thix Find next │
16 " │Replace with: Replace │
17 " │ Replace All│
18 " │[ ]Match whole word only Direction Close │
19 " │[X]Match case ( )Up │
20 " │ (*)Down │
21 " └──────────────────────────────────────────────┘
22
23 " parameters:
24 " title : title of dialog
25 " findonly : if true (1) then search only otherwise
26 " find and replace.
27 " initial_text: optional initial search text
28 " ------------------------------------------------------------
29 " map <Leader>v8 :call forms#dialog#textsearch#Make('Text Search', 0, 'thix')<CR>
30 " forms#dialog#textsearch#Make: {{{1
31 function! forms#dialog#textsearch#Make(title, findonly, ...)
32 function! V8Action(...) dict
33 call self.findbutton.doSelect()
34 endfunction
35 let we_action = forms#newAction({ 'execute': function("V8Action")})
36
37 let children = []
38 let label = forms#newLabel({ 'text': 'Find what:' })
39 call add(children, label)
40
41 let we = forms#newVariableLengthField({
42 \ 'tag': 'what_editor',
43 \ 'size': 10,
44 \ 'on_selection_action': we_action
45 \ })
46 function! we.purpose() dict
47 return [
48 \ "Enter the string that is to be searched for."
49 \ ]
50 endfunction
51 if a:0 > 0 && a:1 != ''
52 call we.setText(a:1)
53 endif
54
55 call add(children, we)
56 let thpoly = forms#newHPoly({'children': children })
57
58 let children = []
59 call add(children, thpoly)
60
61
62 if ! a:findonly
63 " optional
64 let rwchildren = []
65 let label = forms#newLabel({ 'text': 'Replace with:' })
66 call add(rwchildren, label)
67
68 let re = forms#newVariableLengthField({
69 \ 'tag': 'replace_editor',
70 \ 'size': 10,
71 \ 'on_selection_action': we_action
72 \ })
73 function! re.purpose() dict
74 return [
75 \ "Enter the string that is to replace the search string."
76 \ ]
77 endfunction
78
79 call add(rwchildren, re)
80
81 let mhpoly = forms#newHPoly({'children': rwchildren })
82 call add(children, mhpoly)
83 endif
84
85 let mvpoly = forms#newVPoly({'children': children,
86 \ 'alignment': 'R'})
87
88 " match whole word only
89 let cbmw = forms#newCheckBox({'tag': 'match_word'})
90 function! cbmw.purpose() dict
91 return [
92 \ "Should the search command match whole word only."
93 \ ]
94 endfunction
95 let lmw = forms#newLabel({'text': "Match whole word only"})
96 let hpolymw = forms#newHPoly({'children': [cbmw, lmw] })
97
98 " match case
99 let cbmc = forms#newCheckBox({'tag': 'match_case', 'selected': 1 })
100 function! cbmc.purpose() dict
101 return [
102 \ "Should the search command match on case or be case independent."
103 \ ]
104 endfunction
105 let lmc = forms#newLabel({'text': "Match case"})
106 let hpolymc = forms#newHPoly({'children': [cbmc, lmc] })
107
108 let tbsvpoly = forms#newVPoly({'children': [hpolymw, hpolymc] })
109
110 " Direction
111 let ld = forms#newLabel({'text': "Direction"})
5cf376c type to kind
megaannum authored
112 let group = forms#newButtonGroup({ 'member_kind': 'forms#RadioButton'})
4c5e6d4 first commit
megaannum authored
113
114 let rbdu = forms#newRadioButton({'tag': 'dir_up', 'group': group})
115 function! rbdu.purpose() dict
116 return [
117 \ "Search from current postion Up the text."
118 \ ]
119 endfunction
120 let ldu = forms#newLabel({'text': "Up"})
121 let polydu = forms#newHPoly({'children': [rbdu, ldu] })
122
123 let rbdd = forms#newRadioButton({'tag': 'dir_down',
124 \ 'selected': 1,
125 \ 'group': group})
126 function! rbdd.purpose() dict
127 return [
128 \ "Search from current postion Down the text."
129 \ ]
130 endfunction
131 let ldd = forms#newLabel({'text': "Down"})
132 let polydd = forms#newHPoly({'children': [rbdd, ldd] })
133
134 let dvpoly = forms#newVPoly({'children': [ld, polydu, polydd] })
135
136 let hspace = forms#newHSpace({'size': 1})
137
138 " Match checkboxes and Direction
139 let bhpoly = forms#newHPoly({'children': [tbsvpoly, hspace, dvpoly] })
140
141 " left side
142 let vspace = forms#newVSpace({'size': 1})
143 let lvpoly = forms#newVPoly({'children': [mvpoly, vspace, bhpoly] })
144
145 " right side
146 let children = []
147 let attrs = { 'text': 'Find next'}
148 let findlabel = forms#newLabel(attrs)
149 let findbutton = forms#newButton({
150 \ 'tag': 'find',
151 \ 'body': findlabel,
152 \ 'action': g:forms#submitAction})
153 function! findbutton.purpose() dict
154 return [
155 \ "Find the next search match."
156 \ ]
157 endfunction
158 call add(children, findbutton)
159 let we_action.findbutton = findbutton
160
161 if ! a:findonly
162 " optional
163 let attrs = { 'text': 'Replace'}
164 let replacelabel = forms#newLabel(attrs)
165 let replacebutton = forms#newButton({
166 \ 'tag': 'replace',
167 \ 'body': replacelabel,
168 \ 'action': g:forms#submitAction})
169 function! replacebutton.purpose() dict
170 return [
171 \ "Find and replace the next search match."
172 \ ]
173 endfunction
174 call add(children, replacebutton)
175
176 " optional
177 let attrs = { 'text': 'Replace All'}
178 let replacealllabel = forms#newLabel(attrs)
179 let replaceallbutton = forms#newButton({
180 \ 'tag': 'replaceall',
181 \ 'body': replacealllabel,
182 \ 'action': g:forms#submitAction})
183 function! replaceallbutton.purpose() dict
184 return [
185 \ "Find and replace all of the search matches."
186 \ ]
187 endfunction
188 call add(children, replaceallbutton)
189 endif
190
191 let attrs = { 'text': 'Close'}
192 let closelabel = forms#newLabel(attrs)
193 let closebutton = forms#newButton({
194 \ 'tag': 'close',
195 \ 'body': closelabel,
196 \ 'action': g:forms#cancelAction})
197 function! closebutton.purpose() dict
198 return [
199 \ "Close the Form and take no action."
200 \ ]
201 endfunction
202 call add(children, closebutton)
203
204 let rvpoly = forms#newVPoly({'children': children })
205
206 let hspace = forms#newHSpace({'size': 1})
207 let hpoly = forms#newHPoly({'children': [
208 \ lvpoly,
209 \ hspace,
210 \ rvpoly] })
211
212
213 " let box = forms#newBox({ 'body': hpoly })
214 " let bg = forms#newBackground({ 'body': box })
215 let attrs = { 'text': a:title }
216 let titlelabel = forms#newLabel(attrs)
217 let topvpoly = forms#newVPoly({'children': [
218 \ titlelabel,
219 \ hpoly],
220 \ 'alignment': 'C',
221 \ 'mode': 'light'
222 \ })
223 let bg = forms#newBackground({ 'body': topvpoly })
224 let form = forms#newForm({'body': bg })
225 function! form.purpose() dict
226 return [
227 \ "Create a search term and search command."
228 \ ]
229 endfunction
230 let results = form.run()
231 if type(results) == g:self#DICTIONARY_TYPE
232 let sterm = results.what_editor
233
234 if results.match_word
235 let sterm = '\<'.sterm.'\>'
236 endif
237 if results.match_case
238 let sterm = sterm.'\C'
239 else
240 let sterm = sterm.'\c'
241 endif
242
243 if a:findonly
244 " Find
245 if results.find
246 if results.dir_down
247 return '/'.sterm
248 else
249 return '?'.sterm
250 endif
251 else
252 return ''
253 endif
254 else
255 " Find and Replace
256 if results.find
257 if results.dir_down
258 return '/'.sterm
259 else
260 return '?'.sterm
261 endif
262 elseif results.replace
263 return 's/'.sterm.'/'.results.replace_editor.'/'
264 elseif results.replaceall
265 return 'g/'.sterm.'/s//'.results.replace_editor.'/g'
266 else
267 return ''
268 endif
269 endif
270 endif
271 return ''
272 endfunction
273
ee21b40 Better support for both 'light' and 'dard' backgrounds.
megaannum authored
274 " forms#dialog#textsearch#MakeTest: {{{1
275 function! forms#dialog#textsearch#MakeTest()
276 call forms#AppendInput({'type': 'Sleep', 'time': 5})
277 call forms#AppendInput({'type': 'Exit'})
278 let title = "Find Text"
279 let findonly = 1
280 call forms#dialog#textsearch#Make(title, findonly)
281
282 call forms#AppendInput({'type': 'Sleep', 'time': 5})
283 call forms#AppendInput({'type': 'Exit'})
284 let title = "Find and Replace Text"
285 let findonly = 0
286 call forms#dialog#textsearch#Make(title, findonly)
287 endfunction
288
4c5e6d4 first commit
megaannum authored
289 " Modelines: {{{1
290 " ================
291 " vim: ts=4 fdm=marker
Something went wrong with that request. Please try again.