[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/auto-correct 3e4d699 02/11: auto-correct.el: Cleaned up
From: |
Stefan Monnier |
Subject: |
[elpa] externals/auto-correct 3e4d699 02/11: auto-correct.el: Cleaned up support activation/deactivation |
Date: |
Tue, 1 Dec 2020 15:17:28 -0500 (EST) |
branch: externals/auto-correct
commit 3e4d6996b24b14534f5ce050a11e182f6851239a
Author: Ian Dunn <dunni@gnu.org>
Commit: Ian Dunn <dunni@gnu.org>
auto-correct.el: Cleaned up support activation/deactivation
* auto-correct.el (auto-correct-activate-functions):
(auto-correct-deactivate-functions): Removed in favor of normal mode
hooks.
(auto-correct-mode): Removed use of auto-correct-(de)activate-functions.
(auto-correct--add-support):
(auto-correct--remove-support): Merged into one function.
(auto-correct-handle-support): Renamed from
`auto-correct--handle-support'.
(auto-correct--flyspell-activate):
(auto-correct--flyspell-deactivate): Merged into...
(auto-correct--activate-flyspell-support): This.
(auto-correct-enable-flyspell-Support): Use the new function.
(auto-correct--ispell-activate):
(auto-correct--ispell-deactivate): Merged into...
(auto-correct--activate-ispell-support): This.
(auto-correct-enable-ispell-support): Use the new function.
---
auto-correct.el | 118 +++++++++++++++++++++-----------------------------------
1 file changed, 43 insertions(+), 75 deletions(-)
diff --git a/auto-correct.el b/auto-correct.el
index 42f84de..0ce103c 100644
--- a/auto-correct.el
+++ b/auto-correct.el
@@ -53,12 +53,13 @@
;; 1. Create a function that calls `auto-correct--add-or-update-correction'
with
;; the old text and the corrected text from your package.
-;; 2. Write two functions: an activation function and a deactivation function.
-;; These will be called by `auto-correct-mode' to activate and deactivate
-;; support.
+;; 2. Write a function to activate and deactivate support for your package. It
+;; should take a single argument, which is a boolean indicating whether to
+;; activate or deactivate support.
-;; 3. Call `auto-correct--add-support' with your activation and deactivation
-;; functions.
+;; 3. Call `auto-correct-handle-support', passing t as the first argument and
+;; your function as the second. To disable support, pass nil as the first
+;; argument instead.
;; 4. You're done.
@@ -115,18 +116,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
@@ -151,10 +140,7 @@ the command `auto-correct-toggle-ispell-local'.
:group 'auto-correct
:global t
:init-value nil
- :lighter " Auto-Correct"
- (if auto-correct-mode
- (run-hooks 'auto-correct-activate-functions)
- (run-hooks 'auto-correct-deactivate-functions)))
+ :lighter " Auto-Correct")
;; Only enable the abbrev list when auto-correct-mode is active.
(add-to-list 'abbrev-minor-mode-table-alist
@@ -162,35 +148,18 @@ the command `auto-correct-toggle-ispell-local'.
'append
#'equal)
-(defun auto-correct--add-support (activate-fun deactivate-fun)
- "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)
- ;; If `auto-correct-mode' is enabled, activate this package's support.
- (when auto-correct-mode
- (funcall activate-fun)))
-
-(defun auto-correct--remove-support (activate-fun 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)
- ;; If `auto-correct-mode' is enabled, deactivate this package's support.
- (when auto-correct-mode
- (funcall deactivate-fun)))
-
-(defun auto-correct--handle-support (add activate-fun deactivate-fun)
+(defun auto-correct-handle-support (activate support-fun)
"Helper function to add or remove auto-correct support for a package.
-If ADD is non-nil, add support, otherwise remove it.
-ACTIVATE-FUN and DEACTIVATE-FUN are exactly as they are in
-`auto-correct--add-support'."
- (if add
- (auto-correct--add-support activate-fun deactivate-fun)
- (auto-correct--remove-support activate-fun deactivate-fun)))
+If ACTIVATE is non-nil, add support, otherwise remove it.
+SUPPORT-FUN is a function that takes a single argument: a boolean
+indicating whether to activate or deactivate support."
+ (if activate
+ (add-hook 'auto-correct-hook support-fun)
+ (remove-hook 'auto-correct-hook support-fun))
+ ;; If `auto-correct-mode' is enabled, activate or deactivate support.
+ (when auto-correct-mode
+ (funcall support-fun activate)))
;; Flyspell Support
@@ -209,19 +178,20 @@ 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.
+(defun auto-correct--activate-flyspell-support (activate)
+ "Activate or deactivate auto-correct support for flyspell.
-Sets `flyspell-insert-function' to `auto-correct-flyspell-insert'."
- ;; Add flyspell corrections as auto-corrections
- (setq flyspell-insert-function 'auto-correct-flyspell-insert))
+If ACTIVATE is non-nil, activate support for flyspell.
+Otherwise, deactivate it.
-(defun auto-correct--flyspell-deactivate ()
- "Deactivate flyspell auto-correct support."
- (setq flyspell-insert-function 'insert))
+Activation means adding `auto-correct-flyspell-insert' to
+`flyspell-insert-function'."
+ (if activate
+ (add-function :before flyspell-insert-function
+ #'auto-correct-flyspell-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."
@@ -229,10 +199,9 @@ Sets `flyspell-insert-function' to
`auto-correct-flyspell-insert'."
:type 'boolean
:set (lambda (sym val)
(set sym val)
- (auto-correct--handle-support
+ (auto-correct-handle-support
val
- 'auto-correct--flyspell-activate
- 'auto-correct--flyspell-deactivate)))
+ 'auto-correct--activate-flyspell-support)))
;; Ispell support
@@ -267,21 +236,21 @@ This is intended to be added as advice to
`ispell-command-loop'."
(eq correction 'quit))) ;; Session was exited
(not (equal word-before correction))) ;; Word was corrected
(auto-correct--add-or-update-correction word-before correction
-
auto-correct--ispell-use-local-table)))
+
auto-correct--ispell-use-local-table)))
ispell-result)
-(defun auto-correct--ispell-activate ()
- "Activate Ispell auto-correct support.
+(defun auto-correct--activate-ispell-support (activate)
+ "Activate or deactivate Ispell auto-correct support.
-Adds advice to `ispell-command-loop' that adds the result as a
-correction."
- ;; Add corrections from ispell as auto-corrections
- (advice-add 'ispell-command-loop :filter-return
- #'auto-correct--ispell-handler))
+If ACTIVATE is non-nil, activate support for Ispell. Otherwise,
+deactivate it.
-(defun auto-correct--ispell-deactivate ()
- "Deactivate Ispell auto-correct support."
- (advice-remove 'ispell-command-loop #'auto-correct--ispell-handler))
+Activating means adding advice to `ispell-command-loop' that adds
+the result as a correction."
+ (if activate
+ (advice-add 'ispell-command-loop :filter-return
+ #'auto-correct--ispell-handler)
+ (advice-remove 'ispell-command-loop #'auto-correct--ispell-handler)))
(defcustom auto-correct-enable-ispell-support t
"Whether to automatically correct corrections made in Ispell."
@@ -289,10 +258,9 @@ correction."
:type 'boolean
:set (lambda (sym val)
(set sym val)
- (auto-correct--handle-support
+ (auto-correct-handle-support
val
- 'auto-correct--ispell-activate
- 'auto-correct--ispell-deactivate)))
+ 'auto-correct--activate-ispell-support)))
;; Standalone (piggybacks on Ispell)
- [elpa] branch externals/auto-correct created (now 3ab1d23), Stefan Monnier, 2020/12/01
- [elpa] externals/auto-correct 9147742 01/11: Added auto-correct package, Stefan Monnier, 2020/12/01
- [elpa] externals/auto-correct 80fe805 03/11: auto-correct.el: Changed default predicate to enable auto-correct, Stefan Monnier, 2020/12/01
- [elpa] externals/auto-correct 3e4d699 02/11: auto-correct.el: Cleaned up support activation/deactivation,
Stefan Monnier <=
- [elpa] externals/auto-correct fb9950c 07/11: auto-correct: Fixed handle-support bug and bumped version, Stefan Monnier, 2020/12/01
- [elpa] externals/auto-correct 0d38425 10/11: auto-correct: Add support for flyspell-correct-word-*, Stefan Monnier, 2020/12/01
- [elpa] externals/auto-correct 3ab1d23 11/11: * .gitignore: New file, Stefan Monnier, 2020/12/01
- [elpa] externals/auto-correct 0be9302 06/11: auto-correct: Bumped version to 1.1.1, Stefan Monnier, 2020/12/01
- [elpa] externals/auto-correct f9c542b 08/11: auto-correct: Defer loading of flyspell support until after flyspell has been enabled, Stefan Monnier, 2020/12/01
- [elpa] externals/auto-correct 27a2323 04/11: auto-correct.el: Reverted last commit, Stefan Monnier, 2020/12/01
- [elpa] externals/auto-correct 84101c5 05/11: auto-correct: Changed default predicate to a function and added examples, Stefan Monnier, 2020/12/01
- [elpa] externals/auto-correct 12955c8 09/11: Updated copyright on auto-correct, captain, and vigenere, Stefan Monnier, 2020/12/01