1 " Vim indent file generic utility functions
2 " Language: * (various)
3 " Maintainer: Dave Silvia <dsilvia@mchsi.com>
6 " SUMMARY: To use GenericIndent, indent/<your_filename>.vim would have the
7 " following general format:
9 " if exists("b:did_indent") | finish | endif
10 " let b:did_indent = 1
11 " runtime indent/GenericIndent.vim
12 " let b:indentStmts=''
13 " let b:dedentStmts=''
15 " setlocal indentexpr=GenericIndent()
16 " setlocal indentkeys=<your_keys>
17 " call GenericIndentStmts(<your_stmts>)
18 " call GenericDedentStmts(<your_stmts>)
19 " call GenericAllStmts()
23 " NOTE: b:indentStmts, b:dedentStmts, and b:allStmts need to be initialized
24 " to '' before callin the functions because 'indent.vim' explicitly
25 " 'unlet's b:did_indent. This means that the lists will compound if
26 " you change back and forth between buffers. This is true as of
27 " version 6.3, 6/23/2004.
29 " NOTE: By default, GenericIndent is case sensitive.
30 " let b:case_insensitive=1 if you want to ignore case, e.g. DOS batch files
32 " The function 'GenericIndent' is data driven and handles most all cases of
33 " indent checking if you first set up the data. To use this function follow
34 " the example below (taken from the file indent/MuPAD_source.vim)
36 " Before you start, source this file in indent/<your_script>.vim to have it
37 " define functions for your use.
39 "runtime indent/GenericIndent.vim
41 " The data is in 5 sets:
43 " First, set the data set 'indentexpr' to GenericIndent().
45 "setlocal indentexpr=GenericIndent()
47 " Second, set the data set 'indentkeys' to the keywords/expressions that need
48 " to be checked for 'indenting' _as_ they typed.
50 "setlocal indentkeys==end_proc,=else,=then,=elif,=end_if,=end_case,=until,=end_repeat,=end_domain,=end_for,=end_while,=end,o,O
52 " NOTE: 'o,O' at the end of the previous line says you wish to be called
53 " whenever a newline is placed in the buffer. This allows the previous line
54 " to be checked for indentation parameters.
56 " Third, set the data set 'b:indentStmts' to the keywords/expressions that, when
57 " they are on a line _when_ you _press_ the _<Enter>_ key,
58 " you wish to have the next line indented.
60 "call GenericIndentStmts('begin,if,then,else,elif,case,repeat,until,domain,do')
62 " Fourth, set the data set 'b:dedentStmts' to the keywords/expressions that, when
63 " they are on a line you are currently typing, you wish to have that line
64 " 'dedented' (having already been indented because of the previous line's
67 "call GenericDedentStmts('end_proc,then,else,elif,end_if,end_case,until,end_repeat,end_domain,end_for,end_while,end')
69 " Fifth, set the data set 'b:allStmts' to the concatenation of the third and
70 " fourth data sets, used for checking when more than one keyword/expression
73 "call GenericAllStmts()
75 " NOTE: GenericIndentStmts uses two variables: 'b:indentStmtOpen' and
76 " 'b:indentStmtClose' which default to '\<' and '\>' respectively. You can
77 " set (let) these to any value you wish before calling GenericIndentStmts with
78 " your list. Similarly, GenericDedentStmts uses 'b:dedentStmtOpen' and
79 " 'b:dedentStmtClose'.
81 " NOTE: Patterns may be used in the lists passed to Generic[In|De]dentStmts
82 " since each element in the list is copied verbatim.
84 " Optionally, you can set the DEBUGGING flag within your script to have the
85 " debugging messages output. See below for description. This can also be set
86 " (let) from the command line within your editing buffer.
93 " to familiarize yourself with how this works and where you should have this
94 " file and your file(s) installed.
96 " For help with setting 'indentkeys' see:
98 " Also, for some good examples see 'indent/sh.vim' and 'indent/vim.vim' as
99 " well as files for other languages you may be familiar with.
102 " Alternatively, if you'd rather specify yourself, you can enter
103 " 'b:indentStmts', 'b:dedentStmts', and 'b:allStmts' 'literally':
105 "let b:indentStmts='\<begin\>\|\<if\>\|\<then\>\|\<else\>\|\<elif\>\|\<case\>\|\<repeat\>\|\<until\>\|\<domain\>\|\<do\>'
106 "let b:dedentStmts='\<end_proc\>\|\<else\>\|\<elif\>\|\<end_if\>\|\<end_case\>\|\<until\>\|\<end_repeat\>\|\<end_domain\>\|\<end_for\>\|\<end_while\>\|\<end\>'
107 "let b:allStmts=b:indentStmts.'\|'.b:dedentStmts
109 " This is only useful if you have particularly different parameters for
110 " matching each statement.
112 " RECAP: From indent/MuPAD_source.vim
114 "if exists("b:did_indent") | finish | endif
116 "let b:did_indent = 1
118 "runtime indent/GenericIndent.vim
120 "setlocal indentexpr=GenericIndent()
121 "setlocal indentkeys==end_proc,=then,=else,=elif,=end_if,=end_case,=until,=end_repeat,=end_domain,=end_for,=end_while,=end,o,O
122 "call GenericIndentStmts('begin,if,then,else,elif,case,repeat,until,domain,do')
123 "call GenericDedentStmts('end_proc,then,else,elif,end_if,end_case,until,end_repeat,end_domain,end_for,end_while,end')
124 "call GenericAllStmts()
130 let s:myScriptName=expand("<sfile>:t")
132 if exists("*GenericIndent")
136 function GenericAllStmts()
137 let b:allStmts=b:indentStmts.'\|'.b:dedentStmts
138 call DebugGenericIndent(expand("<sfile>").": "."b:indentStmts: ".b:indentStmts.", b:dedentStmts: ".b:dedentStmts.", b:allStmts: ".b:allStmts)
141 function GenericIndentStmts(stmts)
143 let Comma=match(Stmts,',')
144 if Comma == -1 || Comma == strlen(Stmts)-1
145 echoerr "Must supply a comma separated list of at least 2 entries."
146 echoerr "Supplied list: <".Stmts.">"
150 if !exists("b:indentStmtOpen")
151 let b:indentStmtOpen='\<'
153 if !exists("b:indentStmtClose")
154 let b:indentStmtClose='\>'
156 if !exists("b:indentStmts")
159 if b:indentStmts != ''
160 let b:indentStmts=b:indentStmts.'\|'
162 call DebugGenericIndent(expand("<sfile>").": "."b:indentStmtOpen: ".b:indentStmtOpen.", b:indentStmtClose: ".b:indentStmtClose.", b:indentStmts: ".b:indentStmts.", Stmts: ".Stmts)
164 let stmtEntryEnd=Comma
165 let stmtEntry=strpart(Stmts,stmtEntryBegin,stmtEntryEnd-stmtEntryBegin)
166 let Stmts=strpart(Stmts,Comma+1)
167 let Comma=match(Stmts,',')
168 let b:indentStmts=b:indentStmts.b:indentStmtOpen.stmtEntry.b:indentStmtClose
170 let stmtEntryEnd=Comma
171 let stmtEntry=strpart(Stmts,stmtEntryBegin,stmtEntryEnd-stmtEntryBegin)
172 let Stmts=strpart(Stmts,Comma+1)
173 let Comma=match(Stmts,',')
174 let b:indentStmts=b:indentStmts.'\|'.b:indentStmtOpen.stmtEntry.b:indentStmtClose
177 let b:indentStmts=b:indentStmts.'\|'.b:indentStmtOpen.stmtEntry.b:indentStmtClose
180 function GenericDedentStmts(stmts)
182 let Comma=match(Stmts,',')
183 if Comma == -1 || Comma == strlen(Stmts)-1
184 echoerr "Must supply a comma separated list of at least 2 entries."
185 echoerr "Supplied list: <".Stmts.">"
189 if !exists("b:dedentStmtOpen")
190 let b:dedentStmtOpen='\<'
192 if !exists("b:dedentStmtClose")
193 let b:dedentStmtClose='\>'
195 if !exists("b:dedentStmts")
198 if b:dedentStmts != ''
199 let b:dedentStmts=b:dedentStmts.'\|'
201 call DebugGenericIndent(expand("<sfile>").": "."b:dedentStmtOpen: ".b:dedentStmtOpen.", b:dedentStmtClose: ".b:dedentStmtClose.", b:dedentStmts: ".b:dedentStmts.", Stmts: ".Stmts)
203 let stmtEntryEnd=Comma
204 let stmtEntry=strpart(Stmts,stmtEntryBegin,stmtEntryEnd-stmtEntryBegin)
205 let Stmts=strpart(Stmts,Comma+1)
206 let Comma=match(Stmts,',')
207 let b:dedentStmts=b:dedentStmts.b:dedentStmtOpen.stmtEntry.b:dedentStmtClose
209 let stmtEntryEnd=Comma
210 let stmtEntry=strpart(Stmts,stmtEntryBegin,stmtEntryEnd-stmtEntryBegin)
211 let Stmts=strpart(Stmts,Comma+1)
212 let Comma=match(Stmts,',')
213 let b:dedentStmts=b:dedentStmts.'\|'.b:dedentStmtOpen.stmtEntry.b:dedentStmtClose
216 let b:dedentStmts=b:dedentStmts.'\|'.b:dedentStmtOpen.stmtEntry.b:dedentStmtClose
219 " Debugging function. Displays messages in the command area which can be
220 " reviewed using ':messages'. To turn it on use ':let b:DEBUGGING=1'. Once
221 " on, turn off by using ':let b:DEBUGGING=0. If you don't want it at all and
222 " feel it's slowing down your editing (you must have an _awfully_ slow
223 " machine!;-> ), you can just comment out the calls to it from 'GenericIndent'
224 " below. No need to remove the function or the calls, tho', as you never can
225 " tell when they might come in handy!;-)
226 function DebugGenericIndent(msg)
227 if exists("b:DEBUGGING") && b:DEBUGGING
228 echomsg '['.s:hit.']'.s:myScriptName."::".a:msg
232 function GenericIndent()
233 " save ignore case option. Have to set noignorecase for the match
234 " functions to do their job the way we want them to!
235 " NOTE: if you add a return to this function be sure you do
236 " if IgnoreCase | set ignorecase | endif
237 " before returning. You can just cut and paste from here.
238 let IgnoreCase=&ignorecase
239 " let b:case_insensitive=1 if you want to ignore case, e.g. DOS batch files
240 if !exists("b:case_insensitive")
243 " this is used to let DebugGenericIndent display which invocation of the
244 " function goes with which messages.
247 let cline=getline(lnum)
248 let lnum=prevnonblank(lnum)
249 if lnum==0 | if IgnoreCase | set ignorecase | endif | return 0 | endif
250 let pline=getline(lnum)
251 let ndnt=indent(lnum)
252 if !exists("b:allStmts")
253 call GenericAllStmts()
256 call DebugGenericIndent(expand("<sfile>").": "."cline=<".cline.">, pline=<".pline.">, lnum=".lnum.", v:lnum=".v:lnum.", ndnt=".ndnt)
258 " current line, only check dedent
260 " just dedented this line, don't need to do it again.
261 " another dedentStmts was added or an end%[_*] was completed.
262 if s:lastVlnum==v:lnum
263 if IgnoreCase | set ignorecase | endif
266 let s:lastVlnum=v:lnum
267 call DebugGenericIndent(expand("<sfile>").": "."Checking dedent")
269 let dedentKeyBegin=match(srcStr,b:dedentStmts)
270 if dedentKeyBegin != -1
271 let dedentKeyEnd=matchend(srcStr,b:dedentStmts)
272 let dedentKeyStr=strpart(srcStr,dedentKeyBegin,dedentKeyEnd-dedentKeyBegin)
273 "only dedent if it's the beginning of the line
274 if match(srcStr,'^\s*\<'.dedentKeyStr.'\>') != -1
275 call DebugGenericIndent(expand("<sfile>").": "."It's the beginning of the line, dedent")
276 let ndnt=ndnt-&shiftwidth
279 call DebugGenericIndent(expand("<sfile>").": "."dedent - returning ndnt=".ndnt)
281 " previous line, only check indent
282 call DebugGenericIndent(expand("<sfile>").": "."Checking indent")
284 let indentKeyBegin=match(srcStr,b:indentStmts)
285 if indentKeyBegin != -1
286 " only indent if it's the last indentStmts in the line
287 let allKeyBegin=match(srcStr,b:allStmts)
288 let allKeyEnd=matchend(srcStr,b:allStmts)
289 let allKeyStr=strpart(srcStr,allKeyBegin,allKeyEnd-allKeyBegin)
290 let srcStr=strpart(srcStr,allKeyEnd)
291 let allKeyBegin=match(srcStr,b:allStmts)
293 " not the end of the line, check what is and only indent if
294 " it's an indentStmts
295 call DebugGenericIndent(expand("<sfile>").": "."Multiple words in line, checking if last is indent")
296 while allKeyBegin != -1
297 let allKeyEnd=matchend(srcStr,b:allStmts)
298 let allKeyStr=strpart(srcStr,allKeyBegin,allKeyEnd-allKeyBegin)
299 let srcStr=strpart(srcStr,allKeyEnd)
300 let allKeyBegin=match(srcStr,b:allStmts)
302 if match(b:indentStmts,allKeyStr) != -1
303 call DebugGenericIndent(expand("<sfile>").": "."Last word in line is indent")
304 let ndnt=ndnt+&shiftwidth
307 " it's the last indentStmts in the line, go ahead and indent
308 let ndnt=ndnt+&shiftwidth
311 call DebugGenericIndent(expand("<sfile>").": "."indent - returning ndnt=".ndnt)
313 if IgnoreCase | set ignorecase | endif
320 " BUGS: You tell me! Probably. I just haven't found one yet or haven't been