From 468dd225051e60028c3ef3619a7efef8b7a967bd Mon Sep 17 00:00:00 2001 From: zimoun Date: Mon, 16 May 2016 06:24:04 +0200 Subject: [PATCH 1/4] add keyfreq-merge feature (proposal to fix #12) --- keyfreq.el | 65 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/keyfreq.el b/keyfreq.el index 536919c..649d6c1 100644 --- a/keyfreq.el +++ b/keyfreq.el @@ -452,14 +452,24 @@ is used as MAJOR-MODE-SYMBOL argument." (not (file-exists-p keyfreq-file-lock))) -(defun keyfreq-table-save (table &optional mustsave) +(defun keyfreq-table-save (table &optional mustsave keyfreq-file-local) "Append all values from the specified TABLE into the `keyfreq-file' as a sexp of an alist. Then resets the TABLE if it was successfully merged. If MUSTSAVE is t, this function tries to save the table until it gets the lock and successfully saves it. If MUSTSAVE is nil, it -does nothing if the table cannot be saved." +does nothing if the table cannot be saved. + +If KEYFREQ-FILE-LOCAL is nil, then the default value is `keyfreq-file'. +Else it uses this value of file to save TABLE, not considering the stats already store." + + ;; default argument + (setq load-previous-stats nil) + (if (not keyfreq-file-local) + (progn + (setq keyfreq-file-local keyfreq-file) + (setq load-previous-stats t))) ;; Avoid adding nothing to the file (if (> (hash-table-count table) 0) @@ -475,10 +485,11 @@ does nothing if the table cannot be saved." (unwind-protect (progn ;; Load values and merge them with the current keyfreq-table - (keyfreq-table-load table) + (if load-previous-stats + (keyfreq-table-load table)) ;; Write the new frequencies - (with-temp-file keyfreq-file + (with-temp-file keyfreq-file-local (let ((l (cdr (keyfreq-list table 'no-sort)))) (insert "(") (dolist (item l) @@ -506,15 +517,21 @@ does nothing if the table cannot be saved." )))) -(defun keyfreq-table-load (table) +(defun keyfreq-table-load (table &optional keyfreq-file-local) "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." +The table is not reset, so the values are appended to the table. + +If KEYFREQ-FILE-LOCAL is nil, then the default value is `keyfreq-file'. +Else it uses this value of file to load TABLE." + + ;; default argument + (unless (not keyfreq-file-local) (setq keyfreq-file-local keyfreq-file)) ;; Does `keyfreq-file' exist? - (if (file-exists-p keyfreq-file) + (if (file-exists-p keyfreq-file-local) ;; Load sexp (let ((l (with-temp-buffer - (insert-file-contents keyfreq-file) + (insert-file-contents keyfreq-file-local) (goto-char (point-min)) (read (current-buffer))))) @@ -581,6 +598,38 @@ value will take effect only after (re)enabling (keyfreq-mustsave--do) (message "keyfreq data saved into %s" keyfreq-file)) +(defun keyfreq-merge--do (file-A file-B file-C) + "Function called by `keyfreq-merge'" + (let ((table (copy-hash-table keyfreq-table))) + (keyfreq-table-load table file-A) + (keyfreq-table-load table file-B) + (keyfreq-table-save table t file-C))) + +(defun keyfreq-merge () + "Merge two `keyfreq-file'. + +Means read two `keyfreq-file', append them, re-evaluate the stats, and finally save the result. +" + (interactive) + (let ((file-A) + (file-B) + (file-C)) + (setq file-A (read-file-name + (format "File-A to merge (default %s) : " keyfreq-file) + default-directory + keyfreq-file)) + (setq file-B (read-file-name + (format "File-B to merge : ") + default-directory + keyfreq-file)) + (setq file-C (read-file-name + (format "File-C to save (default %s) : " keyfreq-file) + default-directory + keyfreq-file)) + (keyfreq-merge--do file-A file-B file-C) + (message (format "keyfreq statistics merged: from %s and %s into %s" file-A file-B file-C)))) + + (provide 'keyfreq) ;;; keyfreq.el ends here From ba68a1774a7cba77dd81b5b93dd8a21d16bbb7d6 Mon Sep 17 00:00:00 2001 From: zimoun Date: Mon, 16 May 2016 06:28:56 +0200 Subject: [PATCH 2/4] default values at ~/.emacs.d/keyfreq{.lock} (fix #7) --- keyfreq.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/keyfreq.el b/keyfreq.el index 649d6c1..b3cc427 100644 --- a/keyfreq.el +++ b/keyfreq.el @@ -111,14 +111,14 @@ various keyfreq-* functions." :type 'string) -(defcustom keyfreq-file "~/.emacs.keyfreq" +(defcustom keyfreq-file "~/.emacs.d/keyfreq" "File `keyfreq-table' is saved to/loaded from by `keyfreq-table-save' and `keyfreq-table-load' functions by default." :group 'keyfreq :type 'file) -(defcustom keyfreq-file-lock "~/.emacs.keyfreq.lock" +(defcustom keyfreq-file-lock "~/.emacs.d/keyfreq.lock" "Lock file to update the `keyfreq-file'." :group 'keyfreq :type 'file) From 823e1f074a65c07ca64b069b4952d5c80238a902 Mon Sep 17 00:00:00 2001 From: zimoun Date: Mon, 16 May 2016 15:14:10 -0400 Subject: [PATCH 3/4] protect load-previous-stats by adding let --- keyfreq.el | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/keyfreq.el b/keyfreq.el index b3cc427..31f2504 100644 --- a/keyfreq.el +++ b/keyfreq.el @@ -464,6 +464,7 @@ does nothing if the table cannot be saved. If KEYFREQ-FILE-LOCAL is nil, then the default value is `keyfreq-file'. Else it uses this value of file to save TABLE, not considering the stats already store." + (let (load-previous-stats) ;; default argument (setq load-previous-stats nil) (if (not keyfreq-file-local) @@ -514,7 +515,7 @@ Else it uses this value of file to save TABLE, not considering the stats already ;; the 'done' flag to break the while-loop. (setq done t)) - )))) + ))))) (defun keyfreq-table-load (table &optional keyfreq-file-local) @@ -525,7 +526,7 @@ If KEYFREQ-FILE-LOCAL is nil, then the default value is `keyfreq-file'. Else it uses this value of file to load TABLE." ;; default argument - (unless (not keyfreq-file-local) (setq keyfreq-file-local keyfreq-file)) + (unless keyfreq-file-local (setq keyfreq-file-local keyfreq-file)) ;; Does `keyfreq-file' exist? (if (file-exists-p keyfreq-file-local) From 14323101072a8be3d8a2e027570e42c371926421 Mon Sep 17 00:00:00 2001 From: zimoun Date: Mon, 16 May 2016 15:41:14 -0400 Subject: [PATCH 4/4] fix make-hash instead of copy-hash --- keyfreq.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keyfreq.el b/keyfreq.el index 31f2504..309547a 100644 --- a/keyfreq.el +++ b/keyfreq.el @@ -601,7 +601,7 @@ value will take effect only after (re)enabling (defun keyfreq-merge--do (file-A file-B file-C) "Function called by `keyfreq-merge'" - (let ((table (copy-hash-table keyfreq-table))) + (let ((table (make-hash-table :test 'equal :size 128))) (keyfreq-table-load table file-A) (keyfreq-table-load table file-B) (keyfreq-table-save table t file-C)))