zsh zle如何防止在运行时随意修改变量CURSOR?

问题描述

正如the document所说,zle变量CURSOR只能在[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

enter image description here

CURSOR在运行时满足了它的范围定义,zsh-zle是如何实现的?

解决方法

CURSOR的值在Zsh的源代码中处理,该源代码以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_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 $?
0