annotate runtime/ftplugin/AppendMatchGroup.vim @ 48:67300faee616 v7-3-618

v7-3-618
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 01 Aug 2012 18:08:28 +0900
parents 76efa0be13f1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
1 " Vim filetype plugin file utility
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
2 " Language: * (various)
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
3 " Maintainer: Dave Silvia <dsilvia@mchsi.com>
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
4 " Date: 6/30/2004
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
5
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
6 " The start of match (b:SOM) default is:
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
7 " '\<'
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
8 " The end of match (b:EOM) default is:
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
9 " '\>'
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
10 "
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
11 " If you want to use some other start/end of match, just assign the
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
12 " value to the b:SOM|EOM variable in your filetype script.
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
13 "
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
14 " SEE: :h pattern.txt
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
15 " :h pattern-searches
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
16 " :h regular-expression
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
17 " :h matchit
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
18
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
19 let s:myName=expand("<sfile>:t")
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
20
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
21 " matchit.vim not loaded -- don't do anyting
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
22 if !exists("loaded_matchit")
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
23 echomsg s:myName.": matchit.vim not loaded -- finishing without loading"
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
24 finish
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
25 endif
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
26
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
27 " already been here -- don't redefine
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
28 if exists("*AppendMatchGroup")
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
29 finish
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
30 endif
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
31
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
32 " Function To Build b:match_words
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
33 " The following function, 'AppendMatchGroup', helps to increase
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
34 " readability of your filetype script if you choose to use matchit.
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
35 " It also precludes many construction errors, reducing the
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
36 " construction to simply invoking the function with the match words.
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
37 " As an example, let's take the ubiquitous if/then/else/endif type
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
38 " of construct. This is how the entry in your filetype script would look.
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
39 "
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
40 " " source the AppendMatchGroup function file
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
41 " runtime ftplugin/AppendMatchGroup.vim
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
42 "
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
43 " " fill b:match_words
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
44 " call AppendMatchGroup('if,then,else,endif')
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
45 "
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
46 " And the b:match_words constructed would look like:
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
47 "
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
48 " \<if\>:\<then\>:\<else\>:\<endif\>
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
49 "
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
50 " Use of AppendMatchGroup makes your filetype script is a little
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
51 " less busy and a lot more readable. Additionally, it
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
52 " checks three critical things:
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
53 "
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
54 " 1) Do you have at least 2 entries in your match group.
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
55 "
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
56 " 2) Does the buffer variable 'b:match_words' exist? if not, create it.
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
57 "
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
58 " 3) If the buffer variable 'b:match_words' does exist, is the last
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
59 " character a ','? If not, add it before appending.
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
60 "
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
61 " You should now be able to match 'if/then/else/endif' in succession
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
62 " in your source file, in just about any construction you may have
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
63 " chosen for them.
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
64 "
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
65 " To add another group, simply call 'AppendMatchGroup again. E.G.:
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
66 "
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
67 " call AppendMatchGroup('while,do,endwhile')
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
68
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
69 function AppendMatchGroup(mwordList)
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
70 let List=a:mwordList
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
71 let Comma=match(List,',')
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
72 if Comma == -1 || Comma == strlen(List)-1
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
73 echoerr "Must supply a comma separated list of at least 2 entries."
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
74 echoerr "Supplied list: <".List.">"
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
75 return
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
76 endif
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
77 let listEntryBegin=0
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
78 let listEntryEnd=Comma
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
79 let listEntry=strpart(List,listEntryBegin,listEntryEnd-listEntryBegin)
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
80 let List=strpart(List,Comma+1)
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
81 let Comma=match(List,',')
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
82 " if listEntry is all spaces || List is empty || List is all spaces
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
83 if (match(listEntry,'\s\+') == 0 && match(listEntry,'\S\+') == -1)
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
84 \ || List == '' || (match(List,'\s\+') == 0 && match(List,'\S\+') == -1)
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
85 echoerr "Can't use all spaces for an entry <".listEntry.">"
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
86 echoerr "Remaining supplied list: <".List.">"
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
87 return
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
88 endif
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
89
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
90 if !exists("b:SOM")
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
91 let b:SOM='\<'
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
92 endif
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
93 if !exists("b:EOM")
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
94 let b:EOM='\>'
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
95 endif
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
96 if !exists("b:match_words")
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
97 let b:match_words=''
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
98 endif
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
99 if b:match_words != '' && match(b:match_words,',$') == -1
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
100 let b:match_words=b:match_words.','
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
101 endif
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
102 " okay, all set add first entry in this list
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
103 let b:match_words=b:match_words.b:SOM.listEntry.b:EOM.':'
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
104 while Comma != -1
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
105 let listEntryEnd=Comma
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
106 let listEntry=strpart(List,listEntryBegin,listEntryEnd-listEntryBegin)
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
107 let List=strpart(List,Comma+1)
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
108 let Comma=match(List,',')
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
109 " if listEntry is all spaces
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
110 if match(listEntry,'\s\+') == 0 && match(listEntry,'\S\+') == -1
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
111 echoerr "Can't use all spaces for an entry <".listEntry."> - skipping"
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
112 echoerr "Remaining supplied list: <".List.">"
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
113 continue
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
114 endif
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
115 let b:match_words=b:match_words.b:SOM.listEntry.b:EOM.':'
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
116 endwhile
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
117 let listEntry=List
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
118 let b:match_words=b:match_words.b:SOM.listEntry.b:EOM
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
119 endfunction
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
120
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
121 " TODO: Write a wrapper to handle multiple groups in one function call.
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
122 " Don't see a lot of utility in this as it would undoubtedly warrant
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
123 " continuation lines in the filetype script and it would be a toss
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
124 " up as to which is more readable: individual calls one to a line or
76efa0be13f1 Initial revision
atsuki
parents:
diff changeset
125 " a single call with continuation lines. I vote for the former.