[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Can we add a delete-match function to subr(-x).el?
From: |
Paul W. Rankin |
Subject: |
Re: Can we add a delete-match function to subr(-x).el? |
Date: |
Sun, 26 Jan 2020 03:18:19 +1000 |
User-agent: |
mu4e 1.2.0; emacs 28.0.50 |
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)))