From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Karthik Chikmagalur Newsgroups: gmane.emacs.bugs Subject: bug#48009: 28.0.50; Support query-regexp-replace using re-builder Date: Sun, 02 May 2021 20:42:35 -0700 Message-ID: <874kfkh49g.fsf@gmail.com> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="13434"; mail-complaints-to="usenet@ciao.gmane.io" To: 48009@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon May 03 07:03:17 2021 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 1ldQjk-0003Mo-7w for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 03 May 2021 07:03:16 +0200 Original-Received: from localhost ([::1]:56942 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ldQjj-0000gt-1C for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 03 May 2021 01:03:15 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:55252) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ldQjX-0000gl-Mf for bug-gnu-emacs@gnu.org; Mon, 03 May 2021 01:03:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:34463) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ldQjW-0005DA-3f for bug-gnu-emacs@gnu.org; Mon, 03 May 2021 01:03:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1ldQjV-0007Lx-W0 for bug-gnu-emacs@gnu.org; Mon, 03 May 2021 01:03:02 -0400 X-Loop: help-debbugs@gnu.org In-Reply-To: Resent-From: Karthik Chikmagalur Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 03 May 2021 05:03:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 48009 X-GNU-PR-Package: emacs Original-Received: via spool by 48009-submit@debbugs.gnu.org id=B48009.162001817728258 (code B ref 48009); Mon, 03 May 2021 05:03:01 +0000 Original-Received: (at 48009) by debbugs.gnu.org; 3 May 2021 05:02:57 +0000 Original-Received: from localhost ([127.0.0.1]:46009 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ldQjQ-0007Li-HX for submit@debbugs.gnu.org; Mon, 03 May 2021 01:02:57 -0400 Original-Received: from mail-pj1-f41.google.com ([209.85.216.41]:46005) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ldPTn-0006fk-6V for 48009@debbugs.gnu.org; Sun, 02 May 2021 23:42:44 -0400 Original-Received: by mail-pj1-f41.google.com with SMTP id gc22-20020a17090b3116b02901558435aec1so2543344pjb.4 for <48009@debbugs.gnu.org>; Sun, 02 May 2021 20:42:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version; bh=NPAkT40aeAujjzzths7lFAVGQtTsSW+wv6Fc10quSAA=; b=tBf4mteZ28Q7N2KMDpluRUwWq4sWBDne1ESQcMi58T+u1cczpdgdGBFmu2cFL0ggjw 4evxLglaLbDOkqC8m1XB8t8f0HZqmUsOp+FTPA881R7JYlQxPZxEmYTCdjOAF8lZOFEO TpkhJ3ghxxCcRz0Ym/rQRvTWVg2V7BsiZlemqUo5rFpp64qr8leS1ql5+s7rYhWUY00/ wvUpqSWpDR/tr52WAJb7tglHzFTVNf5wyUYTjo4L33IYw69oFkWQdpH1WHcvMzhQDqiF 0BrE9TMAOWrz+sJQtAcZe8BaWmXU3vyYgj1tqbvWuzpcO1vn8gOrqJMRyYPGTsA9mL4c 206g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version; bh=NPAkT40aeAujjzzths7lFAVGQtTsSW+wv6Fc10quSAA=; b=o9GotIVuDOlTVBOD6IXY3vfdxdNGjsIrK5GNDJaKrYkU5+pQlzL9/ld53WqjQDVLUI qG3yt2LEPLA+X2lcHmAWcg+n1a9lAqKf5wgPYc64ijpwtXtllgvbthygO6h1kyJD1qax X3P5kitSH9VynsD3I1U0PMuUkUkn2ViVbHBXcTD2Q9vsgza4hrmdbNY+YVnGYB8WHCyy YNMdwV667RAV8xFRiWUYliwN270huO8OfkG40J4cTP9MjrATLUehdZG/SPEKj3ZrBcal I5yLtDMwYucKMjLLumtjmRfO73NHBghUArwU2gnckK2TKD+cCUdg8Hkn9jph7B4Rgem4 Td9w== X-Gm-Message-State: AOAM532i/VzYY+WrnZub6tmt7M0jAebYE1fyoVUIHGdoB8HHcoqUBX8Q xUPuoVXmZUstBxaJ9GDcacYyz/WZ6Uz+TA== X-Google-Smtp-Source: ABdhPJyl0bmizXg+AMkADy31E8vQgVRnxTxqMTwkjXjfZjLkte9TF0+Me+9iwXAqqiher5nmrlSfFw== X-Received: by 2002:a17:90a:6583:: with SMTP id k3mr19249879pjj.227.1620013357006; Sun, 02 May 2021 20:42:37 -0700 (PDT) Original-Received: from localhost (c-73-231-38-92.hsd1.ca.comcast.net. [73.231.38.92]) by smtp.gmail.com with ESMTPSA id b140sm7645336pfb.98.2021.05.02.20.42.36 for <48009@debbugs.gnu.org> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 May 2021 20:42:36 -0700 (PDT) X-Mailman-Approved-At: Mon, 03 May 2021 01:02:55 -0400 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" Xref: news.gmane.io gmane.emacs.bugs:205467 Archived-At: I wrote up a version of this feature at: https://karthinks.com/software/bridging-islands-in-emacs-1/ It includes a couple of video demos. This makes using an re-builder regexp as the input to query-replace-regexp more convenient. Specifically, it does the following: 1. When re-builder is called with a region active and then used as the input to reb-replace-regexp, replacements are limited to the region. (This is the behavior of query-replace-regexp) 2. When re-builder is called with the region inactive, replacements by reb-replace-regexp are carried out from the location of point in the main buffer. By default re-builder moves the point around when constructing the regexp (incrementally), so where the replacements begin from in the main buffer is not predictable. 3. This code involves advising re-builder (to remember the region and point), re-builder can probably be modified to include restoring the point/region state as a user option. 4. Ideally, it would be great if query-replace-regexp showed incremental matches the way isearch-forward-regexp does, but I'm not familiar with how that works. One advantage to calling reb-regexp-replace from re-builder instead of query-replace-regexp is that the user can specify the regexp in the rx format. CODE: ---------------------------------- (defvar my/re-builder-positions nil "Store point and region bounds before calling re-builder") (advice-add 're-builder :before (defun my/re-builder-save-state (&rest _) "Save into `my/re-builder-positions' the point and region positions before calling `re-builder'." (setq my/re-builder-positions (cons (point) (when (region-active-p) (list (region-beginning) (region-end))))))) (defun reb-replace-regexp (&optional delimited) "Run `query-replace-regexp' with the contents of re-builder. With non-nil optional argument DELIMITED, only replace matches surrounded by word boundaries." (interactive "P") (reb-update-regexp) (let* ((re (reb-target-binding reb-regexp)) (replacement (query-replace-read-to re (concat "Query replace" (if current-prefix-arg (if (eq current-prefix-arg '-) " backward" " word") "") " regexp" (if (with-selected-window reb-target-window (region-active-p)) " in region" "")) t)) (pnt (car my/re-builder-positions)) (beg (cadr my/re-builder-positions)) (end (caddr my/re-builder-positions))) (with-selected-window reb-target-window (goto-char pnt) ; replace with (goto-char (match-beginning 0)) if you want ; to control where in the buffer the replacement starts ; with re-builder (setq my/re-builder-positions nil) (reb-quit) (query-replace-regexp re replacement delimited beg end)))) -------------------- - Karthik Chikmagalur