From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= Newsgroups: gmane.emacs.bugs Subject: bug#33794: 26.1; electric-pair-mode breaks auto-newline minor mode of cc-mode Date: Sat, 22 Dec 2018 02:16:03 +0000 Message-ID: <87pntu8fx8.fsf@gmail.com> References: <20181221134829.29135.qmail@mail.muc.de> <87tvj68j15.fsf@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1545444914 12977 195.159.176.226 (22 Dec 2018 02:15:14 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 22 Dec 2018 02:15:14 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: Alan Mackenzie , bea@klebe.blog, Stefan Monnier , 33794@debbugs.gnu.org To: Beatrix Klebe Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sat Dec 22 03:15:09 2018 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1gaWon-0003HT-6f for geb-bug-gnu-emacs@m.gmane.org; Sat, 22 Dec 2018 03:15:09 +0100 Original-Received: from localhost ([::1]:49405 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gaWqt-0002c3-PZ for geb-bug-gnu-emacs@m.gmane.org; Fri, 21 Dec 2018 21:17:19 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:49686) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gaWqf-0002bl-Li for bug-gnu-emacs@gnu.org; Fri, 21 Dec 2018 21:17:07 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gaWqc-0004dP-Eq for bug-gnu-emacs@gnu.org; Fri, 21 Dec 2018 21:17:05 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:54755) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gaWqc-0004dF-6Z; Fri, 21 Dec 2018 21:17:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1gaWqb-0001PT-VG; Fri, 21 Dec 2018 21:17:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org, bug-cc-mode@gnu.org Resent-Date: Sat, 22 Dec 2018 02:17:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 33794 X-GNU-PR-Package: emacs,cc-mode X-GNU-PR-Keywords: Original-Received: via spool by 33794-submit@debbugs.gnu.org id=B33794.15454449755360 (code B ref 33794); Sat, 22 Dec 2018 02:17:01 +0000 Original-Received: (at 33794) by debbugs.gnu.org; 22 Dec 2018 02:16:15 +0000 Original-Received: from localhost ([127.0.0.1]:59013 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gaWpq-0001ON-QE for submit@debbugs.gnu.org; Fri, 21 Dec 2018 21:16:15 -0500 Original-Received: from mail-wr1-f41.google.com ([209.85.221.41]:35396) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gaWpo-0001O8-QW for 33794@debbugs.gnu.org; Fri, 21 Dec 2018 21:16:13 -0500 Original-Received: by mail-wr1-f41.google.com with SMTP id 96so7019356wrb.2 for <33794@debbugs.gnu.org>; Fri, 21 Dec 2018 18:16:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=2TYjsuh1OhtZaxXBAVYGO6/oefaKcu0CTZYmtgMoCMU=; b=EWUSz64bvRXUdib9eIH+TR3CAuOrDLfmeyMR6G/Ka+w/2981/uIhAxBF1lu6lWZ7Dg jftObpRtgfCuDIHLwWSGVrBxcZmaPf0qVPBb0yvQLn0yy2mx/dcW902XZDOPslO9Y+17 L3i62Agl95QauVEuNVaaeW9tCmQzF/gfb9BfO2mA7o2ILOm6psNrfyB3BQtp8aw9sL5u zw0JUNWqRxPAkp87ptLDVI0kXV5MOOLDIQGJ/g/aIcDLt8igs4qpAaPuwr4GhPVfRv8k TX4po6l4takUVrmTxuzKwTjg0+VUJsesAjjBTz08bl6i8PVCsROVEwcIflT4+37gU8fJ K6+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=2TYjsuh1OhtZaxXBAVYGO6/oefaKcu0CTZYmtgMoCMU=; b=ai73vHB5pslQokKgfxwLRhKQRmPZ6sUUw039mJTmU9uOJovGuwPmgMGj9dAz2rAziT c7ng0J5lkVlKiICen5zKGf5nEZaIglgTO/QelndxlFuNHBYBiWYr/t/FCEbD4C6jzIZa 8Ndp9WeNOdd6fh6I2C1qaTbHSJ3bvWX4P2qv8AgBK1vToGB6o7XBsMHZD6a7ez7iM65+ otzsLXbm9xXjX454fQg1ClULsjpX3gdXFeV3eg6YTQgVrc2S1R0tUUU8cceXYjTnGWrU eZe6A1qiqANlWjDXuNoeOh4aD21Nfq3RGHMNld0uYhcoBkpBNerXZoFL4yOJ3gaqN4mY LVLg== X-Gm-Message-State: AJcUukfHaiXNGAwf6NgLUU/F9YcfxmG6TYdVo9hmFVP5Q5AVbowN51nT xb3snynUTeLIx5YsVWrWP5+Wj65h X-Google-Smtp-Source: ALg8bN5Y3X8/s8z9rtBjISXRVcRwXc6Um2t5DF62J199Lmn3HzJd4IGaFz2IvxaZMc+wG3xaExYluw== X-Received: by 2002:a5d:4d46:: with SMTP id a6mr4754543wru.28.1545444966857; Fri, 21 Dec 2018 18:16:06 -0800 (PST) Original-Received: from lolita.yourcompany.com (188.139.62.94.rev.vodafone.pt. [94.62.139.188]) by smtp.gmail.com with ESMTPSA id d4sm16306468wrp.89.2018.12.21.18.16.04 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 21 Dec 2018 18:16:05 -0800 (PST) In-Reply-To: <87tvj68j15.fsf@gmail.com> ("=?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?="'s message of "Sat, 22 Dec 2018 01:08:54 +0000") 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: 208.118.235.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:153710 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi again Beatrix, In the latest version of the patch I am preparing, if you do (add-hook 'c-mode-hook (lambda () (setq-local electric-layout-rules '((?\{ . before}) (?\{ . after) (?\{ . after-stay))))) You should arive at Allman C-style electric layouts. So typing int main () then brace should give you: int main () { } I attach the two patches for convenience, which you can also find in the scratch/fix-33794-extend-electric-layout-mode branch. Jo=C3=A3o --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Extend-electric-layout-mode-to-handle-more-complex-l.patch >From ab036bdedbb49ecc96d550b5e883e43bb03eaccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20T=C3=A1vora?= Date: Fri, 21 Dec 2018 18:00:08 +0000 Subject: [PATCH 1/2] Extend electric-layout-mode to handle more complex layouts Also, have it play nice with electric-pair-mode. Multiple matching entries in `electric-layout-rules' are executed in order of appearance. When inserting a newline in the 'after-stay rule, ensure electric-pair-open-newline-between-pairs is nil. Arguably the logic behind electric-pair-open-newline-between-pairs should be moved to electric-layout-mode, but the current rule-matching engine doesn't allow for it. The current solution seems to be good enough for the situations reported in bug#33794. * lisp/electric.el (electric-layout-rules): Adjust docstring. (electric-layout-post-self-insert-function): Loop through rules. Bind electric-pair-open-newline-between-pairs to nil when handling after-stay. --- lisp/electric.el | 61 ++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/lisp/electric.el b/lisp/electric.el index 6dbf46b80c..6a307a49b9 100644 --- a/lisp/electric.el +++ b/lisp/electric.el @@ -370,38 +370,43 @@ electric-layout-rules The symbols specify where in relation to CHAR the newline character(s) should be inserted. `after-stay' means insert a -newline after CHAR but stay in the same place.") +newline after CHAR but stay in the same place. + +If multiple rules match, they are all executed in order of +appearance.") (defun electric-layout-post-self-insert-function () - (let* ((rule (cdr (assq last-command-event electric-layout-rules))) - pos) - (when (and rule - (setq pos (electric--after-char-pos)) + (let (pos) + (when (and (setq pos (electric--after-char-pos)) ;; Not in a string or comment. (not (nth 8 (save-excursion (syntax-ppss pos))))) - (let ((end (point-marker)) - (sym (if (functionp rule) (funcall rule) rule))) - (set-marker-insertion-type end (not (eq sym 'after-stay))) - (goto-char pos) - (pcase sym - ;; FIXME: we used `newline' down here which called - ;; self-insert-command and ran post-self-insert-hook recursively. - ;; It happened to make electric-indent-mode work automatically with - ;; electric-layout-mode (at the cost of re-indenting lines - ;; multiple times), but I'm not sure it's what we want. - ;; - ;; FIXME: check eolp before inserting \n? - ('before (goto-char (1- pos)) (skip-chars-backward " \t") - (unless (bolp) (insert "\n"))) - ('after (insert "\n")) - ('after-stay (save-excursion - (let ((electric-layout-rules nil)) - (newline 1 t)))) - ('around (save-excursion - (goto-char (1- pos)) (skip-chars-backward " \t") - (unless (bolp) (insert "\n"))) - (insert "\n"))) ; FIXME: check eolp before inserting \n? - (goto-char end))))) + (goto-char pos) + (dolist (rule electric-layout-rules) + (when (eq last-command-event (car rule)) + (let* ((end (point-marker)) + (rule (cdr rule)) + (sym (if (functionp rule) (funcall rule) rule))) + (set-marker-insertion-type end (not (eq sym 'after-stay))) + (pcase sym + ;; FIXME: we used `newline' down here which called + ;; self-insert-command and ran post-self-insert-hook recursively. + ;; It happened to make electric-indent-mode work automatically with + ;; electric-layout-mode (at the cost of re-indenting lines + ;; multiple times), but I'm not sure it's what we want. + ;; + ;; FIXME: check eolp before inserting \n? + ('before (goto-char (1- pos)) (skip-chars-backward " \t") + (unless (bolp) (insert "\n"))) + ('after (insert "\n")) + ('after-stay (save-excursion + (let ((electric-layout-rules nil) + (electric-pair-open-newline-between-pairs nil)) + (newline 1 t)))) + ('around (save-excursion + (goto-char (1- pos)) (skip-chars-backward " \t") + (unless (bolp) (insert "\n"))) + (insert "\n"))) ; FIXME: check eolp before inserting \n? + (goto-char end))))))) (put 'electric-layout-post-self-insert-function 'priority 40) -- 2.20.0 --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0002-Rework-electric-layout-post-self-insert-function-bug.patch >From dfc19a04f5f455051d158648a963cb1fd8b91c67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20T=C3=A1vora?= Date: Sat, 22 Dec 2018 01:52:08 +0000 Subject: [PATCH 2/2] Rework electric-layout-post-self-insert-function (bug#33794) This should now fix more problems reported in (bug#33794) regarding insertion of newlines before and after the opening brace. Write two automated tests. Also provide a new electric-layout-local-mode for testing. * lisp/electric.el (electric-layout-post-self-insert-function-1): New function that does the work for electric-layout-post-self-insert-function-1. (electric-layout-local-mode): New minor mode. * test/lisp/electric-tests.el (electric-layout-int-main-kernel-style) (electric-layout-int-main-allman-style): Add two tests. --- lisp/electric.el | 49 +++++++++++++++++++++++++------------ test/lisp/electric-tests.el | 34 +++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 15 deletions(-) diff --git a/lisp/electric.el b/lisp/electric.el index 6a307a49b9..24c040d05b 100644 --- a/lisp/electric.el +++ b/lisp/electric.el @@ -376,17 +376,25 @@ electric-layout-rules appearance.") (defun electric-layout-post-self-insert-function () - (let (pos) + (when electric-layout-mode + (electric-layout-post-self-insert-function-1))) + +;; for edebug's sake a separate function +(defun electric-layout-post-self-insert-function-1 () + (let (pos end) (when (and (setq pos (electric--after-char-pos)) ;; Not in a string or comment. (not (nth 8 (save-excursion (syntax-ppss pos))))) (goto-char pos) + (setq end (point-marker)) (dolist (rule electric-layout-rules) (when (eq last-command-event (car rule)) - (let* ((end (point-marker)) - (rule (cdr rule)) - (sym (if (functionp rule) (funcall rule) rule))) - (set-marker-insertion-type end (not (eq sym 'after-stay))) + (let* ((rule (cdr rule)) + (sym (if (functionp rule) (funcall rule) rule)) + (nl (lambda () + (let ((electric-layout-mode nil) + (electric-pair-open-newline-between-pairs nil)) + (newline 1 t))))) (pcase sym ;; FIXME: we used `newline' down here which called ;; self-insert-command and ran post-self-insert-hook recursively. @@ -395,18 +403,16 @@ electric-layout-post-self-insert-function ;; multiple times), but I'm not sure it's what we want. ;; ;; FIXME: check eolp before inserting \n? - ('before (goto-char (1- pos)) (skip-chars-backward " \t") - (unless (bolp) (insert "\n"))) - ('after (insert "\n")) - ('after-stay (save-excursion - (let ((electric-layout-rules nil) - (electric-pair-open-newline-between-pairs nil)) - (newline 1 t)))) + ('before (save-excursion + (goto-char (1- pos)) (skip-chars-backward " \t") + (unless (bolp) (funcall nl)))) + ('after (funcall nl)) + ('after-stay (save-excursion (funcall nl))) ('around (save-excursion (goto-char (1- pos)) (skip-chars-backward " \t") - (unless (bolp) (insert "\n"))) - (insert "\n"))) ; FIXME: check eolp before inserting \n? - (goto-char end))))))) + (unless (bolp) (funcall nl))) + (funcall nl))) ; FIXME: check eolp before inserting \n? + )))))) (put 'electric-layout-post-self-insert-function 'priority 40) @@ -424,6 +430,19 @@ electric-layout-mode (remove-hook 'post-self-insert-hook #'electric-layout-post-self-insert-function)))) +;;;###autoload +(define-minor-mode electric-layout-local-mode + "Toggle `electric-layout-mode' only in this buffer." + :variable (buffer-local-value 'electric-layout-mode (current-buffer)) + (cond + ((eq electric-layout-mode (default-value 'electric-layout-mode)) + (kill-local-variable 'electric-layout-mode)) + ((not (default-value 'electric-layout-mode)) + ;; Locally enabled, but globally disabled. + (electric-layout-mode 1) ; Setup the hooks. + (setq-default electric-layout-mode nil) ; But keep it globally disabled. + ))) + ;;; Electric quoting. (defcustom electric-quote-comment t diff --git a/test/lisp/electric-tests.el b/test/lisp/electric-tests.el index a665d2eb28..971c3e9567 100644 --- a/test/lisp/electric-tests.el +++ b/test/lisp/electric-tests.el @@ -812,5 +812,39 @@ electric-quote-markdown-in-code :bindings '((comment-start . "