asmfmt.vim 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. " asmfmt.vim: Vim command to format Go asm files with asmfmt
  2. " (github.com/klauspost/asmfmt).
  3. "
  4. " This filetype plugin adds new commands for asm buffers:
  5. "
  6. " :Fmt
  7. "
  8. " Filter the current asm buffer through asmfmt.
  9. " It tries to preserve cursor position and avoids
  10. " replacing the buffer with stderr output.
  11. "
  12. " Options:
  13. "
  14. " g:go_asmfmt_autosave [default=0]
  15. "
  16. " Flag to automatically call :Fmt when file is saved.
  17. let s:got_fmt_error = 0
  18. " This is a trimmed-down version of the logic in fmt.vim.
  19. function! go#asmfmt#Format() abort
  20. " Save state.
  21. let l:curw = winsaveview()
  22. " Write the current buffer to a tempfile.
  23. let l:tmpname = tempname()
  24. call writefile(go#util#GetLines(), l:tmpname)
  25. " Run asmfmt.
  26. let path = go#path#CheckBinPath("asmfmt")
  27. if empty(path)
  28. return
  29. endif
  30. let out = go#util#System(path . ' -w ' . l:tmpname)
  31. " If there's no error, replace the current file with the output.
  32. if go#util#ShellError() == 0
  33. " Remove undo point caused by BufWritePre.
  34. try | silent undojoin | catch | endtry
  35. " Replace the current file with the temp file; then reload the buffer.
  36. let old_fileformat = &fileformat
  37. " save old file permissions
  38. let original_fperm = getfperm(expand('%'))
  39. call rename(l:tmpname, expand('%'))
  40. " restore old file permissions
  41. call setfperm(expand('%'), original_fperm)
  42. silent edit!
  43. let &fileformat = old_fileformat
  44. let &syntax = &syntax
  45. endif
  46. " Restore the cursor/window positions.
  47. call winrestview(l:curw)
  48. endfunction
  49. function! go#asmfmt#ToggleAsmFmtAutoSave() abort
  50. if get(g:, "go_asmfmt_autosave", 0)
  51. let g:go_asmfmt_autosave = 1
  52. call go#util#EchoProgress("auto asmfmt enabled")
  53. return
  54. end
  55. let g:go_asmfmt_autosave = 0
  56. call go#util#EchoProgress("auto asmfmt disabled")
  57. endfunction
  58. " vim: sw=2 ts=2 et