clarified globals; protect other bufs from ve=onemore; misc improvements

This commit is contained in:
Reed Esau
2014-02-02 18:18:16 -07:00
parent daf4183a42
commit 5b57c3b06c
2 changed files with 55 additions and 50 deletions

View File

@@ -12,15 +12,17 @@
The _pencil_ plugin aspires to make Vim as powerful a tool for writers as 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 it is for coders by focusing narrowly on the handful of tweaks needed to
smooth the path to writing prose. smooth the path to writing prose.
* For editing files in _text_, _markdown_, _textile_, and other prose-oriented file types * For editing files in _text_, _markdown_, _textile_, and other
prose-oriented file types
* Agnostic on soft line wrap _versus_ hard line breaks, supporting both * Agnostic on soft line wrap _versus_ hard line breaks, supporting both
* Auto-detects wrap mode via modeline and sampling * Auto-detects wrap mode via modeline and sampling
* Adjusts navigation key mappings to suit the wrap mode * Adjusts navigation key mappings to suit the wrap mode
* Creates undo points on common punctuation * Creates undo points on common punctuation
* When using hard line breaks, enables autoformat while inserting text * When using hard line breaks, enables autoformat while inserting text
* Buffer-scoped configuration (leaves your global settings alone) * Buffer-scoped configuration (with a few minor exceptions, _pencil_
preserves your global settings)
* Pure Vimscript with no dependencies * Pure Vimscript with no dependencies
Need spell-check and other features? Vim is about customization. To Need spell-check and other features? Vim is about customization. To
@@ -29,15 +31,15 @@ its rich ecosystem of plugins.
## Why use Vim for writing? ## Why use Vim for writing?
With plenty of word processing apps available, including those that With plenty of word processing applications available, including those
specifically cater to writers, why use a programmers editor like Vim for that specifically cater to writers, why use a programmers editor like Vim
writing? for writing?
There are good reasons NOT to use Vim for writing: There are good reasons NOT to use Vim for writing:
* Primitive in certain respects (no WYSIWYG or proportionally spaced * Primitive in certain respects (no WYSIWYG or proportionally spaced
characters, e.g.) characters, e.g.)
* A modal editor with a steep learning curve * A modal editor with a relatively steep learning curve
* Time and effort to configure to your needs * Time and effort to configure to your needs
But Vim offers a unique editing environment not matched by other writing But Vim offers a unique editing environment not matched by other writing
@@ -54,9 +56,9 @@ tools:
Install using Pathogen, Vundle, Neobundle, or your favorite Vim package Install using Pathogen, Vundle, Neobundle, or your favorite Vim package
manager. manager.
_For those new to Vim: before installing this plugin, you should first _For those new to Vim: before installing this plugin, consider getting
work through one of the many tutorials available to get comfortable with comfortable with the basics of Vim by working through one of the many
the basics of Vim._ tutorials available._
## Configuration ## Configuration
@@ -71,7 +73,7 @@ In most cases you can set a default to suit your preference and let
auto-detection figure out what to do. Add to your `.vimrc`: auto-detection figure out what to do. Add to your `.vimrc`:
```vim ```vim
" standard vim command to enable loading the plugin files " standard vim command to enable loading the plugin files
" (and their indent support) for specific file types. " (and their indent support) for specific file types.
" It may already be in your .vimrc! " It may already be in your .vimrc!
filetype plugin indent on filetype plugin indent on
@@ -99,7 +101,7 @@ to set the behavior for the current buffer:
* `SoftPencil` - enable soft line wrap mode * `SoftPencil` - enable soft line wrap mode
* `HardPencil` - enable hard line break mode * `HardPencil` - enable hard line break mode
* `DropPencil` - removes navigation mappings and restores buffer to global settings * `DropPencil` - removes navigation mappings and restores buffer to global settings
* `TogglePencil` - if on, turns off; if off, enables with detection * `TogglePencil` - if on, turns off; if off, enables with detection
Optionally, you can map to keys in your `.vimrc`: Optionally, you can map to keys in your `.vimrc`:
@@ -161,7 +163,7 @@ reformat paragraphs with standard Vim commands:
* `ggVGgq` or `:g/^/norm gqq` - format all paragraphs in buffer * `ggVGgq` or `:g/^/norm gqq` - format all paragraphs in buffer
* `:%norm vipJ` - unformat all paragraphs in buffer * `:%norm vipJ` - unformat all paragraphs in buffer
Optionally, you can map these sequences to underutilized keys in your Optionally, you can map these operations to underutilized keys in your
`.vimrc`: `.vimrc`:
```vim ```vim
@@ -204,7 +206,7 @@ let g:pencil#cursorwrap = 1 " 0=disable, 1=enable
## Auto-detecting wrap mode ## Auto-detecting wrap mode
If you didn't explicitly specify a wrap mode during initialization, If you didn't explicitly specify a wrap mode during initialization,
_pencil_ will attempt to detect it. _pencil_ will attempt to detect it.
It will first look for a `textwidth` (or `tw`) specified in a modeline. It will first look for a `textwidth` (or `tw`) specified in a modeline.
@@ -221,14 +223,14 @@ At the bottom of this document is a odd-looking code:
<!-- vim: set tw=74 :--> <!-- vim: set tw=74 :-->
``` ```
This is an optional modeline that tells Vim to run the following command This is an **optional** modeline that tells Vim to run the following
upon loading the file into a buffer: command upon loading the file into a buffer:
```vim ```vim
:set textwidth=74 :set textwidth=74
``` ```
It tells _pencil_ to assume hard line breaks, regardless of whether or It tells _pencil_ to assume hard line breaks, regardless of whether or
not soft line wrap is the default editing mode for files of type markdown. not soft line wrap is the default editing mode for files of type markdown.
You explicitly specify soft wrap mode by specifying a textwidth of `0`: You explicitly specify soft wrap mode by specifying a textwidth of `0`:
@@ -256,7 +258,7 @@ let g:pencil#softDetectSample = 20
Set that value to `0` to disable detection via line sampling. Set that value to `0` to disable detection via line sampling.
When the number of bytes on a sampled line per exceeds this next value, When the number of bytes on a sampled line per exceeds this next value,
then _pencil_ assumes soft line wrap. then _pencil_ assumes soft line wrap.
```vim ```vim
let g:pencil#softDetectThreshold = 130 let g:pencil#softDetectThreshold = 130
@@ -284,9 +286,9 @@ If you find this plugin useful, you may want to check out these others by
* [vim-litecorrect][lc] - lightweight auto-correction for Vim * [vim-litecorrect][lc] - lightweight auto-correction for Vim
* [vim-quotable][qu] - extends Vim to support typographic (curly) quotes * [vim-quotable][qu] - extends Vim to support typographic (curly) quotes
* [vim-textobj-sentence][ts] - improving on Vim's native sentence motion command * [vim-textobj-sentence][ts] - improving on Vim's native sentence motion command
* [vim-thematic][th] — modify Vims appearance to suit your task and environment * [vim-thematic][th] — modify Vims appearance to suit your task and environment
* [vim-wheel][wh] - screen-anchored cursor movement for Vim * [vim-wheel][wh] - screen-anchored cursor movement for Vim
* [vim-wordy][wo] - uncovering usage problems in writing * [vim-wordy][wo] - uncovering usage problems in writing
[re]: http://github.com/reedes [re]: http://github.com/reedes
[cp]: http://github.com/reedes/vim-colors-pencil [cp]: http://github.com/reedes/vim-colors-pencil

