Mercurial > dotfiles
comparison vim/vimrc @ 291:aac9b5b1f921
Fix how we set the CWD in Vim based on the project root.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Thu, 19 Mar 2015 17:06:31 -0700 |
parents | 8b6ca7e40f37 |
children | 8bc056d80c39 |
comparison
equal
deleted
inserted
replaced
290:65a5097587d4 | 291:aac9b5b1f921 |
---|---|
194 " Default color scheme. | 194 " Default color scheme. |
195 if has('gui_running') | 195 if has('gui_running') |
196 set background=dark | 196 set background=dark |
197 else | 197 else |
198 set background=dark | 198 set background=dark |
199 let g:solarized_termcolors = 256 | 199 "let g:solarized_termcolors = 256 |
200 let g:solarized_termtrans = 1 | 200 "let g:solarized_termtrans = 1 |
201 endif | 201 endif |
202 colorscheme solarized | 202 colorscheme solarized |
203 | 203 |
204 " Enable file type detection. | 204 " Enable file type detection. |
205 filetype indent plugin on | 205 filetype indent plugin on |
322 | 322 |
323 " Automatically change the current working directory based on a project | 323 " Automatically change the current working directory based on a project |
324 " I'm in. | 324 " I'm in. |
325 augroup VimRCAutoCWD | 325 augroup VimRCAutoCWD |
326 au! | 326 au! |
327 autocmd BufEnter * call s:SetProjectRootCwd() | 327 autocmd BufReadPost * call s:SetProjectRootCwd(1) |
328 autocmd BufEnter * call s:SetProjectRootCwd(0) | |
328 augroup END | 329 augroup END |
329 | 330 |
330 augroup VimRCFileType_markdown | 331 augroup VimRCFileType_markdown |
331 au! | 332 au! |
332 autocmd FileType text,markdown setlocal textwidth=80 | 333 autocmd FileType text,markdown setlocal textwidth=80 |
579 function! s:FindProjectRoot(cur, marker) abort | 580 function! s:FindProjectRoot(cur, marker) abort |
580 let l:cur = a:cur | 581 let l:cur = a:cur |
581 let l:previous_cur = '' | 582 let l:previous_cur = '' |
582 let l:slash = '/' | 583 let l:slash = '/' |
583 if has('win32') | 584 if has('win32') |
584 let l:slash = '\\' | 585 let l:slash = '\' |
585 endif | 586 endif |
586 while l:cur != l:previous_cur | 587 while l:cur != l:previous_cur |
587 let l:marker_path = l:cur . l:slash . a:marker | 588 let l:marker_path = l:cur . l:slash . a:marker |
588 if glob(l:marker_path) != '' | 589 if glob(l:marker_path) != '' |
589 return fnamemodify(l:cur, ':p') | 590 return fnamemodify(l:cur, ':p') |
592 let l:cur = fnamemodify(l:cur, ':h') | 593 let l:cur = fnamemodify(l:cur, ':h') |
593 endwhile | 594 endwhile |
594 return '' | 595 return '' |
595 endfunction | 596 endfunction |
596 | 597 |
597 function! s:SetProjectRootCwd() abort | 598 function! s:SetProjectRootCwd(recompute) abort |
599 if a:recompute != 1 && exists('b:ludo_workdir') | |
600 execute 'lcd!' fnameescape(b:ludo_workdir) | |
601 return | |
602 endif | |
603 | |
598 let l:cur_file_dir = expand('%:p:h', 1) | 604 let l:cur_file_dir = expand('%:p:h', 1) |
599 if l:cur_file_dir =~ '\v^(\w+:)?(//|\\\\)' | 605 if l:cur_file_dir =~ '\v^(\w+:)?(//|\\\\)' |
600 " Don't do shit on filenames coming from the network or something. | 606 " Don't do shit on filenames coming from the network or something. |
601 return | 607 return |
602 endif | 608 endif |
603 let l:found_root = 0 | 609 let l:found_root = 0 |
604 let l:root = l:cur_file_dir | 610 let l:root = '' |
605 let l:markers = [] | 611 let l:markers = [] |
606 if exists('g:ctrlp_root_markers') | 612 if exists('g:ctrlp_root_markers') |
607 let l:markers += g:ctrlp_root_markers | 613 let l:markers += g:ctrlp_root_markers |
608 endif | 614 endif |
609 let l:markers += ['.git', '.hg', '.svn', '.bzr', '_darcs'] | 615 let l:markers += ['.git', '.hg', '.svn', '.bzr', '_darcs'] |
616 let l:unique_markers = [] | |
610 for marker in l:markers | 617 for marker in l:markers |
618 if index(l:unique_markers, marker) < 0 | |
619 call add(l:unique_markers, marker) | |
620 endif | |
621 endfor | |
622 " Find the project root closest to the current file. | |
623 for marker in l:unique_markers | |
611 let l:proj_root = s:FindProjectRoot(l:cur_file_dir, marker) | 624 let l:proj_root = s:FindProjectRoot(l:cur_file_dir, marker) |
612 if l:proj_root != '' | 625 if l:proj_root != '' && len(l:proj_root) > len(l:root) |
613 let l:root = l:proj_root | 626 let l:root = l:proj_root |
614 let l:found_root = 1 | 627 let l:found_root = 1 |
615 break | |
616 endif | 628 endif |
617 endfor | 629 endfor |
618 if l:found_root | 630 if l:found_root |
631 let b:ludo_workdir = l:root | |
619 execute 'lcd!' fnameescape(l:root) | 632 execute 'lcd!' fnameescape(l:root) |
620 endif | 633 endif |
621 endfunction | 634 endfunction |
622 | 635 |
623 function! s:ToggleNERDTree() abort | 636 function! s:ToggleNERDTree() abort |