Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
add smart NERDTree open
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikołaj Biel committed Sep 19, 2016
1 parent 0decec1 commit 23472db
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# vim-nerdtree-tabs changelog

## v1.4.7

* Add smart auto NERDTree open.

## v1.4.6

* Add NERDTreeTabsFind function.
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ You can switch on/off some features of the plugin by setting global vars to 1
values:

* `g:nerdtree_tabs_open_on_gui_startup` (default: `1`)
Open NERDTree on gvim/macvim startup
Open NERDTree on gvim/macvim startup. (When set to `2`,
open only if directory was given as startup argument).

* `g:nerdtree_tabs_open_on_console_startup` (default: `0`)
Open NERDTree on console vim startup
Open NERDTree on console vim startup. (When set to `2`,
open only if directory was given as startup argument).

* `g:nerdtree_tabs_no_startup_for_diff` (default: `1`)
Do not open NERDTree if vim starts in diff mode
Expand Down
40 changes: 30 additions & 10 deletions nerdtree_plugin/vim-nerdtree-tabs.vim
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
" === plugin configuration variables === {{{
"
" open NERDTree on gvim/macvim startup
" Open NERDTree on gvim/macvim startup. When set to `2`,
" open only if directory was given as startup argument.
if !exists('g:nerdtree_tabs_open_on_gui_startup')
let g:nerdtree_tabs_open_on_gui_startup = 1
endif

" open NERDTree on console vim startup (off by default)
" Open NERDTree on console vim startup (off by default). When set to `2`,
" open only if directory was given as startup argument.
if !exists('g:nerdtree_tabs_open_on_console_startup')
let g:nerdtree_tabs_open_on_console_startup = 0
endif
Expand Down Expand Up @@ -463,13 +465,12 @@ endfun
" s:VimEnterHandler() {{{
"
fun! s:VimEnterHandler()
" if the argument to vim is a directory, cd into it
if g:nerdtree_tabs_startup_cd && isdirectory(argv(0))
exe 'cd ' . escape(argv(0), '\ ')
endif
let l:open_nerd_tree_on_startup = (g:nerdtree_tabs_open_on_console_startup == 1 && !has('gui_running')) ||
\ (g:nerdtree_tabs_open_on_gui_startup == 1 && has('gui_running'))

let l:open_nerd_tree_on_startup = (g:nerdtree_tabs_open_on_console_startup && !has('gui_running')) ||
\ (g:nerdtree_tabs_open_on_gui_startup && has('gui_running'))
let l:open_directory_on_startup = isdirectory(argv(0)) &&
\ ((g:nerdtree_tabs_open_on_console_startup == 2 && !has('gui_running')) ||
\ (g:nerdtree_tabs_open_on_gui_startup == 2 && has('gui_running')))

if g:nerdtree_tabs_no_startup_for_diff && &diff
let l:open_nerd_tree_on_startup = 0
Expand All @@ -478,16 +479,35 @@ fun! s:VimEnterHandler()
" this makes sure that globally_active is true when using 'gvim .'
let s:nerdtree_globally_active = l:open_nerd_tree_on_startup

if l:open_nerd_tree_on_startup
" if the argument to vim is a directory, cd into it
if l:open_directory_on_startup || g:nerdtree_tabs_startup_cd && isdirectory(argv(0))
exe 'cd ' . escape(argv(0), '\ ')
endif


if l:open_nerd_tree_on_startup || l:open_directory_on_startup
let l:focus_file = !s:IfFocusOnStartup()
let l:main_bufnr = bufnr('%')

if !s:IsNERDTreePresentInCurrentTab()
call s:NERDTreeOpenAllTabs()
endif

if (l:focus_file && g:nerdtree_tabs_smart_startup_focus == 1) || g:nerdtree_tabs_smart_startup_focus == 2
if (l:focus_file && g:nerdtree_tabs_smart_startup_focus == 1) ||
\ g:nerdtree_tabs_smart_startup_focus == 2 ||
\ l:open_directory_on_startup
exe bufwinnr(l:main_bufnr) . "wincmd w"
endif

if l:open_directory_on_startup
" close buffer not connected to NERDTree and open connected one
new
exe bufwinnr(l:main_bufnr) . "wincmd w"
quit

if g:nerdtree_tabs_smart_startup_focus != 2
NERDTreeFocus
endif
endif
endif
endfun
Expand Down

0 comments on commit 23472db

Please sign in to comment.