View File

@@ -78,7 +78,7 @@ function! pencil#init(...) abort
let l:wrap_arg = get(l:args, 'wrap', 'detect') let l:wrap_arg = get(l:args, 'wrap', 'detect')
if (b:wrap_mode && l:wrap_arg ==# 'toggle') || if (b:wrap_mode && l:wrap_arg ==# 'toggle') ||
\ l:wrap_arg =~# '^\(off\|disable\|false\)$' \ l:wrap_arg =~# '^\(0\|off\|disable\|false\)$'
let b:wrap_mode = s:WRAP_MODE_OFF let b:wrap_mode = s:WRAP_MODE_OFF
elseif l:wrap_arg ==# 'hard' elseif l:wrap_arg ==# 'hard'
let b:wrap_mode = s:WRAP_MODE_HARD let b:wrap_mode = s:WRAP_MODE_HARD
@@ -93,7 +93,7 @@ function! pencil#init(...) abort
" translate default(-1) to soft(1) or hard(2) or off(0) " translate default(-1) to soft(1) or hard(2) or off(0)
if b:wrap_mode == s:WRAP_MODE_DEFAULT if b:wrap_mode == s:WRAP_MODE_DEFAULT
if g:pencil#wrapModeDefault =~# '^\(off\|disable\|false\)$' if g:pencil#wrapModeDefault =~# '^\(0\|off\|disable\|false\)$'
let b:wrap_mode = s:WRAP_MODE_OFF let b:wrap_mode = s:WRAP_MODE_OFF
elseif g:pencil#wrapModeDefault ==# 'soft' elseif g:pencil#wrapModeDefault ==# 'soft'
let b:wrap_mode = s:WRAP_MODE_SOFT let b:wrap_mode = s:WRAP_MODE_SOFT
@@ -130,45 +130,48 @@ function! pencil#init(...) abort
setlocal colorcolumn< setlocal colorcolumn<
endif endif
" global settings
if b:wrap_mode
set display+=lastline
set backspace=indent,eol,start " via tpope/vim-sensible
if g:pencil#joinspaces
set joinspaces " two spaces after .!?
else
set nojoinspaces " only one space after a .!? (default)
endif
endif
" 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+=<,>,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
" window/buffer settings
if b:wrap_mode if b:wrap_mode
setlocal autoindent " needed by fo=n
setlocal nolist setlocal nolist
setlocal wrapmargin=0 setlocal wrapmargin=0
setlocal display+=lastline setlocal autoindent " needed by formatoptions=n
setlocal formatoptions+=1 " don't break line before 1 letter word
setlocal formatoptions+=t
setlocal formatoptions+=n " recognize numbered lists setlocal formatoptions+=n " recognize numbered lists
setlocal formatoptions+=1 " don't break line before 1 letter word
if g:pencil#cursorwrap setlocal formatoptions+=t " autoformat of text, but not comments
setlocal whichwrap+=<,>,h,l,[,]
setlocal virtualedit+=onemore " could break other plugins
endif
" clean out stuff we likely don't want " clean out stuff we likely don't want
setlocal formatoptions-=2 setlocal formatoptions-=2 " use indent of 2nd line for rest of paragraph
setlocal formatoptions-=v setlocal formatoptions-=v " only break line at blank entered during insert
setlocal formatoptions-=w " avoid erratic behavior if mixed spaces
" trailing whitespace continues paragraph
" makes autoformat behave oddly where spaces aren't present
setlocal formatoptions-=w
else else
setlocal autoindent< noautoindent< setlocal autoindent< noautoindent<
setlocal list< nolist< setlocal list< nolist<
setlocal wrapmargin< setlocal wrapmargin<
setlocal display<
setlocal formatoptions< setlocal formatoptions<
setlocal whichwrap<
setlocal virtualedit<
endif
if b:wrap_mode
if g:pencil#joinspaces
setlocal joinspaces " two spaces after .!?
else
setlocal nojoinspaces " only one space after a .!? (default)
endif
else
setlocal joinspaces< nojoinspaces<
endif endif
if b:wrap_mode == s:WRAP_MODE_SOFT if b:wrap_mode == s:WRAP_MODE_SOFT