From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: [ELPA] New package: Auto Correct Mode Date: Mon, 04 Sep 2017 18:17:59 -0400 Message-ID: References: <87zialc7wx.fsf@escafil> <87lglw6cmr.fsf@escafil> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1504563547 14609 195.159.176.226 (4 Sep 2017 22:19:07 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 4 Sep 2017 22:19:07 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Sep 05 00:18:53 2017 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dozhf-0002km-4S for ged-emacs-devel@m.gmane.org; Tue, 05 Sep 2017 00:18:47 +0200 Original-Received: from localhost ([::1]:55877 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dozhk-0003DP-HU for ged-emacs-devel@m.gmane.org; Mon, 04 Sep 2017 18:18:52 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:54884) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dozha-0003D4-J3 for emacs-devel@gnu.org; Mon, 04 Sep 2017 18:18:47 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dozhV-0004YJ-G8 for emacs-devel@gnu.org; Mon, 04 Sep 2017 18:18:42 -0400 Original-Received: from [195.159.176.226] (port=45530 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dozhV-0004Xk-90 for emacs-devel@gnu.org; Mon, 04 Sep 2017 18:18:37 -0400 Original-Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1dozh2-00010T-Vk for emacs-devel@gnu.org; Tue, 05 Sep 2017 00:18:08 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 112 Original-X-Complaints-To: usenet@blaine.gmane.org Cancel-Lock: sha1:IkxB0BBilHM7p7tUoiuV54aI+Ik= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 195.159.176.226 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:217932 Archived-At: > (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."