From 10ad5518971f4e16df5614a0d2cbac16131998f9 Mon Sep 17 00:00:00 2001 From: Eshel Yaron Date: Thu, 12 Jun 2025 07:52:41 +0200 Subject: [PATCH] New minor mode for persisting ELisp completion frecency data --- lisp/progmodes/elisp-mode.el | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/lisp/progmodes/elisp-mode.el b/lisp/progmodes/elisp-mode.el index c4ab016303e..3ce93afcfb5 100644 --- a/lisp/progmodes/elisp-mode.el +++ b/lisp/progmodes/elisp-mode.el @@ -875,6 +875,39 @@ in `completion-at-point-functions' (which see)." (completion-at-point-function-with-frecency-sorting #'elisp-completion-at-point)) +(defun elisp-save-capf-frecency-cache () + (interactive) + (with-temp-buffer + (let ((print-length nil) + (print-level nil) + (print-quoted t) + (print-circle t)) + (prin1 (capfrecency--cache (symbol-function 'elisp-capf)) (current-buffer))) + (let* ((file-precious-flag t) + (file (locate-user-emacs-file "elisp-capf-frecency-cache.eld")) + (dir (file-name-directory file))) + (unless (file-exists-p dir) (make-directory dir t)) + (write-region (point-min) (point-max) file nil + (unless (called-interactively-p 'interactive) 'quiet)) + (set-file-modes file #o600)))) + +(define-minor-mode elisp-persistent-capf-frecency-cache-mode + "Persist ELisp completion frecency cache." + :global t + :group 'lisp + (if elisp-persistent-capf-frecency-cache-mode + (progn + (add-hook 'kill-emacs-hook #'elisp-save-capf-frecency-cache) + (when-let ((file (locate-user-emacs-file "elisp-capf-frecency-cache.eld")) + ((file-exists-p file))) + (setf (capfrecency--cache (symbol-function 'elisp-capf)) + (with-temp-buffer + (insert-file-contents file) + (goto-char (point-min)) + (read (current-buffer)))))) + (remove-hook 'kill-emacs-hook #'elisp-save-capf-frecency-cache) + (elisp-save-capf-frecency-cache))) + (defun elisp--company-kind (str) (let ((sym (intern-soft str))) (cond -- 2.39.5