Skip to content

Commit 9e92523

Browse files
committed
Merge pull request #17 from erikw/toggle_autoformat
Command for toggling auto formatting.
2 parents 7647bc2 + 36ebf3f commit 9e92523

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ It is more convenient to map `:ClangFormat` to your favorite key mapping in norm
2020

2121
If you install [vim-operator-user](https://github.com/kana/vim-operator-user) in advance, you can also map `<Plug>(operator-clang-format)` to your favorite key bind.
2222

23+
`:ClangFormatAutoToggle` command toggles the auto formatting on buffer write.
24+
2325
### What is the difference from `clang-format.py`?
2426

2527
`clang-format.py` is Python script to use clang-format from Vim, which is installed with clang-format.
@@ -103,6 +105,8 @@ autocmd FileType c,cpp,objc nnoremap <buffer><Leader>cf :<C-u>ClangFormat<CR>
103105
autocmd FileType c,cpp,objc vnoremap <buffer><Leader>cf :ClangFormat<CR>
104106
" if you install vim-operator-user
105107
autocmd FileType c,cpp,objc map <buffer><Leader>x <Plug>(operator-clang-format)
108+
" Toggle auto formatting:
109+
nmap <Leader>C :ClangFormatAutoToggle<CR>
106110
```
107111

108112
### For More Information

plugin/clang_format.vim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,15 @@ augroup plugin-clang-format-auto-format
1919
autocmd FileType c,cpp,objc if g:clang_format#auto_formatexpr && !clang_format#is_invalid() | setlocal formatexpr=clang_format#replace(v:lnum,v:lnum+v:count-1) | endif
2020
augroup END
2121

22+
function! ClangFormatAutoToggleFunc()
23+
if !exists("g:clang_format#auto_format") || g:clang_format#auto_format == 0
24+
let g:clang_format#auto_format = 1
25+
echo "Auto clang-format: enabled"
26+
else
27+
let g:clang_format#auto_format = 0
28+
echo "Auto clang-format: disabled"
29+
endif
30+
endfunction
31+
command! ClangFormatAutoToggle call ClangFormatAutoToggleFunc()
32+
2233
let g:loaded_clang_format = 1

0 commit comments

Comments
 (0)