unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#48009: 28.0.50; Support query-regexp-replace using re-builder
@ 2021-04-25  3:36 Phil Sainty
  2021-04-25 10:46 ` Stefan Kangas
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Phil Sainty @ 2021-04-25  3:36 UTC (permalink / raw)
  To: 48009

It would be nice to be able to use `re-builder' to interactively
craft a regexp to pass to `query-replace-regexp' in a streamlined
manner.

Here's a starter, which you can call from `re-builder'.

 (defun reb-query-replace-regexp ()
   "Invoke `query-replace-regexp' in the target buffer."
   (interactive)
   (let ((from (reb-target-binding reb-regexp)))
     (with-selected-window reb-target-window
       (with-current-buffer reb-target-buffer
         (let ((to (query-replace-read-to from "Query replace regexp" t)))
           (query-replace-regexp from to))))))

 (define-key reb-mode-map (kbd "C-c %") #'reb-query-replace-regexp)
 (define-key reb-lisp-mode-map (kbd "C-c %") #'reb-query-replace-regexp)


A complete solution would presumably support the other
`query-replace-regexp' arguments.

I initially wondered about a command for "query-replace-regexp but
automatically using re-builder" (i.e. instead of typing M-C-%), but
I'm not sure that would be very different to just binding `re-builder'
to a key, as you'd still need a custom binding from inside re-builder
to say you were done.  Perhaps such a command would automatically
close the re-builder buffer once the replacements stopped, though?

What do people think?


-Phil


In GNU Emacs 28.0.50 (build 8, x86_64-pc-linux-gnu, X toolkit, cairo version 1.15.10, Xaw3d scroll bars)
 of 2021-04-18 built on shodan
Repository revision: 75c898edc3d7e06b589ce42917ae56e0c40082ac
Repository branch: feature/native-comp
Windowing system distributor 'The X.Org Foundation', version 11.0.12008000
System Description: Ubuntu 18.04.5 LTS






^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#48009: 28.0.50; Support query-regexp-replace using re-builder
  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-26 23:53 ` Tak Kunihiro
  2021-05-03  3:42 ` Karthik Chikmagalur
  2 siblings, 1 reply; 6+ messages in thread
From: Stefan Kangas @ 2021-04-25 10:46 UTC (permalink / raw)
  To: Phil Sainty; +Cc: 48009

Phil Sainty <psainty@orcon.net.nz> writes:

> It would be nice to be able to use `re-builder' to interactively
> craft a regexp to pass to `query-replace-regexp' in a streamlined
> manner.

I think it's a good idea.

> I initially wondered about a command for "query-replace-regexp but
> automatically using re-builder" (i.e. instead of typing M-C-%), but
> I'm not sure that would be very different to just binding `re-builder'
> to a key, as you'd still need a custom binding from inside re-builder
> to say you were done.  Perhaps such a command would automatically
> close the re-builder buffer once the replacements stopped, though?

Why not make `query-replace-regexp' work exactly like isearch does,
i.e. interactively update its matches?  I.e. why should users need to
know about `re-builder' to take advantage of this?

Something like that could be added in addition to a command to go from
`re-builder' to `query-replace-regexp'.





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#48009: 28.0.50; Support query-regexp-replace using re-builder
  2021-04-25 10:46 ` Stefan Kangas
@ 2021-04-25 14:51   ` Phil Sainty
  2021-04-25 15:19     ` Stefan Kangas
  0 siblings, 1 reply; 6+ messages in thread
From: Phil Sainty @ 2021-04-25 14:51 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: 48009

On 25/04/21 10:46 pm, Stefan Kangas wrote:
> Why not make `query-replace-regexp' work exactly like isearch does,
> i.e. interactively update its matches?  I.e. why should users need
> to know about `re-builder' to take advantage of this?
>
> Something like that could be added in addition to a command to go
> from `re-builder' to `query-replace-regexp'.

That could be very nice, although it feels like a pretty significant
complication.  Also, re-builder's interactive updates can sometimes
need a manual C-c C-u (`reb-force-update') if it gets confused (which
I think occasionally happens when the regexp is invalid?), and that
might not be great.

Or did you mean to use the isearch highlighting code, which is perhaps
more forgiving of temporary invalid states?






^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#48009: 28.0.50; Support query-regexp-replace using re-builder
  2021-04-25 14:51   ` Phil Sainty
@ 2021-04-25 15:19     ` Stefan Kangas
  0 siblings, 0 replies; 6+ messages in thread
From: Stefan Kangas @ 2021-04-25 15:19 UTC (permalink / raw)
  To: Phil Sainty; +Cc: 48009

Phil Sainty <psainty@orcon.net.nz> writes:

> That could be very nice, although it feels like a pretty significant
> complication.  Also, re-builder's interactive updates can sometimes
> need a manual C-c C-u (`reb-force-update') if it gets confused (which
> I think occasionally happens when the regexp is invalid?), and that
> might not be great.

True, it would need to be robust enough to handle most of what you throw
at it.  But even with some warts, it would be a significant improvement,
I think.  If it is optional, users run less risk of being caught
unawares.

A naive approach would be to run something akin to `reb-force-update'
automatically after some delay, but I have no idea if that would work in
practice.

> Or did you mean to use the isearch highlighting code, which is perhaps
> more forgiving of temporary invalid states?

I'm not familiar with the code, so I can't help with ideas for how to
best implement something like this, unfortunately.





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#48009: 28.0.50; Support query-regexp-replace using re-builder
  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-26 23:53 ` Tak Kunihiro
  2021-05-03  3:42 ` Karthik Chikmagalur
  2 siblings, 0 replies; 6+ messages in thread
From: Tak Kunihiro @ 2021-04-26 23:53 UTC (permalink / raw)
  To: 48009

> It would be nice to be able to use `re-builder' to interactively
> craft a regexp to pass to `query-replace-regexp' in a streamlined
> manner.

I think it is a good idea. Also pass to isearch-forward-regexp
or vr/query-replace is handy too.





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#48009: 28.0.50; Support query-regexp-replace using re-builder
  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-26 23:53 ` Tak Kunihiro
@ 2021-05-03  3:42 ` Karthik Chikmagalur
  2 siblings, 0 replies; 6+ messages in thread
From: Karthik Chikmagalur @ 2021-05-03  3:42 UTC (permalink / raw)
  To: 48009

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





^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-05-03  3:42 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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

unofficial mirror of bug-gnu-emacs@gnu.org 

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://yhetil.org/emacs-bugs/0 emacs-bugs/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 emacs-bugs emacs-bugs/ https://yhetil.org/emacs-bugs \
		bug-gnu-emacs@gnu.org
	public-inbox-index emacs-bugs

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.yhetil.org/yhetil.emacs.bugs
	nntp://news.gmane.io/gmane.emacs.bugs


code repositories for project(s) associated with this inbox:

	https://git.savannah.gnu.org/cgit/emacs.git

AGPL code for this site: git clone http://ou63pmih66umazou.onion/public-inbox.git