aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2018-06-03 16:07:52 +1200
committerTom Ryder <tom@sanctum.geek.nz>2018-06-03 16:07:52 +1200
commit306eaf37ef79df27a61c01bd1f18fcffed12a682 (patch)
treeb5d1adb1c1d784b0acbdeac9a7a2023953a53b33
parentc92b0b0bdab2bf4a284d1631c3f8d33cc4979aa0 (diff)
downloaddotfiles-306eaf37ef79df27a61c01bd1f18fcffed12a682.tar.gz
Vim ftplugin: hold autoformat in *.md code blocks
-rw-r--r--vim/after/ftplugin/markdown/suspend_autoformat.vim34
1 files changed, 34 insertions, 0 deletions
diff --git a/vim/after/ftplugin/markdown/suspend_autoformat.vim b/vim/after/ftplugin/markdown/suspend_autoformat.vim
new file mode 100644
index 00000000..b2de7828
--- /dev/null
+++ b/vim/after/ftplugin/markdown/suspend_autoformat.vim
@@ -0,0 +1,34 @@
+" Only do this when not done yet for this buffer
+" Also do nothing if 'compatible' enabled, or if no autocmd feature, or if Vim
+" is too old to support the needed autocmd events
+if exists('b:did_ftplugin_markdown_suspend_autoformat') || &compatible
+ finish
+endif
+if !has('autocmd') || v:version < 700
+ finish
+endif
+let b:did_ftplugin_markdown_suspend_autoformat = 1
+if exists('b:undo_ftplugin')
+ let b:undo_ftplugin = b:undo_ftplugin
+ \ . '|unlet b:did_ftplugin_markdown_suspend_autoformat'
+endif
+
+" When editing a code block, quietly remove auto 'a' from 'formatoptions' if
+" present, flagging that we've done so; restore it once we move away.
+autocmd BufWinEnter,CursorMoved,CursorMovedI,WinEnter
+ \ <buffer>
+ \ if getline('.') =~# '\m^ '
+ \ | if &formatoptions =~# '\ma'
+ \ | setlocal formatoptions-=a
+ \ | let b:markdown_suspend_autoformat_suspended = 1
+ \ | endif
+ \ | elseif exists('b:markdown_suspend_autoformat_suspended')
+ \ | setlocal formatoptions+=a
+ \ | unlet b:markdown_suspend_autoformat_suspended
+ \ | endif
+
+" Undo all the above
+if exists('b:undo_ftplugin')
+ let b:undo_ftplugin = b:undo_ftplugin
+ \ . '|setlocal formatoptions<'
+endif