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

PATCH: make curjob and prevjob work better



In particular, these two sequences were still broken after 54294:

% v() { vim "$@"; }; v
[suspend vim]
% urxvt &
[3] 12707
% bg %-
bg: job already in background
% fg
[1]    continued
[exit vim]
% fg
before:
fg: no current job
after:
[3]  + running    urxvt

% sleep 100
^Z
zsh: suspended  sleep 100
% bg
[1]    continued  sleep 100
% fg
before
fg: no current job
after:
[1]  - running    sleep 100

The changed test expected the sole remaining job to be %- which should
not be possible, and now it is properly %+ instead.
---
 Src/jobs.c        | 33 +++++++++++++++++++--------------
 Test/W02jobs.ztst |  2 +-
 2 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/Src/jobs.c b/Src/jobs.c
index b62e6091fa..84d098efb2 100644
--- a/Src/jobs.c
+++ b/Src/jobs.c
@@ -329,7 +329,10 @@ handle_sub(int job, int fg)
 		deletejob(jn, 1);
 	    }
 	}
-	curjob = jn - jobtab;
+	if (fg)
+	    /* don't change curjob if we got here from a signal handler, the superjob
+	     * might not be the current job at all then */
+	    curjob = jn - jobtab;
     } else if (sj->stat & STAT_STOPPED) {
 	struct process *p;
 
@@ -2643,21 +2646,23 @@ bin_fg(char *name, char **argv, Options ops, int func)
 	    }
 	    /* It's time to shuffle the jobs around!  Reset the current job,
 	    and pick a sensible secondary job. */
-	    {
-		/* Exclude this job from setprevjob() consideration. */
+	    if (func != BIN_BG && curjob == job) {
+		curjob = prevjob;
+		prevjob = job;
+	    }
+	    if (prevjob == job) {
 		int saved_thisjob = thisjob;
-		thisjob = job;
-		if (curjob == job) {
-		    curjob = prevjob;
-		    prevjob = (func == BIN_BG) ? -1 : job;
-		}
-		if (prevjob == job || prevjob == -1)
-		    setprevjob();
-		if (curjob == -1) {
-		    curjob = prevjob;
-		    setprevjob();
-		}
+		if (func != BIN_BG && curjob != -1)
+		    /* Exclude this job from setprevjob() consideration. */
+		    thisjob = job;
+		setprevjob();
 		thisjob = saved_thisjob;
+	    } else if (prevjob == -1) {
+		setprevjob();
+	    }
+	    if (curjob == -1) {
+		curjob = prevjob;
+		setprevjob();
 	    }
 	    if (func != BIN_WAIT)
 		/* for bg and fg -- show the job we are operating on */
diff --git a/Test/W02jobs.ztst b/Test/W02jobs.ztst
index bfbb02f7bd..1faac0c441 100644
--- a/Test/W02jobs.ztst
+++ b/Test/W02jobs.ztst
@@ -328,7 +328,7 @@
 *>\[[0-9]##\] [0-9]##
 *>\[[0-9]##\]  + (stopped|suspended)*sleep*
 *>\[[0-9]##\]    continued*
-*>\[[0-9]##\]  - (stopped|suspended)*sleep*
+*>\[[0-9]##\]  + (stopped|suspended)*sleep*
 
 %clean
 
-- 
2.38.1





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