[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Strip signature on reply without standard separator

From: Emanuel Berg
Subject: Re: Strip signature on reply without standard separator
Date: Mon, 19 Sep 2022 02:58:34 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

Satoshi Yoshida wrote:

> I consulted members of on slack.
> Many thanks for their kind help.

Good to hear from Emacs Japan, I love Japanese technology <3

> (setq mu-cite-prefix-format '("> "))
> (setq mu-cite-cited-prefix-regexp "\\(^[^[:blank:]\n<>]+>+[[:blank:]]*\\)")
> (setq mu-cite-top-format '(from " writes:\n\n"))
> (defun strip-signature (regexp replacement)
>   (goto-char (point-min))
>   (while (re-search-forward regexp nil t)
>     (replace-match replacement)))
> (defun my-mu-cite-hook-function ()
>   (save-excursion
>     (dolist (elm '(("^\n-- \n\\(.*\n\\)*" "")

This can be made look better (reduce programmer's reading and
typing code) by making the second formal parameter to
strip-signature &optional, it'd then default to nil which your
function could treat as "don't replace, just drop". That way
you don't have to give those ugly "" as arguments all the
time. But it's absolutely not wrong the way you have it.

In general, if the last argument can be nil for no, empty, nothing,
or does not apply, this argument can safely be made optional
from the above perspective.

Be sure to try it for type (or set it to, e.g. 0, if nil
implies that) before using it as an integer or string tho, as

  (integerp nil) ; nil
  (stringp nil)  ; nil

See this file:

In your case, you could do

(defun strip-signature (re &optional rep)
  (or rep (setq rep ""))
  ;; ...

and not have to change anything else.

>                    ("^\\([^[:blank:]\n<>]+>.*\\)" "> \\1")
>                    ("^\\([^\n>].+\n\n\\)\\(>[[:blank:]]+\n\\)+" "\\1")
>                    ("^> >" ">>")
>                    ("^> -- .*\n\\(>.*\n\\)*" "")
>                    ("^\\(>[[:blank:]]+\n\\)+> \\(best\\( regards\
> \\| wishes\\)?\\|cheers\\|\\(good\\)?bye\\|good luck\\|\\(kind \\|warm\
> \\(est\\)? \\)?regards\\|respectfully\\|\\(yours \\)?sincerely\\( yours\
> \\)?\\|thank you\\( very much\\)?\\|\\(many \\)?thanks\\( in advance\
> \\| very much\\)?\\),[[:blank:]]*\n\\(>.*\n\\)*" "")))
>       (apply #'strip-signature elm))

Okay, I dare say most people would write that

  (dolist (e '((1 2) (3 4)))
    (message "%s %s" (car e) (cadr e)) )

Or maybe

  (require 'cl-lib)
  (cl-loop for (a b) in '((x y) (i j)) do
    (message "%s %s" a b) )

>     (goto-char (point-min))
>     (when (re-search-forward "^\"?\\([^[:blank:]\n<>]+\\)\\([^\n<>]+\\)?\"? \
> <\\([^\n<>]+\\)> writes:" nil t)
>       (let ((first-name (match-string 1))
>             (middle-last-name (or (match-string 2) ""))
>             (mail-address (match-string 3)))
>         (strip-signature (apply #'format "^\\(>[[:blank:]]+\n\\)*>\
> [[:blank:]]*\\(-+[[:blank:]]*\\)?%s\\(%s\\)?\\([[:blank:]]*\\(\n>\
> [[:blank:]]+\\)*<?%s>?\\)?[[:blank:]]*\n\\(>[[:blank:]]+\n\\)*\\'\
> " (mapcar #'regexp-quote (list first-name middle-last-name mail-address))) 
> "")))
>     (strip-signature "^\\(>[>[:blank:]]+\n\\)+\\'" "")             
>     (goto-char (point-max))
>     (ignore-errors
>       (insert-file-contents "~/.signature")
>       (insert "\n-- \n"))))
> (add-hook 'mu-cite-post-cite-hook #'my-mu-cite-hook-function)

I'm sure you can develop it along the same and other lines but
you have understood the message, good. REs are very useful and
you got a lot of practice on those. But also a piece of code
that did what you wanted.

underground experts united

reply via email to

[Prev in Thread] Current Thread [Next in Thread]