about summary refs log tree commit diff
path: root/Completion/Base/Utility/_shadow
blob: 5b0f79c362c9eb2f4b7956f5f225955fe80ee6fb (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
71
#autoload

## Recommended usage:
#  {
#    _shadow fname
#    function fname {
#      # Do your new thing
#    }
#    # Invoke callers of fname
#  } always {
#    _unshadow fname
#  }
## Alternate usage:
# {
#   _shadow -s suffix fname
#   function fname {
#     # Do other stuff
#     fname@suffix new args for fname
#   }
#   # Invoke callers of fname
# } always {
#   _unshadow -s suffix fname
# }
##

# BUGS:
# * `functions -c` acts like `autoload +X`
# * name collisions are possible in alternate usage
# * functions that examine $0 probably misfire

zmodload zsh/parameter # Or what?

# This probably never comes up, but protect ourself from recursive call
# chains that may duplicate the top elements of $funcstack by creating
# a counter of _shadow calls and using it to make shadow names unique.
typeset -gHi _shadowdepth=0

# Create a copy of each fname so that a caller may redefine
_shadow() {
  local -A fsfx=( -s ${funcstack[2]}:${functrace[2]}:$((_shadowdepth+1)) )
  local fname
  zparseopts -K -A fsfx -D s:
  for fname; do
    local shadowname=${fname}@${fsfx[-s]}
    (( ${+functions[$fname]} )) &&
      builtin functions -c $fname $shadowname
  done
  ((_shadowdepth++))
}

# Remove the redefined function and shadowing name
_unshadow() {
  local -A fsfx=( -s ${funcstack[2]}:${functrace[2]}:${_shadowdepth} )
  local fname
  zparseopts -K -A fsfx -D s:
  for fname; do
    local shadowname=${fname}@${fsfx[-s]}
    if (( ${+functions[$shadowname]} )); then
      builtin functions -c $shadowname $fname
      builtin unfunction $shadowname
    elif (( ${+functions[$fname]} )); then
      builtin unfunction $fname
    fi
  done
  ((_shadowdepth--))
}

# This is tricky.  When we call _shadow recursively from autoload,
# there's an extra level of stack in $functrace that will confuse
# the later call to _unshadow.  Fool ourself into working correctly.
(( ARGC )) && _shadow -s ${funcstack[2]}:${functrace[2]}:1 "$@"