diff --git a/README.markdown b/README.markdown index 297ab48..b6d1780 100644 --- a/README.markdown +++ b/README.markdown @@ -122,20 +122,23 @@ to set the behavior for the current buffer: _This ‘autoformat’ feature affects *HardPencil* mode only._ -When in *HardPencil* mode, Vim’s autoformat feature will be enabled by -default while in Insert mode and can offer many of the same benefits as -soft line wrap. But autoformat will cause havoc when editing anything but -paragraphs of words, such as a code block or table. In these cases you -will need to disable it, at least temporarily, via a command: +When you are inserting text in *HardPencil* mode, Vim’s autoformat feature +will be enabled by default and can offer many of the same benefits as soft +line wrap. + +_Pencil_ will disable autoformat if inside a code block. If it doesn’t, or +in other cases where you need to disable autoformat, you can do so with +a command: * `AutoPencil` - enables autoformat * `ManualPencil` - disables autoformat * `ShiftPencil` - toggle to enable if disabled, etc. -Or optionally map the toggle to a key in your `.vimrc`: +Or optionally map the toggle command to a key of your choice in your +`.vimrc`: ```vim -nnoremap pp :ShiftPencil +nnoremap p :ShiftPencil ``` To set the default behavior, add to your `.vimrc`: @@ -157,6 +160,9 @@ augroup END ...where by default, files of type `text` will use hard line endings, but with autoformat disabled. +Advanced users will want to check out `g:pencil#autoformat_exclude` to set +highlight groups for which autoformat will not be enabled. + ### Manual formatting Note that you need not rely on autoformat exclusively and can manually diff --git a/autoload/pencil.vim b/autoload/pencil.vim index 766d7d0..a140982 100644 --- a/autoload/pencil.vim +++ b/autoload/pencil.vim @@ -53,6 +53,27 @@ fun! s:imap(preserve_completion, key, icmd) en endf +fun! s:enable_autoformat(mode) + " mode=1 InsertEnter + " mode=0 InsertLeave + if a:mode + " don't enable autoformat if in a code block (or table) + let l:okay_to_enable = 1 + for l:sid in synstack(line('.'), col('.')) + if match(synIDattr(l:sid, 'name'), + \ g:pencil#autoformat_exclude_re) == -1 + let l:okay_to_enable = 0 + break + en + endfo + if l:okay_to_enable + set formatoptions+=a + en + el + set formatoptions-=a + en +endf + fun! pencil#setAutoFormat(mode) " 1=auto, 0=manual, -1=toggle if !exists('b:last_autoformat') @@ -61,8 +82,8 @@ fun! pencil#setAutoFormat(mode) let b:last_autoformat = a:mode == -1 ? !b:last_autoformat : a:mode if b:last_autoformat aug pencil_autoformat - au InsertEnter set formatoptions+=a - au InsertLeave set formatoptions-=a + au InsertEnter call s:enable_autoformat(1) + au InsertLeave call s:enable_autoformat(0) aug END el sil! au! pencil_autoformat * diff --git a/plugin/pencil.vim b/plugin/pencil.vim index c678db0..0826374 100644 --- a/plugin/pencil.vim +++ b/plugin/pencil.vim @@ -30,6 +30,20 @@ if !exists('g:pencil#autoformat') let g:pencil#autoformat = 1 en +if !exists('g:pencil#autoformat_exclude') + " by default, pencil does NOT start autoformat if inside any of + " the following syntax groups + let g:pencil#autoformat_exclude = [ + \ 'markdownCode', + \ 'markdownHighlight[A-Za-z0-9]+', + \ 'mkdCode', + \ 'mkdIndentCode', + \ 'txtCode', + \ ] +en +let g:pencil#autoformat_exclude_re = + \ '(' . join(g:pencil#autoformat_exclude, '|') . ')' + if !exists('g:pencil#joinspaces') " by default, only one space after full stop (.) let g:pencil#joinspaces = 0