Support Conceal for Markdown; disable smartindent

This commit is contained in:
Reed Esau
2014-08-21 21:55:18 -06:00
parent 096dd965d7
commit 55b4a6e690
3 changed files with 207 additions and 168 deletions

View File

@@ -217,6 +217,32 @@ a hard break. If you wish to retain the default Vim behavior, set the
let g:pencil#cursorwrap = 1 " 0=disable, 1=enable (def)
```
### Concealing markup in Markdown
For syntaxes such as [tpope/markdown][tm] which support Vims Conceal
feature, you can display \_*italic*\_, \*\***bold**\*\* and \*\*\****bold
italic***\*\*\* markup where the `_` and `*` characters will be hidden
when youre not on the line. Set the following to `0` if you dont want
this behavior.
```vim
let g:pencil#conceallevel = 2 " 0=disable, 1=onechar, 2=hide (def)
```
Note that to display the _italic_ and **bold** styles in Vim, you may need
both a font (such as [Cousine][co]) with those style variants as well as
a colorscheme (such as [reedes/vim-colors-pencil][cp]) which supports the
Markdown-specific highlight groups.
For more details on Vims Conceal feature, see:
```vim
:help conceallevel
```
[co]: http://www.google.com/fonts/specimen/Cousine
[tm]: http://github.com/tpope/vim-markdown
## Auto-detecting wrap mode
If you didn't explicitly specify a wrap mode during initialization,
@@ -300,7 +326,7 @@ If you find the _pencil_ plugin useful, check out these others by [@reedes][re]:
* [vim-colors-pencil][cp] - color scheme for Vim inspired by IA Writer
* [vim-lexical][lx] - building on Vims spell-check and thesaurus/dictionary completion
* [vim-litecorrect][lc] - lightweight auto-correction for Vim
* [vim-one][vo] - make use of Vims _+clientserver_ capabilities
* [vim-one][vo] - make use of Vims _+clientserver_ capabilities
* [vim-textobj-quote][qu] - extends Vim to support typographic (curly) quotes
* [vim-textobj-sentence][ts] - improving on Vim's native sentence motion command
* [vim-thematic][th] - modify Vims appearance to suit your task and environment

View File

@@ -6,7 +6,7 @@
" License: The MIT License (MIT)
" ============================================================================
if exists("autoloaded_pencil") | finish | endif
if exists("autoloaded_pencil") | fini | en
let autoloaded_pencil = 1
let s:WRAP_MODE_DEFAULT = -1
@@ -17,7 +17,7 @@ let s:WRAP_MODE_SOFT = 2
" Wrap-mode detector
" Scan lines at end and beginning of file to determine the wrap mode.
" Modelines has priority over long lines found.
function! s:detect_wrap_mode() abort
fun! s:detect_wrap_mode() abort
let b:max_textwidth = -1 " assume no relevant modeline
call s:doModelines()
@@ -25,62 +25,62 @@ function! s:detect_wrap_mode() abort
if b:max_textwidth > 0
" modelines(s) found with positive textwidth, so hard line breaks
return s:WRAP_MODE_HARD
endif
en
if b:max_textwidth == 0 || g:pencil#wrapModeDefault ==# 'soft'
" modeline(s) found only with zero textwidth, so it's soft line wrap
" or, the user wants to default to soft line wrap
return s:WRAP_MODE_SOFT
endif
en
" attempt to rule out soft line wrap
" scan initial lines in an attempt to detect long lines
for l:line in getline(1, g:pencil#softDetectSample)
if len(l:line) > g:pencil#softDetectThreshold
return s:WRAP_MODE_SOFT
endif
endfor
en
endfo
" punt
return s:WRAP_MODE_DEFAULT
endfunction
endf
function! s:imap(preserve_completion, key, icmd)
fun! s:imap(preserve_completion, key, icmd)
if a:preserve_completion
execute ":inoremap <silent> <expr> " . a:key . " pumvisible() ? \"" . a:key . "\" : \"" . a:icmd . "\""
else
execute ":inoremap <silent> " . a:key . " " . a:icmd
endif
endfunction
exe ":ino <silent> <expr> " . a:key . " pumvisible() ? \"" . a:key . "\" : \"" . a:icmd . "\""
el
exe ":ino <silent> " . a:key . " " . a:icmd
en
endf
function! pencil#setAutoFormat(mode)
fun! pencil#setAutoFormat(mode)
" 1=auto, 0=manual, -1=toggle
if !exists('b:last_autoformat')
let b:last_autoformat = 0
endif
en
let b:last_autoformat = a:mode == -1 ? !b:last_autoformat : a:mode
if b:last_autoformat
augroup pencil_autoformat
autocmd InsertEnter <buffer> set formatoptions+=a
autocmd InsertLeave <buffer> set formatoptions-=a
augroup END
else
silent! autocmd! pencil_autoformat * <buffer>
endif
endfunction
aug pencil_autoformat
au InsertEnter <buffer> set formatoptions+=a
au InsertLeave <buffer> set formatoptions-=a
aug END
el
sil! au! pencil_autoformat * <buffer>
en
endf
" Create mappings for word processing
" args:
" 'wrap': 'detect|off|hard|soft|toggle'
function! pencil#init(...) abort
fun! pencil#init(...) abort
let l:args = a:0 ? a:1 : {}
if !exists('b:wrap_mode')
let b:wrap_mode = s:WRAP_MODE_OFF
endif
en
if !exists("b:max_textwidth")
let b:max_textwidth = -1
endif
en
" If user explicitly requested wrap_mode thru args, go with that.
let l:wrap_arg = get(l:args, 'wrap', 'detect')
@@ -88,27 +88,27 @@ function! pencil#init(...) abort
if (b:wrap_mode && l:wrap_arg ==# 'toggle') ||
\ l:wrap_arg =~# '^\(0\|off\|disable\|false\)$'
let b:wrap_mode = s:WRAP_MODE_OFF
elseif l:wrap_arg ==# 'hard'
elsei l:wrap_arg ==# 'hard'
let b:wrap_mode = s:WRAP_MODE_HARD
elseif l:wrap_arg ==# 'soft'
elsei l:wrap_arg ==# 'soft'
let b:wrap_mode = s:WRAP_MODE_SOFT
elseif l:wrap_arg ==# 'default'
elsei l:wrap_arg ==# 'default'
let b:wrap_mode = s:WRAP_MODE_DEFAULT
else
el
" this can return s:WRAP_MODE_ for soft, hard or default
let b:wrap_mode = s:detect_wrap_mode()
endif
en
" translate default(-1) to soft(1) or hard(2) or off(0)
if b:wrap_mode == s:WRAP_MODE_DEFAULT
if g:pencil#wrapModeDefault =~# '^\(0\|off\|disable\|false\)$'
let b:wrap_mode = s:WRAP_MODE_OFF
elseif g:pencil#wrapModeDefault ==# 'soft'
elsei g:pencil#wrapModeDefault ==# 'soft'
let b:wrap_mode = s:WRAP_MODE_SOFT
else
el
let b:wrap_mode = s:WRAP_MODE_HARD
endif
endif
en
en
" autoformat is only used in Hard mode, and then only during
" Insert mode
@@ -119,24 +119,24 @@ function! pencil#init(...) abort
if b:wrap_mode == s:WRAP_MODE_HARD
if &modeline == 0 && b:max_textwidth > 0
" Compensate for disabled modeline
execute 'setlocal textwidth=' . b:max_textwidth
elseif &textwidth == 0
execute 'setlocal textwidth=' . g:pencil#textwidth
else
setlocal textwidth<
endif
setlocal nowrap
elseif b:wrap_mode == s:WRAP_MODE_SOFT
setlocal textwidth=0
setlocal wrap
setlocal linebreak
setlocal colorcolumn=0 " doesn't align as expected
else
setlocal textwidth<
setlocal wrap< nowrap<
setlocal linebreak< nolinebreak<
setlocal colorcolumn<
endif
exe 'setl textwidth=' . b:max_textwidth
elsei &textwidth == 0
exe 'setl textwidth=' . g:pencil#textwidth
el
setl textwidth<
en
setl nowrap
elsei b:wrap_mode == s:WRAP_MODE_SOFT
setl textwidth=0
setl wrap
setl linebreak
setl colorcolumn=0 " doesn't align as expected
el
setl textwidth<
setl wrap< nowrap<
setl linebreak< nolinebreak<
setl colorcolumn<
en
" global settings
if b:wrap_mode
@@ -144,122 +144,128 @@ function! pencil#init(...) abort
set backspace=indent,eol,start
if g:pencil#joinspaces
set joinspaces " two spaces after .!?
else
el
set nojoinspaces " only one space after a .!? (default)
endif
en
"if b:wrap_mode == s:WRAP_MODE_SOFT
" " augment with additional chars
" " TODO not working yet with n and m-dash
" set breakat=\ !@*-+;:,./?([{
"endif
endif
"en
en
" because ve=onemore is relatively rare and could break
" other plugins, restrict its presence to buffer
" Better: restore ve to original setting
if b:wrap_mode && g:pencil#cursorwrap
set whichwrap+=<,>,b,s,h,l,[,]
augroup pencil_cursorwrap
autocmd BufEnter <buffer> set virtualedit+=onemore
autocmd BufLeave <buffer> set virtualedit-=onemore
augroup END
else
silent! autocmd! pencil_cursorwrap * <buffer>
endif
aug pencil_cursorwrap
au BufEnter <buffer> set virtualedit+=onemore
au BufLeave <buffer> set virtualedit-=onemore
aug END
el
sil! au! pencil_cursorwrap * <buffer>
en
" window/buffer settings
if b:wrap_mode
setlocal nolist
setlocal wrapmargin=0
setlocal autoindent " needed by formatoptions=n
setlocal formatoptions+=n " recognize numbered lists
setlocal formatoptions+=1 " don't break line before 1 letter word
setlocal formatoptions+=t " autoformat of text (vim default)
setlocal formatoptions+=c " autoformat of comments (vim default)
setl nolist
setl wrapmargin=0
setl autoindent " needed by formatoptions=n
setl nosmartindent " avoid c-style indents in prose
setl formatoptions+=n " recognize numbered lists
setl formatoptions+=1 " don't break line before 1 letter word
setl formatoptions+=t " autoformat of text (vim default)
setl formatoptions+=c " autoformat of comments (vim default)
" clean out stuff we likely don't want
setlocal formatoptions-=2 " use indent of 2nd line for rest of paragraph
setlocal formatoptions-=v " only break line at blank entered during insert
setlocal formatoptions-=w " avoid erratic behavior if mixed spaces
setlocal formatoptions-=a " autoformat will turn on with Insert in HardPencil mode
setlocal formatoptions-=r " don't insert comment leader
setlocal formatoptions-=o " don't insert comment leader
else
setlocal autoindent< noautoindent<
setlocal list< nolist<
setlocal wrapmargin<
setlocal formatoptions<
endif
setl formatoptions-=2 " use indent of 2nd line for rest of paragraph
setl formatoptions-=v " only break line at blank entered during insert
setl formatoptions-=w " avoid erratic behavior if mixed spaces
setl formatoptions-=a " autoformat will turn on with Insert in HardPencil mode
setl formatoptions-=r " don't insert comment leader
setl formatoptions-=o " don't insert comment leader
exe ":setl cole=" . g:pencil#conceallevel
el
setl smartindent< nosmartindent<
setl autoindent< noautoindent<
setl list< nolist<
setl wrapmargin<
setl formatoptions<
setl cole<
en
if b:wrap_mode == s:WRAP_MODE_SOFT
nnoremap <buffer> <silent> $ g$
nnoremap <buffer> <silent> 0 g0
vnoremap <buffer> <silent> $ g$
vnoremap <buffer> <silent> 0 g0
noremap <buffer> <silent> <Home> g<Home>
noremap <buffer> <silent> <End> g<End>
nn <buffer> <silent> $ g$
nn <buffer> <silent> 0 g0
vn <buffer> <silent> $ g$
vn <buffer> <silent> 0 g0
no <buffer> <silent> <Home> g<Home>
no <buffer> <silent> <End> g<End>
" preserve behavior of home/end keys in popups
call s:imap(1, '<Home>', '<C-o>g<Home>')
call s:imap(1, '<End>' , '<C-o>g<End>' )
else
silent! nunmap <buffer> $
silent! nunmap <buffer> 0
silent! vunmap <buffer> $
silent! vunmap <buffer> 0
silent! nunmap <buffer> <Home>
silent! nunmap <buffer> <End>
silent! iunmap <buffer> <Home>
silent! iunmap <buffer> <End>
endif
el
sil! nun <buffer> $
sil! nun <buffer> 0
sil! vu <buffer> $
sil! vu <buffer> 0
sil! nun <buffer> <Home>
sil! nun <buffer> <End>
sil! iu <buffer> <Home>
sil! iu <buffer> <End>
en
if b:wrap_mode
nnoremap <buffer> <silent> j gj
nnoremap <buffer> <silent> k gk
vnoremap <buffer> <silent> j gj
vnoremap <buffer> <silent> k gk
noremap <buffer> <silent> <Up> gk
noremap <buffer> <silent> <Down> gj
nn <buffer> <silent> j gj
nn <buffer> <silent> k gk
vn <buffer> <silent> j gj
vn <buffer> <silent> k gk
no <buffer> <silent> <Up> gk
no <buffer> <silent> <Down> gj
" preserve behavior of up/down keys in popups
call s:imap(1, '<Up>' , '<C-o>g<Up>' )
call s:imap(1, '<Down>', '<C-o>g<Down>')
else
silent! nunmap <buffer> j
silent! nunmap <buffer> k
silent! vunmap <buffer> j
silent! vunmap <buffer> k
silent! unmap <buffer> <Up>
silent! unmap <buffer> <Down>
el
sil! nun <buffer> j
sil! nun <buffer> k
sil! vu <buffer> j
sil! vu <buffer> k
sil! unm <buffer> <Up>
sil! unm <buffer> <Down>
silent! iunmap <buffer> <Up>
silent! iunmap <buffer> <Down>
endif
sil! iu <buffer> <Up>
sil! iu <buffer> <Down>
en
" set undo points around common punctuation,
" line <c-u> and word <c-w> deletions
if b:wrap_mode
inoremap <buffer> . .<c-g>u
inoremap <buffer> ! !<c-g>u
inoremap <buffer> ? ?<c-g>u
inoremap <buffer> , ,<c-g>u
inoremap <buffer> ; ;<c-g>u
inoremap <buffer> : :<c-g>u
inoremap <buffer> <c-u> <c-g>u<c-u>
inoremap <buffer> <c-w> <c-g>u<c-w>
inoremap <buffer> <cr> <c-g>u<cr>
else
silent! iunmap <buffer> .
silent! iunmap <buffer> !
silent! iunmap <buffer> ?
silent! iunmap <buffer> ,
silent! iunmap <buffer> ;
silent! iunmap <buffer> :
silent! iunmap <buffer> <c-u>
silent! iunmap <buffer> <c-w>
silent! iunmap <buffer> <cr>
endif
endfunction
ino <buffer> . .<c-g>u
ino <buffer> ! !<c-g>u
ino <buffer> ? ?<c-g>u
ino <buffer> , ,<c-g>u
ino <buffer> ; ;<c-g>u
ino <buffer> : :<c-g>u
ino <buffer> <c-u> <c-g>u<c-u>
ino <buffer> <c-w> <c-g>u<c-w>
ino <buffer> <cr> <c-g>u<cr>
el
sil! iu <buffer> .
sil! iu <buffer> !
sil! iu <buffer> ?
sil! iu <buffer> ,
sil! iu <buffer> ;
sil! iu <buffer> :
sil! iu <buffer> <c-u>
sil! iu <buffer> <c-w>
sil! iu <buffer> <cr>
en
endf
" attempt to find a non-zero textwidth, etc.
fun! s:doOne(item) abort
@@ -269,10 +275,10 @@ fun! s:doOne(item) abort
let l:tw = str2nr(l:matches[2])
if l:tw > b:max_textwidth
let b:max_textwidth = l:tw
endif
endif
endif
endfun
en
en
en
endf
" attempt to find a non-zero textwidth, etc.
fun! s:doModeline(line) abort
@@ -280,15 +286,15 @@ fun! s:doModeline(line) abort
if len(l:matches) > 0
for l:item in split(l:matches[3])
call s:doOne(l:item)
endfor
endif
endfo
en
let l:matches = matchlist(a:line, '\%(\S\@<!\%(vi\|vim\([<>=]\?\)\([0-9]\+\)\?\)\|\sex\):\(.\+\)')
if len(l:matches) > 0
for l:item in split(l:matches[3], '[ \t:]')
call s:doOne(l:item)
endfor
endif
endfun
endfo
en
endf
" sample lines for detection, capturing both
" modeline(s) and max line length
@@ -301,12 +307,12 @@ fun! s:doModelines() abort
\ 'v:val =~ ":"'), 'extend(l:lines, { v:val : 0 } )')
for l:line in keys(l:lines)
call s:doModeline(l:line)
endfor
else
endfo
el
for l:line in getline(1, "$")
call s:doModeline(l:line)
endfor
endif
endfun
endfo
en
endf
" vim:ts=2:sw=2:sts=2

View File

@@ -6,7 +6,7 @@
" License: The MIT License (MIT)
" ============================================================================
"
if exists('g:loaded_pencil') || &cp | finish | endif
if exists('g:loaded_pencil') || &cp | fini | en
let g:loaded_pencil = 1
" Save 'cpoptions' and set Vim default to enable line continuations.
@@ -17,53 +17,60 @@ if !exists('g:pencil#wrapModeDefault')
" user-overridable default, if detection fails
" should be 'soft' or 'hard' or 'off'
let g:pencil#wrapModeDefault = 'hard'
endif
en
if !exists('g:pencil#textwidth')
" textwidth used when in hard linebreak mode
let g:pencil#textwidth = 74
endif
en
if !exists('g:pencil#autoformat')
" by default, automatically format text when in Insert mode
" with hard wrap.
let g:pencil#autoformat = 1
endif
en
if !exists('g:pencil#joinspaces')
" by default, only one space after full stop (.)
let g:pencil#joinspaces = 0
endif
en
if !exists('g:pencil#cursorwrap')
" by default, h/l and cursor keys will wrap around hard
" linebreaks. Set to 0 if you don't want this behavior
let g:pencil#cursorwrap = 1
endif
en
if !exists('g:pencil#conceallevel')
" by default, concealing capability in your syntax plugin
" will be enabled. See tpope/vim-markdown for example.
" Set to 0 if you don't want this behavior
let g:pencil#conceallevel = 2
en
if !exists('g:pencil#softDetectSample')
" if no modeline, read as many as this many lines at
" start of file in attempt to detect at least one line
" whose byte count exceeds g:pencil#softDetectThreshold
let g:pencil#softDetectSample = 20
endif
en
if !exists('g:pencil#softDetectThreshold')
" if the byte count of at least one sampled line exceeds
" this number, then pencil assumes soft line wrapping
let g:pencil#softDetectThreshold = 130
endif
en
" # Commands
command -nargs=0 HardPencil call pencil#init({'wrap': 'hard'})
command -nargs=0 SoftPencil call pencil#init({'wrap': 'soft'})
command -nargs=0 DropPencil call pencil#init({'wrap': 'off' })
command -nargs=0 NoPencil call pencil#init({'wrap': 'off' })
command -nargs=0 TogglePencil call pencil#init({'wrap': 'toggle'})
" # coms
com -nargs=0 HardPencil call pencil#init({'wrap': 'hard'})
com -nargs=0 SoftPencil call pencil#init({'wrap': 'soft'})
com -nargs=0 DropPencil call pencil#init({'wrap': 'off' })
com -nargs=0 NoPencil call pencil#init({'wrap': 'off' })
com -nargs=0 TogglePencil call pencil#init({'wrap': 'toggle'})
command -nargs=0 AutoPencil call pencil#setAutoFormat(1)
command -nargs=0 ManualPencil call pencil#setAutoFormat(0)
command -nargs=0 ShiftPencil call pencil#setAutoFormat(-1)
com -nargs=0 AutoPencil call pencil#setAutoFormat(1)
com -nargs=0 ManualPencil call pencil#setAutoFormat(0)
com -nargs=0 ShiftPencil call pencil#setAutoFormat(-1)
let &cpo = s:save_cpo
unlet s:save_cpo