Skip to content

Commit

Permalink
Updated miscellaneous scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Apr 30, 2013
2 parents 5c60686 + 38ded48 commit d956287
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 48 deletions.
6 changes: 3 additions & 3 deletions autoload/xolox/lua.vim
@@ -1,14 +1,14 @@
" Vim auto-load script
" Author: Peter Odding <peter@peterodding.com>
" Last Change: April 28, 2013
" Last Change: April 30, 2013
" URL: http://peterodding.com/code/vim/lua-ftplugin

let g:xolox#lua#version = '0.7.2'
let g:xolox#lua#version = '0.7.3'
let s:miscdir = expand('<sfile>:p:h:h:h') . '/misc/lua-ftplugin'
let s:omnicomplete_script = s:miscdir . '/omnicomplete.lua'
let s:globals_script = s:miscdir . '/globals.lua'

call xolox#misc#compat#check('lua-ftplugin', 1)
call xolox#misc#compat#check('lua-ftplugin', 2)

function! xolox#lua#includeexpr(fname) " {{{1
" Search module path for matching Lua scripts.
Expand Down
4 changes: 2 additions & 2 deletions autoload/xolox/misc/compat.vim
@@ -1,13 +1,13 @@
" Vim auto-load script
" Author: Peter Odding <peter@peterodding.com>
" Last Change: April 20, 2013
" Last Change: April 28, 2013
" URL: http://peterodding.com/code/vim/misc/

" The following integer will be bumped whenever a change in the miscellaneous
" scripts breaks backwards compatibility. This enables my Vim plug-ins to fail
" early when they detect an incompatible version, instead of breaking at the
" worst possible moments :-).
let g:xolox#misc#compat#version = 1
let g:xolox#misc#compat#version = 2

" Remember the directory where the miscellaneous scripts are loaded from
" so the user knows which plug-in to update if incompatibilities arise.
Expand Down
113 changes: 70 additions & 43 deletions autoload/xolox/misc/path.vim
@@ -1,12 +1,13 @@
" Vim auto-load script
" Author: Peter Odding <peter@peterodding.com>
" Last Change: April 18, 2013
" Last Change: April 28, 2013
" URL: http://peterodding.com/code/vim/misc/

let s:windows_compatible = has('win32') || has('win64')
let s:mac_os_x_compatible = has('macunix')

function! xolox#misc#path#which(...)
function! xolox#misc#path#which(...) " {{{1
" Scan the executable search path for programs.
let extensions = s:windows_compatible ? split($PATHEXT, ';') : ['']
let matches = []
let checked = {}
Expand All @@ -29,13 +30,24 @@ function! xolox#misc#path#which(...)
return matches
endfunction

" Split a pathname into a list of path components.

function! xolox#misc#path#split(path)
function! xolox#misc#path#split(path) " {{{1
" Split a pathname into a list of path components.
if type(a:path) == type('')
if s:windows_compatible
return split(a:path, '[\/]\+')
if a:path =~ '^[\/][\/]'
" On Windows, pathnames starting with two slashes or backslashes are
" UNC paths where the leading slashes are significant... In this case
" we split like this:
" '//server/share/directory' -> ['//server', 'share', 'directory']
return split(a:path, '\%>2c[\/]\+')
else
" If it's not a UNC path we can simply split on slashes & backslashes.
return split(a:path, '[\/]\+')
endif
else
" Everything except Windows is treated like UNIX until someone
" has a better suggestion :-). In this case we split like this:
" '/foo/bar/baz' -> ['/', 'foo', 'bar', 'baz']
let absolute = (a:path =~ '^/')
let segments = split(a:path, '/\+')
return absolute ? insert(segments, '/') : segments
Expand All @@ -44,42 +56,61 @@ function! xolox#misc#path#split(path)
return []
endfunction

" Join a list of path components into a pathname.

