From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: joaotavora@gmail.com (=?utf-8?B?Sm/Do28gVMOhdm9yYQ==?=) Newsgroups: gmane.emacs.devel Subject: Re: [patch] make electric-pair-mode smarter/more useful Date: Thu, 12 Dec 2013 17:06:18 +0000 Message-ID: <87r49if185.fsf@gmail.com> References: <87haalh806.fsf@gmail.com> <87d2l9wfne.fsf@yandex.ru> <87fvq49xzp.fsf@gmail.com> <87vbyuwyyc.fsf@gmail.com> <52A93B99.8040308@yandex.ru> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1386868072 29437 80.91.229.3 (12 Dec 2013 17:07:52 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 12 Dec 2013 17:07:52 +0000 (UTC) Cc: emacs-devel@gnu.org, Dmitry Gutov To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Dec 12 18:07:57 2013 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Vr9jw-0001XC-32 for ged-emacs-devel@m.gmane.org; Thu, 12 Dec 2013 18:07:56 +0100 Original-Received: from localhost ([::1]:37782 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vr9jv-0003CS-JE for ged-emacs-devel@m.gmane.org; Thu, 12 Dec 2013 12:07:55 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:36397) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vr9jp-0003CE-4D for emacs-devel@gnu.org; Thu, 12 Dec 2013 12:07:54 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Vr9jk-0002se-39 for emacs-devel@gnu.org; Thu, 12 Dec 2013 12:07:49 -0500 Original-Received: from mail-wg0-x22c.google.com ([2a00:1450:400c:c00::22c]:39021) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vr9jj-0002sY-OJ for emacs-devel@gnu.org; Thu, 12 Dec 2013 12:07:44 -0500 Original-Received: by mail-wg0-f44.google.com with SMTP id a1so743776wgh.35 for ; Thu, 12 Dec 2013 09:07:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type; bh=Z6h+MHcf3Hc7WLNTXF41CVaulzOJCb6PiX4OF3eO8Rg=; b=ezYrzzhn897yzyVR7FjTRWlDtISL6uPl5JehQuWz9cHokuZ7oUnNX3QxB/LdCARHih unMuwEv7CjSemXoMO436KJYTtMCNRxcuKa98Y6x9k0wxGP+JeJ3aJ2sXEZwO8zppZIbA fHakKJstPrF3Fo1lwBmRkP8AijyThU9Dsqwdst/ILdeaqCC6FX4PqhW03XuN8XcGzXIL f0lvLu7bU6+6gXY06ymz8sYFfOU1tu9Q0YJccPWkmXADRW0g5jsqfV43ubHih26UgXoj Tz4q5AdecDZnB97ogVCsEGY7QW3h++tSqhbrm0VvO0QdPb9ZSShQxa98607C9vThB3O7 cwAA== X-Received: by 10.194.58.136 with SMTP id r8mr7777539wjq.4.1386868062782; Thu, 12 Dec 2013 09:07:42 -0800 (PST) Original-Received: from kitaj.yourcompany.com (66.207.108.93.rev.vodafone.pt. [93.108.207.66]) by mx.google.com with ESMTPSA id hb7sm21422236wib.6.2013.12.12.09.07.40 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 12 Dec 2013 09:07:41 -0800 (PST) In-Reply-To: (Stefan Monnier's message of "Thu, 12 Dec 2013 11:30:53 -0500") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:400c:c00::22c X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:166338 Archived-At: Stefan Monnier 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.