From 45f4c46cfe7b1bb860c4449bb7a9bbfec1c17027 Mon Sep 17 00:00:00 2001 From: Reed Esau Date: Wed, 6 May 2015 00:56:49 -0600 Subject: [PATCH] Better behavior for #31, autoformat lists in Markdown For tpope's markdown syntax, restored standard autoformat behavior for unordered lists, which is generally pretty good. Ordered lists had annoying behavior for autoformat, so simply disabling autoformat if detected when insert is entered. This isn't ideal, but it's not clear what else can be done now. Fixed bug for PFormatToggle, where it wasn't properly disabling Vim's autoformat. Provided help in README for mapping key to toggle autoformat. --- README.markdown | 51 +++++++++++++------------------------------- autoload/pencil.vim | 8 ++++--- plugin/pencil.vim | 52 ++++++++++----------------------------------- 3 files changed, 31 insertions(+), 80 deletions(-) diff --git a/README.markdown b/README.markdown index 521dd4a..e1535a8 100644 --- a/README.markdown +++ b/README.markdown @@ -258,6 +258,13 @@ augroup END ...where by default, files of type `text` will use hard line endings, but with autoformat disabled. +Optionally, you can map a toggle to control autoformat in your `.vimrc`: + +```vim +noremap :PFormatToggle +inoremap :PFormatToggle +``` + ## Manual formatting Note that you need not rely on Vim’s autoformat exclusively and can @@ -432,46 +439,18 @@ _not_ in the blacklist. The current blacklist is: ```vim let g:pencil#autoformat_blacklist = [ - \ 'markdownCode', - \ 'markdownH[0-9]', - \ 'markdownUrl', - \ 'markdownIdDeclaration', - \ 'markdownLink', - \ 'markdownRule', - \ 'markdownHighlight[A-Za-z0-9]+', - \ 'markdownListMarker', - \ 'mkdCode', - \ 'mkdRule', - \ 'mkdDelimiter', - \ 'mkdLink', - \ 'mkdListItem', - \ 'mkdIndentCode', + \ 'markdown(Code|H[0-9]|Url|IdDeclaration|Link|Rule|Highlight[A-Za-z0-9]+)', + \ 'markdownOrderedListMarker', + \ 'mkd(Code|Rule|Delimiter|Link|ListItem|IndentCode)', \ 'htmlH[0-9]', - \ 'markdownFencedCodeBlock', - \ 'markdownInlineCode', + \ 'markdown(FencedCodeBlock|InlineCode)', \ 'mmdTable[A-Za-z0-9]*', \ 'txtCode', - \ 'rstCodeBlock', - \ 'rstDirective', - \ 'rstLiteralBlock', - \ 'rstSections', - \ 'texBeginEndName', - \ 'texDelimiter', - \ 'texDocType', - \ 'texInputFile', - \ 'texMath', - \ 'texRefZone', + \ 'rst(CodeBlock|Directive|LiteralBlock|Sections)', + \ 'tex(BeginEndName|Delimiter|DocType|InputFile|Math|RefZone|Title)', \ 'texSection$', - \ 'texTitle', - \ 'asciidocAttributeList', - \ 'asciidocListLabel', - \ 'asciidocLiteral', - \ 'asciidocSidebar', - \ 'asciidocSource', - \ 'asciidocSect[0-9]', - \ 'asciidoc[A-Za-z]*Block', - \ 'asciidoc[A-Za-z]*Macro', - \ 'asciidoc[A-Za-z]*Title', + \ 'asciidoc(AttributeList|ListLabel|Literal|SideBar|Source|Sect[0-9])', + \ 'asciidoc[A-Za-z]*(Block|Macro|Title)', \ ] ``` diff --git a/autoload/pencil.vim b/autoload/pencil.vim index dff8e3b..68fc8ed 100644 --- a/autoload/pencil.vim +++ b/autoload/pencil.vim @@ -121,8 +121,10 @@ fun! s:maybe_enable_autoformat() abort endfo en en - " disallow enable if start of previous line is in blacklist - if l:line > 1 + " disallow enable if start of previous line is in blacklist, + " (To avoid problem of autowrap screwing up adding a new item + " to a list.) + if l:okay_to_enable && l:line > 1 let l:prev_stack = synstack(l:line - 1, 1) for l:sid in l:prev_stack if len(l:sid) > 0 && @@ -152,7 +154,6 @@ fun! pencil#setAutoFormat(af) abort au InsertEnter call s:maybe_enable_autoformat() au InsertLeave set formatoptions-=a aug END - let b:last_autoformat = l:nu_af el sil! au! pencil_autoformat * if l:nu_af && !l:is_hard @@ -162,6 +163,7 @@ fun! pencil#setAutoFormat(af) abort return en en + let b:last_autoformat = l:nu_af endf " Create mappings for word processing diff --git a/plugin/pencil.vim b/plugin/pencil.vim index f67cad3..328149b 100644 --- a/plugin/pencil.vim +++ b/plugin/pencil.vim @@ -58,7 +58,7 @@ if !exists('g:pencil#autoformat') en if !exists('g:pencil#autoformat_blacklist') - " by default, pencil does NOT start autoformat if inside any of + " do not engage autoformat if cursor is inside any of " the following syntax groups " " markdown* (tpope/vim-markdown) @@ -68,46 +68,18 @@ if !exists('g:pencil#autoformat_blacklist') " txtCode (timcharper/textile.vim) " rst*,tex*,asciidoc* (syntax file shipped with vim) let g:pencil#autoformat_blacklist = [ - \ 'markdownCode', - \ 'markdownH[0-9]', - \ 'markdownUrl', - \ 'markdownIdDeclaration', - \ 'markdownLink', - \ 'markdownRule', - \ 'markdownHighlight[A-Za-z0-9]+', - \ 'markdownListMarker', - \ 'mkdCode', - \ 'mkdRule', - \ 'mkdDelimiter', - \ 'mkdLink', - \ 'mkdListItem', - \ 'mkdIndentCode', + \ 'markdown(Code|H[0-9]|Url|IdDeclaration|Link|Rule|Highlight[A-Za-z0-9]+)', + \ 'markdownOrderedListMarker', + \ 'mkd(Code|Rule|Delimiter|Link|ListItem|IndentCode)', \ 'htmlH[0-9]', - \ 'markdownFencedCodeBlock', - \ 'markdownInlineCode', + \ 'markdown(FencedCodeBlock|InlineCode)', \ 'mmdTable[A-Za-z0-9]*', \ 'txtCode', - \ 'rstCodeBlock', - \ 'rstDirective', - \ 'rstLiteralBlock', - \ 'rstSections', - \ 'texBeginEndName', - \ 'texDelimiter', - \ 'texDocType', - \ 'texInputFile', - \ 'texMath', - \ 'texRefZone', + \ 'rst(CodeBlock|Directive|LiteralBlock|Sections)', + \ 'tex(BeginEndName|Delimiter|DocType|InputFile|Math|RefZone|Title)', \ 'texSection$', - \ 'texTitle', - \ 'asciidocAttributeList', - \ 'asciidocListLabel', - \ 'asciidocLiteral', - \ 'asciidocSidebar', - \ 'asciidocSource', - \ 'asciidocSect[0-9]', - \ 'asciidoc[A-Za-z]*Block', - \ 'asciidoc[A-Za-z]*Macro', - \ 'asciidoc[A-Za-z]*Title', + \ 'asciidoc(AttributeList|ListLabel|Literal|SideBar|Source|Sect[0-9])', + \ 'asciidoc[A-Za-z]*(Block|Macro|Title)', \ ] en let g:pencil#autoformat_blacklist_re = @@ -119,10 +91,8 @@ if !exists('g:pencil#autoformat_inline_whitelist') " "'markdownCode' (tpope/vim-markdown) let g:pencil#autoformat_inline_whitelist = [ - \ 'markdownCode', - \ 'markdownLink', - \ 'asciidocAttributeRef', - \ 'asciidocMacro', + \ 'markdown(Code|Link)', + \ 'asciidoc(AttributeRef|Macro)', \ ] en let g:pencil#autoformat_inline_whitelist_re =