zsh-users
 help / color / mirror / code / Atom feed
* multiple background jobs of the same program
@ 1997-09-14  3:26 Jose Unpingco
  1997-09-14 17:28 ` Adam R. Paul
  0 siblings, 1 reply; 7+ messages in thread
From: Jose Unpingco @ 1997-09-14  3:26 UTC (permalink / raw)
  To: zsh

Hi,

I often have multiple vi edit sessions going. When I do the completion
for fg (fg -j  -P % +) that shuffles through the list  of jobs, I only
get the name of the program, i.e.  vi, which is not very helpful since
there are multiple vi sessions going. One  way around this is to refer
to the jobs using the job number.

Anybody have  any suggestions? I'd really  like to use  the completion
for fg more productively.

-- 
Thank you for your time and consideration.  
----------------------------------------------------------------------
        Jose Unpingco   Mail Code ECE 0407; WK# (619) 534-5904
----------------------------------------------------------------------


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: multiple background jobs of the same program
  1997-09-14  3:26 multiple background jobs of the same program Jose Unpingco
@ 1997-09-14 17:28 ` Adam R. Paul
  1997-09-14 19:10   ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Adam R. Paul @ 1997-09-14 17:28 UTC (permalink / raw)
  To: zsh

It would appear that on Sat, Sep 13, 1997 at 08:26:46PM -0700, Jose Unpingco wrote:
> Hi,
> 
> I often have multiple vi edit sessions going. When I do the completion
> for fg (fg -j  -P % +) that shuffles through the list  of jobs, I only
> get the name of the program, i.e.  vi, which is not very helpful since
> there are multiple vi sessions going. One  way around this is to refer
> to the jobs using the job number.
> 
> Anybody have  any suggestions? I'd really  like to use  the completion
> for fg more productively.

I've often wanted the same thing - I just cooked up a compctl that _almost_
works:

setopt interactive_comments

listjobs() {
jobs > /tmp/._zshjobcomp.$$ 2>&1
set -A reply
OIFS="$IFS"
IFS='
'
reply=( $(sed -e '{
s/.\(....\).............\(.*\)/\1 # \2/g
s/\]//g
s/  */ /g
}' /tmp/._zshjobcomp.$$) )
IFS="$OIFS"
}

compctl -P % -K listjobs fg

The only problem is that zsh is escaping the spaces & #'s that result from 
the completion, which looks really ugly.  Also, I don't really want 
interactive_comments set all the time - just for this completion, but I 
haven't thought of any way to do this (yet:)

Any improvements?  I suspect that the sed bit could be replaced by some neato
zsh internal parameter mangling stuff, but I confess I am not terribly handy
with most of the zsh-specific parameter expansion/substitution features.

_Adam

-----
Adam R. Paul    - adamp@sco.com 
SCO Engineering - #include <stddisclaimer.h>

Sfhacca.  Opnchoiggotl allthouuthqu ut tzhuarlahqu tn'ogo khozhna oomgga.  


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: multiple background jobs of the same program
  1997-09-14 17:28 ` Adam R. Paul
@ 1997-09-14 19:10   ` Bart Schaefer
  1997-09-14 22:05     ` Adam R. Paul
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 1997-09-14 19:10 UTC (permalink / raw)
  To: Adam R. Paul, zsh

On Sep 14, 10:28am, Adam R. Paul wrote:
} Subject: Re: multiple background jobs of the same program
}
} setopt interactive_comments
} 
} listjobs() {
} jobs > /tmp/._zshjobcomp.$$ 2>&1
} set -A reply
} OIFS="$IFS"
} IFS='
} '
} reply=( $(sed -e '{
} s/.\(....\).............\(.*\)/\1 # \2/g
} s/\]//g
} s/  */ /g
} }' /tmp/._zshjobcomp.$$) )
} IFS="$OIFS"
} }
} 
} compctl -P % -K listjobs fg
} 
} The only problem is that zsh is escaping the spaces & #'s that result

Change it to

	compctl -Q -P % -K listjobs fg

} Also, I don't really want 
} interactive_comments set all the time

Replace the `#' with `;:'.  This works best if you have menu completion
so that the `;' isn't interpreted as a command separator until after you
have a chance to cycle through all the possibilities.

} Any improvements?

Either remove the temp file or use >| so `setopt clobber' isn't needed.

You don't need `set -A reply'.

Don't change IFS, use "${(@f)...}" to interpret each line as a word.

