Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'master' of https://github.com/xolox/vim-misc

  • Loading branch information...
commit 8310041c6f61534111648af0e45a1d054fa403ee 2 parents 168d0cd + 571abe3
Peter Odding authored
37  autoload/xolox/misc/list.vim
... ...
@@ -1,6 +1,6 @@
1 1
 " Vim auto-load script
2 2
 " Author: Peter Odding <peter@peterodding.com>
3  
-" Last Change: March 15, 2011
  3
+" Last Change: August 31, 2011
4 4
 " URL: http://peterodding.com/code/vim/misc/
5 5
 
6 6
 " Remove duplicate values from {list} in-place (preserves order).
@@ -20,4 +20,39 @@ function! xolox#misc#list#unique(list)
20 20
   return a:list
21 21
 endfunction
22 22
 
  23
+" Binary insertion (more efficient than calling sort() after each insertion).
  24
+
  25
+function! xolox#misc#list#binsert(list, value, ...)
  26
+  let idx = s:binsert_r(a:list, 0, len(a:list), a:value, exists('a:1') && a:1)
  27
+  return insert(a:list, a:value, idx)
  28
+endfunction
  29
+
  30
+function! s:binsert_r(list, low, high, value, ignorecase)
  31
+  let mid = a:low + (a:high - a:low) / 2
  32
+  if a:low == a:high
  33
+    return a:low
  34
+  elseif a:ignorecase ? a:value >? a:list[mid] : a:value > a:list[mid]
  35
+    return s:binsert_r(a:list, mid + 1, a:high, a:value, a:ignorecase)
  36
+  elseif a:ignorecase ? a:value <? a:list[mid] : a:value < a:list[mid]
  37
+    return s:binsert_r(a:list, a:low, mid, a:value, a:ignorecase)
  38
+  else
  39
+    return mid
  40
+  endif
  41
+endfunction
  42
+
  43
+if 0
  44
+  " Tests for xolox#misc#list#binsert().
  45
+  let s:list = ['a', 'B', 'e']
  46
+  function! s:test(value, expected)
  47
+    call xolox#misc#list#binsert(s:list, a:value, 1)
  48
+    if s:list != a:expected
  49
+      call xolox#misc#msg#warn("list.vim: Test failed! Expected %s, got %s",
  50
+            \ string(a:expected), string(s:list))
  51
+    endif
  52
+  endfunction
  53
+  call s:test('c', ['a', 'B', 'c', 'e'])
  54
+  call s:test('D', ['a', 'B', 'c', 'D', 'e'])
  55
+  call s:test('f', ['a', 'B', 'c', 'D', 'e', 'f'])
  56
+endif
  57
+
23 58
 " vim: ts=2 sw=2 et
27  autoload/xolox/misc/option.vim
... ...
@@ -1,6 +1,6 @@
1 1
 " Vim auto-load script
2 2
 " Author: Peter Odding <peter@peterodding.com>
3  
-" Last Change: June 27, 2011
  3
+" Last Change: August 31, 2011
4 4
 " URL: http://peterodding.com/code/vim/misc/
5 5
 
6 6
 function! xolox#misc#option#get(name, ...)
@@ -56,4 +56,29 @@ function! s:escape_tags(s)
56 56
   return escape(a:s, ', ')
57 57
 endfunction
58 58
 
  59
+function! xolox#misc#option#eval_tags(value, ...)
  60
+  let pathnames = []
  61
+  let first_only = exists('a:1') && a:1
  62
+  for pattern in xolox#misc#option#split_tags(a:value)
  63
+    " Make buffer relative pathnames absolute.
  64
+    if pattern =~ '^\./'
  65
+      let directory = xolox#misc#escape#substitute(expand('%:p:h'))
  66
+      let pattern = substitute(pattern, '^.\ze/', directory, '')
  67
+    endif
  68
+    " Make working directory relative pathnames absolute.
  69
+    if xolox#misc#path#is_relative(pattern)
  70
+      let pattern = xolox#misc#path#merge(getcwd(), pattern)
  71
+    endif
  72
+    " Ignore the trailing `;' for recursive upwards searching because we
  73
+    " always want the most specific pathname available.
  74
+    let pattern = substitute(pattern, ';$', '', '')
  75
+    " Expand the pattern.
  76
+    call extend(pathnames, split(expand(pattern), "\n"))
  77
+    if first_only && !empty(pathnames)
  78
+      return pathnames[0]
  79
+    endif
  80
+  endfor
  81
+  return firstonly ? '' : pathnames
  82
+endfunction
  83
+
59 84
 " vim: ts=2 sw=2 et
15  autoload/xolox/misc/path.vim
... ...
@@ -1,6 +1,6 @@
1 1
 " Vim auto-load script
2 2
 " Author: Peter Odding <peter@peterodding.com>
3  
-" Last Change: March 15, 2011
  3
+" Last Change: August 31, 2011
4 4
 " URL: http://peterodding.com/code/vim/misc/
5 5
 
6 6
 let s:windows_compatible = has('win32') || has('win64')
@@ -69,6 +69,7 @@ endfunction
69 69
 " Join a directory and filename into a single pathname.
70 70
 
71 71
 function! xolox#misc#path#merge(parent, child, ...)
  72
+  " TODO Use isabs()!
72 73
   if type(a:parent) == type('') && type(a:child) == type('')
73 74
     if s:windows_compatible
74 75
       let parent = substitute(a:parent, '[\\/]\+$', '', '')
@@ -127,6 +128,18 @@ else
127 128
   endfunction
128 129
 endif
129 130
 
  131
+" Check whether a path is relative.
  132
+
  133
+function! xolox#misc#path#is_relative(path)
  134
+  if a:path =~ '^\w\+://'
  135
+    return 0
  136
+  elseif s:windows_compatible
  137
+    return a:path !~ '^\(\w:\|[\\/]\)'
  138
+  else
  139
+    return a:path !~ '^/'
  140
+  endif
  141
+endfunction
  142
+
130 143
 " Create a temporary directory and return the path.
131 144
 
132 145
 function! xolox#misc#path#tempdir()

0 notes on commit 8310041

Please sign in to comment.
Something went wrong with that request. Please try again.