unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [tip] Don't send messages with forgotten link reference
@ 2022-05-27  3:34 João Pedro
  0 siblings, 0 replies; only message in thread
From: João Pedro @ 2022-05-27  3:34 UTC (permalink / raw)
  To: emacs-devel

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)

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-05-27  3:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-27  3:34 [tip] Don't send messages with forgotten link reference João Pedro

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).