Have listjobs insert the leading % instead of using -P, and use -U to
cause the line so far to be replaced by the completion.   Then when there
are no possible completions (no jobs), nothing gets inserted.  (Your
original inserts "% " when there are no jobs.)

Do the completion for `bg' and `kill', too.

} I suspect that the sed bit could be replaced by some neato
} zsh internal parameter mangling stuff

Since you're doing several substitutions and already reading from a file,
I don't think that's worth it.

Finished product:

listjobs() {
  jobs >| /tmp/._zshjobcomp.$$ 2>&1
  reply=( "${(@f)$(sed -e '{
    s/.\(....\).............\(.*\)/%\1 ;: \2/g
    s/\]//g
    s/  */ /g
    }' /tmp/._zshjobcomp.$$)}" )
}

compctl -U -Q -K listjobs fg bg kill

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: multiple background jobs of the same program
  1997-09-14 19:10   ` Bart Schaefer
@ 1997-09-14 22:05     ` Adam R. Paul
  1997-09-14 23:09       ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Adam R. Paul @ 1997-09-14 22:05 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh

It would appear that on Sun, Sep 14, 1997 at 12:10:42PM -0700, Bart Schaefer wrote:
> On Sep 14, 10:28am, Adam R. Paul wrote:
> } Subject: Re: multiple background jobs of the same program
> }
> } setopt interactive_comments
> } 
> } listjobs() {
> } jobs > /tmp/._zshjobcomp.$$ 2>&1
> } set -A reply
> } OIFS="$IFS"
> } IFS='
> } '
> } reply=( $(sed -e '{
> } s/.\(....\).............\(.*\)/\1 # \2/g
> } s/\]//g
> } s/  */ /g
> } }' /tmp/._zshjobcomp.$$) )
> } IFS="$OIFS"
> } }
> } 
> } compctl -P % -K listjobs fg
> } 
> } The only problem is that zsh is escaping the spaces & #'s that result
> 
> Change it to
> 
> 	compctl -Q -P % -K listjobs fg
> 
> } Also, I don't really want 
> } interactive_comments set all the time
> 
> Replace the `#' with `;:'.  This works best if you have menu completion
> so that the `;' isn't interpreted as a command separator until after you
> have a chance to cycle through all the possibilities.

Hmm, it doesn't appear to work at all unless menu_complete is set :(  
'fg <tab>' yields 'fg %' and no number of tabs causes it to complete anything,
which is unfortunate, as I don't like menu_complete personally - I prefer
to have completion complete as far as is unambiguous, auto-list the completions
and then do menu completion if <tab> is pressed after that.  I think the
options that I use to do this are autolist and list_ambiguous ( but not
menu_complete).
 
> } Any improvements?
> 
> Either remove the temp file or use >| so `setopt clobber' isn't needed.

I'd love to not use a temp file, but it appears that piping 'jobs' output
through anything results in the RHS of the pipe getting nothing whatsoever (ie.
'jobs | cat' produces nothing at all, 'jobs | echo' produces just a newline).
It'd be nice if there was another way to do this - I also have the somewhat
silly:

precmd() {
	jobs > /tmp/.zsh_jobs.$$
	psvar=$(wc -l < /tmp/.zsh_jobs.$$ | tr -d ' ')
}

PS1='%%%1v:%~%#'

to get the number of jobs in my prompt.
 
> You don't need `set -A reply'.

True, dunno why I had that in there :)

> Don't change IFS, use "${(@f)...}" to interpret each line as a word.

Ah, most excellent!  I'll have to tweak a few other compctls I have to
use that (and read up on that construct in the man page:)

> Have listjobs insert the leading % instead of using -P, and use -U to
> cause the line so far to be replaced by the completion.   Then when there
> are no possible completions (no jobs), nothing gets inserted.  (Your
> original inserts "% " when there are no jobs.)
> 
> Do the completion for `bg' and `kill', too.

Naturally.  That was my intention once I got it working the way I wanted
it.

> } I suspect that the sed bit could be replaced by some neato
> } zsh internal parameter mangling stuff
> 
> Since you're doing several substitutions and already reading from a file,
> I don't think that's worth it.
> 
> Finished product:
> 
> listjobs() {
>   jobs >| /tmp/._zshjobcomp.$$ 2>&1
>   reply=( "${(@f)$(sed -e '{
>     s/.\(....\).............\(.*\)/%\1 ;: \2/g
>     s/\]//g
>     s/  */ /g
>     }' /tmp/._zshjobcomp.$$)}" )
> }
> 
> compctl -U -Q -K listjobs fg bg kill

