无法在 Zsh 的 TAB 补全中对 Man 进行两个单词搜索

发布于 2024-07-18 11:25:01 字数 303 浏览 2 评论 0原文

问题:有一个制表符补全,它需要两个单词并计算它们与 Man 的最佳匹配,然后返回最佳匹配

示例:以下伪代码应给出我至少知道 Zsh 的反向菜单完成命令。 现在,如果没有 zgrep,我无法在手册中搜索手册。

man zsh:reverse <TAB>

其中“:”是我想要的分隔符。

初始问题:使用Zsh搜索手册时,按TAB键输入一个单词时,TAB补全会运行哪些文件?

Problem: to have a tab completion which takes two words and calculates the best match from them for Man, and then returns the best matches

Example: The following pseudo-code should give me at least Zsh's reverse-menu-complete -command. Right now, I cannot search manuals inside manuals without zgrep.

man zsh:reverse <TAB>

where ":" is the separator which I want.

Initial Problem: Which files does the TAB completion run when I press TAB for one word in searching manuals by Zsh?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

檐上三寸雪 2024-07-25 11:25:01

我将尝试深入了解 zsh 补全系统的工作原理以及解决此问题的不完整方法。

当您在 zsh 中对 man 使用 TAB 补全时运行的文件位于 /usr/share/zsh/${ZSH_VERSION}/functions 目录下。 该树因发行版而异,但文件名为 _man,并提供 manaproposwhatis 的补全。

调用 _man 后,其工作原理如下(粗略描述):

  1. 如果将 man 的完成和 --local-file 指定为第一个标志,则调用标准文件完成(< code>_files)
  2. 从一组默认值/$MANPATH 构造 manpath 变量。 这是搜索手册页的地方,
  3. 确定我们是否使用节号参数调用 man,如果是,则仅搜索
  4. zstyle ':completion:*:manuals' 单独的 那些节使用了 -sections true,输出中的单独部分(不要在它们之间混合)
  5. 调用 _man_pages 来提供匹配
  6. _man_pages 的手册页列表使用 compfiles -ppages '' '' "$matcher" '' dummy '*' 做了一些魔法。 pages 是包含所请求部分的联机帮助页的所有目录的变量。 实际的通配模式是由隐式参数 $PREFIXcompfiles 的最后一个参数 - 在本例中 * 构造的。 这导致 /usr/share/man/man1 被转换为 /usr/share/man/man1/foo*
  7. 新的 glob 模式列表被通配,获得所有与模式
  8. _man_pages 匹配的文件,然后从文件中删除所有后缀,并使用 compadd 将它们添加到完成小部件选择列表中。

现在,如您所见,该列表手册页的数量直接由 $PREFIX 变量确定。 为了使 zsh:foo 仅列出包含单词 foozsh* 手册页,需要将其拆分为 >: 字符(如果有)。

_man_pages 中的以下添加部分解决了该问题(zsh 4.3.4):

原始:

