From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23661 invoked by alias); 7 Nov 2016 06:22:55 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 22100 Received: (qmail 22915 invoked from network); 7 Nov 2016 06:22:55 -0000 X-Qmail-Scanner-Diagnostics: from li268-139.members.linode.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(178.79.148.139):SA:0(0.0/5.0):. Processed in 0.56445 secs); 07 Nov 2016 06:22:55 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=unavailable autolearn_force=no version=3.4.1 X-Envelope-From: ml@kybu.org X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: none (ns1.primenet.com.au: domain at kybu.org does not designate permitted sender hosts) Date: Mon, 7 Nov 2016 06:22:44 +0000 From: ml@kybu.org To: zsh-users@zsh.org Subject: Re: Menu completion on empty line Message-ID: <20161107062244.GA30450@kybu.org> References: <20161103071706.GA24640@kybu.org> <161105190150.ZM1605@torch.brasslantern.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <161105190150.ZM1605@torch.brasslantern.com> User-Agent: Mutt/1.5.23 (2014-03-12) Amazing, thank you. I will try it out. On Sat, Nov 05, 2016 at 07:01:50PM -0700, Bart Schaefer wrote: > On Nov 3, 7:17am, ml@kybu.org wrote: > } > } If I press the tab key on an empty line and I am in a build directory, > } which can be detected by a presence of, let's say the makefile file, > } it would be useful to get a menu with items: clean build rebuild. I > } could use the tab key to move around these items. If I choose one, it > } would expand to a particular command, like 'make clean'. > > As a prelude, there are two problems with this. > > (1) Completion is VERY single-word-oriented. Anything that completes to > something with spaces in it is going to be quoted by default, so you have > to jump through even more hoops than usual to make it work. > > I would recommend avoiding this by creating three aliases > alias clean='make clean' > alias build='make all' > alias rebuild='clean;build' > and then use the menu to select those aliases. > > (2) By default TAB doesn't do anything on an empty command line except > insert itself. To fix that, you need: > > zstyle ':completion::*' insert-tab 'pending=1' > > With that out of the way ... > > There are two approaches here: Change the widget invoked by the TAB > key, or change the handling for the context of an empty line. The > former would let you do the hoop-jumping to insert multiple words as > the completion, but the latter is probably what you want to do. > > To get started, note that with compinit loaded you can always type > control-x h to get a description of the current context. If you do > that on a blank line, you get: > > tags in context :completion::complete:-command-:: > commands builtins functions aliases suffix-aliases reserved-words jobs parameters (_command_names _autocd) > commands > (_path_commands _command_names _autocd) > jobs > (_jobs _command_names _autocd) > parameters > (_parameters _command_names _autocd) > > This is telling you that: > * the start of an empty line is the special "-command-" context; > * there are eight tags recognized in this context; > * there are five completion functions that may supply candidate > completions for these tags, and which functions use which tags. > > The important thing in this case is that you would want to change the > handling of the -command- context. Here again you have two choices: > Replace the default completion function for -command-, or augment > one of the existing tags. > > I'll note at this point that your three aliases show up in the "aliases" > tag automatically, and if that were good enough we just stop here. But > you want special handling if there's a makefile, so on we go. > > There is an example in the documentation of supplementing a context. > It's found under the fake-always style and discusses the context of the > "cd" command. We need to change it to the context we care about. > > # Create a preferred aliases-make tag for build commands > zstyle ':completion::*:-command-::' \ > tag-order 'aliases:-make:"build\ commands"' '*' > > The "*" there in place of "complete" makes this work for other completers, > though that's not strictly necessary. The additional '*' in the style > definition means that if there is something on the line and it does not > match one of those three aliases, you'll get normal completion. Next: > > # Ignore everything but our three aliases for make > zstyle ':completion::*:-command-:*:aliases-make' \ > ignored-patterns '*~(clean|build|rebuild)' > > Now if you press TAB on an empty line, you'll see only your three > aliases. > > To limit it so this happens only when there is a makefile present, we > need to use "zstyle -e": > > # Create a preferred aliases-make tag for build commands > # but do so only when there is a makefile in this directory > zstyle -e ':completion::*:-command-::' \ > tag-order '[[ -f makefile || -f Makefile ]] && > reply=("aliases:-make:build\ commands" "*")' > > And there you have it. > > > > -- > Barton E. Schaefer