zsh-workers
 help / color / mirror / code / Atom feed
From: Clint Adams <clint@zsh.org>
To: zsh-workers@sunsite.dk
Subject: PATCH: _path_commands
Date: Sun, 19 Aug 2007 18:58:21 -0400	[thread overview]
Message-ID: <20070819225821.GA27141@scowler.net> (raw)

This is a slightly-rearranged version of _path_commands from R.Ramkumar,
to be used for completion commands with descriptions.

Index: Completion/Unix/Type/_path_commands
===================================================================
RCS file: Completion/Unix/Type/_path_commands
diff -N Completion/Unix/Type/_path_commands
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Completion/Unix/Type/_path_commands	19 Aug 2007 22:36:51 -0000
@@ -0,0 +1,77 @@
+#autoload
+
+(( $+functions[_path_commands_caching_policy] )) ||
+_path_commands_caching_policy() {
+
+local oldp file
+typeset -a dbfiles
+
+# rebuild if cache is more than a week old
+oldp=( "$1"(Nmw+1) )
+(( $#oldp )) && return 0
+
+dbfiles=(/usr/share/man/index.(bt|db|dir|pag)(N) \
+  /usr/man/index.(bt|db|dir|pag)(N) \
+  /var/cache/man/index.(bt|db|dir|pag)(N) \
+  /var/catman/index.(bt|db|dir|pag)(N) \
+  /usr/share/man/*/whatis(N))
+
+for file in $dbfiles; do
+  [[ $file -nt $1 ]] && return 0
+done
+
+return 1
+}
+
+_path_commands() {
+local need_desc expl ret=1
+
+if zstyle -T ":completion:${curcontext}:" verbose; then
+  local update_policy first
+  if [[ $+_command_descriptions -eq 0 ]]; then
+    first=yes
+    typeset -A -g _command_descriptions
+  fi
+  zstyle -s ":completion:${curcontext}:" cache-policy update_policy
+  [[ -z "$update_policy" ]] && zstyle ":completion:${curcontext}:" \
+    cache-policy _path_commands_caching_policy
+  if ( [[ -n $first ]] || _cache_invalid command-descriptions ) && \
+    ! _retrieve_cache command-descriptions; then
+    local line
+    for line in "${(f)$(_call_program command-descriptions whatis -s 1 -r .\\\*\; whatis -s 6 -r .\\\* 2> /dev/null)}"; do
+      [[ -n ${line:#(#b)([^ ]#) #\([^ ]#\)( #\[[^ ]#\]|)[ -]#(*)} ]] && continue;
+      [[ -z $match[1] || -z $match[3] || -z ${${match[1]}:#*:*} ]] && continue;
+      _command_descriptions[$match[1]]=$match[3]
+    done
+    _store_cache command-descriptions _command_descriptions
+  fi
+
+  (( $#_command_descriptions )) && need_desc=yes
+fi
+
+if [[ -n $need_desc ]]; then
+  typeset -a dcmds descs cmds
+  local desc cmd sep
+  for cmd in ${(@)commands[(I)$PREFIX*]}; do
+    desc=$_command_descriptions[$cmd]
+    if [[ -z $desc ]]; then
+      cmds+=$cmd
+    else
+      dcmds+=$cmd
+      descs+="$cmd:$desc"
+    fi
+  done
+  zstyle -s ":completion:${curcontext}:" list-separator sep || sep=--
+  zformat -a descs " $sep " $descs
+  descs=("${(@r:COLUMNS-1:)descs}")
+  _wanted commands expl 'external command' \
+    compadd "$@" -ld descs -a dcmds && ret=0
+  _wanted commands expl 'external command' compadd "$@" -a cmds && ret=0
+else
+  _wanted commands expl 'external command' compadd "$@" -k commands && ret=0
+fi
+
+return $ret
+}
+
+_path_commands "$@"


                 reply	other threads:[~2007-08-19 22:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070819225821.GA27141@scowler.net \
    --to=clint@zsh.org \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).