From 9cf57b6a9b79aef03f9a147eb0e6b80e8512faa0 Mon Sep 17 00:00:00 2001 From: Martin Vuille Date: Thu, 1 Sep 2016 19:54:49 -0400 Subject: [PATCH 1/2] Add b:formatters_ and b:formatdef_ overrides --- plugin/autoformat.vim | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/plugin/autoformat.vim b/plugin/autoformat.vim index 1595d78..6160113 100644 --- a/plugin/autoformat.vim +++ b/plugin/autoformat.vim @@ -33,7 +33,10 @@ function! s:find_formatters(...) " Detect configuration for all possible ftypes let b:formatters = [] for supertype in ftypes - let formatters_var = "g:formatters_".supertype + let formatters_var = "b:formatters_".supertype + if !exists(formatters_var) + let formatters_var = "g:formatters_".supertype + endif if !exists(formatters_var) " No formatters defined if verbose @@ -42,7 +45,7 @@ function! s:find_formatters(...) else let formatters = eval(formatters_var) if type(formatters) != type([]) - echoerr formatter_var." is not a list" + echoerr formatters_var." is not a list" else let b:formatters = b:formatters + formatters endif @@ -88,8 +91,11 @@ function! s:TryAllFormatters(...) range let s:index = b:current_formatter_index while 1 - let formatdef_var = 'g:formatdef_'.b:formatters[s:index] " Formatter definition must be existent + let formatdef_var = 'b:formatdef_'.b:formatters[s:index] + if !exists(formatdef_var) + let formatdef_var = 'g:formatdef_'.b:formatters[s:index] + endif if !exists(formatdef_var) echoerr "No format definition found in '".formatdef_var."'." return 0 From ab7778b74b5b5054fe1e11f5bf9a7ad5b4044d9a Mon Sep 17 00:00:00 2001 From: Martin Vuille Date: Thu, 8 Sep 2016 13:29:14 -0400 Subject: [PATCH 2/2] Document b:formatters_ and b:formatdef_ in README --- README.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9927a68..bfd81c2 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,7 @@ To print the currently selected formatter use `:CurrentFormatter`. These latter commands are mostly useful for debugging purposes. If you have a composite filetype with dots (like `django.python` or `php.wordpress`), vim-autoformat first tries to detect and use formatters for the exact original filetype, and -then tries the same for all supertypes occuring from left to right in the original filetype +then tries the same for all supertypes occurring from left to right in the original filetype separated by dots (`.`). ## Default formatprograms @@ -118,7 +118,7 @@ Here is a list of formatprograms that are supported by default, and thus will be Clang-format is a product of LLVM source builds. If you `brew install llvm`, clang-format can be found in /usr/local/Cellar/llvm/bin/. Vim-autoformat checks whether there exists a `.clang-format` or a `_clang-format` file up in - the current directory's ancestry. Based on that it eithers uses that file or tries to match + the current directory's ancestry. Based on that it either uses that file or tries to match vim options as much as possible. Details: http://clang.llvm.org/docs/ClangFormat.html. @@ -224,7 +224,7 @@ If you need a formatter that is not among the defaults, or if you are not satisf #### Basic definitions The formatprograms that available for a certain `` are defined in `g:formatters_`. -This is a list containing string indentifiers, which point to corresponding formatter definitions. +This is a list containing string identifiers, which point to corresponding formatter definitions. The formatter definitions themselves are defined in `g:formatdef_` as a string expression. Defining any of these variable manually in your .vimrc, will override the default value, if existing. @@ -262,6 +262,16 @@ If you have a composite filetype with dots (like `django.python` or `php.wordpre vim-autoformat internally replaces the dots with underscores so you can specify formatters through `g:formatters_django_python` and so on. +To override these options for a local buffer, use the buffer local variants: +`b:formatters_` and `b:formatdef_`. This can be useful, for example, when +working with different projects with conflicting formatting rules, with each project having settings +in its own vimrc or exrc file: + +```vim +let b:formatdef_custom_c='"astyle --mode=c --suffix=none --options=/home/user/special_project/astylerc"' +let b:formatters_c = ['custom_c'] +``` + #### Ranged definitions If your format program supports formatting specific ranges, you can provide a format