From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.bugs Subject: bug#35254: 27.0.50; cc-mode/electric-pair-mode/electric-layout-mode: bad trailing whitespace behavior in cc-mode Date: Wed, 15 May 2019 09:19:22 -0400 Message-ID: References: <87ftqms9db.fsf@secretsauce.net> <871s15k7ll.fsf@gmail.com> <20190513195323.GB5525@ACM> <20190514092735.GB4231@ACM> <20190515100339.GB15042@ACM> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="72192"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: Alan Mackenzie , Noam Postavsky , 35254@debbugs.gnu.org, Dima Kogan To: =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Wed May 15 15:20:19 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.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hQtpS-000IbF-5R for geb-bug-gnu-emacs@m.gmane.org; Wed, 15 May 2019 15:20:18 +0200 Original-Received: from localhost ([127.0.0.1]:37139 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hQtpR-0002L5-09 for geb-bug-gnu-emacs@m.gmane.org; Wed, 15 May 2019 09:20:17 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:47150) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hQtpG-0002Js-2U for bug-gnu-emacs@gnu.org; Wed, 15 May 2019 09:20:07 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hQtpE-0000vO-Ph for bug-gnu-emacs@gnu.org; Wed, 15 May 2019 09:20:06 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:37968) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hQtpC-0000ru-Ei; Wed, 15 May 2019 09:20:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1hQtpC-0000CL-7G; Wed, 15 May 2019 09:20:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org, bug-cc-mode@gnu.org Resent-Date: Wed, 15 May 2019 13:20:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 35254 X-GNU-PR-Package: emacs,cc-mode Original-Received: via spool by 35254-submit@debbugs.gnu.org id=B35254.1557926378713 (code B ref 35254); Wed, 15 May 2019 13:20:02 +0000 Original-Received: (at 35254) by debbugs.gnu.org; 15 May 2019 13:19:38 +0000 Original-Received: from localhost ([127.0.0.1]:51512 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hQton-0000BR-Jv for submit@debbugs.gnu.org; Wed, 15 May 2019 09:19:37 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:23387) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hQtom-0000BA-DT for 35254@debbugs.gnu.org; Wed, 15 May 2019 09:19:37 -0400 Original-Received: from pmg1.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 6607E100A2B; Wed, 15 May 2019 09:19:30 -0400 (EDT) Original-Received: from mail02.iro.umontreal.ca (unknown [172.31.2.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 04C8F1007AD; Wed, 15 May 2019 09:19:29 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1557926369; bh=Mal1KQzVyvsXB+e063WIOzfJPNfyNMvAgR/ss80D+J8=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=akgJMXii4KmvdCJR/E03RRZpFoxy/m4HIuBnfM997lgl0KwQuIqkd6l/2F7PoNvn6 moOyJLNg92vzaaSDtLznmfmQb2Cnojcmsuug4fE2WyjKFgIPVjVzn6djyqtcCRgWjT nSboEOgx7N5hfq2oH8BByecBYFmpwiOGrsWHUeEN1ntj8+Hmx/EXP6UU90NVnBLT4v g/0wvoG0w6+cQjXbOsrzv2NfDZP6ICXeqNjzvQN4EDJ5Sxrj7UjrCeVQW4wH5gZ87+ yNk1XEzDPh0jAwuxvj2Bzb0lGUDRlDigPxYxGGGBVhnxI3Xgs8Dk//VFPRpYTj7GKQ OOW8SIErEuwXQ== Original-Received: from pastel (69-196-155-191.dsl.teksavvy.com [69.196.155.191]) by mail02.iro.umontreal.ca (Postfix) with ESMTPSA id 9F3B7120264; Wed, 15 May 2019 09:19:28 -0400 (EDT) In-Reply-To: ("=?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?="'s message of "Wed, 15 May 2019 12:27:52 +0100") 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:159337 Archived-At: > I've just come across the bug myself, and it is indeed annoying. > Can you check if this patch, which seems the simplest, serves > all purposes? FWIW, I find it rather ugly (makes the two minor modes too tightly intertwined). > It also adds a test to prevent future regressions Another approach might be to do the whitespace erasure in electric-indent without paying any attention to electric-layout/pair, and then in electric-pair to explicitly cause (re)indentation after moving point to between the opened pair? Yet another option is to tell electric-indent about the final position of point and have it refrain from deleting whitespace before that final position, as in the patch below. WDYT? Stefan diff --git a/lisp/elec-pair.el b/lisp/elec-pair.el index 3be09d87b4..a14efff241 100644 --- a/lisp/elec-pair.el +++ b/lisp/elec-pair.el @@ -551,7 +551,8 @@ electric-pair-post-self-insert-function (goto-char pos) (funcall electric-pair-inhibit-predicate last-command-event))))) - (save-excursion (electric-pair--insert pair))))) + (let ((electric-indent--destination (point-marker))) + (save-excursion (electric-pair--insert pair)))))) (_ (when (and (if (functionp electric-pair-open-newline-between-pairs) (funcall electric-pair-open-newline-between-pairs) diff --git a/lisp/electric.el b/lisp/electric.el index 07da2f1d9e..71ebb9cf45 100644 --- a/lisp/electric.el +++ b/lisp/electric.el @@ -231,6 +231,14 @@ electric-indent-functions-without-reindent not try to reindent lines. It is normally better to make the major mode set `electric-indent-inhibit', but this can be used as a workaround.") +(defun electric-indent--inhibited-p () + (or electric-indent-inhibit + (memq indent-line-function + electric-indent-functions-without-reindent))) + +(defvar electric-indent--destination nil + "If non-nil, position to which point will be later restored.") + (defun electric-indent-post-self-insert-function () "Function that `electric-indent-mode' adds to `post-self-insert-hook'. This indents if the hook `electric-indent-functions' returns non-nil, @@ -272,26 +280,26 @@ electric-indent-post-self-insert-function (when at-newline (let ((before (copy-marker (1- pos) t))) (save-excursion - (unless - (or (memq indent-line-function - electric-indent-functions-without-reindent) - electric-indent-inhibit) + (unless (electric-indent--inhibited-p) ;; Don't reindent the previous line if the ;; indentation function is not a real one. (goto-char before) (condition-case-unless-debug () (indent-according-to-mode) - (error (throw 'indent-error nil))) - ;; The goal here will be to remove the trailing - ;; whitespace after reindentation of the previous line - ;; because that may have (re)introduced it. - (goto-char before) - ;; We were at EOL in marker `before' before the call - ;; to `indent-according-to-mode' but after we may - ;; not be (Bug#15767). - (when (and (eolp)) - (delete-horizontal-space t)))))) - (unless (and electric-indent-inhibit + (error (throw 'indent-error nil)))) + ;; The goal here will be to remove the trailing + ;; whitespace after reindentation of the previous line + ;; because that may have (re)introduced it. + (goto-char before) + ;; We were at EOL in marker `before' before the call + ;; to `indent-according-to-mode' but after we may + ;; not be (Bug#15767). + (when (and (eolp) + ;; Don't delete "trailing space" before point! + (not (and electric-indent--destination + (= (point) electric-indent--destination)))) + (delete-horizontal-space t))))) + (unless (and (electric-indent--inhibited-p) (not at-newline)) (condition-case-unless-debug () (indent-according-to-mode) diff --git a/test/lisp/electric-tests.el b/test/lisp/electric-tests.el index 4f1e5729be..0b67fb3f1f 100644 --- a/test/lisp/electric-tests.el +++ b/test/lisp/electric-tests.el @@ -876,15 +876,6 @@ electric-layout-for-c-style-du-jour (call-interactively (key-binding `[,last-command-event]))) (should (equal (buffer-string) "int main () {\n \n}")))) -(define-derived-mode plainer-c-mode c-mode "pC" - "A plainer/saner C-mode with no internal electric machinery." - (c-toggle-electric-state -1) - (setq-local electric-indent-local-mode-hook nil) - (setq-local electric-indent-mode-hook nil) - (electric-indent-local-mode 1) - (dolist (key '(?\" ?\' ?\{ ?\} ?\( ?\) ?\[ ?\])) - (local-set-key (vector key) 'self-insert-command))) - (ert-deftest electric-modes-int-main-allman-style () (ert-with-test-buffer () (plainer-c-mode)