Skip to content

Commit 8522483

Browse files
committed
Updated miscellaneous scripts
2 parents 24827be + 37a9bf4 commit 8522483

File tree

14 files changed

+358
-143
lines changed

14 files changed

+358
-143
lines changed

autoload/xolox/misc/buffer.vim

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,44 @@
1-
" Vim auto-load script
1+
" Handling of special buffers
2+
"
23
" Author: Peter Odding <peter@peterodding.com>
3-
" Last Change: April 18, 2013
4+
" Last Change: May 19, 2013
45
" URL: http://peterodding.com/code/vim/misc/
6+
"
7+
" The functions defined here make it easier to deal with special Vim buffers
8+
" that contain text generated by a Vim plug-in. For example my [vim-notes
9+
" plug-in] [vim-notes] generates several such buffers:
10+
"
11+
" - [:RecentNotes] [RecentNotes] lists recently modified notes
12+
" - [:ShowTaggedNotes] [ShowTaggedNotes] lists notes grouped by tags
13+
" - etc.
14+
"
15+
" Because the text in these buffers is generated, Vim shouldn't bother with
16+
" swap files and it should never prompt the user whether to save changes to
17+
" the generated text.
18+
"
19+
" [vim-notes]: http://peterodding.com/code/vim/notes/
20+
" [RecentNotes]: http://peterodding.com/code/vim/notes/#recentnotes_command
21+
" [ShowTaggedNotes]: http://peterodding.com/code/vim/notes/#showtaggednotes_command
522

623
function! xolox#misc#buffer#is_empty() " {{{1
7-
" Check if the current buffer is an empty, unchanged buffer which can be reused.
24+
" Checks if the current buffer is an empty, unchanged buffer which can be
25+
" reused. Returns 1 if an empty buffer is found, 0 otherwise.
826
return !&modified && expand('%') == '' && line('$') <= 1 && getline(1) == ''
927
endfunction
1028

1129
function! xolox#misc#buffer#prepare(...) " {{{1
12-
" Open a special buffer (with generated contents, not directly edited by the user).
30+
" Open a special buffer, i.e. a buffer that will hold generated contents,
31+
" not directly edited by the user. The buffer can be customized by passing a
32+
" dictionary with the following key/value pairs as the first argument:
33+
"
34+
" - **name** (required): The base name of the buffer (i.e. the base name of
35+
" the file loaded in the buffer, even though it isn't really a file and
36+
" nothing is really 'loaded' :-)
37+
" - **path** (required): The pathname of the buffer. May be relevant if
38+
" [:lcd] [lcd] or ['autochdir'] [acd] is being used.
39+
"
40+
" [lcd]: http://vimdoc.sourceforge.net/htmldoc/editing.html#:lcd
41+
" [acd]: http://vimdoc.sourceforge.net/htmldoc/options.html#'autochdir'
1342
if a:0 == 1 && type(a:1) == type('')
1443
" Backwards compatibility with old interface.
1544
let options = {'name': a:1, 'path': a:1}
@@ -41,7 +70,7 @@ function! xolox#misc#buffer#prepare(...) " {{{1
4170
endfunction
4271

4372
function! xolox#misc#buffer#lock() " {{{1
44-
" Lock a special buffer so it can no longer be edited.
73+
" Lock a special buffer so that its contents can no longer be edited.
4574
setlocal readonly nomodifiable nomodified
4675
endfunction
4776

autoload/xolox/misc/compat.vim

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
1-
" Vim auto-load script
1+
" Compatibility checking.
2+
"
23
" Author: Peter Odding <peter@peterodding.com>
3-
" Last Change: May 13, 2013
4+
" Last Change: May 20, 2013
45
" URL: http://peterodding.com/code/vim/misc/
5-
6-
" The following integer will be bumped whenever a change in the miscellaneous
6+
"
7+
" This Vim script defines a version number for the miscellaneous scripts. Each
8+
" of my plug-ins compares their expected version of the miscellaneous scripts
9+
" against the version number defined inside the miscellaneous scripts.
10+
"
11+
" The version number is incremented whenever a change in the miscellaneous
712
" scripts breaks backwards compatibility. This enables my Vim plug-ins to fail
813
" early when they detect an incompatible version, instead of breaking at the
914
" worst possible moments :-).
10-
let g:xolox#misc#compat#version = 3
15+
let g:xolox#misc#compat#version = 7
1116

