Skip to content

Commit ecc511c

Browse files
author
skywind3000
committed
new command modifier "shebang"
1 parent 78fc6f5 commit ecc511c

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

autoload/asyncrun/program/shebang.vim

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"======================================================================
2+
"
3+
" shebang.vim -
4+
"
5+
" Created by skywind on 2022/01/12
6+
" Last Modified: 2022/01/12 22:30:25
7+
"
8+
"======================================================================
9+
10+
function! asyncrun#program#shebang#translate(opts)
11+
let cmd = asyncrun#utils#strip(a:opts.cmd)
12+
if cmd == ''
13+
return 'echo empty command'
14+
endif
15+
if cmd =~ '^".*"$'
16+
let cmd = asyncrun#utils#strip(strpart(cmd, 1, len(cmd) - 2))
17+
endif
18+
if cmd =~ '^\''.*\''$'
19+
let cmd = asyncrun#utils#strip(strpart(cmd, 1, len(cmd) - 2))
20+
endif
21+
if filereadable(cmd) == 0
22+
return 'echo file not find: ' . cmd
23+
endif
24+
let textlist = readfile(cmd, '', 20)
25+
let shebang = ''
26+
for text in textlist
27+
let text = asyncrun#utils#strip(text)
28+
if text =~ '^#'
29+
let text = asyncrun#utils#strip(strpart(text, 1))
30+
if text =~ '^!'
31+
let shebang = asyncrun#utils#strip(strpart(text, 1))
32+
break
33+
endif
34+
endif
35+
endfor
36+
if shebang == ''
37+
return 'echo shebang not find in: ' . cmd
38+
endif
39+
return shebang . ' "' . cmd . '"'
40+
endfunc
41+
42+

autoload/asyncrun/utils.vim

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ function! asyncrun#utils#errmsg(msg)
2222
endfunc
2323

2424

25+
"----------------------------------------------------------------------
26+
" strip string
27+
"----------------------------------------------------------------------
28+
function! asyncrun#utils#strip(text)
29+
return substitute(a:text, '^\s*\(.\{-}\)\s*$', '\1', '')
30+
endfunc
31+
32+
33+
"----------------------------------------------------------------------
34+
" Replace string
35+
"----------------------------------------------------------------------
36+
function! asyncrun#utils#replace(text, old, new)
37+
let l:data = split(a:text, a:old, 1)
38+
return join(l:data, a:new)
39+
endfunc
40+
41+
2542
"----------------------------------------------------------------------
2643
" display require message
2744
"----------------------------------------------------------------------

0 commit comments

Comments
 (0)