11" ============================================================================
22" File: executioner.vim
33" Maintainer: https://github.com/EvanQuan/vim-executioner/
4- " Version: 1.3.1
4+ " Version: 1.4.0
55"
66" A Vim plugin to easily execute files in the terminal or a separate buffer.
77" You can learn more about it with:
@@ -14,19 +14,7 @@ if exists("g:executioner#loaded")
1414 finish
1515endif
1616
17- " Name and extension
18- if ! exists (" g:executioner#full_name" ) || len (g: executioner #full_name) != 1
19- let g: executioner #full_name = ' %'
20- endif
21- " Just name
22- if ! exists (" g:executioner#base_name" ) || len (g: executioner #base_name) != 1
23- let g: executioner #base_name = ' @'
24- endif
25-
26- if ! exists (" g:executioner#load_defaults" )
27- let g: executioner #load_defaults = 1
28- endif
29-
17+ " Script Variables {{{
3018" Parsed input
3119let s: FILE = 0
3220let s: NAME = 1
@@ -43,8 +31,25 @@ let s:HORIZONTAL = 3
4331let s: INVALID_COMMAND = -1
4432
4533let s: DIRECTORY_SEPARATOR = ' [/\\]'
34+ let s: VIM_COMMAND_PATTERN = ' ^:.\+'
4635
4736let s: has_teriminal = has (" terminal" )
37+ " }}}
38+ " Global variables {{{
39+ " Name and extension
40+ if ! exists (" g:executioner#full_name" ) || len (g: executioner #full_name) != 1
41+ let g: executioner #full_name = ' %'
42+ endif
43+ " Just name
44+ if ! exists (" g:executioner#base_name" ) || len (g: executioner #base_name) != 1
45+ let g: executioner #base_name = ' @'
46+ endif
47+
48+ if ! exists (" g:executioner#load_defaults" )
49+ let g: executioner #load_defaults = 1
50+ endif
51+
52+
4853
4954" extension : command
5055" Command is executed if file has specified extension
@@ -58,7 +63,7 @@ if !exists("g:executioner#names")
5863 let g: executioner #names = {}
5964endif
6065
61- if g: executioner #load_defaults
66+ if g: executioner #load_defaults " {{{
6267 if ! has_key (g: executioner #extensions, ' c' )
6368 let g: executioner #extensions[' c' ] = ' gcc ' . g: executioner #full_name . ' -o ' . g: executioner #base_name . ' .out;./' . g: executioner #base_name . ' .out'
6469 endif
@@ -117,9 +122,11 @@ if g:executioner#load_defaults
117122 if ! has_key (g: executioner #names, ' makefile' )
118123 let g: executioner #names[' makefile' ] = ' make'
119124 endif
120- endif
125+ endif " }}}
121126
122- function ! s: SplitNameAndExtenstion (file ) abort
127+ " }}}
128+
129+ function ! s: SplitNameAndExtenstion (file ) abort " {{{
123130 " Get the extension of file name denoted characters after the last "."
124131 " Paramters:
125132 " string file
@@ -143,13 +150,12 @@ function! s:SplitNameAndExtenstion(file) abort
143150 let s: extension = " "
144151 endif
145152 return [s: name , s: extension ]
146- endfunction
147-
148- function ! s: GetExtension ( ... ) abort
153+ endfunction " }}}
154+ function ! s: GetExtension ( ... ) abort " {{{
155+ " TODO
149156 return 1
150- endfunction
151-
152- function ! s: GetExecuteCommand (parsed_input) abort
157+ endfunction " }}}
158+ function ! s: GetExecuteCommand (parsed_input) abort " {{{
153159 " Parameters:
154160 " list parsed_input [string file, string name, string extension,
155161 " string args]
@@ -174,9 +180,8 @@ function! s:GetExecuteCommand(parsed_input) abort
174180 let s: command = s: Substitute (s: command , g: executioner #full_name,
175181 \ a: parsed_input [s: FILE ])
176182 return s: command
177- endfunction
178-
179- function ! s: Substitute (string , old, new ) abort
183+ endfunction " }}}
184+ function ! s: Substitute (string , old, new ) abort " {{{
180185 " Substitute characters of old with strings of new
181186 " Parameters:
182187 " string string to substitute characters
@@ -189,9 +194,8 @@ function! s:Substitute(string, old, new) abort
189194 let s: new_string .= a: string [i ] == a: old ? a: new : a: string [i ]
190195 endfor
191196 return s: new_string
192- endfunction
193-
194- function ! s: ParseInput (file_with_args) abort
197+ endfunction " }}}
198+ function ! s: ParseInput (file_with_args) abort " {{{
195199 " Parses the input into its components
196200 " Parameters:
197201 " string file_with_args - file name, optionally followed by arguments
@@ -227,25 +231,21 @@ function! s:ParseInput(file_with_args) abort
227231
228232 return [s: file_with_extension , s: file [0 ], s: file [1 ], s: arguments ,
229233 \ s: pathless_name ]
230- endfunction
231-
232- function ! s: ExecuteCommandShell (execute_command)
234+ endfunction " }}}
235+ function ! s: ExecuteCommandShell (execute_command) " {{{
233236 execute " !" . a: execute_command
234- endfunction
235-
236- function ! s: GetSplitPrefixTerminal (split_type, execute_command)
237+ endfunction " }}}
238+ function ! s: GetSplitPrefixTerminal (split_type, execute_command) " {{{
237239 return (a: split_type == s: VERTICAL ? " vertical " : " " ) . " terminal "
238240endfunction
239241
240242function ! s: GetSplitPrefixBuffer (split_type)
241243 return (a: split_type == s: NONE ? " " : " ." ) . " !"
242- endfunction
243-
244- function ! s: ExecuteCommandTerminal (split_type, execute_command) abort
244+ endfunction " }}}
245+ function ! s: ExecuteCommandTerminal (split_type, execute_command) abort " {{{
245246 execute s: GetSplitPrefixTerminal (a: split_type , a: execute_command ) . a: execute_command
246- endfunction
247-
248- function ! s: OpenBufferIfNotExists (split_type)
247+ endfunction " }}}
248+ function ! s: OpenBufferIfNotExists (split_type) " {{{
249249 let output_buffer_name = " Output"
250250 let buffer_split = a: split_type == s: VERTICAL ? ' vertical' : ' botright'
251251 " reuse existing buffer window if it exists otherwise create a new one
@@ -258,9 +258,8 @@ function! s:OpenBufferIfNotExists(split_type)
258258 elseif bufwinnr (s: buffer_number ) != bufwinnr (' %' )
259259 silent execute bufwinnr (s: buffer_number ) . ' wincmd w'
260260 endif
261- endfunction
262-
263- function ! s: ConfigureBuffer ()
261+ endfunction " }}}
262+ function ! s: ConfigureBuffer () " {{{
264263 let output_buffer_filetype = " output"
265264 silent execute " setlocal filetype=" . output_buffer_filetype
266265 setlocal bufhidden = delete
@@ -272,22 +271,19 @@ function! s:ConfigureBuffer()
272271 setlocal nonumber
273272 setlocal norelativenumber
274273 setlocal showbreak = " "
275- endfunction
276-
277- function ! s: SetBufferModifiable ()
274+ endfunction " }}}
275+ function ! s: SetBufferModifiable () " {{{
278276 " clear the buffer and make it modifiable for terminal output
279277 setlocal noreadonly
280278 setlocal modifiable
281279 % delete _
282- endfunction
283-
284- function s: ExecuteCommandInBuffer (split_type, execute_command, file_name)
280+ endfunction " }}}
281+ function ! s: ExecuteCommandInBuffer (split_type, execute_command, file_name) " {{{
285282 echon ' Executing ' . a: file_name . ' ... '
286283 " Execute file
287284 execute s: GetSplitPrefixBuffer (a: split_type ) . a: execute_command
288- endfunction
289-
290- function s: SetBufferReadOnly ()
285+ endfunction " }}}
286+ function ! s: SetBufferReadOnly () " {{{
291287 " resize window to content length
292288 " Note: This is annoying because if you print a lot of lines then your
293289 " code buffer is forced to a height of one line every time you execute
@@ -302,27 +298,35 @@ function s:SetBufferReadOnly()
302298 " make the buffer non modifiable
303299 setlocal readonly
304300 setlocal nomodifiable
305- endfunction
306-
307- function ! s: ExecuteCommandBuffer (split_type, execute_command, file_name) abort
301+ endfunction " }}}
302+ function ! s: ExecuteCommandBuffer (split_type, execute_command, file_name) abort " {{{
308303 call s: OpenBufferIfNotExists (a: split_type )
309304 call s: ConfigureBuffer ()
310305 call s: SetBufferModifiable ()
311306 call s: ExecuteCommandInBuffer (a: split_type , a: execute_command , a: file_name )
312307 call s: SetBufferReadOnly ()
313- endfunction
314-
315- function ! s: ExecuteCommand (split_type, execute_command, file_name) abort
316- if a: split_type == s: NONE || (s: has_teriminal && a: execute_command = ~ ' ;' )
308+ endfunction " }}}
309+ function ! s: IsMultiCommand (execute_command) " {{{
310+ return a: execute_command = ~ ' ;'
311+ endfunction " }}}
312+ function ! s: IsVimCommand (execute_command) " {{{
313+ return a: execute_command = ~ s: VIM_COMMAND_PATTERN
314+ endfunction " }}}
315+ function ! s: ExecuteVimCommand (execute_command) " {{{
316+ execute a: execute_command
317+ endfunction " }}}
318+ function ! s: ExecuteCommand (split_type, execute_command, file_name) abort " {{{
319+ if s: IsVimCommand (a: execute_command )
320+ call s: ExecuteVimCommand (a: execute_command )
321+ elseif a: split_type == s: NONE || (s: has_teriminal && s: IsMultiCommand (a: execute_command ))
317322 call s: ExecuteCommandShell (a: execute_command )
318323 elseif s: has_teriminal
319324 call s: ExecuteCommandTerminal (a: split_type , a: execute_command )
320325 else
321326 call s: ExecuteCommandBuffer (a: split_type , a: execute_command , a: file_name )
322327 endif
323- endfunction
324-
325- function ! s: ParseArgs (has_teriminal, split_type, file_with_args)
328+ endfunction " }}}
329+ function ! s: ParseArgs (has_teriminal, split_type, file_with_args) " {{{
326330 let s: has_teriminal = a: has_teriminal
327331
328332 " Note: Temporary fix. If the command is multicommand (has semicolon),
@@ -333,25 +337,22 @@ function! s:ParseArgs(has_teriminal, split_type, file_with_args)
333337 " is being ran with no arguments.
334338 " Otherwise, assume the first argument is the file to be ran.
335339 let s: file_with_args = a: file_with_args != " " ? a: file_with_args : expand (" %" )
336- endfunction
337-
338- function ! s: CommandIsInvalid (execute_command, file_name)
340+ endfunction " }}}
341+ function ! s: CommandIsInvalid (execute_command, file_name) " {{{
339342 if a: execute_command == s: INVALID_COMMAND
340343 execute " echo \" '" . a: file_name . " ' is not configured to be executable.\" "
341344 return 1
342345 endif
343346 return 0
344- endfunction
345-
346- function ! s: SaveFileIfExists ()
347+ endfunction " }}}
348+ function ! s: SaveFileIfExists () " {{{
347349 " Evaluate saving current buffer
348350 " Don't save and reload current file not in file
349351 if &filetype != " "
350352 silent execute " update | edit"
351353 endif
352- endfunction
353-
354- function ! s: SaveAndExecuteFile (... ) abort
354+ endfunction " }}}
355+ function ! s: SaveAndExecuteFile (... ) abort " {{{
355356 " Since the user is not directly calling this, all arguments are guarenteed
356357 " Parameters:
357358 " a:1 int has_teriminal
@@ -378,14 +379,13 @@ function! s:SaveAndExecuteFile(...) abort
378379
379380 " Finally execute command
380381 call s: ExecuteCommand (s: split_type , execute_command, parsed_input[s: FILE ])
381- endfunction
382-
383-
384- " Create commands
382+ endfunction " }}}
383+ " Create commands {{{
385384command ! -nargs =* Executioner :call s: SaveAndExecuteFile (s: has_teriminal , s: NONE , <q-args> )
386385command ! -nargs =* ExecutionerVertical :call s: SaveAndExecuteFile (s: has_teriminal , s: VERTICAL , <q-args> )
387386command ! -nargs =* ExecutionerHorizontal :call s: SaveAndExecuteFile (s: has_teriminal , s: HORIZONTAL , <q-args> )
388387command ! -nargs =* ExecutionerVerticalBuffer :call s: SaveAndExecuteFile (0 , s: VERTICAL , <q-args> )
389388command ! -nargs =* ExecutionerHorizontalBuffer :call s: SaveAndExecuteFile (0 , s: HORIZONTAL , <q-args> )
389+ " }}}
390390
391391let g: executioner #loaded = 1
0 commit comments