Skip to content

Commit 45f4c46

Browse files
author
Reed Esau
committed
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.
1 parent 536311e commit 45f4c46

File tree

3 files changed

+31
-80
lines changed

3 files changed

+31
-80
lines changed

README.markdown

+15-36
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ augroup END
258258
...where by default, files of type `text` will use hard line endings, but
259259
with autoformat disabled.
260260

261+
Optionally, you can map a toggle to control autoformat in your `.vimrc`:
262+
263+
```vim
264+
noremap <buffer> <silent> <F7> :<C-u>PFormatToggle<cr>
265+
inoremap <buffer> <silent> <F7> <C-o>:PFormatToggle<cr>
266+
```
267+
261268
## Manual formatting
262269

263270
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:
432439

433440
```vim
434441
let g:pencil#autoformat_blacklist = [
435-
\ 'markdownCode',
436-
\ 'markdownH[0-9]',
437-
\ 'markdownUrl',
438-
\ 'markdownIdDeclaration',
439-
\ 'markdownLink',
440-
\ 'markdownRule',
441-
\ 'markdownHighlight[A-Za-z0-9]+',
442-
\ 'markdownListMarker',
443-
\ 'mkdCode',
444-
\ 'mkdRule',
445-
\ 'mkdDelimiter',
446-
\ 'mkdLink',
447-
\ 'mkdListItem',
448-
\ 'mkdIndentCode',
442+
\ 'markdown(Code|H[0-9]|Url|IdDeclaration|Link|Rule|Highlight[A-Za-z0-9]+)',
443+
\ 'markdownOrderedListMarker',
444+
\ 'mkd(Code|Rule|Delimiter|Link|ListItem|IndentCode)',
449445
\ 'htmlH[0-9]',
450-
\ 'markdownFencedCodeBlock',
451-
\ 'markdownInlineCode',
446+
\ 'markdown(FencedCodeBlock|InlineCode)',
452447
\ 'mmdTable[A-Za-z0-9]*',
453448
\ 'txtCode',
454-
\ 'rstCodeBlock',
455-
\ 'rstDirective',
456-
\ 'rstLiteralBlock',
457-
\ 'rstSections',
458-
\ 'texBeginEndName',
459-
\ 'texDelimiter',
460-
\ 'texDocType',
461-
\ 'texInputFile',
462-
\ 'texMath',
463-
\ 'texRefZone',
449+
\ 'rst(CodeBlock|Directive|LiteralBlock|Sections)',
450+
\ 'tex(BeginEndName|Delimiter|DocType|InputFile|Math|RefZone|Title)',
464451
\ 'texSection$',
465-
\ 'texTitle',
466-
\ 'asciidocAttributeList',
467-
\ 'asciidocListLabel',
468-
\ 'asciidocLiteral',
469-
\ 'asciidocSidebar',
470-
\ 'asciidocSource',
471-
\ 'asciidocSect[0-9]',
472-
\ 'asciidoc[A-Za-z]*Block',
473-
\ 'asciidoc[A-Za-z]*Macro',
474-
\ 'asciidoc[A-Za-z]*Title',
452+
\ 'asciidoc(AttributeList|ListLabel|Literal|SideBar|Source|Sect[0-9])',
453+
\ 'asciidoc[A-Za-z]*(Block|Macro|Title)',
475454
\ ]
476455
```
477456

autoload/pencil.vim

+5-3
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,10 @@ fun! s:maybe_enable_autoformat() abort
121121
endfo
122122
en
123123
en
124-
" disallow enable if start of previous line is in blacklist
125-
if l:line > 1
124+
" disallow enable if start of previous line is in blacklist,
125+
" (To avoid problem of autowrap screwing up adding a new item
126+
" to a list.)
127+
if l:okay_to_enable && l:line > 1
126128
let l:prev_stack = synstack(l:line - 1, 1)
127129
for l:sid in l:prev_stack
128130
if len(l:sid) > 0 &&
@@ -152,7 +154,6 @@ fun! pencil#setAutoFormat(af) abort
152154
au InsertEnter <buffer> call s:maybe_enable_autoformat()
153155
au InsertLeave <buffer> set formatoptions-=a
154156
aug END
155-
let b:last_autoformat = l:nu_af
156157
el
157158
sil! au! pencil_autoformat * <buffer>
158159
if l:nu_af && !l:is_hard
@@ -162,6 +163,7 @@ fun! pencil#setAutoFormat(af) abort
162163
return
163164
en
164165
en
166+
let b:last_autoformat = l:nu_af
165167
endf
166168

