Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge vim-misc repository as subdirectory

  • Loading branch information...
commit 4be4d28e05a08bd804e303645b37a52610a8b2e7 2 parents 552609e + a205f91
Peter Odding authored May 25, 2011
13  autoload/xolox/misc/README.md
Source Rendered
... ...
@@ -0,0 +1,13 @@
  1
+# Miscellaneous auto-load Vim scripts
  2
+
  3
+The git repository at <http://github.com/xolox/vim-misc> contains Vim scripts
  4
+that are used by most of the [Vim plug-ins I've written] [plugins] yet don't
  5
+really belong with any single one. I'm hoping to include this repository as a
  6
+git submodule in my other repositories so that I only have to maintain these
  7
+files in one place.
  8
+
  9
+For lack of a better place: I hereby release these scripts under the MIT
  10
+license, in other words feel free to do with them as you please but don't
  11
+misrepresent this work as your own.
  12
+
  13
+[plugins]: http://peterodding.com/code/vim/
18  autoload/xolox/misc/complete.vim
... ...
@@ -0,0 +1,18 @@
  1
+" Vim auto-load script
  2
+" Author: Peter Odding <peter@peterodding.com>
  3
+" Last Change: March 15, 2011
  4
+" URL: http://peterodding.com/code/vim/misc/
  5
+
  6
+" Keyword completion from the current buffer for user defined commands.
  7
+
  8
+function! xolox#misc#complete#keywords(arglead, cmdline, cursorpos)
  9
+  let words = {}
  10
+  for line in getline(1, '$')
  11
+    for word in split(line, '\W\+')
  12
+      let words[word] = 1
  13
+    endfor
  14
+  endfor
  15
+  return sort(keys(filter(words, 'v:key =~# a:arglead')))
  16
+endfunction
  17
+
  18
+" vim: ts=2 sw=2 et
26  autoload/xolox/misc/escape.vim
... ...
@@ -0,0 +1,26 @@
  1
+" Vim auto-load script
  2
+" Author: Peter Odding <peter@peterodding.com>
  3
+" Last Change: March 15, 2011
  4
+" URL: http://peterodding.com/code/vim/misc/
  5
+
  6
+" Convert a string into a :substitute pattern that matches the string literally.
  7
+
  8
+function! xolox#misc#escape#pattern(string)
  9
+  if type(a:string) == type('')
  10
+    let string = escape(a:string, '^$.*\~[]')
  11
+    return substitute(string, '\n', '\\n', 'g')
  12
+  endif
  13
+  return ''
  14
+endfunction
  15
+
  16
+" Convert a string into a :substitute replacement that inserts the string literally.
  17
+
  18
+function! xolox#misc#escape#substitute(string)
  19
+  if type(a:string) == type('')
  20
+    let string = escape(a:string, '\&~%')
  21
+    return substitute(string, '\n', '\\r', 'g')
  22
+  endif
  23
+  return ''
  24
+endfunction
  25
+
  26
+" vim: ts=2 sw=2 et
23  autoload/xolox/misc/list.vim
... ...
@@ -0,0 +1,23 @@
  1
+" Vim auto-load script
  2
+" Author: Peter Odding <peter@peterodding.com>
  3
+" Last Change: March 15, 2011
  4
+" URL: http://peterodding.com/code/vim/misc/
  5
+
  6
+" Remove duplicate values from {list} in-place (preserves order).
  7
+
  8
+function! xolox#misc#list#unique(list)
  9
+  let index = 0
  10
+  while index < len(a:list)
  11
+    let value = a:list[index]
  12
+    let match = index(a:list, value, index+1)
  13
+    if match >= 0
  14
+      call remove(a:list, match)
  15
+    else
  16
+      let index += 1
  17
+    endif
  18
+    unlet value
  19
+  endwhile
  20
+  return a:list
  21
+endfunction
  22
+
  23
+" vim: ts=2 sw=2 et
83  autoload/xolox/misc/msg.vim
... ...
@@ -0,0 +1,83 @@
  1
+" Vim auto-load script
  2
+" Author: Peter Odding <peter@peterodding.com>
  3
+" Last Change: March 15, 2011
  4
+" URL: http://peterodding.com/code/vim/misc/
  5
+
  6
+if !exists('g:xolox_message_buffer')
  7
+  " For when I lose my :messages history :-\
  8
+  let g:xolox_message_buffer = 100
  9
+endif
  10
+
  11
+if !exists('g:xolox_messages')
  12
+  let g:xolox_messages = []
  13
+endif
  14
+
  15
+" Show a formatted informational message to the user.
  16
+
  17
+function! xolox#misc#msg#info(...)
  18
+  call s:show_message('title', a:000)
  19
+endfunction
  20
+
  21
+" Show a formatted warning message to the user.
  22
+
  23
+function! xolox#misc#msg#warn(...)
  24
+  call s:show_message('warningmsg', a:000)
  25
+endfunction
  26
+
  27
+" Show a formatted debugging message to the user?
  28
+
  29
+function! xolox#misc#msg#debug(...)
  30
+  if &vbs >= 1
  31
+    call s:show_message('question', a:000)
  32
+  endif
  33
+endfunction
  34
+
  35
+" The implementation of info() and warn().
  36
+
  37
+function! s:show_message(hlgroup, args)
  38
+  let nargs = len(a:args)
  39
+  if nargs == 1
  40
+    let message = a:args[0]
  41
+  elseif nargs >= 2
  42
+    let message = call('printf', a:args)
  43
+  endif
  44
+  if exists('message')
  45
+    try
  46
+      " Temporarily disable Vim's |hit-enter| prompt and mode display.
  47
+      if !exists('s:more_save')
  48
+        let s:more_save = &more
  49
+        let s:ruler_save = &ruler
  50
+        let s:smd_save = &showmode
  51
+      endif
  52
+      set nomore noshowmode
  53
+      if winnr('$') == 1 | set noruler | endif
  54
+      augroup PluginXoloxHideMode
  55
+        autocmd! CursorHold,CursorHoldI * call s:clear_message()
  56
+      augroup END
  57
+      execute 'echohl' a:hlgroup
  58
+      " Redraw to avoid |hit-enter| prompt.
  59
+      redraw | echomsg message
  60
+      if g:xolox_message_buffer > 0
  61
+        call add(g:xolox_messages, message)
  62
+        if len(g:xolox_messages) > g:xolox_message_buffer
  63
+          call remove(g:xolox_messages, 0)
  64
+        endif
  65
+      endif
  66
+    finally
  67
+      " Always clear message highlighting, even when interrupted by Ctrl-C.
  68
+      echohl none
  69
+    endtry
  70
+  endif
  71
+endfunction
  72
+
  73
+function! s:clear_message()
  74
+  echo ''
  75
+  let &more = s:more_save
  76
+  let &showmode = s:smd_save
  77
+  let &ruler = s:ruler_save
  78
+  unlet s:more_save s:ruler_save s:smd_save
  79
+  autocmd! PluginXoloxHideMode
  80
+  augroup! PluginXoloxHideMode
  81
+endfunction
  82
+
  83
+" vim: ts=2 sw=2 et
46  autoload/xolox/misc/option.vim
... ...
@@ -0,0 +1,46 @@
  1
+" Vim auto-load script
  2
+" Author: Peter Odding <peter@peterodding.com>
  3
+" Last Change: March 15, 2011
  4
+" URL: http://peterodding.com/code/vim/misc/
  5
+
  6
+" Functions to parse multi-valued Vim options like &tags and &runtimepath.
  7
+
  8
+function! xolox#misc#option#split(value)
  9
+  let values = split(a:value, '[^\\]\zs,')
  10
+  return map(values, 's:unescape(v:val)')
  11
+endfunction
  12
+
  13
+function! s:unescape(s)
  14
+  return substitute(a:s, '\\\([\\,]\)', '\1', 'g')
  15
+endfunction
  16
+
  17
+function! xolox#misc#option#join(values)
  18
+  let values = copy(a:values)
  19
+  call map(values, 's:escape(v:val)')
  20
+  return join(values, ',')
  21
+endfunction
  22
+
  23
+function! s:escape(s)
  24
+  return escape(a:s, ',\')
  25
+endfunction
  26
+
  27
+function! xolox#misc#option#split_tags(value)
  28
+  let values = split(a:value, '[^\\]\zs,')
  29
+  return map(values, 's:unescape_tags(v:val)')
  30
+endfunction
  31
+
  32
+function! s:unescape_tags(s)
  33
+  return substitute(a:s, '\\\([\\, ]\)', '\1', 'g')
  34
+endfunction
  35
+
  36
+function! xolox#misc#option#join_tags(values)
  37
+  let values = copy(a:values)
  38
+  call map(values, 's:escape_tags(v:val)')
  39
+  return join(values, ',')
  40
+endfunction
  41
+
  42
+function! s:escape_tags(s)
  43
+  return escape(a:s, ', ')
  44
+endfunction
  45
+
  46
+" vim: ts=2 sw=2 et
12  autoload/xolox/misc/os.vim
... ...
@@ -0,0 +1,12 @@
  1
+" Vim auto-load script
  2
+" Author: Peter Odding <peter@peterodding.com>
  3
+" Last Change: March 15, 2011
  4
+" URL: http://peterodding.com/code/vim/misc/
  5
+
  6
+" Check whether Vim is running on Microsoft Windows.
  7
+
  8
+function! xolox#misc#os#is_win()
  9
+  return has('win16') || has('win32') || has('win64')
  10
+endfunction
  11
+
  12
+" vim: ts=2 sw=2 et
158  autoload/xolox/misc/path.vim
... ...
@@ -0,0 +1,158 @@
  1
+" Vim auto-load script
  2
+" Author: Peter Odding <peter@peterodding.com>
  3
+" Last Change: March 15, 2011
  4
+" URL: http://peterodding.com/code/vim/misc/
  5
+
  6
+let s:windows_compatible = has('win32') || has('win64')
  7
+
  8
+" Split a pathname into a list of path components.
  9
+
  10
+function! xolox#misc#path#split(path)
  11
+  if type(a:path) == type('')
  12
+    if s:windows_compatible
  13
+      return split(a:path, '[\/]\+')
  14
+    else
  15
+      let absolute = (a:path =~ '^/')
  16
+      let segments = split(a:path, '/\+')
  17
+      return absolute ? insert(segments, '/') : segments
  18
+    endif
  19
+  endif
  20
+  return []
  21
+endfunction
  22
+
  23
+" Join a list of path components into a pathname.
  24
+
  25
+function! xolox#misc#path#join(parts)
  26
+  if type(a:parts) == type([])
  27
+    if !s:windows_compatible && a:parts[0] == '/'
  28
+      return join(a:parts, '/')[1 : -1]
  29
+    else
  30
+      return join(a:parts, '/')
  31
+    endif
  32
+  endif
  33
+  return ''
  34
+endfunction
  35
+
  36
+" Canonicalize and resolve a pathname.
  37
+
  38
+function! xolox#misc#path#absolute(path)
  39
+  if type(a:path) == type('')
  40
+    let path = fnamemodify(a:path, ':p')
  41
+    " resolve() doesn't work when there's a trailing path separator.
  42
+    if path =~ '/$'
  43
+      let stripped_slash = 1
  44
+      let path = substitute(path, '/$', '', '')
  45
+    endif
  46
+    let path = resolve(path)
  47
+    " Restore the path separator after calling resolve().
  48
+    if exists('stripped_slash') && path !~ '/$'
  49
+      let path .= '/'
  50
+    endif
  51
+    return path
  52
+  endif
  53
+  return ''
  54
+endfunction
  55
+
  56
+" Make an absolute pathname relative.
  57
+
  58
+function! xolox#misc#path#relative(path, base)
  59
+  let path = xolox#misc#path#split(a:path)
  60
+  let base = xolox#misc#path#split(a:base)
  61
+  while path != [] && base != [] && path[0] == base[0]
  62
+    call remove(path, 0)
  63
+    call remove(base, 0)
  64
+  endwhile
  65
+  let distance = repeat(['..'], len(base))
  66
+  return xolox#misc#path#join(distance + path)
  67
+endfunction
  68
+
  69
+" Join a directory and filename into a single pathname.
  70
+
  71
+function! xolox#misc#path#merge(parent, child, ...)
  72
+  if type(a:parent) == type('') && type(a:child) == type('')
  73
+    if s:windows_compatible
  74
+      let parent = substitute(a:parent, '[\\/]\+$', '', '')
  75
+      let child = substitute(a:child, '^[\\/]\+', '', '')
  76
+      return parent . '\' . child
  77
+    else
  78
+      let parent = substitute(a:parent, '/\+$', '', '')
  79
+      let child = substitute(a:child, '^/\+', '', '')
  80
+      return parent . '/' . child
  81
+    endif
  82
+  endif
  83
+  return ''
  84
+endfunction
  85
+
  86
+" Find the common prefix of path components in a list of pathnames.
  87
+
  88
+function! xolox#misc#path#commonprefix(paths)
  89
+  let common = xolox#misc#path#split(a:paths[0])
  90
+  for path in a:paths
  91
+    let index = 0
  92
+    for segment in xolox#misc#path#split(path)
  93
+      if len(common) <= index
  94
+        break
  95
+      elseif common[index] != segment
  96
+        call remove(common, index, -1)
  97
+        break
  98
+      endif
  99
+      let index += 1
  100
+    endfor
  101
+  endfor
  102
+  return xolox#misc#path#join(common)
  103
+endfunction
  104
+
  105
+" Encode a pathname so it can be used as a filename.
  106
+
  107
+function! xolox#misc#path#encode(path)
  108
+  let mask = s:windows_compatible ? '[*|\\/:"<>?%]' : '[\\/%]'
  109
+  return substitute(a:path, mask, '\=printf("%%%x", char2nr(submatch(0)))', 'g')
  110
+endfunction
  111
+
  112
+" Decode a pathname previously encoded with xolox#misc#path#encode().
  113
+
  114
+function! xolox#misc#path#decode(encoded_path)
  115
+  return substitute(a:encoded_path, '%\(\x\x\?\)', '\=nr2char("0x" . submatch(1))', 'g')
  116
+endfunction
  117
+
  118
+" Check whether two pathnames point to the same file.
  119
+
  120
+if s:windows_compatible
  121
+  function! xolox#misc#path#equals(a, b)
  122
+    return a:a ==? a:b || xolox#misc#path#absolute(a:a) ==? xolox#misc#path#absolute(a:b)
  123
+  endfunction
  124
+else
  125
+  function! xolox#misc#path#equals(a, b)
  126
+    return a:a ==# a:b || xolox#misc#path#absolute(a:a) ==# xolox#misc#path#absolute(a:b)
  127
+  endfunction
  128
+endif
  129
+
  130
+" Create a temporary directory and return the path.
  131
+
  132
+function! xolox#misc#path#tempdir()
  133
+  if !exists('s:tempdir_counter')
  134
+    let s:tempdir_counter = 1
  135
+  endif
  136
+  if exists('*mkdir')
  137
+    if s:windows_compatible
  138
+      let template = $TMP . '\vim_tempdir_'
  139
+    elseif filewritable('/tmp') == 2
  140
+      let template = '/tmp/vim_tempdir_'
  141
+    endif
  142
+  endif
  143
+  if !exists('template')
  144
+    throw "xolox#misc#path#tempdir() hasn't been implemented on your platform!"
  145
+  endif
  146
+  while 1
  147
+    let directory = template . s:tempdir_counter
  148
+    try
  149
+      call mkdir(directory, '', 0700)
  150
+      return directory
  151
+    catch /\<E739\>/
  152
+      " Keep looking for a non-existing directory.
  153
+    endtry
  154
+    let s:tempdir_counter += 1
  155
+  endwhile
  156
+endfunction
  157
+
  158
+" vim: ts=2 sw=2 et
12  autoload/xolox/misc/str.vim
... ...
@@ -0,0 +1,12 @@
  1
+" Vim auto-load script
  2
+" Author: Peter Odding <peter@peterodding.com>
  3
+" Last Change: March 15, 2011
  4
+" URL: http://peterodding.com/code/vim/misc/
  5
+
  6
+" Trim whitespace from start and end of string.
  7
+
  8
+function! xolox#misc#str#trim(s)
  9
+  return substitute(a:s, '^\s*\(.\{-}\)\s*$', '\1', '')
  10
+endfunction
  11
+
  12
+" vim: ts=2 sw=2 et
85  autoload/xolox/misc/timer.vim
... ...
@@ -0,0 +1,85 @@
  1
+" Vim auto-load script
  2
+" Author: Peter Odding <peter@peterodding.com>
  3
+" Last Change: March 15, 2011
  4
+" URL: http://peterodding.com/code/vim/misc/
  5
+
  6
+if !exists('g:timer_enabled')
  7
+  let g:timer_enabled = 0
  8
+endif
  9
+
  10
+if !exists('g:timer_verbosity')
  11
+  let g:timer_verbosity = 1
  12
+endif
  13
+
  14
+let s:has_reltime = has('reltime')
  15
+
  16
+" Start a timer.
  17
+
  18
+function! xolox#misc#timer#start()
  19
+  if g:timer_enabled || &verbose >= g:timer_verbosity
  20
+    return s:has_reltime ? reltime() : [localtime()]
  21
+  endif
  22
+  return []
  23
+endfunction
  24
+
  25
+" Stop a timer and print the elapsed time (only if the user is interested).
  26
+
  27
+function! xolox#misc#timer#stop(...)
  28
+  if (g:timer_enabled || &verbose >= g:timer_verbosity)
  29
+    call call('xolox#misc#msg#info', map(copy(a:000), 's:convert_value(v:val)'))
  30
+  endif
  31
+endfunction
  32
+
  33
+function! s:convert_value(value)
  34
+  if type(a:value) != type([])
  35
+    return a:value
  36
+  elseif !empty(a:value)
  37
+    if s:has_reltime
  38
+      let ts = xolox#misc#str#trim(reltimestr(reltime(a:value)))
  39
+    else
  40
+      let ts = localtime() - a:value[0]
  41
+    endif
  42
+    return xolox#misc#timer#format_timespan(ts)
  43
+  else
  44
+    return '?'
  45
+  endif
  46
+endfunction
  47
+
  48
+" Format number of seconds as human friendly description.
  49
+
  50
+let s:units = [['day', 60 * 60 * 24], ['hour', 60 * 60], ['minute', 60], ['second', 1]]
  51
+
  52
+function! xolox#misc#timer#format_timespan(ts)
  53
+
  54
+  " Convert timespan to integer.
  55
+  let seconds = a:ts + 0
  56
+
  57
+  " Fast common case with extra precision from reltime().
  58
+  if seconds < 5
  59
+    let extract = matchstr(a:ts, '^\d\+\(\.0*[1-9][1-9]\?\)\?')
  60
+    if extract =~ '[123456789]'
  61
+      return extract . ' second' . (extract != '1' ? 's' : '')
  62
+    endif
  63
+  endif
  64
+
  65
+  " Generic but slow code.
  66
+  let result = []
  67
+  for [name, size] in s:units
  68
+    if seconds >= size
  69
+      let counter = seconds / size
  70
+      let seconds = seconds % size
  71
+      let suffix = counter != 1 ? 's' : ''
  72
+      call add(result, printf('%i %s%s', counter, name, suffix))
  73
+    endif
  74
+  endfor
  75
+
  76
+  " Format the resulting text?
  77
+  if len(result) == 1
  78
+    return result[0]
  79
+  else
  80
+    return join(result[0:-2], ', ') . ' and ' . result[-1]
  81
+  endif
  82
+
  83
+endfunction
  84
+
  85
+" vim: ts=2 sw=2 et

0 notes on commit 4be4d28

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