Mercurial > hg > mercurial-crew-with-dirclash
view contrib/bash_completion @ 2039:0c438fd25e6e
bash_completion: small optimization
Right now we always call "hg help $cmd" to get the canonical name of $cmd
(i.e. to go from "co" to "update").
This patch optimistically assumes that $cmd is already the canonical form
and tries to generate completions for it. If that fails, it falls back
to canonicalizing $cmd and trying again.
This means that:
- if a command or alias is explicitly handled by the
_hg_command_specific function, things get somewhat faster
- as long as the canonical $cmd is handled by _hg_command_specific, all
its aliases and abbreviations are also handled.
author | Alexis S. L. Carvalho <alexis@cecm.usp.br> |
---|---|
date | Sun, 02 Apr 2006 18:20:52 +0200 |
parents | 107dc72880f8 |
children | 077a2da7f1de |
line wrap: on
line source
shopt -s extglob _hg_commands() { local commands commands="$("$hg" debugcomplete "$cur" 2>/dev/null)" || commands="" COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$commands' -- "$cur")) } _hg_paths() { local paths="$("$hg" paths 2>/dev/null | sed -e 's/ = .*$//')" COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$paths' -- "$cur")) } _hg_repos() { local i for i in $(compgen -d -- "$cur"); do test ! -d "$i"/.hg || COMPREPLY=(${COMPREPLY[@]:-} "$i") done } _hg_status() { local files="$("$hg" status -n$1 . 2>/dev/null)" COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur")) } _hg_tags() { local tags="$("$hg" tags -q 2>/dev/null)" local IFS=$'\n' COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$tags' -- "$cur")) } # this is "kind of" ugly... _hg_count_non_option() { local i count=0 local filters="$1" for ((i=1; $i<=$COMP_CWORD; i++)); do if [[ "${COMP_WORDS[i]}" != -* ]]; then if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then continue fi count=$(($count + 1)) fi done echo $(($count - 1)) } _hg() { local cur prev cmd opts i # global options that receive an argument local global_args='--cwd|-R|--repository' local hg="$1" COMPREPLY=() cur="$2" prev="$3" # searching for the command # (first non-option argument that doesn't follow a global option that # receives an argument) for ((i=1; $i<=$COMP_CWORD; i++)); do if [[ ${COMP_WORDS[i]} != -* ]]; then if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then cmd="${COMP_WORDS[i]}" break fi fi done if [[ "$cur" == -* ]]; then opts=$("$hg" debugcomplete --options "$cmd" 2>/dev/null) COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur")) return fi # global options case "$prev" in -R|--repository) _hg_repos return ;; --cwd) # Stick with default bash completion return ;; esac if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then _hg_commands return fi # try to generate completion candidates for whatever command the user typed local help local canonical=0 if _hg_command_specific; then return fi # canonicalize the command name and try again help=$("$hg" help "$cmd" 2>/dev/null) if [ $? -ne 0 ]; then # Probably either the command doesn't exist or it's ambiguous return fi cmd=${help#hg } cmd=${cmd%%[$' \n']*} canonical=1 _hg_command_specific } _hg_command_specific() { if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" == --rev ]; then if [ $canonical = 1 ]; then _hg_tags return 0 elif [[ status != "$cmd"* ]]; then _hg_tags return 0 else return 1 fi fi case "$cmd" in help) _hg_commands ;; export|manifest|update) _hg_tags ;; pull|push|outgoing|incoming) _hg_paths _hg_repos ;; paths) _hg_paths ;; add) _hg_status "u" ;; commit) _hg_status "mar" ;; remove) _hg_status "d" ;; forget) _hg_status "a" ;; diff) _hg_status "mar" ;; revert) _hg_status "mard" ;; clone) local count=$(_hg_count_non_option) if [ $count = 1 ]; then _hg_paths fi _hg_repos ;; debugindex|debugindexdot) COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.i" -- "$cur")) ;; debugdata) COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.d" -- "$cur")) ;; *) return 1 ;; esac return 0 } complete -o bashdefault -o default -F _hg hg 2>/dev/null \ || complete -o default -F _hg hg