To solve a problem with the Gnus' IMAP search in Exchange folders with Emacs 28.2, I have the modified piece of code from gnus-search.el below. However, if I add this to my init.el on starting Emacs I get the error that gnus-search-imap is undefined. On the other hand, if I comment out the code, restart Emacs, start Gnus, try searching in an Exchange folder (which without the modified code always returns no result) and then evaluate the modified code, I get no error.

So can I put the code in my init.el somehow or do I just have to patch gnus-search.el? If the later, why is this the case?

(cl-defmethod gnus-search-imap-search-command ((engine gnus-search-imap)
                         (query string))
  "Create the IMAP search command for QUERY.
Currently takes into account support for the LITERAL+ capability.
Other capabilities could be tested here."
  (with-slots (literal-plus) engine
    (when (and literal-plus
         (string-match-p "\n" query))
    (setq query (split-string query "\n")))
     ((consp query)
    ;; We're not really streaming, just need to prevent
    ;; `nnimap-send-command' from waiting for a response.
    (let* ((nnimap-streaming t)
         "UID SEARCH CHARSET UTF-8 %s"
         (pop query))))
      (dolist (l query)
        (process-send-string (get-buffer-process (current-buffer)) l)
        (process-send-string (get-buffer-process (current-buffer))
                 (if (nnimap-newlinep nnimap-object)
      (nnimap-get-response call)))
     (t (nnimap-command "UID SEARCH %s" query)))))

1 Answer 1


So can I put the code in my init.el somehow

You can, but at minimum you need to ensure that Emacs doesn't try to evaluate your modified code before either:

  • any prerequisites of that code have been defined
  • the original function is loaded (which would simply clobber your definition)

The typical solution to both things is to establish which library defines the original function, and arrange for your code to be evaluated after that library has been loaded, and you can do that by wrapping the whole definition in the form like so:

(with-eval-after-load "gnus-search"
  (cl-defmethod gnus-search-imap-search-command ...))

N.b. C-hf gnus-search-imap-search-command will tell you that gnus-search.el is the library in question, which gives us the "gnus-search" value.

This ensures that everything required by the original method is now defined, and (because the original method has been defined) that your definition will clobber it, rather than the other way around.

Your Answer

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

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