From cebbf704f7e3048624bddb2e96eaa1db5fe9ef16 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Tue, 4 Dec 2018 08:45:38 +1300 Subject: Unseat tidy(1) as HTML 'equalprg' Instead, remap \= specifically to use tidy(1) to reformat the whole buffer. --- vim/after/ftplugin/html.vim | 11 ++++++++--- vim/after/ftplugin/html.vim~ | 35 +++++++++++++++++++++++++++++++++++ vim/autoload/html.vim | 7 +++++++ 3 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 vim/after/ftplugin/html.vim~ diff --git a/vim/after/ftplugin/html.vim b/vim/after/ftplugin/html.vim index b5c387fb..5f7155bc 100644 --- a/vim/after/ftplugin/html.vim +++ b/vim/after/ftplugin/html.vim @@ -3,11 +3,16 @@ if &filetype !=# 'html' finish endif -" Use tidy(1) for checking and program formatting +" Use tidy(1) for checking compiler tidy -setlocal equalprg=tidy\ -quiet let b:undo_ftplugin .= '|unlet b:current_compiler' - \ . '|setlocal equalprg< errorformat< makeprg<' + \ . '|setlocal errorformat< makeprg<' + +" tidy(1) copes fine with formatting an entire document, but not just part of +" it; we map \= to do the former, but don't actually set 'equalprg', falling +" back on the good-enough built-in Vim indentation behavior. +nnoremap = :call html#TidyBuffer() +let b:undo_ftplugin .= '|nunmap =' " Set up hooks for timestamp updating augroup html_timestamp diff --git a/vim/after/ftplugin/html.vim~ b/vim/after/ftplugin/html.vim~ new file mode 100644 index 00000000..5f7155bc --- /dev/null +++ b/vim/after/ftplugin/html.vim~ @@ -0,0 +1,35 @@ +" Don't load if the buffer is not actually HTML (e.g. Markdown) +if &filetype !=# 'html' + finish +endif + +" Use tidy(1) for checking +compiler tidy +let b:undo_ftplugin .= '|unlet b:current_compiler' + \ . '|setlocal errorformat< makeprg<' + +" tidy(1) copes fine with formatting an entire document, but not just part of +" it; we map \= to do the former, but don't actually set 'equalprg', falling +" back on the good-enough built-in Vim indentation behavior. +nnoremap = :call html#TidyBuffer() +let b:undo_ftplugin .= '|nunmap =' + +" Set up hooks for timestamp updating +augroup html_timestamp + autocmd BufWritePre + \ if exists('b:html_timestamp_check') + \| call html#TimestampUpdate() + \|endif +augroup END +let b:undo_ftplugin .= '|execute ''autocmd! html_timestamp''' + \ . '|augroup! html_timestamp' + +" Stop here if the user doesn't want ftplugin mappings +if exists('g:no_plugin_maps') || exists('g:no_html_maps') + finish +endif + +" Transform URLs to HTML anchors +nnoremap r + \ :call html#UrlLink() +let b:undo_ftplugin .= '|nunmap r' diff --git a/vim/autoload/html.vim b/vim/autoload/html.vim index c3d99706..e0d47e47 100644 --- a/vim/autoload/html.vim +++ b/vim/autoload/html.vim @@ -14,6 +14,13 @@ function! html#UrlLink() abort endfunction +" Tidy the whole buffer +function! html#TidyBuffer() abort + let l:view = winsaveview() + %!tidy -quiet + call winrestview(l:view) +endfunction + " Update a timestamp function! html#TimestampUpdate() abort if !&modified -- cgit v1.2.3