Skip to content

Commit f5726c4

Browse files
Cimbalintpeters
authored andcommitted
Refactor (#100)
* Allow per-buffer toggling to override global Fixes #98 * Respect global choice in current detection * Refactor for readability/maintanability Also addresses issues raised in #98. Reorder code, organise in sections. - Simplify utility functions: only keep InitVariable, remove its :exe - Whitespace patterns and highlighting mechanisms defined once - Prefs current_line_whitespace_disabled_* can be set only when loading the plugin (*), as this allows a lot of simplification. Clarify variables use/responsability. - Set buffer-local settings b:better_whitespace_enabled and b:strip_whitespace_on_save *only* when Enable/Disable/Toggle is called, or when the determination based on the filetype can be made. - Change SetupAutoCommands to be the only function to be called by: a) checking the buffer-local setting b) taking responsability for clearing the highlighting * Update docs to mirror refactor, fix #91 collisions * Fix #92: Correctly re-init on colorscheme change Bug was caused by snyIDattr() returning a string, empty when the highlight group is undefined, rather than an int with -1 for undefined. * Add mechanism for confirming whitespace stripping Based on PR #69 by @advocateddrummer Also make sure that EOF stripping of empty lines is only done when the EOF is in the given range. * Add option to only strip whitespace on modified lines Diff-based technique to close #38 * Add option to disable stripping-on-save on large files Using the number of lines in the file, based on PR #97 by @kurikomoe Also fix docs. * Adjust documentations * Check if file exists instead of file name non-emtpy Files that were created in vim printed errors on first save. E.g.: :tabe new_file :w * Fix typo in doc/better-whitespace.txt Co-Authored-By: Cimbali <me@cimba.li> * Fix whitespace search to also match current line Co-Authored-By: Cimbali <me@cimba.li> * Correct missing sentence in doc * Clarify ShouldStripWhitespace role, call diff less Use &modified and &modifiable to skip diff and disable strippping (by default) whitespace on save. * Fix incorrect diff for last line of file Appending "\n" to joined buffer lines allows to diff until EOF. Co-Authored-By: Nate Peterson <ntpeters@mtu.edu> * Make max lines more explicit and default to 1000 * Make a command to manually strip lines on changed lines * Remove unused bangs from commands Bangs are only used of `:w` for now * Show errors to user, except E486: Pattern not found That error is the only legitimate one, but is already ignored with the e flag on :s, see :help s_e * Add error for &noro, respect range for changed lines Make commands aware of &readonly and show proper errors. Also improve StripWhitespaceOnChangedLines to take into account the range it is passed. The range to defaults to % which means no different behaviour. With any other range, only perform the stripping on the intersection of the changed lines and that range. * Add a deprecation warning about in the doc * Add error number so people can look the error up The explanation says (emphasis mine): You tried to execute a command that is neither an Ex command nor *a user-defined command*.
1 parent 70a38fa commit f5726c4

File tree

3 files changed

+426
-287
lines changed

3 files changed

+426
-287
lines changed

README.md

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,21 @@ Whitespace highlighting is enabled by default, with a highlight color of red.
6060
:ToggleWhitespace
6161
```
6262
63-
* To disable highlighting for the current line in normal mode call:
64-
```vim
65-
:CurrentLineWhitespaceOff <level>
66-
```
67-
Where `<level>` is either `hard` or `soft`.
68-
69-
* The level `hard` will maintain whitespace highlighting as it is, but may
70-
cause a slow down in Vim since it uses the CursorMoved event to detect and
63+
* The highlighting for the current line in normal mode can be disabled in two ways:
64+
* ```vim
65+
let g:current_line_whitespace_disabled_hard=1
66+
```
67+
This will maintain whitespace highlighting as it is, but may cause a
68+
slow down in Vim since it uses the CursorMoved event to detect and
7169
exclude the current line.
7270
73-
* The level `soft` will use syntax based highlighting, so there shouldn't be
74-
a performance hit like with the `hard` option. The drawback is that this
75-
highlighting will have a lower priority and may be overwritten by higher
76-
priority highlighting.
77-
78-
* To re-enable highlighting for the current line in normal mode:
79-
```vim
80-
:CurrentLineWhitespaceOn
81-
```
71+
* ```vim
72+
let g:current_line_whitespace_disabled_soft=1
73+
```
74+
This will use syntax based highlighting, so there shouldn't be a
75+
performance hit like with the `hard` option. The drawback is that this
76+
highlighting will have a lower priority and may be overwritten by higher
77+
priority highlighting.
8278
8379
* To clean extra whitespace, call:
8480
```vim
@@ -128,6 +124,30 @@ Whitespace highlighting is enabled by default, with a highlight color of red.
128124
where `<desired_filetypes>` is a comma separated list of the file types you want
129125
to be stripped of whitespace on file save ( ie. `javascript,c,cpp,java,html,ruby` )
130126
127+
* If you want to disable automatically stripping whitespace for large files, you can specify
128+
a maximum number of lines (e.g. 1000) by adding the following to your `~/.vimrc`:
129+
130+
```vim
131+
let g:strip_max_file_size = 1000
132+
```
133+
134+
This overrides `let g:strip_whitespace_on_save` but not `:EnableStripWhitespaceOnSave`.
135+
Set to `0` to deactivate.
136+
137+
* By default, you will be asked for confirmation before whitespace is
138+
stripped when you save the file. This can be disabled by adding the
139+
following to your `~/.vimrc`:
140+
```
141+
let g:strip_whitespace_confirm=0
142+
```
143+
144+
* By default, all the lines in the file will have their trailing whitespace stripped
145+
when you save the file. This can be changed to only the modified lines, by adding
146+
the following to your `~/.vimrc`:
147+
```
148+
let g:strip_only_modified_lines=0
149+
```
150+
131151
* To disable this plugin for specific file types, add the following to your `~/.vimrc`:
132152
```vim
133153
let g:better_whitespace_filetypes_blacklist=['<filetype1>', '<filetype2>', '<etc>']
@@ -300,6 +320,43 @@ A: If you know of a better way to do something I am attempting in this plugin,
300320
me or make the changes yourself and open a pull request. If I am doing something that is bad
301321
or can be improved, I am more than willing to hear about it!
302322
323+
## Deprecated commands
324+
Toggling the current line whitespace mode is now a plugin configuration,
325+
and can not be done dynamically anymore. Thus the folowing commands are now deprecated:
326+
327+
```vim
328+
:CurrentLineWhitespaceOff <level>
329+
```
330+
where `<level>` is either `hard` or `soft`, and:
331+
332+
```vim
333+
:CurrentLineWhitespaceOn
334+
```
335+
336+
If you really miss this feature, its withdrawal can easily be overriden
337+
by adding the following to the vimrc (after loading the plugin initially):
338+
339+
```vim
340+
fun! BetterWhitespaceCurrentLineMode(type)
341+
" set setting to whatever was passed
342+
let g:current_line_whitespace_disabled_soft=a:type == 'soft'
343+
let g:current_line_whitespace_disabled_hard=a:type == 'hard'
344+
" reload plugin
345+
unlet! g:loaded_better_whitespace_plugin
346+
runtime plugin/better-whitespace.vim
347+
" Re-override the deprecated commands
348+
command! -nargs=1 CurrentLineWhitespaceOff call BetterWhitespaceCurrentLineMode(<f-args>)
349+
command! CurrentLineWhitespaceOn call BetterWhitespaceCurrentLineMode('off')
350+
" Manually trigger change for current buffer.
351+
" BufWinEnter will take care of the rest.
352+
filetype detect
353+
endfun
354+
355+
" Override deprecated commands, after (!) loading plugin
356+
command! -nargs=1 CurrentLineWhitespaceOff call BetterWhitespaceCurrentLineMode(<f-args>)
357+
command! CurrentLineWhitespaceOn call BetterWhitespaceCurrentLineMode('off')
358+
```
359+
303360
## Promotion
304361
If you like this plugin, please star it on Github and vote it up at Vim.org!
305362

doc/better-whitespace.txt

Lines changed: 91 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,61 +9,120 @@ modes as well (see options below).
99
To blacklist certain filetypes, set 'g:better_whitespace_filetypes_blacklist'
1010
to a list of the desired filetypes.
1111

12+
COMMANDS *better-whitespace-commands*
1213

13-
*ToggleWhitespace*
14+
:ToggleWhitespace *better-whitespace-:ToggleWhitespace*
15+
:EnableWhitespace *better-whitespace-:EnableWhitespace*
16+
:DisableWhitespace *better-whitespace-:DisableWhitespace*
1417

15-
Call :ToggleWhitespace to toggle whitespace highlighting on/off.
18+
Call these functions to toggle whitespace highlighting on/off.
19+
This will set the highlighting of extraneous whitespace for the entire file.
1620

17-
*EnableWhitespace*
18-
19-
To enable whitespace highlighting, call :EnableWhitespace. This will enable
21+
To disable whitespace highlighting, call :DisableWhitespace. This will disable
2022
highlighting of extraneous whitespace for the entire file.
2123

22-
*DisableWhitespace*
24+
:[range]NextTrailingWhitespace *better-whitespace-:NextTrailingWhitespace*
25+
:[range]PrevTrailingWhitespace *better-whitespace-:PrevTrailingWhitespace*
2326

24-
To disable whitespace highlighting, call :DisableWhitespace. This will disable
25-
highlighting of extraneous whitespace for the entire file.
27+
These functions will respectively take you to the next and previous trailing whitespace
28+
in the ranged that they are passed (by default the whole file).
29+
30+
:[range]StripWhitespace[!] *better-whitespace-:StripWhitespace*
31+
32+
Removes extra whitespace, by default on the entire file. To restrict the
33+
portion of the file that it cleans, either give it a range or select a group
34+
of lines in visual mode and then execute it. With the !, ignores &readonly.
2635

27-
*StripWhitespace*
36+
:[range]StripWhitespaceOnChangedLines[!] *better-whitespace-:StripWhitespaceOnChangedLines*
2837

29-
To remove extra whitespace, just call :StripWhitespace.By default this
30-
operates on the entire file. To restrict the portion of the file that it
31-
cleans, either give it a range or select a group of lines in visual mode
32-
and then execute it.
38+
Like :StripWhitespace, but only on the lines that are modified.
3339

34-
*ToggleStripWhitespaceOnSave*
40+
:ToggleStripWhitespaceOnSave *better-whitespace-:ToggleStripWhitespaceOnSave*
41+
:EnableStripWhitespaceOnSave *better-whitespace-:EnableStripWhitespaceOnSave*
42+
:DisableStripWhitespaceOnSave *better-whitespace-:DisableStripWhitespaceOnSave*
3543

3644
This enables/disables stripping of extra whitespace on file save.
45+
If `g:strip_only_modified_lines` is set to 1, only modified lines will be stripped.
3746

38-
*CurrentLineWhitespaceOff*
3947

40-
Calling :CurrentLineWhitespaceOff with the option either 'hard' or 'soft' will
41-
disable whitespace highlighting for the current line.
48+
DEPRECATED: :CurrentLineWhitespaceOn and :CurrentLineWhitespaceOff with the
49+
option either 'hard' or 'soft' are now deprecated and replaced by the
50+
following preferences:
51+
`g:current_line_whitespace_disabled_hard`
52+
`g:current_line_whitespace_disabled_soft`
4253

43-
If 'hard' option is used, then all highlighting remains the same except that
44-
the current line is not highlighted.
45-
WARNING: Since this uses CursorMoved events to prevent highlighting of the
46-
current line there is a chance this can cause a significant slow down of Vim,
47-
especially with regard to large files.
4854

49-
If the 'soft' option is used, then highlighting will have a lower priority
50-
potentially causing this highlighting to be overwritten by other, higher
51-
priority highlighting. The benefit is that there should be no slowdown with
52-
this option.
5355

54-
*CurrentLineWhitespaceOn
56+
PREFERENCES *better-whitespace-preferences*
5557

56-
Call :CurrentLineWhitespaceOn to enable whitespace highlighting for the current
57-
line. Highlighting is still disabled for the current line while in insert mode.
58+
`g:better_whitespace_enabled` (defaults to 1)
59+
Set this to enable whitespace highlighting by default, set to 0 to disable it.
60+
61+
`g:better_whitespace_filetypes_blacklist` (defaults to ['diff', 'gitcommit',
62+
'unite', 'qf', 'help', 'markdown'])
63+
Disables better-whitespace by default on these file types.
64+
Overrides `g:better_whitespace_enabled`, and can be manually overriden with
65+
|better-whitespace-:EnableWhitespace| and related commands.
66+
67+
`g:strip_only_modified_lines` (defaults to 0)
68+
When stripping whitespace on save, only perform the stripping on the lines
69+
that have been modified.
70+
Uses `diff`, which should be available on all platforms (see |E810|).
71+
72+
`g:strip_max_file_size` (defaults to 1000)
73+
Skip stripping whitespace on files that have more lines than the value in this
74+
variable. 0 means disabled, thus strip for any file size. This can be manually
75+
overriden with |better-whitespace-:EnableStripWhitespaceOnSave| and related commands.
76+
77+
`g:better_whitespace_operator` *better-whitespace-operator*
5878

59-
*operator*
6079
By default, an operator is provided mapped to: <leader>s.
61-
To modify the key mapping for this operator, set g:better_whitespace_operator.
80+
To modify the key mapping for this operator, set `g:better_whitespace_operator`.
6281
This operator will strip whitespace over the currently selected region in visual
63-
mode, or for the following motion in normal mode.
82+
mode, or for the following motion in normal mode. Set an empty value to disable.
83+
6484
Note: This operator will not be mapped if an existing, user-defined mapping is
6585
detected for the provided operator value.
6686

87+
*better-whitespace-colors*
88+
Set the highlight color for trailing whitespaces:
89+
`g:better_whitespace_ctermcolor` (defaults to 'red')
90+
`g:better_whitespace_guicolor` (defaults to '#FF0000')
91+
92+
*better-whitespace-current_line*
93+
`g:current_line_whitespace_disabled_hard` (defaults to 0)
94+
Set this to disable highlighting on the current line in all modes
95+
WARNING: This checks for current line on cursor move, which can significantly
96+
impact the performance of Vim (especially on large files)
97+
WARNING: Ignored if g:current_line_whitespace_disabled_soft is set.
98+
99+
`g:current_line_whitespace_disabled_soft` (defaults to 0)
100+
Set this to disable highlighting of the current line in all modes
101+
This setting will not have the performance impact of the above, but
102+
highlighting throughout the file may be overridden by other highlight
103+
patterns with higher priority.
104+
105+
106+
`g:strip_whitespace_confirm` (defaults to 1)
107+
Set to 0 to deactivate asking for confirmation before whitespace is
108+
stripped when you save the file.
109+
110+
`g:show_spaces_that_precede_tabs` (defaults to 0)
111+
Set this to match space characters that appear before or in-between tabs
112+
113+
`g:strip_whitespace_on_save` (defaults to 0)
114+
Set this to enable stripping whitespace on file save for files where
115+
better-whitespace is enabled.
116+
117+
`g:strip_whitelines_at_eof` (defaults to 0)
118+
Set this to enable stripping white lines at the end of the file when we
119+
strip whitespace from the end of lines.
120+
121+
`g:better_whitespace_skip_empty_lines` (defaults to 0)
122+
Skips empty (whitespace-only) lines for highlighting.
123+
124+
125+
67126
Repository exists at: http://github.com/ntpeters/vim-better-whitespace
68127

69128
Originally inspired by: https://github.com/bronson/vim-trailing-whitespace

0 commit comments

Comments
 (0)