2

I have recently moved to emacs as my mail client. I am using message-mode to write and send email through notmuch. Specifically, the major mode is called Message[Notmuch].

I would like to remap C-c C-c (mail-send-and-exit) and C-c C-s (mail-send) to safe analogues that require a confirmation in the minibuffer before the email is sent. So far I have:

(defun safe-mail-send-and-exit ()
  (interactive)
  (if (string-equal (read-from-minibuffer "Are you sure? ") "yes")
      (mail-send-and-exit)
    (message "Not sent!")))

When I call this function within message-mode, I get an error:

Symbol’s function definition is void: mail-send-and-exit

How can I pass the context of the email and the mode to my function?

6
  • In your question, you refer to send-mail-and-exit but in your code you refer to mail-send-and-exit. Maybe that's why it can't find mail-send-and-exit. Unfortunately, I'm not that familiar with notmuch.
    – g-gundam
    Commented Dec 8, 2022 at 14:53
  • 1
    Hey, sorry, just a typo on my part in the post. Those should be mail-send and mail-send-and-exit, I'll edit the original post.
    – johngarg
    Commented Dec 8, 2022 at 14:59
  • Emacs is telling you that function doesn't exist, so let's try to find the right one. Temporarily undo your remapping of C-c C-c and then figure out what function it was originally bound to by doing C-h k and then C-c C-c. I bet it'll be something other than mail-send-and-exit.
    – g-gundam
    Commented Dec 8, 2022 at 15:12
  • Actually, you're right. How silly of me... Thanks!
    – johngarg
    Commented Dec 8, 2022 at 15:32
  • 1
    Functions in Emacs Lisp are globally scoped, so if the function is defined at all, it will be available everywhere.
    – NickD
    Commented Dec 8, 2022 at 16:19

1 Answer 1

2

Put (require 'THE-LIBRARY) inside your definition of safe-mail-send-and-exit, at the beginning (just after (interactive), where THE-LIBRARY is the library that defines function mail-send-and-exit.

(defun safe-mail-send-and-exit ()
  (interactive)
  (require 'THE-LIBRARY) ; Replace THE-LIBRARY with the right library name.
  (if (string-equal (read-from-minibuffer "Are you sure? ") "yes")
      (mail-send-and-exit)
    (message "Not sent!")))

Or else ensure that you load that library before defining (or at least before invoking) that function. E.g.:

(with-eval-after-load THE-LIBRARY
  (defun safe-mail-send-and-exit ()
   ;; ...
   ))

Not part of your question, but consider replacing this:

(string-equal (read-from-minibuffer "Are you sure? ") "yes")

with just this:

(yes-or-no-p "Are you sure? ")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.