Subject: Re: bug#67000: 30.0.50; [PATCH] Add support for reading/writing IELM input history

From: Simen Heggestøyl <[email-masked]>
Date: Fri, 19 Jan 2024 08:48:51 +0100

Stefan Kangas <[email-masked]> writes:

> I'm not against adding a hook to `kill-emacs-hook', but it should be
> added when you start IELM and not on top-level.  We want to avoid any
> side effects from merely loading a file.

Fixed.

> I don't think this will work in its current form either: AFAICT,
> `comint-write-input-ring' saves it for the current buffer, and that's
> not guaranteed to be the *ielm* buffer when `kill-emacs-hook' runs.
>
> So you'd want to first switch to that buffer, I think.  It could also
> have been renamed meanwhile, so I think you want to save the actual
> buffer and not it's name (i.e. use `get-buffer').

Right, good catch. New attempt in the attached patch!
>From 738801b3126e78cf893d58e47fa156aeeec84cfc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simen=20Heggest=C3=B8yl?= <[email-masked]>
Date: Tue, 16 Jan 2024 08:21:41 +0100
Subject: [PATCH] Add support for reading/writing IELM input history

* lisp/ielm.el (inferior-emacs-lisp-mode): Add support for
reading/writing input history.
(ielm--history-file-name): Name of the file to read/write IELM input
history.
(ielm--exit): Holds a function to call when Emacs is killed to write
out the input history.
(ielm--input-history-writer): Helper function for writing the IELM
input history out to file.
---
 etc/NEWS     | 11 +++++++++++
 lisp/ielm.el | 29 +++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index 735a05f6579..b87ba93f456 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1254,6 +1254,17 @@ chat buffers use by default.
 *** New command 'customize-dirlocals'.
 This command pops up a buffer to edit the settings in ".dir-locals.el".
 
+** IELM
+
+---
+*** IELM now remembers input history between sessions.
+
+---
+*** New variable 'ielm-history-file-name'.
+If non-nil, name of the file to read/write IELM input history.  Set to
+nil to revert IELM to the old behavior of not remembering input
+history between sessions.
+
 
 * New Modes and Packages in Emacs 30.1
 
diff --git a/lisp/ielm.el b/lisp/ielm.el
index 777aebb70cf..54da62c4359 100644
--- a/lisp/ielm.el
+++ b/lisp/ielm.el
@@ -110,6 +110,13 @@ ielm-dynamic-multiline-inputs
 such as `edebug-defun' to work with such inputs."
   :type 'boolean)
 
+(defcustom ielm-history-file-name
+  (locate-user-emacs-file "ielm-history.eld")
+  "If non-nil, name of the file to read/write IELM input history."
+  :type '(choice (const :tag "nil" nil)
+                 file)
+  :version "30.1")
+
 (defvaralias 'inferior-emacs-lisp-mode-hook 'ielm-mode-hook)
 (defcustom ielm-mode-hook nil
   "Hooks to be run when IELM (`inferior-emacs-lisp-mode') is started."
@@ -503,6 +510,17 @@ ielm--expand-ellipsis
     (funcall pp-default-function beg end)
     end))
 
+;;; Input history
+
+(defvar ielm--exit
+  "Function to call when Emacs is killed.")
+
+(defun ielm--input-history-writer (buf)
+  "Return a function writing IELM input history to BUF."
+  (lambda ()
+    (with-current-buffer buf
+      (comint-write-input-ring))))
+
 ;;; Major mode
 
 (define-derived-mode inferior-emacs-lisp-mode comint-mode "IELM"
@@ -605,6 +623,17 @@ inferior-emacs-lisp-mode
             #'ielm-indirect-setup-hook 'append t)
   (setq comint-indirect-setup-function #'emacs-lisp-mode)
 
+  ;; Input history
+  (setq-local comint-input-ring-file-name ielm-history-file-name)
+  (setq-local ielm--exit (ielm--input-history-writer (current-buffer)))
+  (setq-local kill-buffer-hook
+              (lambda ()
+                (funcall ielm--exit)
+                (remove-hook 'kill-emacs-hook ielm--exit)))
+  (unless noninteractive
+    (add-hook 'kill-emacs-hook ielm--exit))
+  (comint-read-input-ring t)
+
   ;; A dummy process to keep comint happy. It will never get any input
   (unless (comint-check-proc (current-buffer))
     ;; Was cat, but on non-Unix platforms that might not exist, so
-- 
2.39.2

-- Simen