Thanks very much!  Now if I can only get it to work without having 
menu_complete set, I'll be one happier camper ;-)

Thanks,
	_Adam

-----
Adam R. Paul    - adamp@sco.com 
SCO Engineering - from std import disclaimer

Pdro.  Aunagl gyeaoi tuthshaothyi iafhthath iagcha.  


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: multiple background jobs of the same program
  1997-09-14 22:05     ` Adam R. Paul
@ 1997-09-14 23:09       ` Bart Schaefer
  1997-09-15  3:02         ` Adam R. Paul
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 1997-09-14 23:09 UTC (permalink / raw)
  To: Adam R. Paul; +Cc: zsh

On Sep 14,  3:05pm, Adam R. Paul wrote:
} Subject: Re: multiple background jobs of the same program
}
} > Replace the `#' with `;:'.  This works best if you have menu completion
} > so that the `;' isn't interpreted as a command separator until after you
} > have a chance to cycle through all the possibilities.
} 
} Hmm, it doesn't appear to work at all unless menu_complete is set :(  

I use automenu, not menucomplete, and it's OK there ....

However, it works for me with autolist and listambiguous, at least as far
as getting the listing:

zagzig[37] fg <TAB>
%1 ;: vim fo       %2 ;: info -f zsh  %3 ;: vim bar
zagzig[37] fg %

At this point if I hit TAB again I get a beep, and if I supply (say) the
digit 1 and then hit TAB I encounter what must be a completion bug:

zagzig[37] fg %1<TAB>
zagzig[37] fg %^@1
%1 ;: vim fo       %2 ;: info -f zsh  %3 ;: vim bar
zagzig[37] fg %

That ^@ in there is a nul byte that zsh is incorrectly inserting.  It then
gets erased by compctl -U, but it shouldn't be there to begin with.

Anyway, the ;: has nothing to do with this particular problem -- get rid of
the -U if you don't use automenu or menucomplete, and things should be much
better.

I just played around with this a bit and discovered that you don't need -U
at all as long as reply is an empty array (rather than an array containing
the empty string) when there are no jobs -- which you can do by testing
whether "jobs" produced any output before running the "sed".  See below.

} > Either remove the temp file or use >| so `setopt clobber' isn't needed.
} 
} I'd love to not use a temp file, but it appears that piping 'jobs' output

No, I didn't mean don't use the temp file; I meant end the function with
"rm /tmp/._zshjobcomp.$$".

Anyway, to summarize:

listjobs() {
  jobs >| /tmp/._zshjobcomp.$$ 2>&1
  if [[ -s /tmp/._zshjobcomp.$$ ]]
  then
   reply=( "${(@f)$(sed -e '{
    s/.\(....\).............\(.*\)/%\1 ;: \2/g
    s/\]//g
    s/  */ /g
    }' /tmp/._zshjobcomp.$$)}" )
  else
   reply=()
  fi
}

compctl -Q -K listjobs fg bg kill

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: multiple background jobs of the same program
  1997-09-14 23:09       ` Bart Schaefer
@ 1997-09-15  3:02         ` Adam R. Paul
       [not found]           ` <5len6q9692.fsf@tequila.systemsz.cs.yale.edu>
  0 siblings, 1 reply; 7+ messages in thread
From: Adam R. Paul @ 1997-09-15  3:02 UTC (permalink / raw)
  To: zsh

It would appear that on Sun, Sep 14, 1997 at 04:09:10PM -0700, Bart Schaefer wrote:
> On Sep 14,  3:05pm, Adam R. Paul wrote:
> } Subject: Re: multiple background jobs of the same program
> }
> } > Replace the `#' with `;:'.  This works best if you have menu completion
> } > so that the `;' isn't interpreted as a command separator until after you
> } > have a chance to cycle through all the possibilities.
> } 
> } Hmm, it doesn't appear to work at all unless menu_complete is set :(  
> 
> I use automenu, not menucomplete, and it's OK there ....

Hmm, I think I'm using automenu too (its not set explicitly, but it does
show up in my 'unsetopt' completion (which completes on set options), and 
not in my 'setopt' completion (which completes on unset options:) )

> However, it works for me with autolist and listambiguous, at least as far
> as getting the listing:
> 
> zagzig[37] fg <TAB>
> %1 ;: vim fo       %2 ;: info -f zsh  %3 ;: vim bar
> zagzig[37] fg %
> 
> At this point if I hit TAB again I get a beep, and if I supply (say) the
> digit 1 and then hit TAB I encounter what must be a completion bug:
> 
> zagzig[37] fg %1<TAB>
> zagzig[37] fg %^@1
> %1 ;: vim fo       %2 ;: info -f zsh  %3 ;: vim bar
> zagzig[37] fg %
> 
> That ^@ in there is a nul byte that zsh is incorrectly inserting.  It then
> gets erased by compctl -U, but it shouldn't be there to begin with.
> 
> Anyway, the ;: has nothing to do with this particular problem -- get rid of
> the -U if you don't use automenu or menucomplete, and things should be much
> better.
> 
> I just played around with this a bit and discovered that you don't need -U
> at all as long as reply is an empty array (rather than an array containing
> the empty string) when there are no jobs -- which you can do by testing
> whether "jobs" produced any output before running the "sed".  See below.
> 
> } > Either remove the temp file or use >| so `setopt clobber' isn't needed.
> } 
> } I'd love to not use a temp file, but it appears that piping 'jobs' output
> 
> No, I didn't mean don't use the temp file; I meant end the function with
> "rm /tmp/._zshjobcomp.$$".
> 
> Anyway, to summarize:
> 
> listjobs() {
>   jobs >| /tmp/._zshjobcomp.$$ 2>&1
>   if [[ -s /tmp/._zshjobcomp.$$ ]]
>   then
>    reply=( "${(@f)$(sed -e '{
>     s/.\(....\).............\(.*\)/%\1 ;: \2/g
>     s/\]//g
>     s/  */ /g
>     }' /tmp/._zshjobcomp.$$)}" )
>   else
>    reply=()
>   fi
> }
> 
> compctl -Q -K listjobs fg bg kill

