Improve autoformat support for asciidoc #28

For all document types, autoformat blacklisting now sneaks a look a the start of the previous line, to allow for non-heading text to immediately follow, without autoformat being engaged in a way that will cause the heading to be captured in the auto formatted text.
This commit is contained in:
Reed Esau
2015-03-15 16:04:30 -06:00
parent 320c7fc284
commit c465e30c19
3 changed files with 80 additions and 60 deletions

View File

@@ -45,7 +45,8 @@ Several reasons have been offered:
to reach for mouse, track pad, or arrow keys to reach for mouse, track pad, or arrow keys
* Minimal chording, with many mnemonic-friendly commands * Minimal chording, with many mnemonic-friendly commands
* Sophisticated capabilities for navigating and manipulating text * Sophisticated capabilities for navigating and manipulating text
* Highly configurable to suit your needs, with many great plugins available * Highly configurable, enabling you to build a workflow that suits your
needs, with many great plugins available
* No proprietary format lock-in * No proprietary format lock-in
But while such reasons might be sound, they remain scant justification to But while such reasons might be sound, they remain scant justification to
@@ -174,10 +175,8 @@ augroup pencil
augroup END augroup END
``` ```
For other prose-oriented plugins, consult the [See also](#see-also) For a list of other prose-oriented plugins, consult the [See
section below. also](#see-also) section below.
For more details, see the advanced initialization section below.
## Hard line breaks or soft line wrap? ## Hard line breaks or soft line wrap?
@@ -210,8 +209,8 @@ if auto-detect might suggest soft line wrap.
You can enable, disable, and toggle _pencil_ as a command: You can enable, disable, and toggle _pencil_ as a command:
* `Pencil` - initialize _pencil_ with auto-detect for the current buffer * `Pencil` - initialize _pencil_ with auto-detect for the current buffer
* `PencilOff` (or `NoPencil`) - removes navigation mappings and restores buffer to global settings * `NoPencil` (or `PencilOff`) - removes navigation mappings and restores buffer to global settings
* `PencilToggle` (or `TogglePencil`) - if on, turns off; if off, initializes with auto-detect * `TogglePencil` (or `PencilToggle`) - if on, turns off; if off, initializes with auto-detect
Because auto-detect might not work as intended, you can invoke a command Because auto-detect might not work as intended, you can invoke a command
to set the behavior for the current buffer: to set the behavior for the current buffer:
@@ -261,8 +260,8 @@ with autoformat disabled.
## Manual formatting ## Manual formatting
Note that you need not rely on autoformat exclusively and can manually Note that you need not rely on Vims autoformat exclusively and can
reformat paragraphs with standard Vim commands: manually reformat paragraphs with standard Vim commands:
* `gqip` or `gwip` - format current paragraph * `gqip` or `gwip` - format current paragraph
* `vipJ` - unformat (i.e., join all lines with hard line breaks) in current paragraph * `vipJ` - unformat (i.e., join all lines with hard line breaks) in current paragraph
@@ -418,11 +417,11 @@ statements into a function.
_The autoformat feature affects *HardPencil* (hard line break) mode _The autoformat feature affects *HardPencil* (hard line break) mode
only._ only._
When editing formatted text, such as a table or code block, autoformat When editing formatted text, such as a table or code block, Vims
will wreak havoc with the formatting. In these cases you will want to autoformat will wreak havoc with the formatting. In these cases you will
temporarily deactivate autoformat, such as with the `PFormatOff` or want to temporarily deactivate autoformat, such as with the `PFormatOff`
`PFormatToggle` commands described above. However, in most cases, you wont or `PFormatToggle` commands described above. However, in most cases, you
need to do this. wont need to do this.
_pencil_ will detect the syntax highlight group at the cursor position to _pencil_ will detect the syntax highlight group at the cursor position to
determine whether or not autoformat should be activated. determine whether or not autoformat should be activated.
@@ -432,7 +431,7 @@ mode provided that the syntax highlighting group at the cursor position is
_not_ in the blacklist. The current blacklist is: _not_ in the blacklist. The current blacklist is:
```vim ```vim
let g:pencil#autoformat_blacklist = [ let g:pencil#autoformat_blacklist = [
\ 'markdownCode', \ 'markdownCode',
\ 'markdownH[0-9]', \ 'markdownH[0-9]',
\ 'markdownUrl', \ 'markdownUrl',
@@ -444,6 +443,7 @@ let g:pencil#autoformat_blacklist = [
\ 'mkdRule', \ 'mkdRule',
\ 'mkdDelimiter', \ 'mkdDelimiter',
\ 'mkdLink', \ 'mkdLink',
\ 'mkdListItem',
\ 'mkdIndentCode', \ 'mkdIndentCode',
\ 'htmlH[0-9]', \ 'htmlH[0-9]',
\ 'markdownFencedCodeBlock', \ 'markdownFencedCodeBlock',
@@ -462,20 +462,21 @@ let g:pencil#autoformat_blacklist = [
\ 'texRefZone', \ 'texRefZone',
\ 'texSection$', \ 'texSection$',
\ 'texTitle', \ 'texTitle',
\ 'asciidocAttribute', \ 'asciidocAttributeList',
\ 'asciidocList', \ 'asciidocListLabel',
\ 'asciidocLiteral', \ 'asciidocLiteral',
\ 'asciidocSidebar',
\ 'asciidocSource',
\ 'asciidocSect[0-9]',
\ 'asciidoc[A-Za-z]*Block', \ 'asciidoc[A-Za-z]*Block',
\ 'asciidoc[A-Za-z]*Macro', \ 'asciidoc[A-Za-z]*Macro',
\ 'asciidoc[A-Za-z]*Title', \ 'asciidoc[A-Za-z]*Title',
\ ] \ ]
``` ```
_WARNING: the implementation of this blacklist may be changing in the _WARNING: the implementation of this blacklist will be changing in the
future._ future._
Theres also a whitelist to override the blacklist. See code for details.
You can override this in your `.vimrc`. Additions to defaults with good You can override this in your `.vimrc`. Additions to defaults with good
use cases are welcome. use cases are welcome.