function! xolox#misc#path#join(parts)
function! xolox#misc#path#join(parts) " {{{1
" Join a list of pathname components into a single pathname.
if type(a:parts) == type([])
if !s:windows_compatible && a:parts[0] == '/'
return join(a:parts, '/')[1 : -1]
if s:windows_compatible
return join(a:parts, xolox#misc#path#directory_separator())
elseif a:parts[0] == '/'
" Absolute path on UNIX (non-Windows).
return '/' . join(a:parts[1:], '/')
else
" Relative path on UNIX (non-Windows).
return join(a:parts, '/')
endif
endif
return ''
endfunction

" Canonicalize and resolve a pathname.
function! xolox#misc#path#directory_separator() " {{{1
" Find the preferred directory separator for the platform and settings.
return exists('+shellslash') && &shellslash ? '/' : '\'
endfunction

function! xolox#misc#path#absolute(path)
function! xolox#misc#path#absolute(path) " {{{1
" Canonicalize and resolve a pathname, regardless of whether it exists. This
" is intended to support string comparison to determine whether two pathnames
" point to the same directory or file.
if type(a:path) == type('')
let path = fnamemodify(a:path, ':p')
" resolve() doesn't work when there's a trailing path separator.
if path =~ '/$'
let stripped_slash = 1
let path = substitute(path, '/$', '', '')
let path = a:path
" Make the pathname absolute.
if path =~ '^\~'
" Expand ~ to $HOME.
let path = $HOME . '/' . path[1:]
elseif xolox#misc#path#is_relative(path)
" Make relative pathnames absolute.
let path = getcwd() . '/' . path
endif
let path = resolve(path)
" Restore the path separator after calling resolve().
if exists('stripped_slash') && path !~ '/$'
let path .= '/'
" Resolve symbolic links to find the canonical pathname. In my tests this
" also removes all symbolic pathname segments (`.' and `..'), even when
" the pathname does not exist. Also there used to be a bug in resolve()
" where it wouldn't resolve pathnames ending in a directory separator.
" Since it's not much trouble to work around, that's what we do.
let path = resolve(substitute(path, s:windows_compatible ? '[\/]\+$' : '/\+$', '', ''))
" Normalize directory separators (especially relevant on Windows).
let parts = xolox#misc#path#split(path)
if s:windows_compatible && parts[0] =~ '^[\/][\/]'
" Also normalize the two leading "directory separators" (I'm not
" sure what else to call them :-) in Windows UNC pathnames.
let parts[0] = repeat(xolox#misc#path#directory_separator(), 2) . parts[0][2:]
endif
return path
return xolox#misc#path#join(parts)
endif
return ''
endfunction

" Make an absolute pathname relative.

function! xolox#misc#path#relative(path, base)
function! xolox#misc#path#relative(path, base) " {{{1
" Make an absolute pathname relative.
let path = xolox#misc#path#split(a:path)
let base = xolox#misc#path#split(a:base)
while path != [] && base != [] && path[0] == base[0]
Expand All @@ -90,9 +121,9 @@ function! xolox#misc#path#relative(path, base)
return xolox#misc#path#join(distance + path)
endfunction

" Join a directory and filename into a single pathname.

function! xolox#misc#path#merge(parent, child, ...)
function! xolox#misc#path#merge(parent, child, ...) " {{{1
" Join a directory and filename into a single pathname.
" TODO Use isabs()!
if type(a:parent) == type('') && type(a:child) == type('')
if s:windows_compatible
Expand All @@ -108,9 +139,8 @@ function! xolox#misc#path#merge(parent, child, ...)
return ''
endfunction

" Find the common prefix of path components in a list of pathnames.

function! xolox#misc#path#commonprefix(paths)
function! xolox#misc#path#commonprefix(paths) " {{{1
" Find the common prefix of path components in a list of pathnames.
let common = xolox#misc#path#split(a:paths[0])
for path in a:paths
let index = 0
Expand All @@ -127,9 +157,8 @@ function! xolox#misc#path#commonprefix(paths)
return xolox#misc#path#join(common)
endfunction

" Encode a pathname so it can be used as a filename.

function! xolox#misc#path#encode(path)
function! xolox#misc#path#encode(path) " {{{1
" Encode a pathname so it can be used as a filename.
if s:windows_compatible
let mask = '[*|\\/:"<>?%]'
elseif s:mac_os_x_compatible
Expand All @@ -140,13 +169,13 @@ function! xolox#misc#path#encode(path)
return substitute(a:path, mask, '\=printf("%%%x", char2nr(submatch(0)))', 'g')
endfunction

" Decode a pathname previously encoded with xolox#misc#path#encode().

function! xolox#misc#path#decode(encoded_path)
function! xolox#misc#path#decode(encoded_path) " {{{1
" Decode a pathname previously encoded with xolox#misc#path#encode().
return substitute(a:encoded_path, '%\(\x\x\?\)', '\=nr2char("0x" . submatch(1))', 'g')
endfunction

" Check whether two pathnames point to the same file.
" xolox#misc#path#equals(a, b) - Check whether two pathnames point to the same file. {{{1

if s:windows_compatible
function! xolox#misc#path#equals(a, b)
Expand All @@ -158,9 +187,8 @@ else
endfunction
endif

" Check whether a path is relative.

function! xolox#misc#path#is_relative(path)
function! xolox#misc#path#is_relative(path) " {{{1
" Check whether a path is relative.
if a:path =~ '^\w\+://'
return 0
elseif s:windows_compatible
Expand All @@ -170,9 +198,8 @@ function! xolox#misc#path#is_relative(path)
endif
endfunction

" Create a temporary directory and return the path.

function! xolox#misc#path#tempdir()
function! xolox#misc#path#tempdir() " {{{1
" Create a temporary directory and return the path.
if !exists('s:tempdir_counter')
let s:tempdir_counter = 1
endif
Expand Down

0 comments on commit d956287

Please sign in to comment.