1
0
mirror of https://github.com/amix/vimrc synced 2025-02-28 14:12:51 +08:00
amix-vimrc-mirror/sources_non_forked/vim-go/autoload/go/asmfmt.vim

77 lines
1.9 KiB
VimL
Raw Normal View History

2016-02-20 21:13:10 +08: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 22:46:49 +08:00
" g:go_asmfmt_autosave [default=0]
2016-02-20 21:13:10 +08:00
"
" Flag to automatically call :Fmt when file is saved.
2018-12-17 19:28:27 +08:00
" don't spam the user when Vim is started in Vi compatibility mode
let s:cpo_save = &cpo
set cpo&vim
2016-02-20 21:13:10 +08:00
let s:got_fmt_error = 0
" This is a trimmed-down version of the logic in fmt.vim.
2016-12-27 22:46:49 +08:00
function! go#asmfmt#Format() abort
2016-02-20 21:13:10 +08:00
" Save state.
let l:curw = winsaveview()
" Write the current buffer to a tempfile.
let l:tmpname = tempname()
2017-02-11 21:01:38 +08:00
call writefile(go#util#GetLines(), l:tmpname)
2016-02-20 21:13:10 +08:00
" Run asmfmt.
2018-06-14 18:31:12 +08:00
let [l:out, l:err] = go#util#Exec(['asmfmt', '-w', l:tmpname])
if l:err
call go#util#EchoError(l:out)
2016-02-20 21:13:10 +08:00
return
endif
2018-06-14 18:31:12 +08:00
" Remove undo point caused by BufWritePre.
try | silent undojoin | catch | endtry
2016-02-20 21:13:10 +08:00
2018-06-14 18:31:12 +08:00
" Replace the current file with the temp file; then reload the buffer.
let old_fileformat = &fileformat
" save old file permissions
let original_fperm = getfperm(expand('%'))
call rename(l:tmpname, expand('%'))
" restore old file permissions
call setfperm(expand('%'), original_fperm)
silent edit!
let &fileformat = old_fileformat
let &syntax = &syntax
2016-02-20 21:13:10 +08:00
" Restore the cursor/window positions.
call winrestview(l:curw)
endfunction
2016-06-26 19:12:36 +08:00
2016-12-27 22:46:49 +08:00
function! go#asmfmt#ToggleAsmFmtAutoSave() abort
2018-06-14 18:31:12 +08:00
if go#config#AsmfmtAutosave()
call go#config#SetAsmfmtAutosave(1)
2016-12-27 22:46:49 +08:00
call go#util#EchoProgress("auto asmfmt enabled")
2016-08-02 20:48:32 +08:00
return
end
2018-06-14 18:31:12 +08:00
call go#config#SetAsmfmtAutosave(0)
2016-12-27 22:46:49 +08:00
call go#util#EchoProgress("auto asmfmt disabled")
2016-08-02 20:48:32 +08:00
endfunction
2018-12-17 19:28:27 +08:00
" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save
2016-06-26 19:12:36 +08:00
" vim: sw=2 ts=2 et