Permalink
Browse files

Updated miscellaneous scripts

  • Loading branch information...
2 parents c22f65c + 38ded48 commit 1cddae8d9d4dadd59a9c3f3863b6130089e8f904 @xolox committed Apr 30, 2013
Showing with 75 additions and 48 deletions.
  1. +2 −2 autoload/xolox/misc/compat.vim
  2. +70 −43 autoload/xolox/misc/path.vim
  3. +3 −3 autoload/xolox/notes.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.
@@ -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 = {}
@@ -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
@@ -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]
@@ -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
@@ -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
@@ -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
@@ -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)
@@ -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
@@ -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
@@ -1,15 +1,15 @@
" 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/notes/
" Note: This file is encoded in UTF-8 including a byte order mark so
" that Vim loads the script using the right encoding transparently.
-let g:xolox#notes#version = '0.17.9'
+let g:xolox#notes#version = '0.17.10'
let s:scriptdir = expand('<sfile>:p:h')
-call xolox#misc#compat#check('notes', 1)
+call xolox#misc#compat#check('notes', 2)
function! xolox#notes#init() " {{{1
" Initialize the configuration of the notes plug-in. This is a bit tricky:

0 comments on commit 1cddae8

Please sign in to comment.