unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: joaotavora@gmail.com (João Távora)
To: Stefan Monnier <monnier@IRO.UMontreal.CA>
Cc: emacs-devel@gnu.org, Dmitry Gutov <dgutov@yandex.ru>
Subject: Re: [patch] make electric-pair-mode smarter/more useful
Date: Thu, 12 Dec 2013 17:06:18 +0000	[thread overview]
Message-ID: <87r49if185.fsf@gmail.com> (raw)
In-Reply-To: <jwvd2l28257.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Thu, 12 Dec 2013 11:30:53 -0500")

Stefan Monnier <monnier@IRO.UMontreal.CA> writes:

> As explained, electric-layout-mode is directly inspired from cc-mode's
> corresponding feature.  In C modes and friends, you rarely (if ever)
> write "{ }", so there was no need for such a thing.

By the way, this is unrelated, but c-electric-backspace breaks the
`autobackspacing` feature

> I think it makes sense.  I'd welcome an extension of
> electric-layout-mode which provides such a functionality.

Is something like this what you had in mind? Notice the
`electric-indent-mode' gotcha (described in another one of your FIXME's)

diff --git a/lisp/electric.el b/lisp/electric.el
index b227e3d..b852e5e 100644
--- a/lisp/electric.el
+++ b/lisp/electric.el
@@ -767,12 +767,12 @@ See options `electric-pair-pairs' and `electric-pair-skip-self'."

 ;;; Electric newlines after/before/around some chars.

-(defvar electric-layout-rules '()
+(defvar electric-layout-rules `((?\n . ,#'electric-pair-newline-between-pairs-rule))
   "List of rules saying where to automatically insert newlines.
-Each rule has the form (CHAR . WHERE) where CHAR is the char
-that was just inserted and WHERE specifies where to insert newlines
-and can be: nil, `before', `after', `around', or a function of no
-arguments that returns one of those symbols.")
+Each rule has the form (CHAR . WHERE) where CHAR is the char that
+was just inserted and WHERE specifies where to insert newlines
+and can be: nil, `before', `after', `around', `after-stay', or a
+function of no arguments that returns one of those symbols.")

 (defun electric-layout-post-self-insert-function ()
   (let* ((rule (cdr (assq last-command-event electric-layout-rules)))
@@ -781,23 +781,45 @@ arguments that returns one of those symbols.")
                (setq pos (electric--after-char-pos))
                ;; Not in a string or comment.
                (not (nth 8 (save-excursion (syntax-ppss pos)))))
-      (let ((end (copy-marker (point) t)))
+      (let ((end (copy-marker (point)))
+            (sym (if (functionp rule) (funcall rule) rule)))
+        (set-marker-insertion-type end (not (eq sym 'after-stay)))
         (goto-char pos)
-        (pcase (if (functionp rule) (funcall rule) rule)
+        (case 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"))      ; FIXME: check eolp before inserting \n?
+          (`after  (insert "\n"))
+          ;; FIXME: indenting here is a no-no, but see the beginning
+          ;; note in `electric-indent-post-self-insert-function'. We
+          ;; have to find someway to notify that function that we
+          ;; affected more text than just the one between `pos' and
+          ;; `end'.
+          (`after-stay (save-excursion
+                         (insert "\n")
+                         (if electric-indent-mode
+                             (indent-according-to-mode))))
           (`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)))))

+(defun electric-pair-newline-between-pairs-rule ()
+  (when (and electric-pair-mode
+             (not (eobp))
+             (eq (save-excursion
+                   (skip-chars-backward "\n\t ")
+                   (char-before))
+                 (electric-pair--pair-of (char-after))))
+    'after-stay))
+
 ;;;###autoload
 (define-minor-mode electric-layout-mode
   "Automatically insert newlines around some chars.



  reply	other threads:[~2013-12-12 17:06 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-06 23:31 [patch] make electric-pair-mode smarter/more useful João Távora
2013-12-07  2:09 ` Leo Liu
2013-12-07  2:36 ` Dmitry Gutov
2013-12-07 21:01   ` João Távora
2013-12-07 23:16     ` Stefan Monnier
2013-12-12  3:05       ` João Távora
2013-12-12  4:29         ` Dmitry Gutov
2013-12-12 11:26           ` João Távora
2013-12-12 16:30           ` Stefan Monnier
2013-12-12 17:06             ` João Távora [this message]
2013-12-12 20:12               ` Stefan Monnier
2013-12-13  2:55               ` Dmitry Gutov
2013-12-14 15:18                 ` Stefan Monnier
2013-12-14 16:56                   ` Dmitry Gutov
2013-12-15  1:39                     ` Stefan Monnier
2013-12-16  0:35                       ` João Távora
2013-12-16  3:34                         ` Stefan Monnier
2013-12-16 19:26                           ` João Távora
2013-12-17  1:54                             ` Stefan Monnier
2013-12-18  2:43                               ` João Távora
2013-12-18 15:32                                 ` João Távora
2013-12-23 14:41                                   ` João Távora
2013-12-24 14:29                                     ` Bozhidar Batsov
2013-12-07 23:07 ` Stefan Monnier
2013-12-12  3:01   ` João Távora
2013-12-12 18:08     ` Stefan Monnier
2013-12-13  1:02       ` João Távora
2013-12-13  2:32         ` Stefan Monnier
2013-12-15 22:10           ` João Távora
2013-12-16  3:22             ` Stefan Monnier
2013-12-16 14:21               ` João Távora
2013-12-16 15:30                 ` Stefan Monnier
2013-12-16 18:40                   ` Stefan Monnier
2013-12-16 19:06                     ` João Távora
2013-12-17  1:42                       ` Stefan Monnier
     [not found]                   ` <CALDnm52AoShN891-L9=Cbng98UtYPEntzO+n_XDMmEL+UV0r-A@mail.gmail.com>
2013-12-16 19:02                     ` Fwd: " João Távora

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r49if185.fsf@gmail.com \
    --to=joaotavora@gmail.com \
    --cc=dgutov@yandex.ru \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@IRO.UMontreal.CA \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).