mirror of
https://github.com/preservim/vim-pencil.git
synced 2025-11-19 23:33:39 -05:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dd8321a2d5 | ||
|
|
f89a3ac998 | ||
|
|
290b0872d0 | ||
|
|
632ae63c2c | ||
|
|
5f819bfec0 |
@@ -23,7 +23,7 @@ smooth the path to writing prose.
|
||||
deletion via line `<C-U>` and word `<C-W>`
|
||||
* When using hard line breaks, _pencil_ enables Vim’s autoformat while
|
||||
inserting text, except for tables and code blocks where you won’t want
|
||||
it
|
||||
it (**NEW: blacklisting now based on filetype**)
|
||||
* Buffer-scoped configuration (with a few minor exceptions, _pencil_ preserves
|
||||
your global settings)
|
||||
* Support for Vim’s Conceal feature to hide markup defined by Syntax plugins
|
||||
@@ -101,8 +101,6 @@ Plugin 'reedes/vim-pencil'
|
||||
:PluginInstall
|
||||
```
|
||||
|
||||
For Vundle version < 0.10.2, replace `Plugin` with `Bundle` above.
|
||||
|
||||
#### Plug
|
||||
|
||||
Add to your `.vimrc` and save:
|
||||
@@ -258,7 +256,7 @@ 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`:
|
||||
Optionally, you can map a key in your `.vimrc` to toggle Vim's autoformat:
|
||||
|
||||
```vim
|
||||
noremap <buffer> <silent> <F7> :<C-u>PFormatToggle<cr>
|
||||
@@ -375,12 +373,22 @@ or if using [bling/vim-airline][va]:
|
||||
let g:airline_section_x = '%{PencilMode()}'
|
||||
```
|
||||
|
||||
If you don’t like the default indicators, you can specify different ones:
|
||||
The default indicators now include ‘auto’ for when Vim’s autoformat is
|
||||
activated in hard line break mode.
|
||||
|
||||
```vim
|
||||
let g:pencil#mode_indicators = {'hard': '␍', 'soft': '⤸', 'off': '',}
|
||||
let g:pencil#mode_indicators = {'hard': 'H', 'auto': 'A', 'soft': 'S', 'off': '',}
|
||||
```
|
||||
|
||||
If Unicode is detected, the default indicators are:
|
||||
|
||||
```vim
|
||||
let g:pencil#mode_indicators = {'hard': '␍', 'auto': 'ª', 'soft': '⤸', 'off': '',}
|
||||
```
|
||||
|
||||
If you don’t like the default indicators, you can specify your own in
|
||||
your `.vimrc`.
|
||||
|
||||
Note that `PencilMode()` will return blank for buffers in which _pencil_
|
||||
has not been initialized.
|
||||
|
||||
@@ -419,7 +427,7 @@ augroup END
|
||||
Alternatives include `after/ftplugin` modules as well as refactoring initialization
|
||||
statements into a function.
|
||||
|
||||
### Autoformat blacklisting
|
||||
### Autoformat blacklisting (and whitelisting)
|
||||
|
||||
_The ‘autoformat’ feature affects *HardPencil* (hard line break) mode
|
||||
only._
|
||||
@@ -433,32 +441,40 @@ 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.
|
||||
|
||||
If autoformat is on, it will be activated at the time you enter Insert
|
||||
mode provided that the syntax highlighting group at the cursor position is
|
||||
_not_ in the blacklist. The current blacklist is:
|
||||
If you haven’t explicitly disabled autoformat, it will be activated at
|
||||
the time you enter Insert mode provided that the syntax highlighting
|
||||
group at the cursor position is _not_ in the blacklist.
|
||||
|
||||
Blacklists are now declared by file type. The default blacklists (and
|
||||
whitelists) are declared in the `plugin/pencil.vim` module. Here’s an
|
||||
excerpt showing the configuration for the ‘markdown’ file type:
|
||||
|
||||
```vim
|
||||
let g:pencil#autoformat_blacklist = [
|
||||
\ '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]',
|
||||
\ 'markdown(FencedCodeBlock|InlineCode)',
|
||||
\ 'mmdTable[A-Za-z0-9]*',
|
||||
\ 'txtCode',
|
||||
\ 'rst(CodeBlock|Directive|LiteralBlock|Sections)',
|
||||
\ 'tex(BeginEndName|Delimiter|DocType|InputFile|Math|RefZone|Title)',
|
||||
\ 'texSection$',
|
||||
\ 'asciidoc(AttributeList|ListLabel|Literal|SideBar|Source|Sect[0-9])',
|
||||
\ 'asciidoc[A-Za-z]*(Block|Macro|Title)',
|
||||
\ ]
|
||||
let g:pencil#autoformat_config = {
|
||||
\ 'markdown': {
|
||||
\ 'black': [
|
||||
\ 'htmlH[0-9]',
|
||||
\ 'markdown(Code|H[0-9]|Url|IdDeclaration|Link|Rule|Highlight[A-Za-z0-9]+)',
|
||||
\ 'markdown(FencedCodeBlock|InlineCode)',
|
||||
\ 'mkd(Code|Rule|Delimiter|Link|ListItem|IndentCode)',
|
||||
\ 'mmdTable[A-Za-z0-9]*',
|
||||
\ ],
|
||||
\ 'white': [
|
||||
\ 'markdown(Code|Link)',
|
||||
\ ],
|
||||
\ },
|
||||
[snip]
|
||||
\ }
|
||||
```
|
||||
|
||||
_WARNING: the implementation of this blacklist will be changing in the
|
||||
future._
|
||||
For example, if editing a file of type ‘markdown’ and you enter Insert
|
||||
mode from inside a `markdownFencedCodeBlock` highlight group, then Vim’s
|
||||
autoformat will _not_ be activated.
|
||||
|
||||
You can override this in your `.vimrc`. Additions to defaults with good
|
||||
use cases are welcome.
|
||||
The whitelist will override the blacklist and allow Vim’s autoformat to
|
||||
be activated if text that would normally be blacklisted doesn’t dominate
|
||||
the entire line. This allows autoformat to work with `inline` code and
|
||||
links.
|
||||
|
||||
### Auto-detecting wrap mode
|
||||
|
||||
|
||||
@@ -53,7 +53,19 @@ fun! s:imap(preserve_completion, key, icmd) abort
|
||||
endf
|
||||
|
||||
fun! s:maybe_enable_autoformat() abort
|
||||
" don't enable autoformat if in a code block or table
|
||||
" don't enable autoformat if in a blacklisted code block or table,
|
||||
" allowing for reprieve via whitelist in certain cases
|
||||
|
||||
let l:ft = get(g:pencil#autoformat_aliases, &ft, &ft)
|
||||
let l:af_cfg = get(g:pencil#autoformat_config, l:ft, {})
|
||||
let l:black = get(l:af_cfg, 'black', [])
|
||||
let l:white = get(l:af_cfg, 'white', [])
|
||||
let l:has_black_re = len(l:black) > 0
|
||||
let l:has_white_re = len(l:white) > 0
|
||||
let l:black_re = l:has_black_re ? '\v(' . join( l:black, '|') . ')' : ''
|
||||
let l:white_re = l:has_white_re ? '\v(' . join( l:white, '|') . ')' : ''
|
||||
let l:enforce_previous_line = get(l:af_cfg, 'enforce-previous-line', 0)
|
||||
|
||||
let l:okay_to_enable = 1
|
||||
let l:line = line('.')
|
||||
let l:col = col('.')
|
||||
@@ -88,21 +100,21 @@ fun! s:maybe_enable_autoformat() abort
|
||||
endw
|
||||
en
|
||||
" enforce blacklist by scanning for syntax matches
|
||||
for l:sid in l:stack
|
||||
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
|
||||
" enforce whitelist by detecting inline `markup` for
|
||||
" which we DO want autoformat to be enabled (e.g.,
|
||||
" tpope's markdownCode)
|
||||
if !l:okay_to_enable
|
||||
if l:has_black_re
|
||||
for l:sid in l:stack
|
||||
if match(synIDattr(l:sid, 'name'), l:black_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
|
||||
en
|
||||
" enforce whitelist by detecting inline `markup` for which we DO want
|
||||
" autoformat to be enabled (e.g., tpope's markdownCode)
|
||||
if l:has_white_re && !l:okay_to_enable
|
||||
" one final check for an empty stack at the start and end of line,
|
||||
" either of which greenlights a whitelist check
|
||||
if !l:found_empty
|
||||
@@ -113,8 +125,7 @@ fun! s:maybe_enable_autoformat() abort
|
||||
en
|
||||
if l:found_empty
|
||||
for l:sid in l:stack
|
||||
if match(synIDattr(l:sid, 'name'),
|
||||
\ g:pencil#autoformat_inline_whitelist_re) >= 0
|
||||
if match(synIDattr(l:sid, 'name'), l:white_re) >= 0
|
||||
let l:okay_to_enable = 1
|
||||
break
|
||||
en
|
||||
@@ -122,14 +133,11 @@ fun! s:maybe_enable_autoformat() abort
|
||||
en
|
||||
en
|
||||
" 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
|
||||
if l:has_black_re && l:enforce_previous_line && 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 &&
|
||||
\ match(synIDattr(l:sid, 'name'),
|
||||
\ g:pencil#autoformat_blacklist_re) >= 0
|
||||
\ match(synIDattr(l:sid, 'name'), l:black_re) >= 0
|
||||
let l:okay_to_enable = 0
|
||||
break
|
||||
en
|
||||
|
||||
@@ -31,7 +31,11 @@ fun! PencilMode()
|
||||
if b:pencil_wrap_mode ==# s:WRAP_MODE_SOFT
|
||||
return get(g:pencil#mode_indicators, 'soft', 'S')
|
||||
elsei b:pencil_wrap_mode ==# s:WRAP_MODE_HARD
|
||||
return get(g:pencil#mode_indicators, 'hard', 'H')
|
||||
if &fo =~ 'a'
|
||||
return get(g:pencil#mode_indicators, 'auto', 'A')
|
||||
el
|
||||
return get(g:pencil#mode_indicators, 'hard', 'H')
|
||||
en
|
||||
el
|
||||
return get(g:pencil#mode_indicators, 'off', '')
|
||||
en
|
||||
@@ -57,9 +61,9 @@ if !exists('g:pencil#autoformat')
|
||||
let g:pencil#autoformat = 1
|
||||
en
|
||||
|
||||
if !exists('g:pencil#autoformat_blacklist')
|
||||
" do not engage autoformat if cursor is inside any of
|
||||
" the following syntax groups
|
||||
if !exists('g:pencil#autoformat_config')
|
||||
" Do not activate autoformat if entering Insert mode when
|
||||
" the cursor is inside any of the following syntax groups.
|
||||
"
|
||||
" markdown* (tpope/vim-markdown)
|
||||
" mkd*, htmlH[0-9] (plasticboy/vim-markdown)
|
||||
@@ -67,36 +71,56 @@ if !exists('g:pencil#autoformat_blacklist')
|
||||
" mmdTable[A-Za-z0-9]* (mattly/vim-markdown-enhancements)
|
||||
" txtCode (timcharper/textile.vim)
|
||||
" rst*,tex*,asciidoc* (syntax file shipped with vim)
|
||||
let g:pencil#autoformat_blacklist = [
|
||||
\ '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]',
|
||||
\ 'markdown(FencedCodeBlock|InlineCode)',
|
||||
\ 'mmdTable[A-Za-z0-9]*',
|
||||
\ 'txtCode',
|
||||
\ 'rst(CodeBlock|Directive|LiteralBlock|Sections)',
|
||||
\ 'tex(BeginEndName|Delimiter|DocType|InputFile|Math|RefZone|Title)',
|
||||
\ 'texSection$',
|
||||
\ 'asciidoc(AttributeList|ListLabel|Literal|SideBar|Source|Sect[0-9])',
|
||||
\ 'asciidoc[A-Za-z]*(Block|Macro|Title)',
|
||||
\ ]
|
||||
let g:pencil#autoformat_config = {
|
||||
\ 'markdown': {
|
||||
\ 'black': [
|
||||
\ 'htmlH[0-9]',
|
||||
\ 'markdown(Code|H[0-9]|Url|IdDeclaration|Link|Rule|Highlight[A-Za-z0-9]+)',
|
||||
\ 'markdown(FencedCodeBlock|InlineCode)',
|
||||
\ 'mkd(Code|Rule|Delimiter|Link|ListItem|IndentCode)',
|
||||
\ 'mmdTable[A-Za-z0-9]*',
|
||||
\ ],
|
||||
\ 'white': [
|
||||
\ 'markdown(Code|Link)',
|
||||
\ ],
|
||||
\ },
|
||||
\ 'asciidoc': {
|
||||
\ 'black': [
|
||||
\ 'asciidoc(AttributeList|AttributeEntry|ListLabel|Literal|SideBar|Source|Sect[0-9])',
|
||||
\ 'asciidoc[A-Za-z]*(Block|Macro|Title)',
|
||||
\ ],
|
||||
\ 'white': [
|
||||
\ 'asciidoc(AttributeRef|Macro)',
|
||||
\ ],
|
||||
\ 'enforce-previous-line': 1,
|
||||
\ },
|
||||
\ 'rst': {
|
||||
\ 'black': [
|
||||
\ 'rst(CodeBlock|Directive|LiteralBlock|Sections)',
|
||||
\ ],
|
||||
\ },
|
||||
\ 'tex': {
|
||||
\ 'black': [
|
||||
\ 'tex(BeginEndName|Delimiter|DocType|InputFile|Math|RefZone|Title)',
|
||||
\ 'texSection$',
|
||||
\ ],
|
||||
\ 'enforce-previous-line': 1,
|
||||
\ },
|
||||
\ 'textile': {
|
||||
\ 'black': [
|
||||
\ 'txtCode',
|
||||
\ ],
|
||||
\ },
|
||||
\ }
|
||||
en
|
||||
let g:pencil#autoformat_blacklist_re =
|
||||
\ '\v(' . join(g:pencil#autoformat_blacklist, '|') . ')'
|
||||
|
||||
if !exists('g:pencil#autoformat_inline_whitelist')
|
||||
" grant autoformat a reprieve (allow enabling) if any of
|
||||
" following syntax groups doesn't dominate the whole line
|
||||
"
|
||||
"'markdownCode' (tpope/vim-markdown)
|
||||
let g:pencil#autoformat_inline_whitelist = [
|
||||
\ 'markdown(Code|Link)',
|
||||
\ 'asciidoc(AttributeRef|Macro)',
|
||||
\ ]
|
||||
if !exists('g:pencil#autoformat_aliases')
|
||||
" Aliases used exclusively for autoformat config.
|
||||
" Pencil will NOT modify the filetype setting.
|
||||
let g:pencil#autoformat_aliases = {
|
||||
\ 'md': 'markdown',
|
||||
\ 'mkd': 'markdown',
|
||||
\ }
|
||||
en
|
||||
let g:pencil#autoformat_inline_whitelist_re =
|
||||
\ '\v(' . join(g:pencil#autoformat_inline_whitelist, '|') . ')'
|
||||
|
||||
if !exists('g:pencil#joinspaces')
|
||||
" by default, only one space after full stop (.)
|
||||
@@ -136,9 +160,9 @@ en
|
||||
if !exists('g:pencil#mode_indicators')
|
||||
" used to set PencilMode() for statusline
|
||||
if s:unicode_enabled()
|
||||
let g:pencil#mode_indicators = {'hard': '␍', 'soft': '⤸', 'off': '',}
|
||||
let g:pencil#mode_indicators = {'hard': '␍', 'auto': 'ª', 'soft': '⤸', 'off': '',}
|
||||
el
|
||||
let g:pencil#mode_indicators = {'hard': 'H', 'soft': 'S', 'off': '',}
|
||||
let g:pencil#mode_indicators = {'hard': 'H', 'auto': 'A', 'soft': 'S', 'off': '',}
|
||||
en
|
||||
en
|
||||
|
||||
|
||||
Reference in New Issue
Block a user