View File

@@ -44,7 +44,7 @@ fun! s:detect_wrap_mode() abort
return s:WRAP_MODE_DEFAULT return s:WRAP_MODE_DEFAULT
endf endf
fun! s:imap(preserve_completion, key, icmd) fun! s:imap(preserve_completion, key, icmd) abort
if a:preserve_completion if a:preserve_completion
exe ":ino <buffer> <silent> <expr> " . a:key . " pumvisible() ? \"" . a:key . "\" : \"" . a:icmd . "\"" exe ":ino <buffer> <silent> <expr> " . a:key . " pumvisible() ? \"" . a:key . "\" : \"" . a:icmd . "\""
el el
@@ -52,7 +52,7 @@ fun! s:imap(preserve_completion, key, icmd)
en en
endf endf
fun! s:maybe_enable_autoformat() fun! s:maybe_enable_autoformat() abort
" don't enable autoformat if in a code block or table " don't enable autoformat if in a code block or table
let l:okay_to_enable = 1 let l:okay_to_enable = 1
let l:line = line('.') let l:line = line('.')
@@ -92,6 +92,10 @@ fun! s:maybe_enable_autoformat()
if match(synIDattr(l:sid, 'name'), if match(synIDattr(l:sid, 'name'),
\ g:pencil#autoformat_blacklist_re) >= 0 \ g:pencil#autoformat_blacklist_re) >= 0
let l:okay_to_enable = 0 let l:okay_to_enable = 0
"echohl WarningMsg
"echo 'hit blacklist line=' . l:line . ' col=' . l:col .
" \ ' name=' . synIDattr(l:sid, 'name')
"echohl NONE
break break
en en
endfo endfo
@@ -117,12 +121,24 @@ fun! s:maybe_enable_autoformat()
endfo endfo
en en
en en
" disallow enable if start of previous line is in blacklist
if l:line > 1
let l:prev_stack = synstack(l:line - 1, 1)
for l:sid in l:prev_stack
if len(l:sid) > 0 &&
\ match(synIDattr(l:sid, 'name'),
\ g:pencil#autoformat_blacklist_re) >= 0
let l:okay_to_enable = 0
break
en
endfo
en
if l:okay_to_enable if l:okay_to_enable
set formatoptions+=a set formatoptions+=a
en en
endf endf
fun! pencil#setAutoFormat(af) fun! pencil#setAutoFormat(af) abort
" 1=auto, 0=manual, -1=toggle " 1=auto, 0=manual, -1=toggle
if !exists('b:last_autoformat') if !exists('b:last_autoformat')
let b:last_autoformat = 0 let b:last_autoformat = 0

View File

@@ -98,9 +98,12 @@ if !exists('g:pencil#autoformat_blacklist')
\ 'texRefZone', \ 'texRefZone',
\ 'texSection$', \ 'texSection$',
\ 'texTitle', \ 'texTitle',
\ 'asciidocAttribute', \ 'asciidocAttributeList',
\ 'asciidocList', \ 'asciidocListLabel',
\ 'asciidocLiteral', \ 'asciidocLiteral',
\ 'asciidocSidebar',
\ 'asciidocSource',
\ 'asciidocSect[0-9]',
\ 'asciidoc[A-Za-z]*Block', \ 'asciidoc[A-Za-z]*Block',
\ 'asciidoc[A-Za-z]*Macro', \ 'asciidoc[A-Za-z]*Macro',
\ 'asciidoc[A-Za-z]*Title', \ 'asciidoc[A-Za-z]*Title',
@@ -117,8 +120,8 @@ if !exists('g:pencil#autoformat_inline_whitelist')
let g:pencil#autoformat_inline_whitelist = [ let g:pencil#autoformat_inline_whitelist = [
\ 'markdownCode', \ 'markdownCode',
\ 'markdownLink', \ 'markdownLink',
\ 'asciidocAttributeRef',
\ 'asciidocMacro', \ 'asciidocMacro',
\ 'asciidocQuotedMonospaced',
\ ] \ ]
en en
let g:pencil#autoformat_inline_whitelist_re = let g:pencil#autoformat_inline_whitelist_re =