Skip to content

Commit a96f62c

Browse files
d-k-cvivien
authored andcommitted
add an option to apply style conditionally
Currently the kernel coding style applies to every buffers which has the c or cpp filetype. Some users might want a finer control and apply this style only to their kernels. This commit changes the way the style is applied. Users can now define a "g:linuxsty_patterns" array in their vimrc and the style will be applied only if the buffer's path matches one of the pattern. [vd: fix a "conditionnaly" typo and break in configure loop.] fixes vivien#3 closes vivien#2
1 parent db8c80e commit a96f62c

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

plugin/linuxsty.vim

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
" This script is inspired from an article written by Bart:
66
" http://www.jukie.net/bart/blog/vim-and-linux-coding-style
77
" and various user comments.
8+
"
9+
" For those who want to apply these options conditionally, you can define an
10+
" array of patterns in your vimrc and these options will be applied only if
11+
" the buffer's path matches one of the pattern. In the following example,
12+
" options will be applied only if "/linux/" or "/kernel" is in buffer's path.
13+
"
14+
" let g:linuxsty_patterns = [ "/linux/", "/kernel/" ]
815

916
if exists("g:loaded_linuxsty")
1017
finish
@@ -16,12 +23,32 @@ set wildignore+=*.ko,*.mod.c,*.order,modules.builtin
1623
augroup linuxsty
1724
autocmd!
1825

19-
autocmd FileType c,cpp call s:LinuxFormatting()
20-
autocmd FileType c,cpp call s:LinuxKeywords()
21-
autocmd FileType c,cpp call s:LinuxHighlighting()
26+
autocmd FileType c,cpp call s:LinuxConfigure()
2227
autocmd FileType diff,kconfig setlocal tabstop=8
2328
augroup END
2429

30+
function s:LinuxConfigure()
31+
let apply_style = 0
32+
33+
if exists("g:linuxsty_patterns")
34+
let path = expand('%:p')
35+
for p in g:linuxsty_patterns
36+
if path =~ p
37+
let apply_style = 1
38+
break
39+
endif
40+
endfor
41+
else
42+
let apply_style = 1
43+
endif
44+
45+
if apply_style
46+
call s:LinuxFormatting()
47+
call s:LinuxKeywords()
48+
call s:LinuxHighlighting()
49+
endif
50+
endfunction
51+
2552
function s:LinuxFormatting()
2653
setlocal tabstop=8
2754
setlocal shiftwidth=8

0 commit comments

Comments
 (0)