]> git.eshelyaron.com Git - emacs.git/commitdiff
New minor mode for persisting ELisp completion frecency data
authorEshel Yaron <me@eshelyaron.com>
Thu, 12 Jun 2025 05:52:41 +0000 (07:52 +0200)
committerEshel Yaron <me@eshelyaron.com>
Thu, 12 Jun 2025 14:28:14 +0000 (16:28 +0200)
lisp/progmodes/elisp-mode.el

index c4ab016303e81b8bd91a6a222bc3b27df703358c..3ce93afcfb551f9fd6c963526ebe277398126369 100644 (file)
@@ -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