Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a parameter for identifying the applied state #13

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,57 @@ It will automatically apply known rules to kernel related files, such as .c,
error highlighting (like exceeding 80 chars).

If you have any comments, fixes or requests, feel free to contact me or send me
a patch. The development also takes place on its official [Github
a patch. The development also takes place on its official [Github
repository](https://github.com/vivien/vim-linux-coding-style).

The plugin is also available at [vim.org](www.vim.org), script ID
The plugin is also available at [vim.org](www.vim.org), script ID
[4369](http://www.vim.org/scripts/script.php?script_id=4369).

## Installation

You can just drop the linuxsty.vim file in your ~/.vim/plugin directory.
Alternatively you can use the Git repository with a manager such as
You can just drop the linuxsty.vim file in your ~/.vim/plugin directory.
Alternatively you can use the Git repository with a manager such as
[Pathogen](https://github.com/tpope/vim-pathogen).

## Usage

By default the Linux coding style is enabled for any file known to the Linux
By default the Linux coding style is enabled for any file known to the Linux
project (C files, headers, patches, Kconfig, etc.).

If you prefer a finer control and apply it only on some files, define
a "g:linuxsty_patterns" array in your vimrc and the style will be applied only
if the buffer's path matches one of the pattern. For instance, you can match
If you prefer a finer control and apply it only on some files, define
a `g:linuxsty_patterns` array in your vimrc and the style will be applied only
if the buffer's path matches one of the pattern. For instance, you can match
only projects under /usr/src/ and /linux with the following:

let g:linuxsty_patterns = [ "/usr/src/", "/linux" ]

If you want to enable the coding style on demand without checking the filetype,
you can use the :LinuxCodingStyle command. For instance, you can map it with
If you want to enable the coding style on demand without checking the filetype,
you can use the `:LinuxCodingStyle` command. For instance, you can map it with
the following in your vimrc:

nnoremap <silent> <leader>a :LinuxCodingStyle<cr>

In oder to detect, if the coding style is applied or not there exists two
parameters per buffer. The `b:apply_linux_style` is set to 1 if the
style should be applied (e.g. the `g:linuxsty_patterns` matches).

The `b:linuxsty_applied` is on the other hand set to 1 if the `:LinuxCodingStyle`
is called and finished for the current buffer. This includes also calls by other
plugins or even manual calls.
An example how this could be useful is to set the |listchars| depending on the
`b:linuxsty_applied` state.

function! LoadCustomStyle()
if (exists("b:linuxsty_applied")) && b:linuxsty_applied == 1
set listchars=tab:\ \ ,nbsp:_,trail:.
else
set listchars=tab:>~,nbsp:_,trail:.
endif
endfun
autocmd BufEnter * :call LoadCustomStyle()


## License

Copyright (c) Vivien Didelot. Distributed under the same terms as Vim itself.
Copyright (c) Vivien Didelot. Distributed under the same terms as Vim itself.
See :help license.
9 changes: 5 additions & 4 deletions plugin/linuxsty.vim
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ augroup linuxsty
augroup END

function s:LinuxConfigure()
let apply_style = 0
let b:apply_linux_style = 0

if exists("g:linuxsty_patterns")
let path = expand('%:p')
for p in g:linuxsty_patterns
if path =~ p
let apply_style = 1
let b:apply_linux_style = 1
break
endif
endfor
else
let apply_style = 1
let b:apply_linux_style = 1
endif

if apply_style
if b:apply_linux_style
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, may I ask you to add a single line let b:linuxsty_applied = 1 in this statement? So that we change only one line and keep consistent with an implicit linuxsty namespace here.

A small addition in the README file to mention this new buffer variable would be a must.
If you tell me that it works as you expect, I'll happily merge this. Thanks!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added extra commit for the readme file and changed it to b:linuxsty_applied
I did see no problem popping up and its working fine.

call s:LinuxCodingStyle()
endif
endfunction
Expand All @@ -53,6 +53,7 @@ function! s:LinuxCodingStyle()
call s:LinuxFormatting()
call s:LinuxKeywords()
call s:LinuxHighlighting()
let b:linuxsty_applied = 1
endfunction

function s:LinuxFormatting()
Expand Down