~spacexplorer/+junk/myenv

« back to all changes in this revision

Viewing changes to vim/vim/plugin/libList.vim

  • Committer: Kim Allamandola
  • Date: 2011-05-02 05:39:17 UTC
  • Revision ID: spacexplorer@gmail.com-20110502053917-x0yl2lr9ri4yskr2
InitĀ import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
" File: libList.vim
 
2
" Last Change: 2001 Dec 10
 
3
" Maintainer: Gontran BAERTS <gbcreation@free.fr>
 
4
" Version: 0.1
 
5
"
 
6
" Please don't hesitate to correct my english :)
 
7
" Send corrections to <gbcreation@free.fr>
 
8
"
 
9
"-----------------------------------------------------------------------------
 
10
" Description: libList.vim is a set of functions to work with lists or one
 
11
" level arrays.
 
12
"
 
13
"-----------------------------------------------------------------------------
 
14
" To Enable: Normally, this file will reside in your plugins directory and be
 
15
" automatically sourced.
 
16
"
 
17
"-----------------------------------------------------------------------------
 
18
" Usage: Lists are strings variable with values separated by g:listSep
 
19
" character (comma" by default). You may redefine g:listSep variable as you
 
20
" wish.
 
21
"
 
22
" Here are available functions :
 
23
"
 
24
" - AddListItem( array, newItem, index ) :
 
25
"               Add item "newItem" to array "array" at "index" position
 
26
" - GetListItem( array, index ) :
 
27
"               Return item at "index" position in array "array"
 
28
" - GetListMatchItem( array, pattern ) :
 
29
"               Return item matching "pattern" in array "array"
 
30
" - GetListCount( array ) :
 
31
"               Return the number of items in array "array"
 
32
" - RemoveListItem( array, index ) :
 
33
"               Remove item at "index" position from array "array"
 
34
" - ReplaceListItem( array, index, item ) :
 
35
"               Remove item at "index" position by "item" in array "array"
 
36
" - ExchangeListItems( array, item1Index, item2Index ) :
 
37
"               Exchange item "item1Index" with item "item2Index" in array "array"
 
38
" - QuickSortList( array, beg, end ) :
 
39
"               Return array "array" with items between "beg" and "end" sorted
 
40
"
 
41
" Example:
 
42
" let mylist=""
 
43
" echo GetListCount( mylist ) " --> 0
 
44
" let mylist = AddListItem( mylist, "One", 0 ) " mylist == "One"
 
45
" let mylist = AddListItem( mylist, "Three", 1 ) " mylist == "One,Three"
 
46
" let mylist = AddListItem( mylist, "Two", 1 ) " mylist == "One,Two,Three"
 
47
" echo GetListCount( mylist ) " --> 3
 
48
" echo GetListItem( mylist, 2 ) " --> Three
 
49
" echo GetListMatchItem( mylist, "w" ) " --> two
 
50
" echo GetListMatchItem( mylist, "e" ) " --> One
 
51
" let mylist = RemoveListItem( mylist, 2 ) " mylist == "One,Two"
 
52
" echo GetListCount( mylist ) " --> 2
 
53
" let mylist = ReplaceListItem( mylist, 0, "Three" ) " mylist == "Three,Two"
 
54
" let mylist = ExchangeListItems( mylist, 0, 1 ) " mylist == "Two,Three"
 
55
" let mylist = AddListItem( mylist, "One", 0 ) " mylist == "One,Two,Three"
 
56
" let mylist = QuickSortList( mylist, 0, GetListCount(mylist)-1 )
 
57
" " mylist == "One,Three,Two"
 
58
"
 
59
"-----------------------------------------------------------------------------
 
60
" Updates:
 
61
" in version 0.1
 
62
" - First version
 
63
 
 
64
" Has this already been loaded ?
 
65
if exists("loaded_libList")
 
66
       finish
 
67
endif
 
