;;; my-ielm-history.el -- to save ielm history using a hook. (posted, ;;; madhu 20241013) ;; comint-input-ring-separator and ielm-write-input-wring taken from ;; sly (defun my-ielm-read-input-ring () (let ((comint-input-ring-separator "####\n")) (comint-read-input-ring))) (defun my-ielm-process-sentinel (proc msg) (let ((buffer (process-buffer (get-process proc)))) (when (buffer-live-p buffer) (with-current-buffer buffer (my-ielm-write-input-ring)))) (internal-default-process-sentinel proc msg)) (defun my-ielm-write-input-ring () ;; not enough to make it buffer-local. The binding has to exist when ;; the rings is being written to " *Temp Input History*" (let* ((comint-input-ring-separator "####\n") ;; To merge the file's history with the current buffer's ;; history, sntart by deep-copying `comint-input-ring' to a ;; separate variable. (current-ring (copy-tree comint-input-ring 'vectors-too)) (histcontrol 'erasedups) (index (ring-length current-ring)) ) ;; this sets `comint-input-ring' from the file (my-ielm-read-input-ring) ;; loop `current-ring', which potentially contains new entries and ;; re-add entries to `comint-input-ring', which is now synched ;; with the file and will be written to disk. (cl-loop for i from (1- index) downto 0 for item = (ring-ref current-ring i) for existing-index = (ring-member comint-input-ring item) do (cond ((and existing-index (eq histcontrol 'erasedups)) (ring-remove comint-input-ring existing-index) (ring-insert comint-input-ring item)) ((and existing-index (not histcontrol)) (ring-insert comint-input-ring item)) (t (ring-insert comint-input-ring item))) unless (ring-member comint-input-ring item) do (ring-insert comint-input-ring item)) ;; Now save `comint-input-ring' (let ((coding-system-for-write 'utf-8-unix)) (comint-write-input-ring)))) ;; madhu 060520 (add-hook 'ielm-mode-hook (defun my-ielm-mode-hook () (set-process-sentinel (get-buffer-process (current-buffer)) 'my-ielm-process-sentinel) ;;(make-variable-buffer-local 'comint-input-ring-separator) (setq comint-input-ignoredups t comint-input-ring-file-name ;;comint-input-ring-separator "####\n" ;madhu 200131 (expand-file-name "~/.ielm-history.el") comint-input-ring-size 1000) ;;(make-local-hook 'kill-buffer-hook) (add-hook 'kill-buffer-hook 'my-ielm-write-input-ring nil t))) (defun fix-ielm-buffer () "put #### markers between sexps in a ielm history buffer without those" (while (not (eobp)) (forward-sexp) (assert (eolp)) (cond((eolp) (insert "####") (forward-line 1)) (t (debug)))))