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 part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; THIS IS BETA SOFTWARE! This back end should not mess up or
29 ;; even delete your mails, but having a backup is always a good idea.
31 ;; This is a back end for using the mairix search engine with
32 ;; Gnus. Mairix is a tool for searching words in locally stored
33 ;; mail. Mairix is very fast which allows using it efficiently for
34 ;; "smart folders", e.g. folders which are associated with search
35 ;; queries. Of course, you can also use this back end just for
36 ;; calling mairix with some search query.
38 ;; Mairix is written by Richard Curnow. More information can be found at
39 ;; http://www.rpcurnow.force9.co.uk/mairix/
41 ;; For details about setting up mairix&Gnus&nnmairix.el, look at the
44 ;; http://www.emacswiki.org/cgi-bin/wiki/GnusMairix
46 ;; The newest version of nnmairix.el can be found at
48 ;; http://www.emacswiki.org/cgi-bin/emacs/nnmairix.el
50 ;; For impatient people, here's the setup in a nutshell:
52 ;; This back end requires an installed mairix binary which is
53 ;; configured to index your mail folder. You don't have to specify a
54 ;; search folder (but it does no harm, either). Visit the man page of
55 ;; mairix and mairixrc for details.
57 ;; Put nnmairix.el into your search path and "(require 'nnmarix)" into
58 ;; your .gnus. Then call nnmairix-create-default-group (or 'G b
59 ;; c'). This function will ask for all necessary information to create
60 ;; a mairix server in Gnus with the default search folder. This
61 ;; default search folder will be used for all temporary searches: call
62 ;; nnmairix-search ('G b s') and enter a mairix query (like
63 ;; f:test@example.com). To create a mairix group for one specific
64 ;; search query, use 'G b g'. See the emacswiki or the source for more
67 ;; Commentary on the code: nnmairix sits between Gnus and the "real"
68 ;; back end which handles the mail (currently nnml, nnimap and
69 ;; nnmaildir were tested). I know this is all a bit hacky, but so far
70 ;; it works for me. This is the first back end I've written for Gnus,
71 ;; so I'd appreciate any comments, suggestions, bug reports (and, of
72 ;; course, patches) for improving nnmairix.
74 ;; nnmairix does not use an active file, since I wanted to contain the
75 ;; back end "inside Gnus" as much as possible without the need of an
76 ;; external file. It stores the query/folder information in the group
77 ;; parameters instead. This also implies that once you kill a mairix
78 ;; group, it's gone for good. I don't think that this is really
79 ;; problematic, since I don't see the need in unsubscribing and
80 ;; re-subscribing search groups
82 ;; Every mairix server is "responsible" for one mairix installation,
83 ;; i.e. you can have several mairix servers for different mairix
84 ;; configurations. Not that I think anyone will actually do this, but
85 ;; I thought it would be a "nice to have feature"...
88 ;; * When using Maildir: path and filename of a mail can change due to
89 ;; reading/replying/etc. This can lead to dangling symlinks in
90 ;; nnmairix groups and it depends on the back end how well it deals
91 ;; with that (some IMAP servers out there may not be amused). Update the
92 ;; database ('G b u') and the group to fix it.
93 ;; * Mairix does only support us-ascii characters.
95 ;; TODO/MISSING FEATURES:
96 ;; * Possibility to propagate flags like seen, replied, ticked
97 ;; to original message
98 ;; * Support of more back ends (nnmh, nnfolder, nnmbox...)?
99 ;; * Maybe use an active file instead of group parameters?
100 ;; * Use "-a" when updating groups which are not newly created
104 ;; 02/06/2008 - version 0.5
106 ;; * New function: nnmairix-goto-original-article. Uses the
107 ;; registry or the mail file path for determining original group.
109 ;; * Deal with empty Xref header
111 ;; * Changed summary mode keybindings since the old ones were
114 ;; (Thanks to Tassilo Horn and Ted Zlatanov for their help)
116 ;; 01/07/2008 - version 0.4
118 ;; * New/fixed doc strings and code cleanup.
120 ;; 18/11/2007 - version 0.3
122 ;; * Fixed bugs when dealing with nnml and native servers
124 ;; * Make variables customizable
126 ;; 10/10/2007 - version 0.2
128 ;; * Use nnml-directory/directory server variables for nnml and
129 ;; nnmaildir backends as path for search folders. This way it
130 ;; becomes independent of 'base' setting in .mairixirc (but not for
133 ;; * As a result: Changed nnmairix-backend-to-server so that user
134 ;; is asked when more than one nnmairix server exists and we do not
135 ;; know which one is responsible for current backend.
137 ;; * Rename files when using nnml backends so that there are no
138 ;; holes in article numbers. This should fix all problems regarding
139 ;; wrong article counts with nnml.
141 ;; * More commands for creating queries (using widgets or the
144 ;; * Fixed bug in nnmairix-create-search-group-from-message
146 ;; * Changed copyright to FSF
148 ;; (Thanks to Georg C. F. Greve and Bastien for suggestions and
151 ;; 10/03/2007 - version 0.1 - first release
157 (require 'gnus-group
)
163 (nnoo-declare nnmairix
)
168 (defun nnmairix-group-mode-hook ()
169 "Nnmairix group mode keymap."
170 (define-key gnus-group-mode-map
171 (kbd "G b") (make-sparse-keymap))
172 (define-key gnus-group-mode-map
173 (kbd "G b g") 'nnmairix-create-search-group
)
174 (define-key gnus-group-mode-map
175 (kbd "G b c") 'nnmairix-create-server-and-default-group
)
176 (define-key gnus-group-mode-map
177 (kbd "G b q") 'nnmairix-group-change-query-this-group
)
178 (define-key gnus-group-mode-map
179 (kbd "G b t") 'nnmairix-group-toggle-threads-this-group
)
180 (define-key gnus-group-mode-map
181 (kbd "G b u") 'nnmairix-update-database
)
182 (define-key gnus-group-mode-map
183 (kbd "G b s") 'nnmairix-search
)
184 (define-key gnus-group-mode-map
185 (kbd "G b i") 'nnmairix-search-interactive
)
186 (define-key gnus-group-mode-map
187 (kbd "G b m") 'nnmairix-widget-search
))
190 (defun nnmairix-summary-mode-hook ()
191 "Nnmairix summary mode keymap."
192 (define-key gnus-summary-mode-map
193 (kbd "$ t") 'nnmairix-search-thread-this-article
)
194 (define-key gnus-summary-mode-map
195 (kbd "$ f") 'nnmairix-search-from-this-article
)
196 (define-key gnus-summary-mode-map
197 (kbd "$ m") 'nnmairix-widget-search-from-this-article
)
198 (define-key gnus-summary-mode-map
199 (kbd "$ g") 'nnmairix-create-search-group-from-message
)
200 (define-key gnus-summary-mode-map
201 (kbd "$ o") 'nnmairix-goto-original-article
))
203 (add-hook 'gnus-group-mode-hook
'nnmairix-group-mode-hook
)
204 (add-hook 'gnus-summary-mode-hook
'nnmairix-summary-mode-hook
)
207 ;; Customizable stuff
209 (defgroup nnmairix nil
210 "Backend for the Mairix mail search engine."
213 (defcustom nnmairix-group-prefix
"zz_mairix"
214 "Prefix for mairix search groups on back end server.
215 nnmairix will create these groups automatically on the back end
216 server for each nnmairix search group. The name on the back end
217 server will be this prefix plus a random number. You can delete
218 unused nnmairix groups on the back end using
219 `nnmairix-purge-old-groups'."
224 (defcustom nnmairix-mairix-output-buffer
"*mairix output*"
225 "Buffer used for mairix output."
230 (defcustom nnmairix-customize-query-buffer
"*mairix query*"
231 "Name of the buffer for customizing Mairix queries."
236 (defcustom nnmairix-mairix-update-options
'("-F" "-Q")
237 "Options when calling mairix for updating the database.
238 The default is '-F' and '-Q' for making updates faster. You
239 should call mairix without these options from time to
240 time (e.g. via cron job)."
242 :type
'(repeat string
)
245 (defcustom nnmairix-mairix-synchronous-update nil
246 "Set this to t if you want Emacs to wait for mairix updating the database."
251 (defcustom nnmairix-rename-files-for-nnml t
252 "Rename nnml mail files so that they are consecutively numbered.
253 When using nnml as backend, mairix might produce holes in the
254 article numbers which will produce wrong article counts by
255 Gnus. This option controls whether nnmairix should rename the
256 files consecutively."
261 (defcustom nnmairix-widget-fields-list
262 '(("from" "f" "From") ("to" "t" "To") ("cc" "c" "Cc")
263 ("subject" "s" "Subject") ("to" "tc" "To or Cc")
264 ("from" "a" "Address") (nil "b" "Body") (nil "n" "Attachment")
265 ("Message-ID" "m" "Message ID") (nil "s" "Size") (nil "d" "Date"))
266 "Fields that should be editable during interactive query customization.
268 Header, corresponding mairix command and description for editable
269 fields in interactive query customization. The header specifies
270 which header contents should be inserted into the editable field
271 when creating a Mairix query based on the current message (can be
272 nil for disabling this)."
276 (const :tag
"none" nil
)
277 (const :tag
"From" "from")
278 (const :tag
"To" "to")
279 (const :tag
"Cc" "cc")
280 (const :tag
"Subject" "subject")
281 (const :tag
"Message ID" "Message-ID"))
282 (string :tag
"Command")
283 (string :tag
"Description")))
286 (defcustom nnmairix-widget-select-window-function
287 (lambda () (select-window (get-largest-window)))
288 "Function for selecting the window for customizing the mairix query.
289 The default chooses the largest window in the current frame."
294 ;; ==== Other variables
296 (defvar nnmairix-widget-other
298 "Other editable mairix commands when using customization widgets.
299 Currently there are 'threads and 'flags.")
301 (defvar nnmairix-interactive-query-parameters
302 '((?f
"from" "f" "From") (?t
"to" "t" "To") (?c
"to" "tc" "To or Cc")
303 (?a
"from" "a" "Address") (?s
"subject" "s" "Subject") (?b nil
"b" "Body")
304 (?d nil
"d" "Date") (?n nil
"n" "Attachment"))
305 "Things that should be editable during interactive query generation.
306 Every list element consists of the following entries: Keystroke,
307 message field (if any), mairix command and description.")
309 (defvar nnmairix-delete-and-create-on-change
'(nnimap nnmaildir nnml
)
310 "Controls on which backends groups should be deleted and re-created.
311 This variable is a list of back ends where the search group should
312 be completely deleted and re-created when the query or thread
313 parameter changes. I know this is rather \"brute force\" and maybe
314 even dangerous (you have backups, right?), but it should be used at
315 least for nnimap since some IMAP servers are really not amused when
316 mailbox content changes behind their back. It usually also corrects
317 the problem of \"holes\" in the article numbers which often lead to a
318 wrong count of total articles shown by Gnus.")
320 ;;; === Server variables
322 (defvoo nnmairix-backend nil
323 "Backend where mairix stores its searches.")
325 (defvoo nnmairix-backend-server nil
326 "Name of the server where mairix stores its searches.")
328 (defvoo nnmairix-mairix-command
"mairix"
329 "Command to call mairix for this nnmairix server.")
331 (defvoo nnmairix-hidden-folders nil
332 "Set this to t if the back end server uses hidden directories for
333 its maildir mail folders (e.g. the Dovecot IMAP server or mutt).")
335 (defvoo nnmairix-default-group nil
336 "Default search group. This is the group which is used for all
337 temporary searches, e.g. nnmairix-search.")
339 ;;; === Internal variables
341 (defconst nnmairix-group-regexp
342 (format "%s-\\(.*\\)-[0-9]+" nnmairix-group-prefix
)
343 "Regexp for mairix groups on back end.")
345 (defconst nnmairix-valid-backends
'(nnimap nnml nnmaildir
)
346 "Backends supported by nnmairix.
347 Other backends might or might not work.")
349 (defvar nnmairix-last-server nil
350 "Last chosen server.")
352 (defvar nnmairix-current-server nil
355 ;;; === Gnus backend functions
357 (nnoo-define-basics nnmairix
)
359 (gnus-declare-backend "nnmairix" 'mail
'address
)
361 (deffoo nnmairix-open-server
(server &optional definitions
)
362 ;; just set server variables
363 (setq nnmairix-current-server server
)
364 (nnoo-change-server 'nnmairix server definitions
))
366 (deffoo nnmairix-request-group
(group &optional server fast
)
367 ;; Call mairix and request group on back end server
368 (when server
(nnmairix-open-server server
))
369 (let* ((qualgroup (if server
370 (gnus-group-prefixed-name group
(list 'nnmairix server
))
372 (query (gnus-group-get-parameter qualgroup
'query t
))
373 (folder (gnus-group-get-parameter qualgroup
'folder
))
374 (threads (gnus-group-get-parameter qualgroup
'threads
))
375 (backendmethod (gnus-server-to-method
376 (format "%s:%s" (symbol-name nnmairix-backend
)
377 nnmairix-backend-server
)))
378 rval mfolder folderpath
)
381 ;; No folder parameter -> error
382 (nnheader-report 'nnmairix
"Check folder parameter for group %s" group
)
385 ;; No query -> return empty group
387 (set-buffer nntp-server-buffer
)
389 (insert (concat "211 0 1 0 " group
))
392 ;; For maildir++ folders: create a hidden directory (prepend dot)
393 (setq mfolder
(if (and nnmairix-hidden-folders
394 (not (string-match "^\\." folder
)))
397 ;; For nnml and nnmaildir, precede mfolder with directory where mail
398 ;; is actually stored so that it's independent of 'base' setting
400 (when (eq nnmairix-backend
'nnml
)
401 (setq folderpath
(cadr (assoc 'nnml-directory backendmethod
)))
402 ;; if nnml-directory is not explicitly set, use global value
403 (when (not folderpath
)
404 (setq folderpath nnml-directory
)))
405 (when (eq nnmairix-backend
'nnmaildir
)
407 (cadr (assoc 'directory backendmethod
))))
411 (file-name-as-directory
415 ;; If (not fast), call Mairix binary
418 (nnmairix-call-mairix-binary
419 (split-string nnmairix-mairix-command
)
420 mfolder query threads
)))
421 ;; Check return value
423 ((zerop rval
) ; call was succesful
424 (nnmairix-call-backend
425 "open-server" nnmairix-backend-server
)
426 ;; If we're dealing with nnml, rename files
427 ;; consecutively and make new active file for this
429 (when (eq nnmairix-backend
'nnml
)
430 (when nnmairix-rename-files-for-nnml
431 (nnmairix-rename-files-consecutively mfolder
))
432 (nnml-generate-nov-databases-directory mfolder
))
433 (nnmairix-call-backend
434 "request-scan" folder nnmairix-backend-server
)
437 (nnmairix-request-group-with-article-number-correction
440 (save-excursion (set-buffer nnmairix-mairix-output-buffer
)
441 (goto-char (point-min))
442 (looking-at "^Matched 0 messages")))
443 ;; No messages found -> return empty group
444 (nnheader-message 5 "Mairix: No matches found.")
445 (set-buffer nntp-server-buffer
)
447 (insert (concat "211 0 1 0 " group
))
449 ;; Everything else is an error
452 'nnmairix
"Error running marix. See buffer %s for details"
453 nnmairix-mairix-output-buffer
)
457 (deffoo nnmairix-request-create-group
(group &optional server args
)
458 (let ((qualgroup (if server
(gnus-group-prefixed-name group
(list 'nnmairix server
))
463 (when server
(nnmairix-open-server server
))
464 (gnus-group-add-parameter qualgroup
'(query . nil
))
465 (gnus-group-add-parameter qualgroup
'(threads . nil
))
467 (setq count
(1+ count
))
468 (setq groupname
(format "%s-%s-%s" nnmairix-group-prefix group
469 (number-to-string count
)))
470 (setq exist
(nnmairix-call-backend
471 "request-group" groupname nnmairix-backend-server
)))
472 (nnmairix-call-backend
473 "request-create-group" groupname nnmairix-backend-server
)
474 (gnus-group-add-parameter qualgroup
'(folder . nil
))
475 (gnus-group-set-parameter qualgroup
'folder groupname
))
479 (deffoo nnmairix-retrieve-headers
(articles group
&optional server fetch-old
)
480 (when server
(nnmairix-open-server server
))
481 (let* ((folder (nnmairix-get-backend-folder group server
))
482 (corr (nnmairix-get-numcorr group server
))
486 (not (zerop (cadr corr
)))
487 (numberp (car articles
)))
488 (setq numcorr
(cadr corr
))
491 (lambda (arg) (- arg numcorr
))
493 (setq rval
(nnmairix-call-backend
494 "retrieve-headers" articles folder nnmairix-backend-server fetch-old
))
496 (nnmairix-replace-group-and-numbers articles folder group numcorr
)
499 (deffoo nnmairix-request-article
(article &optional group server to-buffer
)
500 (when server
(nnmairix-open-server server
))
501 (let ((folder (nnmairix-get-backend-folder group server
))
502 (corr (nnmairix-get-numcorr group server
)))
506 (not (zerop (cadr corr
))))
507 (setq article
(- article
(cadr corr
))))
508 (nnmairix-call-backend
509 "request-article" article folder nnmairix-backend-server to-buffer
))
512 (deffoo nnmairix-close-group
(group &optional server
)
513 ;; Should we do something here?
517 (deffoo nnmairix-request-list
(&optional server
)
518 (when server
(nnmairix-open-server server
))
519 (if (nnmairix-call-backend "request-list" nnmairix-backend-server
)
520 (let (cpoint cur qualgroup folder
)
522 (set-buffer nntp-server-buffer
)
523 (goto-char (point-min))
524 (setq cpoint
(point))
525 (while (re-search-forward nnmairix-group-regexp
(point-max) t
)
526 (setq cur
(match-string 1)
527 qualgroup
(gnus-group-prefixed-name cur
528 (list 'nnmairix server
)))
529 (if (and (gnus-group-entry qualgroup
)
530 (string= (match-string 0)
531 (gnus-group-get-parameter qualgroup
'folder
)))
534 (delete-region cpoint
(point-at-bol))
536 (setq cpoint
(point)))
538 (delete-region cpoint
(point-max)))
543 (nnoo-define-skeleton nnmairix
)
546 ;;; === Interactive functions
548 (defun nnmairix-create-search-group (server group query threads
)
549 "Create on SERVER nnmairix search group GROUP with QUERY.
550 If THREADS is t, include whole threads from found messages. If
551 called interactively, user will be asked for parameters."
554 (gnus-server-to-method (car (nnmairix-get-server)))
555 (read-string "Group name: ")
556 (read-string "Query: ")
557 (y-or-n-p "Include threads? ")))
558 (when (and (stringp query
)
559 (string-match "\\s-" query
))
560 (setq query
(split-string query
)))
561 (when (not (listp query
))
562 (setq query
(list query
)))
563 (when (and server group query
)
565 (let ((groupname (gnus-group-prefixed-name group server
))
567 (set-buffer gnus-group-buffer
)
568 (gnus-group-make-group group server
)
569 (gnus-group-set-parameter groupname
'query query
)
570 (gnus-group-set-parameter groupname
'threads threads
)
571 (nnmairix-update-and-clear-marks groupname
)))))
573 (defun nnmairix-search-interactive ()
574 "Create mairix search interactively with the minibuffer."
576 (let ((char-header nnmairix-interactive-query-parameters
)
577 header finished query achar
)
578 (while (not finished
)
580 (message "Query (%s): " (nnmairix-create-message-line-for-search))
581 (setq achar
(read-char))
582 (when (not (assoc achar char-header
))
584 (setq header
(read-string
585 (concat "Match " (nth 3 (assoc achar char-header
)) " on: ")))
586 (push (concat (nth 2 (assoc achar char-header
)) ":" header
) query
)
587 (setq finished
(not (y-or-n-p "Add another search query? "))
590 (mapconcat 'identity query
" ")
591 (car (nnmairix-get-server))
592 (y-or-n-p "Include whole threads? "))))
594 (defun nnmairix-create-search-group-from-message ()
595 "Interactively create search group with query based on current message."
597 (let ((char-header nnmairix-interactive-query-parameters
)
598 (server (nnmairix-backend-to-server gnus-current-select-method
))
599 query achar header finished group threads cq
)
600 (when (or (not (gnus-buffer-live-p gnus-article-buffer
))
601 (not (gnus-buffer-live-p gnus-summary-buffer
)))
602 (error "No article or summary buffer"))
604 (error "No nnmairix server found for back end %s:%s"
605 (symbol-name (car gnus-current-select-method
))
606 (nth 1 gnus-current-select-method
)))
607 (while (not finished
)
609 (gnus-summary-toggle-header 1)
611 (message "Query (%s): " (nnmairix-create-message-line-for-search))
612 (setq achar
(read-char))
613 (when (not (assoc achar char-header
))
615 (set-buffer gnus-article-buffer
)
617 (when (setq cq
(nth 1 (assoc achar char-header
)))
619 (nnmairix-replace-illegal-chars
620 (gnus-fetch-field (nth 1 (assoc achar char-header
))))))
621 (setq header
(read-string
622 (concat "Match " (nth 3 (assoc achar char-header
)) " on: ")
624 (push (concat (nth 2 (assoc achar char-header
)) ":" header
) query
)
625 (setq finished
(not (y-or-n-p "Add another search query? "))
627 (setq threads
(y-or-n-p "Include whole threads? "))
628 (setq group
(read-string "Group name: "))
629 (set-buffer gnus-summary-buffer
)
630 (message "Creating group %s on server %s with query %s." group
631 (gnus-method-to-server server
) (mapconcat 'identity query
" "))
632 (nnmairix-create-search-group server group query threads
)))
634 (defun nnmairix-create-server-and-default-group ()
635 "Interactively create new nnmairix server with default search group.
636 All necessary information will be queried from the user."
638 (let* ((name (read-string "Name of the mairix server: "))
639 (server (completing-read "Back end server (TAB for completion): "
640 (nnmairix-get-valid-servers)))
641 (mairix (read-string "Command to call mairix: " "mairix"))
642 (defaultgroup (read-string "Default search group: "))
643 (backend (symbol-name (car (gnus-server-to-method server
))))
644 (servername (nth 1 (gnus-server-to-method server
)))
645 (hidden (and (string-match "^nn\\(imap\\|maildir\\)$" backend
)
647 "Does the back end server work with maildir++ (i.e. hidden directories)? ")))
650 (apply (intern (format "%s-%s" backend
"open-server"))
654 (string-match "^\\." defaultgroup
))
655 (setq defaultgroup
(substring defaultgroup
1)))
656 ;; Create default search group
657 (gnus-group-make-group
658 defaultgroup
(list 'nnmairix name
(list 'nnmairix-backend
(intern backend
))
659 (list 'nnmairix-backend-server servername
)
660 (list 'nnmairix-mairix-command mairix
)
661 (list 'nnmairix-hidden-folders hidden
)
662 (list 'nnmairix-default-group defaultgroup
)))))
665 (defun nnmairix-group-change-query-this-group (&optional query
)
666 "Set QUERY for group under cursor."
668 (let* ((group (gnus-group-group-name))
669 (method (gnus-find-method-for-group group
))
670 (oldquery (gnus-group-get-parameter group
'query t
)))
671 (if (eq (car method
) 'nnmairix
)
673 (when (listp oldquery
)
674 (setq oldquery
(mapconcat 'identity oldquery
" ")))
675 (setq query
(or query
676 (read-string "New query: " oldquery
)))
677 (when (stringp query
)
678 (setq query
(split-string query
)))
680 (gnus-group-set-parameter group
'query query
)
681 (nnmairix-update-and-clear-marks group
)))
682 (error "This is no nnmairix group"))))
685 (defun nnmairix-group-toggle-threads-this-group (&optional threads
)
686 "Toggle threads parameter for this group.
687 If THREADS is a positive number, set threads parameter to t.
688 If THREADS is a negative number, set it to nil."
690 (let* ((group (gnus-group-group-name))
691 (method (gnus-find-method-for-group group
))
692 (getthreads (or threads
693 (not (gnus-group-get-parameter group
'threads
)))))
694 (if (eq (car method
) 'nnmairix
)
696 (when (numberp getthreads
)
697 (setq getthreads
(> getthreads
0)))
698 (gnus-group-set-parameter group
'threads getthreads
)
700 (message "Threads activated for group %s" group
)
701 (message "Threads deacitavted for group %s" group
))
702 (nnmairix-update-and-clear-marks group
))
703 (error "This is no nnmairix group"))))
706 (defun nnmairix-search (query &optional server threads
)
707 "Sends QUERY to nnmairix backend SERVER, using default its search group.
709 Default search group is automatically entered and results are shown.
710 If THREADS is t, enable threads.
711 If THREADS is a negative number, disable threads.
712 Otherwise, leave threads parameter as it is."
713 (interactive (list (read-string "Query: ")))
715 (setq server
(car (nnmairix-get-server))))
717 (error "No opened nnmairix server found")
718 (setq server
(gnus-server-to-method server
)))
719 (nnmairix-open-server (nth 1 server
))
720 (let* ((qualgroup (gnus-group-prefixed-name nnmairix-default-group
721 (list 'nnmairix
(nth 1 server
)))))
722 (set-buffer gnus-group-buffer
)
723 (when (stringp query
)
724 (setq query
(split-string query
)))
725 (gnus-group-set-parameter qualgroup
'query query
)
726 (if (symbolp threads
)
727 (when (eq threads
't
)
728 (gnus-group-set-parameter qualgroup
'threads t
))
730 (gnus-group-set-parameter qualgroup
'threads nil
)))
731 (nnmairix-update-and-clear-marks qualgroup
)
732 (when (not (zerop (gnus-group-unread qualgroup
)))
733 (gnus-group-read-group nil t qualgroup
))))
735 (defun nnmairix-search-thread-this-article ()
736 "Search thread for the current article.
737 This is effectively a shortcut for calling `nnmairix-search'
738 with m:msgid of the current article and enabled threads."
741 (nnmairix-backend-to-server gnus-current-select-method
))
744 (if (gnus-buffer-live-p gnus-article-buffer
)
747 (set-buffer gnus-article-buffer
)
748 (gnus-summary-toggle-header 1)
749 (setq mid
(message-fetch-field "Message-ID")))
750 (while (string-match "[<>]" mid
)
751 (setq mid
(replace-match "" t t mid
)))
752 (nnmairix-search (concat "m:" mid
) server t
))
753 (message "No article buffer."))
754 (error "No nnmairix server found for back end %s:%s"
755 (symbol-name (car gnus-current-select-method
))
756 (nth 1 gnus-current-select-method
)))))
758 (defun nnmairix-search-from-this-article ()
759 "Search messages from sender of the current article.
760 This is effectively a shortcut for calling `nnmairix-search' with
764 (nnmairix-backend-to-server gnus-current-select-method
))
767 (if (gnus-buffer-live-p gnus-article-buffer
)
770 (set-buffer gnus-article-buffer
)
771 (gnus-summary-toggle-header 1)
772 (setq from
(cadr (gnus-extract-address-components
773 (gnus-fetch-field "From"))))
774 (nnmairix-search (concat "f:" from
) server -
1)))
775 (message "No article buffer."))
776 (error "No nnmairix server found for back end %s:%s"
777 (symbol-name (car gnus-current-select-method
))
778 (nth 1 gnus-current-select-method
)))))
781 (defun nnmairix-purge-old-groups (&optional dontask server
)
782 "Delete mairix search groups which are no longer used.
784 You may want to call this from time to time if you are creating
785 and deleting lots of nnmairix groups. If DONTASK is t, do not ask
786 before deleting a group on the back end. SERVER specifies nnmairix server."
788 (let ((server (or server
789 (gnus-server-to-method (car (nnmairix-get-server))))))
790 (if (nnmairix-open-server (nth 1 server
))
791 (when (nnmairix-call-backend
792 "request-list" nnmairix-backend-server
)
793 (let (cur qualgroup folder
)
795 (set-buffer nntp-server-buffer
)
796 (goto-char (point-min))
797 (while (re-search-forward nnmairix-group-regexp
(point-max) t
)
798 (setq cur
(match-string 0)
799 qualgroup
(gnus-group-prefixed-name
800 (match-string 1) server
))
801 (when (not (and (gnus-group-entry qualgroup
)
803 (gnus-group-get-parameter
804 qualgroup
'folder
))))
807 (concat "Delete group " cur
808 " on server " nnmairix-backend-server
"? ")))
809 (nnmairix-call-backend
810 "request-delete-group" cur t nnmairix-backend-server
)))))))
811 (message "Couldn't open server %s" (nth 1 server
)))))
814 (defun nnmairix-update-database (&optional servers
)
815 "Call mairix for updating the database for SERVERS.
817 If SERVERS is nil, do update for all nnmairix servers. Mairix
818 will be called asynchronously unless
819 `nnmairix-mairix-synchronous-update' is t. Mairix will be called
820 with `nnmairix-mairix-update-options'."
822 (let ((servers (or servers
823 (nnmairix-get-nnmairix-servers)))
824 args cur commandsplit
)
826 (setq cur
(car (pop servers
)))
827 (nnmairix-open-server
828 (nth 1 (gnus-server-to-method cur
)))
829 (setq commandsplit
(split-string nnmairix-mairix-command
))
830 (nnheader-message 7 "Updating mairix database for %s..." cur
)
831 (if nnmairix-mairix-synchronous-update
833 (setq args
(append (list (car commandsplit
) nil
834 (get-buffer nnmairix-mairix-output-buffer
)
836 (if (> (length commandsplit
) 1)
837 (setq args
(append args
(cdr commandsplit
) nnmairix-mairix-update-options
))
838 (setq args
(append args nnmairix-mairix-update-options
)))
839 (apply 'call-process args
)
840 (nnheader-message 7 "Updating mairix database for %s... done" cur
))
842 (setq args
(append (list cur
(get-buffer nnmairix-mairix-output-buffer
)
843 (car commandsplit
))))
844 (if (> (length commandsplit
) 1)
845 (setq args
(append args
(cdr commandsplit
) nnmairix-mairix-update-options
))
846 (setq args
(append args nnmairix-mairix-update-options
)))
847 (set-process-sentinel (apply 'start-process args
)
848 'nnmairix-sentinel-mairix-update-finished
))))))
850 ;; Silence byte-compiler.
852 (defvar gnus-registry-install
)
853 (autoload 'gnus-registry-fetch-group
"gnus-registry"))
855 (defun nnmairix-goto-original-article (&optional no-registry
)
856 "Jump to the original group and display article.
857 The original group of the article is first determined with the
858 registry (if enabled). If the registry is not enabled or did not
859 find the article or the prefix NO-REGISTRY is non-nil, this
860 function will try to determine the original group form the path
861 of the mail file. The path is obtained through another mairix
864 (when (not (eq (car gnus-current-select-method
) 'nnmairix
))
865 (let ((method (gnus-find-method-for-group gnus-newsgroup-name
)))
866 (if (eq (car method
) 'nnmairix
)
867 (nnmairix-open-server (nth 1 method
))
868 (error "Not in a nnmairix group"))))
869 (when (not (gnus-buffer-live-p gnus-article-buffer
))
870 (error "No article buffer available"))
871 (let ((server (nth 1 gnus-current-select-method
))
872 mid rval group allgroups
)
875 (set-buffer gnus-article-buffer
)
876 (gnus-summary-toggle-header 1)
877 (setq mid
(message-fetch-field "Message-ID"))
878 ;; first check the registry (if available)
879 (when (and (boundp 'gnus-registry-install
)
880 gnus-registry-install
882 (setq group
(gnus-registry-fetch-group mid
)))
883 (while (string-match "[<>]" mid
)
884 (setq mid
(replace-match "" t t mid
)))
886 ;; registry was not available or did not find article
887 ;; so we search again with mairix in raw mode to get filename
888 (nnmairix-open-server server
)
890 (nnmairix-call-mairix-binary-raw
891 (split-string nnmairix-mairix-command
)
892 (list (concat "m:" mid
))))
894 ;; determine original group(s) from filename
896 (set-buffer nnmairix-mairix-output-buffer
)
897 (goto-char (point-min))
898 (while (looking-at "/")
899 (push (nnmairix-determine-original-group)
902 (if (> (length allgroups
) 1)
905 "Message exists in more than one group. Choose: "
907 (setq group
(car allgroups
))))
908 (error "Mairix could not find original article. See buffer %s for details"
909 nnmairix-mairix-output-buffer
))))
911 ;; show article in summary buffer
912 (nnmairix-show-original-article group mid
)
913 (message "Couldn't find original article"))))
915 (defun nnmairix-determine-original-group ()
916 "Try to determine to original group from the file path."
917 (let (path filename serverbase group maildirflag allgroups
)
918 (re-search-forward "^\\(.*\\)/\\(.*?\\)$")
919 (setq path
(expand-file-name (match-string 1)))
920 (setq filename
(match-string 2))
921 ;; when we deal with maildir, remove cur/new/tmp from path
922 (setq maildirflag
(string-match ".+\\..+\\..+" filename
))
925 (replace-regexp-in-string
926 ".*\\(/cur\\|/new\\|/tmp\\)$" "" path t t
1)))
927 ;; we first check nnml and nnmaildir servers
931 (dolist (cur gnus-opened-servers
)
932 (when (or (and (not maildirflag
)
933 (eq (caar cur
) 'nnml
))
935 (eq (caar cur
) 'nnmaildir
)))
936 ;; get base path from server
938 (setq serverbase
(cadr (assoc 'directory
(car cur
))))
939 (setq serverbase
(cadr (assoc 'nnml-directory
(car cur
))))
940 (when (not serverbase
)
941 (setq serverbase nnml-directory
)))
942 (setq serverbase
(file-name-as-directory
943 (expand-file-name serverbase
)))
944 (when (string-match (concat serverbase
"\\(.*\\)") path
)
945 ;; looks good - rest of the path should be the group
946 (setq group
(match-string 1 path
))
947 (when (string-match "/$" group
)
948 (setq group
(replace-match "" t t group
)))
949 (when (not maildirflag
)
950 ;; for nnml: convert slashes to dots
951 (while (string-match "/" group
)
952 (setq group
(replace-match "." t t group
))))
953 (setq group
(gnus-group-prefixed-name group
(car cur
)))
954 ;; check whether this group actually exists
955 (when (gnus-group-entry group
)
956 (throw 'found group
)))))))
958 ;; we haven't found it yet --> look for nnimap groups
959 ;; assume last element of the path is the group
960 (string-match "^.*/\\.?\\(.*\\)$" path
)
961 (setq group
(match-string 1 path
))
962 ;; convert dots to slashes (nested group)
963 (while (string-match "\\." group
)
964 (setq group
(replace-match "/" t t group
)))
965 (dolist (cur gnus-opened-servers
)
966 (when (eq (caar cur
) 'nnimap
)
967 (when (gnus-group-entry
968 (gnus-group-prefixed-name group
(car cur
)))
970 (gnus-group-prefixed-name group
(car cur
))
972 (if (> (length allgroups
) 1)
973 (setq group
(completing-read
974 "Group %s exists on more than one IMAP server. Choose: "
976 (setq group
(car allgroups
))))
980 ;;; ==== Helper functions
982 (defun nnmairix-request-group-with-article-number-correction (folder qualgroup
)
983 "Request FOLDER on backend for nnmairix QUALGROUP and article number correction."
985 (nnmairix-call-backend "request-group" folder nnmairix-backend-server
)
986 (set-buffer nnmairix-mairix-output-buffer
)
987 (goto-char (point-min))
988 (re-search-forward "^Matched.*messages")
989 (nnheader-message 7 (match-string 0))
990 (set-buffer nntp-server-buffer
)
991 (goto-char (point-min))
992 (let ((status (read (current-buffer)))
993 (total (read (current-buffer)))
994 (low (read (current-buffer)))
995 (high (read (current-buffer)))
996 (corr (gnus-group-get-parameter qualgroup
'numcorr t
)))
999 ;; Article number correction
1001 (> (+ (car (cddr corr
)) high
) 0))
1003 (when (car corr
) ;Group has changed
1007 (+ (car (cddr corr
)) high
)))
1008 (gnus-group-set-parameter
1009 qualgroup
'numcorr corr
))
1010 (setq low
(+ low
(cadr corr
))
1011 high
(+ high
(cadr corr
))))
1012 (when (member nnmairix-backend
1013 nnmairix-delete-and-create-on-change
)
1014 (gnus-group-set-parameter
1015 qualgroup
'numcorr
(list nil
0 high
))))
1017 (insert (format "%d %d %d %d %s" status total low high
1018 (gnus-group-real-name qualgroup
)))
1022 'nnmairix
"Error calling back end on group %s" folder
)
1025 (defun nnmairix-call-mairix-binary (command folder query threads
)
1026 "Call mairix binary with COMMAND, using FOLDER and QUERY.
1027 If THREADS is non-nil, enable full threads."
1028 (let ((args (cons (car command
) '(nil t nil
))))
1031 (get-buffer-create nnmairix-mairix-output-buffer
))
1033 (when (> (length command
) 1)
1034 (setq args
(append args
(cdr command
))))
1036 (setq args
(append args
'("-t"))))
1037 (apply 'call-process
1038 (append args
(list "-o" folder
) query
)))))
1040 (defun nnmairix-call-mairix-binary-raw (command query
)
1041 "Call mairix binary with COMMAND and QUERY in raw mode."
1042 (let ((args (cons (car command
) '(nil t nil
))))
1045 (get-buffer-create nnmairix-mairix-output-buffer
))
1047 (when (> (length command
) 1)
1048 (setq args
(append args
(cdr command
))))
1049 (setq args
(append args
'("-r")))
1050 (apply 'call-process
1051 (append args query
)))))
1053 (defun nnmairix-get-server ()
1054 "If there exists just one nnmairix server, return its value.
1055 Otherwise, ask user for server."
1056 (let ((openedserver (nnmairix-get-nnmairix-servers)))
1057 (when (not openedserver
)
1058 (error "No opened nnmairix server found"))
1059 (if (> (length openedserver
) 1)
1063 (setq nnmairix-last-server
1064 (list (completing-read "Server: " openedserver nil
1
1065 (or nnmairix-last-server
1067 nnmairix-last-server
)
1068 (car openedserver
))))
1070 (defun nnmairix-get-nnmairix-servers (&optional all
)
1071 "Return available nnmairix servers.
1072 If ALL is t, return also the unopened/failed ones."
1073 (let ((alist gnus-opened-servers
)
1074 server openedserver
)
1076 (setq server
(pop alist
))
1079 (eq (cadr server
) 'ok
))
1080 (eq (caar server
) 'nnmairix
)
1081 (not (member (car server
) gnus-ephemeral-servers
)))
1083 (concat (symbol-name (caar server
)) ":" (nth 1 (car server
))))
1084 (push (list server
) openedserver
)))
1088 (defun nnmairix-get-valid-servers ()
1089 "Return list of valid backend servers for nnmairix groups."
1090 (let ((alist gnus-opened-servers
)
1091 (mairixservers (nnmairix-get-nnmairix-servers t
))
1092 server mserver openedserver occ cur
)
1093 ;; Get list of all nnmairix backends (i.e. backends which are
1094 ;; already occupied)
1095 (dolist (cur mairixservers
)
1099 (cadr (assoc 'nnmairix-backend
1100 (gnus-server-to-method (car cur
)))))
1102 (cadr (assoc 'nnmairix-backend-server
1103 (gnus-server-to-method (car cur
)))))
1106 (setq server
(pop alist
))
1107 (setq mserver
(gnus-method-to-server (car server
)))
1108 ;; If this is the native server, convert it to the real server
1109 ;; name to avoid confusion
1110 (when (string= mserver
"native")
1111 (setq mserver
(format "%s:%s"
1113 (nth 1 (car server
)))))
1115 (eq (cadr server
) 'ok
)
1116 (member (caar server
) nnmairix-valid-backends
)
1117 (not (member (car server
) gnus-ephemeral-servers
))
1118 (not (member (gnus-method-to-server (car server
)) occ
)))
1124 (defun nnmairix-call-backend (func &rest args
)
1125 "Call a function FUNC on backend with ARGS."
1126 (apply (intern (format "%s-%s" (symbol-name nnmairix-backend
) func
)) args
))
1128 (defun nnmairix-get-backend-folder (group &optional server
)
1129 "Return back end GROUP from nnmairix group on SERVER."
1130 (let* ((qualgroup (if server
1131 (gnus-group-prefixed-name group
(list 'nnmairix server
))
1133 (folder (gnus-group-get-parameter qualgroup
'folder
)))
1136 (defun nnmairix-get-numcorr (group &optional server
)
1137 "Return values for article number correction nnmairix GROUP on SERVER."
1138 (let* ((qualgroup (if server
1139 (gnus-group-prefixed-name group
(list 'nnmairix server
))
1141 (corr (gnus-group-get-parameter qualgroup
'numcorr t
)))
1145 (defun nnmairix-rename-files-consecutively (path)
1146 "Rename all nnml mail files in PATH so that they have consecutive numbers.
1147 This should correct problems of wrong article counts when using
1148 nnmairix with nnml backends."
1151 (mapcar 'string-to-number
1152 (directory-files path nil
"[0-9]+" t
))
1154 (lastplusone (car files
))
1155 (path (file-name-as-directory path
)))
1157 (when (not (= cur lastplusone
))
1158 (rename-file (concat path
1159 (number-to-string cur
))
1161 (number-to-string lastplusone
)))
1162 (setq cur lastplusone
))
1163 (setq lastplusone
(1+ cur
)))))
1165 (defun nnmairix-replace-group-and-numbers (articles backendgroup mairixgroup numc
)
1166 "Replace folder names in Xref header and correct article numbers.
1167 Do this for all ARTICLES on BACKENDGROUP. Replace using
1168 MAIRIXGROUP. NUMC contains values for article number correction."
1169 (let ((buf (get-buffer-create " *nnmairix buffer*"))
1170 (corr (not (zerop numc
)))
1171 (name (buffer-name nntp-server-buffer
))
1176 (set-buffer nntp-server-buffer
)
1177 (goto-char (point-min))
1178 (nnheader-message 7 "nnmairix: Rewriting headers...")
1181 (when (or (looking-at (number-to-string article
))
1182 (nnheader-find-nov-line article
))
1183 (setq cur
(nnheader-parse-nov))
1185 (setq article
(+ (mail-header-number cur
) numc
))
1186 (mail-header-set-number cur article
))
1187 (setq xref
(mail-header-xref cur
))
1188 (when (and (stringp xref
)
1189 (string-match (format "[ \t]%s:[0-9]+" backendgroup
) xref
))
1190 (setq xref
(replace-match (format " %s:%d" mairixgroup article
) t nil xref
))
1191 (mail-header-set-xref cur xref
))
1193 (nnheader-insert-nov cur
)
1194 (set-buffer nntp-server-buffer
)
1198 (nnheader-message 7 "nnmairix: Rewriting headers... done")
1199 (kill-buffer nntp-server-buffer
)
1201 (rename-buffer name
)
1202 (setq nntp-server-buffer buf
))))
1204 (defun nnmairix-backend-to-server (server)
1205 "Return nnmairix server most probably responsible for back end SERVER.
1206 User will be asked if this cannot be determined. Result is saved in
1207 parameter 'indexed-servers of corresponding default search
1209 (let ((allservers (nnmairix-get-nnmairix-servers))
1210 mairixserver found defaultgroup
)
1211 (if (> (length allservers
) 1)
1213 ;; If there is more than one nnmairix server, we go through them
1214 (while (and allservers
(not found
))
1215 (setq mairixserver
(gnus-server-to-method (car (pop allservers
))))
1216 ;; First we look if SERVER is the backend of current nnmairix server
1217 (setq found
(and (eq (cadr (assoc 'nnmairix-backend mairixserver
))
1219 (string= (cadr (assoc 'nnmairix-backend-server mairixserver
))
1221 ;; If that's not the case, we look at 'indexed-servers
1222 ;; variable in default search group
1224 (setq defaultgroup
(cadr (assoc 'nnmairix-default-group mairixserver
)))
1225 (setq found
(member (gnus-method-to-server server
)
1226 (gnus-group-get-parameter
1227 (gnus-group-prefixed-name defaultgroup
1229 'indexed-servers t
)))))
1230 ;; If still not found, we ask user
1233 (gnus-server-to-method
1235 (format "Cannot determine which nnmairix server indexes %s. Please specify: "
1236 (gnus-method-to-server server
))
1237 (nnmairix-get-nnmairix-servers) nil nil
"nnmairix:")))
1238 ;; Save result in parameter of default search group so that
1239 ;; we don't have to ask again
1240 (setq defaultgroup
(gnus-group-prefixed-name
1241 (cadr (assoc 'nnmairix-default-group mairixserver
)) mairixserver
))
1242 (gnus-group-set-parameter
1245 (append (gnus-group-get-parameter defaultgroup
'indexed-servers t
)
1246 (list (gnus-method-to-server server
)))))
1248 ;; If there is just one (or none) nnmairix server:
1249 (gnus-server-to-method (caar allservers
)))))
1251 (defun nnmairix-update-and-clear-marks (group &optional method
)
1252 "Update group and clear all marks from GROUP using METHOD."
1254 (setq group
(gnus-group-prefixed-name group method
)))
1255 (let ((method (or method
1256 (gnus-find-method-for-group group
)))
1257 (folder (gnus-group-get-parameter group
'folder
))
1258 (corr (gnus-group-get-parameter group
'numcorr t
))
1260 (if (eq (nth 0 method
) 'nnmairix
)
1262 (nnmairix-open-server (nth 1 method
))
1263 (set-buffer gnus-group-buffer
)
1264 (setq info
(gnus-get-info group
))
1265 ;; Clear active and info
1266 (gnus-set-active group nil
)
1267 (gnus-info-clear-data info
)
1268 ;; Delete and re-create group if needed
1269 (when (member nnmairix-backend nnmairix-delete-and-create-on-change
)
1270 (if (string-match nnmairix-group-regexp folder
)
1272 (nnmairix-call-backend "open-server"
1273 nnmairix-backend-server
)
1274 (nnmairix-call-backend "request-delete-group"
1275 folder t nnmairix-backend-server
)
1276 (nnmairix-call-backend "request-create-group"
1277 folder nnmairix-backend-server
)
1278 ;; set flag that group has changed for article number correction
1281 (gnus-group-set-parameter group
'numcorr corr
)))
1282 (error "Nnmairix-update-and-clear-marks - delete/create with\
1283 non-mairix group!! - check folder parameter")))
1284 (when (gnus-group-jump-to-group group
)
1285 (gnus-group-get-new-news-this-group)))
1286 (error "Nnmairix-update-and-clear-marks - Called with non-nnmairix group"))))
1289 (defun nnmairix-sentinel-mairix-update-finished (proc status
)
1290 "Sentinel for mairix update process PROC with STATUS."
1291 (if (equal status
"finished\n")
1292 (nnheader-message 7 "Updating mairix database for %s... done" proc
)
1293 (error "There was an error updating the mairix database for server %s. \
1294 See %s for details" proc nnmairix-mairix-output-buffer
)))
1296 (defun nnmairix-create-message-line-for-search ()
1297 "Create message line for interactive query in minibuffer."
1301 (format "%c=%s" (car cur
) (nth 3 cur
))))
1302 nnmairix-interactive-query-parameters
","))
1304 (defun nnmairix-replace-illegal-chars (header)
1305 "Replace illegal characters in HEADER for mairix query."
1307 (if (> emacs-major-version
20)
1308 (while (string-match "[^-.@/,& [:alnum:]]" header
)
1309 (setq header
(replace-match "" t t header
)))
1310 (while (string-match "[[]{}:<>]" header
)
1311 (setq header
(replace-match "" t t header
))))
1312 (while (string-match "[-& ]" header
)
1313 (setq header
(replace-match "," t t header
)))
1316 (defun nnmairix-show-original-article (group mid
)
1317 "Switch to GROUP and display Article with message-id MID."
1318 (when (string-match "Summary" (buffer-name (current-buffer)))
1319 (gnus-summary-exit))
1320 (pop-to-buffer gnus-group-buffer
)
1321 (gnus-group-jump-to-group group
)
1322 (gnus-summary-read-group group
1 t
)
1323 (gnus-summary-refer-article mid
)
1324 (gnus-summary-limit-to-headers (format "message-id: <%s>" mid
))
1325 (gnus-summary-select-article)
1327 (gnus-summary-show-article)
1328 (nnheader-message 5 "Switched to group %s." group
))
1331 ;; ==== Widget stuff
1333 (defvar nnmairix-widgets
)
1334 (defvar nnmairix-widgets-values nil
)
1336 (defun nnmairix-widget-search-from-this-article ()
1337 "Create mairix query based on current article using graphical widgets."
1339 (nnmairix-widget-search
1340 (nnmairix-widget-get-values)))
1343 (defun nnmairix-widget-get-values ()
1344 "Create values for editable fields from current article."
1345 (if (not (gnus-buffer-live-p gnus-article-buffer
))
1346 (error "No article buffer available")
1348 (gnus-summary-toggle-header 1)
1349 (set-buffer gnus-article-buffer
)
1353 (list (car (cddr field
))
1355 (nnmairix-replace-illegal-chars
1356 (gnus-fetch-field (car field
)))
1358 nnmairix-widget-fields-list
))))
1361 (defun nnmairix-widget-search (&optional mvalues
)
1362 "Create mairix query interactively using graphical widgets.
1363 MVALUES may contain values from current article."
1365 ;; Select window for mairix customization
1366 (funcall nnmairix-widget-select-window-function
)
1368 (nnmairix-widget-create-query mvalues
)
1370 (widget-create 'push-button
1373 (lambda (&rest ignore
)
1374 (nnmairix-widget-send-query nnmairix-widgets
1376 (lambda (&rest ignore
)
1377 (nnmairix-widget-send-query nnmairix-widgets
1381 (widget-create 'push-button
1384 (lambda (&rest ignore
)
1385 (nnmairix-widget-create-group nnmairix-widgets
1387 (lambda (&rest ignore
)
1388 (nnmairix-widget-create-group nnmairix-widgets
1390 "Create permanent group")
1392 (widget-create 'push-button
1393 :notify
(lambda (&rest ignore
)
1394 (kill-buffer nnmairix-customize-query-buffer
))
1396 (use-local-map widget-keymap
)
1398 (goto-char (point-min)))
1400 (defun nnmairix-widget-send-query (widgets &optional withvalues
)
1401 "Send query from WIDGETS to mairix binary.
1402 If WITHVALUES is t, query is based on current article."
1404 (nnmairix-widget-make-query-from-widgets widgets
)
1406 (gnus-method-to-server
1407 (nnmairix-backend-to-server gnus-current-select-method
))
1408 (car (nnmairix-get-server)))
1409 (if (widget-value (cadr (assoc "Threads" widgets
)))
1412 (kill-buffer nnmairix-customize-query-buffer
))
1414 (defun nnmairix-widget-create-group (widgets &optional withvalues
)
1415 "Create nnmairix group based on current widget values WIDGETS.
1416 If WITHVALUES is t, query is based on current article."
1417 (let ((group (read-string "Name of the group: ")))
1418 (when (not (zerop (length group
)))
1419 (nnmairix-create-search-group
1421 (gnus-method-to-server
1422 (nnmairix-backend-to-server gnus-current-select-method
))
1423 (car (nnmairix-get-server)))
1425 (nnmairix-widget-make-query-from-widgets widgets
)
1426 (widget-value (cadr (assoc "Threads" widgets
))))))
1427 (kill-buffer nnmairix-customize-query-buffer
))
1430 (defun nnmairix-widget-make-query-from-widgets (widgets)
1431 "Create mairix query from widget values WIDGETS."
1432 (let (query temp flag
)
1433 ;; first we do the editable fields
1434 (dolist (cur nnmairix-widget-fields-list
)
1435 ;; See if checkbox is checked
1437 (cadr (assoc (concat "c" (car (cddr cur
))) widgets
)))
1438 ;; create query for the field
1443 (nnmairix-replace-illegal-chars
1445 (cadr (assoc (concat "e" (car (cddr cur
))) widgets
)))))
1448 (when (member 'flags nnmairix-widget-other
)
1454 (widget-value (cadr (assoc (car flag
) nnmairix-widgets
))))
1455 (if (string= "yes" temp
)
1457 (if (string= "no" temp
)
1458 (concat "-" (cadr flag
))))))
1459 '(("seen" "s") ("replied" "r") ("flagged" "f")) ""))
1460 (when (not (zerop (length flag
)))
1461 (push (concat "F:" flag
) query
)))
1462 ;; return query string
1463 (mapconcat 'identity query
" ")))
1466 (defun nnmairix-widget-create-query (&optional values
)
1467 "Create widgets for creating mairix queries.
1468 Fill in VALUES if based on an article."
1470 (when (get-buffer nnmairix-customize-query-buffer
)
1471 (kill-buffer nnmairix-customize-query-buffer
))
1472 (switch-to-buffer nnmairix-customize-query-buffer
)
1473 (kill-all-local-variables)
1475 (widget-insert "Specify your query for Mairix (check boxes for activating fields):\n\n")
1476 (widget-insert "(Whitespaces will be converted to ',' (i.e. AND). Use '/' for OR.)\n\n")
1477 ; (make-local-variable 'nnmairix-widgets)
1478 (setq nnmairix-widgets
(nnmairix-widget-build-editable-fields values
))
1479 (when (member 'flags nnmairix-widget-other
)
1480 (widget-insert "\nFlags:\n Seen: ")
1481 (nnmairix-widget-add "seen"
1484 '(item "yes") '(item "no") '(item "ignore"))
1485 (widget-insert " Replied: ")
1486 (nnmairix-widget-add "replied"
1489 '(item "yes") '(item "no") '(item "ignore"))
1490 (widget-insert " Ticked: ")
1491 (nnmairix-widget-add "flagged"
1494 '(item "yes") '(item "no") '(item "ignore")))
1495 (when (member 'threads nnmairix-widget-other
)
1496 (widget-insert "\n")
1497 (nnmairix-widget-add "Threads" 'checkbox nil
))
1498 (widget-insert " Show full threads\n\n")))
1501 (defun nnmairix-widget-build-editable-fields (values)
1502 "Build editable field widgets in `nnmairix-widget-fields-list'.
1503 VALUES may contain values for editable fields from current article."
1504 ;; how can this be done less ugly?
1509 (setq field
(car (cddr field
)))
1515 (widget-create 'checkbox
1517 :notify
(lambda (widget &rest ignore
)
1518 (nnmairix-widget-toggle-activate widget
))
1523 (widget-create 'editable-field
1525 :format
(concat " " field
":"
1526 (make-string (- 11 (length field
)) ?\
)
1528 :value
(or (cadr (assoc field values
)) ""))))
1530 (widget-insert "\n")
1531 ;; Deactivate editable field
1532 (widget-apply (cadr (nth 1 ret
)) :deactivate
)))
1533 nnmairix-widget-fields-list
)
1536 (defun nnmairix-widget-add (name &rest args
)
1537 "Add a widget NAME with optional ARGS."
1540 (apply 'widget-create args
))
1543 (defun nnmairix-widget-toggle-activate (widget)
1544 "Toggle activation status of WIDGET dependent on corresponding checkbox value."
1545 (let ((field (widget-get widget
:tag
)))
1546 (if (widget-value widget
)
1548 (cadr (assoc (concat "e" field
) nnmairix-widgets
))
1551 (cadr (assoc (concat "e" field
) nnmairix-widgets
))
1557 ;; arch-tag: bb187498-b229-4a55-8c07-6d3f80713e94
1558 ;;; nnmairix.el ends here