emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [patch] make electric-pair-mode smarter/more useful


From: João Távora
Subject: Re: [patch] make electric-pair-mode smarter/more useful
Date: Thu, 12 Dec 2013 17:06:18 +0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

Stefan Monnier <address@hidden> 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 via email to

[Prev in Thread] Current Thread [Next in Thread]