Thanks much again!  That's _almost_ perfect :)  The only remaining thing is
that if a job is suspended due to tty output/input, the sed script doesn't
quite do the right thing (the completion looks like:

fg %1 ;: tty output) vi foo

which then gives a parse error if you hit <return> due to the unmatched ')'.
Oh well, no biggie:)


Thanks again, Bart,
	_Adam

-----
Adam R. Paul    - adamp@sco.com 
SCO Engineering - #include <stddisclaimer.h>

Nshiaththao.  Thcolkathsh iont eth n'aathtaghaa kgandu ouaiaaphu thbakggaghn.  


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: multiple background jobs of the same program
       [not found]             ` <5ld8ma95sf.fsf@tequila.systemsz.cs.yale.edu>
@ 1997-09-15 14:56               ` Bart Schaefer
  0 siblings, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 1997-09-15 14:56 UTC (permalink / raw)
  To: zsh-users

On Sep 15,  7:26am, Stefan Monnier wrote:
> Subject: Re: multiple background jobs of the same program
>
> I must be stupid, because a much simpler alternative works just as well if not
> better:

What's $TMP ?  When I first cut'n'pasted this, I got

zsh: permission denied: /zshjobcomp.30447

Here's a better idea:

    complete_listjobs() {
      tmpfile=${TMPPREFIX}jobcomp.$$
      jobs >&| $tmpfile
      if [[ -s $tmpfile ]] then
         reply=("${(@f)$(sed -e 's/^\[\([0-9]*\)\][-+ ]*/%\1 ;: /' $tmpfile)}")
      else
        reply=()
      fi
    }
     
    compctl -Q -K complete_listjobs fg bg kill

If not for the nofunctionargzero option, I'd use $TMPPREFIX$0.$$ for the
temp file name.

> - only list the plainly suspended jobs (and not waiting on tty) for bg
> - aknowledge the fact that kill has more uses than to kill jobs. the jobs
>   completion should only work once "%" is entered. "ls /proc" might be used
>   as well as "kill -l" for other cases.

Too bad both of those would require separate but very similar functions.
Or is there some way to get at the name of the command that caused the
completion to be selected?

(Also too bad that using /proc isn't portable. `ps` output is better, but
only marginally.)

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~1997-09-15 15:04 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-09-14  3:26 multiple background jobs of the same program Jose Unpingco
1997-09-14 17:28 ` Adam R. Paul
1997-09-14 19:10   ` Bart Schaefer
1997-09-14 22:05     ` Adam R. Paul
1997-09-14 23:09       ` Bart Schaefer
1997-09-15  3:02         ` Adam R. Paul
     [not found]           ` <5len6q9692.fsf@tequila.systemsz.cs.yale.edu>
     [not found]             ` <5ld8ma95sf.fsf@tequila.systemsz.cs.yale.edu>
1997-09-15 14:56               ` Bart Schaefer

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).