From 84737f2ebe533ffac9ebc21da8d1f57216962641 Mon Sep 17 00:00:00 2001 From: Chris Patuzzo Date: Sun, 24 Feb 2019 23:01:12 +0000 Subject: [PATCH] Add a NERDTreeMinimalMenu feature (#938) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Extract the menu action prompts to a function * Add a NERDTreeMinimalMenu configuration option If this option is set, Vim’s command line displays a single-line version of the menu: Which action? (a*/m/d/r/o/q/c/l): Pressing ‘a’ will call the ‘add child node’ menu item as that is its shortcut. The * denotes the position of the currently selected menu item which can be changed with NERDTreeMenuDown and Up in the usual way. The user may wish to set these to , or h, l if they prefer. * Minimise the text once a menu item is chosen If NERDTreeMinimalMenu is set, then echo a single line prompt after the menu item is chosen. This reduces noise for users who are already familiar with how the menu works and prevents Vim scrolling down to accommodate the menu text. If the directory is non-empty we display ‘Delete directory?’ instead of ‘Delete?’ and the user must still type ‘yes’ to confirm, rather than just pressing ‘y’. * Document the new NERDTreeMinimalMenu feature * Tweak the format for the minimal menu prompt As per @PhilRunninger’s suggestions: - The menu now shows the ‘j/k/enter’ help text - The first word of the selected item is shown* - Use commas between menu items so that it’s visually distinct from the ‘j/k/enter’ text * I tried displaying the full menuItem text, but this can be quite long, e.g. > (o)pen the current node with system editor This causes the menu to jump around a lot. We could add another minimal versions of these, but I think the first word seems ok for now. --- doc/NERDTree.txt | 23 +++++++++ lib/nerdtree/menu_controller.vim | 40 ++++++++++++---- nerdtree_plugin/fs_menu.vim | 82 ++++++++++++++++++++++---------- plugin/NERD_tree.vim | 1 + 4 files changed, 111 insertions(+), 35 deletions(-) diff --git a/doc/NERDTree.txt b/doc/NERDTree.txt index 0034a93..5bf169c 100644 --- a/doc/NERDTree.txt +++ b/doc/NERDTree.txt @@ -719,6 +719,9 @@ the NERD tree. These settings should be set in your vimrc, using `:let`. |NERDTreeMinimalUI| Disables display of the 'Bookmarks' label and 'Press ? for help' text. +|NERDTreeMinimalMenu| Use a compact menu that fits on a single line + for adding, copying, deleting, etc + |NERDTreeCascadeSingleChildDir| Collapses on the same line directories that have only one child directory. @@ -1116,6 +1119,26 @@ of the following lines for this setting: > let NERDTreeMinimalUI=1 < +------------------------------------------------------------------------------ + *NERDTreeMinimalMenu* +Values: 0 or 1 +Default: 0 + +This setting makes NERD tree use a smaller, more compact menu for adding, +copying, deleting nodes. This menu fits on a single line so Vim doesn't need to +scroll down to present it. This setting is recommended for users already +familiar with the menu items. It will look similar to this: + + Menu: [ (a)dd ,m,d,r,o,q,c,l] (Use j/k/enter or shortcut): + +An action can be selected with its shortcut key or with the NERDTreeMenuUp and +NERDTreeMenuDown keys, then pressing enter. + +Use one of the following lines for this setting: > + let NERDTreeMinimalMenu=0 + let NERDTreeMinimalMenu=1 + + ------------------------------------------------------------------------------ *NERDTreeCascadeSingleChildDir* Values: 0 or 1 diff --git a/lib/nerdtree/menu_controller.vim b/lib/nerdtree/menu_controller.vim index 84bdb26..26dbd29 100644 --- a/lib/nerdtree/menu_controller.vim +++ b/lib/nerdtree/menu_controller.vim @@ -15,6 +15,11 @@ function! s:MenuController.New(menuItems) return newMenuController endfunction +" FUNCTION: s:MenuController.isMinimal() {{{1 +function! s:MenuController.isMinimal() + return g:NERDTreeMinimalMenu +endfunction + " FUNCTION: MenuController.showMenu() {{{1 " Enter the main loop of the NERDTree menu, prompting the user to select " a menu item. @@ -49,16 +54,27 @@ endfunction "FUNCTION: MenuController._echoPrompt() {{{1 function! s:MenuController._echoPrompt() - echo "NERDTree Menu. Use " . g:NERDTreeMenuDown . "/" . g:NERDTreeMenuUp . "/enter and the shortcuts indicated" - echo "==========================================================" + let navHelp = "Use " . g:NERDTreeMenuDown . "/" . g:NERDTreeMenuUp . "/enter" - for i in range(0, len(self.menuItems)-1) - if self.selection == i - echo "> " . self.menuItems[i].text - else - echo " " . self.menuItems[i].text - endif - endfor + if self.isMinimal() + let selection = self.menuItems[self.selection].text + + let shortcuts = map(copy(self.menuItems), "v:val['shortcut']") + let shortcuts[self.selection] = " " . split(selection)[0] . " " + + echo "Menu: [" . join(shortcuts, ",") . "] (" . navHelp . " or shortcut): " + else + echo "NERDTree Menu. " . navHelp . " . or the shortcuts indicated" + echo "=========================================================" + + for i in range(0, len(self.menuItems)-1) + if self.selection == i + echo "> " . self.menuItems[i].text + else + echo " " . self.menuItems[i].text + endif + endfor + endif endfunction "FUNCTION: MenuController._current(key) {{{1 @@ -129,7 +145,11 @@ endfunction "FUNCTION: MenuController._setCmdheight() {{{1 "sets &cmdheight to whatever is needed to display the menu function! s:MenuController._setCmdheight() - let &cmdheight = len(self.menuItems) + 3 + if self.isMinimal() + let &cmdheight = 1 + else + let &cmdheight = len(self.menuItems) + 3 + endif endfunction "FUNCTION: MenuController._saveOptions() {{{1 diff --git a/nerdtree_plugin/fs_menu.vim b/nerdtree_plugin/fs_menu.vim index e0911e4..3eef517 100644 --- a/nerdtree_plugin/fs_menu.vim +++ b/nerdtree_plugin/fs_menu.vim @@ -44,6 +44,47 @@ else call NERDTreeAddMenuItem({'text': '(l)ist the current node', 'shortcut': 'l', 'callback': 'NERDTreeListNodeWin32'}) endif +"FUNCTION: s:inputPrompt(action){{{1 +"returns the string that should be prompted to the user for the given action +" +"Args: +"action: the action that is being performed, e.g. 'delete' +function! s:inputPrompt(action) + if a:action == "add" + let title = "Add a childnode" + let info = "Enter the dir/file name to be created. Dirs end with a '/'" + let minimal = "Add node:" + + elseif a:action == "copy" + let title = "Copy the current node" + let info = "Enter the new path to copy the node to:" + let minimal = "Copy to:" + + elseif a:action == "delete" + let title = "Delete the current node" + let info = "Are you sure you wish to delete the node:" + let minimal = "Delete?" + + elseif a:action == "deleteNonEmpty" + let title = "Delete the current node" + let info = "STOP! Directory is not empty! To delete, type 'yes'" + let minimal = "Delete directory?" + + elseif a:action == "move" + let title = "Rename the current node" + let info = "Enter the new path for the node:" + let minimal = "Move to:" + endif + + if g:NERDTreeMenuController.isMinimal() + redraw! " Clear the menu + return minimal . " " + else + let divider = "==========================================================" + return title . "\n" . divider . "\n" . info . "\n" + end +endfunction + "FUNCTION: s:promptToDelBuffer(bufnum, msg){{{1 "prints out the given msg and, if the user responds by pushing 'y' then the "buffer with the given bufnum is deleted @@ -114,14 +155,12 @@ function! s:renameBuffer(bufNum, newNodeName, isDirectory) " This happens when answering Cancel if confirmation is needed. Do nothing. endtry endfunction + "FUNCTION: NERDTreeAddNode(){{{1 function! NERDTreeAddNode() let curDirNode = g:NERDTreeDirNode.GetSelected() - - let newNodeName = input("Add a childnode\n". - \ "==========================================================\n". - \ "Enter the dir/file name to be created. Dirs end with a '/'\n" . - \ "", curDirNode.path.str() . g:NERDTreePath.Slash(), "file") + let prompt = s:inputPrompt("add") + let newNodeName = input(prompt, curDirNode.path.str() . g:NERDTreePath.Slash(), "file") if newNodeName ==# '' call nerdtree#echo("Node Creation Aborted.") @@ -144,6 +183,8 @@ function! NERDTreeAddNode() call NERDTreeRender() call newTreeNode.putCursorHere(1, 0) endif + + redraw! catch /^NERDTree/ call nerdtree#echoWarning("Node Not Created.") endtry @@ -152,10 +193,8 @@ endfunction "FUNCTION: NERDTreeMoveNode(){{{1 function! NERDTreeMoveNode() let curNode = g:NERDTreeFileNode.GetSelected() - let newNodePath = input("Rename the current node\n" . - \ "==========================================================\n" . - \ "Enter the new path for the node: \n" . - \ "", curNode.path.str(), "file") + let prompt = s:inputPrompt("move") + let newNodePath = input(prompt, curNode.path.str(), "file") if newNodePath ==# '' call nerdtree#echo("Node Renaming Aborted.") @@ -194,7 +233,7 @@ function! NERDTreeMoveNode() call curNode.putCursorHere(1, 0) - redraw + redraw! catch /^NERDTree/ call nerdtree#echoWarning("Node Not Renamed.") endtry @@ -209,21 +248,16 @@ function! NERDTreeDeleteNode() if currentNode.path.isDirectory && ((currentNode.isOpen && currentNode.getChildCount() > 0) || \ (len(currentNode._glob('*', 1)) > 0)) - let choice =input("Delete the current node\n" . - \ "==========================================================\n" . - \ "STOP! Directory is not empty! To delete, type 'yes'\n" . - \ "" . currentNode.path.str() . ": ") + let prompt = s:inputPrompt("deleteNonEmpty") . currentNode.path.str() . ": " + let choice = input(prompt) let confirmed = choice ==# 'yes' else - echo "Delete the current node\n" . - \ "==========================================================\n". - \ "Are you sure you wish to delete the node:\n" . - \ "" . currentNode.path.str() . " (yN):" + let prompt = s:inputPrompt("delete") . currentNode.path.str() . " (yN): " + echo prompt let choice = nr2char(getchar()) let confirmed = choice ==# 'y' endif - if confirmed try call currentNode.delete() @@ -237,7 +271,7 @@ function! NERDTreeDeleteNode() call s:promptToDelBuffer(bufnum, prompt) endif - redraw + redraw! catch /^NERDTree/ call nerdtree#echoWarning("Could not remove node") endtry @@ -292,10 +326,8 @@ function! NERDTreeCopyNode() let l:shellslash = &shellslash let &shellslash = 0 let currentNode = g:NERDTreeFileNode.GetSelected() - let newNodePath = input("Copy the current node\n" . - \ "==========================================================\n" . - \ "Enter the new path to copy the node to: \n" . - \ "", currentNode.path.str(), "file") + let prompt = s:inputPrompt("copy") + let newNodePath = input(prompt, currentNode.path.str(), "file") if newNodePath != "" "strip trailing slash @@ -329,7 +361,7 @@ function! NERDTreeCopyNode() call nerdtree#echo("Copy aborted.") endif let &shellslash = l:shellslash - redraw + redraw! endfunction " FUNCTION: NERDTreeQuickLook() {{{1 diff --git a/plugin/NERD_tree.vim b/plugin/NERD_tree.vim index c3de3f2..5a04fc3 100644 --- a/plugin/NERD_tree.vim +++ b/plugin/NERD_tree.vim @@ -51,6 +51,7 @@ call s:initVariable("g:NERDTreeSortHiddenFirst", 1) call s:initVariable("g:NERDTreeChDirMode", 0) call s:initVariable("g:NERDTreeCreatePrefix", "silent") call s:initVariable("g:NERDTreeMinimalUI", 0) +call s:initVariable("g:NERDTreeMinimalMenu", 0) if !exists("g:NERDTreeIgnore") let g:NERDTreeIgnore = ['\~$'] endif