[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [ELPA] New package: Auto Correct Mode
From: |
Stefan Monnier |
Subject: |
Re: [ELPA] New package: Auto Correct Mode |
Date: |
Mon, 04 Sep 2017 18:17:59 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) |
> (if auto-correct-mode
> (run-hooks 'auto-correct-activate-functions)
> (run-hooks 'auto-correct-deactivate-functions)))
BTW, these are regular hooks (i.e. called with no arguments and only
for their side-effects), so their name should preferably end in
"-hook" than in "-functions".
Also, note that `auto-correct-mode` already runs (thanks to
`define-minor-mode`) `auto-correct-mode-hook` every time the mode is
changed (enabled or disabled), so the above two hooks
aren't indispensable.
E.g. you could use the patch below or simplify further by changing
auto-correct--add/remove-support to only take a single argument. Or by
dropping this altogether since the third party could simply add itself
to auto-correct-hook directly, since that's a standard interface for
minor modes.
[ While I'm here, I'll wonder aloud why you used "--" names for functions
which are meant to be used by third party packages, since we normally
use "--" to mean that this is an internal function/var. ]
The patch below also changes the flyspell part of the code to use
add-function so as not to make any assumption about the value of
flyspell-insert-function.
Stefan
diff --git a/packages/auto-correct/auto-correct.el
b/packages/auto-correct/auto-correct.el
index 42f84de1f..286b6678d 100644
--- a/packages/auto-correct/auto-correct.el
+++ b/packages/auto-correct/auto-correct.el
@@ -115,18 +115,6 @@ locally. If nil, the correction will be made whenever
(write-abbrev-file)
(message "\"%s\" now expands to \"%s\"" bef aft)))
-;; Extension Support
-
-(defvar auto-correct-activate-functions nil
- "Functions to run to activate auto-correct support in various packages.
-
-These are called by `auto-correct-mode' when it is enabled.")
-
-(defvar auto-correct-deactivate-functions nil
- "Functions to run to deactivate auto-correct support in various packages.
-
-These are called by `auto-correct-mode' when it is disabled.")
-
;; The mode
;;;###autoload
@@ -152,9 +140,7 @@ the command `auto-correct-toggle-ispell-local'.
:global t
:init-value nil
:lighter " Auto-Correct"
- (if auto-correct-mode
- (run-hooks 'auto-correct-activate-functions)
- (run-hooks 'auto-correct-deactivate-functions)))
+ )
;; Only enable the abbrev list when auto-correct-mode is active.
(add-to-list 'abbrev-minor-mode-table-alist
@@ -166,8 +152,8 @@ the command `auto-correct-toggle-ispell-local'.
"Add auto-correct support for a spelling package.
Support will be activated by ACTIVATE-FUN and deactivated by DEACTIVATE-FUN."
- (add-hook 'auto-correct-activate-functions activate-fun)
- (add-hook 'auto-correct-deactivate-functions deactivate-fun)
+ (add-hook 'auto-correct-hook
+ (lambda () (funcall (if auto-correct-mode activate-fun
deactivate-fun))))
;; If `auto-correct-mode' is enabled, activate this package's support.
(when auto-correct-mode
(funcall activate-fun)))
@@ -176,8 +162,9 @@ Support will be activated by ACTIVATE-FUN and deactivated
by DEACTIVATE-FUN."
"Remove support for a spelling package.
Support will be activated by ACTIVATE-FUN and deactivated by DEACTIVATE-FUN."
- (remove-hook 'auto-correct-activate-functions activate-fun)
- (remove-hook 'auto-correct-deactivate-functions deactivate-fun)
+ (remove-hook 'auto-correct-hook
+ (lambda ()
+ (funcall (if auto-correct-mode activate-fun deactivate-fun))))
;; If `auto-correct-mode' is enabled, deactivate this package's support.
(when auto-correct-mode
(funcall deactivate-fun)))
@@ -209,19 +196,19 @@ When `auto-correct-mode' is enabled, this function is set
as
(let ((old-word flyspell-auto-correct-word)
(new-word word)
(local (not flyspell-use-global-abbrev-table-p)))
- (auto-correct--add-or-update-correction old-word new-word local)
- (insert word)))
+ (auto-correct--add-or-update-correction old-word new-word local)))
(defun auto-correct--flyspell-activate ()
"Activate flyspell auto-correct support.
Sets `flyspell-insert-function' to `auto-correct-flyspell-insert'."
;; Add flyspell corrections as auto-corrections
- (setq flyspell-insert-function 'auto-correct-flyspell-insert))
+ (add-function :before flyspell-insert-function
+ #'auto-correct-flyspell-insert))
(defun auto-correct--flyspell-deactivate ()
"Deactivate flyspell auto-correct support."
- (setq flyspell-insert-function 'insert))
+ (remove-function flyspell-insert-function #'auto-correct-flyspell-insert))
(defcustom auto-correct-enable-flyspell-support t
"Whether to automatically correct corrections made in flyspell."