22" Description: Extract selection to a new buffer
33" Author: Alexander Skachko <alexander.skachko@gmail.com>
44" Homepage: https://github.com/lucerion/vim-extract
5- " Version: 0.4. 0 (2016 -09-19 )
5+ " Version: 1.0. 0 (2017 -09-03 )
66" Licence: BSD-3-Clause
77" ==============================================================
88
9- func ! extract#extract (selection , buffer_options)
9+ func ! extract#extract (selection , buffer_options) abort
1010 if exists (' g:loaded_buffr' )
1111 call s: extract (a: selection , a: buffer_options )
1212 else
@@ -15,62 +15,66 @@ func! extract#extract(selection, buffer_options)
1515 endif
1616endfunc
1717
18- func ! s: extract (selection , buffer_options)
18+ func ! s: extract (selection , buffer_options) abort
1919 let l: selection = getline (a: selection .start_line, a: selection .end_line)
2020
2121 if a: selection .count
2222 call s: delete_lines (a: selection .start_line, a: selection .end_line)
2323 endif
24+
2425 call s: open_buffer (a: buffer_options )
26+
2527 if a: buffer_options .clear
2628 call s: clear_buffer ()
2729 end
30+
2831 if a: selection .count
2932 call s: insert_selection (l: selection )
33+
3034 if g: extract_hidden
3135 call s: close_buffer ()
3236 endif
3337 endif
3438endfunc
3539
36- func ! s: open_buffer (buffer_options)
40+ func ! s: open_buffer (buffer_options) abort
3741 let l: buffer_options = s: buffer_options (a: buffer_options )
3842 call buffr#open_or_create_buffer (l: buffer_options )
3943 call s: set_buffer_defaults (l: buffer_options )
4044endfunc
4145
42- func ! s: close_buffer ()
46+ func ! s: close_buffer () abort
4347 silent exec ' close'
4448endfunc
4549
46- func ! s: buffer_options (buffer_options)
50+ func ! s: buffer_options (buffer_options) abort
4751 let l: default_buffer_options = {
4852 \ ' name' : substitute (g: extract_buffer_name , ' {filename}' , expand (' %:t' ), ' g' )
4953 \ }
5054
5155 return extend (l: default_buffer_options , a: buffer_options )
5256endfunc
5357
54- func ! s: clear_buffer ()
55- silent exec ' normal! ggVGd '
58+ func ! s: clear_buffer () abort
59+ call s: delete_lines ( 1 , ' $ ' )
5660endfunc
5761
58- func ! s: insert_selection (selection )
62+ func ! s: insert_selection (selection ) abort
5963 let l: last_line = line (' $' )
6064 if l: last_line == 1
6165 call append (0 , a: selection )
62- silent exec ' normal! Gdd '
66+ call s: delete_lines ( ' $ ' , ' $ ' )
6367 else
6468 call append (l: last_line , a: selection )
6569 silent exec ' normal! G'
6670 endif
6771endfunc
6872
69- func ! s: delete_lines (start_line, end_line)
70- exec a: start_line . ' ,' . a: end_line . ' delete'
73+ func ! s: delete_lines (start_line, end_line) abort
74+ silent exec a: start_line . ' ,' . a: end_line . ' delete _ '
7175endfunc
7276
73- func ! s: set_buffer_defaults (buffer_options)
77+ func ! s: set_buffer_defaults (buffer_options) abort
7478 setlocal buftype = nofile
7579 setlocal bufhidden = hide
7680 setlocal nobuflisted
@@ -84,27 +88,27 @@ func! s:set_buffer_defaults(buffer_options)
8488 augroup END
8589endfunc
8690
87- func ! s: save_state ()
91+ func ! s: save_state () abort
8892 if expand (' <afile>' ) == s: buffer_options .name && ! s: is_buffer_empty ()
8993 exec ' write /tmp/' . s: buffer_options .name
9094 endif
9195endfunc
9296
93- func ! s: load_state ()
97+ func ! s: load_state () abort
9498 let l: file = ' /tmp/' . s: buffer_options .name
9599 if expand (' <afile>' ) == s: buffer_options .name && filereadable (l: file )
96100 if ! s: buffer_options .clear
97101 call append (0 , readfile (l: file ))
98- silent exec ' normal! Gdd '
102+ call s: delete_lines ( ' $ ' , ' $ ' )
99103 endif
100- exec ' !rm ' . l: file
104+ call delete ( l: file)
101105 endif
102106endfunc
103107
104- func ! s: is_buffer_empty ()
108+ func ! s: is_buffer_empty () abort
105109 return line (' $' ) == 1 && getline (1 ) == ' ' ? 1 : 0
106110endfunc
107111
108- func ! s: show_error (message)
112+ func ! s: show_error (message) abort
109113 echohl ErrorMsg | echomsg a: message | echohl None
110114endfunc
0 commit comments