2
" Last Change: 2001 Dec 10
3
" Maintainer: Gontran BAERTS <gbcreation@free.fr>
6
" Please don't hesitate to correct my english :)
7
" Send corrections to <gbcreation@free.fr>
9
"-----------------------------------------------------------------------------
10
" Description: libList.vim is a set of functions to work with lists or one
13
"-----------------------------------------------------------------------------
14
" To Enable: Normally, this file will reside in your plugins directory and be
15
" automatically sourced.
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
22
" Here are available functions :
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
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"
59
"-----------------------------------------------------------------------------
64
" Has this already been loaded ?
65
if exists("loaded_libList")
72
" You may change the separator character et any time.
78
" Add new item at given position.
79
" First item index is 0 (zero).
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).
86
"String containing array values, including newItem.
88
function AddListItem( array, newItem, index )
93
return a:newItem . g:listSep . a:array
95
return substitute( a:array, '\(\%(^\|' . g:listSep . '\)[^' . g:listSep . ']\+\)\{' . a:index . '\}', '\0' . g:listSep . a:newItem , "" )
100
" Get item at given position.
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).
106
"String representing the item.
108
function GetListItem( array, index )
110
return matchstr( a:array, '^[^' . g:listSep . ']\+' )
112
return matchstr( a:array, "[^" . g:listSep . "]\\+", matchend( a:array, '\(\%(^\|' . g:listSep . '\)[^' . g:listSep . ']\+\)\{' . a:index . '\}' . g:listSep ) )
118
" Get the first item matching given pattern.
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
125
"String representing the first item that matches the pattern.
127
function GetListMatchItem( array, pattern )
128
return matchstr( a:array, '[^' . g:listSep . ']*' . a:pattern . '[^' . g:listSep . ']*' )
133
" Replace item at given position by a new one.
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.
140
"String containing array values.
142
function ReplaceListItem( array, index, item )
144
return substitute( a:array, '^[^' .g:listSep. ']\+', a:item, "" )
146
return substitute( a:array, '\(\%(\%(^\|' . g:listSep . '\)[^' . g:listSep . ']\+\)\{' . a:index . '\}\)' . g:listSep . '[^' . g:listSep . ']\+', '\1' . g:listSep . a:item , "" )
152
" Remove item at given position.
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).
158
"String containing array values, except the removed one.
160
function RemoveListItem( array, index )
162
return substitute( a:array, '^[^' .g:listSep. ']\+\(' . g:listSep . '\|$\)', "", "" )
164
return substitute( a:array, '\(\%(\%(^\|' . g:listSep . '\)[^' . g:listSep . ']\+\)\{' . a:index . '\}\)' . g:listSep . '[^' . g:listSep . ']\+', '\1', "" )
170
" Exchange item at position item1Index with item at position item2Index.
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).
178
"String containing array values.
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 )
188
" Number of items in array.
190
" - array : Array/List (string of values).
192
"Integer representing the number of items in array.
193
"Index of last item is GetListCount(array)-1.
195
function GetListCount( array )
196
if a:array == "" | return 0 | endif
200
let pos = matchend( a:array, g:listSep, pos )
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.
214
"String containing array values with indicated range of items sorted.
216
function QuickSortList( array, beg, end )
218
let pivot = GetListItem( array, a:beg )
222
while GetListItem( array, r ) > pivot
226
let array = ReplaceListItem( array, l, GetListItem( array, r ) )
227
let array = ReplaceListItem( array, r, pivot )
231
while GetListItem( array, l ) < pivot
235
let array = ReplaceListItem( array, r, GetListItem( array, l ) )
236
let array = ReplaceListItem( array, l, pivot )
241
let array = QuickSortList( array, a:beg, l-1 )
244
let array = QuickSortList( array, l+1, a:end )