Subject: Re: bug#33794: 26.1; electric-pair-mode breaks auto-newline minor mode of cc-mode

From: Alan Mackenzie <[email-masked]>
Date: 23 Dec 2018 14:48:37 -0000

Hello again, Beatrix.

In article <[email-masked]> you wrote:
> When using cc-mode, turning on electric-pair-mode causes the
> auto-newline minor mode to stop inserting newlines where expected. This
> is relevant to the formatting of C# code with the Allman/BSD brace style
> in particular, though it would be nice if these modes specifically did
> work together.

> In GNU Emacs 26.1 (build 1, x86_64-apple-darwin14.5.0, NS
> appkit-1348.17 Version 10.10.5 (Build 14F2511))
>  of 2018-05-30 built on builder10-10.porkrind.org
> Windowing system distributor 'Apple', version 10.3.1671
> Recent messages:
> Saving file /Users/bea/.emacs.d/lisp/dotnet.el...
> Wrote /Users/bea/.emacs.d/lisp/dotnet.el
> ; expected
> Undo!
> ; expected
> Undo! [2 times]
> ; expected
> Auto-saving...
> ; expected [2 times]
> Making completion list...

I think the following workaround will give you a lot of what you want.
I've tried it out only on C Mode, however.  The pertinent file is in
.../emacs/lisp/progmodes/.

Sadly it is only a workaround, since there doesn't seem to be any
interface to electric-pair-mode usable by programs.  However, it should
help, whilst we at Emacs negotiate amongst ourselves.

Happy Christmas, if you celebrate it!  (And a good time, otherwise!).

diff -r 7ec0a318802a cc-cmds.el
--- a/cc-cmds.el	Thu Dec 20 12:07:55 2018 +0000
+++ b/cc-cmds.el	Sun Dec 23 14:34:51 2018 +0000
@@ -721,7 +721,9 @@
 
     ;; Insert the brace.  Note that expand-abbrev might reindent
     ;; the line here if there's a preceding "else" or something.
-    (self-insert-command (prefix-numeric-value arg))
+    (let (post-self-insert-hook) ; the only way to get dependable
+				 ; functionality from self-insert-command.
+      (self-insert-command (prefix-numeric-value arg)))
 
     (when (and c-electric-flag (not literal) (not arg))
       (if (not (looking-at "[ \t]*\\\\?$"))
@@ -844,6 +846,15 @@
 		(c-newline-and-indent))
 	    ))))
 
+    ;; Emulate `electric-pair-mode'.
+    (when (and (boundp 'electric-pair-mode)
+	       electric-pair-mode
+	       (eq last-command-event ?{))
+      (save-excursion
+	(newline)
+	(let ((last-command-event ?}))
+	  (c-electric-brace nil))))
+
     ;; blink the paren
     (and (eq (c-last-command-char) ?\})
 	 (not executing-kbd-macro)

-- 
Alan Mackenzie (Nuremberg, Germany).