New indicator for use in status line; fixes #6

This commit is contained in:
Reed Esau
2014-08-31 03:48:12 -06:00
parent f18b74bac5
commit a275f35c6f
3 changed files with 101 additions and 38 deletions

View File

@@ -14,7 +14,8 @@ The _pencil_ plugin aspires to make Vim as powerful a tool for writers as
it is for coders by focusing narrowly on the handful of tweaks needed to
smooth the path to writing prose.
* For editing prose-oriented file types such as _text_, _markdown_, and _textile_
* For editing prose-oriented file types such as _text_, _markdown_, and
_textile_
* Agnostic on soft line wrap _versus_ hard line breaks, supporting both
* Auto-detects wrap mode via modeline and sampling
* Adjusts navigation key mappings to suit the wrap mode
@@ -25,6 +26,7 @@ smooth the path to writing prose.
preserves your global settings)
* Support for Vims Conceal feature to hide markup defined by Syntax
plugins (e.g., `_` and `*` markup for styled text in \_*Markdown*\_)
* Support for display of mode indicator (`h✎`, e.g.) in the status line
* Pure Vimscript with no dependencies
Need spell-check and other features? Vim is about customization. To
@@ -53,9 +55,9 @@ You can find that reason in Vim's mysterious command sequences. Take `cas`
for instance. You might see it as a mnemonic for _Change Around Sentence_
to replace an existing sentence. But dig a bit deeper to discover that
such commands have a grammar of their own, comprised of nouns, verbs, and
modifiers. Think of them as the composable building blocks of a _domain specific
language_ for manipulating text, one that can become a powerful tool in
expressing yourself. For more details on vi-style editing, see...
modifiers. Think of them as the composable building blocks of a _domain
specific language_ for manipulating text, one that can become a powerful
tool in expressing yourself. For more details on vi-style editing, see...
* [Learn to speak vim verbs, nouns, and modifiers!][ls] (December 2011)
* [Your problem with Vim is that you don't grok vi][gv] (December 2011)
@@ -164,7 +166,8 @@ augroup END
with autoformat disabled.
(\*) Advanced users will want to check out `g:pencil#autoformat_blacklist`
to set highlight groups for which autoformat will not be enabled.
to set highlight groups for which autoformat will not be enabled when
entering Insert mode.
### Manual formatting
@@ -260,6 +263,34 @@ terminal to support **bold** and _italic_ styles.
[co]: http://www.google.com/fonts/specimen/Cousine
[tm]: http://github.com/tpope/vim-markdown
### Status line indicator
Your status line can reflect the wrap mode for _pencil_ buffers. For
example, `h✎` to represent `HardPencil` (hard line break) mode.
To configure your status line, add to your `.vimrc`:
```vim
set statusline=%<%f\ %{PencilMode()}\ %P
```
or if using [bling/vim-airline][va]:
```vim
let g:airline_section_x = '%{PencilMode()}'
```
If you dont like the default indicators, you can specify different ones:
```vim
let g:pencil#mode_indicators = {'hard': 'h✎ ', 'soft': 's✎ ', 'off': '×✎ '}
```
Note that `PencilMode()` will return blank for buffers in which _pencil_
has not been initialized.
[va]: http://github.com/bling/vim-airline
## Auto-detecting wrap mode
**(For advanced users looking to tweak _pencil's_ behavior.)**
@@ -322,11 +353,7 @@ then _pencil_ assumes soft line wrap.
let g:pencil#softDetectThreshold = 130
```
If no such lines found, _pencil_ falls back to the default mentioned earlier:
```vim
let g:pencil#wrapModeDefault = 'hard' " or 'soft'
```
If no such lines found, _pencil_ falls back to the default wrap mode.
## See also

View File

@@ -27,7 +27,7 @@ fun! s:detect_wrap_mode() abort
return s:WRAP_MODE_HARD
en
if b:max_textwidth == 0 || g:pencil#wrapModeDefault ==# 'soft'
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
@@ -53,7 +53,7 @@ fun! s:imap(preserve_completion, key, icmd)
en
endf
fun! s:enable_autoformat()
fun! s:maybe_enable_autoformat()
" don't enable autoformat if in a code block or table
let l:okay_to_enable = 1
let l:col = col('.')
@@ -82,10 +82,10 @@ fun! pencil#setAutoFormat(mode)
if !exists('b:last_autoformat')
let b:last_autoformat = 0
en
let b:last_autoformat = a:mode == -1 ? !b:last_autoformat : a:mode
let b:last_autoformat = a:mode ==# -1 ? !b:last_autoformat : a:mode
if b:last_autoformat
aug pencil_autoformat
au InsertEnter <buffer> call s:enable_autoformat()
au InsertEnter <buffer> call s:maybe_enable_autoformat()
au InsertLeave <buffer> set formatoptions-=a
aug END
el
@@ -99,8 +99,8 @@ endf
fun! pencil#init(...) abort
let l:args = a:0 ? a:1 : {}
if !exists('b:wrap_mode')
let b:wrap_mode = s:WRAP_MODE_OFF
if !exists('b:pencil_wrap_mode')
let b:pencil_wrap_mode = s:WRAP_MODE_OFF
en
if !exists("b:max_textwidth")
let b:max_textwidth = -1
@@ -109,49 +109,49 @@ fun! pencil#init(...) abort
" If user explicitly requested wrap_mode thru args, go with that.
let l:wrap_arg = get(l:args, 'wrap', 'detect')
if (b:wrap_mode && l:wrap_arg ==# 'toggle') ||
if (b:pencil_wrap_mode && l:wrap_arg ==# 'toggle') ||
\ l:wrap_arg =~# '^\(0\|off\|disable\|false\)$'
let b:wrap_mode = s:WRAP_MODE_OFF
let b:pencil_wrap_mode = s:WRAP_MODE_OFF
elsei l:wrap_arg ==# 'hard'
let b:wrap_mode = s:WRAP_MODE_HARD
let b:pencil_wrap_mode = s:WRAP_MODE_HARD
elsei l:wrap_arg ==# 'soft'
let b:wrap_mode = s:WRAP_MODE_SOFT
let b:pencil_wrap_mode = s:WRAP_MODE_SOFT
elsei l:wrap_arg ==# 'default'
let b:wrap_mode = s:WRAP_MODE_DEFAULT
let b:pencil_wrap_mode = s:WRAP_MODE_DEFAULT
el
" this can return s:WRAP_MODE_ for soft, hard or default
let b:wrap_mode = s:detect_wrap_mode()
let b:pencil_wrap_mode = s:detect_wrap_mode()
en
" translate default(-1) to soft(1) or hard(2) or off(0)
if b:wrap_mode == s:WRAP_MODE_DEFAULT
if b:pencil_wrap_mode ==# s:WRAP_MODE_DEFAULT
if g:pencil#wrapModeDefault =~# '^\(0\|off\|disable\|false\)$'
let b:wrap_mode = s:WRAP_MODE_OFF
let b:pencil_wrap_mode = s:WRAP_MODE_OFF
elsei g:pencil#wrapModeDefault ==# 'soft'
let b:wrap_mode = s:WRAP_MODE_SOFT
let b:pencil_wrap_mode = s:WRAP_MODE_SOFT
el
let b:wrap_mode = s:WRAP_MODE_HARD
let b:pencil_wrap_mode = s:WRAP_MODE_HARD
en
en
" autoformat is only used in Hard mode, and then only during
" Insert mode
call pencil#setAutoFormat(
\ b:wrap_mode == s:WRAP_MODE_HARD &&
\ b:pencil_wrap_mode ==# s:WRAP_MODE_HARD &&
\ get(l:args, 'autoformat', g:pencil#autoformat))
if b:wrap_mode == s:WRAP_MODE_HARD
if &modeline == 0 && b:max_textwidth > 0
if b:pencil_wrap_mode ==# s:WRAP_MODE_HARD
if &modeline ==# 0 && b:max_textwidth > 0
" Compensate for disabled modeline
exe 'setl textwidth=' . b:max_textwidth
elsei &textwidth == 0
elsei &textwidth ==# 0
exe 'setl textwidth=' .
\ get(l:args, 'textwidth', g:pencil#textwidth)
el
setl textwidth<
en
setl nowrap
elsei b:wrap_mode == s:WRAP_MODE_SOFT
elsei b:pencil_wrap_mode ==# s:WRAP_MODE_SOFT
setl textwidth=0
setl wrap
setl linebreak
@@ -168,7 +168,7 @@ fun! pencil#init(...) abort
en
" global settings
if b:wrap_mode
if b:pencil_wrap_mode
set display+=lastline
set backspace=indent,eol,start
if get(l:args, 'joinspaces', g:pencil#joinspaces)
@@ -177,7 +177,7 @@ fun! pencil#init(...) abort
set nojoinspaces " only one space after a .!? (default)
en
"if b:wrap_mode == s:WRAP_MODE_SOFT
"if b:pencil_wrap_mode ==# s:WRAP_MODE_SOFT
" " augment with additional chars
" set breakat=\ !@*-+;:,./?([{
"en
@@ -186,7 +186,7 @@ fun! pencil#init(...) abort
" 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 && get(l:args, 'cursorwrap', g:pencil#cursorwrap)
if b:pencil_wrap_mode && get(l:args, 'cursorwrap', g:pencil#cursorwrap)
set whichwrap+=<,>,b,s,h,l,[,]
aug pencil_cursorwrap
au BufEnter <buffer> set virtualedit+=onemore
@@ -197,7 +197,7 @@ fun! pencil#init(...) abort
en
" window/buffer settings
if b:wrap_mode
if b:pencil_wrap_mode
setl nolist
setl wrapmargin=0
setl autoindent " needed by formatoptions=n
@@ -236,7 +236,7 @@ fun! pencil#init(...) abort
en
en
if b:wrap_mode == s:WRAP_MODE_SOFT
if b:pencil_wrap_mode ==# s:WRAP_MODE_SOFT
nn <buffer> <silent> $ g$
nn <buffer> <silent> 0 g0
vn <buffer> <silent> $ g$
@@ -258,7 +258,7 @@ fun! pencil#init(...) abort
sil! iu <buffer> <End>
en
if b:wrap_mode
if b:pencil_wrap_mode
nn <buffer> <silent> j gj
nn <buffer> <silent> k gk
vn <buffer> <silent> j gj
@@ -283,7 +283,7 @@ fun! pencil#init(...) abort
" set undo points around common punctuation,
" line <c-u> and word <c-w> deletions
if b:wrap_mode
if b:pencil_wrap_mode
ino <buffer> . .<c-g>u
ino <buffer> ! !<c-g>u
ino <buffer> ? ?<c-g>u

View File

@@ -13,6 +13,33 @@ let g:loaded_pencil = 1
let s:save_cpo = &cpo
set cpo&vim
let s:WRAP_MODE_DEFAULT = -1
let s:WRAP_MODE_OFF = 0
let s:WRAP_MODE_HARD = 1
let s:WRAP_MODE_SOFT = 2
fun! s:unicode_enabled()
retu &encoding ==# 'utf-8'
endf
" helper for statusline
"
" Note that it shouldn't be dependent on init(), which
" won't have been called for non-prose modules.
fun! PencilMode()
if exists('b:pencil_wrap_mode')
if b:pencil_wrap_mode ==# s:WRAP_MODE_SOFT
return get(g:pencil#mode_indicators, 'soft', 'sp')
elsei b:pencil_wrap_mode ==# s:WRAP_MODE_HARD
return get(g:pencil#mode_indicators, 'hard', 'hp')
el
return get(g:pencil#mode_indicators, 'off', '')
en
else
return ''
en
endf
if !exists('g:pencil#wrapModeDefault')
" user-overridable default, if detection fails
" should be 'soft' or 'hard' or 'off'
@@ -92,6 +119,15 @@ if !exists('g:pencil#softDetectThreshold')
let g:pencil#softDetectThreshold = 130
en
if !exists('g:pencil#mode_indicators')
" used to set PencilMode() for statusline
if s:unicode_enabled()
let g:pencil#mode_indicators = {'hard': 'h✎ ', 'soft': 's✎ ', 'off': '×✎ ',}
el
let g:pencil#mode_indicators = {'hard': 'hp', 'soft': 'sp', 'off': '',}
en
en
" # coms
com -nargs=0 HardPencil call pencil#init({'wrap': 'hard'})
com -nargs=0 SoftPencil call pencil#init({'wrap': 'soft'})