Skip to content

Commit 9389297

Browse files
committed
executioner.vim 1.2.2
Fix file names in g:executioner#names not being detected correctly if executed in another directory.
1 parent d852a6b commit 9389297

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

plugin/executioner.vim

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
" ============================================================================
22
" File: executioner.vim
33
" Maintainer: https://github.com/EvanQuan/vim-executioner/
4-
" Version: 1.2.1
4+
" Version: 1.2.2
55
"
66
" A Vim plugin to easily execute files in the terminal or a separate buffer.
77
" ============================================================================
88

99
if exists("g:executioner#loaded")
1010
finish
1111
endif
12-
let g:executioner#loaded = 1
1312

1413
" Name and extension
1514
if !exists("g:executioner#full_name")
@@ -31,6 +30,7 @@ let s:FILE = 0
3130
let s:NAME = 1
3231
let s:EXTENSION = 2
3332
let s:ARGS = 3
33+
let s:PATHLESS_NAME = 4
3434

3535
" Split types
3636
let s:NONE = 0
@@ -41,6 +41,8 @@ let s:HORIZONTAL = 3
4141
let s:EXTENSION_COMMAND = 0
4242
let s:NAME_COMMAND = 1
4343

44+
let s:DIRECTORY_SEPARATOR = '[/\\]'
45+
4446
" extension : command
4547
" Command is executed if file has specified extension
4648
if !exists("g:executioner#extensions")
@@ -143,12 +145,15 @@ endfunction
143145

144146
function! s:GetExecuteCommand(parsed_input) abort
145147
" Parameters:
146-
" list parsed_input [string file, string name, string extension, string args]
148+
" list parsed_input [string file, string name, string extension,
149+
" string args]
147150
" Returns:
148151
" string the execute command of the parsed_input if executable
149152
" "" if not executable
150-
if has_key(g:executioner#names, a:parsed_input[s:FILE])
151-
let s:command = g:executioner#names[a:parsed_input[s:FILE]]
153+
if !filereadable(expand(s:parsed_input[s:FILE]))
154+
let s:command = ""
155+
elseif has_key(g:executioner#names, a:parsed_input[s:PATHLESS_NAME])
156+
let s:command = g:executioner#names[a:parsed_input[s:PATHLESS_NAME]]
152157
\ . a:parsed_input[s:ARGS]
153158
elseif has_key(g:executioner#extensions, a:parsed_input[s:EXTENSION])
154159
let s:command = g:executioner#extensions[a:parsed_input[s:EXTENSION]]
@@ -190,7 +195,8 @@ function! s:ParseInput(file_with_args) abort
190195
" Parameters:
191196
" string file_with_args - file name, optionally followed by arguments
192197
" Returns:
193-
" list [string file, string name, string extension, string arguments]
198+
" list [string file, string name, string extension, string arguments,
199+
" string pathless_name]
194200

195201
" If no arguments supplied, then there is nothing to parse
196202
if len(a:file_with_args) == 0
@@ -209,12 +215,17 @@ function! s:ParseInput(file_with_args) abort
209215
" Split the file into its name and extension.
210216
let s:file = s:SplitNameAndExtenstion(s:file_with_extension)
211217

218+
" Remove path from name
219+
let s:directories = split(s:file[0], s:DIRECTORY_SEPARATOR)
220+
let s:pathless_name = s:directories[len(s:directories) - 1]
221+
212222
" Remaining terms are arguments
213223
" Join all arguments back together with spaces
214224
let s:arguments = len(s:input_terms) > 1 ?
215225
\ " " . join(s:input_terms[1:], " ") : ""
216226

217-
return [s:file_with_extension, s:file[0], s:file[1], s:arguments]
227+
return [s:file_with_extension, s:file[0], s:file[1], s:arguments,
228+
\ s:pathless_name]
218229
endfunction
219230

220231
function! s:GetSplitPrefix(split_type) abort
@@ -395,8 +406,27 @@ endfunction
395406
" endfunction
396407

397408
" nnoremap <leader>d :call g:Debug(2, "test.cpp")<CR>
409+
"
410+
function! g:Test()
411+
let s:string1 = 'foo/bar/file1.extension'
412+
let s:string2 = 'foo\bar\file2.extension'
413+
414+
let s:split1 = split(s:string1, s:DIRECTORY_SEPARATOR)
415+
let s:split2 = split(s:string2, s:DIRECTORY_SEPARATOR)
416+
417+
let s:name1 = s:split1[len(s:split1) - 1]
418+
let s:name2 = s:split2[len(s:split2) - 1]
419+
420+
echom s:name1
421+
echom s:name2
422+
endfunction
423+
424+
nnoremap <leader>d :call g:Test()<CR>
425+
398426

399427
" Create commands
400428
command! -nargs=* Executioner :call s:SaveAndExecuteFile(s:NONE, <q-args>)
401429
command! -nargs=* ExecutionerVertical :call s:SaveAndExecuteFile(s:VERTICAL, <q-args>)
402430
command! -nargs=* ExecutionerHorizontal :call s:SaveAndExecuteFile(s:HORIZONTAL, <q-args>)
431+
432+
let g:executioner#loaded = 1

0 commit comments

Comments
 (0)