unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Can we add a delete-match function to subr(-x).el?
@ 2020-01-25  4:55 Paul W. Rankin
  2020-01-25  5:23 ` Stefan Kangas
  2020-01-25 15:04 ` Stefan Monnier
  0 siblings, 2 replies; 5+ messages in thread
From: Paul W. Rankin @ 2020-01-25  4:55 UTC (permalink / raw)
  To: Emacs Devel

Hello Emacizens,

I frequently forget that Emacs doesn't include a delete-match function
to be called after (re-)search-forward or the like.

Can we add something like the following to subr(-x).el?

    (defun delete-match (&optional subexp)
      "Delete text matched by last search.

    Optional argument SUBEXP specifies the subexpression to delete,
    or delete the entire match."
      (when (consp (match-data))
        (unless group (setq subexp 0))
        (delete-region (match-beginning subexp) (match-end subexp))))



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

* Re: Can we add a delete-match function to subr(-x).el?
  2020-01-25  4:55 Can we add a delete-match function to subr(-x).el? Paul W. Rankin
@ 2020-01-25  5:23 ` Stefan Kangas
  2020-01-25  5:32   ` Paul W. Rankin
  2020-01-25 15:04 ` Stefan Monnier
  1 sibling, 1 reply; 5+ messages in thread
From: Stefan Kangas @ 2020-01-25  5:23 UTC (permalink / raw)
  To: Paul W. Rankin; +Cc: Emacs Devel

Paul W. Rankin <hello@paulwrankin.com> writes:

> I frequently forget that Emacs doesn't include a delete-match function
> to be called after (re-)search-forward or the like.
>
> Can we add something like the following to subr(-x).el?
>
>     (defun delete-match (&optional subexp)
>       "Delete text matched by last search.
>
>     Optional argument SUBEXP specifies the subexpression to delete,
>     or delete the entire match."
>       (when (consp (match-data))
>         (unless group (setq subexp 0))
>         (delete-region (match-beginning subexp) (match-end subexp))))

I think adding that would be convenient.

You have a typo in the code btw, should say:
    (unless subexp (setq subexp 0))

Best regards,
Stefan Kangas



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

* Re: Can we add a delete-match function to subr(-x).el?
  2020-01-25  5:23 ` Stefan Kangas
@ 2020-01-25  5:32   ` Paul W. Rankin
  0 siblings, 0 replies; 5+ messages in thread
From: Paul W. Rankin @ 2020-01-25  5:32 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: Emacs Devel


On Sat, Jan 25 2020, Stefan Kangas wrote:
>
> You have a typo in the code btw, should say:
>     (unless subexp (setq subexp 0))

Whoops, thanks! I'd been using this in my init for a while with "group"
and too quickly made the change for Emaconsistency.

    (defun delete-match (&optional subexp)
      "Delete text matched by last search.

    Optional argument SUBEXP specifies the subexpression to delete,
    or delete the entire match."
      (when (consp (match-data))
        (unless subexp (setq subexp 0))
        (delete-region (match-beginning subexp) (match-end subexp))))




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

* Re: Can we add a delete-match function to subr(-x).el?
  2020-01-25  4:55 Can we add a delete-match function to subr(-x).el? Paul W. Rankin
  2020-01-25  5:23 ` Stefan Kangas
@ 2020-01-25 15:04 ` Stefan Monnier
  2020-01-25 17:18   ` Paul W. Rankin
  1 sibling, 1 reply; 5+ messages in thread
From: Stefan Monnier @ 2020-01-25 15:04 UTC (permalink / raw)
  To: Paul W. Rankin; +Cc: Emacs Devel

>       (when (consp (match-data))

This allocates N cons cells just to test if there's something and in
practice there's basically *always* something (the match data keeps
info about the last successful match, so even if your last match was
a failure, `match-data` won't be nil]).


        Stefan




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

* Re: Can we add a delete-match function to subr(-x).el?
  2020-01-25 15:04 ` Stefan Monnier
@ 2020-01-25 17:18   ` Paul W. Rankin
  0 siblings, 0 replies; 5+ messages in thread
From: Paul W. Rankin @ 2020-01-25 17:18 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Emacs Devel

On Sun, Jan 26 2020, Stefan Monnier wrote:
>>       (when (consp (match-data))
>
> This allocates N cons cells just to test if there's something and in
> practice there's basically *always* something (the match data keeps
> info about the last successful match, so even if your last match was
> a failure, `match-data` won't be nil]).

I figured this was a waste, just wanted it to look more thorough.

Would this be better as a macro?

    (defmacro delete-match (&optional subexp)
      "Delete text matched by last search.

    Optional argument SUBEXP specifies the subexpression to delete,
    or delete the entire match."
      (list 'unless subexp (list 'setq subexp 0))
      (list 'delete-region (list 'match-beginning subexp)
                           (list 'match-end subexp)))

I like this because of this:

    (macroexpand-all
     '(while (re-search-forward "foo\\(ba[rz]\\)" nil t)
        (delete-match 1)))

    => (while (re-search-forward "foo\\(ba[rz]\\)" nil t)
         (delete-region (match-beginning 1) (match-end 1)))



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

end of thread, other threads:[~2020-01-25 17:18 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-25  4:55 Can we add a delete-match function to subr(-x).el? Paul W. Rankin
2020-01-25  5:23 ` Stefan Kangas
2020-01-25  5:32   ` Paul W. Rankin
2020-01-25 15:04 ` Stefan Monnier
2020-01-25 17:18   ` Paul W. Rankin

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).