Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: RE: Some problems with menu list.



Andrej Everyone wrote:

> > Question to everyone: should we remove processes-list, i.e. the second
> > call to ps? The problem is: how do we replace it. I think it would be
> > nice if _pids would look at the first line of the output of ps to see
> > which column gives the pids. If it can't find that out, it uses the
> > first numeric column (easier to implement: the first number in each
> > line). For special cases, users should be able to set a style to tell
> > _pids which column to use.
> >
> > Would that be ok for everyone?
> 
> Yes. Looking at header should be quite robust. Position of pid varies from
> system to system, but, hopefully, it is always called PID :-) This would make
> _pids usable out of the box on almost every system.

I could convince myself to change this now.

This makes _pids look at the first line to find out which numbers
should be completed. It doesn't really use `columns', but character-
based matching (which is easier to write and, hopefully, faster and
correct). As a fallback, the first number from each of the second to
last line is completed. I haven't added the style I suggested to give
the number from where to complete. That seemed too silly, on second
thought. Unless someone can come up with a reasonable example where
this is needed.

The patch also documents all this.

Bye
 Sven

Index: Completion/Builtins/_pids
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Builtins/_pids,v
retrieving revision 1.7
diff -u -r1.7 _pids
--- Completion/Builtins/_pids	2000/08/15 08:32:26	1.7
+++ Completion/Builtins/_pids	2000/08/15 09:19:42
@@ -3,7 +3,7 @@
 # If given the `-m <pattern>' option, this tries to complete only pids
 # of processes whose command line match the `<pattern>'.
 
-local out list expl match desc listargs args all nm ret=1
+local out pids index list expl match desc listargs args all nm ret=1
 
 _tags processes || return 1
 
@@ -22,24 +22,26 @@
 
 zstyle -s ":completion:${curcontext}:processes" command args
 
-out="$(_call processes ps 2>/dev/null)"
+out=( "${(@f)$(_call processes ps 2>/dev/null)}" )
+desc="$out[1]"
+out=( "${(@M)out[2,-1]:#${~match}}" )
+
+if [[ "$desc" = (#i)(|*[[:blank:]])pid(|[[:blank:]]*) ]]; then
+  index="${#desc[1,(r)(#i)[[:blank:]]pid]}"
+  pids=( "${(@)${(@M)out#${(l:index::?:)~:-}[^[:blank:]]#}##*[[:blank:]]}" )
+else
+  pids=( "${(@)${(@M)out##[^0-9]#[0-9]#}##*[[:blank:]]}" )
+fi
 
 if zstyle -T ":completion:${curcontext}:processes" verbose; then
-  zstyle -s ":completion:${curcontext}:processes-list" command listargs
-  (( $#listargs )) || listargs=( "$args[@]" )
-  if [[ "$listargs" = "$args" ]]; then
-    list=("${(@Mr:COLUMNS-1:)${(f@)out}[2,-1]:#${~match}}")
-  else
-    list=("${(@Mr:COLUMNS-1:)${(f@)$(_call processes-list ps 2>/dev/null)}[2,-1]:#${~match}}")
-  fi
+  list=( "${(@Mr:COLUMNS-1:)out}" )
   desc=(-ld list)
 else
   desc=()
 fi
 
 _wanted processes expl 'process ID' \
-    compadd "$@" "$desc[@]" "$all[@]" - \
-        ${${${(M)${(f)"${out}"}[2,-1]:#${~match}}## #}%% *} && ret=0
+    compadd "$@" "$desc[@]" "$all[@]" -a pids && ret=0
 
 if [[ -n "$all" ]]; then
   zstyle -s ":completion:${curcontext}:processes" insert-ids out || out=menu
Index: Completion/Builtins/_zstyle
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Builtins/_zstyle,v
retrieving revision 1.17
diff -u -r1.17 _zstyle
--- Completion/Builtins/_zstyle	2000/08/02 13:45:52	1.17
+++ Completion/Builtins/_zstyle	2000/08/15 09:19:42
@@ -103,9 +103,9 @@
 history-words hosts indexes jobs keymaps keysyms local-directories
 libraries limits manuals maps messages modifiers modules my-accounts
 named-directories names nicknames options original other-accounts packages
-parameters path-directories paths pods ports prefixes processes ps regex
-sequences sessions signals strings tags targets types urls users values
-warnings widgets windows zsh-options)
+parameters path-directories paths pods ports prefixes processes
+processes-names ps regex sequences sessions signals strings tags targets
+types urls users values warnings widgets windows zsh-options)
 
 _arguments -C ':context:->contexts' ':style:->styles' '*:argument:->style-arg'
 
Index: Doc/Zsh/compsys.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/compsys.yo,v
retrieving revision 1.92
diff -u -r1.92 compsys.yo
--- Doc/Zsh/compsys.yo	2000/08/10 23:06:42	1.92
+++ Doc/Zsh/compsys.yo	2000/08/15 09:19:44
@@ -744,11 +744,6 @@
 item(tt(processes))(
 for process identifiers
 )
-kindex(processes-list, completion tag)
-item(tt(processes-list))(
-used to look up the tt(command) style when generating the list to
-display for process identifiers
-)
 kindex(processes-names, completion tag)
 item(tt(processes-names))(
 used to look up the tt(command) style when generating the names of
@@ -934,11 +929,14 @@
 command.
 
 As an example, the function generating process IDs as matches uses this
-style with the tt(processes) tag to generate the IDs to complete and when
-the tt(verbose) style is `true', it uses this style with the
-tt(processes-list) tag to generate the strings to display.  When using
-different values for these two tags one should ensure that the process
-IDs appear in the same order in both lists.
+style with the tt(processes) tag to generate the IDs to complete and
+the list of processes to display (if the tt(verbose) style is `true').
+The list produced by the command should look like the output of the
+tt(ps) command.  The first line is not displayed, but is searched for
+the string `tt(PID)' (or `tt(pid)') to find the position of the
+process IDs in the following lines.  If the line does not contain
+`tt(PID)', the first numbers in each of the other lines are taken as the 
+process IDs to complete.
 )
 kindex(completer, completion style)
 item(tt(completer))(

--
Sven Wischnowsky                         wischnow@xxxxxxxxxxxxxxxxxxxxxxx



Messages sorted by: Reverse Date, Date, Thread, Author