aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2018-06-04 16:55:41 +1200
committerTom Ryder <tom@sanctum.geek.nz>2018-06-04 16:55:41 +1200
commit8e6412db2013b11cece845d4e6cbe541a1875bdb (patch)
treee92219358c9eb86166854bdca69365597d6a9c95
parent4d4d9dc7d44038c32255197ceddc534f95275918 (diff)
downloaddotfiles-8e6412db2013b11cece845d4e6cbe541a1875bdb.tar.gz
Refactor suspend_autoformat.vim, add autoload
* Add a function to suspend autoformatting for the duration of pasting lines. * Factor the ftplugin's functions out to be autoloaded; this requires Vim >=7.0, but it already needed that. * Add Makefile infrastructure for new autoload directories/files.
-rw-r--r--Makefile10
-rw-r--r--vim/after/ftplugin/markdown/autoformat.vim33
-rw-r--r--vim/after/ftplugin/markdown/suspend_autoformat.vim34
-rw-r--r--vim/autoload/ftplugin/markdown/autoformat.vim39
4 files changed, 82 insertions, 34 deletions
diff --git a/Makefile b/Makefile
index fd08950d..345952cd 100644
--- a/Makefile
+++ b/Makefile
@@ -43,6 +43,7 @@
install-vim-after-indent \
install-vim-after-plugin \
install-vim-after-syntax \
+ install-vim-autoload \
install-vim-bundle \
install-vim-compiler \
install-vim-config \
@@ -491,6 +492,7 @@ VIMDIR = $(HOME)/.vim
VIMRC = $(HOME)/.vimrc
install-vim: install-vim-after \
+ install-vim-autoload \
install-vim-bundle \
install-vim-compiler \
install-vim-config \
@@ -522,6 +524,14 @@ install-vim-after-syntax:
mkdir -p $(VIMDIR)/after/syntax
cp -p -- vim/after/syntax/*.vim $(VIMDIR)/after/syntax
+install-vim-autoload:
+ find vim/autoload \
+ -type d -exec sh -c \
+ 'mkdir -- $(VIMDIR)/"$${1#vim/}"' _ {} \; \
+ -o \
+ -type f -exec sh -c \
+ 'cp -p -- "$$1" $(VIMDIR)/"$${1#vim/}"' _ {} \;
+
install-vim-bundle: install-vim-config
find vim/bundle/*/* \
-type d -exec sh -c \
diff --git a/vim/after/ftplugin/markdown/autoformat.vim b/vim/after/ftplugin/markdown/autoformat.vim
new file mode 100644
index 00000000..9a963fbb
--- /dev/null
+++ b/vim/after/ftplugin/markdown/autoformat.vim
@@ -0,0 +1,33 @@
+" 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_autoformat') || &compatible
+ finish
+endif
+if !has('autocmd') || v:version < 700
+ finish
+endif
+let b:did_ftplugin_markdown_autoformat = 1
+if exists('b:undo_ftplugin')
+ let b:undo_ftplugin = b:undo_ftplugin
+ \ . '|unlet b:did_ftplugin_markdown_autoformat'
+endif
+
+" Suspend auto-formatting when in a code block (four-space indent)
+autocmd BufWinEnter,CursorMoved,CursorMovedI,WinEnter
+ \ <buffer>
+ \ call ftplugin#markdown#autoformat#Line()
+
+" Suspend auto-format when pasting anything with a linebreak
+nnoremap <buffer> <silent>
+ \ p
+ \ :<C-u>call ftplugin#markdown#autoformat#PutBelow()<CR>
+nnoremap <buffer> <silent>
+ \ P
+ \ :<C-u>call ftplugin#markdown#autoformat#PutAbove()<CR>
+
+" Undo all the above
+if exists('b:undo_ftplugin')
+ let b:undo_ftplugin = b:undo_ftplugin
+ \ . '|setlocal formatoptions<'
+endif
diff --git a/vim/after/ftplugin/markdown/suspend_autoformat.vim b/vim/after/ftplugin/markdown/suspend_autoformat.vim
deleted file mode 100644
index b2de7828..00000000
--- a/vim/after/ftplugin/markdown/suspend_autoformat.vim
+++ /dev/null
@@ -1,34 +0,0 @@
-" 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
diff --git a/vim/autoload/ftplugin/markdown/autoformat.vim b/vim/autoload/ftplugin/markdown/autoformat.vim
new file mode 100644
index 00000000..1d01c58e
--- /dev/null
+++ b/vim/autoload/ftplugin/markdown/autoformat.vim
@@ -0,0 +1,39 @@
+" Autoload functions for after/ftplugin/markdown/autoformat.vim
+
+" Suspend auto-format when pasting anything with a linebreak
+function! ftplugin#markdown#autoformat#Line() abort
+ if getline('.') =~# '\m^ '
+ if &formatoptions =~# '\ma'
+ setlocal formatoptions-=a
+ let b:markdown_autoformat_suspended = 1
+ endif
+ elseif exists('b:markdown_autoformat_suspended')
+ setlocal formatoptions+=a
+ unlet b:markdown_autoformat_suspended
+ endif
+endfunction
+
+" Suspend auto-formatting when in a code block (four-space indent)
+function! ftplugin#markdown#autoformat#Put(above) abort
+ let l:suspended = 0
+ if &formatoptions =~# '\ma' && @" =~# '\m\n'
+ setlocal formatoptions-=a
+ let l:suspended = 1
+ endif
+ if a:above
+ normal! P
+ else
+ normal! p
+ endif
+ if l:suspended
+ setlocal formatoptions+=a
+ endif
+endfunction
+
+" Wrapper functions for #Put() above/below
+function! ftplugin#markdown#autoformat#PutAbove() abort
+ call ftplugin#markdown#autoformat#Put(1)
+endfunction
+function! ftplugin#markdown#autoformat#PutBelow() abort
+ call ftplugin#markdown#autoformat#Put(0)
+endfunction