keyfreq icon indicating copy to clipboard operation
keyfreq copied to clipboard

Something emptied the keyfreq file

Open meedstrom opened this issue 2 years ago • 1 comments

Unfortunately I cannot reproduce, but I lost the content of my ~/.emacs.keyfreq. It may not even be a keyfreq bug, but I think it's useful to have an issue to discuss this. Keyfreq could be more resilient in this case, or at least warn when it happens, like what I now put in my init files:

(use-package keyfreq
  :config
  (if (and (file-exists-p keyfreq-file)
           (= 0 (doom-file-size) keyfreq-file))
      (warn "File mysteriously blanked: keyfreq-file!")
    (keyfreq-mode)
    (keyfreq-autosave-mode)))

It's fortunate that the package spits out errors and warnings (though nondescriptive) when the file is empty, so I could track down the problem. I recommend keeping it that way, perhaps just making the warnings more descriptive.

If you have an idea how to track down the cause of this, I'm all ears.

meedstrom avatar Sep 24 '21 12:09 meedstrom

I encountered this a while ago and found a simple fix for it of my own. I think from memory if the keyfreq file structure was not valid lisp for some reason it would just clobber it. so I write a little error handler to backup the file and continue and normal, so it will continue recording to a new file they might be mergable later...

(defun keyfreq-table-load (table)
    "Load all values from the `keyfreq-file' and add them in the TABLE.
The table is not reset, so the values are appended to the table."

    ;; Does `keyfreq-file' exist?
    (if (file-exists-p keyfreq-file)
        ;; Load sexp
        (let ((l (with-temp-buffer
                   (insert-file-contents keyfreq-file)
                   (goto-char (point-min))
-                  (read (current-buffer)))))
+                   (condition-case error (read (current-buffer))
+                     ;; If reading fails just backup the file and bail
+                     (t (rename-file keyfreq-file
+                                     (concat keyfreq-file "_backup_"
+                                             (format-time-string  "%Y-%m-%d_%H%M_%S" 
+                                                                  (current-time)))))
+                     )))

              ;; Add the values in the table
              (while (and (listp l) l)
                (if (listp (car l))
                    (unless (keyfreq-match-p (cdr (caar l)))
                      (puthash (caar l) (+ (gethash (caar l) table 0) (cdar l)) table)))
                (setq l (cdr l)))
              ))))

Mallchad avatar Nov 06 '23 21:11 Mallchad