Skip to content

Commit b6b3952

Browse files
committed
Support for generic file type plug-ins
Thanks to Britton Kerin for the suggestion!
1 parent be83d35 commit b6b3952

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
# Automatic reloading of Vim scripts
22

3-
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:
3+
The [reload.vim][reload] plug-in automatically reloads various types of [Vim][vim] scripts as you're editing them 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:
44

5-
* [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;
5+
* [Standard plug-ins](http://vimdoc.sourceforge.net/htmldoc/usr_05.html#standard-plugin) located in `~/.vim/plugin` on UNIX, `~\vimfiles\plugin` on Windows;
66

7-
* [Auto-load scripts](http://vimdoc.sourceforge.net/htmldoc/eval.html#autoload) located at `~/.vim/autoload/*.vim` on UNIX, `~\_vimfiles\autoload\*.vim` on Windows;
7+
* [Auto-load scripts](http://vimdoc.sourceforge.net/htmldoc/eval.html#autoload) located in or below `~/.vim/autoload` on UNIX, `~\vimfiles\autoload` on Windows;
88

9-
* [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;
9+
* [File-type plug-ins](http://vimdoc.sourceforge.net/htmldoc/filetype.html#filetype-plugins) located in or below `~/.vim/ftplugin` on UNIX, `~\vimfiles\ftplugin` on Windows;
1010

11-
* [Syntax highlighting scripts](http://vimdoc.sourceforge.net/htmldoc/syntax.html#syntax-highlighting) located at `~/.vim/syntax/*.vim` on UNIX, `~\_vimfiles\syntax\*.vim` on Windows;
11+
* [Syntax highlighting scripts](http://vimdoc.sourceforge.net/htmldoc/syntax.html#syntax-highlighting) located in `~/.vim/syntax` on UNIX, `~\vimfiles\syntax` on Windows;
1212

13-
* [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;
13+
* [File-type indentation plug-ins](http://vimdoc.sourceforge.net/htmldoc/usr_30.html#30.3) located in `~/.vim/indent` on UNIX, `~\vimfiles\indent` on Windows;
1414

15-
* [Color scheme scripts](http://vimdoc.sourceforge.net/htmldoc/syntax.html#:colorscheme) located at `~/.vim/colors/*.vim` on UNIX, `~\_vimfiles\colors\*.vim` on Windows.
15+
* [Color scheme scripts](http://vimdoc.sourceforge.net/htmldoc/syntax.html#:colorscheme) located in `~/.vim/colors` on UNIX, `~\vimfiles\colors` on Windows.
16+
17+
The directories listed above are Vim's defaults but you're free to change the ['runtimepath'](http://vimdoc.sourceforge.net/htmldoc/options.html#%27runtimepath%27) and reloading will still work.
1618

1719
Note that [vimrc scripts][vimrc] are not reloaded because that seems to cause more trouble than it's worth...
1820

autoload.vim

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
" Vim script
2-
" Last Change: October 14, 2010
2+
" Last Change: February 4, 2011
33
" Author: Peter Odding
44
" URL: http://peterodding.com/code/vim/reload/
55

@@ -15,15 +15,15 @@ if has('win32') || has('win64')
1515
let s:scripttypes = [
1616
\ ['s:reload_plugin', '\c[\\/]plugin[\\/].\{-}\.vim$'],
1717
\ ['s:reload_autoload', '\c[\\/]autoload[\\/].\{-}\.vim$'],
18-
\ ['s:reload_ftplugin', '\c[\\/]ftplugin[\\/][^\\/]\+\.vim$'],
18+
\ ['s:reload_ftplugin', '\c[\\/]ftplugin[\\/]\([^\\/_]\+\)\%([\\/_]\([^\\/]\+\)\)\?\.vim$'],
1919
\ ['s:reload_syntax', '\c[\\/]syntax[\\/][^\\/]\+\.vim$'],
2020
\ ['s:reload_indent', '\c[\\/]indent[\\/][^\\/]\+\.vim$'],
2121
\ ['s:reload_colors', '\c[\\/]colors[\\/][^\\/]\+\.vim$']]
2222
else
2323
let s:scripttypes = [
2424
\ ['s:reload_plugin', '\C/plugin/.\{-}\.vim$'],
2525
\ ['s:reload_autoload', '\C/autoload/.\{-}\.vim$'],
26-
\ ['s:reload_ftplugin', '\C/ftplugin/[^/]\+\.vim$'],
26+
\ ['s:reload_ftplugin', '\C/ftplugin/\([^/_]\+\)\%([/_]\([^/]\+\)\)\?\.vim$'],
2727
\ ['s:reload_syntax', '\C/syntax/[^/]\+\.vim$'],
2828
\ ['s:reload_indent', '\C/indent/[^/]\+\.vim$'],
2929
\ ['s:reload_colors', '\C/colors/[^/]\+\.vim$']]
@@ -41,12 +41,23 @@ if !exists('s:reload_script_active')
4141
let filename = s:unresolve_scriptname(a:filename)
4242
for [callback, pattern] in s:scripttypes
4343
if filename =~ pattern
44-
let args = [start_time, filename, fnamemodify(filename, ':~')]
44+
let friendly_name = fnamemodify(filename, ':~')
45+
if pattern =~ 'ftplugin'
46+
" Determine include guard for generic file type plug-ins.
47+
let matches = matchlist(filename, pattern)
48+
if len(matches) >= 3
49+
let s:include_guard = 'b:loaded_' . matches[1] . '_' . matches[2]
50+
" s:reload_ftplugin() knows the 2nd argument is really a file type.
51+
let filename = matches[1]
52+
endif
53+
endif
54+
let args = [start_time, filename, friendly_name]
4555
let result = call(callback, args)
4656
if type(result) == type([])
4757
call call('xolox#timer#stop', result)
4858
endif
49-
unlet result
59+
unlet! result s:include_guard
60+
break
5061
endif
5162
endfor
5263
endif
@@ -56,11 +67,7 @@ endif
5667

5768
function! s:reload_plugin(start_time, filename, friendly_name) " {{{1
5869
call s:reload_message('plug-in', a:friendly_name)
59-
" Clear include guard so full plug-in can be reloaded?
60-
let variable = 'g:loaded_' . fnamemodify(a:filename, ':t:r')
61-
if exists(variable)
62-
execute 'unlet' variable
63-
endif
70+
unlet! g:loaded_{fnamemodify(a:filename, ':t:r')}
6471
execute 'source' fnameescape(a:filename)
6572
return ["%s: Reloaded %s plug-in in %s.", s:script, a:friendly_name, a:start_time]
6673
endfunction
@@ -73,28 +80,27 @@ if !exists('s:reload_script_active')
7380
endfunction
7481
endif
7582

76-
function! s:reload_ftplugin(st, fn, hr) " {{{1
77-
return s:reload_buffers(a:st, a:fn, a:hr, 'file type plug-in', 'b:reload_ftplugin')
83+
function! s:reload_ftplugin(st, ft, hr) " {{{1
84+
return s:reload_buffers(a:st, a:ft, a:hr, 'file type plug-in', 'b:reload_ftplugin')
7885
endfunction
7986

8087
function! s:reload_syntax(st, fn, hr) " {{{1
81-
return s:reload_buffers(a:st, a:fn, a:hr, 'syntax script', 'b:reload_syntax')
88+
return s:reload_buffers(a:st, fnamemodify(a:fn, ':t:r'), a:hr, 'syntax script', 'b:reload_syntax')
8289
endfunction
8390

8491
function! s:reload_indent(st, fn, hr) " {{{1
85-
return s:reload_buffers(a:st, a:fn, a:hr, 'indent script', 'b:reload_indent')
92+
return s:reload_buffers(a:st, fnamemodify(a:fn, ':t:r'), a:hr, 'indent script', 'b:reload_indent')
8693
endfunction
8794

88-
function! s:reload_buffers(start_time, filename, friendly_name, script_type, variable)
89-
let type = fnamemodify(a:filename, ':t:r')
95+
function! s:reload_buffers(start_time, filetype, friendly_name, script_type, variable)
9096
" Make sure we can restore the user's context after reloading!
9197
let bufnr_save = bufnr('%')
9298
let view_save = winsaveview()
9399
" Temporarily enable the SwapExists automatic command to prevent the E325
94100
" prompt from rearing its ugly head while reloading (in :bufdo below).
95101
let s:reloading_buffers = 1
96102
call s:reload_message(a:script_type, a:friendly_name)
97-
silent hide bufdo if &ft == type | execute 'let' a:variable '= 1' | endif
103+
silent hide bufdo if &ft == a:filetype | execute 'let' a:variable '= 1' | endif
98104
call xolox#reload#windows()
99105
" Restore the user's context.
100106
silent execute 'buffer' bufnr_save
@@ -122,6 +128,9 @@ endfunction
122128
function! s:reload_window()
123129
if exists('b:reload_ftplugin')
124130
unlet! b:reload_ftplugin b:did_ftplugin
131+
if exists('s:include_guard') && exists(s:include_guard)
132+
unlet {s:include_guard}
133+
endif
125134
let &filetype = &filetype
126135
endif
127136
if exists('b:reload_syntax')

reload.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
" Vim script
2-
" Last Change: October 14, 2010
2+
" Last Change: February 4, 2011
33
" Author: Peter Odding
44
" URL: http://peterodding.com/code/vim/reload/
55
" License: MIT
6-
" Version: 0.5.2
6+
" Version: 0.6
77

88
" Support for automatic update using the GLVS plug-in.
99
" GetLatestVimScripts: 3148 1 :AutoInstall: reload.zip

0 commit comments

Comments
 (0)