blob: 31a72de3f663f506c58fc7ff293553c6a89f2f90 [file] [log] [blame]
Luca Stefani017aea32016-11-24 00:06:17 +01001" vim600: set foldmethod=marker:
2"
3" Version: 1.4
4" Description: Per buffer, togglable syntax highlighting of tabs and trailing
5" spaces.
6" Author: Adam Lazur <adam@lazur.org>, extended by Jonathan Palardy
7" URL: https://github.com/jpalardy/spacehi.vim
8" License: Redistribution and use of this file, with or without
9" modification, are permitted without restriction.
10"
11" Section: Documentation {{{1
12"
13" This plugin will highlight tabs, nbsps and trailing spaces on a line, with the
14" ability to toggle the highlighting on and off. Using highlighting to
15" illuminate these characters is preferrable to using listchars and set list
16" because it allows you to copy from the vim window without getting shrapnel
17" in your buffer.
18"
19" NOTE: "set list" will override SpaceHi's highlighting.
20"
21" Highlighting can be turned on and off with the functions SpaceHi() and
22" NoSpaceHi() respectively. You can also toggle the highlighting state by
23" using ToggleSpaceHi(). By default, ToggleSpaceHi is bound to the key F3.
24"
25" You can customize the colors by setting the following variables to a string
26" of key=val that would normally follow "highlight group" command:
27"
28" g:spacehi_spacecolor
29" g:spacehi_tabcolor
30" g:spacehi_nbspcolor
31"
32" The defaults can be found in the "Default Global Vars" section below.
33"
34" You can give a list of filetypes to exclude
35"
36" If you want to highlight tabs and trailing spaces by default for every file
37" that is syntax highlighted, you can add the following to your vimrc:
38"
39" autocmd syntax * SpaceHi
40"
41" The author currently uses the following (a little overboard) in his vimrc:
42"
43" autocmd BufNewFile,BufReadPost,FilterReadPost,FileReadPost,Syntax * SpaceHi
44" au FileType help NoSpaceHi
45
46" Section: Plugin header {{{1
47" If we have already loaded this file, don't load it again.
48if exists("loaded_spacehi")
49 finish
50endif
51let loaded_spacehi=1
52
53" Section: Default Global Vars {{{1
54if !exists("g:spacehi_tabcolor")
55 " highlight tabs with red underline
56 let g:spacehi_tabcolor="ctermfg=White ctermbg=Red guifg=White guibg=Red"
57endif
58if !exists("g:spacehi_spacecolor")
59 " highlight trailing spaces in blue underline
60 let g:spacehi_spacecolor="ctermfg=Black ctermbg=Yellow guifg=Blue guibg=Yellow"
61endif
62if !exists("g:spacehi_nbspcolor")
63 " highlight nbsps with red underline
64 let g:spacehi_nbspcolor="ctermfg=White ctermbg=Red guifg=White guibg=Red"
65endif
66
67" Section: Functions {{{1
68" Function: s:SpaceHi() {{{2
69" Turn on highlighting of spaces and tabs
70function! s:SpaceHi()
71 " highlight tabs
72 syntax match spacehiTab /\t/ containedin=ALL
73 execute("highlight spacehiTab " . g:spacehi_tabcolor)
74
75 " highlight trailing spaces
76 syntax match spacehiTrailingSpace /\s\+$/ containedin=ALL
77 execute("highlight spacehiTrailingSpace " . g:spacehi_spacecolor)
78
79 " highlight nbsps
80 syntax match spacehiNbsp /\%d160/ containedin=ALL
81 execute("highlight spacehiNbsp " . g:spacehi_nbspcolor)
82
83 let b:spacehi = 1
84endfunction
85
86" Function: s:NoSpaceHi() {{{2
87" Turn off highlighting of spaces and tabs
88function! s:NoSpaceHi()
89 syntax clear spacehiTab
90 syntax clear spacehiTrailingSpace
91 syntax clear spacehiNbsp
92 let b:spacehi = 0
93endfunction
94
95" Function: s:ToggleSpaceHi() {{{2
96" Toggle highlighting of spaces and tabs
97function! s:ToggleSpaceHi()
98 if exists("b:spacehi") && b:spacehi
99 call s:NoSpaceHi()
100 echo "spacehi off"
101 else
102 call s:SpaceHi()
103 echo "spacehi on"
104 endif
105endfunction
106
107" Section: Commands {{{1
108com! SpaceHi call s:SpaceHi()
109com! NoSpaceHi call s:NoSpaceHi()
110com! ToggleSpaceHi call s:ToggleSpaceHi()
111
112" Section: Default mappings {{{1
113" Only insert a map to ToggleSpaceHi if they don't already have a map to
114" the function and don't have something bound to F3
115if !hasmapto('ToggleSpaceHi') && maparg("<F3>") == ""
116 map <silent> <unique> <F3> :ToggleSpaceHi<CR>
117endif