1 ;;; nnmairix.el --- Mairix back end for Gnus, the Emacs newsreader
3 ;; Copyright (C) 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: David Engster <dengste@eml.cc>
6 ;; Keywords: mail searching
9 ;; This file is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; This file is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
26 ;; THIS IS BETA SOFTWARE! This back end should not mess up or
27 ;; even delete your mails, but having a backup is always a good idea.
29 ;; This is a back end for using the mairix search engine with
30 ;; Gnus. Mairix is a tool for searching words in locally stored
31 ;; mail. Mairix is very fast which allows using it efficiently for
32 ;; "smart folders", e.g. folders which are associated with search
33 ;; queries. Of course, you can also use this back end just for
34 ;; calling mairix with some search query.
36 ;; Mairix is written by Richard Curnow. More information can be found at
37 ;; http://www.rpcurnow.force9.co.uk/mairix/
39 ;; For details about setting up mairix&Gnus&nnmairix.el, look at the
42 ;; http://www.emacswiki.org/cgi-bin/wiki/GnusMairix
44 ;; The newest version of nnmairix.el can be found at
46 ;; http://www.emacswiki.org/cgi-bin/emacs/nnmairix.el
48 ;; For impatient people, here's the setup in a nutshell:
50 ;; This back end requires an installed mairix binary which is
51 ;; configured to index your mail folder. You don't have to specify a
52 ;; search folder (but it does no harm, either). Visit the man page of
53 ;; mairix and mairixrc for details.
55 ;; Put nnmairix.el into your search path and "(require 'nnmarix)" into
56 ;; your .gnus. Then call nnmairix-create-default-group (or 'G b
57 ;; c'). This function will ask for all necessary information to create
58 ;; a mairix server in Gnus with the default search folder. This
59 ;; default search folder will be used for all temporary searches: call
60 ;; nnmairix-search ('G b s') and enter a mairix query (like
61 ;; f:test@example.com). To create a mairix group for one specific
62 ;; search query, use 'G b g'. See the emacswiki or the source for more
65 ;; Commentary on the code: nnmairix sits between Gnus and the "real"
66 ;; back end which handles the mail (currently nnml, nnimap and
67 ;; nnmaildir were tested). I know this is all a bit hacky, but so far
68 ;; it works for me. This is the first back end I've written for Gnus,
69 ;; so I'd appreciate any comments, suggestions, bug reports (and, of
70 ;; course, patches) for improving nnmairix.
72 ;; nnmairix does not use an active file, since I wanted to contain the
73 ;; back end "inside Gnus" as much as possible without the need of an
74 ;; external file. It stores the query/folder information in the group
75 ;; parameters instead. This also implies that once you kill a mairix
76 ;; group, it's gone for good. I don't think that this is really
77 ;; problematic, since I don't see the need in unsubscribing and
78 ;; re-subscribing search groups
80 ;; Every mairix server is "responsible" for one mairix installation,
81 ;; i.e. you can have several mairix servers for different mairix
82 ;; configurations. Not that I think anyone will actually do this, but
83 ;; I thought it would be a "nice to have feature"...
86 ;; * When using Maildir: path and filename of a mail can change due to
87 ;; reading/replying/etc. This can lead to dangling symlinks in
88 ;; nnmairix groups and it depends on the back end how well it deals
89 ;; with that (some IMAP servers out there may not be amused). Update the
90 ;; database ('G b u') and the group to fix it.
91 ;; * Mairix does only support us-ascii characters.
93 ;; TODO/MISSING FEATURES:
94 ;; * Possibility to propagate flags like seen, replied, ticked
95 ;; to original message
96 ;; * Support of more back ends (nnmh, nnfolder, nnmbox...)?
97 ;; * Maybe use an active file instead of group parameters?
98 ;; * Use "-a" when updating groups which are not newly created
102 ;; 02/06/2008 - version 0.5
104 ;; * New function: nnmairix-goto-original-article. Uses the
105 ;; registry or the mail file path for determining original group.
107 ;; * Deal with empty Xref header
109 ;; * Changed summary mode keybindings since the old ones were
112 ;; (Thanks to Tassilo Horn and Ted Zlatanov for their help)
114 ;; 01/07/2008 - version 0.4
116 ;; * New/fixed doc strings and code cleanup.
118 ;; 18/11/2007 - version 0.3
120 ;; * Fixed bugs when dealing with nnml and native servers
122 ;; * Make variables customizable
124 ;; 10/10/2007 - version 0.2
126 ;; * Use nnml-directory/directory server variables for nnml and
127 ;; nnmaildir backends as path for search folders. This way it
128 ;; becomes independent of 'base' setting in .mairixirc (but not for
131 ;; * As a result: Changed nnmairix-backend-to-server so that user
132 ;; is asked when more than one nnmairix server exists and we do not
133 ;; know which one is responsible for current backend.
135 ;; * Rename files when using nnml backends so that there are no
136 ;; holes in article numbers. This should fix all problems regarding
137 ;; wrong article counts with nnml.
139 ;; * More commands for creating queries (using widgets or the
142 ;; * Fixed bug in nnmairix-create-search-group-from-message
144 ;; * Changed copyright to FSF
146 ;; (Thanks to Georg C. F. Greve and Bastien for suggestions and
149 ;; 10/03/2007 - version 0.1 - first release
155 (require 'gnus-group)
161 (nnoo-declare nnmairix)
166 (defun nnmairix-group-mode-hook ()
167 "Nnmairix group mode keymap."
168 (define-key gnus-group-mode-map
169 (kbd "G b") (make-sparse-keymap))
170 (define-key gnus-group-mode-map
171 (kbd "G b g") 'nnmairix-create-search-group)
172 (define-key gnus-group-mode-map
173 (kbd "G b c") 'nnmairix-create-server-and-default-group)
174 (define-key gnus-group-mode-map
175 (kbd "G b q") 'nnmairix-group-change-query-this-group)
176 (define-key gnus-group-mode-map
177 (kbd "G b t") 'nnmairix-group-toggle-threads-this-group)
178 (define-key gnus-group-mode-map
179 (kbd "G b u") 'nnmairix-update-database)
180 (define-key gnus-group-mode-map
181 (kbd "G b s") 'nnmairix-search)
182 (define-key gnus-group-mode-map
183 (kbd "G b i") 'nnmairix-search-interactive)
184 (define-key gnus-group-mode-map
185 (kbd "G b m") 'nnmairix-widget-search))
188 (defun nnmairix-summary-mode-hook ()
189 "Nnmairix summary mode keymap."
190 (define-key gnus-summary-mode-map
191 (kbd "$ t") 'nnmairix-search-thread-this-article)
192 (define-key gnus-summary-mode-map
193 (kbd "$ f") 'nnmairix-search-from-this-article)
194 (define-key gnus-summary-mode-map
195 (kbd "$ m") 'nnmairix-widget-search-from-this-article)
196 (define-key gnus-summary-mode-map
197 (kbd "$ g") 'nnmairix-create-search-group-from-message)
198 (define-key gnus-summary-mode-map
199 (kbd "$ o") 'nnmairix-goto-original-article))
201 (add-hook 'gnus-group-mode-hook 'nnmairix-group-mode-hook)
202 (add-hook 'gnus-summary-mode-hook 'nnmairix-summary-mode-hook)
205 ;; Customizable stuff
207 (defgroup nnmairix nil
208 "Backend for the Mairix mail search engine."
211 (defcustom nnmairix-group-prefix "zz_mairix"
212 "Prefix for mairix search groups on back end server.
213 nnmairix will create these groups automatically on the back end
214 server for each nnmairix search group. The name on the back end
215 server will be this prefix plus a random number. You can delete
216 unused nnmairix groups on the back end using
217 `nnmairix-purge-old-groups'."
222 (defcustom nnmairix-mairix-output-buffer "*mairix output*"
223 "Buffer used for mairix output."
228 (defcustom nnmairix-customize-query-buffer "*mairix query*"
229 "Name of the buffer for customizing Mairix queries."
234 (defcustom nnmairix-mairix-update-options '("-F" "-Q")
235 "Options when calling mairix for updating the database.
236 The default is '-F' and '-Q' for making updates faster. You
237 should call mairix without these options from time to
238 time (e.g. via cron job)."
240 :type '(repeat string)
243 (defcustom nnmairix-mairix-synchronous-update nil
244 "Set this to t if you want Emacs to wait for mairix updating the database."
249 (defcustom nnmairix-rename-files-for-nnml t
250 "Rename nnml mail files so that they are consecutively numbered.
251 When using nnml as backend, mairix might produce holes in the
252 article numbers which will produce wrong article counts by
253 Gnus. This option controls whether nnmairix should rename the
254 files consecutively."
259 (defcustom nnmairix-widget-fields-list
260 '(("from" "f" "From") ("to" "t" "To") ("cc" "c" "Cc")
261 ("subject" "s" "Subject") ("to" "tc" "To or Cc")
262 ("from" "a" "Address") (nil "b" "Body") (nil "n" "Attachment")
263 ("Message-ID" "m" "Message ID") (nil "s" "Size") (nil "d" "Date"))
264 "Fields that should be editable during interactive query customization.
266 Header, corresponding mairix command and description for editable
267 fields in interactive query customization. The header specifies
268 which header contents should be inserted into the editable field
269 when creating a Mairix query based on the current message (can be
270 nil for disabling this)."
274 (const :tag "none" nil)
275 (const :tag "From" "from")
276 (const :tag "To" "to")
277 (const :tag "Cc" "cc")
278 (const :tag "Subject" "subject")
279 (const :tag "Message ID" "Message-ID"))
280 (string :tag "Command")
281 (string :tag "Description")))
284 (defcustom nnmairix-widget-select-window-function
285 (lambda () (select-window (get-largest-window)))
286 "Function for selecting the window for customizing the mairix query.
287 The default chooses the largest window in the current frame."
292 ;; ==== Other variables
294 (defvar nnmairix-widget-other
296 "Other editable mairix commands when using customization widgets.
297 Currently there are 'threads and 'flags.")
299 (defvar nnmairix-interactive-query-parameters
300 '((?f "from" "f" "From") (?t "to" "t" "To") (?c "to" "tc" "To or Cc")
301 (?a "from" "a" "Address") (?s "subject" "s" "Subject") (?b nil "b" "Body")
302 (?d nil "d" "Date") (?n nil "n" "Attachment"))
303 "Things that should be editable during interactive query generation.
304 Every list element consists of the following entries: Keystroke,
305 message field (if any), mairix command and description.")
307 (defvar nnmairix-delete-and-create-on-change '(nnimap nnmaildir nnml)
308 "Controls on which backends groups should be deleted and re-created.
309 This variable is a list of back ends where the search group should
310 be completely deleted and re-created when the query or thread
311 parameter changes. I know this is rather \"brute force\" and maybe
312 even dangerous (you have backups, right?), but it should be used at
313 least for nnimap since some IMAP servers are really not amused when
314 mailbox content changes behind their back. It usually also corrects
315 the problem of \"holes\" in the article numbers which often lead to a
316 wrong count of total articles shown by Gnus.")
318 ;;; === Server variables
320 (defvoo nnmairix-backend nil
321 "Backend where mairix stores its searches.")
323 (defvoo nnmairix-backend-server nil
324 "Name of the server where mairix stores its searches.")
326 (defvoo nnmairix-mairix-command "mairix"
327 "Command to call mairix for this nnmairix server.")
329 (defvoo nnmairix-hidden-folders nil
330 "Set this to t if the back end server uses hidden directories for
331 its maildir mail folders (e.g. the Dovecot IMAP server or mutt).")
333 (defvoo nnmairix-default-group nil
334 "Default search group. This is the group which is used for all
335 temporary searches, e.g. nnmairix-search.")
337 ;;; === Internal variables
339 ;; Regexp for mairix groups on back end
340 (setq nnmairix-group-regexp (format "%s-\\(.*\\)-[0-9]+" nnmairix-group-prefix))
342 ;; Back ends (hopefully...) supported by nnmairix.
343 ;; Other backends might or might not work.
344 (setq nnmairix-valid-backends '(nnimap nnml nnmaildir))
346 ;; Last chosen server
347 (setq nnmairix-last-server nil)
350 (setq nnmairix-current-server nil)
352 ;;; === Gnus backend functions
354 (nnoo-define-basics nnmairix)
356 (gnus-declare-backend "nnmairix" 'mail 'address)
358 (deffoo nnmairix-open-server (server &optional definitions)
359 ;; just set server variables
360 (setq nnmairix-current-server server)
361 (nnoo-change-server 'nnmairix server definitions))
363 (deffoo nnmairix-request-group (group &optional server fast)
364 ;; Call mairix and request group on back end server
365 (when server (nnmairix-open-server server))
366 (let* ((qualgroup (if server
367 (gnus-group-prefixed-name group (list 'nnmairix server))
369 (query (gnus-group-get-parameter qualgroup 'query t))
370 (folder (gnus-group-get-parameter qualgroup 'folder))
371 (threads (gnus-group-get-parameter qualgroup 'threads))
372 (backendmethod (gnus-server-to-method
373 (format "%s:%s" (symbol-name nnmairix-backend)
374 nnmairix-backend-server)))
375 rval mfolder folderpath)
378 ;; No folder parameter -> error
379 (nnheader-report 'nnmairix "Check folder parameter for group %s" group)
382 ;; No query -> return empty group
384 (set-buffer nntp-server-buffer)
386 (insert (concat "211 0 1 0 " group))
389 ;; For maildir++ folders: create a hidden directory (prepend dot)
390 (setq mfolder (if (and nnmairix-hidden-folders
391 (not (string-match "^\\." folder)))
394 ;; For nnml and nnmaildir, precede mfolder with directory where mail
395 ;; is actually stored so that it's independent of 'base' setting
397 (when (eq nnmairix-backend 'nnml)
398 (setq folderpath (cadr (assoc 'nnml-directory backendmethod)))
399 ;; if nnml-directory is not explicitly set, use global value
400 (when (not folderpath)
401 (setq folderpath nnml-directory)))
402 (when (eq nnmairix-backend 'nnmaildir)
404 (cadr (assoc 'directory backendmethod))))
408 (file-name-as-directory
412 ;; If (not fast), call Mairix binary
415 (nnmairix-call-mairix-binary
416 (split-string nnmairix-mairix-command)
417 mfolder query threads)))
418 ;; Check return value
420 ((zerop rval) ; call was succesful
421 (nnmairix-call-backend
422 "open-server" nnmairix-backend-server)
423 ;; If we're dealing with nnml, rename files
424 ;; consecutively and make new active file for this
426 (when (eq nnmairix-backend 'nnml)
427 (when nnmairix-rename-files-for-nnml
428 (nnmairix-rename-files-consecutively mfolder))
429 (nnml-generate-nov-databases-directory mfolder))
430 (nnmairix-call-backend
431 "request-scan" folder nnmairix-backend-server)
434 (nnmairix-request-group-with-article-number-correction folder qualgroup)))
436 (save-excursion (set-buffer nnmairix-mairix-output-buffer)
437 (goto-char (point-min))
438 (looking-at "^Matched 0 messages")))
439 ;; No messages found -> return empty group
440 (nnheader-message 5 "Mairix: No matches found.")
441 (set-buffer nntp-server-buffer)
443 (insert (concat "211 0 1 0 " group))
445 ;; Everything else is an error
448 'nnmairix "Error running marix. See buffer %s for details"
449 nnmairix-mairix-output-buffer)
453 (deffoo nnmairix-request-create-group (group &optional server args)
454 (let ((qualgroup (if server (gnus-group-prefixed-name group (list 'nnmairix server))
459 (when server (nnmairix-open-server server))
460 (gnus-group-add-parameter qualgroup '(query . nil))
461 (gnus-group-add-parameter qualgroup '(threads . nil))
463 (setq count (1+ count))
464 (setq groupname (format "%s-%s-%s" nnmairix-group-prefix group
465 (number-to-string count)))
466 (setq exist (nnmairix-call-backend
467 "request-group" groupname nnmairix-backend-server)))
468 (nnmairix-call-backend
469 "request-create-group" groupname nnmairix-backend-server)
470 (gnus-group-add-parameter qualgroup '(folder . nil))
471 (gnus-group-set-parameter qualgroup 'folder groupname))
475 (deffoo nnmairix-retrieve-headers (articles group &optional server fetch-old)
476 (when server (nnmairix-open-server server))
477 (let* ((folder (nnmairix-get-backend-folder group server))
478 (corr (nnmairix-get-numcorr group server))
482 (not (zerop (cadr corr)))
483 (numberp (car articles)))
484 (setq numcorr (cadr corr))
487 (lambda (arg) (- arg numcorr))
489 (setq rval (nnmairix-call-backend
490 "retrieve-headers" articles folder nnmairix-backend-server fetch-old))
492 (nnmairix-replace-group-and-numbers articles folder group numcorr)
495 (deffoo nnmairix-request-article (article &optional group server to-buffer)
496 (when server (nnmairix-open-server server))
497 (let ((folder (nnmairix-get-backend-folder group server))
498 (corr (nnmairix-get-numcorr group server)))
502 (not (zerop (cadr corr))))
503 (setq article (- article (cadr corr))))
504 (nnmairix-call-backend
505 "request-article" article folder nnmairix-backend-server to-buffer))
508 (deffoo nnmairix-close-group (group &optional server)
509 ;; Should we do something here?
513 (deffoo nnmairix-request-list (&optional server)
514 (when server (nnmairix-open-server server))
515 (if (nnmairix-call-backend "request-list" nnmairix-backend-server)
516 (let (cpoint cur qualgroup folder)
518 (set-buffer nntp-server-buffer)
519 (goto-char (point-min))
520 (setq cpoint (point))
521 (while (re-search-forward nnmairix-group-regexp (point-max) t)
522 (setq cur (match-string 1)
523 qualgroup (gnus-group-prefixed-name cur
524 (list 'nnmairix server)))
525 (if (and (gnus-group-entry qualgroup)
526 (string= (match-string 0)
527 (gnus-group-get-parameter qualgroup 'folder)))
530 (delete-region cpoint (point-at-bol))
532 (setq cpoint (point)))
534 (delete-region cpoint (point-max)))
539 (nnoo-define-skeleton nnmairix)
542 ;;; === Interactive functions
544 (defun nnmairix-create-search-group (server group query threads)
545 "Create on SERVER nnmairix search group GROUP with QUERY.
546 If THREADS is t, include whole threads from found messages. If
547 called interactively, user will be asked for parameters."
550 (gnus-server-to-method (car (nnmairix-get-server)))
551 (read-string "Group name: ")
552 (read-string "Query: ")
553 (y-or-n-p "Include threads? ")))
554 (when (and (stringp query)
555 (string-match "\\s-" query))
556 (setq query (split-string query)))
557 (when (not (listp query))
558 (setq query (list query)))
559 (when (and server group query)
561 (let ((groupname (gnus-group-prefixed-name group server))
563 (set-buffer gnus-group-buffer)
564 (gnus-group-make-group group server)
565 (gnus-group-set-parameter groupname 'query query)
566 (gnus-group-set-parameter groupname 'threads threads)
567 (nnmairix-update-and-clear-marks groupname)))))
569 (defun nnmairix-search-interactive ()
570 "Create mairix search interactively with the minibuffer."
572 (let ((char-header nnmairix-interactive-query-parameters)
573 header finished query achar)
574 (while (not finished)
576 (message "Query (%s): " (nnmairix-create-message-line-for-search))
577 (setq achar (read-char))
578 (when (not (assoc achar char-header))
580 (setq header (read-string
581 (concat "Match " (nth 3 (assoc achar char-header)) " on: ")))
582 (push (concat (nth 2 (assoc achar char-header)) ":" header) query)
583 (setq finished (not (y-or-n-p "Add another search query? "))
586 (mapconcat 'identity query " ")
587 (car (nnmairix-get-server))
588 (y-or-n-p "Include whole threads? "))))
590 (defun nnmairix-create-search-group-from-message ()
591 "Interactively create search group with query based on current message."
593 (let ((char-header nnmairix-interactive-query-parameters)
594 (server (nnmairix-backend-to-server gnus-current-select-method))
595 query achar header finished group threads cq)
596 (when (or (not (gnus-buffer-live-p gnus-article-buffer))
597 (not (gnus-buffer-live-p gnus-summary-buffer)))
598 (error "No article or summary buffer"))
600 (error "No nnmairix server found for back end %s:%s"
601 (symbol-name (car gnus-current-select-method))
602 (nth 1 gnus-current-select-method)))
603 (while (not finished)
605 (gnus-summary-toggle-header 1)
607 (message "Query (%s): " (nnmairix-create-message-line-for-search))
608 (setq achar (read-char))
609 (when (not (assoc achar char-header))
611 (set-buffer gnus-article-buffer)
613 (when (setq cq (nth 1 (assoc achar char-header)))
615 (nnmairix-replace-illegal-chars
616 (gnus-fetch-field (nth 1 (assoc achar char-header))))))
617 (setq header (read-string
618 (concat "Match " (nth 3 (assoc achar char-header)) " on: ")
620 (push (concat (nth 2 (assoc achar char-header)) ":" header) query)
621 (setq finished (not (y-or-n-p "Add another search query? "))
623 (setq threads (y-or-n-p "Include whole threads? "))
624 (setq group (read-string "Group name: "))
625 (set-buffer gnus-summary-buffer)
626 (message "Creating group %s on server %s with query %s." group
627 (gnus-method-to-server server) (mapconcat 'identity query " "))
628 (nnmairix-create-search-group server group query threads)))
630 (defun nnmairix-create-server-and-default-group ()
631 "Interactively create new nnmairix server with default search group.
632 All necessary information will be queried from the user."
634 (let* ((name (read-string "Name of the mairix server: "))
635 (server (completing-read "Back end server (TAB for completion): "
636 (nnmairix-get-valid-servers)))
637 (mairix (read-string "Command to call mairix: " "mairix"))
638 (defaultgroup (read-string "Default search group: "))
639 (backend (symbol-name (car (gnus-server-to-method server))))
640 (servername (nth 1 (gnus-server-to-method server)))
641 (hidden (and (string-match "^nn\\(imap\\|maildir\\)$" backend)
643 "Does the back end server work with maildir++ (i.e. hidden directories)? ")))
646 (apply (intern (format "%s-%s" backend "open-server"))
650 (string-match "^\\." defaultgroup))
651 (setq defaultgroup (substring defaultgroup 1)))
652 ;; Create default search group
653 (gnus-group-make-group
654 defaultgroup (list 'nnmairix name (list 'nnmairix-backend (intern backend))
655 (list 'nnmairix-backend-server servername)
656 (list 'nnmairix-mairix-command mairix)
657 (list 'nnmairix-hidden-folders hidden)
658 (list 'nnmairix-default-group defaultgroup)))))
661 (defun nnmairix-group-change-query-this-group (&optional query)
662 "Set QUERY for group under cursor."
664 (let* ((group (gnus-group-group-name))
665 (method (gnus-find-method-for-group group))
666 (oldquery (gnus-group-get-parameter group 'query t)))
667 (if (eq (car method) 'nnmairix)
669 (when (listp oldquery)
670 (setq oldquery (mapconcat 'identity oldquery " ")))
671 (setq query (or query
672 (read-string "New query: " oldquery)))
673 (when (stringp query)
674 (setq query (split-string query)))
676 (gnus-group-set-parameter group 'query query)
677 (nnmairix-update-and-clear-marks group)))
678 (error "This is no nnmairix group"))))
681 (defun nnmairix-group-toggle-threads-this-group (&optional threads)
682 "Toggle threads parameter for this group.
683 If THREADS is a positive number, set threads parameter to t.
684 If THREADS is a negative number, set it to nil."
686 (let* ((group (gnus-group-group-name))
687 (method (gnus-find-method-for-group group))
688 (getthreads (or threads
689 (not (gnus-group-get-parameter group 'threads)))))
690 (if (eq (car method) 'nnmairix)
692 (when (numberp getthreads)
693 (setq getthreads (> getthreads 0)))
694 (gnus-group-set-parameter group 'threads getthreads)
696 (message "Threads activated for group %s" group)
697 (message "Threads deacitavted for group %s" group))
698 (nnmairix-update-and-clear-marks group))
699 (error "This is no nnmairix group"))))
702 (defun nnmairix-search (query &optional server threads)
703 "Sends QUERY to nnmairix backend SERVER, using default its search group.
705 Default search group is automatically entered and results are shown.
706 If THREADS is t, enable threads.
707 If THREADS is a negative number, disable threads.
708 Otherwise, leave threads parameter as it is."
709 (interactive (list (read-string "Query: ")))
711 (setq server (car (nnmairix-get-server))))
713 (error "No opened nnmairix server found")
714 (setq server (gnus-server-to-method server)))
715 (nnmairix-open-server (nth 1 server))
716 (let* ((qualgroup (gnus-group-prefixed-name nnmairix-default-group
717 (list 'nnmairix (nth 1 server)))))
718 (set-buffer gnus-group-buffer)
719 (when (stringp query)
720 (setq query (split-string query)))
721 (gnus-group-set-parameter qualgroup 'query query)
722 (if (symbolp threads)
723 (when (eq threads 't)
724 (gnus-group-set-parameter qualgroup 'threads t))
726 (gnus-group-set-parameter qualgroup 'threads nil)))
727 (nnmairix-update-and-clear-marks qualgroup)
728 (when (not (zerop (gnus-group-unread qualgroup)))
729 (gnus-group-read-group nil t qualgroup))))
731 (defun nnmairix-search-thread-this-article ()
732 "Search thread for the current article.
733 This is effectively a shortcut for calling `nnmairix-search'
734 with m:msgid of the current article and enabled threads."
737 (nnmairix-backend-to-server gnus-current-select-method))
740 (if (gnus-buffer-live-p gnus-article-buffer)
743 (set-buffer gnus-article-buffer)
744 (gnus-summary-toggle-header 1)
745 (setq mid (message-fetch-field "Message-ID")))
746 (while (string-match "[<>]" mid)
747 (setq mid (replace-match "" t t mid)))
748 (nnmairix-search (concat "m:" mid) server t))
749 (message "No article buffer."))
750 (error "No nnmairix server found for back end %s:%s"
751 (symbol-name (car gnus-current-select-method))
752 (nth 1 gnus-current-select-method)))))
754 (defun nnmairix-search-from-this-article ()
755 "Search messages from sender of the current article.
756 This is effectively a shortcut for calling `nnmairix-search' with
760 (nnmairix-backend-to-server gnus-current-select-method))
763 (if (gnus-buffer-live-p gnus-article-buffer)
766 (set-buffer gnus-article-buffer)
767 (gnus-summary-toggle-header 1)
768 (setq from (cadr (gnus-extract-address-components
769 (gnus-fetch-field "From"))))
770 (nnmairix-search (concat "f:" from) server -1)))
771 (message "No article buffer."))
772 (error "No nnmairix server found for back end %s:%s"
773 (symbol-name (car gnus-current-select-method))
774 (nth 1 gnus-current-select-method)))))
777 (defun nnmairix-purge-old-groups (&optional dontask server)
778 "Delete mairix search groups which are no longer used.
780 You may want to call this from time to time if you are creating
781 and deleting lots of nnmairix groups. If DONTASK is t, do not ask
782 before deleting a group on the back end. SERVER specifies nnmairix server."
784 (let ((server (or server
785 (gnus-server-to-method (car (nnmairix-get-server))))))
786 (if (nnmairix-open-server (nth 1 server))
787 (when (nnmairix-call-backend
788 "request-list" nnmairix-backend-server)
789 (let (cur qualgroup folder)
791 (set-buffer nntp-server-buffer)
792 (goto-char (point-min))
793 (while (re-search-forward nnmairix-group-regexp (point-max) t)
794 (setq cur (match-string 0)
795 qualgroup (gnus-group-prefixed-name
796 (match-string 1) server))
797 (when (not (and (gnus-group-entry qualgroup)
799 (gnus-group-get-parameter
800 qualgroup 'folder))))
803 (concat "Delete group " cur
804 " on server " nnmairix-backend-server "? ")))
805 (nnmairix-call-backend
806 "request-delete-group" cur t nnmairix-backend-server)))))))
807 (message "Couldn't open server %s" (nth 1 server)))))
810 (defun nnmairix-update-database (&optional servers)
811 "Call mairix for updating the database for SERVERS.
813 If SERVERS is nil, do update for all nnmairix servers. Mairix
814 will be called asynchronously unless
815 `nnmairix-mairix-synchronous-update' is t. Mairix will be called
816 with `nnmairix-mairix-update-options'."
818 (let ((servers (or servers
819 (nnmairix-get-nnmairix-servers)))
820 args cur commandsplit)
822 (setq cur (car (pop servers)))
823 (nnmairix-open-server
824 (nth 1 (gnus-server-to-method cur)))
825 (setq commandsplit (split-string nnmairix-mairix-command))
826 (nnheader-message 7 "Updating mairix database for %s..." cur)
827 (if nnmairix-mairix-synchronous-update
829 (setq args (append (list (car commandsplit) nil
830 (get-buffer nnmairix-mairix-output-buffer)
832 (if (> (length commandsplit) 1)
833 (setq args (append args (cdr commandsplit) nnmairix-mairix-update-options))
834 (setq args (append args nnmairix-mairix-update-options)))
835 (apply 'call-process args)
836 (nnheader-message 7 "Updating mairix database for %s... done" cur))
838 (setq args (append (list cur (get-buffer nnmairix-mairix-output-buffer)
839 (car commandsplit))))
840 (if (> (length commandsplit) 1)
841 (setq args (append args (cdr commandsplit) nnmairix-mairix-update-options))
842 (setq args (append args nnmairix-mairix-update-options)))
843 (set-process-sentinel (apply 'start-process args)
844 'nnmairix-sentinel-mairix-update-finished))))))
846 (defun nnmairix-goto-original-article (&optional no-registry)
847 "Jump to the original group and display article.
848 The original group of the article is first determined with the
849 registry (if enabled). If the registry is not enabled or did not
850 find the article or the prefix NO-REGISTRY is non-nil, this
851 function will try to determine the original group form the path
852 of the mail file. The path is obtained through another mairix
855 (when (not (eq (car gnus-current-select-method) 'nnmairix))
856 (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
857 (if (eq (car method) 'nnmairix)
858 (nnmairix-open-server (nth 1 method))
859 (error "Not in a nnmairix group"))))
860 (when (not (gnus-buffer-live-p gnus-article-buffer))
861 (error "No article buffer available"))
862 (let ((server (nth 1 gnus-current-select-method))
863 mid rval group allgroups)
866 (set-buffer gnus-article-buffer)
867 (gnus-summary-toggle-header 1)
868 (setq mid (message-fetch-field "Message-ID"))
869 ;; first check the registry (if available)
870 (when (and (boundp 'gnus-registry-install)
871 gnus-registry-install
873 (setq group (gnus-registry-fetch-group mid)))
874 (while (string-match "[<>]" mid)
875 (setq mid (replace-match "" t t mid)))
877 ;; registry was not available or did not find article
878 ;; so we search again with mairix in raw mode to get filename
879 (nnmairix-open-server server)
881 (nnmairix-call-mairix-binary-raw
882 (split-string nnmairix-mairix-command)
883 (list (concat "m:" mid))))
885 ;; determine original group(s) from filename
887 (set-buffer nnmairix-mairix-output-buffer)
888 (goto-char (point-min))
889 (while (looking-at "/")
890 (push (nnmairix-determine-original-group)
893 (if (> (length allgroups) 1)
896 "Message exists in more than one group. Choose: "
898 (setq group (car allgroups))))
899 (error "Mairix could not find original article. See buffer %s for details"
900 nnmairix-mairix-output-buffer))))
902 ;; show article in summary buffer
903 (nnmairix-show-original-article group mid)
904 (message "Couldn't find original article"))))
906 (defun nnmairix-determine-original-group ()
907 "Try to determine to original group from the file path."
908 (let (path filename serverbase group maildirflag allgroups)
909 (re-search-forward "^\\(.*\\)/\\(.*?\\)$")
910 (setq path (expand-file-name (match-string 1)))
911 (setq filename (match-string 2))
912 ;; when we deal with maildir, remove cur/new/tmp from path
913 (setq maildirflag (string-match ".+\\..+\\..+" filename))
916 (replace-regexp-in-string
917 ".*\\(/cur\\|/new\\|/tmp\\)$" "" path t t 1)))
918 ;; we first check nnml and nnmaildir servers
922 (dolist (cur gnus-opened-servers)
923 (when (or (and (not maildirflag)
924 (eq (caar cur) 'nnml))
926 (eq (caar cur) 'nnmaildir)))
927 ;; get base path from server
929 (setq serverbase (cadr (assoc 'directory (car cur))))
930 (setq serverbase (cadr (assoc 'nnml-directory (car cur))))
931 (when (not serverbase)
932 (setq serverbase nnml-directory)))
933 (setq serverbase (file-name-as-directory
934 (expand-file-name serverbase)))
935 (when (string-match (concat serverbase "\\(.*\\)") path)
936 ;; looks good - rest of the path should be the group
937 (setq group (match-string 1 path))
938 (when (string-match "/$" group)
939 (setq group (replace-match "" t t group)))
940 (when (not maildirflag)
941 ;; for nnml: convert slashes to dots
942 (while (string-match "/" group)
943 (setq group (replace-match "." t t group))))
944 (setq group (gnus-group-prefixed-name group (car cur)))
945 ;; check whether this group actually exists
946 (when (gnus-group-entry group)
947 (throw 'found group)))))))
949 ;; we haven't found it yet --> look for nnimap groups
950 ;; assume last element of the path is the group
951 (string-match "^.*/\\.?\\(.*\\)$" path)
952 (setq group (match-string 1 path))
953 ;; convert dots to slashes (nested group)
954 (while (string-match "\\." group)
955 (setq group (replace-match "/" t t group)))
956 (dolist (cur gnus-opened-servers)
957 (when (eq (caar cur) 'nnimap)
958 (when (gnus-group-entry
959 (gnus-group-prefixed-name group (car cur)))
961 (gnus-group-prefixed-name group (car cur))
963 (if (> (length allgroups) 1)
964 (setq group (completing-read
965 "Group %s exists on more than one IMAP server. Choose: "
967 (setq group (car allgroups))))
971 ;;; ==== Helper functions
973 (defun nnmairix-request-group-with-article-number-correction (folder qualgroup)
974 "Request FOLDER on backend for nnmairix QUALGROUP and article number correction."
976 (nnmairix-call-backend
977 "request-group" folder nnmairix-backend-server fast)
978 (set-buffer nnmairix-mairix-output-buffer)
979 (goto-char (point-min))
980 (re-search-forward "^Matched.*messages")
981 (nnheader-message 7 (match-string 0))
982 (set-buffer nntp-server-buffer)
983 (goto-char (point-min))
984 (let ((status (read (current-buffer)))
985 (total (read (current-buffer)))
986 (low (read (current-buffer)))
987 (high (read (current-buffer)))
988 (corr (gnus-group-get-parameter qualgroup 'numcorr t)))
991 ;; Article number correction
993 (> (+ (car (cddr corr)) high) 0))
995 (when (car corr) ;Group has changed
999 (+ (car (cddr corr)) high)))
1000 (gnus-group-set-parameter
1001 qualgroup 'numcorr corr))
1002 (setq low (+ low (cadr corr))
1003 high (+ high (cadr corr))))
1004 (when (member nnmairix-backend
1005 nnmairix-delete-and-create-on-change)
1006 (gnus-group-set-parameter
1007 qualgroup 'numcorr (list nil 0 high))))
1009 (insert (format "%d %d %d %d %s" status total low high group))
1013 'nnmairix "Error calling back end on group %s" folder)
1016 (defun nnmairix-call-mairix-binary (command folder query threads)
1017 "Call mairix binary with COMMAND, using FOLDER and QUERY.
1018 If THREADS is non-nil, enable full threads."
1019 (let ((args (cons (car command) '(nil t nil))))
1022 (get-buffer-create nnmairix-mairix-output-buffer))
1024 (when (> (length command) 1)
1025 (setq args (append args (cdr command))))
1027 (setq args (append args '("-t"))))
1028 (apply 'call-process
1029 (append args (list "-o" folder) query)))))
1031 (defun nnmairix-call-mairix-binary-raw (command query)
1032 "Call mairix binary with COMMAND and QUERY in raw mode."
1033 (let ((args (cons (car command) '(nil t nil))))
1036 (get-buffer-create nnmairix-mairix-output-buffer))
1038 (when (> (length command) 1)
1039 (setq args (append args (cdr command))))
1040 (setq args (append args '("-r")))
1041 (apply 'call-process
1042 (append args query)))))
1044 (defun nnmairix-get-server ()
1045 "If there exists just one nnmairix server, return its value.
1046 Otherwise, ask user for server."
1047 (let ((openedserver (nnmairix-get-nnmairix-servers)))
1048 (when (not openedserver)
1049 (error "No opened nnmairix server found"))
1050 (if (> (length openedserver) 1)
1054 (setq nnmairix-last-server
1055 (list (completing-read "Server: " openedserver nil 1
1056 (or nnmairix-last-server
1058 nnmairix-last-server)
1059 (car openedserver))))
1061 (defun nnmairix-get-nnmairix-servers (&optional all)
1062 "Return available nnmairix servers.
1063 If ALL is t, return also the unopened/failed ones."
1064 (let ((alist gnus-opened-servers)
1065 server openedserver)
1067 (setq server (pop alist))
1070 (eq (cadr server) 'ok))
1071 (eq (caar server) 'nnmairix)
1072 (not (member (car server) gnus-ephemeral-servers)))
1074 (concat (symbol-name (caar server)) ":" (nth 1 (car server))))
1075 (push (list server) openedserver)))
1079 (defun nnmairix-get-valid-servers ()
1080 "Return list of valid backend servers for nnmairix groups."
1081 (let ((alist gnus-opened-servers)
1082 (mairixservers (nnmairix-get-nnmairix-servers t))
1083 server mserver openedserver occ cur)
1084 ;; Get list of all nnmairix backends (i.e. backends which are
1085 ;; already occupied)
1086 (dolist (cur mairixservers)
1090 (cadr (assoc 'nnmairix-backend
1091 (gnus-server-to-method (car cur)))))
1093 (cadr (assoc 'nnmairix-backend-server
1094 (gnus-server-to-method (car cur)))))
1097 (setq server (pop alist))
1098 (setq mserver (gnus-method-to-server (car server)))
1099 ;; If this is the native server, convert it to the real server
1100 ;; name to avoid confusion
1101 (when (string= mserver "native")
1102 (setq mserver (format "%s:%s"
1104 (nth 1 (car server)))))
1106 (eq (cadr server) 'ok)
1107 (member (caar server) nnmairix-valid-backends)
1108 (not (member (car server) gnus-ephemeral-servers))
1109 (not (member (gnus-method-to-server (car server)) occ)))
1115 (defun nnmairix-call-backend (func &rest args)
1116 "Call a function FUNC on backend with ARGS."
1117 (apply (intern (format "%s-%s" (symbol-name nnmairix-backend) func)) args))
1119 (defun nnmairix-get-backend-folder (group &optional server)
1120 "Return back end GROUP from nnmairix group on SERVER."
1121 (let* ((qualgroup (if server
1122 (gnus-group-prefixed-name group (list 'nnmairix server))
1124 (folder (gnus-group-get-parameter qualgroup 'folder)))
1127 (defun nnmairix-get-numcorr (group &optional server)
1128 "Return values for article number correction nnmairix GROUP on SERVER."
1129 (let* ((qualgroup (if server
1130 (gnus-group-prefixed-name group (list 'nnmairix server))
1132 (corr (gnus-group-get-parameter qualgroup 'numcorr t)))
1136 (defun nnmairix-rename-files-consecutively (path)
1137 "Rename all nnml mail files in PATH so that they have consecutive numbers.
1138 This should correct problems of wrong article counts when using
1139 nnmairix with nnml backends."
1142 (mapcar 'string-to-number
1143 (directory-files path nil "[0-9]+" t))
1145 (lastplusone (car files))
1146 (path (file-name-as-directory path)))
1148 (when (not (= cur lastplusone))
1149 (rename-file (concat path
1150 (number-to-string cur))
1152 (number-to-string lastplusone)))
1153 (setq cur lastplusone))
1154 (setq lastplusone (1+ cur)))))
1156 (defun nnmairix-replace-group-and-numbers (articles backendgroup mairixgroup numc)
1157 "Replace folder names in Xref header and correct article numbers.
1158 Do this for all ARTICLES on BACKENDGROUP. Replace using
1159 MAIRIXGROUP. NUMC contains values for article number correction."
1160 (let ((buf (get-buffer-create " *nnmairix buffer*"))
1161 (corr (not (zerop numc)))
1162 (name (buffer-name nntp-server-buffer))
1167 (set-buffer nntp-server-buffer)
1168 (goto-char (point-min))
1169 (nnheader-message 7 "nnmairix: Rewriting headers...")
1173 (when (or (looking-at (number-to-string article))
1174 (nnheader-find-nov-line article))
1175 (setq cur (nnheader-parse-nov))
1177 (setq article (+ (mail-header-number cur) numc))
1178 (mail-header-set-number cur article))
1179 (setq xref (mail-header-xref cur))
1180 (when (and (stringp xref)
1181 (string-match (format "[ \t]%s:[0-9]+" backendgroup) xref))
1182 (setq xref (replace-match (format " %s:%d" mairixgroup article) t nil xref))
1183 (mail-header-set-xref cur xref))
1185 (nnheader-insert-nov cur)
1186 (set-buffer nntp-server-buffer)
1188 (forward-line 1)))))
1190 (nnheader-message 7 "nnmairix: Rewriting headers... done")
1191 (kill-buffer nntp-server-buffer)
1193 (rename-buffer name)
1194 (setq nntp-server-buffer buf))))
1196 (defun nnmairix-backend-to-server (server)
1197 "Return nnmairix server most probably responsible for back end SERVER.
1198 User will be asked if this cannot be determined. Result is saved in
1199 parameter 'indexed-servers of corresponding default search
1201 (let ((allservers (nnmairix-get-nnmairix-servers))
1202 mairixserver found defaultgroup)
1203 (if (> (length allservers) 1)
1205 ;; If there is more than one nnmairix server, we go through them
1206 (while (and allservers (not found))
1207 (setq mairixserver (gnus-server-to-method (car (pop allservers))))
1208 ;; First we look if SERVER is the backend of current nnmairix server
1209 (setq found (and (eq (cadr (assoc 'nnmairix-backend mairixserver))
1211 (string= (cadr (assoc 'nnmairix-backend-server mairixserver))
1213 ;; If that's not the case, we look at 'indexed-servers
1214 ;; variable in default search group
1216 (setq defaultgroup (cadr (assoc 'nnmairix-default-group mairixserver)))
1217 (setq found (member (gnus-method-to-server server)
1218 (gnus-group-get-parameter
1219 (gnus-group-prefixed-name defaultgroup
1221 'indexed-servers t)))))
1222 ;; If still not found, we ask user
1225 (gnus-server-to-method
1227 (format "Cannot determine which nnmairix server indexes %s. Please specify: "
1228 (gnus-method-to-server server))
1229 (nnmairix-get-nnmairix-servers) nil nil "nnmairix:")))
1230 ;; Save result in parameter of default search group so that
1231 ;; we don't have to ask again
1232 (setq defaultgroup (gnus-group-prefixed-name
1233 (cadr (assoc 'nnmairix-default-group mairixserver)) mairixserver))
1234 (gnus-group-set-parameter
1237 (append (gnus-group-get-parameter defaultgroup 'indexed-servers t)
1238 (list (gnus-method-to-server server)))))
1240 ;; If there is just one (or none) nnmairix server:
1241 (gnus-server-to-method (caar allservers)))))
1243 (defun nnmairix-update-and-clear-marks (group &optional method)
1244 "Update group and clear all marks from GROUP using METHOD."
1246 (setq group (gnus-group-prefixed-name group method)))
1247 (let ((method (or method
1248 (gnus-find-method-for-group group)))
1249 (folder (gnus-group-get-parameter group 'folder))
1250 (corr (gnus-group-get-parameter group 'numcorr t))
1252 (if (eq (nth 0 method) 'nnmairix)
1254 (nnmairix-open-server (nth 1 method))
1255 (set-buffer gnus-group-buffer)
1256 (setq info (gnus-get-info group))
1257 ;; Clear active and info
1258 (gnus-set-active group nil)
1259 (gnus-info-clear-data info)
1260 ;; Delete and re-create group if needed
1261 (when (member nnmairix-backend nnmairix-delete-and-create-on-change)
1262 (if (string-match nnmairix-group-regexp folder)
1264 (nnmairix-call-backend "open-server"
1265 nnmairix-backend-server)
1266 (nnmairix-call-backend "request-delete-group"
1267 folder t nnmairix-backend-server)
1268 (nnmairix-call-backend "request-create-group"
1269 folder nnmairix-backend-server)
1270 ;; set flag that group has changed for article number correction
1273 (gnus-group-set-parameter group 'numcorr corr)))
1274 (error "Nnmairix-update-and-clear-marks - delete/create with\
1275 non-mairix group!! - check folder parameter")))
1276 (when (gnus-group-jump-to-group group)
1277 (gnus-group-get-new-news-this-group)))
1278 (error "Nnmairix-update-and-clear-marks - Called with non-nnmairix group"))))
1281 (defun nnmairix-sentinel-mairix-update-finished (proc status)
1282 "Sentinel for mairix update process PROC with STATUS."
1283 (if (equal status "finished\n")
1284 (nnheader-message 7 "Updating mairix database for %s... done" proc)
1285 (error "There was an error updating the mairix database for server %s. \
1286 See %s for details" proc nnmairix-mairix-output-buffer)))
1288 (defun nnmairix-create-message-line-for-search ()
1289 "Create message line for interactive query in minibuffer."
1293 (format "%c=%s" (car cur) (nth 3 cur))))
1294 nnmairix-interactive-query-parameters ","))
1296 (defun nnmairix-replace-illegal-chars (header)
1297 "Replace illegal characters in HEADER for mairix query."
1299 (if (> emacs-major-version 20)
1300 (while (string-match "[^-.@/,& [:alnum:]]" header)
1301 (setq header (replace-match "" t t header)))
1302 (while (string-match "[[]{}:<>]" header)
1303 (setq header (replace-match "" t t header))))
1304 (while (string-match "[-& ]" header)
1305 (setq header (replace-match "," t t header)))
1308 (defun nnmairix-show-original-article (group mid)
1309 "Switch to GROUP and display Article with message-id MID."
1310 (when (string-match "Summary" (buffer-name (current-buffer)))
1311 (gnus-summary-exit))
1312 (pop-to-buffer gnus-group-buffer)
1313 (gnus-group-jump-to-group group)
1314 (gnus-summary-read-group group 1 t)
1315 (gnus-summary-refer-article mid)
1316 (gnus-summary-limit-to-headers (format "message-id: <%s>" mid))
1317 (gnus-summary-select-article)
1319 (gnus-summary-show-article)
1320 (nnheader-message 5 "Switched to group %s." group))
1323 ;; ==== Widget stuff
1325 (defvar nnmairix-widgets)
1326 (defvar nnmairix-widgets-values nil)
1328 (defun nnmairix-widget-search-from-this-article ()
1329 "Create mairix query based on current article using graphical widgets."
1331 (nnmairix-widget-search
1332 (nnmairix-widget-get-values)))
1335 (defun nnmairix-widget-get-values ()
1336 "Create values for editable fields from current article."
1337 (if (not (gnus-buffer-live-p gnus-article-buffer))
1338 (error "No article buffer available")
1340 (gnus-summary-toggle-header 1)
1341 (set-buffer gnus-article-buffer)
1347 (nnmairix-replace-illegal-chars
1348 (gnus-fetch-field (car field)))
1350 nnmairix-widget-fields-list))))
1353 (defun nnmairix-widget-search (&optional mvalues)
1354 "Create mairix query interactively using graphical widgets.
1355 MVALUES may contain values from current article."
1357 ;; Select window for mairix customization
1358 (funcall nnmairix-widget-select-window-function)
1360 (nnmairix-widget-create-query mvalues)
1362 (widget-create 'push-button
1365 (lambda (&rest ignore)
1366 (nnmairix-widget-send-query nnmairix-widgets
1368 (lambda (&rest ignore)
1369 (nnmairix-widget-send-query nnmairix-widgets
1373 (widget-create 'push-button
1376 (lambda (&rest ignore)
1377 (nnmairix-widget-create-group nnmairix-widgets
1379 (lambda (&rest ignore)
1380 (nnmairix-widget-create-group nnmairix-widgets
1382 "Create permanent group")
1384 (widget-create 'push-button
1385 :notify (lambda (&rest ignore)
1386 (kill-buffer nnmairix-customize-query-buffer))
1388 (use-local-map widget-keymap)
1390 (goto-char (point-min)))
1392 (defun nnmairix-widget-send-query (widgets &optional withvalues)
1393 "Send query from WIDGETS to mairix binary.
1394 If WITHVALUES is t, query is based on current article."
1396 (nnmairix-widget-make-query-from-widgets widgets)
1398 (gnus-method-to-server
1399 (nnmairix-backend-to-server gnus-current-select-method))
1400 (car (nnmairix-get-server)))
1401 (if (widget-value (cadr (assoc "Threads" widgets)))
1404 (kill-buffer nnmairix-customize-query-buffer))
1406 (defun nnmairix-widget-create-group (widgets &optional withvalues)
1407 "Create nnmairix group based on current widget values WIDGETS.
1408 If WITHVALUES is t, query is based on current article."
1409 (let ((group (read-string "Name of the group: ")))
1410 (when (not (zerop (length group)))
1411 (nnmairix-create-search-group
1413 (gnus-method-to-server
1414 (nnmairix-backend-to-server gnus-current-select-method))
1415 (car (nnmairix-get-server)))
1417 (nnmairix-widget-make-query-from-widgets widgets)
1418 (widget-value (cadr (assoc "Threads" widgets))))))
1419 (kill-buffer nnmairix-customize-query-buffer))
1422 (defun nnmairix-widget-make-query-from-widgets (widgets)
1423 "Create mairix query from widget values WIDGETS."
1424 (let (query temp flag)
1425 ;; first we do the editable fields
1426 (dolist (cur nnmairix-widget-fields-list)
1427 ;; See if checkbox is checked
1429 (cadr (assoc (concat "c" (caddr cur)) widgets)))
1430 ;; create query for the field
1435 (nnmairix-replace-illegal-chars
1437 (cadr (assoc (concat "e" (caddr cur)) widgets)))))
1440 (when (member 'flags nnmairix-widget-other)
1446 (widget-value (cadr (assoc (car flag) nnmairix-widgets))))
1447 (if (string= "yes" temp)
1449 (if (string= "no" temp)
1450 (concat "-" (cadr flag))))))
1451 '(("seen" "s") ("replied" "r") ("flagged" "f")) ""))
1452 (when (not (zerop (length flag)))
1453 (push (concat "F:" flag) query)))
1454 ;; return query string
1455 (mapconcat 'identity query " ")))
1458 (defun nnmairix-widget-create-query (&optional values)
1459 "Create widgets for creating mairix queries.
1460 Fill in VALUES if based on an article."
1462 (when (get-buffer nnmairix-customize-query-buffer)
1463 (kill-buffer nnmairix-customize-query-buffer))
1464 (switch-to-buffer nnmairix-customize-query-buffer)
1465 (kill-all-local-variables)
1467 (widget-insert "Specify your query for Mairix (check boxes for activating fields):\n\n")
1468 (widget-insert "(Whitespaces will be converted to ',' (i.e. AND). Use '/' for OR.)\n\n")
1469 ; (make-local-variable 'nnmairix-widgets)
1470 (setq nnmairix-widgets (nnmairix-widget-build-editable-fields values))
1471 (when (member 'flags nnmairix-widget-other)
1472 (widget-insert "\nFlags:\n Seen: ")
1473 (nnmairix-widget-add "seen"
1476 '(item "yes") '(item "no") '(item "ignore"))
1477 (widget-insert " Replied: ")
1478 (nnmairix-widget-add "replied"
1481 '(item "yes") '(item "no") '(item "ignore"))
1482 (widget-insert " Ticked: ")
1483 (nnmairix-widget-add "flagged"
1486 '(item "yes") '(item "no") '(item "ignore")))
1487 (when (member 'threads nnmairix-widget-other)
1488 (widget-insert "\n")
1489 (nnmairix-widget-add "Threads" 'checkbox nil))
1490 (widget-insert " Show full threads\n\n")))
1493 (defun nnmairix-widget-build-editable-fields (values)
1494 "Build editable field widgets in `nnmairix-widget-fields-list'.
1495 VALUES may contain values for editable fields from current article."
1496 ;; how can this be done less ugly?
1501 (setq field (caddr field))
1507 (widget-create 'checkbox
1509 :notify (lambda (widget &rest ignore)
1510 (nnmairix-widget-toggle-activate widget))
1515 (widget-create 'editable-field
1517 :format (concat " " field ":"
1518 (make-string (- 11 (length field)) ?\ )
1520 :value (or (cadr (assoc field values)) ""))))
1522 (widget-insert "\n")
1523 ;; Deactivate editable field
1524 (widget-apply (cadr (nth 1 ret)) :deactivate)))
1525 nnmairix-widget-fields-list)
1528 (defun nnmairix-widget-add (name &rest args)
1529 "Add a widget NAME with optional ARGS."
1532 (apply 'widget-create args))
1535 (defun nnmairix-widget-toggle-activate (widget)
1536 "Toggle activation status of WIDGET dependent on corresponding checkbox value."
1537 (let ((field (widget-get widget :tag)))
1538 (if (widget-value widget)
1540 (cadr (assoc (concat "e" field) nnmairix-widgets))
1543 (cadr (assoc (concat "e" field) nnmairix-widgets))
1548 ;;; nnmairix.el ends here