summaryrefslogtreecommitdiffstats
path: root/indent/autohotkey.vim
blob: cd97cd65bf62a2ae8fe610afff85f77a6b93bb47 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
if polyglot#init#is_disabled(expand('<sfile>:p'), 'autohotkey', 'indent/autohotkey.vim')
  finish
endif

" Vim indent file
" Language:   AutoHotkey
" Maintainer: Hirotoshi Namikawa <hnamikaw1@gmail.com>
" URL:        http://github.com/hnamikaw/vim-autohotkey
" License:    Same as Vim.

if exists('b:did_indent')
    finish
endif

setlocal autoindent
setlocal indentexpr=GetAutoHotkeyIndent()
setlocal indentkeys=!^F,o,O,0{,0},=if,=else,=return
setlocal expandtab

let b:undo_indent = 'setlocal '.join([
            \   'autoindent<',
            \   'indentexpr<',
            \   'indentkeys<',
            \   'expandtab<',
            \ ])

let s:TRUE = !0
let s:FALSE = 0

" Check BEGIN BLOCK
" TRUE:
" {
" { ; with comment
" if {
" if { ; with comment
"
" FALSE:
" ; if { comment
" sleep 1000 ; {
function! IsBeginBlockByStr(str)
    return a:str =~? '^[^;]*{\s*\(;.*\)\?$' ? s:TRUE : s:FALSE
endfunction

" Check END BLOCK
" TRUE:
" }
" } ; with comment
" } else {
" } else { ; with comment
"
" FALSE:
" ; } else {
function! IsEndBlockByStr(str)
    return a:str =~? '^\s*}.*\(;.*\)\?$' ? s:TRUE : s:FALSE
endfunction

" Check DOUBLE CORON
" TRUE:
" LAlt up::
" LAlt up:: ; with comment
"
" FALSE:
" ; LAlt up::
" sleep 1000 ; ::
function! IsDoubleCoronByStr(str)
    return a:str =~? '^[^;]*::\s*\(;.*\)\?$' ? s:TRUE : s:FALSE
endfunction

" Check RETURN
" TRUE:
" return
" return 1
" return ; with comment
"
" FALSE:
" ; return
function! IsReturnByStr(str)
    return a:str =~? '^\s*return.*\(;.*\)\?$' ? s:TRUE : s:FALSE
endfunction

" Check IF STATEMENT(without BLOCK)
" TRUE:
" if
" if ; with comment
" else
" else ; with comment
"
" FALSE:
" if {
" else {
function! IsIfStatementByStr(str)
    return a:str =~? '^\s*\(if\|else\)[^{]*\(;.*\)\?$' ? s:TRUE : s:FALSE
endfunction

" Check inside of BLOCK.
" TRUE:
" if {
"     hogehoge
"     fugafuga <--- line_num
" }
"
" FALSE:
" foobar <--- line_num
function! IsInsideOfBlockByNum(line_num)
    let block_indent_level = 0

    for scan_line_num in range(1, a:line_num)
        if IsBeginBlockByStr(getline(scan_line_num)) == s:TRUE
            let block_indent_level += 1
        endif

        if IsEndBlockByStr(getline(scan_line_num)) == s:TRUE
            let block_indent_level -= 1
        endif
    endfor

    return block_indent_level >= 1 ? s:TRUE : s:FALSE
endfunction

function! AddIndentByInd(indent)
    return a:indent + &l:shiftwidth
endfunction

function! UnIndentByInd(indent)
    return a:indent - &l:shiftwidth
endfunction

function! GetAutoHotkeyIndent()
    let  l0_num = v:lnum
    let  l1_num = v:lnum - 1
    let pl1_num = prevnonblank(l1_num)
    let pl2_num = prevnonblank(pl1_num - 1)

    let  l0_str = getline(l0_num)
    let pl1_str = getline(pl1_num)
    let pl2_str = getline(pl2_num)
    let pl1_ind = indent(pl1_num)
    let pl2_ind = indent(pl2_num)

    " Case: Next line of IF STATEMENT(without BLOCK)
    " if bar = 1
    "     callFunc1() <--- AddIndent
    "
    " if bar = 1
    " { <--- No! AddIndent
    if IsIfStatementByStr(pl1_str) == s:TRUE && IsBeginBlockByStr(l0_str) == s:FALSE
        return AddIndentByInd(pl1_ind)
    endif

    " Case: End of IF STATEMENT(without BLOCK)
    " if bar = 1
    "     callFunc1()
    " if bar = 2 <--- UnIndent
    "
    " Case: End of IF STATEMENT(without BLOCK) and END BLOCK(of outer block)
    " if foo
    " {
    "     if bar = 3
    "         callFunc3()
    " } <--- UnIndent (2level)
    if IsIfStatementByStr(pl2_str) == s:TRUE && IsBeginBlockByStr(pl1_str) == s:FALSE
        if IsEndBlockByStr(l0_str) == s:FALSE
            return UnIndentByInd(pl1_ind)
        else
            return UnIndentByInd(pl2_ind)
        endif
    endif

    " Case: Next line of BEGIN BLOCK
    " Swap(ByRef Left, ByRef Right)
    " {
    "     temp := Left <--- AddIndent
    "     Left := Right
    "     Right := temp
    " }
    if IsBeginBlockByStr(pl1_str) == s:TRUE
        return AddIndentByInd(pl1_ind)
    endif

    " Case: END BLOCK
    " Swap(ByRef Left, ByRef Right)
    " {
    "     temp := Left
    "     Left := Right
    "     Right := temp
    " } <--- UnIndent
    if IsEndBlockByStr(l0_str) == s:TRUE
        return UnIndentByInd(pl1_ind)
    endif

    " Case: Next line of DOUBLE CORON
    " #n::
    "     Run Notepad <--- AddIndent
    " return
    if IsDoubleCoronByStr(pl1_str) == s:TRUE
        return AddIndentByInd(pl1_ind)
    endif

    " Case: RETURN
    " Note: It is not nothing if in the BLOCK.
    " #n::
    "     Run Notepad
    " return <--- UnIndent
    " ~~~
    " if foo
    " {
    "     callFunc1()
    "     return <--- No! UnIndent
    " }
    if IsReturnByStr(l0_str) == s:TRUE && IsInsideOfBlockByNum(l0_num) == s:FALSE
        return UnIndentByInd(pl1_ind)
    endif

    " Case: Top line.
    if pl1_num == 0
        return 0
    endif

    " Case: It does not match anything.
    return pl1_ind
endfunction

let b:did_indent = 1