Martin Kjær Jørgensen via Users list for the GNU Emacs text
editor <[email protected]> writes:
>
> Thank you :-) . That is very close to what I was thinking
> about. I'll try it out, and maybe adapt it a bit.
Two corrections to the regular expressions used in the
function definition:
1. They did not account for white-space (space or tab)
character(s) that might follow ‘>’. So, the match
would leave the spaces that followed the level-1 ‘>’,
but preceded the level-2 ‘>’.
2. They did not account for the following behavior when
matching complemented bracket expressions, that is,
[^...]:
"A complemented bracket expression can match a
newline, unless newline is mentioned as one of the
characters not to match. This is in contrast to the
handling of regexps in programs such as ‘grep’."
(info "(elisp) Regexp Special")
So, the match would find a ‘>’ alone on a line and
would remove it AND the empty line that followed it.
Here are the expressions from the email I sent previously,
followed by the new expressions:
1. Old:
(delete-matching-lines "^>$\\|^>[^>].*$" begin end)
New:
(delete-matching-lines
"^>[[:space:]]*[^\n]$\\|^>[[:space:]]+[^>\n]+.*"
begin end)
2. Old:
(replace-regexp "^>\\(.*\\)" "\\1" nil begin end)
New:
(replace-regexp "^>[[:space:]]*\\(.*\\)$" "\\1" nil begin end)
Given that an email’s text can have any form whatsoever (no
syntax), these expressions still will not cover all cases,
but they ought to cover a high percentage of the common
cases.
Here is a new complete version of ‘delete-top-level’,
including updated comments and simplified expression for
setting ‘begin’:
(defun delete-top-level ()
"Delete the top-level reply lines from a buffer in Message mode."
(interactive)
(if (eq major-mode 'message-mode)
;; Restrict all editing to lines from the beginning of the message
;; body down to either the line before the signature or the end of
;; the buffer, if there is no signature.
(let ((begin (message-goto-body))
(end (progn
(when (message-goto-signature)
(forward-line -2))
(point))))
;; Delete all lines that either:
;; - start with ‘>’, followed by zero or more white
;; space characters on the line, or
;; - start with ‘>’, followed by zero or more white-space
;; characters, followed by at least one non-‘>’ character,
;; followed by any character(s)
(delete-matching-lines
"^>[[:space:]]*[^\n]$\\|^>[[:space:]]+[^>\n]+.*"
begin end)
;; In the lines that remain, replace any lines that:
;; - start with a ‘>’, followed by zero or more white-space
;; characters, followed by any characters
;; with a line that:
;; - consists of all of the characters after the ‘>’ and,
;; optionally, white-space characters
(replace-regexp "^>[[:space:]]*\\(.*\\)$" "\\1" nil begin end))
;; Otherwise...
(user-error "This command only works in message buffers")))
--
The lyf so short, the craft so long to lerne.
- Geoffrey Chaucer, The Parliament of Birds.
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.