Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Restricting regex search to region

9 views
Skip to first unread message

Loris Bennett

unread,
Dec 21, 2021, 3:40:26 AM12/21/21
to
Hi,

Recently I have been receiving more an more emails which, to me, contain
an excessive number of blank lines. When I reply to these using Gnus,
these blank lines become quoted with '>'.

I would therefore like to be able to mark a region and remove such
lines. This is what I have so far:

(defun lb/remove-quoted-empty-lines ()
"Remove superfluous quoted empty lines in reply"
(interactive)
(save-excursion
(goto-char (region-beginning))
(while (re-search-forward "^>\s*\n" nil t)
(replace-match "" t nil))))

However, this does not stop at the end of the region, but does the
replacement in the remainder of the buffer.

What do I need to add to restrict the replacement to the region?

Cheers,

Loris

Marco Wahl

unread,
Dec 21, 2021, 6:33:10 AM12/21/21
to
What about

(re-search-forward "^>\s*\n" (region-end) t)

?


Loris Bennett

unread,
Dec 21, 2021, 7:05:12 AM12/21/21
to
Hi Marco,
Ah, thanks. That's pretty obvious. My at the best of times mediocre
coding skills seem to have left early for the holidays :-/

Cheers,

Loris

--
This signature is currently under construction.

Axel Reichert

unread,
Dec 21, 2021, 12:59:17 PM12/21/21
to
Loris Bennett <loris....@fu-berlin.de> writes:

> emails which, to me, contain an excessive number of blank lines. When
> I reply to these using Gnus, these blank lines become quoted with '>'.

First wash, then reply:

https://www.gnu.org/software/emacs/manual/html_node/gnus/Article-Washing.html

W E A

or

W E a

will probably do the job.

And if not:

https://www.gnu.org/software/emacs/manual/html_node/emacs/Replace.html

states that replacements act on the region if active.

Best regards

Axel

Loris Bennett

unread,
Dec 22, 2021, 2:49:38 AM12/22/21
to
Axel Reichert <ma...@axel-reichert.de> writes:

> Loris Bennett <loris....@fu-berlin.de> writes:
>
>> emails which, to me, contain an excessive number of blank lines. When
>> I reply to these using Gnus, these blank lines become quoted with '>'.
>
> First wash, then reply:
>
> https://www.gnu.org/software/emacs/manual/html_node/gnus/Article-Washing.html
>
> W E A
>
> or
>
> W E a
>
> will probably do the job.

Thanks for reminding me about washing. Unfortunately W E A or W E a
don't quite do what I want. It seems to me that each CR(?) has been
doubled. If I use C-u g to display the raw message, the line spacing is
as I would expect.

Maybe I should ask on the Gnus ML about custom washing functions.

> And if not:
>
> https://www.gnu.org/software/emacs/manual/html_node/emacs/Replace.html
>
> states that replacements act on the region if active.

True, but if I use re-search-forward in my own function, I still have
to set the argument 'limit-of-search' properly, as Marco pointed out.

--
Dr. Loris Bennett (Herr/Mr)
ZEDAT, Freie Universität Berlin Email loris....@fu-berlin.de

Steve G

unread,
Mar 24, 2022, 11:16:34 AM3/24/22
to
It is called `save-restriction'. It is used with a narrowed-buffer. It
can be confusing, I have written the following that I use. I use the
prefix `si:' for convience when I use the minibuffer for expandsion -
stands for system interface...

(defun si:delete-blank-lines ()
"Delete all blank lines from buffer or active region."
(interactive)
(cond ((region-active-p)
(save-excursion
(goto-char (region-end))
(while (not (bobp))
(forward-line -1)
(while (looking-at-p "^$")
(delete-blank-lines)))))
(t
(save-excursion
(while (not (eobp))
(forward-line 1))
(while (not (bobp))
(forward-line -1)
(while (looking-at-p "^$")
(delete-blank-lines)))))))

This routine should run rather fast because most of the function calls
are functions written in C and already compiled to native code. Also by
starting at the end of the region and working backward you push the gap
to the end (I think) of the buffer/region, this should make looking up
regexps faster without a stall. Not that any of this matters these days.

You could do this...

(defun si:delete-lines-for-gnus ()
"Delete all blank lines from buffer or active starting with `>' ."
(interactive)
(cond ((region-active-p)
;; when the region is active only apply to region
(save-excursion
(goto-char (region-end))
(while (not (bobp))
(forward-line -1)
;; change the regexp
;; - the `$' means end of line so you do not need to look for
;; the '\n' or '\r' or '\r\n'...
;;
(while (looking-at-p "^>\s*$")
(delete-blank-lines)))))
(t
;; no region selected - apply to whole buffer
(save-excursion
(while (not (eobp))
(forward-line 1))
(while (not (bobp))
(forward-line -1)
(while (looking-at-p "^>\s*$")
(delete-blank-lines)))))))


Another example i just wrote for doing some database stuff. This one
adds a `interactive' method to the function. This means the function is
designed for user interface. The variables <SREG> and <EREG> are start
of region, and end of region. These buffer positions are passed to the
function automatically by using the interactive method with "r".

The emacs data structure of a buffer is amazingly powerful when used
with an interactive lisp interpreter. Unfortunately the language is old
and hencforth very large and somewhat archaic. On the other hand Lisp does not
change often :)

(defun db-text-kill-lines (sreg ereg)
;; interactive - tell emacs to pass the region as a parameter.
(interactive "r")
(let ((lst '(".jpg" ".png" ".jpeg" ".gif" ".txt" ".cue" ".log" ".m3u" ".md5" ".par.*" ".svf" ".ffp")))
(save-excursion
(dolist (l lst)
(goto-char sreg)
(while (re-search-forward (concat "^.*" (regexp-quote l) "$") ereg t)
(replace-match ""))))))


The real genius of emacs is the interactive minibuffer. The function
`read-from-minibuffer' is great. Although it is getting harder to learn.

(defun si:goto-position (&optional prefix)
"Goto line. With +prefix goto point. With -prefix goto column."
(interactive "P")
(cond ((not prefix)
(call-interactively 'goto-line))
((> (prefix-numeric-value prefix) 0)
(let ((current-prefix-arg nil))
(call-interactively 'goto-char)))
(t (let ((val (condition-case nil
(car (read-from-string
(read-from-minibuffer "Goto Column: ")))
(file-error nil))))
(if (numberp val)
(move-to-column val))))))


> Cheers,

A skip across the pond :)

Hope this helps some.

0 new messages