Skip to content

Commit d501b0e

Browse files
committed
indent: fix #15 and update README
1 parent 5f8fa16 commit d501b0e

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

README.md

+35
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,41 @@ With Vundle
2323
" inside .vimrc
2424
Plugin 'pboettch/vim-cmake-syntax'
2525

26+
## Indentation
27+
28+
There is also an indent file which does some intelligent alignment.
29+
30+
For control-keywords (`if`, `while`, `foreach`, `macro`, etc) it automatically adds a
31+
`shiftwidth()` (and substracts it for a `end`-keyword).
32+
33+
For commands (so everything which has arguments between parenthesis `(...)`) is tries to do the following.
34+
35+
Either it aligns all arguments on a new line in the same column as the opening parenthesis if the first argument is on the
36+
same line as the command:
37+
38+
```cmake
39+
add_custom_target(TARGET name
40+
DEPENDS target
41+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
42+
```
43+
44+
or it indents it with one additional `shiftwidth()` if the first argument is on a new line
45+
46+
```cmake
47+
add_custom_target(
48+
TARGET name
49+
DEPENDS target
50+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
51+
```
52+
53+
By setting `g:cmake_indent_no_command_argument_align` to 1 in your vimrc-file the old behavior is activated:
54+
55+
```cmake
56+
add_custom_target(TARGET name
57+
DEPENDS target
58+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
59+
```
60+
2661
## Test
2762

2863
There is a ever growing test-suite based on ctest located in test/

indent/cmake.vim

+15-5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ let s:cmake_indent_close_regex = '^' . s:cmake_regex_arguments .
4646
let s:cmake_indent_begin_regex = '^\s*\(IF\|MACRO\|FOREACH\|ELSE\|ELSEIF\|WHILE\|FUNCTION\)\s*('
4747
let s:cmake_indent_end_regex = '^\s*\(ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\|ELSEIF\|ENDWHILE\|ENDFUNCTION\)\s*('
4848

49+
if !exists('g:cmake_indent_no_command_argument_align')
50+
let g:cmake_indent_no_command_argument_align = 0
51+
endif
52+
4953
fun! CMakeGetIndent(lnum)
5054
let this_line = getline(a:lnum)
5155

@@ -74,16 +78,22 @@ fun! CMakeGetIndent(lnum)
7478
if previous_line =~? s:cmake_indent_open_regex " open parenthesis
7579
if previous_line !~? s:cmake_indent_close_regex " closing parenthesis is not on the same line
7680
call cursor(lnum, 1)
77-
let s = searchpos('(') " find first ( which is by cmake-design not a string
78-
if strlen(previous_line) == s[1] " if ( is the last char on this line, do not align with ( but use sw()
81+
let s = searchpos('(\s*$') " find '(' with nothing or only spaces until the end of the line
82+
if s[0] == lnum
7983
let ind += shiftwidth()
80-
else
81-
let ind = s[1]
84+
else " an argument after the keyword
85+
call cursor(lnum, 1)
86+
let s = searchpos('(') " find position of first '('
87+
if g:cmake_indent_no_command_argument_align == 1 " old behavior
88+
let ind += shiftwidth()
89+
else
90+
let ind = s[1]
91+
endif
8292
endif
8393
endif
8494
elseif previous_line =~? s:cmake_indent_close_regex " close parenthesis
8595
call cursor(lnum, strlen(previous_line))
86-
let pairpos = searchpos(s:cmake_indent_open_regex, 'nbz')
96+
let pairpos = searchpos(s:cmake_indent_open_regex, 'nbz') " find corresponding open paren
8797
if pairpos[0] != 0
8898
let ind = indent(pairpos[0])
8999
endif

0 commit comments

Comments
 (0)