-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfancy.vim
More file actions
384 lines (314 loc) · 9.15 KB
/
fancy.vim
File metadata and controls
384 lines (314 loc) · 9.15 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
" Internal variables and functions {{{
let s:id = 0
let s:fancy_objects = []
fun! s:error(message)
echohl ErrorMsg | echomsg a:message | echohl NONE
let v:errmsg = a:message
endf
fun! s:function(name) abort
return function(a:name)
endf
fun! s:add_methods(namespace, method_names) abort
for name in a:method_names
let s:{a:namespace}_prototype[name] = s:function('s:'.a:namespace.'_'.name)
endfor
endf
fun! s:get_id()
let s:id += 1
return s:id
endf
" }}}
" Buffer prototype {{{
let s:buffer_prototype = {}
" Returns a new buffer instance.
"
" Arguments:
" - buffer number (as per bufnr spec), use current buffer when not set;
" - number of fancy object if any.
fun! s:buffer(...) abort
let buffer = {
\ '#': bufnr(a:0 ? a:1 : '%'),
\ 'id': (a:0 > 1 && a:2) ? a:2 : 0,
\ 'pos': getpos('.')
\ }
call extend(buffer, s:buffer_prototype, 'keep')
return buffer
endf
fun! s:buffer_getvar(var) dict abort
return getbufvar(self['#'], a:var)
endf
fun! s:buffer_setvar(var, value) dict abort
return setbufvar(self['#'], a:var, a:value)
endf
fun! s:buffer_spec() dict abort
let full = bufname(self['#'])
if full =~# '^fancy://.*//\d\+'
let path = substitute(full, '^fancy://\(.*\)//\d\+', '\1', '')
elseif full != ''
let path = fnamemodify(full, ':p')
else
let path = ''
endif
let id = (full =~# '^fancy://.*//\d\+')
\ ? substitute(full, '^fancy://.*//\(\d\+\)', '\1', '')
\ : self.id
return 'fancy://'.path.'//'.id
endf
fun! s:buffer_name() dict abort
return self.path()
endf
fun! s:buffer_path() dict abort
return substitute(self.spec(), '^fancy://\(.*\)//\d\+', '\1', '')
endf
fun! s:buffer_fancy_id() dict abort
return substitute(self.spec(), '^fancy://.*//\(\d\+\)', '\1', '')
endf
fun! s:buffer_exists() dict abort
return bufexists(self.spec()) && (bufwinnr(self.spec()) != -1)
endf
fun! s:buffer_delete() dict abort
call delete(self.name())
if fnamemodify(bufname('$'), ':p') ==# self.name()
sil exe 'bwipeout '.bufnr('$')
endif
endf
" Returns the content of the buffer.
"
" Arguments:
" - the line number to start (read from the beginning if not set);
" - the line number to end (read until the end if not set).
fun! s:buffer_read(...) dict abort
return getbufline(self.name(),
\ a:0 ? a:1 : 1,
\ (a:0 == 2) ? a:2 : '$')
endf
" Write text to the buffer.
"
" Arguments:
" - the optional line number to start with;
" - text to write (as per setline spec).
fun! s:buffer_write(...) dict abort
if empty(a:0)
return
elseif a:0 == 1
let [lnum, text] = [1, a:1]
else
let [lnum, text] = [a:1, a:2]
endif
return setline(lnum, text)
endf
" Returns the buffer content with or without indentation.
"
" Arguments:
" - the indentation level (dedent buffer when value is negative and indent otherwise)
" - the line number to start (read from the beginning if not set)
" - the line number to end (process until the end if not set)
fun! s:buffer_indent(indent, ...) dict abort
let start_at = a:0 ? a:1 : 1
let end_at = (a:0 > 1) ? a:2 : '$'
return a:indent < 0
\ ? map(self.read(start_at, end_at), 'fancy#util#dedent_line(v:val, a:indent)')
\ : map(self.read(start_at, end_at), 'fancy#util#indent_line(v:val, a:indent)')
endf
call s:add_methods('buffer', [
\ 'getvar', 'setvar', 'name', 'delete', 'read', 'write',
\ 'exists', 'spec', 'path', 'fancy_id', 'indent'
\ ])
" }}}
" Matcher prototype {{{
let s:matcher_prototype = {}
fun! s:matcher() abort
let matcher = {
\ 'start_at': 0,
\ 'end_at': 0,
\ 'indent_level': 0,
\ }
call extend(matcher, s:matcher_prototype, 'keep')
return matcher
endf
" Returns the number of the first line of the region.
fun! s:matcher_start_line(...) dict abort
endf
" Returns the number of the last line of the region.
fun! s:matcher_end_line(...) dict abort
endf
" Returns the filetype of the region.
"
" Arguments:
" - the fancy object (can be used to read and extract data from buffer).
fun! s:matcher_filetype(fancy, ...) dict abort
endf
" Find fenced region and save it position if any. Return false if
" no region has been found and true otherwise.
fun! s:matcher_find_region() dict abort
let self.start_at = self.start_line()
let self.indent_level = indent(self.start_at)
let self.end_at = self.end_line()
return (self.start_at != 0 && self.end_at != 0) ? 1 : 0
endf
fun! s:matcher_search_forward(pattern) dict abort
return search(a:pattern, 'cnW')
endf
fun! s:matcher_search_backward(pattern) dict abort
return search(a:pattern, 'bcnW')
endf
call s:add_methods('matcher', [
\ 'filetype', 'start_line', 'end_line', 'find_region',
\ 'search_forward', 'search_backward'
\ ])
" }}}
" Loader prototype {{{
let s:loader_prototype = {}
fun! s:loader() abort
let loader = {
\ 'filetypes': {}
\ }
call extend(loader, s:loader_prototype, 'keep')
return loader
endf
fun! s:loader_load_from_cache(ft) dict abort
return self.filetypes[a:ft]
endf
fun! s:loader_save_to_cache(ft, list) dict abort
let self.filetypes[a:ft] = a:list
endf
fun! s:loader_is_cached(ft) dict abort
return has_key(self.filetypes, a:ft)
endf
fun! s:loader_is_defined(ft) dict abort
let path = 'autoload/fancy/ft/'.a:ft.'.vim'
return !empty(globpath(&rtp, path))
endf
fun! s:loader_load(ft) dict abort
if self.is_cached(a:ft)
return self.load_from_cache(a:ft)
elseif self.is_defined(a:ft)
let matchers = fancy#ft#{a:ft}#matchers()
call self.save_to_cache(a:ft, matchers)
return matchers
endif
endf
call s:add_methods('loader', [
\ 'load', 'load_from_cache', 'save_to_cache', 'is_cached', 'is_defined'
\ ])
" }}}
" Fancy prototype {{{
let s:fancy_prototype = {}
fun! s:fancy() abort
let matchers = s:loader().load(&filetype)
if empty(matchers)
call s:error(printf('%s: no available matcher', &filetype))
return
endif
let found = 0
for matcher in matchers
if matcher.find_region()
let found = 1
break
endif
endfor
if !found
call s:error('No fenced block found! Aborting!')
return
endif
let fancy = {
\ 'id': s:get_id(),
\ 'matcher': matcher,
\ 'buffer': s:buffer(),
\ }
call extend(fancy, s:fancy_prototype, 'keep')
call add(s:fancy_objects, fancy)
return fancy
endf
fun! s:fancy_filetype() dict abort
let filetype = self.matcher.filetype(self)
return empty(filetype)
\ ? self.buffer.getvar('&filetype')
\ : filetype
endf
fun! s:fancy_text() dict abort
return self.buffer.indent(
\ -self.matcher.indent_level,
\ self.matcher.start_at + 1,
\ self.matcher.end_at - 1)
endf
fun! s:fancy_destroy() dict abort
call remove(s:fancy_objects, index(s:fancy_objects, self))
endf
call s:add_methods('fancy', ['filetype', 'text', 'destroy'])
" Returns fancy object bound to buffer.
fun! s:lookup_fancy(buffer)
let fancy_id = a:buffer.fancy_id()
let found = filter(copy(s:fancy_objects), 'v:val["id"] == fancy_id')
if empty(found)
call s:error('Original buffer does no longer exist! Aborting!')
return
endif
return found[0]
endf
" }}}
" Fancy public interface {{{
fun! fancy#matcher() abort
return s:matcher()
endf
fun! fancy#fancy() abort
return s:fancy()
endf
fun! fancy#init() abort
" Create a new fancy instance for the current buffer. Exit silently when
" fenced region does not found.
let fancy = fancy#fancy()
if (type(fancy) != type({}))
return
endif
" Create a new temporary file and open it in split.
let name = tempname()
exe 'split '.name
" Bind buffer to the fancy object and
" - copy fenced region into it;
" - detect and set filetype;
" - ensure the buffer is wiped out when it's no longer displayed
" in a window;
" - mark buffer as nomodified, to prevent warning when trying to close it;
" - disable swap file for the buffer;
" - show buffer in the buffer list;
" - rename buffer according with its spec.
let buffer = s:buffer(name, fancy.id)
call buffer.write(fancy.text())
call buffer.setvar('&ft', fancy.filetype())
call buffer.setvar('&bufhidden', 'wipe')
call buffer.setvar('&modified', 0)
call buffer.setvar('&swapfile', 0)
call buffer.setvar('&buflisted', 1)
sil exe 'file '.buffer.spec()
endf
fun! fancy#sync(bufnr) abort
" Get buffer and related fancy object.
let buffer = s:buffer(a:bufnr)
let fancy = s:lookup_fancy(buffer)
" Go to original buffer.
let winnr = bufwinnr(fancy.buffer.name())
if (winnr != winnr())
exe 'noa' winnr 'wincmd w'
endif
" Sync any changes.
if (fancy.matcher.end_at - fancy.matcher.start_at > 1)
exe printf('%s,%s delete _', fancy.matcher.start_at + 1, fancy.matcher.end_at - 1)
endif
call append(fancy.matcher.start_at, buffer.indent(fancy.matcher.indent_level))
" Restore the original cursor position.
call setpos('.', fancy.buffer.pos)
" Update start/end block position.
call fancy.matcher.find_region()
endf
fun! fancy#write(bufnr) abort
let buffer = s:buffer(a:bufnr)
sil exe 'write! '.buffer.path()
call buffer.setvar('&modified', 0)
endf
fun! fancy#destroy(bufnr) abort
let buffer = s:buffer(a:bufnr)
let fancy = s:lookup_fancy(buffer)
call fancy.destroy()
endf
" }}}