about summary refs log tree commit diff
path: root/Functions/Zle/match-word-context
blob: da68b6c7545ac8625bda630aff372e07724ee97a (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
# See if we can extend the word context to something more specific.
# curcontext must be set to the base context by this point; it
# will be appended to directly.

emulate -L zsh
setopt extendedglob

local -a worcon bufwords
local pat tag lastword word
integer iword

zstyle -a $curcontext word-context worcon || return 0

if (( ${#worcon} % 2 )); then
  zle -M "Bad word-context style in context $curcontext"
  return
fi

bufwords=(${(z)LBUFFER})
iword=${#bufwords}
lastword=${bufwords[-1]}
bufwords=(${(z)BUFFER})

if [[ $lastword = ${bufwords[iword]} ]]; then
  # If the word immediately left of the cursor is complete,
  # we're not on it.  Either we're on unquoted whitespace, or
  # the start of a new word.  Test the latter.
  if [[ -z $RBUFFER ]]; then
    # Nothing there, so not in a word.
      word=''
  elif [[ $RBUFFER[1] = [[:space:]] ]]; then
    # Whitespace, so not in a word.
    word=' '
  else
    # We want the next word along.
    word=${bufwords[iword+1]}
  fi
else
  # We're on a word.
  word=${bufwords[iword]}
fi

for pat tag in "${worcon[@]}"; do
  if [[ $word = ${~pat} ]]; then
    curcontext+=":$tag"
    return
  fi
done