1217
" Remember the directory where the miscellaneous scripts are loaded from
1318
" so the user knows which plug-in to update if incompatibilities arise.
14-
let s:misc_directory = fnamemodify(expand('<sfile>'), ':p:h')
19+
let s:misc_directory = fnamemodify(expand('<sfile>'), ':~:h')
1520

16-
function! xolox#misc#compat#check(plugin_name, required_version)
21+
function! xolox#misc#compat#check(plugin_name, plugin_version, required_version)
22+
" Expects three arguments:
23+
"
24+
" 1. The name of the Vim plug-in that is using the miscellaneous scripts
25+
" 2. The version of the Vim plug-in that is using the miscellaneous scripts
26+
" 3. The version of the miscellaneous scripts expected by the plug-in
27+
"
28+
" When the loaded version of the miscellaneous scripts is different from the
29+
" version expected by the plug-in, this function will raise an error message
30+
" that explains what went wrong.
1731
if a:required_version != g:xolox#misc#compat#version
18-
let msg = "The %s plug-in requires version %i of the miscellaneous scripts, however version %i was loaded from %s!"
19-
throw printf(msg, a:plugin_name, a:required_version, g:xolox#misc#compat#version, s:misc_directory)
32+
let msg = "The %s %s plug-in expects version %i of the miscellaneous scripts, however version %i was loaded from the directory %s! Please upgrade your plug-ins to the latest releases to resolve this problem."
33+
throw printf(msg, a:plugin_name, a:plugin_version, a:required_version, g:xolox#misc#compat#version, s:misc_directory)
2034
endif
2135
endfunction
2236

autoload/xolox/misc/complete.vim

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
" Vim auto-load script
1+
" Tab completion for user defined commands.
2+
"
23
" Author: Peter Odding <peter@peterodding.com>
3-
" Last Change: March 15, 2011
4+
" Last Change: May 19, 2013
45
" URL: http://peterodding.com/code/vim/misc/
56

6-
" Keyword completion from the current buffer for user defined commands.
7-
87
function! xolox#misc#complete#keywords(arglead, cmdline, cursorpos)
8+
" This function can be used to perform keyword completion for user defined
9+
" Vim commands based on the contents of the current buffer. Here's an
10+
" example of how you would use it:
11+
"
12+
" :command -nargs=* -complete=customlist,xolox#misc#complete#keywords MyCmd call s:MyCmd(<f-args>)
913
let words = {}
1014
for line in getline(1, '$')
1115
for word in split(line, '\W\+')

autoload/xolox/misc/escape.vim

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,45 @@
1-
" Vim auto-load script
1+
" String escaping functions.
2+
"
23
" Author: Peter Odding <peter@peterodding.com>
3-
" Last Change: November 21, 2011
4+
" Last Change: May 19, 2013
45
" URL: http://peterodding.com/code/vim/misc/
56

6-
" Convert a string into a :substitute pattern that matches the string literally.
7-
8-
function! xolox#misc#escape#pattern(string)
7+
function! xolox#misc#escape#pattern(string) " {{{1
8+
" Takes a single string argument and converts it into a [:substitute]
9+
" [subcmd] / [substitute()] [subfun] pattern string that matches the given
10+
" string literally.
11+
"
12+
" [subfun]: http://vimdoc.sourceforge.net/htmldoc/eval.html#substitute()
13+
" [subcmd]: http://vimdoc.sourceforge.net/htmldoc/change.html#:substitute
914
if type(a:string) == type('')
1015
let string = escape(a:string, '^$.*\~[]')
1116
return substitute(string, '\n', '\\n', 'g')
1217
endif
1318
return ''
1419
endfunction
1520

16-
" Convert a string into a :substitute replacement that inserts the string literally.
17-
18-
function! xolox#misc#escape#substitute(string)
21+
function! xolox#misc#escape#substitute(string) " {{{1
22+
" Takes a single string argument and converts it into a [:substitute]
23+
" [subcmd] / [substitute()] [subfun] replacement string that inserts the
24+
" given string literally.
1925
if type(a:string) == type('')
2026
let string = escape(a:string, '\&~%')
2127
return substitute(string, '\n', '\\r', 'g')
2228
endif
2329
return ''
2430
endfunction
2531

26-
" Convert a string into a quoted command line argument. I was going to add a
27-
" long rant here about &shellslash, but really, it won't make any difference.
28-
" Let's just suffice to say that I have yet to encounter a single person out
29-
" there who uses this option for its intended purpose (running a UNIX-style
30-
" shell on Windows).
31-
32-
function! xolox#misc#escape#shell(string)
32+
function! xolox#misc#escape#shell(string) " {{{1
33+
" Takes a single string argument and converts it into a quoted command line
34+
" argument.
35+
"
36+
" I was going to add a long rant here about Vim's ['shellslash' option]
37+
" [shellslash], but really, it won't make any difference. Let's just suffice
38+
" to say that I have yet to encounter a single person out there who uses
39+
" this option for its intended purpose (running a UNIX style shell on
40+
" Microsoft Windows).
41+
"
42+
" [shellslash]: http://vimdoc.sourceforge.net/htmldoc/options.html#'shellslash'
3343
if xolox#misc#os#is_win()
3444
try
3545
let ssl_save = &shellslash

autoload/xolox/misc/list.vim

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1-
" Vim auto-load script
1+
" List handling functions.
2+
"
23
" Author: Peter Odding <peter@peterodding.com>
3-
" Last Change: August 31, 2011
4+
" Last Change: May 19, 2013
45
" URL: http://peterodding.com/code/vim/misc/
56

6-
" Remove duplicate values from {list} in-place (preserves order).
7-
8-
function! xolox#misc#list#unique(list)
7+
function! xolox#misc#list#unique(list) " {{{1
8+
" Remove duplicate values from the given list in-place (preserves order).
99
call reverse(a:list)
1010
call filter(a:list, 'count(a:list, v:val) == 1')
1111
return reverse(a:list)
1212
endfunction
1313

14-
" Binary insertion (more efficient than calling sort() after each insertion).
15-
16-
function! xolox#misc#list#binsert(list, value, ...)
14+
function! xolox#misc#list#binsert(list, value, ...) " {{{1
15+
" Performs in-place binary insertion, which depending on your use case can
16+
" be more efficient than calling Vim's [sort()] [sort] function after each
17+
" insertion (in cases where a single, final sort is not an option). Expects
18+
" three arguments:
19+
"
20+
" 1. A list
21+
" 2. A value to insert
22+
" 3. 1 (true) when case should be ignored, 0 (false) otherwise
23+
"
24+
" [sort]: http://vimdoc.sourceforge.net/htmldoc/eval.html#sort()
1725
let idx = s:binsert_r(a:list, 0, len(a:list), a:value, exists('a:1') && a:1)
1826
return insert(a:list, a:value, idx)
1927
endfunction

autoload/xolox/misc/msg.vim

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
" Vim auto-load script
1+
" Functions to interact with the user.
2+
"
23
" Author: Peter Odding <peter@peterodding.com>
3-
" Last Change: March 15, 2011
4+
" Last Change: May 19, 2013
45
" URL: http://peterodding.com/code/vim/misc/
56

67
if !exists('g:xolox_message_buffer')
@@ -12,29 +13,34 @@ if !exists('g:xolox_messages')
1213
let g:xolox_messages = []
1314
endif
1415

15-
" Show a formatted informational message to the user.
16-
17-
function! xolox#misc#msg#info(...)
16+
function! xolox#misc#msg#info(...) " {{{1
17+
" Show a formatted informational message to the user. This function has the
18+
" same argument handling as Vim's [printf()] [printf] function.
19+
"
20+
" [printf]: http://vimdoc.sourceforge.net/htmldoc/eval.html#printf()
1821
call s:show_message('title', a:000)
1922
endfunction
2023

21-
" Show a formatted warning message to the user.
22-
23-
function! xolox#misc#msg#warn(...)
24+
function! xolox#misc#msg#warn(...) " {{{1
25+
" Show a formatted warning message to the user. This function has the same
26+
" argument handling as Vim's [printf()] [printf] function.
2427
call s:show_message('warningmsg', a:000)
2528
endfunction
2629

27-
" Show a formatted debugging message to the user?
28-
29-
function! xolox#misc#msg#debug(...)
30+
function! xolox#misc#msg#debug(...) " {{{1
31+
" Show a formatted debugging message to the user, if the user has enabled
32+
" increased verbosity by setting Vim's ['verbose'] [verbose] option to one
33+
" (1) or higher. This function has the same argument handling as Vim's
34+
" [printf()] [printf] function.
35+
"
36+
" [verbose]: http://vimdoc.sourceforge.net/htmldoc/options.html#'verbose'
3037
if &vbs >= 1
3138
call s:show_message('question', a:000)
3239
endif
3340
endfunction
3441

35-
" The implementation of info() and warn().
36-
37-
function! s:show_message(hlgroup, args)
42+
function! s:show_message(hlgroup, args) " {{{1
43+
" The implementation of info() and warn().
3844
let nargs = len(a:args)
3945
if nargs == 1
4046
let message = a:args[0]
@@ -56,7 +62,10 @@ function! s:show_message(hlgroup, args)
5662
augroup END
5763
execute 'echohl' a:hlgroup
5864
" Redraw to avoid |hit-enter| prompt.
59-
redraw | echomsg message
65+
redraw
66+
for line in split(message, "\n")
67+
echomsg line
68+
endfor
6069
if g:xolox_message_buffer > 0
6170
call add(g:xolox_messages, message)
6271
if len(g:xolox_messages) > g:xolox_message_buffer
@@ -70,7 +79,8 @@ function! s:show_message(hlgroup, args)
7079
endif
7180
endfunction
7281

73-
function! s:clear_message()
82+
function! s:clear_message() " {{{1
83+
" Callback to clear message after some time has passed.
7484
echo ''
7585
let &more = s:more_save
7686
let &showmode = s:smd_save

autoload/xolox/misc/open.vim

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
" Vim auto-load script
1+
" Integration between Vim and its environment.
2+
"
23
" Author: Peter Odding <peter@peterodding.com>
3-
" Last Change: November 21, 2011
4+
" Last Change: May 19, 2013
45
" URL: http://peterodding.com/code/vim/misc/
56

67
if !exists('s:version')
@@ -9,7 +10,20 @@ if !exists('s:version')
910
let s:handlers = ['gnome-open', 'kde-open', 'exo-open', 'xdg-open']
1011
endif
1112

12-
function! xolox#misc#open#file(path, ...)
13+
function! xolox#misc#open#file(path, ...) " {{{1
14+
" Given a pathname as the first argument, this opens the file with the
15+
" program associated with the file type. So for example a text file might
16+
" open in Vim, an `*.html` file would probably open in your web browser and
17+
" a media file would open in a media player.
18+
"
19+
" This should work on Windows, Mac OS X and most Linux distributions. If
20+
" this fails to find a file association, you can pass one or more external
21+
" commands to try as additional arguments. For example:
22+
"
23+
" :call xolox#misc#open#file('/path/to/my/file', 'firefox', 'google-chrome')
24+
"
25+
" This generally shouldn't be necessary but it might come in handy now and
26+
" then.
1327
if xolox#misc#os#is_win()
1428
try
1529
call xolox#shell#open_with_windows_shell(a:path)
@@ -35,7 +49,15 @@ function! xolox#misc#open#file(path, ...)
3549
throw printf(s:enoimpl, s:script, 'xolox#misc#open#file')
3650
endfunction
3751

38-
function! xolox#misc#open#url(url)
52+
function! xolox#misc#open#url(url) " {{{1
53+
" Given a URL as the first argument, this opens the URL in your preferred or
54+
" best available web browser:
55+
"
56+
" - In GUI environments a graphical web browser will open (or a new tab will
57+
" be created in an existing window)
58+
" - In console Vim without a GUI environment, when you have any of `lynx`,
59+
" `links` or `w3m` installed it will launch a command line web browser in
60+
" front of Vim (temporarily suspending Vim)
3961
let url = a:url
4062
if url !~ '^\w\+://'
4163
if url !~ '@'
@@ -56,7 +78,7 @@ function! xolox#misc#open#url(url)
5678
call xolox#misc#open#file(url, 'firefox', 'google-chrome')
5779
endfunction
5880

59-
function! s:handle_error(cmd, output)
81+
function! s:handle_error(cmd, output) " {{{1
6082
if v:shell_error
6183
let message = "open.vim %s: Failed to execute program! (command line: %s%s)"
6284
let output = strtrans(xolox#misc#str#trim(a:output))

0 commit comments

Comments
 (0)