From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.bugs Subject: bug#38406: 27.0.50; post-self-insert-hook does not hold its contract in cc-mode derived modes Date: Wed, 4 Dec 2019 20:41:59 +0000 Message-ID: <20191204204159.GA7587@ACM> References: <20191130143638.GA6716@ACM> <20191201150738.GB5085@ACM> <83imn0lyed.fsf@gnu.org> <20191201192709.GE5085@ACM> <83blsrn58a.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="99845"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Mutt/1.10.1 (2018-07-13) Cc: 38406@debbugs.gnu.org To: Eli Zaretskii , yyoncho@gmail.com Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Wed Dec 04 21:43:28 2019 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1icbUd-000PqV-KY for geb-bug-gnu-emacs@m.gmane.org; Wed, 04 Dec 2019 21:43:27 +0100 Original-Received: from localhost ([::1]:46980 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1icbUc-0002Ef-DI for geb-bug-gnu-emacs@m.gmane.org; Wed, 04 Dec 2019 15:43:26 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:34110) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1icbUL-0002EH-C8 for bug-gnu-emacs@gnu.org; Wed, 04 Dec 2019 15:43:11 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1icbUH-0007IA-GR for bug-gnu-emacs@gnu.org; Wed, 04 Dec 2019 15:43:07 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:36748) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1icbUE-0006oN-8J for bug-gnu-emacs@gnu.org; Wed, 04 Dec 2019 15:43:03 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1icbUE-0004EW-3C for bug-gnu-emacs@gnu.org; Wed, 04 Dec 2019 15:43:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Alan Mackenzie Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 04 Dec 2019 20:43:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 38406 X-GNU-PR-Package: emacs Original-Received: via spool by 38406-submit@debbugs.gnu.org id=B38406.157549213116208 (code B ref 38406); Wed, 04 Dec 2019 20:43:02 +0000 Original-Received: (at 38406) by debbugs.gnu.org; 4 Dec 2019 20:42:11 +0000 Original-Received: from localhost ([127.0.0.1]:42721 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1icbTP-0004DH-C3 for submit@debbugs.gnu.org; Wed, 04 Dec 2019 15:42:11 -0500 Original-Received: from colin.muc.de ([193.149.48.1]:32400 helo=mail.muc.de) by debbugs.gnu.org with smtp (Exim 4.84_2) (envelope-from ) id 1icbTI-0004D9-F9 for 38406@debbugs.gnu.org; Wed, 04 Dec 2019 15:42:05 -0500 Original-Received: (qmail 1870 invoked by uid 3782); 4 Dec 2019 20:42:03 -0000 Original-Received: from acm.muc.de (p4FE15AD3.dip0.t-ipconnect.de [79.225.90.211]) by colin.muc.de (tmda-ofmipd) with ESMTP; Wed, 04 Dec 2019 21:41:59 +0100 Original-Received: (qmail 8080 invoked by uid 1000); 4 Dec 2019 20:41:59 -0000 Content-Disposition: inline In-Reply-To: <83blsrn58a.fsf@gnu.org> X-Delivery-Agent: TMDA/1.1.12 (Macallan) X-Primary-Address: acm@muc.de X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:172850 Archived-At: Hello, Eli and Ivan. On Sun, Dec 01, 2019 at 22:47:01 +0200, Eli Zaretskii wrote: > > Date: Sun, 1 Dec 2019 19:27:09 +0000 > > Cc: yyoncho@gmail.com, 38406@debbugs.gnu.org > > From: Alan Mackenzie [ .... ] > But breaking a hook's contract as a means to teach people not to shoot > themselves in the foot is not right. If the uses are legitimate, they > should be able to do them; if they aren't, let them cope with the > consequences. > > So to call this hook at the end of c-electric-brace would mean > > having to filter the hook first (at the very least, to remove > > electric-pair-post-self-insert-function), which just seems very > > hackish and unsatisfactory. > It doesn't seem too hackish to me, and as a nice bonus we will have > post-self-insert-hook act as per its contract again. > So could you please do that? TIA. OK, here's a patch which I think does just what's wanted. Would you please try it out, Ivan, then let me know that it works, or about any problems which there still may be? Thanks. It's merely necessary to apply the patch below, byte compile cc-cmds.el, and load it. (In the highly unlikely event you want any help with the patch or the compilation, feel free to send me private email.) The patch should apply cleanly to the Emacs master branch. diff -r d20020192bd6 cc-cmds.el --- a/cc-cmds.el Sat Nov 30 21:10:11 2019 +0000 +++ b/cc-cmds.el Wed Dec 04 20:15:11 2019 +0000 @@ -493,6 +493,34 @@ (c-hungry-delete-forward) (c-hungry-delete-backwards))) +(defvar c--unsafe-post-self-insert-hook-functions + '(smie-blink-matching-open + electric-pair-post-self-insert-function + blink-paren-post-self-insert-function + electric-indent-post-self-insert-function + electric-layout-post-self-insert-function + electric-quote-post-self-insert-function) + "Known unsafe functions when members of `post-self-insert-hook' in CC Mode") + +(defun c--call-post-self-insert-hook-more-safely () + ;; Call post-self-insert-hook, having removed from `post-self-insert-hook' + ;; function known not to be safe to CC Mode. The result is of no + ;; significance. Note that the hook call is NOT absolutely safe. + (let ((src post-self-insert-hook) + dest) + (while src + (cond + ((memq (car src) c--unsafe-post-self-insert-hook-functions)) + ((eq (car src) t) + (let ((src (default-value 'post-self-insert-hook))) + (while src + (unless (memq (car src) c--unsafe-post-self-insert-hook-functions) + (add-hook dest (car src) t)) ; Preserve the order of the functions. + (setq src (cdr src))))) + (t (add-hook dest (car src) t))) ; Preserve the order of the functions. + (setq src (cdr src))) + (run-hooks dest))) + (defun c-electric-pound (arg) "Insert a \"#\". If `c-electric-flag' is set, handle it specially according to the variable @@ -522,7 +550,8 @@ (insert (c-last-command-char)) (and (not bolp) (goto-char (- (point-max) pos))) - ))) + )) + (c--call-post-self-insert-hook-more-safely)) (defun c-point-syntax () ;; Return the syntactic context of the construct at point. (This is NOT @@ -903,7 +932,8 @@ (save-excursion (c-save-buffer-state nil (c-backward-syntactic-ws safepos)) - (funcall old-blink-paren))))) + (funcall old-blink-paren))) + (c--call-post-self-insert-hook-more-safely))) (defun c-electric-slash (arg) "Insert a slash character. @@ -955,7 +985,8 @@ (let (post-self-insert-hook) ; Disable random functionality. (self-insert-command (prefix-numeric-value arg))) (if indentp - (indent-according-to-mode)))) + (indent-according-to-mode)) + (c--call-post-self-insert-hook-more-safely))) (defun c-electric-star (arg) "Insert a star character. @@ -985,7 +1016,8 @@ (bolp)))) (let (c-echo-syntactic-information-p) ; shut this up (indent-according-to-mode)) - )) + ) + (c--call-post-self-insert-hook-more-safely)) (defun c-electric-semi&comma (arg) "Insert a comma or semicolon. @@ -1057,8 +1089,8 @@ (setq add-newline-p (not (eq answer 'stop))) )) (if add-newline-p - (c-newline-and-indent)) - ))))) + (c-newline-and-indent))))) + (c--call-post-self-insert-hook-more-safely))) (defun c-electric-colon (arg) "Insert a colon. @@ -1160,8 +1192,8 @@ ;; does a newline go after the colon? (if (and (memq 'after (cdr-safe newlines)) (not is-scope-op)) - (c-newline-and-indent)) - )))) + (c-newline-and-indent)))) + (c--call-post-self-insert-hook-more-safely))) (defun c-electric-lt-gt (arg) "Insert a \"<\" or \">\" character. @@ -1251,7 +1283,8 @@ ;; From now (2016-01-01), the syntax-table text properties on < and > ;; are applied in an after-change function, not during redisplay. Hence ;; we no longer need to call (sit-for 0) for blink paren to work. - (funcall blink-paren-function))))) + (funcall blink-paren-function)))) + (c--call-post-self-insert-hook-more-safely)) (defun c-electric-paren (arg) "Insert a parenthesis. @@ -1370,7 +1403,8 @@ ;; Apply `electric-pair-mode' stuff inside a string or comment. (when (and (boundp 'electric-pair-mode) electric-pair-mode) (let (post-self-insert-hook) - (electric-pair-post-self-insert-function)))))) + (electric-pair-post-self-insert-function)))) + (c--call-post-self-insert-hook-more-safely))) (defun c-electric-continued-statement () "Reindent the current line if appropriate. -- Alan Mackenzie (Nuremberg, Germany).