_man_pages() {
  local matcher pages dummy sopt

  zparseopts -E M+:=matcher

  if (( $#matcher )); then
    matcher=( ${matcher:#-M} )
    matcher="$matcher"
  else
    matcher=
  fi

  pages=( ${(M)dirs:#*$sect/} )

  compfiles -p pages '' '' "$matcher" '' dummy '*'
  pages=( ${^~pages}(N:t) )

  (($#mrd)) && pages[$#pages+1]=($(awk $awk $mrd))

  # Remove any compression suffix, then remove the minimum possible string
  # beginning with .<->: that handles problem cases like files called
  # `POSIX.1.5'.

  [[ $OSTYPE = solaris* ]] && sopt='-s '
  if ((CURRENT > 2)) ||
      ! zstyle -t ":completion:${curcontext}:manuals.$sect" insert-sections
  then
    compadd "$@" - ${pages%.(?|<->*(|.gz|.bz2|.Z))}
  else
    compadd "$@" -P "$sopt$sect " - ${pages%.(?|<->*(|.gz|.bz2|.Z))}
  fi
}

已修改(查找 ##mod 注释):

_man_pages() {
  local matcher pages dummy sopt

  zparseopts -E M+:=matcher

  if (( $#matcher )); then
    matcher=( ${matcher:#-M} )
    matcher="$matcher"
  else
    matcher=
  fi

  pages=( ${(M)dirs:#*$sect/} )

  ##mod
  # split components by the ":" character
  local pref_words manpage_grep orig_prefix
  # save original prefix (just in case)
  orig_prefix=${PREFIX}
  # split $PREFIX by ':' and make it an array
  pref_words=${PREFIX//:/ }
  set -A pref_words ${=pref_words}
  # if there are both manpage name and grep string, use both
  if (( $#pref_words == 2 )); then
      manpage_grep=$pref_words[2]
      # PREFIX is used internally by compfiles
      PREFIX=$pref_words[1]
  elif (( $#pref_words == 1 )) && [[ ${PREFIX[1,1]} == ":" ]]; then
      # otherwise, prefix is empty and only grep string exists
      PREFIX=
      manpage_grep=$pref_words[1]
  fi


  compfiles -p pages '' '' "$matcher" '' dummy '*'
  ##mod: complete, but don't strip path names
  pages=( ${^~pages} )

  (($#mrd)) && pages[$#pages+1]=($(awk $awk $mrd))

  ##mod: grep pages
  # Build a list of matching pages that pass the grep
  local matching_pages
  typeset -a matching_pages

  # manpage_grep exists and not empty 
  if (( ${#manpage_grep} > 0 )); then
      for p in ${pages}; do
          zgrep "${manpage_grep}" $p > /dev/null
          if (( $? == 0 )); then
              #echo "$p matched $manpage_grep"
              matching_pages+=($p)
          fi
      done
  else
  # there's no manpage_grep, so all pages match
      matching_pages=( ${pages} )
  fi

  #echo "\nmatching manpages: "${matching_pages}
  pages=( ${matching_pages}(N:t) )
  # keep the stripped prefix for now
  #PREFIX=${orig_prefix}


  # Remove any compression suffix, then remove the minimum possible string
  # beginning with .<->: that handles problem cases like files called
  # `POSIX.1.5'.


  [[ $OSTYPE = solaris* ]] && sopt='-s '
  if ((CURRENT > 2)) ||
      ! zstyle -t ":completion:${curcontext}:manuals.$sect" insert-sections
  then
    compadd "$@" - ${pages%.(?|<->*(|.gz|.bz2|.Z))}
  else
    compadd "$@" -P "$sopt$sect " - ${pages%.(?|<->*(|.gz|.bz2|.Z))}
  fi
}

但是,它仍然无法完全正常工作(如果您取消注释 #echo "$p matches $manpage_grep" 行,您可以看到它确实构建了列表) - 我怀疑在内部某个地方,完成系统看到,例如,“zshcompctl”与前缀“不匹配” zsh:foo",并且不显示结果匹配。 我试图在剥离 grep 字符串后保持 $PREFIX 不变,但它仍然不起作用。

无论如何,这至少应该让您开始。

I will attempt to provide an insight to how zsh completion system works and an incomplete go at this problem.

The file that runs when you use TAB completion for man in zsh is located under the /usr/share/zsh/${ZSH_VERSION}/functions directory. The tree varies across distributions, but the file is named _man, and provides completion for man, apropos and whatis.

After _man is invoked, it works as following (rough description):

  1. if completing for man and --local-file was specified as first flag, invoke standard files completion (_files)
  2. construct manpath variable from a set of defaults / $MANPATH. This is where the manpages will be searched
  3. determine if we invoked man with a section number parameter, if yes - only those sections will be searched
  4. if the zstyle ':completion:*:manuals' separate-sections true was used, separate sections in output (don't mix between them)
  5. invoke _man_pages to provide a list of man pages for the match
  6. _man_pages now does a bit of magic with compfiles -p pages '' '' "$matcher" '' dummy '*'. pages is the variable with all the directories containing manpages for requested section(s). The actual globbing pattern is constructed from the implicit parameter $PREFIX and the last parameter to compfiles - * in this case. This results in /usr/share/man/man1 to be transformed into /usr/share/man/man1/foo*
  7. The new list of glob patterns is globbed, obtaining all files which match the pattern
  8. _man_pages then strips any suffixes from the files and adds them to the completion widget list of choices by using compadd

Now, as you can see, the list of manpages is directly determined by $PREFIX variable. In order to make zsh:foo to list only man pages of zsh* which contain the word foo, it needs to be split across : character (if any).

The following addition in _man_pages partially solve the issue (zsh 4.3.4):

Original:

_man_pages() {
  local matcher pages dummy sopt

  zparseopts -E M+:=matcher

  if (( $#matcher )); then
    matcher=( ${matcher:#-M} )
    matcher="$matcher"
  else
    matcher=
  fi

  pages=( ${(M)dirs:#*$sect/} )

  compfiles -p pages '' '' "$matcher" '' dummy '*'
  pages=( ${^~pages}(N:t) )

  (($#mrd)) && pages[$#pages+1]=($(awk $awk $mrd))

  # Remove any compression suffix, then remove the minimum possible string
  # beginning with .<->: that handles problem cases like files called
  # `POSIX.1.5'.

  [[ $OSTYPE = solaris* ]] && sopt='-s '
  if ((CURRENT > 2)) ||
      ! zstyle -t ":completion:${curcontext}:manuals.$sect" insert-sections
  then
    compadd "$@" - ${pages%.(?|<->*(|.gz|.bz2|.Z))}
  else
    compadd "$@" -P "$sopt$sect " - ${pages%.(?|<->*(|.gz|.bz2|.Z))}
  fi
}

Modified (look for ##mod comments):

_man_pages() {
  local matcher pages dummy sopt

  zparseopts -E M+:=matcher

  if (( $#matcher )); then
    matcher=( ${matcher:#-M} )
    matcher="$matcher"
  else
    matcher=
  fi

  pages=( ${(M)dirs:#*$sect/} )

  ##mod
  # split components by the ":" character
  local pref_words manpage_grep orig_prefix
  # save original prefix (just in case)
  orig_prefix=${PREFIX}
  # split $PREFIX by ':' and make it an array
  pref_words=${PREFIX//:/ }
  set -A pref_words ${=pref_words}
  # if there are both manpage name and grep string, use both
  if (( $#pref_words == 2 )); then
      manpage_grep=$pref_words[2]
      # PREFIX is used internally by compfiles
      PREFIX=$pref_words[1]
  elif (( $#pref_words == 1 )) && [[ ${PREFIX[1,1]} == ":" ]]; then
      # otherwise, prefix is empty and only grep string exists
      PREFIX=
      manpage_grep=$pref_words[1]
  fi


  compfiles -p pages '' '' "$matcher" '' dummy '*'
  ##mod: complete, but don't strip path names
  pages=( ${^~pages} )

  (($#mrd)) && pages[$#pages+1]=($(awk $awk $mrd))

  ##mod: grep pages
  # Build a list of matching pages that pass the grep
  local matching_pages
  typeset -a matching_pages

  # manpage_grep exists and not empty 
  if (( ${#manpage_grep} > 0 )); then
      for p in ${pages}; do
          zgrep "${manpage_grep}" $p > /dev/null
          if (( $? == 0 )); then
              #echo "$p matched $manpage_grep"
              matching_pages+=($p)
          fi
      done
  else
  # there's no manpage_grep, so all pages match
      matching_pages=( ${pages} )
  fi

  #echo "\nmatching manpages: "${matching_pages}
  pages=( ${matching_pages}(N:t) )
  # keep the stripped prefix for now
  #PREFIX=${orig_prefix}


  # Remove any compression suffix, then remove the minimum possible string
  # beginning with .<->: that handles problem cases like files called
  # `POSIX.1.5'.


  [[ $OSTYPE = solaris* ]] && sopt='-s '
  if ((CURRENT > 2)) ||
      ! zstyle -t ":completion:${curcontext}:manuals.$sect" insert-sections
  then
    compadd "$@" - ${pages%.(?|<->*(|.gz|.bz2|.Z))}
  else
    compadd "$@" -P "$sopt$sect " - ${pages%.(?|<->*(|.gz|.bz2|.Z))}
  fi
}

However, it's still not fully working (if you uncomment the #echo "$p matched $manpage_grep" line, you can see that it does build the list) - I suspect that somewhere internally, the completion system sees that, for instance, "zshcompctl" is not matched by prefix "zsh:foo", and does not display the resulting matches. I've tried to keep $PREFIX as it is after stripping the grep string, but it still does not want to work.

At any rate, this at least should get you started.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文