summaryrefslogtreecommitdiff
path: root/runtime/ftplugin/AppendMatchGroup.vim
blob: 34e80b5f8398e1f17e4fe73c918f68158f6cec28 (plain)
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
" Vim filetype plugin file utility
" Language:    * (various)
" Maintainer:  Dave Silvia <dsilvia@mchsi.com>
" Date:        6/30/2004

" The start of match (b:SOM) default is:
"       '\<'
" The end of match (b:EOM) default is:
"       '\>'
"
" If you want to use some other start/end of match, just assign the
" value to the b:SOM|EOM variable in your filetype script.
"
" SEE: :h pattern.txt
"      :h pattern-searches
"      :h regular-expression
"      :h matchit

let s:myName=expand("<sfile>:t")

" matchit.vim not loaded -- don't do anyting
if !exists("loaded_matchit")
	echomsg s:myName.": matchit.vim not loaded -- finishing without loading"
	finish
endif

" already been here -- don't redefine
if exists("*AppendMatchGroup")
	finish
endif

" Function To Build b:match_words
" The following function, 'AppendMatchGroup', helps to increase
" readability of your filetype script if you choose to use matchit.
" It also precludes many construction errors, reducing the
" construction to simply invoking the function with the match words.
" As an example, let's take the ubiquitous if/then/else/endif type
" of construct.  This is how the entry in your filetype script would look.
"
"     " source the AppendMatchGroup function file
"     runtime ftplugin/AppendMatchGroup.vim
"
"     " fill b:match_words
"     call AppendMatchGroup('if,then,else,endif')
"
" And the b:match_words constructed would look like:
"
"     \<if\>:\<then\>:\<else\>:\<endif\>
" 
" Use of AppendMatchGroup makes your filetype script is a little
" less busy and a lot more readable.  Additionally, it
" checks three critical things:
"
"      1)  Do you have at least 2 entries in your match group.
"
"      2)  Does the buffer variable 'b:match_words' exist?  if not, create it.
"
"      3)  If the buffer variable 'b:match_words' does exist, is the last
"          character a ','?  If not, add it before appending.
" 
" You should now be able to match 'if/then/else/endif' in succession
" in your source file, in just about any construction you may have
" chosen for them.
"
" To add another group, simply call 'AppendMatchGroup again.  E.G.:
"
"      call AppendMatchGroup('while,do,endwhile')

function AppendMatchGroup(mwordList)
	let List=a:mwordList
	let Comma=match(List,',')
	if Comma == -1 || Comma == strlen(List)-1
		echoerr "Must supply a comma separated list of at least 2 entries."
		echoerr "Supplied list: <".List.">"
		return
	endif
	let listEntryBegin=0
	let listEntryEnd=Comma
	let listEntry=strpart(List,listEntryBegin,listEntryEnd-listEntryBegin)
	let List=strpart(List,Comma+1)
	let Comma=match(List,',')
	" if listEntry is all spaces || List is empty || List is all spaces
	if (match(listEntry,'\s\+') == 0 && match(listEntry,'\S\+') == -1)
			\ || List == '' || (match(List,'\s\+') == 0 && match(List,'\S\+') == -1)
		echoerr "Can't use all spaces for an entry <".listEntry.">"
		echoerr "Remaining supplied list: <".List.">"
		return
	endif

	if !exists("b:SOM")
		let b:SOM='\<'
	endif
	if !exists("b:EOM")
		let b:EOM='\>'
	endif
	if !exists("b:match_words")
		let b:match_words=''
	endif
	if b:match_words != '' && match(b:match_words,',$') == -1
		let b:match_words=b:match_words.','
	endif
	" okay, all set add first entry in this list
	let b:match_words=b:match_words.b:SOM.listEntry.b:EOM.':'
	while Comma != -1
		let listEntryEnd=Comma
		let listEntry=strpart(List,listEntryBegin,listEntryEnd-listEntryBegin)
		let List=strpart(List,Comma+1)
		let Comma=match(List,',')
		" if listEntry is all spaces
		if match(listEntry,'\s\+') == 0 && match(listEntry,'\S\+') == -1
			echoerr "Can't use all spaces for an entry <".listEntry."> - skipping"
			echoerr "Remaining supplied list: <".List.">"
			continue
		endif
		let b:match_words=b:match_words.b:SOM.listEntry.b:EOM.':'
	endwhile
	let listEntry=List
	let b:match_words=b:match_words.b:SOM.listEntry.b:EOM
endfunction

" TODO:  Write a wrapper to handle multiple groups in one function call.
"        Don't see a lot of utility in this as it would undoubtedly warrant
"        continuation lines in the filetype script and it would be a toss
"        up as to which is more readable: individual calls one to a line or
"        a single call with continuation lines.  I vote for the former.