Compare commits

...

2 Commits

Author SHA1 Message Date
TEC d405304d86
Fix too-small abbrev obarray bug with autocorrect
I'm seeing a bug here, but I'm surprised. Surely more people than just
me have run into abbrev tables being cut short because it grew past the
default obarray size?
2024-03-27 01:05:14 +08:00
TEC 44e3a693da
Avoid failing to entirely overwrite abbrev file
This can happen when the current buffer is smaller than the existing
content when a numerical APPEND argument to write-region.
2024-03-27 01:03:49 +08:00
1 changed files with 16 additions and 5 deletions

View File

@ -4279,12 +4279,21 @@ too. We could just not save it, but it seems nice to get the count information.
(defvar autocorrect-abbrev-table--saved-version 0
"The version of `autocorrect-abbrev-table' saved to disk.")
(declare-function math-next-small-prime "calc-comb")
(defun autocorrect--setup-abbrevs ()
"Setup `autocorrect-abbrev-table'.
Also set it as a parent of `global-abbrev-table'."
(unless autocorrect-abbrev-table
(setq autocorrect-abbrev-table
(make-abbrev-table (list :enable-function #'autocorrect--appropriate-p)))
(let ((obarray-default-size
(if (>= emacs-major-version 30)
4 ; In Emacs 30+ obarrays will dynamically grow.
(require 'calc-comb)
(math-next-small-prime
(+ (hash-table-size autocorrect-record-table) 50)))))
(setq autocorrect-abbrev-table
(make-abbrev-table
(list :enable-function #'autocorrect--appropriate-p))))
(abbrev-table-put
global-abbrev-table :parents
(cons autocorrect-abbrev-table
@ -4310,7 +4319,7 @@ Also set it as a parent of `global-abbrev-table'."
(setq coding-system-for-write 'utf-8-emacs))
(goto-char (point-min))
(insert (format ";;-*-coding: %s;-*-\n\n" coding-system-for-write))
(write-region nil nil autocorrect-abbrev-file 0)))
(write-region nil nil autocorrect-abbrev-file)))
(setq autocorrect-abbrev-table--saved-version
(abbrev-table-get autocorrect-abbrev-table
:abbrev-table-modiff))))
@ -4409,12 +4418,14 @@ split the actual reading and the abbrev generation into two parts though.
"Ensure that all entries of the abbrev table are valid."
(obarray-map
(lambda (misspelling)
(when (stringp misspelling) ; Abbrev's obarrays start with a symbol
(setq misspelling (symbol-name misspelling))
(unless (string-empty-p misspelling) ; Abbrev uses an empty symbol for metadata.
(let ((corrections (gethash misspelling autocorrect-record-table)))
(unless (and (= (length corrections) 1)
(>= (cdar corrections)
autocorrect-count-threshold-history))
(define-abbrev autocorrect-abbrev-table misspelling nil)))))
(define-abbrev autocorrect-abbrev-table misspelling nil)
(unintern misspelling autocorrect-abbrev-table)))))
autocorrect-abbrev-table))
(defun autocorrect--create-history-abbrevs ()