My dotfiles. Period.

local_vimrc.vim 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. "=============================================================================
  2. " $Id$
  3. " File: plugin/local_vimrc.vim {{{1
  4. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  5. " <URL:http://code.google.com/p/lh-vim/>
  6. " Version: 1.10
  7. " Created: 09th Apr 2003
  8. " Last Update: 10th Apr 2012
  9. " License: GPLv3
  10. "------------------------------------------------------------------------
  11. " Description: Solution to Yakov Lerner's question on Vim ML {{{2
  12. " Search for a _vimrc_local.vim file in the parents directories and
  13. " source it if found.
  14. "
  15. " Initial Question:
  16. " "Is it possible, after sourcing ~/.exrc, to traverse from $HOME down
  17. " to cwd, and source .exrc from every directory if present ?
  18. " (And if cwd is not under $HOME, just source ~/.exrc).
  19. " What do I put into .vimrc to do this ?
  20. "
  21. " "Example: current dir is ~/a/b/c. Files are sourced in this order:
  22. " ~/.exrc, then ~/a/.exrc, ~/a/b/.exrc, ~/a/b/c/.exrc.
  23. " No messages if some of .exrc does not exist."
  24. " }}}2
  25. "------------------------------------------------------------------------
  26. " Installation: {{{2
  27. " 0- Set g:local_vimrc in your .vimrc if you wish to use filenames other
  28. " than '_vimrc_local.vim'
  29. " a- Drop this plugin into a {rtp}/plugin/ directory, and install
  30. " lh-vim-lib.
  31. " b- Define _vimrc_local.vim files into your directories
  32. "
  33. " Ideally, each foo/bar/_vimrc_local.vim should be defined the same
  34. " way as a ftplugin, i.e.: {{{3
  35. " " Global stuff that needs to be updated/override
  36. " let g:bar = 'bar' " YES! This is a global variable!
  37. "
  38. " " Local stuff that needs to be defined once for each buffer
  39. " if exists('b:foo_bar_local_vimrc') | finish | endif
  40. " let b:foo_bar_local_vimrc = 1
  41. " setlocal xxx
  42. " nnoremap <buffer> foo :call <sid>s:Foo()<cr>
  43. " let b:foo = 'foo'
  44. "
  45. " " Global stuff that needs to be defined once only => functions
  46. " if exists('g:foo_bar_local_vimrc') | finish | endif
  47. " let g:foo_bar_local_vimrc = 1
  48. " function s:Foo()
  49. " ...
  50. " endfunction
  51. " c- In order to load the local variable before a skeleton is read, ask
  52. " the maintainer of template-file expander pluin to explicitly execute
  53. " :SourceLocalVimrc before doing the actual expansion.
  54. "
  55. " History: {{{2
  56. " v1.10 s:k_version in local_vimrc files is automatically incremented
  57. " on saving
  58. " v1.9 New command :SourceLocalVimrc in order to explicitly load the
  59. " local-vimrc file before creating new files from a template (We
  60. " can't just rely on BufNewFile as there is no guaranty
  61. " local_vimrc's BufNewFile will be called before the one from the
  62. " Template Expander Plugin => it's up to the TEP to call the
  63. " function)
  64. " v1.8 No more infinite recursion on file in non existent paths.
  65. " + patch from cristiklein to support paths with spaces
  66. " v1.7 Don't search a local vimrc with remote paths (ftp://, http, ... )
  67. " v1.6 Sometimes root path is Z:\\, which is quite odd
  68. " v1.5 The auto-command is moved to the au-group LocalVimrc
  69. " v1.4 Stop the recursion when we get to // or \\ (UNC paths)
  70. " v1.3 More comments.
  71. " Trace of the directories searched when 'verbose' >= 2
  72. " v1.2 Stops at $HOME or at root (/)
  73. " v1.1 Uses _vimrc_local.vim
  74. " v1.0 Initial solution
  75. " TODO: {{{2
  76. " (*) Add option to stop looking at $HOME or elsewhere
  77. " ([bg]:lv_stop_at : string, default $HOME)
  78. " See also: alternative scripts: #441, #3393, #1860, ...
  79. " }}}1
  80. "=============================================================================
  81. "=============================================================================
  82. " Avoid global reinclusion {{{1
  83. let s:k_version = 109
  84. if exists("g:loaded_local_vimrc")
  85. \ && (g:loaded_local_vimrc >= s:k_version)
  86. \ && !exists('g:force_reload_local_vimrc')
  87. finish
  88. endif
  89. let g:loaded_local_vimrc_vim = s:k_version
  90. let s:cpo_save=&cpo
  91. set cpo&vim
  92. " Avoid global reinclusion }}}1
  93. "------------------------------------------------------------------------
  94. " Commands {{{1
  95. command! -nargs=0 SourceLocalVimrc call s:Main(expand('%:p'))
  96. " Functions {{{1
  97. " Name of the files used {{{2
  98. function! s:LocalVimrcName()
  99. return exists('g:local_vimrc') ? g:local_vimrc : '_vimrc_local.vim'
  100. endfunction
  101. let s:local_vimrc = s:LocalVimrcName()
  102. " Value of $HOME -- actually a regex. {{{2
  103. let s:home = substitute($HOME, '/\|\\', '[/\\\\]', 'g')
  104. " Regex used to determine when we must stop looking for local-vimrc's {{{2
  105. " Sometimes paths appears as Z:\\ ....
  106. let s:re_last_path = '^/\=$\|^[A-Za-z]:[/\\]\+$\|^//$\|^\\\\$'.
  107. \ ((s:home != '') ? ('\|^'.s:home.'$') : '')
  108. " The main function {{{2
  109. function! s:SourceLocal(path)
  110. let up_path = fnamemodify(a:path,':h')
  111. if up_path == '.' " likelly a non existant path
  112. if ! isdirectory(a:path)
  113. call lh#common#warning_msg("[local_vimrc] The current file '".expand('%:p:')."' seems to be in a non-existant directory: '".a:path."'")
  114. endif
  115. let up_path = getcwd()
  116. endif
  117. " call confirm('crt='.a:path."\nup=".up_path."\n$HOME=".s:home, '&Ok', 1)
  118. " echomsg ('crt='.a:path."\nup=".up_path."\n$HOME=".s:home)
  119. if (a:path !~ s:re_last_path)
  120. if (up_path !~ s:re_last_path)
  121. " Recursive call: first check the parent directory
  122. call s:SourceLocal(up_path)
  123. elseif filereadable(up_path.'/'.s:local_vimrc)
  124. " Terminal condition: try to source the upper(/uppest?) local-vimrc
  125. if &verbose >= 2
  126. echo 'Check '.up_path.' for '.s:local_vimrc.' ... found!'
  127. endif
  128. exe 'source '.escape(up_path.'/'.s:local_vimrc, ' \$,')
  129. elseif &verbose >= 2
  130. echo 'Check '.up_path.' for '.s:local_vimrc.' ... none!'
  131. endif
  132. endif
  133. " In all case, try to source the local-vimrc present in the directory
  134. " currently considered
  135. if filereadable(a:path.'/'.s:local_vimrc)
  136. if &verbose >= 2
  137. echo 'Check '.a:path.' for '.s:local_vimrc.' ... found!'
  138. endif
  139. exe 'source '.escape(a:path.'/'.s:local_vimrc, ' \$,')
  140. elseif &verbose >= 2
  141. echo 'Check '.a:path.' for '.s:local_vimrc.' ... none!'
  142. endif
  143. endfunction
  144. function! s:CheckForbiddenPath(path)
  145. let ok = a:path !~ '^\(s\=ftp:\|s\=http:\|scp:\|^$\)'
  146. return ok
  147. endfunction
  148. function! s:Main(path)
  149. " echomsg 'Sourcing: '.a:path
  150. if !s:CheckForbiddenPath(a:path)
  151. return
  152. else
  153. call s:SourceLocal(a:path)
  154. endif
  155. endfunction
  156. " Update s:k_version in local_vimrc files {{{2
  157. function! s:IncrementVersionOnSave()
  158. let l = search('let s:k_version', 'n')
  159. if l > 0
  160. let nl = substitute(getline(l), '\(let\s\+s:k_version\s*=\s*\)\(\d\+\)\s*$', '\=submatch(1).(1+submatch(2))', '')
  161. call setline(l, nl)
  162. endif
  163. endfunction
  164. " Auto-command {{{2
  165. " => automate the loading of local-vimrc's every time we change buffers
  166. aug LocalVimrc
  167. au!
  168. au BufEnter * :call s:Main(expand('<afile>:p:h'))
  169. exe 'au BufWritePre '.s:local_vimrc . ' call s:IncrementVersionOnSave()'
  170. aug END
  171. " Functions }}}1
  172. "------------------------------------------------------------------------
  173. let &cpo=s:cpo_save
  174. "=============================================================================
  175. " vim600: set fdm=marker: