@@ -24,6 +24,20 @@ fun! s:get_id()
24
24
return s: id
25
25
endf
26
26
27
+ fun ! s: indent_line (line , indent )
28
+ return printf (' %*s%s' , a: indent , a: indent ? ' ' : ' ' , a: line )
29
+ endf
30
+
31
+ fun ! s: dedent_line (line , indent )
32
+ return substitute (a: line , ' ^\s\{' .a: indent .' \}' , ' ' , ' ' )
33
+ endf
34
+
35
+ fun ! s: indent_lines (lines , indent )
36
+ return a: indent < 0
37
+ \ ? map (lines , ' s:dedent_line(v:val, indent)' )
38
+ \ : map (lines , ' s:indent_line(v:val, indent)' )
39
+ endf
40
+
27
41
" }}}
28
42
" Buffer prototype {{{
29
43
@@ -104,9 +118,23 @@ fun! s:buffer_write(...) dict abort
104
118
return setline (lnum, text)
105
119
endf
106
120
121
+ " Returns the buffer content with or without indentation.
122
+ "
123
+ " The arguments are:
124
+ " - the indentation level (dedent buffer when value is negative and indent otherwise)
125
+ " - the line number to start (read from the beginning if not set)
126
+ " - the line number to end (process until the end if not set)
127
+ fun ! s: buffer_indent (indent , ... ) dict abort
128
+ let start_at = a: 0 ? a: 1 : 1
129
+ let end_at = (a: 0 > 1 ) ? a: 2 : ' $'
130
+ return a: indent < 0
131
+ \ ? map (self .read (start_at, end_at), ' s:dedent_line(v:val, a:indent)' )
132
+ \ : map (self .read (start_at, end_at), ' s:indent_line(v:val, a:indent)' )
133
+ endf
134
+
107
135
call s: add_methods (' buffer' , [
108
136
\ ' getvar' , ' setvar' , ' name' , ' delete' , ' read' , ' write' ,
109
- \ ' exists' , ' spec' , ' path' , ' fancy_id'
137
+ \ ' exists' , ' spec' , ' path' , ' fancy_id' , ' indent '
110
138
\ ])
111
139
112
140
" }}}
@@ -140,7 +168,8 @@ fun! s:fancy() abort
140
168
\ ' options' : search_options,
141
169
\ ' start_at' : start_at,
142
170
\ ' end_at' : end_at,
143
- \ ' buffer' : s: buffer ()
171
+ \ ' buffer' : s: buffer (),
172
+ \ ' indent_level' : indent (start_at)
144
173
\ }
145
174
call extend (fancy, s: fancy_prototype , ' keep' )
146
175
@@ -160,7 +189,10 @@ fun! s:fancy_filetype() dict abort
160
189
endf
161
190
162
191
fun ! s: fancy_text () dict abort
163
- return self .buffer .read (self .start_at + 1 , self .end_at - 1 )
192
+ return self .buffer .indent (
193
+ \ - self .indent_level,
194
+ \ self .start_at + 1 ,
195
+ \ self .end_at - 1 )
164
196
endf
165
197
166
198
fun ! s: fancy_destroy () dict abort
@@ -219,14 +251,15 @@ fun! s:edit()
219
251
endf
220
252
221
253
fun ! s: destroy (... )
222
- let bufnr = a: 0 ? str2nr ( a: 1 [0 ]) : ' %'
254
+ let bufnr = a: 0 ? a: 1 [0 ] : ' %'
223
255
let buffer = s: buffer (bufnr )
224
256
let fancy = s: lookup_fancy (buffer .fancy_id ())
225
257
call fancy.destroy ()
226
258
endf
227
259
228
- fun ! s: sync ()
229
- let buffer = s: buffer ()
260
+ fun ! s: sync (... )
261
+ let bufnr = a: 0 ? a: 1 [0 ] : ' %'
262
+ let buffer = s: buffer (bufnr )
230
263
let fancy = s: lookup_fancy (buffer .fancy_id ())
231
264
232
265
" Go to original buffer.
@@ -239,7 +272,7 @@ fun! s:sync()
239
272
if (fancy.end_at - fancy.start_at > 1 )
240
273
exe printf (' %s,%s delete _' , fancy.start_at + 1 , fancy.end_at - 1 )
241
274
endif
242
- call append (fancy.start_at, buffer .read ( ))
275
+ call append (fancy.start_at, buffer .indent (fancy.indent_level ))
243
276
244
277
" Restore the original cursor position.
245
278
call setpos (' .' , fancy.buffer .pos)
@@ -248,8 +281,9 @@ fun! s:sync()
248
281
let [fancy.start_at, fancy.end_at] = s: get_region_bounds (fancy.options )
249
282
endf
250
283
251
- fun ! s: write ()
252
- sil exe ' write! ' .s: buffer ().path ()
284
+ fun ! s: write (... )
285
+ let bufnr = a: 0 ? a: 1 [0 ] : ' %'
286
+ sil exe ' write! ' .s: buffer (bufnr ).path ()
253
287
setl nomodified
254
288
endf
255
289
@@ -265,11 +299,11 @@ fun! fancy#edit() abort
265
299
endf
266
300
267
301
fun ! fancy#sync (... ) abort
268
- return s: sync ()
302
+ return s: sync (a: 000 )
269
303
endf
270
304
271
305
fun ! fancy#write (... ) abort
272
- return s: write ()
306
+ return s: write (a: 000 )
273
307
endf
274
308
275
309
fun ! fancy#destroy (... ) abort
0 commit comments