[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/spell-fu d0700095ea 28/86: Add header to cache loading & e
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/spell-fu d0700095ea 28/86: Add header to cache loading & error checking for cache content |
Date: |
Thu, 7 Jul 2022 12:03:40 -0400 (EDT) |
branch: elpa/spell-fu
commit d0700095ea833cdfb855f0d16003c83412fbb758
Author: Campbell Barton <ideasman42@gmail.com>
Commit: Campbell Barton <ideasman42@gmail.com>
Add header to cache loading & error checking for cache content
Any errors reading the cache are reported and a new cache is generate.
---
spell-fu.el | 96 +++++++++++++++++++++++++++++++++++++++++++++----------------
1 file changed, 71 insertions(+), 25 deletions(-)
diff --git a/spell-fu.el b/spell-fu.el
index f083863c56..73e2925438 100644
--- a/spell-fu.el
+++ b/spell-fu.el
@@ -116,6 +116,11 @@ Notes:
;; ---------------------------------------------------------------------------
;; Internal Variables
+
+;; Use to ensure the cache is not from a previous release.
+;; Only ever increase.
+(defconst spell-fu--cache-version "0.1")
+
;; List of language, dictionary mappings.
(defvar spell-fu--cache-table-alist nil)
@@ -133,7 +138,7 @@ Notes:
(defun spell-fu--cache-file (dict)
"Return the location of the cache file with dictionary DICT."
- (expand-file-name (format "words_%s.el" dict) spell-fu-directory))
+ (expand-file-name (format "words_%s.el.data" dict) spell-fu-directory))
(defun spell-fu--words-file (dict)
"Return the location of the word-list with dictionary DICT."
@@ -315,43 +320,81 @@ Argument WORDS-FILE the file to write the word list into."
;; ---------------------------------------------------------------------------
;; Word List Cache
-(defun spell-fu--cache-from-word-list (words-file cache-file)
+(defun spell-fu--cache-from-word-list-impl (words-file cache-file)
"Create CACHE-FILE from WORDS-FILE.
The resulting cache is returned as a minor optimization for first-time loading,
where we need to create this data in order to write it,
save some time by not spending time reading it back."
+ (message "%S" (file-name-nondirectory cache-file))
+ (let
+ ( ;; The header, an associative list of items.
+ (cache-header (list (cons "version" spell-fu--cache-version)))
+ (word-table nil))
- (let ((word-table nil))
-
- (spell-fu--with-message-prefix "Spell-fu generating cache: "
- (message "%S" (file-name-nondirectory cache-file))
-
- (with-temp-buffer
- (insert-file-contents-literally words-file)
- (setq word-table
- (make-hash-table :test #'equal :size (count-lines (point-min)
(point-max))))
- (while (not (eobp))
- (let ((l (buffer-substring-no-properties (line-beginning-position)
(line-end-position))))
- ;; Value of 't' is just for simplicity, it's no used except for
check the item exists.
- (puthash (downcase l) t word-table)
- (forward-line 1))))
+ (with-temp-buffer
+ (insert-file-contents-literally words-file)
+ (setq word-table (make-hash-table :test #'equal :size (count-lines
(point-min) (point-max))))
+ (while (not (eobp))
+ (let ((l (buffer-substring-no-properties (line-beginning-position)
(line-end-position))))
+ ;; Value of 't' is just for simplicity, it's no used except for
check the item exists.
+ (puthash (downcase l) t word-table)
+ (forward-line 1))))
- ;; Write write it to a file.
- (with-temp-buffer
- (prin1 word-table (current-buffer))
- (write-region nil nil cache-file nil 0)))
+ ;; Write write it to a file.
+ (with-temp-buffer
+ (prin1 cache-header (current-buffer))
+ (prin1 word-table (current-buffer))
+ (write-region nil nil cache-file nil 0))
;; Return the resulting word table.
word-table))
-(defun spell-fu--cache-words-load (cache-file)
- "Return the Lisp content from reading CACHE-FILE."
+(defun spell-fu--cache-from-word-list (words-file cache-file)
+ "Create CACHE-FILE from WORDS-FILE."
+ (spell-fu--with-message-prefix "Spell-fu generating cache: "
+ (condition-case err
+ (spell-fu--cache-from-word-list-impl words-file cache-file)
+ (error
+ ;; Should be rare: if the file is corrupt or cannot be read for any
reason.
+ (progn
+ (message "failed, %s" (error-message-string err))
+ nil)))))
+
+
+(defun spell-fu--cache-words-load-impl (cache-file)
+ "Return the Lisp content from reading CACHE-FILE.
+
+On failure of any kind, return nil, the caller will need to regenerate the
cache."
(with-temp-buffer
(insert-file-contents-literally cache-file)
(goto-char (point-min))
- (read (current-buffer))))
+ ;; Check header.
+ (let ((cache-header (read (current-buffer))))
+ (unless (listp cache-header)
+ (error "Expected cache-header to be list, not %S" (type-of
cache-header)))
+
+ (let ((version (assoc-default "version" cache-header)))
+ (unless (string-equal version spell-fu--cache-version)
+ (error "Require cache version %S, not %S" spell-fu--cache-version
version))))
+
+ ;; Read the contents.
+ (let ((word-table (read (current-buffer))))
+ (unless (hash-table-p word-table)
+ (error "Expected cache to contain a hash-table, not %S" (type-of
word-table)))
+ word-table)))
+
+(defun spell-fu--cache-words-load (cache-file)
+ "Return the Lisp content from reading CACHE-FILE."
+ (spell-fu--with-message-prefix "Spell-fu reading cache: "
+ (condition-case err
+ (spell-fu--cache-words-load-impl cache-file)
+ (error
+ ;; Should be rare: if the file is corrupt or cannot be read for any
reason.
+ (progn
+ (message "failed, %s" (error-message-string err))
+ nil)))))
;; ---------------------------------------------------------------------------
;; Word List Initialization
@@ -382,8 +425,11 @@ save some time by not spending time reading it back."
;; Load cache or create it, creating it returns the cache
;; to avoid some slow-down on first load.
(setq spell-fu--cache-table
- (if (and (file-exists-p cache-file) (not (spell-fu--file-is-older
cache-file words-file)))
- (spell-fu--cache-words-load cache-file)
+ (or
+ (and
+ (file-exists-p cache-file)
+ (not (spell-fu--file-is-older cache-file words-file))
+ (spell-fu--cache-words-load cache-file))
(spell-fu--cache-from-word-list words-file cache-file)))
;; Add to to `spell-fu--cache-table-alist' for reuse on next load.
- [nongnu] elpa/spell-fu 3f4fc34204 32/86: Add 'spell-fu-buffer' utility, (continued)
- [nongnu] elpa/spell-fu 3f4fc34204 32/86: Add 'spell-fu-buffer' utility, ELPA Syncer, 2022/07/07
- [nongnu] elpa/spell-fu ddad489f2c 43/86: Adds affix dict support via aspell expand cmd, ELPA Syncer, 2022/07/07
- [nongnu] elpa/spell-fu ce64f4bc4d 51/86: Cleanup: remove arguments that are always fixed, ELPA Syncer, 2022/07/07
- [nongnu] elpa/spell-fu 1b765f8029 58/86: Cleanup: replace setq with let binding, ELPA Syncer, 2022/07/07
- [nongnu] elpa/spell-fu 1159eeec13 63/86: Fix including trailing single-quote in the word, ELPA Syncer, 2022/07/07
- [nongnu] elpa/spell-fu 32fcbd9e8e 68/86: Initial changes for multiple dictionaries, ELPA Syncer, 2022/07/07
- [nongnu] elpa/spell-fu 1f3e5b8f05 72/86: readme: Fix typos, ELPA Syncer, 2022/07/07
- [nongnu] elpa/spell-fu 2f2fd6de00 83/86: Change URL to codeberg, ELPA Syncer, 2022/07/07
- [nongnu] elpa/spell-fu 00b75e5741 25/86: Cleanup: docstring, ELPA Syncer, 2022/07/07
- [nongnu] elpa/spell-fu 1bc18879b6 27/86: Add license file, ELPA Syncer, 2022/07/07
- [nongnu] elpa/spell-fu d0700095ea 28/86: Add header to cache loading & error checking for cache content,
ELPA Syncer <=
- [nongnu] elpa/spell-fu f6c849f860 30/86: Fix disabling spell-fu when used without a timer, ELPA Syncer, 2022/07/07
- [nongnu] elpa/spell-fu 502d523b46 31/86: Fix unintended modification of the standard syntax table, ELPA Syncer, 2022/07/07
- [nongnu] elpa/spell-fu e62cfc4f08 34/86: Add support for personal dictionary management, ELPA Syncer, 2022/07/07
- [nongnu] elpa/spell-fu 86d46cf63e 37/86: fix: encode word to utf8, ELPA Syncer, 2022/07/07
- [nongnu] elpa/spell-fu 9180a77865 48/86: Fix #15: don't enable (when used globally) for application buffers, ELPA Syncer, 2022/07/07
- [nongnu] elpa/spell-fu 41a133d119 52/86: Fix #16: Fails to spellcheck when whitespace-mode splits a word, ELPA Syncer, 2022/07/07
- [nongnu] elpa/spell-fu 7c2542e109 73/86: Merge branch 'multiple-dictionaries' into 'master', ELPA Syncer, 2022/07/07
- [nongnu] elpa/spell-fu 50be652a6e 77/86: Fix overlay faces masking other faces (when checking faces-at-point), ELPA Syncer, 2022/07/07
- [nongnu] elpa/spell-fu cbe5cd84c3 86/86: Correct URL, ELPA Syncer, 2022/07/07
- [nongnu] elpa/spell-fu 41a7ebcbab 23/86: Cleanup: replace cl-letf with advice macro, ELPA Syncer, 2022/07/07