mirror of
https://github.com/preservim/vim-pencil.git
synced 2025-11-12 11:53:47 -05:00
#7 fix - avoid enabling autoformat for code blocks
At least a partial fix for the problem of autoformat wreaking havoc on code blocks. Couldn't find any highlighting groups for tables. If anybody can find them, we can add them to the exclude list.
This commit is contained in:
@@ -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 <silent> <leader>pp :ShiftPencil<cr>
|
||||
nnoremap <silent> <leader>p :ShiftPencil<cr>
|
||||
```
|
||||
|
||||
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
|
||||
|
||||
@@ -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 <buffer> set formatoptions+=a
|
||||
au InsertLeave <buffer> set formatoptions-=a
|
||||
au InsertEnter <buffer> call s:enable_autoformat(1)
|
||||
au InsertLeave <buffer> call s:enable_autoformat(0)
|
||||
aug END
|
||||
el
|
||||
sil! au! pencil_autoformat * <buffer>
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user