正如the document所说,zle变量游标只能在[0, $#BUFFER]范围内。
测试代码(放入.zshrc,^[OP为F1):
testCursor() {
echo "\nOriginal C: $CURSOR"
BUFFER="a"
echo "Change Buffer: $CURSOR"
CURSOR=$((CURSOR+10))
echo "Force edit: $CURSOR"
CURSOR=100
echo "Force assign: $CURSOR"
}
zle -N testCursor
bindkey '^[OP' testCursor

CURSOR在运行时满足了它的范围定义,zsh-zle是如何实现的?
发布于 2020-09-08 21:45:01
Zsh值是在CURSOR的源代码中处理的,源代码是用C编程语言https://github.com/zsh-users/zsh/blob/3c93497eb701d8f220bc32d38e1f12bfb534c390/Src/Zle/zle_params.c#L266实现的
您无法在Zsh shell代码中声明类似的约束变量。
但是,您可以为它编写一个数学函数:
# Declare a global integer.
typeset -gi TEST=0
# -H makes these hidden, that is, not listed automatically.
typeset -gHi _TEST_MIN=0 _TEST_MAX=10
# Load `min` and `max` functions.
autoload -Uz zmathfunc && zmathfunc
set_test() {
(( TEST = min(max($1,$_TEST_MIN),$_TEST_MAX) ))
}
get_test() {
return $(( min(max($TEST,$_TEST_MIN),$_TEST_MAX) ))
}
# Declare `set_test` as a math function accepting exactly one numeric argument.
functions -M set_test 1
# Declare `get_test` as a math function accepting exactly zero arguments.
functions -M get_test 0然后,您可以使用以下语法在算术语句中使用这些语句:
❯ print $(( get_test() ))
0
❯ (( set_test(100) ))
❯ print $(( get_test() ))
10但也可以在其他上下文中使用此语法:
❯ set_test -1
❯ get_test; print $?
0https://stackoverflow.com/questions/63572571
复制相似问题