Subject: bug#66700: 30.0.50; Some history is missing with multiple eshell buffers

From: Liu Hui <[email-masked]>
Date: Thu, 16 Nov 2023 17:47:43 +0800

Jim Porter <[email-masked]> 于2023年11月15日周三 01:15写道：
>
> On 11/14/2023 4:43 AM, Sean Whitton wrote:
> > On Tue 14 Nov 2023 at 02:32pm +02, Eli Zaretskii wrote:
> >>
> >> IMO, this is too soon to change the default behavior in incompatible
> >> ways.  We need to wait for a while, perhaps at least one major
> >> release, before we do that.
> >
> > This is fair enough.  It would be really useful to have the default
> > behaviour now for those of used to it from other shells, though -- Liu,
> > perhaps you would be interested in adding a defcustom?
>
> Yeah, I think a defcustom would be good here. That would also make it
> easier for us to test out the new code and find any bugs.

Thank you all for your suggestions. A defcustom has been added in the
attached patch without changing previous behavior.
From e2cd3a9d28b35806d4e6a83e98f0dc25550651e2 Mon Sep 17 00:00:00 2001
From: Liu Hui <[email-masked]>
Date: Thu, 16 Nov 2023 11:45:28 +0800
Subject: [PATCH] Add option `eshell-history-append'

* lisp/eshell/em-hist.el (eshell-history-append): New option.
(eshell--save-history): New function.
(eshell-hist-initialize):
(eshell-save-some-history): Replace eshell-write-history with
eshell--save-history, which respects the new option.
---
 lisp/eshell/em-hist.el | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/lisp/eshell/em-hist.el b/lisp/eshell/em-hist.el
index cf03f8399a6..79336204847 100644
--- a/lisp/eshell/em-hist.el
+++ b/lisp/eshell/em-hist.el
@@ -116,6 +116,12 @@ eshell-save-history-on-exit
 		 (const :tag "Ask" ask)
 		 (const :tag "Always save" t)))
 
+(defcustom eshell-history-append nil
+  "If non-nil, append new entries to the history file when saving history."
+  :type '(choice (const :tag "Overwrite history file" nil)
+		 (const :tag "Append new entries to file" t))
+  :version "30.1")
+
 (defcustom eshell-input-filter 'eshell-input-filter-default
   "Predicate for filtering additions to input history.
 Takes one argument, the input.  If non-nil, the input may be saved on
@@ -294,17 +300,21 @@ eshell-hist-initialize
     (if eshell-history-file-name
 	(eshell-read-history nil t))
 
-    (add-hook 'eshell-exit-hook #'eshell-write-history nil t))
+    (add-hook 'eshell-exit-hook #'eshell--save-history nil t))
 
   (unless eshell-history-ring
     (setq eshell-history-ring (make-ring eshell-history-size)))
 
-  (add-hook 'eshell-exit-hook #'eshell-write-history nil t)
+  (add-hook 'eshell-exit-hook #'eshell--save-history nil t)
 
   (add-hook 'kill-emacs-query-functions #'eshell-save-some-history)
 
   (add-hook 'eshell-input-filter-functions #'eshell-add-to-history nil t))
 
+(defun eshell--save-history ()
+  "Save the history for current Eshell buffer."
+  (eshell-write-history nil eshell-history-append))
+
 (defun eshell-save-some-history ()
   "Save the history for any open Eshell buffers."
   (dolist (buf (buffer-list))
@@ -318,7 +328,7 @@ eshell-save-some-history
 			(format-message
 			 "Save input history for Eshell buffer `%s'? "
 			 (buffer-name buf)))))
-	      (eshell-write-history)))))
+	      (eshell--save-history)))))
   t)
 
 (defun eshell/history (&rest args)
-- 
2.25.1