2016-02-20 08:13:10 -05:00
|
|
|
" asmfmt.vim: Vim command to format Go asm files with asmfmt
|
|
|
|
" (github.com/klauspost/asmfmt).
|
|
|
|
"
|
|
|
|
" This filetype plugin adds new commands for asm buffers:
|
|
|
|
"
|
|
|
|
" :Fmt
|
|
|
|
"
|
|
|
|
" Filter the current asm buffer through asmfmt.
|
|
|
|
" It tries to preserve cursor position and avoids
|
|
|
|
" replacing the buffer with stderr output.
|
|
|
|
"
|
|
|
|
" Options:
|
|
|
|
"
|
2016-12-27 09:46:49 -05:00
|
|
|
" g:go_asmfmt_autosave [default=0]
|
2016-02-20 08:13:10 -05:00
|
|
|
"
|
|
|
|
" Flag to automatically call :Fmt when file is saved.
|
|
|
|
|
|
|
|
let s:got_fmt_error = 0
|
|
|
|
|
|
|
|
" This is a trimmed-down version of the logic in fmt.vim.
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#asmfmt#Format() abort
|
2016-02-20 08:13:10 -05:00
|
|
|
" Save state.
|
|
|
|
let l:curw = winsaveview()
|
|
|
|
|
|
|
|
" Write the current buffer to a tempfile.
|
|
|
|
let l:tmpname = tempname()
|
2017-02-11 08:01:38 -05:00
|
|
|
call writefile(go#util#GetLines(), l:tmpname)
|
2016-02-20 08:13:10 -05:00
|
|
|
|
|
|
|
" Run asmfmt.
|
|
|
|
let path = go#path#CheckBinPath("asmfmt")
|
|
|
|
if empty(path)
|
|
|
|
return
|
|
|
|
endif
|
2016-05-14 07:57:54 -04:00
|
|
|
let out = go#util#System(path . ' -w ' . l:tmpname)
|
2016-02-20 08:13:10 -05:00
|
|
|
|
|
|
|
" If there's no error, replace the current file with the output.
|
2016-05-14 07:57:54 -04:00
|
|
|
if go#util#ShellError() == 0
|
2016-02-20 08:13:10 -05:00
|
|
|
" Remove undo point caused by BufWritePre.
|
|
|
|
try | silent undojoin | catch | endtry
|
|
|
|
|
|
|
|
" Replace the current file with the temp file; then reload the buffer.
|
|
|
|
let old_fileformat = &fileformat
|
2016-08-02 08:48:32 -04:00
|
|
|
" save old file permissions
|
|
|
|
let original_fperm = getfperm(expand('%'))
|
2016-02-20 08:13:10 -05:00
|
|
|
call rename(l:tmpname, expand('%'))
|
2016-08-02 08:48:32 -04:00
|
|
|
" restore old file permissions
|
|
|
|
call setfperm(expand('%'), original_fperm)
|
2016-02-20 08:13:10 -05:00
|
|
|
silent edit!
|
|
|
|
let &fileformat = old_fileformat
|
|
|
|
let &syntax = &syntax
|
|
|
|
endif
|
|
|
|
|
|
|
|
" Restore the cursor/window positions.
|
|
|
|
call winrestview(l:curw)
|
|
|
|
endfunction
|
2016-06-26 07:12:36 -04:00
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#asmfmt#ToggleAsmFmtAutoSave() abort
|
|
|
|
if get(g:, "go_asmfmt_autosave", 0)
|
|
|
|
let g:go_asmfmt_autosave = 1
|
|
|
|
call go#util#EchoProgress("auto asmfmt enabled")
|
2016-08-02 08:48:32 -04:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
let g:go_asmfmt_autosave = 0
|
|
|
|
call go#util#EchoProgress("auto asmfmt disabled")
|
2016-08-02 08:48:32 -04:00
|
|
|
endfunction
|
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" vim: sw=2 ts=2 et
|