about summary refs log tree commit diff
diff options
context:
space:
mode:
authorClint Adams <clint@users.sourceforge.net>2007-08-19 23:04:07 +0000
committerClint Adams <clint@users.sourceforge.net>2007-08-19 23:04:07 +0000
commit4e945291a26ceb293371f258ded95b3ee6ab0715 (patch)
treea4d509428218057c7daee4e1771638f1f69d4457
parent1380ae67c2a700a43df04da5680ef5cbcf9088f6 (diff)
downloadzsh-4e945291a26ceb293371f258ded95b3ee6ab0715.tar.gz
zsh-4e945291a26ceb293371f258ded95b3ee6ab0715.tar.xz
zsh-4e945291a26ceb293371f258ded95b3ee6ab0715.zip
23782: commands with descriptions.
-rw-r--r--ChangeLog3
-rw-r--r--Completion/Unix/Type/_path_commands77
2 files changed, 80 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index be68cc918..c20ad070f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 2007-08-19  Clint Adams  <clint@zsh.org>
 
+	* R.Ramkumar, tweaked: 23782: Completion/Unix/Type/_path_commands:
+	commands with descriptions.
+
 	* 23781: Completion/Debian/Command/_dput: base host completion
 	on output of `dput -H`.
 
diff --git a/Completion/Unix/Type/_path_commands b/Completion/Unix/Type/_path_commands
new file mode 100644
index 000000000..714e2e232
--- /dev/null
+++ b/Completion/Unix/Type/_path_commands
@@ -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 "$@"