From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eshel Yaron via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#72437: Unexpected behavior when inserting with prefix arg in electric-pair-mode Date: Sat, 21 Sep 2024 12:23:55 +0200 Message-ID: References: <87le1edqqm.fsf@breatheoutbreathe.in> <86h6c2ug0p.fsf@gnu.org> <86a5htvclm.fsf@gnu.org> <86zfpttpfl.fsf@gnu.org> <20F64DCD-929F-4B4C-BF85-ABE26FC7C995@breatheoutbreathe.in> <87plp0w7g7.fsf@breatheoutbreathe.in> Reply-To: Eshel Yaron Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="29895"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 72437@debbugs.gnu.org, eliz@gnu.org, monnier@iro.umontreal.ca To: Joseph Turner Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Sep 21 12:24:54 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1srxIM-0007dI-HQ for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 21 Sep 2024 12:24:54 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1srxIB-0008Ut-6v; Sat, 21 Sep 2024 06:24:43 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1srxIA-0008Uj-AT for bug-gnu-emacs@gnu.org; Sat, 21 Sep 2024 06:24:42 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1srxIA-0004cE-1p for bug-gnu-emacs@gnu.org; Sat, 21 Sep 2024 06:24:42 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:References:In-Reply-To:From:To:Subject; bh=e8g6sKTnS217kH/o7IWl1R5Ic4p0VDjYpZaErM2oWzc=; b=a5F/TtUtCJ3rVc+NgaYoOhkRqa1u8XB30arm8rqozihugQZle23Ne7P/4SqCllitFwaitUS7G9Z2SUMubDB83Zth0BJmkSqHUO4XI/fGWTXgPUecpVtQweAYZAYnrKR/L7mgnRGpUTUb+OOzq6/8t90iLtJRRnwKycxIhAGtRvseZNGkg+CioYcJx+LhcKipFdg4Vy0RzjpHsyjrpFqHp7ROsLFkpVNxMt1qKiOwVnWMj/v6pJOokk5+RsBKKWusPgkXga2xrJ0EpdS6QOfbXyLaZkkqo6E9nqMVNij3R8TKT5gLueNR2UVJ3bw7qZWovmXF4bsqYxK3XnTt9zefIg==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1srxIU-0005oK-2S for bug-gnu-emacs@gnu.org; Sat, 21 Sep 2024 06:25:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Eshel Yaron Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 21 Sep 2024 10:25:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 72437 X-GNU-PR-Package: emacs X-Debbugs-Original-Cc: 72437@debbugs.gnu.org, "Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors" , Eli Zaretskii , Stefan Monnier Original-Received: via spool by submit@debbugs.gnu.org id=B.172691426522165 (code B ref -1); Sat, 21 Sep 2024 10:25:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 21 Sep 2024 10:24:25 +0000 Original-Received: from localhost ([127.0.0.1]:37271 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1srxHs-0005lJ-HX for submit@debbugs.gnu.org; Sat, 21 Sep 2024 06:24:25 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:44186) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1srxHr-0005lC-60 for submit@debbugs.gnu.org; Sat, 21 Sep 2024 06:24:23 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1srxHW-0008SH-PI for bug-gnu-emacs@gnu.org; Sat, 21 Sep 2024 06:24:02 -0400 Original-Received: from mail.eshelyaron.com ([107.175.124.16] helo=eshelyaron.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1srxHU-0004NU-PV; Sat, 21 Sep 2024 06:24:02 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=eshelyaron.com; s=mail; t=1726914238; bh=ssc4kCf3kiM5BK6rN2Qnogf4vLDs5MTJqNfmvWPgixA=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=Ble6x4DfXLid9SMJ+8mvHJ1x3HrpepyFmcKb3Tq4ATbnl0+C+rWGiBhVOzFdvPsdu kVcQexvQrqZNUuUTkkvTl8oUYebc9xsnVG0hYSX1CI/gdCOfc6QBOdJmwd63zqQKqz hB6Sgqt+NuODkT6xDjuhS9fIW+Q0fcQz8nlvKtiWebwL4RtUhvI9M3vTSn30Ii3hUo NubHDzC4faXz+D0C1maUXwpphjVJSdCkfM8LHqZi36HfCWiTN+3osQRfDUNKooxU1M 0fDRp/RB9e81DvrTOy6p0YZCufd0x0a6OxFk0Vu82bSWWwc0BKddhCkqx4I4YyTl/V +BMgwxMM++qLA== In-Reply-To: <87plp0w7g7.fsf@breatheoutbreathe.in> (Joseph Turner's message of "Wed, 18 Sep 2024 22:14:48 -0700") Received-SPF: pass client-ip=107.175.124.16; envelope-from=me@eshelyaron.com; helo=eshelyaron.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list 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-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:292173 Archived-At: --=-=-= Content-Type: text/plain Joseph Turner writes: > Joseph Turner writes: > >> On August 13, 2024 10:07:23 PM PDT, Eshel Yaron wrote: >>>Hi Stefan, >>> >>>Stefan Monnier writes: >>> >>>>> Let's hear what Stefan thinks (and anyone else who has an >>>>> opinion or some case other than electric-pair-mode). >>>> >>>> I think the requested behavior makes a lot of sense, but I lost touch >>>> with the way elec-pair works and interacts with other electric modes, so >>>> I'm not sure how to make it work without breaking other things. >>> >>>In case you missed it, the patch I shared upthread might give you >>>some ideas. It implements the requested behavior, safely, I think. >>>So I would just ask Joseph to try it out for a while and see if it >>>works as expected and if any other issues crop up :) >> >> Thanks for this patch! I am testing it in my daily config. So far >> so good, and I'll report back after a few weeks of usage. > > I have been running this patch for over a month with no issue. That's great, thanks for testing it and for reporting back! > I vote to merge it! Agreed. Here's a full patch with tests, doc updates and a NEWS entry: --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Teach-Electric-Pair-mode-about-prefix-arguments.patch >From 8e732a6db9cc2855c2b0f307129555bb2551ff96 Mon Sep 17 00:00:00 2001 From: Eshel Yaron Date: Sat, 21 Sep 2024 12:15:16 +0200 Subject: [PATCH] Teach Electric Pair mode about prefix arguments * lisp/elec-pair.el (electric-pair--insert): New arg TIMES. (electric-pair-inhibit-if-helps-balance) (electric-pair-post-self-insert-function) (electric-pair-open-newline-between-pairs-psif) (electric-pair-skip-if-helps-balance): Respect prefix arg. (electric-pair-delete-pair): Delete ARG chars, not just 1. * test/lisp/electric-tests.el (autowrapping-multi-1) (autowrapping-multi-2): New tests. * doc/emacs/programs.texi (Matching): Mention prefix arg support in Electric Pair mode documentation. * etc/NEWS: Announce it. --- doc/emacs/programs.texi | 4 +- etc/NEWS | 6 +++ lisp/elec-pair.el | 87 ++++++++++++++++++++----------------- test/lisp/electric-tests.el | 16 +++++++ 4 files changed, 72 insertions(+), 41 deletions(-) diff --git a/doc/emacs/programs.texi b/doc/emacs/programs.texi index 42b57143bf1..948f4128acf 100644 --- a/doc/emacs/programs.texi +++ b/doc/emacs/programs.texi @@ -1061,7 +1061,9 @@ Matching over. If the region is active (@pxref{Mark}), insertion of a delimiter operates on the region: the characters in the region are enclosed in a pair of matching delimiters, leaving point after the -delimiter you typed. +delimiter you typed. If you provide a prefix argument when inserting +a delimiter, the numeric value of that prefix argument specifies the +number of pairs to insert. These variables control additional features of Electric Pair mode: diff --git a/etc/NEWS b/etc/NEWS index b902aaf7ead..f22215851a6 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -147,6 +147,12 @@ instead of raising an error. Note that if you have disabled Transient Mark mode you might prefer to use 'unix-word-rubout', as this feature relies on there being an active region. ++++ +** Electric Pair mode can now pair multiple delimiters at once. +You can now insert or wrap text with multiple sets of parentheses and +other matching delimiters at once with Electric Pair mode, by providing +a prefix argument when inserting one of the delimiters. + * Changes in Specialized Modes and Packages in Emacs 31.1 diff --git a/lisp/elec-pair.el b/lisp/elec-pair.el index 40618e9ef38..1fae0ba6ba4 100644 --- a/lisp/elec-pair.el +++ b/lisp/elec-pair.el @@ -260,7 +260,7 @@ electric-pair-syntax-info (list ?\( (cdr direct) t string-or-comment))) (reverse (list ?\) (car reverse) t string-or-comment))))) -(defun electric-pair--insert (char) +(defun electric-pair--insert (char times) (let ((last-command-event char) (blink-matching-paren nil) (electric-pair-mode nil) @@ -271,7 +271,7 @@ electric-pair--insert ;; us to add these newlines, and is probably about to kick in ;; again after we add the closer. (electric-layout-allow-duplicate-newlines t)) - (self-insert-command 1))) + (self-insert-command times))) (defun electric-pair--syntax-ppss (&optional pos where) "Like `syntax-ppss', but sometimes fallback to `parse-partial-sexp'. @@ -455,7 +455,8 @@ electric-pair-inhibit-if-helps-balance (atomic-change-group ;; Don't use `delete-char'; that may modify the head of the ;; undo list. - (delete-region (point) (1- (point))) + (delete-region (- (point) (prefix-numeric-value current-prefix-arg)) + (point)) (throw 'done (cond ((eq ?\( syntax) @@ -474,25 +475,26 @@ electric-pair-skip-if-helps-balance Works by first removing the character from the buffer, then doing some list calculations, finally restoring the situation as if nothing happened." - (pcase (electric-pair-syntax-info char) - (`(,syntax ,pair ,_ ,s-or-c) - (unwind-protect - (progn - (delete-char -1) - (cond ((eq syntax ?\)) - (let* ((pair-data - (electric-pair--balance-info - -1 s-or-c)) - (innermost (car pair-data)) - (outermost (cdr pair-data))) - (and - (cond ((car outermost) - (car innermost)) - ((car innermost) - (not (eq (cdr outermost) pair))))))) - ((eq syntax ?\") - (electric-pair--inside-string-p char)))) - (insert char))))) + (let ((num (prefix-numeric-value current-prefix-arg))) + (pcase (electric-pair-syntax-info char) + (`(,syntax ,pair ,_ ,s-or-c) + (unwind-protect + (progn + (delete-char (- num)) + (cond ((eq syntax ?\)) + (let* ((pair-data + (electric-pair--balance-info + (- num) s-or-c)) + (innermost (car pair-data)) + (outermost (cdr pair-data))) + (and + (cond ((car outermost) + (car innermost)) + ((car innermost) + (not (eq (cdr outermost) pair))))))) + ((eq syntax ?\") + (electric-pair--inside-string-p char)))) + (insert (make-string num char))))))) (defun electric-pair-default-skip-self (char) (if electric-pair-preserve-balance @@ -527,11 +529,14 @@ electric-pair-post-self-insert-function `electric-pair-inhibit-predicate', `electric-pair-skip-self' and `electric-pair-skip-whitespace' (which see)." (let* ((pos (and electric-pair-mode (electric--after-char-pos))) + (num (when pos (prefix-numeric-value current-prefix-arg))) + (beg (when num (- pos num))) (skip-whitespace-info)) (pcase (electric-pair-syntax-info last-command-event) (`(,syntax ,pair ,unconditional ,_) (cond ((null pos) nil) + ((zerop num) nil) ;; Wrap a pair around the active region. ;; ((and (memq syntax '(?\( ?\) ?\" ?\$)) (use-region-p)) @@ -545,23 +550,24 @@ electric-pair-post-self-insert-function (>= (mark) (point)))) (save-excursion (goto-char (mark)) - (electric-pair--insert pair)) - (delete-region pos (1- pos)) - (electric-pair--insert pair) + (electric-pair--insert pair num)) + (delete-region beg pos) + (electric-pair--insert pair num) (goto-char (mark)) - (electric-pair--insert last-command-event))) + (electric-pair--insert last-command-event num))) ;; Backslash-escaped: no pairing, no skipping. ((save-excursion - (goto-char (1- pos)) + (goto-char beg) (not (zerop (% (skip-syntax-backward "\\") 2)))) - nil) + (let ((current-prefix-arg (1- num))) + (electric-pair-post-self-insert-function))) ;; Skip self. ((and (memq syntax '(?\) ?\" ?\$)) (and (or unconditional (if (functionp electric-pair-skip-self) (electric-pair--save-literal-point-excursion - (goto-char pos) - (funcall electric-pair-skip-self last-command-event)) + (goto-char pos) + (funcall electric-pair-skip-self last-command-event)) electric-pair-skip-self)) (save-excursion (when (and (not (and unconditional @@ -580,19 +586,19 @@ electric-pair-post-self-insert-function ;; live with it for now. (when skip-whitespace-info (funcall electric-pair-skip-whitespace-function)) - (delete-region (1- pos) (if (eq skip-whitespace-info 'chomp) - (point) - pos)) - (forward-char)) + (delete-region beg (if (eq skip-whitespace-info 'chomp) + (point) + pos)) + (forward-char num)) ;; Insert matching pair. ((and (memq syntax '(?\( ?\" ?\$)) (not overwrite-mode) (or unconditional (not (electric-pair--save-literal-point-excursion - (goto-char pos) - (funcall electric-pair-inhibit-predicate - last-command-event))))) - (save-excursion (electric-pair--insert pair)))))))) + (goto-char pos) + (funcall electric-pair-inhibit-predicate + last-command-event))))) + (save-excursion (electric-pair--insert pair num)))))))) (defun electric-pair-open-newline-between-pairs-psif () "Honor `electric-pair-open-newline-between-pairs'. @@ -604,7 +610,8 @@ electric-pair-open-newline-between-pairs-psif (< (1+ (point-min)) (point) (point-max)) (eq (save-excursion (skip-chars-backward "\t\s") - (char-before (1- (point)))) + (char-before (- (point) + (prefix-numeric-value current-prefix-arg)))) (matching-paren (char-after)))) (save-excursion (newline 1 t)))) @@ -618,7 +625,7 @@ electric-pair-delete-pair ARG and KILLP are passed directly to `backward-delete-char-untabify', which see." (interactive "*p\nP") - (delete-char 1) + (delete-char arg) (backward-delete-char-untabify arg killp)) (defvar electric-pair-mode-map diff --git a/test/lisp/electric-tests.el b/test/lisp/electric-tests.el index 720313511a6..76c18594b26 100644 --- a/test/lisp/electric-tests.el +++ b/test/lisp/electric-tests.el @@ -653,6 +653,22 @@ autowrapping-7 (skip-chars-backward "\"") (mark-sexp -1))) +(define-electric-pair-test autowrapping-multi-1 + "foo" "(" :expected-string "(((((foo)))))" :expected-point 6 + :bindings '((current-prefix-arg . 5)) + :fixture-fn (lambda () + (electric-pair-mode 1) + (mark-sexp 1))) + +(define-electric-pair-test autowrapping-multi-2 + "foo" ")" :expected-string "(((((foo)))))" :expected-point 14 + :bindings '((current-prefix-arg . 5)) + :fixture-fn (lambda () + (electric-pair-mode 1) + (goto-char (point-max)) + (skip-chars-backward "\"") + (mark-sexp -1))) + ;;; Electric quotes (define-electric-pair-test electric-quote-string -- 2.46.0 --=-=-=--