167169
" Create mappings for word processing

plugin/pencil.vim

+11-41
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ if !exists('g:pencil#autoformat')
5858
en
5959

6060
if !exists('g:pencil#autoformat_blacklist')
61-
" by default, pencil does NOT start autoformat if inside any of
61+
" do not engage autoformat if cursor is inside any of
6262
" the following syntax groups
6363
"
6464
" markdown* (tpope/vim-markdown)
@@ -68,46 +68,18 @@ if !exists('g:pencil#autoformat_blacklist')
6868
" txtCode (timcharper/textile.vim)
6969
" rst*,tex*,asciidoc* (syntax file shipped with vim)
7070
let g:pencil#autoformat_blacklist = [
71-
\ 'markdownCode',
72-
\ 'markdownH[0-9]',
73-
\ 'markdownUrl',
74-
\ 'markdownIdDeclaration',
75-
\ 'markdownLink',
76-
\ 'markdownRule',
77-
\ 'markdownHighlight[A-Za-z0-9]+',
78-
\ 'markdownListMarker',
79-
\ 'mkdCode',
80-
\ 'mkdRule',
81-
\ 'mkdDelimiter',
82-
\ 'mkdLink',
83-
\ 'mkdListItem',
84-
\ 'mkdIndentCode',
71+
\ 'markdown(Code|H[0-9]|Url|IdDeclaration|Link|Rule|Highlight[A-Za-z0-9]+)',
72+
\ 'markdownOrderedListMarker',
73+
\ 'mkd(Code|Rule|Delimiter|Link|ListItem|IndentCode)',
8574
\ 'htmlH[0-9]',
86-
\ 'markdownFencedCodeBlock',
87-
\ 'markdownInlineCode',
75+
\ 'markdown(FencedCodeBlock|InlineCode)',
8876
\ 'mmdTable[A-Za-z0-9]*',
8977
\ 'txtCode',
90-
\ 'rstCodeBlock',
91-
\ 'rstDirective',
92-
\ 'rstLiteralBlock',
93-
\ 'rstSections',
94-
\ 'texBeginEndName',
95-
\ 'texDelimiter',
96-
\ 'texDocType',
97-
\ 'texInputFile',
98-
\ 'texMath',
99-
\ 'texRefZone',
78+
\ 'rst(CodeBlock|Directive|LiteralBlock|Sections)',
79+
\ 'tex(BeginEndName|Delimiter|DocType|InputFile|Math|RefZone|Title)',
10080
\ 'texSection$',
101-
\ 'texTitle',
102-
\ 'asciidocAttributeList',
103-
\ 'asciidocListLabel',
104-
\ 'asciidocLiteral',
105-
\ 'asciidocSidebar',
106-
\ 'asciidocSource',
107-
\ 'asciidocSect[0-9]',
108-
\ 'asciidoc[A-Za-z]*Block',
109-
\ 'asciidoc[A-Za-z]*Macro',
110-
\ 'asciidoc[A-Za-z]*Title',
81+
\ 'asciidoc(AttributeList|ListLabel|Literal|SideBar|Source|Sect[0-9])',
82+
\ 'asciidoc[A-Za-z]*(Block|Macro|Title)',
11183
\ ]
11284
en
11385
let g:pencil#autoformat_blacklist_re =
@@ -119,10 +91,8 @@ if !exists('g:pencil#autoformat_inline_whitelist')
11991
"
12092
"'markdownCode' (tpope/vim-markdown)
12193
let g:pencil#autoformat_inline_whitelist = [
122-
\ 'markdownCode',
123-
\ 'markdownLink',
124-
\ 'asciidocAttributeRef',
125-
\ 'asciidocMacro',
94+
\ 'markdown(Code|Link)',
95+
\ 'asciidoc(AttributeRef|Macro)',
12696
\ ]
12797
en
12898
let g:pencil#autoformat_inline_whitelist_re =

0 commit comments

Comments
 (0)