Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore sudoedit filetype detection #104

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ include:
[guard][].
* `:SudoWrite`: Write a privileged file with `sudo`.
* `:SudoEdit`: Edit a privileged file with `sudo`.
* File type detection for `sudo -e` is based on original file name.
* Typing a shebang line causes the file type to be re-detected. Additionally
the file will be automatically made executable (`chmod +x`) after the next
write.
Expand Down
4 changes: 4 additions & 0 deletions doc/eunuch.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ COMMANDS *eunuch-commands*

PASSIVE BEHAVIORS *eunuch-passive*

File type detection for files edited with `sudoedit` and `sudo -e` happens
based on the original file name, as found in the proc filesystem, and
currently works only on Linux and BSD.

If you type a line at the beginning of a file that starts with #! and press
<CR>, The current file type will be re-detected. This is implemented using a
<CR> map. If you already have a <CR> map, Eunuch will attempt to combine with
Expand Down
32 changes: 32 additions & 0 deletions plugin/eunuch.vim
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,38 @@ command! -bar -bang SudoWrite
\ write!
endif

if has('linux')
let s:ppid = matchlist(readfile('/proc/self/status'), '^PPid:\s\+\(\d\+\)')[1]
elseif has('bsd')
let s:ppid = split(readfile('/proc/curproc/status')[0], ' ')[2]
endif

if exists('s:ppid')
let s:parent_cmdline = split(readfile('/proc/' . s:ppid . '/cmdline')[0], '\n')

if s:parent_cmdline[0] ==# 'sudoedit'
let s:sudo_files_offset = 1
elseif s:parent_cmdline[0] ==# 'sudo' && s:parent_cmdline[1] ==# '-e'
let s:sudo_files_offset = 2
endif
endif

function! s:SudoEditInit() abort
let files = s:parent_cmdline[s:sudo_files_offset:-1]
if len(files) ==# argc()
for i in range(argc())
execute 'autocmd BufEnter' fnameescape(argv(i))
\ 'if empty(&filetype) || &filetype ==# "conf"'
\ '|doautocmd filetypedetect BufReadPost' fnameescape(files[i])
\ '|endif'
endfor
endif
endfunction

if exists('s:sudo_files_offset')
call s:SudoEditInit()
endif

command! -bar Wall call s:Wall()
if exists(':W') !=# 2
command! -bar W Wall
Expand Down