about summary refs log tree commit diff
path: root/Completion/Commands/_next_tags
blob: 8bd3f5921d925eb41ff1cfa198b20fd179de47c9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#compdef -k complete-word \C-xn

# Main widget/completer.

_next_tags() {

  if [[ $#funcstack -gt 1 ]]; then

    # Called as completer, probably `remove' our helper function. A better
    # test would be nice, but I think one should still be able to edit the
    # current word between attempts to complete it.

    [[ $_next_tags_pre != ${LBUFFER%${PREFIX}} ]] && unset _sort_tags

    return 1
  else
    local comp

    if [[ -z $compstate[old_list] ]]; then
      comp=()
    else
      comp=(_next_tags _complete)
    fi

    (( $+_sort_tags )) || _next_tags_not=

    _sort_tags=_next_tags_sort
    _next_tags_pre="${LBUFFER%${PREFIX}}"
    _next_tags_not="$_next_tags_not $_lastcomp[tags]"

    _main_complete "$comp[@]"

    [[ $compstate[insert] = automenu ]] &&
       compstate[insert]=automenu-unambiguous

    compstate[insert]=''
    compstate[list]='list force'
  fi
}

# Helper function for sorting tags. Most of this is copied from _tags.

_next_tags_sort() {
  local order tags tag nodef

  zstyle -a ":completion:${curcontext}:" tag-order order ||
    order=( 'arguments values' options globbed-files directories all-files )

  # But we also remove the tags we've already tried...

  tags=( "${(@)order:#(${(j:|:)~${=_next_tags_not}})}" )

  # ... unless that would remove all offered tags.

  [[ $#tags -ne $#order && "$tags" != *(${(j:|:)~argv})* ]] &&
    tags=( $order ) _next_tags_not=

  for tag in $tags; do
    case $tag in
    -)     nodef=yes;;
    *\(\)) "${${tag%%[ 	]#\(\)}##[ 	]#}" "$@";;
    \!*)   comptry "${(@)argv:#(${(j:|:)~${=tag[2,-1]}})}";;
    ?*)    comptry ${=tag};;
    esac
  done

  [[ -z "$nodef" ]] && comptry "$@"
}

[[ -o kshautoload ]] || _next_tags "$@"