68
let loaded_libList=1
 
69
 
 
70
"**
 
71
" Separator:
 
72
" You may change the separator character et any time.
 
73
"**
 
74
let g:listSep = ","
 
75
 
 
76
"**
 
77
"AddListItem:
 
78
"       Add new item at given position.
 
79
"       First item index is 0 (zero).
 
80
"Parameters:
 
81
" - array : Array/List (string of values) which receives the new item.
 
82
" - newItem : String containing the item value to add.
 
83
" - index : Integer indicating the position at which the new item is added.
 
84
"                       It must be greater than or equals to 0 (zero).
 
85
"Return:
 
86
"String containing array values, including newItem.
 
87
"**
 
88
function AddListItem( array, newItem, index )
 
89
        if a:index == 0
 
90
                if a:array == ""
 
91
                        return a:newItem
 
92
                endif
 
93
                return a:newItem . g:listSep . a:array
 
94
        endif
 
95
        return substitute( a:array, '\(\%(^\|' . g:listSep . '\)[^' . g:listSep . ']\+\)\{' . a:index . '\}', '\0' . g:listSep . a:newItem , "" )
 
96
endfunction
 
97
 
 
98
"**
 
99
"GetListItem:
 
100
"       Get item at given position.
 
101
"Parameters:
 
102
" - array : Array/List (string of values).
 
103
" - index : Integer indicating the position of item to return.
 
104
"                       It must be greater than or equals to 0 (zero).
 
105
"Return:
 
106
"String representing the item.
 
107
"**
 
108
function GetListItem( array, index )
 
109
        if a:index == 0
 
110
                return matchstr( a:array, '^[^' . g:listSep . ']\+' )
 
111
        else
 
112
                return matchstr( a:array, "[^" . g:listSep . "]\\+", matchend( a:array, '\(\%(^\|' . g:listSep . '\)[^' . g:listSep . ']\+\)\{' . a:index . '\}' . g:listSep ) )
 
113
        endif
 
114
endfunction
 
115
 
 
116
"**
 
117
"GetListMatchItem:
 
118
"       Get the first item matching given pattern.
 
119
"Parameters:
 
120
" - array : Array/List (string of values).
 
121
" - pattern : Regular expression to match with items.
 
122
"                         Avoid to use ^, $ and listSep characters in pattern, unless you
 
123
"                         know what you do.
 
124
"Return:
 
125
"String representing the first item that matches the pattern.
 
126
"**
 
127
function GetListMatchItem( array, pattern )
 
128
        return matchstr( a:array, '[^' . g:listSep . ']*' . a:pattern . '[^' . g:listSep . ']*' )
 
129
endfunction
 
130
 
 
131
"**
 
132
"ReplaceListItem:
 
133
"       Replace item at given position by a new one.
 
134
"Parameters:
 
135
" - array : Array/List (string of values).
 
136
" - index : Integer indicating the position of item to replace.
 
137
"                       It must be greater than or equals to 0 (zero).
 
138
" - item : String containing the new value of the replaced item.
 
139
"Return:
 
140
"String containing array values.
 
141
"**
 
142
function ReplaceListItem( array, index, item )
 
143
        if a:index == 0
 
144
                return substitute( a:array, '^[^' .g:listSep. ']\+', a:item, "" )
 
145
        else
 
146
                return substitute( a:array, '\(\%(\%(^\|' . g:listSep . '\)[^' . g:listSep . ']\+\)\{' . a:index . '\}\)' . g:listSep . '[^' . g:listSep . ']\+', '\1' . g:listSep . a:item , "" )
 
147
        endif
 
148
endfunction
 
149
 
 
150
"**
 
151
"RemoveListItem:
 
152
"       Remove item at given position.
 
153
"Parameters:
 
154
" - array : Array/List (string of values) from which remove an item.
 
155
" - index : Integer indicating the position of item to remove.
 
