From: Karthik Chikmagalur <karthikchikmagalur@gmail.com>
To: 48009@debbugs.gnu.org
Subject: bug#48009: 28.0.50; Support query-regexp-replace using re-builder
Date: Sun, 02 May 2021 20:42:35 -0700 [thread overview]
Message-ID: <874kfkh49g.fsf@gmail.com> (raw)
In-Reply-To: <d285a7fb-f7bd-545f-1d1b-7ed4f9abb461@orcon.net.nz>
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
prev parent reply other threads:[~2021-05-03 3:42 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-25 3:36 bug#48009: 28.0.50; Support query-regexp-replace using re-builder Phil Sainty
2021-04-25 10:46 ` Stefan Kangas
2021-04-25 14:51 ` Phil Sainty
2021-04-25 15:19 ` Stefan Kangas
2021-04-26 23:53 ` Tak Kunihiro
2021-05-03 3:42 ` Karthik Chikmagalur [this message]
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=874kfkh49g.fsf@gmail.com \
--to=karthikchikmagalur@gmail.com \
--cc=48009@debbugs.gnu.org \
/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).