aboutsummaryrefslogtreecommitdiff
path: root/vim/plugin/shebang_create_exec.vim
blob: 7fabd8f7b03773c2c471a02e56c102850806d091 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"
" shebang_create_exec.vim: Make a file executable on first save if it starts with a
" shebang.
"
" Author: Tom Ryder <tom@sanctum.geek.nz>
" License: Same as Vim itself
"
if exists('g:loaded_shebang_create_exec') || &compatible
  finish
endif
if !has('autocmd') || !has('unix') || !exists('*shellescape')
  finish
endif
let g:loaded_shebang_create_exec = 1

" Set up hook for before writes to check the buffer for new shebangs
augroup shebang_create_exec
  autocmd!
  autocmd BufWritePre * call s:Check(expand('<afile>:p'))
augroup END

" If the buffer starts with a shebang and the file being saved to doesn't
" exist yet, set up a hook to make it executable after the write is done
function! s:Check(filename) abort
  if stridx(getline(1), '#!') == 0 && !filereadable(a:filename)
    autocmd shebang_create_exec BufWritePost <buffer>
          \ call s:Chmod(expand('<afile>:p'))
  endif
endfunction

" Make the file executable and clear away the hook that called us
function! s:Chmod(filename) abort
  autocmd! shebang_create_exec BufWritePost <buffer>
  call system('chmod +x '.shellescape(a:filename))
endfunction