156
"                       It must be greater than or equals to 0 (zero).
 
157
"Return:
 
158
"String containing array values, except the removed one.
 
159
"**
 
160
function RemoveListItem( array, index )
 
161
        if a:index == 0
 
162
                return substitute( a:array, '^[^' .g:listSep. ']\+\(' . g:listSep . '\|$\)', "", "" )
 
163
        else
 
164
                return substitute( a:array, '\(\%(\%(^\|' . g:listSep . '\)[^' . g:listSep . ']\+\)\{' . a:index . '\}\)' . g:listSep . '[^' . g:listSep . ']\+', '\1', "" )
 
165
        endif
 
166
endfunction
 
167
 
 
168
"**
 
169
"ExchangeListItems:
 
170
"       Exchange item at position item1Index with item at position item2Index.
 
171
"Parameters:
 
172
" - array : Array/List (string of values).
 
173
" - item1index : Integer indicating the position of the first item to exchange.
 
174
"                                It must be greater than or equals to 0 (zero).
 
175
" - item2index : Integer indicating the position of the second item to
 
176
"                                exchange. It must be greater than or equals to 0 (zero).
 
177
"Return:
 
178
"String containing array values.
 
179
"**
 
180
function ExchangeListItems( array, item1Index, item2Index )
 
181
        let item1 = GetListItem( a:array, a:item1Index )
 
182
        let array = ReplaceListItem( a:array, a:item1Index, GetListItem( a:array, a:item2Index ) )
 
183
        return ReplaceListItem( array, a:item2Index, item1 )
 
184
endfunction
 
185
 
 
186
"**
 
187
"GetListCount:
 
188
"       Number of items in array.
 
189
"Parameters:
 
190
" - array : Array/List (string of values).
 
191
"Return:
 
192
"Integer representing the number of items in array.
 
193
"Index of last item is GetListCount(array)-1.
 
194
"**
 
195
function GetListCount( array )
 
196
        if a:array == "" | return 0 | endif
 
197
        let pos = 0
 
198
        let cnt = 0
 
199
        while pos != -1
 
200
                let pos = matchend( a:array, g:listSep, pos )
 
201
                let cnt = cnt + 1
 
202
        endwhile
 
203
        return cnt
 
204
endfunction
 
205
 
 
206
"**
 
207
"QuickSortList:
 
208
"       Sort array.
 
209
"Parameters:
 
210
" - array : Array/List (string of values).
 
211
" - beg : Min index of the range of items to sort.
 
212
" - end : Max index of the range of items to sort.
 
213
"Return:
 
214
"String containing array values with indicated range of items sorted.
 
215
"**
 
216
function QuickSortList( array, beg, end )
 
217
        let array = a:array
 
218
        let pivot = GetListItem( array, a:beg )
 
219
        let l = a:beg
 
220
        let r = a:end
 
221
        while l < r
 
222
                while GetListItem( array, r ) > pivot
 
223
                        let r = r - 1
 
224
                endwhile
 
225
                if l != r
 
226
                        let array = ReplaceListItem( array, l, GetListItem( array, r ) )
 
227
                        let array = ReplaceListItem( array, r, pivot )
 
228
                        let l = l + 1
 
229
                endif
 
230
 
 
231
                while GetListItem( array, l ) < pivot
 
232
                        let l = l + 1
 
233
                endwhile
 
234
                if l != r
 
235
                        let array = ReplaceListItem( array, r, GetListItem( array, l ) )
 
236
                        let array = ReplaceListItem( array, l, pivot )
 
237
                        let r = r - 1
 
238
                endif
 
239
        endwhile
 
240
        if a:beg < l-1
 
241
                let array = QuickSortList( array, a:beg, l-1 )
 
242
        endif
 
243
        if a:end > l+1
 
244
                let array = QuickSortList( array, l+1, a:end )
 
245
        endif
 
246
        return array
 
247
endfunction
 
248
 
 
249