« Spam | Main | Next choice of reading »

Letting Gnus run sa-learn

After being fed up with spam I decided to install "SpamAssassin":http://www.spamassassin.org/ on my server. This was very easy and with a bit of tweaking to the suggested .procmailrc everything's running fine. The only thing missing was a keyboard shortcut in "Gnus":http://www.gnus.org/ running sa-learn for spam & ham mails...
I found some elisp code to run a command on the current article over on "my.gnus.org":http://my.gnus.org/ (specifically the howto Pipe an article through a command) but found that the code didn't really work as I wanted it to. Running sa-learn only produces one line of ouput (unless you're debugging with "-D"), and I wanted that output to show up in the minibuffer. I ended up with this edited version of the function:
(defun pfm-run-command-on-article (command &optional replace)
  "Run the full text of the current article through a filter.

The full text of the current article is run through the specified COMMAND
as a filter. The output of the command is returned."

  (interactive "sCommand: \nP")
  (let ((n (gnus-summary-article-number))
        (g gnus-newsgroup-name))
     (with-temp-buffer
       (gnus-request-article n g (current-buffer))
       (shell-command-on-region
        (point-min) (point-max)
        command)
       (if replace
            (gnus-request-replace-article n g (current-buffer)))
        )))
Then I've added two keyboard shortcuts to the gnus-summary-mode-map keymap:
(define-key gnus-summary-mode-map
         (read-kbd-macro "C-c R s")
         '(lambda () (interactive) (pfm-run-command-on-article "sa-learn --spam --single" ))
)
(define-key gnus-summary-mode-map
        (read-kbd-macro "C-c R h")
        '(lambda () (interactive) (pfm-run-command-on-article "sa-learn --ham --single"))
[Update 2003-08-14 21:45 - Changed the define-key code so it binds to gnus-summary-mode-map instead of global-map. Tested it in Gnus 5.10.1, and it worked nicely. 'C-c R' is by default not bound to anything, so it should be safe to use] [Update 2003-08-15 10:18 - Changed to fix the errors gAm commented]

Comments (4)

gAm:

I am not sure whether this is the proscribed way to perform bindings into the keymap for the *summary ...* buffer, but substituting gnus-summary-mode-map for global-map in your keybinding binds into the required map, given that gnus is already loaded (and the keymap created).

(require 'gnus)
(define-key gnus-summary-mode-map
    (kbd "v")
    '(lambda () (interactive) (message "Test of keybinding in summary mode" )))

Just ran a quick test to check if I could rebind the 'v' key to display a test message in the minibuffer.

Just be careful not to clobber existing bindings. ;)

The key binding worked brilliantly, I'll update the article. Thanks!

gAm:

Since you've edited the article, you should also remove the reference to global bindings in the text (as well as you claim of lacking knowledge of binding keys in local maps).

Good point, fixed that too now. Thanks!

About

This page contains a single entry from the blog posted on August 13, 2003 4:39 PM.

The previous post in this blog was Spam.

The next post in this blog is Next choice of reading.

Many more can be found on the main index page or by looking through the archives.