about summary refs log tree commit diff
path: root/Completion/Core/_match
blob: a4499dc0876148e65abdc5bab33cfd6ad2284616 (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
#autoload

# This is intended to be used as a completer function after the normal
# completer as in: `compconf completer=_complete:_match'.
# It temporarily switches on pattern matching, allowing you to try 
# completion on patterns without having to setopt glob_complete.
#
# Note, however, that this is only really useful if you don't use the
# expand-or-complete function because otherwise the pattern will
# be expanded using globbing.
#
# Configuration keys used:
#
#  match_original
#    If this is set to a `only', pattern matching will only be tried
#    with the string from the line. If it is set to any other non-empty
#    string, the original pattern will be tried first and if that yields
#    no completions, matching will be tried again with a `*' inserted
#    at the cursor position. If this key is not set or set to an empty
#    string, matching will only be attempted with the `*' inserted.
#
#  match_insert
#    If this is set to a string starting with `unambig', menucompletion
#    will only be turned on if no unambiguous string could be built
#    that is at least as long as the original string.

local tmp opm="$compstate[pattern_match]" ret=0

# Do nothing if we don't have a pattern or there are still global
# match specifications to try.

tmp="${${:-$PREFIX$SUFFIX}#[~=]}"
[[ "$tmp:q" = "$tmp" ||
   compstate[matcher] -ne compstate[total_matchers] ]] && return 1

# Try completion without inserting a `*'?

if [[ -n "$compconfig[match_original]" ]]; then
  compstate[matcher]=-1
  compstate[pattern_match]='-'
  _complete && ret=1
  compstate[pattern_match]="$opm"
  compstate[matcher]="$compstate[total_matchers]"

  if (( ret )); then
    [[ "$compconfig[match_insert]" = unambig* &&
       $#compstate[unambiguous] -ge ${#:-${PREFIX}${SUFFIX}} ]] && 
        compstate[pattern_insert]=unambiguous
    return 0
  fi
fi

# No completion with inserting `*'?

[[ "$compconfig[match_original]" = only ]] && return 1

compstate[matcher]=-1
compstate[pattern_match]='*'
_complete && ret=1
compstate[pattern_match]="$opm"
compstate[matcher]="$compstate[total_matchers]"

[[ ret -eq 1 && "$compconfig[match_insert]" = unambig* &&
   $#compstate[unambiguous] -ge ${#:-${PREFIX}${SUFFIX}} ]] && 
    compstate[pattern_insert]=unambiguous

return 1-ret