dot/lisp-init.lisp: Get CLisp to save and restore its REPL history.
authorMark Wooding <mdw@distorted.org.uk>
Wed, 1 Jul 2020 18:38:17 +0000 (19:38 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 9 Aug 2020 03:34:42 +0000 (04:34 +0100)
This is much more painful than it ought to be, but it seems to work.

dot/lisp-init.lisp

index 3cdcd5e..da50a6f 100644 (file)
 #+clisp
 (setf custom:*parse-namestring-ansi* t)
 
+;; CLisp history.
+#+(and clisp readline)
+(progn
+  (export '(*history-file* *history-size*))
+  (defvar *history-file* (format nil "~A/.clisp-history" (ext:getenv "HOME"))
+    "File to preserve the REPL history.")
+  (defvar *history-size* 1000)
+  (unless (and (probe-file *history-file*) nil)
+    (let (old-umask stream)
+      ;; Ugh.  There's no proper open(2) veneer.  Play with umask(2) to avoid
+      ;; a window in which an adversary can open the file.
+      (unwind-protect
+          (setf old-umask (os:umask #o077)
+                stream (open *history-file*
+                             :direction :output
+                             :if-exists :overwrite
+                             :if-does-not-exist :create))
+       (when stream (close stream))
+       (when old-umask (os:umask old-umask)))))
+  (readline:read-history *history-file*)
+  (if *history-size* (readline:stifle-history *history-size*)
+      (readline:unstifle-history))
+  (push (lambda () (readline:write-history *history-file*))
+       custom:*fini-hooks*))
+
 ;; Shebang.
 (set-dispatch-macro-character
  #\# #\!