├── README.md ├── after └── syntax │ └── c.vim └── plugin └── linuxsty.vim /README.md: -------------------------------------------------------------------------------- 1 | # linuxsty.vim 2 | 3 | This plugin is meant to help you respecting the Linux kernel coding style, 4 | described at: https://www.kernel.org/doc/Documentation/process/coding-style.rst 5 | HTML rendering: https://www.kernel.org/doc/html/latest/process/coding-style.html 6 | 7 | It will automatically apply known rules to kernel related files, such as `.c`, 8 | `.h`, `Kconfig` and patch files. The main rules are about indentation and syntax 9 | error highlighting (like exceeding 100 chars). 10 | 11 | The original plugin was written by Vivien Didelot, developed in this 12 | [Github repository](https://github.com/vivien/vim-linux-coding-style) and 13 | available at [vim.org](https://www.vim.org), script ID 14 | [4369](https://www.vim.org/scripts/script.php?script_id=4369), but those 15 | locations are now out-of-date. 16 | 17 | The current development location for this repo can be found in this 18 | [Github repository](https://github.com/gregkh/kernel-coding-style). 19 | 20 | ## Installation 21 | 22 | You can just drop the `linuxsty.vim` file in your `~/.vim/plugin` directory. 23 | Alternatively you can use the Git repository with a manager such as 24 | [Pathogen](https://github.com/tpope/vim-pathogen). 25 | 26 | ## Usage 27 | 28 | By default the Linux coding style is enabled for any file known to the Linux 29 | project (C files, headers, patches, Kconfig, etc.). 30 | 31 | If you prefer a finer control and apply it only on some files, define a 32 | `g:linuxsty_patterns` array in your `vimrc` and the style will be applied only 33 | if the buffer's path matches one of the pattern. For instance, you can match 34 | only projects under `/usr/src/` and `/linux` with the following: 35 | 36 | let g:linuxsty_patterns = [ "/usr/src/", "/linux" ] 37 | 38 | If you want to save the current file's directory and automatically call 39 | `:LinuxCodingStyle` next time, you can define the following option in your 40 | `vimrc`: 41 | 42 | let g:linuxsty_save_path = 1 43 | 44 | If you want to enable the coding style on demand without checking the filetype, 45 | you can use the `:LinuxCodingStyle` command. For instance, you can map it with 46 | the following in your `vimrc`: 47 | 48 | nnoremap a :LinuxCodingStyle 49 | 50 | ## License 51 | 52 | Copyright (c) Vivien Didelot. Distributed under the same terms as Vim itself. 53 | See :help license. 54 | -------------------------------------------------------------------------------- /after/syntax/c.vim: -------------------------------------------------------------------------------- 1 | " Extend C syntax settings with Linux Kernel-specific extensions. 2 | 3 | call g:LinuxConfigure("syntax_only") 4 | -------------------------------------------------------------------------------- /plugin/linuxsty.vim: -------------------------------------------------------------------------------- 1 | " Vim plugin to fit the Linux kernel coding style and help kernel development 2 | " Maintainer: Greg Kroah-Hartman 3 | " License: Distributed under the same terms as Vim itself. 4 | " 5 | " Originally developed by Vivien Didelot 6 | " 7 | " This script is inspired from an article written by Bart: 8 | " http://www.jukie.net/bart/blog/vim-and-linux-coding-style 9 | " and various user comments. 10 | " 11 | " For those who want to apply these options conditionally, you can define an 12 | " array of patterns in your vimrc and these options will be applied only if 13 | " the buffer's path matches one of the pattern. In the following example, 14 | " options will be applied only if "/linux/" or "/kernel" is in buffer's path. 15 | " 16 | " let g:linuxsty_patterns = [ "/linux/", "/kernel/" ] 17 | " 18 | " If you want to save the current file's directory and automatically call 19 | " LinuxCodingStyle next time, you can define the following option in your 20 | " vimrc: 21 | " 22 | " let g:linuxsty_save_path = 1 23 | " 24 | " To override the default text width for C kernel files define the following 25 | " option in your vimrc: 26 | " 27 | " let g:linuxsty_text_width = 80 28 | 29 | if exists("g:loaded_linuxsty") 30 | finish 31 | endif 32 | let g:loaded_linuxsty = 1 33 | 34 | let g:linuxsty_save_path = get(g:, 'linuxsty_save_path', 0) 35 | let g:linuxsty_text_width = get(g:, 'linuxsty_text_width', 100) 36 | 37 | set wildignore+=*.ko,*.mod.c,*.order,modules.builtin 38 | 39 | augroup linuxsty 40 | autocmd! 41 | 42 | autocmd FileType c,cpp call g:LinuxConfigure("settings_only") 43 | autocmd FileType diff setlocal ts=8 44 | autocmd FileType rst setlocal ts=8 sw=8 sts=8 noet 45 | autocmd FileType kconfig setlocal ts=8 sw=8 sts=8 noet 46 | autocmd FileType dts setlocal ts=8 sw=8 sts=8 noet 47 | autocmd FileType make setlocal ts=8 sw=8 sts=8 noet 48 | augroup END 49 | 50 | function g:LinuxConfigure(what) 51 | let apply_style = 0 52 | 53 | if exists("g:linuxsty_patterns") 54 | let path = expand('%:p') 55 | for p in g:linuxsty_patterns 56 | if path =~ p 57 | let apply_style = 1 58 | break 59 | endif 60 | endfor 61 | else 62 | let apply_style = 1 63 | endif 64 | 65 | if apply_style 66 | call s:LinuxCodingStyle(a:what) 67 | endif 68 | endfunction 69 | 70 | command! LinuxCodingStyle call s:LinuxCodingStyle("all") 71 | 72 | function! s:LinuxCodingStyle(what) 73 | if a:what != "settings_only" 74 | call s:LinuxSyntax() 75 | endif 76 | 77 | if a:what != "syntax_only" 78 | call s:LinuxFormatting() 79 | call s:LinuxHighlighting() 80 | call s:LinuxSavePath() 81 | endif 82 | endfunction 83 | 84 | function s:LinuxFormatting() 85 | setlocal tabstop=8 86 | setlocal shiftwidth=8 87 | setlocal softtabstop=8 88 | execute "setlocal textwidth=" . g:linuxsty_text_width 89 | setlocal noexpandtab 90 | 91 | setlocal cindent 92 | setlocal cinoptions=:0,l1,t0,g0,(0 93 | endfunction 94 | 95 | function s:LinuxSyntax() 96 | syn keyword cStatement fallthrough return_ptr 97 | syn keyword cStorageClass noinline __always_inline __must_check 98 | syn keyword cStorageClass __pure __weak __noclone 99 | syn keyword cStorageClass __free __cleanup 100 | syn keyword cStorageClass __used __always_unused __maybe_unused 101 | syn keyword cOperator likely unlikely 102 | syn keyword cType u8 u16 u32 u64 s8 s16 s32 s64 103 | syn keyword cType __u8 __u16 __u32 __u64 __s8 __s16 __s32 __s64 104 | syn keyword cType __le16 __le32 __le64 __be16 __be32 __be64 105 | syn keyword LinuxGuard guard scoped_guard scoped_cond_guard 106 | 107 | syn match LinuxError / \+\ze\t/ " spaces before tab 108 | syn match LinuxError /\%>100v[^()\{\}\[\]<>]\+/ " virtual column 101 and more 109 | 110 | " __deprecated should not be used anymore, please see 111 | " include/linux/compiler_attributes.h for why. 112 | syn match LinuxError /\<__deprecated\>/ 113 | 114 | " highlight various for_each() variants 115 | syn match cRepeat /\v^\s*\zs((h)?list_|device_)?for_each(_\w+)?(\()@=/ 116 | 117 | highlight default link LinuxError ErrorMsg 118 | highlight default link LinuxGuard cConditional 119 | endfunction 120 | 121 | function s:LinuxHighlighting() 122 | " Highlight trailing whitespace, unless we're in insert mode and the 123 | " cursor's placed right after the whitespace. This prevents us from having 124 | " to put up with whitespace being highlighted in the middle of typing 125 | " something 126 | autocmd InsertEnter * match LinuxError /\s\+\%#\@