mirror of
https://github.com/preservim/vim-pencil.git
synced 2025-11-12 11:53:47 -05:00
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:
109
README.markdown
109
README.markdown
@@ -45,7 +45,8 @@ Several reasons have been offered:
|
||||
to reach for mouse, track pad, or arrow keys
|
||||
* Minimal chording, with many mnemonic-friendly commands
|
||||
* 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
|
||||
|
||||
But while such reasons might be sound, they remain scant justification to
|
||||
@@ -174,10 +175,8 @@ augroup pencil
|
||||
augroup END
|
||||
```
|
||||
|
||||
For other prose-oriented plugins, consult the [See also](#see-also)
|
||||
section below.
|
||||
|
||||
For more details, see the advanced initialization section below.
|
||||
For a list of other prose-oriented plugins, consult the [See
|
||||
also](#see-also) section below.
|
||||
|
||||
## 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:
|
||||
|
||||
* `Pencil` - initialize _pencil_ with auto-detect for the current buffer
|
||||
* `PencilOff` (or `NoPencil`) - removes navigation mappings and restores buffer to global settings
|
||||
* `PencilToggle` (or `TogglePencil`) - if on, turns off; if off, initializes with auto-detect
|
||||
* `NoPencil` (or `PencilOff`) - removes navigation mappings and restores buffer to global settings
|
||||
* `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
|
||||
to set the behavior for the current buffer:
|
||||
@@ -261,8 +260,8 @@ with autoformat disabled.
|
||||
|
||||
## Manual formatting
|
||||
|
||||
Note that you need not rely on autoformat exclusively and can manually
|
||||
reformat paragraphs with standard Vim commands:
|
||||
Note that you need not rely on Vim’s autoformat exclusively and can
|
||||
manually reformat paragraphs with standard Vim commands:
|
||||
|
||||
* `gqip` or `gwip` - format 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
|
||||
only._
|
||||
|
||||
When editing formatted text, such as a table or code block, autoformat
|
||||
will wreak havoc with the formatting. In these cases you will want to
|
||||
temporarily deactivate autoformat, such as with the `PFormatOff` or
|
||||
`PFormatToggle` commands described above. However, in most cases, you won’t
|
||||
need to do this.
|
||||
When editing formatted text, such as a table or code block, Vim’s
|
||||
autoformat will wreak havoc with the formatting. In these cases you will
|
||||
want to temporarily deactivate autoformat, such as with the `PFormatOff`
|
||||
or `PFormatToggle` commands described above. However, in most cases, you
|
||||
won’t need to do this.
|
||||
|
||||
_pencil_ will detect the syntax highlight group at the cursor position to
|
||||
determine whether or not autoformat should be activated.
|
||||
@@ -432,50 +431,52 @@ mode provided that the syntax highlighting group at the cursor position is
|
||||
_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]+',
|
||||
\ 'mkdCode',
|
||||
\ 'mkdRule',
|
||||
\ 'mkdDelimiter',
|
||||
\ 'mkdLink',
|
||||
\ 'mkdIndentCode',
|
||||
\ 'htmlH[0-9]',
|
||||
\ 'markdownFencedCodeBlock',
|
||||
\ 'markdownInlineCode',
|
||||
\ 'mmdTable[A-Za-z0-9]*',
|
||||
\ 'txtCode',
|
||||
\ 'rstCodeBlock',
|
||||
\ 'rstDirective',
|
||||
\ 'rstLiteralBlock',
|
||||
\ 'rstSections',
|
||||
\ 'texBeginEndName',
|
||||
\ 'texDelimiter',
|
||||
\ 'texDocType',
|
||||
\ 'texInputFile',
|
||||
\ 'texMath',
|
||||
\ 'texRefZone',
|
||||
\ 'texSection$',
|
||||
\ 'texTitle',
|
||||
\ 'asciidocAttribute',
|
||||
\ 'asciidocList',
|
||||
\ 'asciidocLiteral',
|
||||
\ 'asciidoc[A-Za-z]*Block',
|
||||
\ 'asciidoc[A-Za-z]*Macro',
|
||||
\ 'asciidoc[A-Za-z]*Title',
|
||||
\ ]
|
||||
let g:pencil#autoformat_blacklist = [
|
||||
\ 'markdownCode',
|
||||
\ 'markdownH[0-9]',
|
||||
\ 'markdownUrl',
|
||||
\ 'markdownIdDeclaration',
|
||||
\ 'markdownLink',
|
||||
\ 'markdownRule',
|
||||
\ 'markdownHighlight[A-Za-z0-9]+',
|
||||
\ 'mkdCode',
|
||||
\ 'mkdRule',
|
||||
\ 'mkdDelimiter',
|
||||
\ 'mkdLink',
|
||||
\ 'mkdListItem',
|
||||
\ 'mkdIndentCode',
|
||||
\ 'htmlH[0-9]',
|
||||
\ 'markdownFencedCodeBlock',
|
||||
\ 'markdownInlineCode',
|
||||
\ 'mmdTable[A-Za-z0-9]*',
|
||||
\ 'txtCode',
|
||||
\ 'rstCodeBlock',
|
||||
\ 'rstDirective',
|
||||
\ 'rstLiteralBlock',
|
||||
\ 'rstSections',
|
||||
\ 'texBeginEndName',
|
||||
\ 'texDelimiter',
|
||||
\ 'texDocType',
|
||||
\ 'texInputFile',
|
||||
\ 'texMath',
|
||||
\ 'texRefZone',
|
||||
\ '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',
|
||||
\ ]
|
||||
```
|
||||
|
||||
_WARNING: the implementation of this blacklist may be changing in the
|
||||
_WARNING: the implementation of this blacklist will be changing in the
|
||||
future._
|
||||
|
||||
There’s also a whitelist to override the blacklist. See code for details.
|
||||
|
||||
You can override this in your `.vimrc`. Additions to defaults with good
|
||||
use cases are welcome.
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ fun! s:detect_wrap_mode() abort
|
||||
return s:WRAP_MODE_DEFAULT
|
||||
endf
|
||||
|
||||
fun! s:imap(preserve_completion, key, icmd)
|
||||
fun! s:imap(preserve_completion, key, icmd) abort
|
||||
if a:preserve_completion
|
||||
exe ":ino <buffer> <silent> <expr> " . a:key . " pumvisible() ? \"" . a:key . "\" : \"" . a:icmd . "\""
|
||||
el
|
||||
@@ -52,7 +52,7 @@ fun! s:imap(preserve_completion, key, icmd)
|
||||
en
|
||||
endf
|
||||
|
||||
fun! s:maybe_enable_autoformat()
|
||||
fun! s:maybe_enable_autoformat() abort
|
||||
" don't enable autoformat if in a code block or table
|
||||
let l:okay_to_enable = 1
|
||||
let l:line = line('.')
|
||||
@@ -92,6 +92,10 @@ fun! s:maybe_enable_autoformat()
|
||||
if match(synIDattr(l:sid, 'name'),
|
||||
\ g:pencil#autoformat_blacklist_re) >= 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
|
||||
en
|
||||
endfo
|
||||
@@ -117,12 +121,24 @@ fun! s:maybe_enable_autoformat()
|
||||
endfo
|
||||
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
|
||||
set formatoptions+=a
|
||||
en
|
||||
endf
|
||||
|
||||
fun! pencil#setAutoFormat(af)
|
||||
fun! pencil#setAutoFormat(af) abort
|
||||
" 1=auto, 0=manual, -1=toggle
|
||||
if !exists('b:last_autoformat')
|
||||
let b:last_autoformat = 0
|
||||
|
||||
@@ -98,9 +98,12 @@ if !exists('g:pencil#autoformat_blacklist')
|
||||
\ 'texRefZone',
|
||||
\ 'texSection$',
|
||||
\ 'texTitle',
|
||||
\ 'asciidocAttribute',
|
||||
\ 'asciidocList',
|
||||
\ 'asciidocAttributeList',
|
||||
\ 'asciidocListLabel',
|
||||
\ 'asciidocLiteral',
|
||||
\ 'asciidocSidebar',
|
||||
\ 'asciidocSource',
|
||||
\ 'asciidocSect[0-9]',
|
||||
\ 'asciidoc[A-Za-z]*Block',
|
||||
\ 'asciidoc[A-Za-z]*Macro',
|
||||
\ 'asciidoc[A-Za-z]*Title',
|
||||
@@ -117,8 +120,8 @@ if !exists('g:pencil#autoformat_inline_whitelist')
|
||||
let g:pencil#autoformat_inline_whitelist = [
|
||||
\ 'markdownCode',
|
||||
\ 'markdownLink',
|
||||
\ 'asciidocAttributeRef',
|
||||
\ 'asciidocMacro',
|
||||
\ 'asciidocQuotedMonospaced',
|
||||
\ ]
|
||||
en
|
||||
let g:pencil#autoformat_inline_whitelist_re =
|
||||
|
||||
Reference in New Issue
Block a user