Subject: Re: bug#57129: 29.0.50; Improve behavior of conditionals in Eshell

From: Jim Porter <[email-masked]>
Date: Sat, 20 Aug 2022 11:03:27 -0700

On 8/15/2022 11:58 AM, Eli Zaretskii wrote:
>> Cc: [email-masked], [email-masked]
>> From: Jim Porter <[email-masked]>
>> Date: Mon, 15 Aug 2022 11:30:07 -0700
>>
>> The temp files are created by Eshell in lisp/eshell/esh-var.el in the
>> function 'eshell-parse-variable-ref', specifically in the part starting
>> with:
>>
>>     (eq (char-after) ?\<)
> 
> Ah, okay.  It's a (mis)feature of Gnulib's gen_tempname function
> (which is the guts of make-temp-file) in its implementation for
> MS-Windows (and maybe other platforms?): it always begins from the
> same "random" characters in the file name, and only generates other
> random characters if there's already a file by that name.  So if you
> are careful and delete the temporary file each time after usage, and
> never need more than one temporary file at the same time, you will get
> the same name every call.

In addition to the changes to temporary file name generation, I think it 
would be useful for Eshell to kill the temporary buffer too. If you use 
this feature in Eshell a lot, the temporary buffers could pile up, 
consuming memory for no real benefit. (A user who wanted the buffer to 
stick around would probably redirect to a non-temporary file, or even 
just to a buffer.) Attached is a patch to do this.
From 62e4316bad6645ff12ef7587735b9e1c5e74fe21 Mon Sep 17 00:00:00 2001
From: Jim Porter <[email-masked]>
Date: Sat, 20 Aug 2022 10:48:32 -0700
Subject: [PATCH] Kill the buffer associated with a temp file when using
 '$<command>' in Eshell

* lisp/eshell/esh-var.el (eshell-parse-variable-ref): Kill the temp
file's buffer when we're done.
---
 lisp/eshell/esh-var.el | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/lisp/eshell/esh-var.el b/lisp/eshell/esh-var.el
index 2f6614b5d7..a9df172e88 100644
--- a/lisp/eshell/esh-var.el
+++ b/lisp/eshell/esh-var.el
@@ -490,8 +490,11 @@ eshell-parse-variable-ref
                            ;; by `eshell-do-eval', which requires very
                            ;; particular forms in order to work
                            ;; properly.  See bug#54190.
-                           (list (function (lambda ()
-                                   (delete-file ,temp))))))
+                           (list (function
+                                  (lambda ()
+                                    (delete-file ,temp)
+                                    (when-let ((buffer (get-file-buffer ,temp)))
+                                      (kill-buffer buffer)))))))
                    (eshell-apply-indices ,temp indices ,eshell-current-quoted)))
             (goto-char (1+ end)))))))
    ((eq (char-after) ?\()
-- 
2.25.1