zsh-users
 help / color / mirror / code / Atom feed
* Completion for command-not-found "commands"
@ 2012-07-24  4:08 Benjamin R. Haskell
  2012-07-24 15:49 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Benjamin R. Haskell @ 2012-07-24  4:08 UTC (permalink / raw)
  To: Zsh Users

The very-short version of my question is: How can I dynamically get 
completion for `git rm` when what I've typed on the commandline so far 
is `grm`?  ("dynamically" meaning without defining a bunch of compdef's 
up front.)

Longer version:

A colleague of mine finally convinced me that using his 'g___' aliases 
for git commands was superior to using my 'g ___' versions.  E.g.:

==> in his ~/.bashrc <==
alias gst='git status'
alias gco='git checkout'
========================

==> vs. my ~/.gitconfig <==
[alias]
   st = status
   co = checkout
==> and my ~/.zshrc <==
alias g=git
=======================

However, I'm still convinced that I'd rather keep mine as git 
[alias]'es, rather than shell aliases (When getting slightly complex, 
git [alias]es can be shell functions).  So, I wrote the following:

valid_git_alias () {
   git config -l | grep -qF "alias.$1="
}

valid_git_command () {
   # my `awk` is better than my `zsh` for this kind of string manipulation:
   git help --all | awk '/---/ { ok=1 ; OFS="\n" ; ORS="" } /^ / { NF=NF+1 ; if (ok) print $0 }' | grep -qF $1
}

command_not_found_handler () {
  [[ $1 = g* ]] || return 1
  local al=${1#g}
  shift
  valid_git_alias $al || valid_git_command $al || return 1
  git $al "$@"
  return 0
}

This lets me do:

`gst` -- gets converted to `git st` (which is one of my git [alias]es)
`grm` -- gets converted to `git rm` (which is a built-in git command)

The problem is that I've lost shell-completion (which I had with the `g 
___` version).  E.g. `g checkout <Tab>` would complete branches and 
tags.  And, with:

_git-co () { _git-checkout "$@" }

`g co <Tab>` would behave the same.

-- 
Best,
Ben


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Completion for command-not-found "commands"
  2012-07-24  4:08 Completion for command-not-found "commands" Benjamin R. Haskell
@ 2012-07-24 15:49 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2012-07-24 15:49 UTC (permalink / raw)
  To: Zsh Users

On Jul 24, 12:08am, Benjamin R. Haskell wrote:
}
} The very-short version of my question is: How can I dynamically get 
} completion for `git rm` when what I've typed on the commandline so far 
} is `grm`?  ("dynamically" meaning without defining a bunch of compdef's 
} up front.)

You probably want to write a completer function to insert into your
"completers" style.  This is a crude version but mostly works:

    _check_for_git_alias () {
	[[ $words[1] = g* ]] || return 1
	local al=${words[1]#g}

	if valid_git_alias $al || valid_git_command $al
	then
	    words[1]=(git $al)
	    ((++CURRENT))
	    _normal
	else
	    return 1
	fi
    }
    zstyle ':completion:*' completer _check_for_git_alias \
	whatever you already had for this style goes here

Obviously there are a bunch more tests you can do to figure out whether
it's even reasonable to treat $words[1] as a git alias or command, and
you may want to try this in some other ordering of the zstyle rather
than right at the beginning, but hopefully this gets you started.

-- 
Barton E. Schaefer


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-07-24 15:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-24  4:08 Completion for command-not-found "commands" Benjamin R. Haskell
2012-07-24 15:49 ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).