This repository has been archived by the owner on Jul 29, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 222
/
indentLine.vim
458 lines (389 loc) · 15.6 KB
/
indentLine.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
" Script Name: indentLine.vim
" Author: Yggdroot <archofortune@gmail.com>
"
" Description: To show the indention levels with thin vertical lines
scriptencoding utf-8
if !has("conceal")
echoerr "conceal is invalid. To use the plugin, please enable the conceal"
finish
endif
if exists("g:indentLine_loaded")
finish
endif
let g:indentLine_loaded = 1
let g:indentLine_newVersion = get(g:,'indentLine_newVersion',v:version > 704 || v:version == 704 && has("patch792"))
let g:indentLine_char = get(g:, 'indentLine_char', (&encoding ==# "utf-8" && &term isnot# "linux" ? '¦' : '|'))
let g:indentLine_char_list = get(g:, 'indentLine_char_list', [])
let g:indentLine_first_char = get(g:, 'indentLine_first_char', (&encoding ==# "utf-8" && &term isnot# "linux" ? '¦' : '|'))
let g:indentLine_indentLevel = get(g:, 'indentLine_indentLevel', 20)
let g:indentLine_enabled = get(g:, 'indentLine_enabled', 1)
let g:indentLine_fileType = get(g:, 'indentLine_fileType', [])
let g:indentLine_fileTypeExclude = get(g:, 'indentLine_fileTypeExclude', ['leaderf'])
let g:indentLine_bufNameExclude = get(g:, 'indentLine_bufNameExclude', [])
let g:indentLine_bufTypeExclude = get(g:, 'indentLine_bufTypeExclude', [])
let g:indentLine_showFirstIndentLevel = get(g:, 'indentLine_showFirstIndentLevel', 0)
let g:indentLine_maxLines = get(g:, 'indentLine_maxLines', 3000)
let g:indentLine_setColors = get(g:, 'indentLine_setColors', 1)
let g:indentLine_setConceal = get(g:, 'indentLine_setConceal', 1)
let g:indentLine_defaultGroup = get(g:, 'indentLine_defaultGroup', "")
let g:indentLine_faster = get(g:, 'indentLine_faster', 0)
let g:indentLine_leadingSpaceChar = get(g:, 'indentLine_leadingSpaceChar', (&encoding ==# "utf-8" && &term isnot# "linux" ? '˰' : '.'))
let g:indentLine_leadingSpaceEnabled = get(g:, 'indentLine_leadingSpaceEnabled', 0)
let g:indentLine_mysyntaxfile = fnamemodify(expand("<sfile>"), ":p:h:h")."/syntax/indentLine.vim"
"{{{1 function! s:InitColor()
function! s:InitColor()
if !g:indentLine_setColors
return
endif
let default_term_bg = "NONE"
let default_gui_bg = "NONE"
if &background ==# "light"
let default_term_fg = 249
let default_gui_fg = "Grey70"
else
let default_term_fg = 239
let default_gui_fg = "Grey30"
endif
if g:indentLine_defaultGroup != ""
let default_id = synIDtrans(hlID(g:indentLine_defaultGroup))
let default_term_fg = synIDattr(default_id, "fg", "cterm") == "" ? default_term_fg : synIDattr(default_id, "fg", "cterm")
let default_term_bg = synIDattr(default_id, "bg", "cterm") == "" ? default_term_bg : synIDattr(default_id, "bg", "cterm")
let default_gui_fg = synIDattr(default_id, "fg", "gui") == "" ? default_gui_fg : synIDattr(default_id, "fg", "gui")
let default_gui_bg = synIDattr(default_id, "bg", "gui") == "" ? default_gui_bg : synIDattr(default_id, "bg", "gui")
endif
if !exists("g:indentLine_color_term")
let term_color = default_term_fg
else
let term_color = g:indentLine_color_term
endif
if !exists("g:indentLine_bgcolor_term")
let term_bgcolor = default_term_bg
else
let term_bgcolor = g:indentLine_bgcolor_term
endif
if !exists("g:indentLine_color_gui")
let gui_color = default_gui_fg
else
let gui_color = g:indentLine_color_gui
endif
if !exists("g:indentLine_bgcolor_gui")
let gui_bgcolor = default_gui_bg
else
let gui_bgcolor = g:indentLine_bgcolor_gui
endif
execute "highlight Conceal cterm=NONE ctermfg=" . term_color . " ctermbg=" . term_bgcolor
execute "highlight Conceal gui=NONE guifg=" . gui_color . " guibg=" . gui_bgcolor
if &term ==# "linux"
if &background ==# "light"
let tty_color = exists("g:indentLine_color_tty_light") ? g:indentLine_color_tty_light : 4
else
let tty_color = exists("g:indentLine_color_tty_dark") ? g:indentLine_color_tty_dark : 2
endif
execute "highlight Conceal cterm=bold ctermfg=" . tty_color . " ctermbg=NONE"
endif
endfunction
"{{{1 function! s:SetConcealOption()
function! s:SetConcealOption()
if !g:indentLine_setConceal
return
endif
if !(exists("b:indentLine_ConcealOptionSet") && b:indentLine_ConcealOptionSet)
let b:indentLine_ConcealOptionSet = 1
let b:indentLine_original_concealcursor = &l:concealcursor
let b:indentLine_original_conceallevel = &l:conceallevel
let &l:concealcursor = exists("g:indentLine_concealcursor") ? g:indentLine_concealcursor : "inc"
let &l:conceallevel = exists("g:indentLine_conceallevel") ? g:indentLine_conceallevel : "2"
endif
endfunction
"{{{1 function! s:ResetConcealOption()
function! s:ResetConcealOption()
if exists("b:indentLine_ConcealOptionSet") && b:indentLine_ConcealOptionSet
if exists("b:indentLine_original_concealcursor")
let &l:concealcursor = b:indentLine_original_concealcursor
endif
if exists("b:indentLine_original_conceallevel")
let &l:conceallevel = b:indentLine_original_conceallevel
endif
let b:indentLine_ConcealOptionSet = 0
endif
endfunction
"{{{1 function! s:DisableOnDiff()
function! s:DisableOnDiff()
if &diff
call s:IndentLinesDisable()
call s:LeadingSpaceDisable()
endif
endfunction
"{{{1 function! s:VimEnter()
function! s:VimEnter()
let init_winnr = winnr()
noautocmd windo call s:DisableOnDiff()
noautocmd exec init_winnr . "wincmd w"
endfunction
"{{{1 function! s:ToggleOnDiff()
function! s:ToggleOnDiff()
if &diff
call s:IndentLinesDisable()
call s:LeadingSpaceDisable()
else
call s:Setup()
endif
endfunction
"{{{1 function! s:IndentLinesEnable()
function! s:IndentLinesEnable()
let s:indentSpace = get(g:, 'indentLine_indentSpace', &l:shiftwidth == 0 ? &l:tabstop : &l:shiftwidth)
if g:indentLine_newVersion
if exists("b:indentLine_enabled") && b:indentLine_enabled == 0
return
endif
if !exists("w:indentLine_indentLineId")
let w:indentLine_indentLineId = []
endif
call s:SetConcealOption()
if g:indentLine_showFirstIndentLevel
call add(w:indentLine_indentLineId, matchadd('Conceal', '^ ', 0, -1, {'conceal': g:indentLine_first_char}))
endif
let n = len(g:indentLine_char_list)
let level = 0
for i in range(s:indentSpace+1, s:indentSpace * g:indentLine_indentLevel + 1, s:indentSpace)
if n > 0
let char = g:indentLine_char_list[level % n]
let level += 1
else
let char = g:indentLine_char
endif
call add(w:indentLine_indentLineId, matchadd('Conceal', '^\s\+\zs\%'.i.'v ', 0, -1, {'conceal': char}))
endfor
return
endif
if exists("b:indentLine_enabled") && b:indentLine_enabled
return
else
let b:indentLine_enabled = 1
endif
call s:SetConcealOption()
let g:mysyntaxfile = g:indentLine_mysyntaxfile
if g:indentLine_showFirstIndentLevel
execute 'syntax match IndentLine /^ / containedin=ALL conceal cchar=' . g:indentLine_first_char
endif
if g:indentLine_faster
execute 'syntax match IndentLineSpace /^\s\+/ containedin=ALL contains=IndentLine'
execute 'syntax match IndentLine / \{'.(s:indentSpace-1).'}\zs / contained conceal cchar=' . g:indentLine_char
execute 'syntax match IndentLine /\t\zs / contained conceal cchar=' . g:indentLine_char
else
let pattern = line('$') < g:indentLine_maxLines ? 'v' : 'c'
for i in range(s:indentSpace+1, s:indentSpace * s:indentLine_indentLevel + 1, s:indentSpace)
execute 'syntax match IndentLine /\%(^\s\+\)\@<=\%'.i.pattern.' / containedin=ALL conceal cchar=' . g:indentLine_char
endfor
endif
endfunction
"{{{1 function! s:IndentLinesDisable()
function! s:IndentLinesDisable()
if g:indentLine_newVersion
if exists("w:indentLine_indentLineId") && ! empty(w:indentLine_indentLineId)
for id in w:indentLine_indentLineId
try
call matchdelete(id)
catch /^Vim\%((\a\+)\)\=:E80[23]/
endtry
endfor
let w:indentLine_indentLineId = []
endif
call s:ResetConcealOption()
return
endif
let b:indentLine_enabled = 0
try
syntax clear IndentLine
syntax clear IndentLineSpace
catch /^Vim\%((\a\+)\)\=:E28/ " catch error E28
endtry
endfunction
"{{{1 function! s:IndentLinesToggle()
function! s:IndentLinesToggle()
if g:indentLine_newVersion
if exists("w:indentLine_indentLineId") && ! empty(w:indentLine_indentLineId)
let b:indentLine_enabled = 0
call s:IndentLinesDisable()
else
let b:indentLine_enabled = 1
call s:IndentLinesEnable()
endif
return
endif
if exists("b:indentLine_enabled") && b:indentLine_enabled
call s:IndentLinesDisable()
else
call s:IndentLinesEnable()
endif
endfunction
"{{{1 function! s:ResetWidth(...)
function! s:ResetWidth(...)
if 0 < a:0
noautocmd let &l:shiftwidth = a:1
endif
let b:indentLine_enabled = 1
call s:IndentLinesDisable()
call s:IndentLinesEnable()
endfunction
"{{{1 function! s:AutoResetWidth()
function! s:AutoResetWidth()
let l:enable = get(
\ b:,
\ 'indentLine_enabled',
\ g:indentLine_enabled ? s:Filter() : 0
\)
let g:indentLine_autoResetWidth = get(g:, 'indentLine_autoResetWidth', 1)
if l:enable != 1 || g:indentLine_autoResetWidth != 1
return
endif
let b:indentLine_enabled = l:enable
call s:IndentLinesDisable()
call s:IndentLinesEnable()
endfunction
"{{{1 function! s:Filter()
function! s:Filter()
if index(g:indentLine_fileTypeExclude, &filetype) != -1
return 0
endif
if index(g:indentLine_bufTypeExclude, &buftype) != -1
return 0
endif
if len(g:indentLine_fileType) != 0 && index(g:indentLine_fileType, &filetype) == -1
return 0
endif
for name in g:indentLine_bufNameExclude
if matchstr(bufname(''), name) == bufname('')
return 0
endif
endfor
return 1
endfunction
"{{{1 function! s:Disable()
function! s:Disable()
if s:Filter() == 0
call s:IndentLinesDisable()
call s:LeadingSpaceDisable()
endif
endfunction
"{{{1 function! s:Setup()
function! s:Setup()
if &filetype ==# ""
call s:InitColor()
endif
if s:Filter() && g:indentLine_enabled || exists("b:indentLine_enabled") && b:indentLine_enabled
call s:IndentLinesEnable()
endif
if s:Filter() && g:indentLine_leadingSpaceEnabled || exists("b:indentLine_leadingSpaceEnabled") && b:indentLine_leadingSpaceEnabled
call s:LeadingSpaceEnable()
endif
endfunction
"{{{1 function! s:LeadingSpaceEnable()
function! s:LeadingSpaceEnable()
if g:indentLine_newVersion
if exists("b:indentLine_leadingSpaceEnabled") && b:indentLine_leadingSpaceEnabled == 0
return
endif
if !exists("w:indentLine_leadingSpaceId")
let w:indentLine_leadingSpaceId = []
endif
call s:SetConcealOption()
call add(w:indentLine_leadingSpaceId, matchadd('Conceal', '\%(^\s*\)\@<= ', 0, -1, {'conceal': g:indentLine_leadingSpaceChar}))
if exists("w:indentLine_indentLineId") && ! empty(w:indentLine_indentLineId)
call s:ResetWidth()
endif
return
endif
if g:indentLine_faster
echoerr 'LeadingSpace can not be shown when g:indentLine_faster == 1'
return
endif
let g:mysyntaxfile = g:indentLine_mysyntaxfile
let b:indentLine_leadingSpaceEnabled = 1
call s:SetConcealOption()
execute 'syntax match IndentLineLeadingSpace /\%(^\s*\)\@<= / containedin=ALLBUT,IndentLine conceal cchar=' . g:indentLine_leadingSpaceChar
endfunction
"{{{1 function! s:LeadingSpaceDisable()
function! s:LeadingSpaceDisable()
if g:indentLine_newVersion
if exists("w:indentLine_leadingSpaceId") && ! empty(w:indentLine_leadingSpaceId)
for id in w:indentLine_leadingSpaceId
try
call matchdelete(id)
catch /^Vim\%((\a\+)\)\=:E80[23]/
endtry
endfor
let w:indentLine_leadingSpaceId = []
endif
return
endif
let b:indentLine_leadingSpaceEnabled = 0
try
syntax clear IndentLineLeadingSpace
catch /^Vim\%((\a\+)\)\=:E28/ " catch error E28
endtry
endfunction
"{{{1 function! s:LeadingSpaceToggle()
function! s:LeadingSpaceToggle()
if g:indentLine_newVersion
if exists("w:indentLine_leadingSpaceId") && ! empty(w:indentLine_leadingSpaceId)
let b:indentLine_leadingSpaceEnabled = 0
call s:LeadingSpaceDisable()
else
let b:indentLine_leadingSpaceEnabled = 1
call s:LeadingSpaceEnable()
endif
return
endif
if exists("b:indentLine_leadingSpaceEnabled") && b:indentLine_leadingSpaceEnabled
call s:LeadingSpaceDisable()
else
call s:LeadingSpaceEnable()
endif
endfunction
"{{{1 augroup indentLine
augroup indentLine
autocmd!
if g:indentLine_newVersion
autocmd BufRead,BufNewFile,ColorScheme,Syntax * call s:InitColor()
if exists("##WinNew")
autocmd WinNew * call s:Setup()
endif
autocmd BufWinEnter * call s:IndentLinesDisable() | call s:LeadingSpaceDisable() | call s:Setup()
autocmd FileType * call s:Disable()
if exists("##OptionSet")
autocmd OptionSet diff call s:ToggleOnDiff()
autocmd OptionSet shiftwidth,tabstop noautocmd call s:AutoResetWidth()
endif
autocmd VimEnter * call s:VimEnter()
else
autocmd BufWinEnter * call s:Setup()
autocmd User * if exists("b:indentLine_enabled") || exists("b:indentLine_leadingSpaceEnabled") |
\ call s:Setup() | endif
autocmd BufRead,BufNewFile,ColorScheme,Syntax * call s:InitColor()
autocmd BufUnload * let b:indentLine_enabled = 0 | let b:indentLine_leadingSpaceEnabled = 0
autocmd SourcePre $VIMRUNTIME/syntax/nosyntax.vim doautocmd indentLine BufUnload
autocmd FileChangedShellPost * doautocmd indentLine BufUnload | call s:Setup()
if exists("##OptionSet")
autocmd OptionSet diff call s:ToggleOnDiff()
autocmd OptionSet shiftwidth,tabstop noautocmd call s:AutoResetWidth()
endif
autocmd VimEnter * call s:VimEnter()
endif
augroup END
"{{{1 commands
command! -nargs=? IndentLinesReset call s:ResetWidth(<f-args>)
command! -bar IndentLinesToggle call s:IndentLinesToggle()
if g:indentLine_newVersion
command! -bar IndentLinesEnable let b:indentLine_enabled = 1 | call s:IndentLinesEnable()
command! -bar IndentLinesDisable let b:indentLine_enabled = 0 | call s:IndentLinesDisable()
command! -bar LeadingSpaceEnable let b:indentLine_leadingSpaceEnabled = 1 | call s:LeadingSpaceEnable()
command! -bar LeadingSpaceDisable let b:indentLine_leadingSpaceEnabled = 0 | call s:LeadingSpaceDisable()
else
command! -bar IndentLinesEnable call s:IndentLinesEnable()
command! -bar IndentLinesDisable call s:IndentLinesDisable()
command! -bar LeadingSpaceEnable call s:LeadingSpaceEnable()
command! -bar LeadingSpaceDisable call s:LeadingSpaceDisable()
endif
command! LeadingSpaceToggle call s:LeadingSpaceToggle()
" vim:et:ts=4:sw=4:fdm=marker:fmr={{{,}}}