Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit c1c9496ebe9c1794cd0c8e41d6f96a732007e15a 2 parents 56e23b6 + 2cfd515
@xolox authored
View
29 autoload/xolox/misc/README.md
@@ -1,13 +1,24 @@
# Miscellaneous auto-load Vim scripts
-The git repository at <http://github.com/xolox/vim-misc> contains Vim scripts
-that are used by most of the [Vim plug-ins I've written] [plugins] yet don't
-really belong with any single one. I'm hoping to include this repository as a
-git submodule in my other repositories so that I only have to maintain these
-files in one place.
-
-For lack of a better place: I hereby release these scripts under the MIT
-license, in other words feel free to do with them as you please but don't
-misrepresent this work as your own.
+The git repository at <http://github.com/xolox/vim-misc> contains Vim scripts that are used by most of the [Vim plug-ins I've written] [plugins] yet don't really belong with any single one. I include this repository as a subdirectory of my plug-in repositories using the following commands:
+
+ $ git remote add -f vim-misc https://github.com/xolox/vim-misc.git
+ $ git merge -s ours --no-commit vim-misc/master
+ $ git read-tree --prefix=autoload/xolox/misc/ -u vim-misc/master
+ $ git commit -m "Merge vim-misc repository as subdirectory"
+
+To update a plug-in repository to the latest versions of the miscellaneous auto-load scripts I execute the following command:
+
+ $ git pull -s subtree vim-misc master
+
+## Contact
+
+If you have questions, bug reports, suggestions, etc. the author can be contacted at <peter@peterodding.com>. The latest version is available at <http://peterodding.com/code/vim/misc> and <http://github.com/xolox/vim-misc>.
+
+## License
+
+This software is licensed under the [MIT license](http://en.wikipedia.org/wiki/MIT_License).
+© 2011 Peter Odding &lt;<peter@peterodding.com>&gt;.
+
[plugins]: http://peterodding.com/code/vim/
View
37 autoload/xolox/misc/buffer.vim
@@ -0,0 +1,37 @@
+" Vim auto-load script
+" Author: Peter Odding <peter@peterodding.com>
+" Last Change: September 4, 2011
+" URL: http://peterodding.com/code/vim/misc/
+
+function! xolox#misc#buffer#is_empty()
+ " Check if the current buffer is an empty, unchanged buffer which can be reused.
+ return !&modified && expand('%') == '' && line('$') <= 1 && getline(1) == ''
+endfunction
+
+function! xolox#misc#buffer#prepare(bufname)
+ let bufname = '[' . a:bufname . ']'
+ let buffers = tabpagebuflist()
+ call map(buffers, 'fnamemodify(bufname(v:val), ":t:r")')
+ let idx = index(buffers, bufname)
+ if idx >= 0
+ execute (idx + 1) . 'wincmd w'
+ elseif !(xolox#misc#buffer#is_empty() || expand('%:t') == bufname)
+ vsplit
+ endif
+ silent execute 'edit' fnameescape(bufname)
+ lcd " clear working directory
+ setlocal buftype=nofile bufhidden=hide noswapfile
+ let &l:statusline = bufname
+ call xolox#misc#buffer#unlock()
+ silent %delete
+endfunction
+
+function! xolox#misc#buffer#lock()
+ " Lock a special buffer so it can no longer be edited.
+ setlocal readonly nomodifiable nomodified
+endfunction
+
+function! xolox#misc#buffer#unlock()
+ " Unlock a special buffer so that its content can be updated.
+ setlocal noreadonly modifiable
+endfunction
View
37 autoload/xolox/misc/list.vim
@@ -1,6 +1,6 @@
" Vim auto-load script
" Author: Peter Odding <peter@peterodding.com>
-" Last Change: March 15, 2011
+" Last Change: August 31, 2011
" URL: http://peterodding.com/code/vim/misc/
" Remove duplicate values from {list} in-place (preserves order).
@@ -20,4 +20,39 @@ function! xolox#misc#list#unique(list)
return a:list
endfunction
+" Binary insertion (more efficient than calling sort() after each insertion).
+
+function! xolox#misc#list#binsert(list, value, ...)
+ let idx = s:binsert_r(a:list, 0, len(a:list), a:value, exists('a:1') && a:1)
+ return insert(a:list, a:value, idx)
+endfunction
+
+function! s:binsert_r(list, low, high, value, ignorecase)
+ let mid = a:low + (a:high - a:low) / 2
+ if a:low == a:high
+ return a:low
+ elseif a:ignorecase ? a:value >? a:list[mid] : a:value > a:list[mid]
+ return s:binsert_r(a:list, mid + 1, a:high, a:value, a:ignorecase)
+ elseif a:ignorecase ? a:value <? a:list[mid] : a:value < a:list[mid]
+ return s:binsert_r(a:list, a:low, mid, a:value, a:ignorecase)
+ else
+ return mid
+ endif
+endfunction
+
+if 0
+ " Tests for xolox#misc#list#binsert().
+ let s:list = ['a', 'B', 'e']
+ function! s:test(value, expected)
+ call xolox#misc#list#binsert(s:list, a:value, 1)
+ if s:list != a:expected
+ call xolox#misc#msg#warn("list.vim: Test failed! Expected %s, got %s",
+ \ string(a:expected), string(s:list))
+ endif
+ endfunction
+ call s:test('c', ['a', 'B', 'c', 'e'])
+ call s:test('D', ['a', 'B', 'c', 'D', 'e'])
+ call s:test('f', ['a', 'B', 'c', 'D', 'e', 'f'])
+endif
+
" vim: ts=2 sw=2 et
View
70 autoload/xolox/misc/open.vim
@@ -0,0 +1,70 @@
+" Vim auto-load script
+" Author: Peter Odding <peter@peterodding.com>
+" Last Change: June 18, 2011
+" URL: http://peterodding.com/code/vim/misc/
+
+if !exists('s:version')
+ let s:version = '1.0'
+ let s:enoimpl = "open.vim %s: %s() hasn't been implemented for your platform! If you have suggestions, please contact peter@peterodding.com."
+ let s:handlers = ['gnome-open', 'kde-open', 'exo-open', 'xdg-open']
+endif
+
+function! xolox#misc#open#file(path, ...)
+ if xolox#misc#os#is_win()
+ try
+ call xolox#shell#open_with_windows_shell(a:path)
+ catch /^Vim\%((\a\+)\)\=:E117/
+ let command = '!start CMD /C START "" %s'
+ silent execute printf(command, shellescape(a:path))
+ endtry
+ return
+ elseif has('macunix')
+ let cmd = 'open ' . shellescape(a:path) . ' 2>&1'
+ call s:handle_error(cmd, system(cmd))
+ return
+ else
+ for handler in s:handlers + a:000
+ if executable(handler)
+ call xolox#misc#msg#debug("open.vim %s: Using '%s' to open '%s'.", s:version, handler, a:path)
+ let cmd = shellescape(handler) . ' ' . shellescape(a:path) . ' 2>&1'
+ call s:handle_error(cmd, system(cmd))
+ return
+ endif
+ endfor
+ endif
+ throw printf(s:enoimpl, s:script, 'xolox#misc#open#file')
+endfunction
+
+function! xolox#misc#open#url(url)
+ let url = a:url
+ if url !~ '^\w\+://'
+ if url !~ '@'
+ let url = 'http://' . url
+ elseif url !~ '^mailto:'
+ let url = 'mailto:' . url
+ endif
+ endif
+ if has('unix') && !has('gui_running') && $DISPLAY == ''
+ for browser in ['lynx', 'links', 'w3m']
+ if executable(browser)
+ execute '!' . browser fnameescape(url)
+ call s:handle_error(browser . ' ' . url, '')
+ return
+ endif
+ endfor
+ endif
+ call xolox#misc#open#file(url, 'firefox', 'google-chrome')
+endfunction
+
+function! s:handle_error(cmd, output)
+ if v:shell_error
+ let message = "open.vim %s: Failed to execute program! (command line: %s%s)"
+ let output = strtrans(xolox#misc#str#trim(a:output))
+ if output != ''
+ let output = ", output: " . string(output)
+ endif
+ throw printf(message, s:version, a:cmd, output)
+ endif
+endfunction
+
+" vim: et ts=2 sw=2 fdm=marker
View
40 autoload/xolox/misc/option.vim
@@ -1,8 +1,21 @@
" Vim auto-load script
" Author: Peter Odding <peter@peterodding.com>
-" Last Change: March 15, 2011
+" Last Change: August 31, 2011
" URL: http://peterodding.com/code/vim/misc/
+function! xolox#misc#option#get(name, ...)
+ if exists('b:' . a:name)
+ " Buffer local variable.
+ return eval('b:' . a:name)
+ elseif exists('g:' . a:name)
+ " Global variable.
+ return eval('g:' . a:name)
+ elseif exists('a:1')
+ " Default value.
+ return a:1
+ endif
+endfunction
+
" Functions to parse multi-valued Vim options like &tags and &runtimepath.
function! xolox#misc#option#split(value)
@@ -43,4 +56,29 @@ function! s:escape_tags(s)
return escape(a:s, ', ')
endfunction
+function! xolox#misc#option#eval_tags(value, ...)
+ let pathnames = []
+ let first_only = exists('a:1') && a:1
+ for pattern in xolox#misc#option#split_tags(a:value)
+ " Make buffer relative pathnames absolute.
+ if pattern =~ '^\./'
+ let directory = xolox#misc#escape#substitute(expand('%:p:h'))
+ let pattern = substitute(pattern, '^.\ze/', directory, '')
+ endif
+ " Make working directory relative pathnames absolute.
+ if xolox#misc#path#is_relative(pattern)
+ let pattern = xolox#misc#path#merge(getcwd(), pattern)
+ endif
+ " Ignore the trailing `;' for recursive upwards searching because we
+ " always want the most specific pathname available.
+ let pattern = substitute(pattern, ';$', '', '')
+ " Expand the pattern.
+ call extend(pathnames, split(expand(pattern), "\n"))
+ if first_only && !empty(pathnames)
+ return pathnames[0]
+ endif
+ endfor
+ return firstonly ? '' : pathnames
+endfunction
+
" vim: ts=2 sw=2 et
View
15 autoload/xolox/misc/path.vim
@@ -1,6 +1,6 @@
" Vim auto-load script
" Author: Peter Odding <peter@peterodding.com>
-" Last Change: March 15, 2011
+" Last Change: August 31, 2011
" URL: http://peterodding.com/code/vim/misc/
let s:windows_compatible = has('win32') || has('win64')
@@ -69,6 +69,7 @@ endfunction
" Join a directory and filename into a single pathname.
function! xolox#misc#path#merge(parent, child, ...)
+ " TODO Use isabs()!
if type(a:parent) == type('') && type(a:child) == type('')
if s:windows_compatible
let parent = substitute(a:parent, '[\\/]\+$', '', '')
@@ -127,6 +128,18 @@ else
endfunction
endif
+" Check whether a path is relative.
+
+function! xolox#misc#path#is_relative(path)
+ if a:path =~ '^\w\+://'
+ return 0
+ elseif s:windows_compatible
+ return a:path !~ '^\(\w:\|[\\/]\)'
+ else
+ return a:path !~ '^/'
+ endif
+endfunction
+
" Create a temporary directory and return the path.
function! xolox#misc#path#tempdir()
View
4 autoload/xolox/misc/str.vim
@@ -1,12 +1,12 @@
" Vim auto-load script
" Author: Peter Odding <peter@peterodding.com>
-" Last Change: March 15, 2011
+" Last Change: June 14, 2011
" URL: http://peterodding.com/code/vim/misc/
" Trim whitespace from start and end of string.
function! xolox#misc#str#trim(s)
- return substitute(a:s, '^\s*\(.\{-}\)\s*$', '\1', '')
+ return substitute(a:s, '^\_s*\(.\{-}\)\_s*$', '\1', '')
endfunction
" vim: ts=2 sw=2 et
Please sign in to comment.
Something went wrong with that request. Please try again.