Skip to content

Commit 3b1a424

Browse files
committed
executioner 1.4.0
Add ability to execute files with Vim commands
1 parent 659e26c commit 3b1a424

File tree

3 files changed

+105
-77
lines changed

3 files changed

+105
-77
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ including `>`, `<`, and `|` characters.
158158
Any terminal window command that involves input redirection will fall back to
159159
the buffer equivalent if input redirection operators are found.
160160

161+
#### Vim Commands
162+
163+
If any executing command defined in `g:executioner#extensions` or
164+
`g:executioner#names` starts with ':', a Vim command will be executed instead
165+
of a shell, terminal window, or buffer. For example, if the following is
166+
defined in your `vimrc`:
167+
```vim
168+
let g:executioner#extensions['md'] = ':InstantMarkdownPreview'
169+
let g:executioner#extensions['markdown'] = ':InstantMarkdownPreview'
170+
```
171+
then running any markdown files with any of the executioner commands will
172+
execute the Vim command `:InstantMarkdownPreview`.
173+
161174
#### Key Mappings
162175

163176
By default, Executioner does not provide any key mappings as to not override

doc/executioner.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*executioner.txt* For Vim version 8.1 Last change: 2019 January 02
1+
*executioner.txt* For Vim version 8.1 Last change: 2019 January 06
22

33
_____ _ _ ~
44
| ___| | | (_) ~
@@ -21,6 +21,7 @@ CONTENTS *executioner-contents*
2121
2. Functionality............................|executioner-functionality|
2222
2.1 Commands............................|executioner-commands|
2323
2.2.1 Terminal Window vs. Buffer....|executioner-terminal-vs-buffer|
24+
2.2.2 Vim Commands..................|executioner-vim-commands|
2425
2.2 Variables...........................|executioner-variables|
2526
3. Mappings.................................|executioner-mappings|
2627

@@ -158,6 +159,20 @@ including `>`, `<`, and `|` characters.
158159
Any terminal window command that involves input redirection will fall back to
159160
the buffer equivalent if input redirection operators are found.
160161

162+
------------------------------------------------------------------------------
163+
2.2.2 Vim Commands *executioner-vim-commands*
164+
165+
If any executing command defined in |g:executioner#extensions| or
166+
|g:executioner#names| starts with ':', a Vim command will be executed instead
167+
of a shell, terminal window, or buffer. For example, if the following is
168+
defined in your |vimrc|: >
169+
170+
let g:executioner#extensions['md'] = ':InstantMarkdownPreview'
171+
let g:executioner#extensions['markdown'] = ':InstantMarkdownPreview'
172+
<
173+
then running any markdown files with any of the |executioner-commands| will
174+
execute the Vim command `:InstantMarkdownPreview`.
175+
161176
------------------------------------------------------------------------------
162177
2.2 Variables *executioner-variables*
163178

plugin/executioner.vim

Lines changed: 76 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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
1515
endif
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
3119
let s:FILE = 0
3220
let s:NAME = 1
@@ -43,8 +31,25 @@ let s:HORIZONTAL = 3
4331
let s:INVALID_COMMAND = -1
4432

4533
let s:DIRECTORY_SEPARATOR = '[/\\]'
34+
let s:VIM_COMMAND_PATTERN = '^:.\+'
4635

4736
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+
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 = {}
5964
endif
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 "
238240
endfunction
239241

240242
function! 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 {{{
385384
command! -nargs=* Executioner :call s:SaveAndExecuteFile(s:has_teriminal, s:NONE, <q-args>)
386385
command! -nargs=* ExecutionerVertical :call s:SaveAndExecuteFile(s:has_teriminal, s:VERTICAL, <q-args>)
387386
command! -nargs=* ExecutionerHorizontal :call s:SaveAndExecuteFile(s:has_teriminal, s:HORIZONTAL, <q-args>)
388387
command! -nargs=* ExecutionerVerticalBuffer :call s:SaveAndExecuteFile(0, s:VERTICAL, <q-args>)
389388
command! -nargs=* ExecutionerHorizontalBuffer :call s:SaveAndExecuteFile(0, s:HORIZONTAL, <q-args>)
389+
" }}}
390390

391391
let g:executioner#loaded = 1

0 commit comments

Comments
 (0)