Subject: Re: Aborted command saved in history
From: Bart Schaefer <[email-masked]>
To: zsh workers <[email-masked]>
Date: Wed, 27 Jan 2016 21:45:10 -0800
List-Id: Zsh Workers List <zsh-workers.zsh.org>

On Jan 28,  2:06am, Mikael Magnusson wrote:
}
} If i type
}  {<enter>
} and press ctrl-c, the " {" is saved in my history file without
} consulting zshaddhistory()

zshaddhistory is consulted, it just immediately returns without doing
anything because the error condition from the keyboard interrupt is
still persisting.

I'm a little reluctant to push this down into callhookfunc() because
it may be the right thing in other contexts that an error condition
prevents hooks from running?  Although if that's true we could save
a lot of no-op doshfunc() calls by testing errflag in callhookfunc().

Moved hookargs to the closer scope because why not, and so that the
newlinklist() happens inside queue_signals().

diff --git a/Src/hist.c b/Src/hist.c
index 007366a..7f9e4db 100644
--- a/Src/hist.c
+++ b/Src/hist.c
@@ -1378,7 +1378,6 @@ should_ignore_line(Eprog prog)
 mod_export int
 hend(Eprog prog)
 {
-    LinkList hookargs = newlinklist();
     int flag, hookret, stack_pos = histsave_stack_pos;
     /*
      * save:
@@ -1418,9 +1417,17 @@ hend(Eprog prog)
 	DPUTS(hptr < chline, "History end pointer off start of line");
 	*hptr = '\0';
     }
-    addlinknode(hookargs, "zshaddhistory");
-    addlinknode(hookargs, chline);
-    callhookfunc("zshaddhistory", hookargs, 1, &hookret);
+    {
+	LinkList hookargs = newlinklist();
+	int save_errflag = errflag;
+	errflag = 0;
+
+	addlinknode(hookargs, "zshaddhistory");
+	addlinknode(hookargs, chline);
+	callhookfunc("zshaddhistory", hookargs, 1, &hookret);
+
+	errflag |= save_errflag;
+    }
     /* For history sharing, lock history file once for both read and write */
     hf = getsparam("HISTFILE");
     if (isset(SHAREHISTORY) && !lockhistfile(hf, 0)) {