emacs-devel
[Top][All Lists]
Advanced

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

[tip] Don't send messages with forgotten link reference


From: João Pedro
Subject: [tip] Don't send messages with forgotten link reference
Date: Fri, 27 May 2022 00:34:06 -0300

I've come to you with a small tip that might be very helpful if you,
like me, also suffers from ADHD or is just forgotten.

Some time in the past I've seen a function that looks for words like
"attached" in the message body and checks if there is any attachment on
the message to be sent, querying the user if they want to send it as is,
without attachments.

I, more often than not, refer to some link in the e-mail, with e.g. [1],
but end up forgetting to send the actual link itself somewhere in the
body of the e-mail, forcing me to send another e-mail with only the link
in question.

But now, with the power of Emacs Lisp that (should) will no longer
happen! I have come up with a possible solution for that problem,
inspired by the "attachment checker" I mentioned previously, and I'd
like to share it here (I'm not the only one that forgets to post the
actual link...). This function is intended to be used in
`message-send-hook', and it seems like it works as it should for now.
Let me know if you encounter any error, false positive or if you have
any improvement to propose! Here's the function:

    (defun message-check-forgotten-link ()
      "Check if there is any link reference without a link.
    This function is intended to be used in `message-send-hook'.

    We look through the message buffer for any line -- that isn't a
    quote -- with a link reference, e.g. [1]. If it finds one, we
    then look for a another instance of the link reference followed
    by a link (string starting with https?://) and, if we can't find
    it, ask the user wether they want to abort sending the message."
      (let* ((not-quote-re "^[[:blank:]]*[^>]")
             (link-ref-re 
"\\[\\([0-9]+\\)\\]\\(?:[[:blank:]]\\|[[:punct:]]\\)*")
             (after-link-re "\\([[:word:]].*?\\)[[:space:]]")
             (regexp (format "%s.*%s%s" not-quote-re link-ref-re after-link-re))
             refs refs+links)
        (message-goto-body)
        (save-match-data
          (while (re-search-forward regexp nil 'noerror 1)
            (let ((ref (match-string-no-properties 1))
                  (link? (match-string-no-properties 2)))
              (if (string-match "\\`https?://" link?)
                  (push (cons ref (list :link link?)) refs+links)
                (push ref refs)))))
        (dolist (ref refs)
          (when (and (not (assoc ref refs+links))
                     (y-or-n-p
                      (format "No link found for reference [%s], abort?" ref)))
            (user-error "Aborting")))))

Best regards,

-- 
João Pedro de Amorim Paula
IT undergraduate at Universidade Federal do Rio Grande do Norte (UFRN)

reply via email to

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