From e01a13110dd49373dd338b736a822caf9fbf0221 Mon Sep 17 00:00:00 2001 From: Dmytro Meleshko Date: Mon, 14 Jun 2021 16:36:05 +0300 Subject: [PATCH] [zsh] make the default selection in the manpage finder widget smarter --- zsh/zle.zsh | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/zsh/zle.zsh b/zsh/zle.zsh index 098c40c..5ee805a 100644 --- a/zsh/zle.zsh +++ b/zsh/zle.zsh @@ -152,7 +152,35 @@ # find man page widget {{{ _widget_find_man_page() { local words=("${(@z)BUFFER}") - local cmd_name="${words[1]}" + + local cmd_name arg i is_subcommand + for (( i = 1; i <= ${#words}; i++ )); do + arg="${words[$i]}" + + # Skip flags + if [[ "$arg" == '-'* ]]; then + continue + fi + + # Skip command prefixes + if [[ -z "$is_subcommand" && "$arg" == (noglob|nocorrect|exec|command|builtin|nohup|disown|sudo|time|gtime|prime-run) ]]; then + continue + fi + + if [[ -z "$is_subcommand" ]]; then + cmd_name="${arg}" + else + cmd_name="${cmd_name}-${arg}" + fi + + if [[ -z "$is_subcommand" && "$arg" == (git|hub|npm|apt|docker|pip|perf) ]]; then + is_subcommand=1 + continue + fi + + break + done + zle push-line local manpage="" manpage="$(fzf-search-manpage "$cmd_name")"