[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#48009: 28.0.50; Support query-regexp-replace using re-builder
From: |
Karthik Chikmagalur |
Subject: |
bug#48009: 28.0.50; Support query-regexp-replace using re-builder |
Date: |
Sun, 02 May 2021 20:42:35 -0700 |
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 in Thread] |
Current Thread |
[Next in Thread] |
- bug#48009: 28.0.50; Support query-regexp-replace using re-builder,
Karthik Chikmagalur <=