1
1
" ============================================================================
2
2
" File: executioner.vim
3
3
" Maintainer: https://github.com/EvanQuan/vim-executioner/
4
- " Version: 1.3.1
4
+ " Version: 1.4.0
5
5
"
6
6
" A Vim plugin to easily execute files in the terminal or a separate buffer.
7
7
" You can learn more about it with:
@@ -14,19 +14,7 @@ if exists("g:executioner#loaded")
14
14
finish
15
15
endif
16
16
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 {{{
30
18
" Parsed input
31
19
let s: FILE = 0
32
20
let s: NAME = 1
@@ -43,8 +31,25 @@ let s:HORIZONTAL = 3
43
31
let s: INVALID_COMMAND = -1
44
32
45
33
let s: DIRECTORY_SEPARATOR = ' [/\\]'
34
+ let s: VIM_COMMAND_PATTERN = ' ^:.\+'
46
35
47
36
let 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
+
48
53
49
54
" extension : command
50
55
" Command is executed if file has specified extension
@@ -58,7 +63,7 @@ if !exists("g:executioner#names")
58
63
let g: executioner #names = {}
59
64
endif
60
65
61
- if g: executioner #load_defaults
66
+ if g: executioner #load_defaults " {{{
62
67
if ! has_key (g: executioner #extensions, ' c' )
63
68
let g: executioner #extensions[' c' ] = ' gcc ' . g: executioner #full_name . ' -o ' . g: executioner #base_name . ' .out;./' . g: executioner #base_name . ' .out'
64
69
endif
@@ -117,9 +122,11 @@ if g:executioner#load_defaults
117
122
if ! has_key (g: executioner #names, ' makefile' )
118
123
let g: executioner #names[' makefile' ] = ' make'
119
124
endif
120
- endif
125
+ endif " }}}
121
126
122
- function ! s: SplitNameAndExtenstion (file ) abort
127
+ " }}}
128
+
129
+ function ! s: SplitNameAndExtenstion (file ) abort " {{{
123
130
" Get the extension of file name denoted characters after the last "."
124
131
" Paramters:
125
132
" string file
@@ -143,13 +150,12 @@ function! s:SplitNameAndExtenstion(file) abort
143
150
let s: extension = " "
144
151
endif
145
152
return [s: name , s: extension ]
146
- endfunction
147
-
148
- function ! s: GetExtension ( ... ) abort
153
+ endfunction " }}}
154
+ function ! s: GetExtension ( ... ) abort " {{{
155
+ " TODO
149
156
return 1
150
- endfunction
151
-
152
- function ! s: GetExecuteCommand (parsed_input) abort
157
+ endfunction " }}}
158
+ function ! s: GetExecuteCommand (parsed_input) abort " {{{
153
159
" Parameters:
154
160
" list parsed_input [string file, string name, string extension,
155
161
" string args]
@@ -174,9 +180,8 @@ function! s:GetExecuteCommand(parsed_input) abort
174
180
let s: command = s: Substitute (s: command , g: executioner #full_name,
175
181
\ a: parsed_input [s: FILE ])
176
182
return s: command
177
- endfunction
178
-
179
- function ! s: Substitute (string , old, new ) abort
183
+ endfunction " }}}
184
+ function ! s: Substitute (string , old, new ) abort " {{{
180
185
" Substitute characters of old with strings of new
181
186
" Parameters:
182
187
" string string to substitute characters
@@ -189,9 +194,8 @@ function! s:Substitute(string, old, new) abort
189
194
let s: new_string .= a: string [i ] == a: old ? a: new : a: string [i ]
190
195
endfor
191
196
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 " {{{
195
199
" Parses the input into its components
196
200
" Parameters:
197
201
" string file_with_args - file name, optionally followed by arguments
@@ -227,25 +231,21 @@ function! s:ParseInput(file_with_args) abort
227
231
228
232
return [s: file_with_extension , s: file [0 ], s: file [1 ], s: arguments ,
229
233
\ s: pathless_name ]
230
- endfunction
231
-
232
- function ! s: ExecuteCommandShell (execute_command)
234
+ endfunction " }}}
235
+ function ! s: ExecuteCommandShell (execute_command) " {{{
233
236
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) " {{{
237
239
return (a: split_type == s: VERTICAL ? " vertical " : " " ) . " terminal "
238
240
endfunction
239
241
240
242
function ! s: GetSplitPrefixBuffer (split_type)
241
243
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 " {{{
245
246
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) " {{{
249
249
let output_buffer_name = " Output"
250
250
let buffer_split = a: split_type == s: VERTICAL ? ' vertical' : ' botright'
251
251
" reuse existing buffer window if it exists otherwise create a new one
@@ -258,9 +258,8 @@ function! s:OpenBufferIfNotExists(split_type)
258
258
elseif bufwinnr (s: buffer_number ) != bufwinnr (' %' )
259
259
silent execute bufwinnr (s: buffer_number ) . ' wincmd w'
260
260
endif
261
- endfunction
262
-
263
- function ! s: ConfigureBuffer ()
261
+ endfunction " }}}
262
+ function ! s: ConfigureBuffer () " {{{
264
263
let output_buffer_filetype = " output"
265
264
silent execute " setlocal filetype=" . output_buffer_filetype
266
265
setlocal bufhidden = delete
@@ -272,22 +271,19 @@ function! s:ConfigureBuffer()
272
271
setlocal nonumber
273
272
setlocal norelativenumber
274
273
setlocal showbreak = " "
275
- endfunction
276
-
277
- function ! s: SetBufferModifiable ()
274
+ endfunction " }}}
275
+ function ! s: SetBufferModifiable () " {{{
278
276
" clear the buffer and make it modifiable for terminal output
279
277
setlocal noreadonly
280
278
setlocal modifiable
281
279
% 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) " {{{
285
282
echon ' Executing ' . a: file_name . ' ... '
286
283
" Execute file
287
284
execute s: GetSplitPrefixBuffer (a: split_type ) . a: execute_command
288
- endfunction
289
-
290
- function s: SetBufferReadOnly ()
285
+ endfunction " }}}
286
+ function ! s: SetBufferReadOnly () " {{{
291
287
" resize window to content length
292
288
" Note: This is annoying because if you print a lot of lines then your
293
289
" code buffer is forced to a height of one line every time you execute
@@ -302,27 +298,35 @@ function s:SetBufferReadOnly()
302
298
" make the buffer non modifiable
303
299
setlocal readonly
304
300
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 " {{{
308
303
call s: OpenBufferIfNotExists (a: split_type )
309
304
call s: ConfigureBuffer ()
310
305
call s: SetBufferModifiable ()
311
306
call s: ExecuteCommandInBuffer (a: split_type , a: execute_command , a: file_name )
312
307
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 ))
317
322
call s: ExecuteCommandShell (a: execute_command )
318
323
elseif s: has_teriminal
319
324
call s: ExecuteCommandTerminal (a: split_type , a: execute_command )
320
325
else
321
326
call s: ExecuteCommandBuffer (a: split_type , a: execute_command , a: file_name )
322
327
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) " {{{
326
330
let s: has_teriminal = a: has_teriminal
327
331
328
332
" Note: Temporary fix. If the command is multicommand (has semicolon),
@@ -333,25 +337,22 @@ function! s:ParseArgs(has_teriminal, split_type, file_with_args)
333
337
" is being ran with no arguments.
334
338
" Otherwise, assume the first argument is the file to be ran.
335
339
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) " {{{
339
342
if a: execute_command == s: INVALID_COMMAND
340
343
execute " echo \" '" . a: file_name . " ' is not configured to be executable.\" "
341
344
return 1
342
345
endif
343
346
return 0
344
- endfunction
345
-
346
- function ! s: SaveFileIfExists ()
347
+ endfunction " }}}
348
+ function ! s: SaveFileIfExists () " {{{
347
349
" Evaluate saving current buffer
348
350
" Don't save and reload current file not in file
349
351
if &filetype != " "
350
352
silent execute " update | edit"
351
353
endif
352
- endfunction
353
-
354
- function ! s: SaveAndExecuteFile (... ) abort
354
+ endfunction " }}}
355
+ function ! s: SaveAndExecuteFile (... ) abort " {{{
355
356
" Since the user is not directly calling this, all arguments are guarenteed
356
357
" Parameters:
357
358
" a:1 int has_teriminal
@@ -378,14 +379,13 @@ function! s:SaveAndExecuteFile(...) abort
378
379
379
380
" Finally execute command
380
381
call s: ExecuteCommand (s: split_type , execute_command, parsed_input[s: FILE ])
381
- endfunction
382
-
383
-
384
- " Create commands
382
+ endfunction " }}}
383
+ " Create commands {{{
385
384
command ! -nargs =* Executioner :call s: SaveAndExecuteFile (s: has_teriminal , s: NONE , <q-args> )
386
385
command ! -nargs =* ExecutionerVertical :call s: SaveAndExecuteFile (s: has_teriminal , s: VERTICAL , <q-args> )
387
386
command ! -nargs =* ExecutionerHorizontal :call s: SaveAndExecuteFile (s: has_teriminal , s: HORIZONTAL , <q-args> )
388
387
command ! -nargs =* ExecutionerVerticalBuffer :call s: SaveAndExecuteFile (0 , s: VERTICAL , <q-args> )
389
388
command ! -nargs =* ExecutionerHorizontalBuffer :call s: SaveAndExecuteFile (0 , s: HORIZONTAL , <q-args> )
389
+ " }}}
390
390
391
391
let g: executioner #loaded = 1
0 commit comments