Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit e5a695efaa324be42103397b6203fd662ee4ab08 0 parents
@xolox authored
Showing with 330 additions and 0 deletions.
  1. +97 −0 README.md
  2. +195 −0 autoload.vim
  3. +38 −0 reload.vim
97 README.md
@@ -0,0 +1,97 @@
+# Automatic reloading of Vim scripts
+
+The [reload.vim][reload] plug-in automatically reloads various types of [Vim][vim] scripts as they're being edited in Vim to give you instant feedback on the changes you make. For example while writing a Vim syntax script you can open a split window of the relevant file type and every time you [:update][update] your syntax script, [reload.vim][reload] will refresh the syntax highlighting in the split window. Automatic reloading of Vim scripts is currently supported for the following types of scripts:
+
+ * [Standard plug-ins](http://vimdoc.sourceforge.net/htmldoc/usr_05.html#standard-plugin) located at `~/.vim/plugin/*.vim` on UNIX, `~\_vimfiles\plugin\*.vim` on Windows;
+
+ * [Auto-load scripts](http://vimdoc.sourceforge.net/htmldoc/eval.html#autoload) located at `~/.vim/autoload/*.vim` on UNIX, `~\_vimfiles\autoload\*.vim` on Windows;
+
+ * [File-type plug-ins](http://vimdoc.sourceforge.net/htmldoc/filetype.html#filetype-plugins) located at `~/.vim/ftplugin/*.vim` on UNIX, `~\_vimfiles\ftplugin\*.vim` on Windows;
+
+ * [Syntax highlighting scripts](http://vimdoc.sourceforge.net/htmldoc/syntax.html#syntax-highlighting) located at `~/.vim/syntax/*.vim` on UNIX, `~\_vimfiles\syntax\*.vim` on Windows;
+
+ * [File-type indentation plug-ins](http://vimdoc.sourceforge.net/htmldoc/usr_30.html#30.3) located at `~/.vim/indent/*.vim` on UNIX, `~\_vimfiles\indent\*.vim` on Windows;
+
+ * [Color scheme scripts](http://vimdoc.sourceforge.net/htmldoc/syntax.html#:colorscheme) located at `~/.vim/colors/*.vim` on UNIX, `~\_vimfiles\colors\*.vim` on Windows.
+
+Note that [vimrc scripts][vimrc] are not reloaded because that seems to cause more trouble than it's worth...
+
+## Usage
+
+Out of the box the [reload.vim][reload] plug-in is configured to automatically reload all Vim scripts that it knows how to. If you like it this way then you don't need to configure anything! However if you don't like the automatic reloading then you'll need the following:
+
+### The `g:reload_on_write` option
+
+If you don't like automatic reloading because it slows Vim down or causes problems you can add the following line to your [vimrc script][vimrc]:
+
+ let g:reload_on_write = 0
+
+This disables automatic reloading which means you'll have to reload scripts using the command discussed below.
+
+### The `:ReloadScript` command
+
+You can execute the `:ReloadScript` command to reload the Vim script you're editing. If you provide a script name as argument to the command then that script will be reloaded instead, e.g.:
+
+ :ReloadScript ~/.vim/plugin/reload.vim
+
+If after executing this command you see Vim errors such as "Function already exists" ([E122](http://vimdoc.sourceforge.net/htmldoc/eval.html#E122)) or "Command already exists" ([E174](http://vimdoc.sourceforge.net/htmldoc/map.html#E174)) then you'll need to change your Vim script(s) slightly to enable reloading, see below.
+
+## Things that prevent reloading
+
+If you want your Vim plug-ins and/or other scripts to be automatically reloaded they'll have to be written a certain way, though you can consider the following points good practice for Vim script writing anyway:
+
+### Use a bang in command/function definitions!
+
+Function and command definitions using Vim's [:command](http://vimdoc.sourceforge.net/htmldoc/map.html#:command) and [:function](http://vimdoc.sourceforge.net/htmldoc/eval.html#:function) built-ins should include a [bang (!)](http://vimdoc.sourceforge.net/htmldoc/map.html#:command-bang) symbol, otherwise Vim will complain that the command or function already exists:
+
+ " Bad:
+ :command MyCmd call MyFun()
+ :function MyFun()
+ :endfunction
+
+ " Good:
+ :command! MyCmd call MyFun()
+ :function! MyFun()
+ :endfunction
+
+### Use automatic command groups
+
+Automatic commands using Vim's [:autocmd][autocmd] built-in should be defined inside of an [automatic command group](http://vimdoc.sourceforge.net/htmldoc/autocmd.html#:augroup) that's cleared so the automatic commands don't stack indefinitely when your [:autocmd][autocmd] commands are executed several times:
+
+ " Bad example: If the following line were re-evaluated, the message would
+ " appear multiple times the next time the automatic command fires:
+ :autocmd TabEnter * echomsg "Entered tab page"
+
+ " Good example: The following three lines can be reloaded without the
+ " message appearing multiple times:
+ :augroup MyPlugin
+ : autocmd! TabEnter * echomsg "Entered tab page"
+ :augroup END
+
+## Alternatives
+
+The [ReloadScript](http://www.vim.org/scripts/script.php?script_id=1904) plug-in on [www.vim.org][vim] also supports reloading of Vim scripts, but there are a few notable differences:
+
+ * This plug-in focuses on automatic reloading (I'm lazy) while the other one requires manual reloading;
+
+ * This plug-in doesn't clear inclusion guard variables while the other one does;
+
+ * This plug-in will *never* [:source](http://vimdoc.sourceforge.net/htmldoc/repeat.html#:source) a file that hasn't already been loaded by Vim -- it checks using Vim's [:scriptnames](http://vimdoc.sourceforge.net/htmldoc/repeat.html#:scriptnames) command;
+
+ * This plug-in can more or less reload itself ;-)
+
+## 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/reload/> and <http://github.com/xolox/vim-reload>.
+
+## License
+
+This software is licensed under the [MIT license](http://en.wikipedia.org/wiki/MIT_License).
+© 2010 Peter Odding &lt;<peter@peterodding.com>&gt;.
+
+
+[autocmd]: http://vimdoc.sourceforge.net/htmldoc/autocmd.html#:autocmd
+[reload]: http://github.com/xolox/vim-reload/blob/master/reload.vim
+[update]: http://vimdoc.sourceforge.net/htmldoc/editing.html#:update
+[vim]: http://www.vim.org/
+[vimrc]: http://vimdoc.sourceforge.net/htmldoc/starting.html#vimrc
195 autoload.vim
@@ -0,0 +1,195 @@
+" Vim script
+" Last Change: July 13, 2010
+" Author: Peter Odding
+" URL: http://peterodding.com/code/vim/reload/
+
+let s:script = expand('<sfile>:t')
+
+" Patterns to match various types of Vim script names. {{{1
+
+" Enable line continuation.
+let s:cpo_save = &cpo
+set cpoptions-=C
+
+if has('win32') || has('win64')
+ let s:scripttypes = [
+ \ ['s:reload_plugin', '\c[\\/]plugin[\\/].\{-}\.vim$'],
+ \ ['s:reload_autoload', '\c[\\/]autoload[\\/].\{-}\.vim$'],
+ \ ['s:reload_ftplugin', '\c[\\/]ftplugin[\\/][^\\/]\+\.vim$'],
+ \ ['s:reload_syntax', '\c[\\/]syntax[\\/][^\\/]\+\.vim$'],
+ \ ['s:reload_indent', '\c[\\/]indent[\\/][^\\/]\+\.vim$'],
+ \ ['s:reload_colors', '\c[\\/]colors[\\/][^\\/]\+\.vim$']]
+else
+ let s:scripttypes = [
+ \ ['s:reload_plugin', '\C/plugin/.\{-}\.vim$'],
+ \ ['s:reload_autoload', '\C/autoload/.\{-}\.vim$'],
+ \ ['s:reload_ftplugin', '\C/ftplugin/[^/]\+\.vim$'],
+ \ ['s:reload_syntax', '\C/syntax/[^/]\+\.vim$'],
+ \ ['s:reload_indent', '\C/indent/[^/]\+\.vim$'],
+ \ ['s:reload_colors', '\C/colors/[^/]\+\.vim$']]
+endif
+
+" Restore compatibility options
+let &cpo = s:cpo_save
+unlet s:cpo_save
+
+if !exists('s:reload_script_active')
+ function! xolox#reload#script(filename) " {{{1
+ let s:reload_script_active = 1
+ let start_time = xolox#timer#start()
+ if s:script_sourced(a:filename)
+ let filename = s:unresolve_scriptname(a:filename)
+ for [callback, pattern] in s:scripttypes
+ if filename =~ pattern
+ let friendly_name = fnamemodify(filename, ':~')
+ call call(callback, [start_time, filename, friendly_name])
+ endif
+ endfor
+ endif
+ unlet s:reload_script_active
+ endfunction
+endif
+
+function! s:reload_plugin(start_time, filename, friendly_name) " {{{1
+ call s:reload_message('plug-in', a:friendly_name)
+ execute 'source' fnameescape(a:filename)
+ let msg = "%s: Reloaded %s plug-in in %s."
+ call xolox#timer#stop(msg, s:script, a:friendly_name, a:start_time)
+endfunction
+
+if !exists('s:reload_script_active')
+ function! s:reload_autoload(start_time, filename, friendly_name) " {{{1
+ call s:reload_message('auto-load script', a:friendly_name)
+ execute 'source' fnameescape(a:filename)
+ let msg = "%s: Reloaded %s auto-load script in %s."
+ call xolox#timer#stop(msg, s:script, a:friendly_name, a:start_time)
+ endfunction
+endif
+
+function! s:reload_ftplugin(start_time, filename, friendly_name) " {{{1
+ let type = fnamemodify(a:filename, ':t:r')
+ let view = s:save_view()
+ call s:change_swapchoice(1)
+ call s:reload_message('file type plug-in', a:friendly_name)
+ silent hide bufdo if &ft == type | let b:reload_ftplugin = 1 | endif
+ call xolox#reload#windows()
+ call s:restore_view(view)
+ call s:change_swapchoice(0)
+ let msg = "%s: Reloaded %s file type plug-in in %s."
+ call xolox#timer#stop(msg, s:script, a:friendly_name, a:start_time)
+endfunction
+
+function! s:reload_syntax(start_time, filename, friendly_name) " {{{1
+ let type = fnamemodify(a:filename, ':t:r')
+ let view = s:save_view()
+ call s:change_swapchoice(1)
+ call s:reload_message('syntax highlighting', a:friendly_name)
+ silent hide bufdo if &syn == type | let b:reload_syntax = 1 | endif
+ call xolox#reload#windows()
+ call s:restore_view(view)
+ call s:change_swapchoice(0)
+ let msg = "%s: Reloaded %s syntax script in %s."
+ call xolox#timer#stop(msg, s:script, a:friendly_name, a:start_time)
+endfunction
+
+function! s:reload_indent(start_time, filename, friendly_name) " {{{1
+ let type = fnamemodify(a:filename, ':t:r')
+ let view = s:save_view()
+ call s:change_swapchoice(1)
+ call s:reload_message('indentation plug-in', a:friendly_name)
+ silent hide bufdo if &ft == type | let b:reload_indent = 1 | endif
+ call xolox#reload#windows()
+ call s:restore_view(view)
+ call s:change_swapchoice(0)
+ let msg = "%s: Reloaded %s indent script in %s."
+ call xolox#timer#stop(msg, s:script, a:friendly_name, a:start_time)
+endfunction
+
+function! xolox#reload#windows() " {{{1
+ let window = winnr()
+ try
+ windo call s:reload_window()
+ finally
+ execute window . 'wincmd w'
+ endtry
+endfunction
+
+function! s:reload_window()
+ if exists('b:reload_ftplugin')
+ unlet! b:reload_ftplugin b:did_ftplugin
+ let &filetype = &filetype
+ endif
+ if exists('b:reload_syntax')
+ unlet! b:reload_syntax b:current_syntax
+ let &syntax = &syntax
+ endif
+ if exists('b:reload_indent')
+ unlet! b:reload_indent b:did_indent
+ let &filetype = &filetype
+ endif
+endfunction
+
+function! s:reload_colors(start_time, filename, friendly_name) " {{{1
+ let colorscheme = fnamemodify(a:filename, ':t:r')
+ if exists('g:colors_name') && g:colors_name == colorscheme
+ call s:reload_message('color scheme', a:friendly_name)
+ let escaped = fnameescape(colorscheme)
+ execute 'colorscheme' escaped
+ execute 'doautocmd colorscheme' escaped
+ let msg = "%s: Reloaded %s color scheme in %s."
+ call xolox#timer#stop(msg, s:script, a:friendly_name, a:start_time)
+ endif
+endfunction
+
+" Miscellaneous functions. {{{1
+
+let s:loaded_scripts = {}
+
+function! s:script_sourced(filename) " {{{2
+ call s:parse_scriptnames()
+ return has_key(s:loaded_scripts, resolve(a:filename))
+endfunction
+
+function! s:unresolve_scriptname(filename) " {{{2
+ call s:parse_scriptnames()
+ return get(s:loaded_scripts, resolve(a:filename), a:filename)
+endfunction
+
+function! s:parse_scriptnames() " {{{2
+ let listing = ''
+ redir => listing
+ silent scriptnames
+ redir END
+ let lines = split(listing, "\n")
+ let num_loaded = len(s:loaded_scripts)
+ if len(lines) > num_loaded
+ for line in lines[num_loaded : -1]
+ let filename = matchstr(line, '^\s*\d\+:\s\+\zs.\+$')
+ let s:loaded_scripts[resolve(filename)] = filename
+ endfor
+ endif
+endfunction
+
+function! s:change_swapchoice(enable) " {{{2
+ let augroup = 'PluginReloadScriptsSC'
+ if a:enable
+ execute xolox#swapchoice#change(augroup, 'e')
+ else
+ execute xolox#swapchoice#restore(augroup)
+ endif
+endfunction
+
+function! s:reload_message(scripttype, scriptname) " {{{2
+ call xolox#message('%s: Reloading %s %s', s:script, a:scripttype, a:scriptname)
+endfunction
+
+function! s:save_view() " {{{2
+ return [bufnr('%'), winsaveview()]
+endfunction
+
+function! s:restore_view(view) " {{{2
+ silent execute 'buffer' a:view[0]
+ call winrestview(a:view[1])
+endfunction
+
+" vim: ts=2 sw=2 et
38 reload.vim
@@ -0,0 +1,38 @@
+" Vim script
+" Last Change: July 12, 2010
+" Author: Peter Odding
+" URL: http://peterodding.com/code/vim/reload/
+" License: MIT
+" Version: 0.3
+
+if !exists('g:reload_on_write')
+ let g:reload_on_write = 1
+endif
+
+command! -bar -nargs=? -complete=file ReloadScript call s:ReloadCmd(<q-args>)
+
+augroup PluginReloadScripts
+ autocmd!
+ autocmd BufWritePost *.vim call s:AutoReload()
+ autocmd TabEnter * call xolox#reload#windows()
+augroup END
+
+function! s:ReloadCmd(arg)
+ if a:arg !~ '\S'
+ call xolox#reload#script(expand('%:p'))
+ else
+ call xolox#reload#script(fnamemodify(a:arg, ':p'))
+ endif
+endfunction
+
+if !exists('s:auto_reload_active')
+ function! s:AutoReload()
+ if g:reload_on_write
+ let s:auto_reload_active = 1
+ call xolox#reload#script(expand('%:p'))
+ unlet s:auto_reload_active
+ endif
+ endfunction
+endif
+
+" vim: ts=2 sw=2 et
Please sign in to comment.
Something went wrong with that request. Please try again.