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

Fix up the history upon "fg"



Here's a possibly-handy function.  When you use "fg" or "bg" (or one of
the abbreviations, e.g. "%+"), "getjobs" finds the original text of that
command and inserts it into the history, so that e.g., "!!" will execute
that command again rather than executing "fg" again.

If you use several arguments to "fg", e.g. "fg %2 %3 %1", the commands
for all those jobs are collected and concatenated with semicolons.

Arguably this could be implemented in C as an optional behavior of bin_fg,
so the fg command itself could be removed from the history and replaced
with the job text; but I'm not feeling that ambitious today.

This requires at least zsh-3.1.9, I think.  Definitely not 3.0.anything.

    # Call this from the preexec function like so:
    #   preexec() {
    #	  getjobs "${(z)1}"
    #   }
    getjobs () {
	setopt localoptions noshwordsplit noksharrays
	local texts
	case $1 in
	    fg|bg) shift; [[ -n $1 ]] || set -- %% ;;
	    %*) ;;
	    *) return 0 ;;
	esac
	repeat $#
	do
	    # This case statement emulates jobs.c:getjob()
	    case $1 in
		%(|[%+])) 1=${(k)jobstates[(r)*:+:*]} ;;
		%-) 1=${(k)jobstates[(r)*:-:*]} ;;
		%<->) 1=${1#%} ;;
		%[?]*) 1=${${(Ok)jobtexts[(R)*${1#%[?]}*]}[1]} ;;
		*) 1=${${(Ok)jobtexts[(R)$1*]}[1]} ;;
	    esac
	    [[ -n $1 ]] && texts=($texts ${jobtexts[$1]})
	    shift
	done
	# Remove the "-s" below if you'd prefer that this just report
	# what jobs are being affected rather than modify the history
	(( $#texts )) && print -s ${(j:; :)texts}
	return 0
    }

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

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   



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