# HG changeset patch # User Ludovic Chabant # Date 1426809991 25200 # Node ID aac9b5b1f921679dd866c9ae251150583bcc00a1 # Parent 65a5097587d4ff4cb7e667caac59b446a3c50c80 Fix how we set the CWD in Vim based on the project root. diff -r 65a5097587d4 -r aac9b5b1f921 vim/vimrc --- a/vim/vimrc Thu Mar 19 17:02:58 2015 -0700 +++ b/vim/vimrc Thu Mar 19 17:06:31 2015 -0700 @@ -196,8 +196,8 @@ set background=dark else set background=dark - let g:solarized_termcolors = 256 - let g:solarized_termtrans = 1 + "let g:solarized_termcolors = 256 + "let g:solarized_termtrans = 1 endif colorscheme solarized @@ -324,7 +324,8 @@ " I'm in. augroup VimRCAutoCWD au! - autocmd BufEnter * call s:SetProjectRootCwd() + autocmd BufReadPost * call s:SetProjectRootCwd(1) + autocmd BufEnter * call s:SetProjectRootCwd(0) augroup END augroup VimRCFileType_markdown @@ -581,7 +582,7 @@ let l:previous_cur = '' let l:slash = '/' if has('win32') - let l:slash = '\\' + let l:slash = '\' endif while l:cur != l:previous_cur let l:marker_path = l:cur . l:slash . a:marker @@ -594,28 +595,40 @@ return '' endfunction -function! s:SetProjectRootCwd() abort +function! s:SetProjectRootCwd(recompute) abort + if a:recompute != 1 && exists('b:ludo_workdir') + execute 'lcd!' fnameescape(b:ludo_workdir) + return + endif + let l:cur_file_dir = expand('%:p:h', 1) if l:cur_file_dir =~ '\v^(\w+:)?(//|\\\\)' " Don't do shit on filenames coming from the network or something. return endif let l:found_root = 0 - let l:root = l:cur_file_dir + let l:root = '' let l:markers = [] if exists('g:ctrlp_root_markers') let l:markers += g:ctrlp_root_markers endif let l:markers += ['.git', '.hg', '.svn', '.bzr', '_darcs'] + let l:unique_markers = [] for marker in l:markers + if index(l:unique_markers, marker) < 0 + call add(l:unique_markers, marker) + endif + endfor + " Find the project root closest to the current file. + for marker in l:unique_markers let l:proj_root = s:FindProjectRoot(l:cur_file_dir, marker) - if l:proj_root != '' + if l:proj_root != '' && len(l:proj_root) > len(l:root) let l:root = l:proj_root let l:found_root = 1 - break endif endfor if l:found_root + let b:ludo_workdir = l:root execute 'lcd!' fnameescape(l:root) endif endfunction