annotate runtime/ftplugin/AppendMatchGroup.vim @ 2275:e4d849f4df03 vim73

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