Mercurial > emacs
annotate lisp/gnus/nnmairix.el @ 92351:1faa9403a7f5
Rename describe-project to describe-gnu-project.
author | Kim F. Storm <storm@cua.dk> |
---|---|
date | Fri, 29 Feb 2008 23:38:43 +0000 |
parents | 261f98568bde |
children | 1af6d6eab2e9 |
rev | line source |
---|---|
92255 | 1 ;;; nnmairix.el --- Mairix back end for Gnus, the Emacs newsreader |
2 | |
3 ;; Copyright (C) 2007, 2008 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: David Engster <dengste@eml.cc> | |
6 ;; Keywords: mail searching | |
7 ;; Version: 0.5 | |
8 | |
92257 | 9 ;; This file is part of GNU Emacs. |
10 | |
11 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
92255 | 12 ;; it under the terms of the GNU General Public License as published by |
92257 | 13 ;; the Free Software Foundation; either version 3, or (at your option) |
92255 | 14 ;; any later version. |
15 | |
92257 | 16 ;; GNU Emacs is distributed in the hope that it will be useful, |
92255 | 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. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
92257 | 22 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
92255 | 24 ;; Boston, MA 02110-1301, USA. |
25 | |
26 ;;; Commentary: | |
27 | |
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. | |
30 | |
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. | |
37 ;; | |
38 ;; Mairix is written by Richard Curnow. More information can be found at | |
39 ;; http://www.rpcurnow.force9.co.uk/mairix/ | |
40 ;; | |
41 ;; For details about setting up mairix&Gnus&nnmairix.el, look at the | |
42 ;; emacswiki: | |
43 ;; | |
44 ;; http://www.emacswiki.org/cgi-bin/wiki/GnusMairix | |
45 ;; | |
46 ;; The newest version of nnmairix.el can be found at | |
92257 | 47 ;; |
92255 | 48 ;; http://www.emacswiki.org/cgi-bin/emacs/nnmairix.el |
49 | |
50 ;; For impatient people, here's the setup in a nutshell: | |
51 ;; | |
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. | |
56 ;; | |
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 | |
65 ;; information. | |
66 | |
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. | |
73 | |
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 | |
81 | |
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"... | |
86 | |
87 ;; KNOWN BUGS: | |
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. | |
94 | |
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 | |
92257 | 101 |
92255 | 102 ;;; Changelog: |
103 ;; | |
104 ;; 02/06/2008 - version 0.5 | |
92257 | 105 ;; |
92255 | 106 ;; * New function: nnmairix-goto-original-article. Uses the |
107 ;; registry or the mail file path for determining original group. | |
92257 | 108 ;; |
92255 | 109 ;; * Deal with empty Xref header |
110 ;; | |
111 ;; * Changed summary mode keybindings since the old ones were | |
112 ;; already taken | |
113 ;; | |
114 ;; (Thanks to Tassilo Horn and Ted Zlatanov for their help) | |
115 ;; | |
116 ;; 01/07/2008 - version 0.4 | |
117 ;; | |
118 ;; * New/fixed doc strings and code cleanup. | |
119 ;; | |
120 ;; 18/11/2007 - version 0.3 | |
121 ;; | |
122 ;; * Fixed bugs when dealing with nnml and native servers | |
92257 | 123 ;; |
92255 | 124 ;; * Make variables customizable |
125 ;; | |
126 ;; 10/10/2007 - version 0.2 | |
127 ;; | |
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 | |
131 ;; nnimap). | |
132 ;; | |
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. | |
136 ;; | |
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. | |
140 ;; | |
141 ;; * More commands for creating queries (using widgets or the | |
142 ;; minibuffer). | |
143 ;; | |
144 ;; * Fixed bug in nnmairix-create-search-group-from-message | |
145 ;; | |
146 ;; * Changed copyright to FSF | |
147 ;; | |
148 ;; (Thanks to Georg C. F. Greve and Bastien for suggestions and | |
149 ;; ideas!) | |
150 ;; | |
151 ;; 10/03/2007 - version 0.1 - first release | |
152 | |
153 | |
154 ;;; Code: | |
155 | |
156 (require 'nnoo) | |
157 (require 'gnus-group) | |
158 (require 'gnus-sum) | |
159 (require 'message) | |
160 (require 'nnml) | |
161 (require 'widget) | |
162 | |
163 (nnoo-declare nnmairix) | |
164 | |
165 ;;; === Keymaps | |
166 | |
167 ;; Group mode | |
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)) | |
188 | |
189 ;; Summary mode | |
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)) | |
202 | |
203 (add-hook 'gnus-group-mode-hook 'nnmairix-group-mode-hook) | |
204 (add-hook 'gnus-summary-mode-hook 'nnmairix-summary-mode-hook) | |
205 | |
206 | |
207 ;; Customizable stuff | |
208 | |
209 (defgroup nnmairix nil | |
210 "Backend for the Mairix mail search engine." | |
211 :group 'gnus) | |
212 | |
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'." | |
92257 | 220 :version "23.1" |
92255 | 221 :type 'string |
222 :group 'nnmairix) | |
223 | |
224 (defcustom nnmairix-mairix-output-buffer "*mairix output*" | |
225 "Buffer used for mairix output." | |
92257 | 226 :version "23.1" |
92255 | 227 :type 'string |
228 :group 'nnmairix) | |
229 | |
230 (defcustom nnmairix-customize-query-buffer "*mairix query*" | |
231 "Name of the buffer for customizing Mairix queries." | |
92257 | 232 :version "23.1" |
92255 | 233 :type 'string |
234 :group 'nnmairix) | |
235 | |
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)." | |
92257 | 241 :version "23.1" |
92255 | 242 :type '(repeat string) |
243 :group 'nnmairix) | |
244 | |
245 (defcustom nnmairix-mairix-synchronous-update nil | |
246 "Set this to t if you want Emacs to wait for mairix updating the database." | |
92257 | 247 :version "23.1" |
92255 | 248 :type 'boolean |
249 :group 'nnmairix) | |
250 | |
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." | |
92257 | 257 :version "23.1" |
92255 | 258 :type 'boolean |
259 :group 'nnmairix) | |
260 | |
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. | |
267 | |
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)." | |
92257 | 273 :version "23.1" |
92255 | 274 :type '(repeat (list |
275 (choice :tag "Field" | |
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"))) | |
284 :group 'nnmairix) | |
285 | |
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." | |
92257 | 290 :version "23.1" |
92255 | 291 :type 'function |
292 :group 'nnmairix) | |
293 | |
294 ;; ==== Other variables | |
295 | |
296 (defvar nnmairix-widget-other | |
297 '(threads flags) | |
298 "Other editable mairix commands when using customization widgets. | |
299 Currently there are 'threads and 'flags.") | |
300 | |
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.") | |
308 | |
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.") | |
319 | |
320 ;;; === Server variables | |
321 | |
322 (defvoo nnmairix-backend nil | |
323 "Backend where mairix stores its searches.") | |
324 | |
325 (defvoo nnmairix-backend-server nil | |
326 "Name of the server where mairix stores its searches.") | |
327 | |
328 (defvoo nnmairix-mairix-command "mairix" | |
329 "Command to call mairix for this nnmairix server.") | |
330 | |
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).") | |
334 | |
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.") | |
338 | |
339 ;;; === Internal variables | |
340 | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
341 (defconst nnmairix-group-regexp |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
342 (format "%s-\\(.*\\)-[0-9]+" nnmairix-group-prefix) |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
343 "Regexp for mairix groups on back end.") |
92255 | 344 |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
345 (defconst nnmairix-valid-backends '(nnimap nnml nnmaildir) |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
346 "Backends supported by nnmairix. |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
347 Other backends might or might not work.") |
92255 | 348 |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
349 (defvar nnmairix-last-server nil |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
350 "Last chosen server.") |
92255 | 351 |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
352 (defvar nnmairix-current-server nil |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
353 "Current server.") |
92255 | 354 |
355 ;;; === Gnus backend functions | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
356 |
92255 | 357 (nnoo-define-basics nnmairix) |
358 | |
359 (gnus-declare-backend "nnmairix" 'mail 'address) | |
360 | |
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)) | |
365 | |
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)) | |
371 group)) | |
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) | |
379 (cond | |
380 ((not folder) | |
381 ;; No folder parameter -> error | |
382 (nnheader-report 'nnmairix "Check folder parameter for group %s" group) | |
383 nil) | |
384 ((not query) | |
385 ;; No query -> return empty group | |
386 (save-excursion | |
387 (set-buffer nntp-server-buffer) | |
388 (erase-buffer) | |
389 (insert (concat "211 0 1 0 " group)) | |
390 t)) | |
391 (t | |
392 ;; For maildir++ folders: create a hidden directory (prepend dot) | |
393 (setq mfolder (if (and nnmairix-hidden-folders | |
394 (not (string-match "^\\." folder))) | |
395 (concat "." folder) | |
396 folder)) | |
397 ;; For nnml and nnmaildir, precede mfolder with directory where mail | |
398 ;; is actually stored so that it's independent of 'base' setting | |
399 ;; in .mairixrc. | |
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) | |
406 (setq folderpath | |
407 (cadr (assoc 'directory backendmethod)))) | |
408 (when folderpath | |
409 (setq mfolder | |
410 (concat | |
411 (file-name-as-directory | |
412 (expand-file-name | |
413 folderpath)) | |
414 mfolder))) | |
415 ;; If (not fast), call Mairix binary | |
416 (setq rval | |
417 (if fast 0 | |
418 (nnmairix-call-mairix-binary | |
419 (split-string nnmairix-mairix-command) | |
420 mfolder query threads))) | |
421 ;; Check return value | |
422 (cond | |
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 | |
428 ;; group | |
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) | |
435 (if fast | |
436 t | |
92260
261f98568bde
(nnmairix-request-group): Bind nnmairix-fast and nnmairix-group around
Glenn Morris <rgm@gnu.org>
parents:
92259
diff
changeset
|
437 (let ((nnmairix-fast fast) |
261f98568bde
(nnmairix-request-group): Bind nnmairix-fast and nnmairix-group around
Glenn Morris <rgm@gnu.org>
parents:
92259
diff
changeset
|
438 (nnmairix-group group)) |
261f98568bde
(nnmairix-request-group): Bind nnmairix-fast and nnmairix-group around
Glenn Morris <rgm@gnu.org>
parents:
92259
diff
changeset
|
439 (nnmairix-request-group-with-article-number-correction |
261f98568bde
(nnmairix-request-group): Bind nnmairix-fast and nnmairix-group around
Glenn Morris <rgm@gnu.org>
parents:
92259
diff
changeset
|
440 folder qualgroup)))) |
92255 | 441 ((and (= rval 1) |
442 (save-excursion (set-buffer nnmairix-mairix-output-buffer) | |
443 (goto-char (point-min)) | |
444 (looking-at "^Matched 0 messages"))) | |
445 ;; No messages found -> return empty group | |
446 (nnheader-message 5 "Mairix: No matches found.") | |
447 (set-buffer nntp-server-buffer) | |
448 (erase-buffer) | |
449 (insert (concat "211 0 1 0 " group)) | |
450 t) | |
451 ;; Everything else is an error | |
452 (t | |
453 (nnheader-report | |
454 'nnmairix "Error running marix. See buffer %s for details" | |
455 nnmairix-mairix-output-buffer) | |
456 nil)))))) | |
457 | |
458 | |
459 (deffoo nnmairix-request-create-group (group &optional server args) | |
460 (let ((qualgroup (if server (gnus-group-prefixed-name group (list 'nnmairix server)) | |
461 group)) | |
462 (exist t) | |
463 (count 0) | |
464 groupname info) | |
465 (when server (nnmairix-open-server server)) | |
466 (gnus-group-add-parameter qualgroup '(query . nil)) | |
467 (gnus-group-add-parameter qualgroup '(threads . nil)) | |
468 (while exist | |
469 (setq count (1+ count)) | |
470 (setq groupname (format "%s-%s-%s" nnmairix-group-prefix group | |
471 (number-to-string count))) | |
472 (setq exist (nnmairix-call-backend | |
473 "request-group" groupname nnmairix-backend-server))) | |
474 (nnmairix-call-backend | |
475 "request-create-group" groupname nnmairix-backend-server) | |
476 (gnus-group-add-parameter qualgroup '(folder . nil)) | |
477 (gnus-group-set-parameter qualgroup 'folder groupname)) | |
478 t) | |
479 | |
480 | |
481 (deffoo nnmairix-retrieve-headers (articles group &optional server fetch-old) | |
482 (when server (nnmairix-open-server server)) | |
483 (let* ((folder (nnmairix-get-backend-folder group server)) | |
484 (corr (nnmairix-get-numcorr group server)) | |
485 (numcorr 0) | |
486 rval) | |
487 (when (and corr | |
488 (not (zerop (cadr corr))) | |
489 (numberp (car articles))) | |
490 (setq numcorr (cadr corr)) | |
491 (setq articles | |
492 (mapcar | |
493 (lambda (arg) (- arg numcorr)) | |
494 articles))) | |
495 (setq rval (nnmairix-call-backend | |
496 "retrieve-headers" articles folder nnmairix-backend-server fetch-old)) | |
497 (when (eq rval 'nov) | |
498 (nnmairix-replace-group-and-numbers articles folder group numcorr) | |
499 rval))) | |
500 | |
501 (deffoo nnmairix-request-article (article &optional group server to-buffer) | |
502 (when server (nnmairix-open-server server)) | |
503 (let ((folder (nnmairix-get-backend-folder group server)) | |
504 (corr (nnmairix-get-numcorr group server))) | |
505 (when (and | |
506 (numberp article) | |
507 corr | |
508 (not (zerop (cadr corr)))) | |
509 (setq article (- article (cadr corr)))) | |
510 (nnmairix-call-backend | |
511 "request-article" article folder nnmairix-backend-server to-buffer)) | |
512 t) | |
513 | |
514 (deffoo nnmairix-close-group (group &optional server) | |
515 ;; Should we do something here? | |
516 nil) | |
517 | |
518 | |
519 (deffoo nnmairix-request-list (&optional server) | |
520 (when server (nnmairix-open-server server)) | |
521 (if (nnmairix-call-backend "request-list" nnmairix-backend-server) | |
522 (let (cpoint cur qualgroup folder) | |
523 (save-excursion | |
524 (set-buffer nntp-server-buffer) | |
525 (goto-char (point-min)) | |
526 (setq cpoint (point)) | |
527 (while (re-search-forward nnmairix-group-regexp (point-max) t) | |
528 (setq cur (match-string 1) | |
529 qualgroup (gnus-group-prefixed-name cur | |
530 (list 'nnmairix server))) | |
531 (if (and (gnus-group-entry qualgroup) | |
532 (string= (match-string 0) | |
533 (gnus-group-get-parameter qualgroup 'folder))) | |
534 (progn | |
535 (replace-match cur) | |
536 (delete-region cpoint (point-at-bol)) | |
537 (forward-line) | |
538 (setq cpoint (point))) | |
539 (forward-line))) | |
540 (delete-region cpoint (point-max))) | |
541 t) | |
542 nil)) | |
543 | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
544 |
92255 | 545 (nnoo-define-skeleton nnmairix) |
546 | |
547 | |
548 ;;; === Interactive functions | |
549 | |
550 (defun nnmairix-create-search-group (server group query threads) | |
551 "Create on SERVER nnmairix search group GROUP with QUERY. | |
552 If THREADS is t, include whole threads from found messages. If | |
553 called interactively, user will be asked for parameters." | |
554 (interactive | |
555 (list | |
556 (gnus-server-to-method (car (nnmairix-get-server))) | |
557 (read-string "Group name: ") | |
558 (read-string "Query: ") | |
559 (y-or-n-p "Include threads? "))) | |
560 (when (and (stringp query) | |
561 (string-match "\\s-" query)) | |
562 (setq query (split-string query))) | |
563 (when (not (listp query)) | |
564 (setq query (list query))) | |
565 (when (and server group query) | |
566 (save-excursion | |
567 (let ((groupname (gnus-group-prefixed-name group server)) | |
568 info) | |
569 (set-buffer gnus-group-buffer) | |
570 (gnus-group-make-group group server) | |
571 (gnus-group-set-parameter groupname 'query query) | |
572 (gnus-group-set-parameter groupname 'threads threads) | |
573 (nnmairix-update-and-clear-marks groupname))))) | |
574 | |
575 (defun nnmairix-search-interactive () | |
576 "Create mairix search interactively with the minibuffer." | |
577 (interactive) | |
578 (let ((char-header nnmairix-interactive-query-parameters) | |
579 header finished query achar) | |
580 (while (not finished) | |
581 (while (not achar) | |
582 (message "Query (%s): " (nnmairix-create-message-line-for-search)) | |
583 (setq achar (read-char)) | |
584 (when (not (assoc achar char-header)) | |
585 (setq achar nil))) | |
586 (setq header (read-string | |
587 (concat "Match " (nth 3 (assoc achar char-header)) " on: "))) | |
588 (push (concat (nth 2 (assoc achar char-header)) ":" header) query) | |
589 (setq finished (not (y-or-n-p "Add another search query? ")) | |
590 achar nil)) | |
591 (nnmairix-search | |
592 (mapconcat 'identity query " ") | |
593 (car (nnmairix-get-server)) | |
594 (y-or-n-p "Include whole threads? ")))) | |
595 | |
596 (defun nnmairix-create-search-group-from-message () | |
597 "Interactively create search group with query based on current message." | |
598 (interactive) | |
599 (let ((char-header nnmairix-interactive-query-parameters) | |
600 (server (nnmairix-backend-to-server gnus-current-select-method)) | |
601 query achar header finished group threads cq) | |
602 (when (or (not (gnus-buffer-live-p gnus-article-buffer)) | |
603 (not (gnus-buffer-live-p gnus-summary-buffer))) | |
604 (error "No article or summary buffer")) | |
605 (when (not server) | |
606 (error "No nnmairix server found for back end %s:%s" | |
607 (symbol-name (car gnus-current-select-method)) | |
608 (nth 1 gnus-current-select-method))) | |
609 (while (not finished) | |
610 (save-excursion | |
611 (gnus-summary-toggle-header 1) | |
612 (while (not achar) | |
613 (message "Query (%s): " (nnmairix-create-message-line-for-search)) | |
614 (setq achar (read-char)) | |
615 (when (not (assoc achar char-header)) | |
616 (setq achar nil))) | |
617 (set-buffer gnus-article-buffer) | |
618 (setq header nil) | |
619 (when (setq cq (nth 1 (assoc achar char-header))) | |
620 (setq header | |
621 (nnmairix-replace-illegal-chars | |
622 (gnus-fetch-field (nth 1 (assoc achar char-header)))))) | |
623 (setq header (read-string | |
624 (concat "Match " (nth 3 (assoc achar char-header)) " on: ") | |
625 header)) | |
626 (push (concat (nth 2 (assoc achar char-header)) ":" header) query) | |
627 (setq finished (not (y-or-n-p "Add another search query? ")) | |
628 achar nil))) | |
629 (setq threads (y-or-n-p "Include whole threads? ")) | |
630 (setq group (read-string "Group name: ")) | |
631 (set-buffer gnus-summary-buffer) | |
632 (message "Creating group %s on server %s with query %s." group | |
633 (gnus-method-to-server server) (mapconcat 'identity query " ")) | |
634 (nnmairix-create-search-group server group query threads))) | |
635 | |
636 (defun nnmairix-create-server-and-default-group () | |
637 "Interactively create new nnmairix server with default search group. | |
638 All necessary information will be queried from the user." | |
639 (interactive) | |
640 (let* ((name (read-string "Name of the mairix server: ")) | |
641 (server (completing-read "Back end server (TAB for completion): " | |
642 (nnmairix-get-valid-servers))) | |
643 (mairix (read-string "Command to call mairix: " "mairix")) | |
644 (defaultgroup (read-string "Default search group: ")) | |
645 (backend (symbol-name (car (gnus-server-to-method server)))) | |
646 (servername (nth 1 (gnus-server-to-method server))) | |
647 (hidden (and (string-match "^nn\\(imap\\|maildir\\)$" backend) | |
648 (y-or-n-p | |
649 "Does the back end server work with maildir++ (i.e. hidden directories)? "))) | |
650 create) | |
651 | |
652 (apply (intern (format "%s-%s" backend "open-server")) | |
653 (list servername)) | |
654 | |
655 (when (and hidden | |
656 (string-match "^\\." defaultgroup)) | |
657 (setq defaultgroup (substring defaultgroup 1))) | |
658 ;; Create default search group | |
659 (gnus-group-make-group | |
660 defaultgroup (list 'nnmairix name (list 'nnmairix-backend (intern backend)) | |
661 (list 'nnmairix-backend-server servername) | |
662 (list 'nnmairix-mairix-command mairix) | |
663 (list 'nnmairix-hidden-folders hidden) | |
664 (list 'nnmairix-default-group defaultgroup))))) | |
665 | |
666 | |
667 (defun nnmairix-group-change-query-this-group (&optional query) | |
668 "Set QUERY for group under cursor." | |
669 (interactive) | |
670 (let* ((group (gnus-group-group-name)) | |
671 (method (gnus-find-method-for-group group)) | |
672 (oldquery (gnus-group-get-parameter group 'query t))) | |
673 (if (eq (car method) 'nnmairix) | |
674 (progn | |
675 (when (listp oldquery) | |
676 (setq oldquery (mapconcat 'identity oldquery " "))) | |
677 (setq query (or query | |
678 (read-string "New query: " oldquery))) | |
679 (when (stringp query) | |
680 (setq query (split-string query))) | |
681 (when query | |
682 (gnus-group-set-parameter group 'query query) | |
683 (nnmairix-update-and-clear-marks group))) | |
684 (error "This is no nnmairix group")))) | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
685 |
92255 | 686 |
687 (defun nnmairix-group-toggle-threads-this-group (&optional threads) | |
688 "Toggle threads parameter for this group. | |
689 If THREADS is a positive number, set threads parameter to t. | |
690 If THREADS is a negative number, set it to nil." | |
691 (interactive) | |
692 (let* ((group (gnus-group-group-name)) | |
693 (method (gnus-find-method-for-group group)) | |
694 (getthreads (or threads | |
695 (not (gnus-group-get-parameter group 'threads))))) | |
696 (if (eq (car method) 'nnmairix) | |
697 (progn | |
698 (when (numberp getthreads) | |
699 (setq getthreads (> getthreads 0))) | |
700 (gnus-group-set-parameter group 'threads getthreads) | |
701 (if getthreads | |
702 (message "Threads activated for group %s" group) | |
703 (message "Threads deacitavted for group %s" group)) | |
704 (nnmairix-update-and-clear-marks group)) | |
705 (error "This is no nnmairix group")))) | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
706 |
92255 | 707 |
708 (defun nnmairix-search (query &optional server threads) | |
709 "Sends QUERY to nnmairix backend SERVER, using default its search group. | |
710 | |
711 Default search group is automatically entered and results are shown. | |
712 If THREADS is t, enable threads. | |
713 If THREADS is a negative number, disable threads. | |
714 Otherwise, leave threads parameter as it is." | |
715 (interactive (list (read-string "Query: "))) | |
716 (when (not server) | |
717 (setq server (car (nnmairix-get-server)))) | |
718 (if (not server) | |
719 (error "No opened nnmairix server found") | |
720 (setq server (gnus-server-to-method server))) | |
721 (nnmairix-open-server (nth 1 server)) | |
722 (let* ((qualgroup (gnus-group-prefixed-name nnmairix-default-group | |
723 (list 'nnmairix (nth 1 server))))) | |
724 (set-buffer gnus-group-buffer) | |
725 (when (stringp query) | |
726 (setq query (split-string query))) | |
727 (gnus-group-set-parameter qualgroup 'query query) | |
728 (if (symbolp threads) | |
729 (when (eq threads 't) | |
730 (gnus-group-set-parameter qualgroup 'threads t)) | |
731 (when (< threads 0) | |
732 (gnus-group-set-parameter qualgroup 'threads nil))) | |
733 (nnmairix-update-and-clear-marks qualgroup) | |
734 (when (not (zerop (gnus-group-unread qualgroup))) | |
735 (gnus-group-read-group nil t qualgroup)))) | |
736 | |
737 (defun nnmairix-search-thread-this-article () | |
738 "Search thread for the current article. | |
739 This is effectively a shortcut for calling `nnmairix-search' | |
740 with m:msgid of the current article and enabled threads." | |
741 (interactive) | |
742 (let* ((server | |
743 (nnmairix-backend-to-server gnus-current-select-method)) | |
744 mid) | |
745 (if server | |
746 (if (gnus-buffer-live-p gnus-article-buffer) | |
747 (progn | |
748 (save-excursion | |
749 (set-buffer gnus-article-buffer) | |
750 (gnus-summary-toggle-header 1) | |
751 (setq mid (message-fetch-field "Message-ID"))) | |
752 (while (string-match "[<>]" mid) | |
753 (setq mid (replace-match "" t t mid))) | |
754 (nnmairix-search (concat "m:" mid) server t)) | |
755 (message "No article buffer.")) | |
756 (error "No nnmairix server found for back end %s:%s" | |
757 (symbol-name (car gnus-current-select-method)) | |
758 (nth 1 gnus-current-select-method))))) | |
759 | |
760 (defun nnmairix-search-from-this-article () | |
761 "Search messages from sender of the current article. | |
762 This is effectively a shortcut for calling `nnmairix-search' with | |
763 f:current_from." | |
764 (interactive) | |
765 (let* ((server | |
766 (nnmairix-backend-to-server gnus-current-select-method)) | |
767 from) | |
768 (if server | |
769 (if (gnus-buffer-live-p gnus-article-buffer) | |
770 (progn | |
771 (save-excursion | |
772 (set-buffer gnus-article-buffer) | |
773 (gnus-summary-toggle-header 1) | |
774 (setq from (cadr (gnus-extract-address-components | |
775 (gnus-fetch-field "From")))) | |
776 (nnmairix-search (concat "f:" from) server -1))) | |
777 (message "No article buffer.")) | |
778 (error "No nnmairix server found for back end %s:%s" | |
779 (symbol-name (car gnus-current-select-method)) | |
780 (nth 1 gnus-current-select-method))))) | |
781 | |
782 | |
783 (defun nnmairix-purge-old-groups (&optional dontask server) | |
784 "Delete mairix search groups which are no longer used. | |
785 | |
786 You may want to call this from time to time if you are creating | |
787 and deleting lots of nnmairix groups. If DONTASK is t, do not ask | |
788 before deleting a group on the back end. SERVER specifies nnmairix server." | |
789 (interactive) | |
790 (let ((server (or server | |
791 (gnus-server-to-method (car (nnmairix-get-server)))))) | |
792 (if (nnmairix-open-server (nth 1 server)) | |
793 (when (nnmairix-call-backend | |
794 "request-list" nnmairix-backend-server) | |
795 (let (cur qualgroup folder) | |
796 (save-excursion | |
797 (set-buffer nntp-server-buffer) | |
798 (goto-char (point-min)) | |
799 (while (re-search-forward nnmairix-group-regexp (point-max) t) | |
800 (setq cur (match-string 0) | |
801 qualgroup (gnus-group-prefixed-name | |
802 (match-string 1) server)) | |
803 (when (not (and (gnus-group-entry qualgroup) | |
804 (string= cur | |
805 (gnus-group-get-parameter | |
806 qualgroup 'folder)))) | |
807 (when (or dontask | |
808 (y-or-n-p | |
809 (concat "Delete group " cur | |
810 " on server " nnmairix-backend-server "? "))) | |
811 (nnmairix-call-backend | |
812 "request-delete-group" cur t nnmairix-backend-server))))))) | |
813 (message "Couldn't open server %s" (nth 1 server))))) | |
814 | |
815 | |
816 (defun nnmairix-update-database (&optional servers) | |
817 "Call mairix for updating the database for SERVERS. | |
818 | |
819 If SERVERS is nil, do update for all nnmairix servers. Mairix | |
820 will be called asynchronously unless | |
821 `nnmairix-mairix-synchronous-update' is t. Mairix will be called | |
822 with `nnmairix-mairix-update-options'." | |
823 (interactive) | |
824 (let ((servers (or servers | |
825 (nnmairix-get-nnmairix-servers))) | |
826 args cur commandsplit) | |
827 (while servers | |
828 (setq cur (car (pop servers))) | |
829 (nnmairix-open-server | |
830 (nth 1 (gnus-server-to-method cur))) | |
831 (setq commandsplit (split-string nnmairix-mairix-command)) | |
832 (nnheader-message 7 "Updating mairix database for %s..." cur) | |
833 (if nnmairix-mairix-synchronous-update | |
834 (progn | |
835 (setq args (append (list (car commandsplit) nil | |
836 (get-buffer nnmairix-mairix-output-buffer) | |
837 nil))) | |
838 (if (> (length commandsplit) 1) | |
839 (setq args (append args (cdr commandsplit) nnmairix-mairix-update-options)) | |
840 (setq args (append args nnmairix-mairix-update-options))) | |
841 (apply 'call-process args) | |
842 (nnheader-message 7 "Updating mairix database for %s... done" cur)) | |
843 (progn | |
844 (setq args (append (list cur (get-buffer nnmairix-mairix-output-buffer) | |
845 (car commandsplit)))) | |
846 (if (> (length commandsplit) 1) | |
847 (setq args (append args (cdr commandsplit) nnmairix-mairix-update-options)) | |
848 (setq args (append args nnmairix-mairix-update-options))) | |
849 (set-process-sentinel (apply 'start-process args) | |
850 'nnmairix-sentinel-mairix-update-finished)))))) | |
851 | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
852 (autoload 'gnus-registry-fetch-group "gnus-registry") |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
853 |
92255 | 854 (defun nnmairix-goto-original-article (&optional no-registry) |
855 "Jump to the original group and display article. | |
856 The original group of the article is first determined with the | |
857 registry (if enabled). If the registry is not enabled or did not | |
858 find the article or the prefix NO-REGISTRY is non-nil, this | |
859 function will try to determine the original group form the path | |
860 of the mail file. The path is obtained through another mairix | |
861 search in raw mode." | |
862 (interactive "P") | |
863 (when (not (eq (car gnus-current-select-method) 'nnmairix)) | |
864 (let ((method (gnus-find-method-for-group gnus-newsgroup-name))) | |
865 (if (eq (car method) 'nnmairix) | |
866 (nnmairix-open-server (nth 1 method)) | |
867 (error "Not in a nnmairix group")))) | |
868 (when (not (gnus-buffer-live-p gnus-article-buffer)) | |
869 (error "No article buffer available")) | |
870 (let ((server (nth 1 gnus-current-select-method)) | |
871 mid rval group allgroups) | |
872 ;; get message id | |
873 (save-excursion | |
874 (set-buffer gnus-article-buffer) | |
875 (gnus-summary-toggle-header 1) | |
876 (setq mid (message-fetch-field "Message-ID")) | |
877 ;; first check the registry (if available) | |
878 (when (and (boundp 'gnus-registry-install) | |
879 gnus-registry-install | |
880 (not no-registry)) | |
881 (setq group (gnus-registry-fetch-group mid))) | |
882 (while (string-match "[<>]" mid) | |
883 (setq mid (replace-match "" t t mid))) | |
884 (unless group | |
885 ;; registry was not available or did not find article | |
886 ;; so we search again with mairix in raw mode to get filename | |
887 (nnmairix-open-server server) | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
888 (setq rval |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
889 (nnmairix-call-mairix-binary-raw |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
890 (split-string nnmairix-mairix-command) |
92255 | 891 (list (concat "m:" mid)))) |
892 (if (zerop rval) | |
893 ;; determine original group(s) from filename | |
894 (save-excursion | |
895 (set-buffer nnmairix-mairix-output-buffer) | |
896 (goto-char (point-min)) | |
897 (while (looking-at "/") | |
898 (push (nnmairix-determine-original-group) | |
899 allgroups) | |
900 (forward-line 1)) | |
901 (if (> (length allgroups) 1) | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
902 (setq group |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
903 (completing-read |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
904 "Message exists in more than one group. Choose: " |
92255 | 905 allgroups nil t)) |
906 (setq group (car allgroups)))) | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
907 (error "Mairix could not find original article. See buffer %s for details" |
92255 | 908 nnmairix-mairix-output-buffer)))) |
909 (if group | |
910 ;; show article in summary buffer | |
911 (nnmairix-show-original-article group mid) | |
912 (message "Couldn't find original article")))) | |
913 | |
914 (defun nnmairix-determine-original-group () | |
915 "Try to determine to original group from the file path." | |
916 (let (path filename serverbase group maildirflag allgroups) | |
917 (re-search-forward "^\\(.*\\)/\\(.*?\\)$") | |
918 (setq path (expand-file-name (match-string 1))) | |
919 (setq filename (match-string 2)) | |
920 ;; when we deal with maildir, remove cur/new/tmp from path | |
921 (setq maildirflag (string-match ".+\\..+\\..+" filename)) | |
922 (when maildirflag | |
923 (setq path | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
924 (replace-regexp-in-string |
92255 | 925 ".*\\(/cur\\|/new\\|/tmp\\)$" "" path t t 1))) |
926 ;; we first check nnml and nnmaildir servers | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
927 (setq |
92255 | 928 group |
929 (catch 'found | |
930 (dolist (cur gnus-opened-servers) | |
931 (when (or (and (not maildirflag) | |
932 (eq (caar cur) 'nnml)) | |
933 (and maildirflag | |
934 (eq (caar cur) 'nnmaildir))) | |
935 ;; get base path from server | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
936 (if maildirflag |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
937 (setq serverbase (cadr (assoc 'directory (car cur)))) |
92255 | 938 (setq serverbase (cadr (assoc 'nnml-directory (car cur)))) |
939 (when (not serverbase) | |
940 (setq serverbase nnml-directory))) | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
941 (setq serverbase (file-name-as-directory |
92255 | 942 (expand-file-name serverbase))) |
943 (when (string-match (concat serverbase "\\(.*\\)") path) | |
944 ;; looks good - rest of the path should be the group | |
945 (setq group (match-string 1 path)) | |
946 (when (string-match "/$" group) | |
947 (setq group (replace-match "" t t group))) | |
948 (when (not maildirflag) | |
949 ;; for nnml: convert slashes to dots | |
950 (while (string-match "/" group) | |
951 (setq group (replace-match "." t t group)))) | |
952 (setq group (gnus-group-prefixed-name group (car cur))) | |
953 ;; check whether this group actually exists | |
954 (when (gnus-group-entry group) | |
955 (throw 'found group))))))) | |
956 (unless group | |
957 ;; we haven't found it yet --> look for nnimap groups | |
958 ;; assume last element of the path is the group | |
959 (string-match "^.*/\\.?\\(.*\\)$" path) | |
960 (setq group (match-string 1 path)) | |
961 ;; convert dots to slashes (nested group) | |
962 (while (string-match "\\." group) | |
963 (setq group (replace-match "/" t t group))) | |
964 (dolist (cur gnus-opened-servers) | |
965 (when (eq (caar cur) 'nnimap) | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
966 (when (gnus-group-entry |
92255 | 967 (gnus-group-prefixed-name group (car cur))) |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
968 (push |
92255 | 969 (gnus-group-prefixed-name group (car cur)) |
970 allgroups)))) | |
971 (if (> (length allgroups) 1) | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
972 (setq group (completing-read |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
973 "Group %s exists on more than one IMAP server. Choose: " |
92255 | 974 allgroups nil t)) |
975 (setq group (car allgroups)))) | |
976 group)) | |
977 | |
978 | |
979 ;;; ==== Helper functions | |
980 | |
92260
261f98568bde
(nnmairix-request-group): Bind nnmairix-fast and nnmairix-group around
Glenn Morris <rgm@gnu.org>
parents:
92259
diff
changeset
|
981 ;; Set locally in nnmairix-request-group, which is the only caller of |
261f98568bde
(nnmairix-request-group): Bind nnmairix-fast and nnmairix-group around
Glenn Morris <rgm@gnu.org>
parents:
92259
diff
changeset
|
982 ;; this function. |
261f98568bde
(nnmairix-request-group): Bind nnmairix-fast and nnmairix-group around
Glenn Morris <rgm@gnu.org>
parents:
92259
diff
changeset
|
983 (defvar nnmairix-fast) |
261f98568bde
(nnmairix-request-group): Bind nnmairix-fast and nnmairix-group around
Glenn Morris <rgm@gnu.org>
parents:
92259
diff
changeset
|
984 (defvar nnmairix-group) |
261f98568bde
(nnmairix-request-group): Bind nnmairix-fast and nnmairix-group around
Glenn Morris <rgm@gnu.org>
parents:
92259
diff
changeset
|
985 |
92255 | 986 (defun nnmairix-request-group-with-article-number-correction (folder qualgroup) |
987 "Request FOLDER on backend for nnmairix QUALGROUP and article number correction." | |
988 (save-excursion | |
92260
261f98568bde
(nnmairix-request-group): Bind nnmairix-fast and nnmairix-group around
Glenn Morris <rgm@gnu.org>
parents:
92259
diff
changeset
|
989 ;; FIXME nnmairix-request-group only calls this when fast is nil (?). |
92255 | 990 (nnmairix-call-backend |
92260
261f98568bde
(nnmairix-request-group): Bind nnmairix-fast and nnmairix-group around
Glenn Morris <rgm@gnu.org>
parents:
92259
diff
changeset
|
991 "request-group" folder nnmairix-backend-server nnmairix-fast) |
92255 | 992 (set-buffer nnmairix-mairix-output-buffer) |
993 (goto-char (point-min)) | |
994 (re-search-forward "^Matched.*messages") | |
995 (nnheader-message 7 (match-string 0)) | |
996 (set-buffer nntp-server-buffer) | |
997 (goto-char (point-min)) | |
998 (let ((status (read (current-buffer))) | |
999 (total (read (current-buffer))) | |
1000 (low (read (current-buffer))) | |
1001 (high (read (current-buffer))) | |
1002 (corr (gnus-group-get-parameter qualgroup 'numcorr t))) | |
1003 (if (= status 211) | |
1004 (progn | |
1005 ;; Article number correction | |
1006 (if (and corr | |
1007 (> (+ (car (cddr corr)) high) 0)) | |
1008 (progn | |
1009 (when (car corr) ;Group has changed | |
1010 (setq corr | |
1011 (list nil | |
1012 (car (cddr corr)) | |
1013 (+ (car (cddr corr)) high))) | |
1014 (gnus-group-set-parameter | |
1015 qualgroup 'numcorr corr)) | |
1016 (setq low (+ low (cadr corr)) | |
1017 high (+ high (cadr corr)))) | |
1018 (when (member nnmairix-backend | |
1019 nnmairix-delete-and-create-on-change) | |
1020 (gnus-group-set-parameter | |
1021 qualgroup 'numcorr (list nil 0 high)))) | |
1022 (erase-buffer) | |
92260
261f98568bde
(nnmairix-request-group): Bind nnmairix-fast and nnmairix-group around
Glenn Morris <rgm@gnu.org>
parents:
92259
diff
changeset
|
1023 (insert (format "%d %d %d %d %s" status total low high |
261f98568bde
(nnmairix-request-group): Bind nnmairix-fast and nnmairix-group around
Glenn Morris <rgm@gnu.org>
parents:
92259
diff
changeset
|
1024 nnmairix-group)) |
92255 | 1025 t) |
1026 (progn | |
1027 (nnheader-report | |
1028 'nnmairix "Error calling back end on group %s" folder) | |
1029 nil))))) | |
1030 | |
1031 (defun nnmairix-call-mairix-binary (command folder query threads) | |
1032 "Call mairix binary with COMMAND, using FOLDER and QUERY. | |
1033 If THREADS is non-nil, enable full threads." | |
1034 (let ((args (cons (car command) '(nil t nil)))) | |
1035 (save-excursion | |
1036 (set-buffer | |
1037 (get-buffer-create nnmairix-mairix-output-buffer)) | |
1038 (erase-buffer) | |
1039 (when (> (length command) 1) | |
1040 (setq args (append args (cdr command)))) | |
1041 (when threads | |
1042 (setq args (append args '("-t")))) | |
1043 (apply 'call-process | |
1044 (append args (list "-o" folder) query))))) | |
1045 | |
1046 (defun nnmairix-call-mairix-binary-raw (command query) | |
1047 "Call mairix binary with COMMAND and QUERY in raw mode." | |
1048 (let ((args (cons (car command) '(nil t nil)))) | |
1049 (save-excursion | |
1050 (set-buffer | |
1051 (get-buffer-create nnmairix-mairix-output-buffer)) | |
1052 (erase-buffer) | |
1053 (when (> (length command) 1) | |
1054 (setq args (append args (cdr command)))) | |
1055 (setq args (append args '("-r"))) | |
1056 (apply 'call-process | |
1057 (append args query))))) | |
1058 | |
1059 (defun nnmairix-get-server () | |
1060 "If there exists just one nnmairix server, return its value. | |
1061 Otherwise, ask user for server." | |
1062 (let ((openedserver (nnmairix-get-nnmairix-servers))) | |
1063 (when (not openedserver) | |
1064 (error "No opened nnmairix server found")) | |
1065 (if (> (length openedserver) 1) | |
1066 (progn | |
1067 (while | |
1068 (equal '("") | |
1069 (setq nnmairix-last-server | |
1070 (list (completing-read "Server: " openedserver nil 1 | |
1071 (or nnmairix-last-server | |
1072 "nnmairix:")))))) | |
1073 nnmairix-last-server) | |
1074 (car openedserver)))) | |
1075 | |
1076 (defun nnmairix-get-nnmairix-servers (&optional all) | |
1077 "Return available nnmairix servers. | |
1078 If ALL is t, return also the unopened/failed ones." | |
1079 (let ((alist gnus-opened-servers) | |
1080 server openedserver) | |
1081 (while alist | |
1082 (setq server (pop alist)) | |
1083 (when (and server | |
1084 (or all | |
1085 (eq (cadr server) 'ok)) | |
1086 (eq (caar server) 'nnmairix) | |
1087 (not (member (car server) gnus-ephemeral-servers))) | |
1088 (setq server | |
1089 (concat (symbol-name (caar server)) ":" (nth 1 (car server)))) | |
1090 (push (list server) openedserver))) | |
1091 openedserver)) | |
1092 | |
1093 | |
1094 (defun nnmairix-get-valid-servers () | |
1095 "Return list of valid backend servers for nnmairix groups." | |
1096 (let ((alist gnus-opened-servers) | |
1097 (mairixservers (nnmairix-get-nnmairix-servers t)) | |
1098 server mserver openedserver occ cur) | |
1099 ;; Get list of all nnmairix backends (i.e. backends which are | |
1100 ;; already occupied) | |
1101 (dolist (cur mairixservers) | |
1102 (push | |
1103 (concat | |
1104 (symbol-name | |
1105 (cadr (assoc 'nnmairix-backend | |
1106 (gnus-server-to-method (car cur))))) | |
1107 ":" | |
1108 (cadr (assoc 'nnmairix-backend-server | |
1109 (gnus-server-to-method (car cur))))) | |
1110 occ)) | |
1111 (while alist | |
1112 (setq server (pop alist)) | |
1113 (setq mserver (gnus-method-to-server (car server))) | |
1114 ;; If this is the native server, convert it to the real server | |
1115 ;; name to avoid confusion | |
1116 (when (string= mserver "native") | |
1117 (setq mserver (format "%s:%s" | |
1118 (caar server) | |
1119 (nth 1 (car server))))) | |
1120 (when (and server | |
1121 (eq (cadr server) 'ok) | |
1122 (member (caar server) nnmairix-valid-backends) | |
1123 (not (member (car server) gnus-ephemeral-servers)) | |
1124 (not (member (gnus-method-to-server (car server)) occ))) | |
1125 (push | |
1126 (list mserver) | |
1127 openedserver))) | |
1128 openedserver)) | |
1129 | |
1130 (defun nnmairix-call-backend (func &rest args) | |
1131 "Call a function FUNC on backend with ARGS." | |
1132 (apply (intern (format "%s-%s" (symbol-name nnmairix-backend) func)) args)) | |
1133 | |
1134 (defun nnmairix-get-backend-folder (group &optional server) | |
1135 "Return back end GROUP from nnmairix group on SERVER." | |
1136 (let* ((qualgroup (if server | |
1137 (gnus-group-prefixed-name group (list 'nnmairix server)) | |
1138 group)) | |
1139 (folder (gnus-group-get-parameter qualgroup 'folder))) | |
1140 folder)) | |
1141 | |
1142 (defun nnmairix-get-numcorr (group &optional server) | |
1143 "Return values for article number correction nnmairix GROUP on SERVER." | |
1144 (let* ((qualgroup (if server | |
1145 (gnus-group-prefixed-name group (list 'nnmairix server)) | |
1146 group)) | |
1147 (corr (gnus-group-get-parameter qualgroup 'numcorr t))) | |
1148 corr)) | |
1149 | |
1150 | |
1151 (defun nnmairix-rename-files-consecutively (path) | |
1152 "Rename all nnml mail files in PATH so that they have consecutive numbers. | |
1153 This should correct problems of wrong article counts when using | |
1154 nnmairix with nnml backends." | |
1155 (let* ((files | |
1156 (sort | |
1157 (mapcar 'string-to-number | |
1158 (directory-files path nil "[0-9]+" t)) | |
1159 '<)) | |
1160 (lastplusone (car files)) | |
1161 (path (file-name-as-directory path))) | |
1162 (dolist (cur files) | |
1163 (when (not (= cur lastplusone)) | |
1164 (rename-file (concat path | |
1165 (number-to-string cur)) | |
1166 (concat path | |
1167 (number-to-string lastplusone))) | |
1168 (setq cur lastplusone)) | |
1169 (setq lastplusone (1+ cur))))) | |
1170 | |
1171 (defun nnmairix-replace-group-and-numbers (articles backendgroup mairixgroup numc) | |
1172 "Replace folder names in Xref header and correct article numbers. | |
1173 Do this for all ARTICLES on BACKENDGROUP. Replace using | |
1174 MAIRIXGROUP. NUMC contains values for article number correction." | |
1175 (let ((buf (get-buffer-create " *nnmairix buffer*")) | |
1176 (corr (not (zerop numc))) | |
1177 (name (buffer-name nntp-server-buffer)) | |
1178 header cur xref) | |
1179 (save-excursion | |
1180 (set-buffer buf) | |
1181 (erase-buffer) | |
1182 (set-buffer nntp-server-buffer) | |
1183 (goto-char (point-min)) | |
1184 (nnheader-message 7 "nnmairix: Rewriting headers...") | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1185 (mapc |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1186 (lambda (article) |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1187 (when (or (looking-at (number-to-string article)) |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1188 (nnheader-find-nov-line article)) |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1189 (setq cur (nnheader-parse-nov)) |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1190 (when corr |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1191 (setq article (+ (mail-header-number cur) numc)) |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1192 (mail-header-set-number cur article)) |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1193 (setq xref (mail-header-xref cur)) |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1194 (when (and (stringp xref) |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1195 (string-match (format "[ \t]%s:[0-9]+" backendgroup) xref)) |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1196 (setq xref (replace-match (format " %s:%d" mairixgroup article) t nil xref)) |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1197 (mail-header-set-xref cur xref)) |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1198 (set-buffer buf) |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1199 (nnheader-insert-nov cur) |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1200 (set-buffer nntp-server-buffer) |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1201 (when (not (eobp)) |
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1202 (forward-line 1)))) |
92255 | 1203 articles) |
1204 (nnheader-message 7 "nnmairix: Rewriting headers... done") | |
1205 (kill-buffer nntp-server-buffer) | |
1206 (set-buffer buf) | |
1207 (rename-buffer name) | |
1208 (setq nntp-server-buffer buf)))) | |
1209 | |
1210 (defun nnmairix-backend-to-server (server) | |
1211 "Return nnmairix server most probably responsible for back end SERVER. | |
1212 User will be asked if this cannot be determined. Result is saved in | |
1213 parameter 'indexed-servers of corresponding default search | |
1214 group." | |
1215 (let ((allservers (nnmairix-get-nnmairix-servers)) | |
1216 mairixserver found defaultgroup) | |
1217 (if (> (length allservers) 1) | |
1218 (progn | |
1219 ;; If there is more than one nnmairix server, we go through them | |
1220 (while (and allservers (not found)) | |
1221 (setq mairixserver (gnus-server-to-method (car (pop allservers)))) | |
1222 ;; First we look if SERVER is the backend of current nnmairix server | |
1223 (setq found (and (eq (cadr (assoc 'nnmairix-backend mairixserver)) | |
1224 (car server)) | |
1225 (string= (cadr (assoc 'nnmairix-backend-server mairixserver)) | |
1226 (nth 1 server)))) | |
1227 ;; If that's not the case, we look at 'indexed-servers | |
1228 ;; variable in default search group | |
1229 (when (not found) | |
1230 (setq defaultgroup (cadr (assoc 'nnmairix-default-group mairixserver))) | |
1231 (setq found (member (gnus-method-to-server server) | |
1232 (gnus-group-get-parameter | |
1233 (gnus-group-prefixed-name defaultgroup | |
1234 mairixserver) | |
1235 'indexed-servers t))))) | |
1236 ;; If still not found, we ask user | |
1237 (when (not found) | |
1238 (setq mairixserver | |
1239 (gnus-server-to-method | |
1240 (completing-read | |
1241 (format "Cannot determine which nnmairix server indexes %s. Please specify: " | |
1242 (gnus-method-to-server server)) | |
1243 (nnmairix-get-nnmairix-servers) nil nil "nnmairix:"))) | |
1244 ;; Save result in parameter of default search group so that | |
1245 ;; we don't have to ask again | |
1246 (setq defaultgroup (gnus-group-prefixed-name | |
1247 (cadr (assoc 'nnmairix-default-group mairixserver)) mairixserver)) | |
1248 (gnus-group-set-parameter | |
1249 defaultgroup | |
1250 'indexed-servers | |
1251 (append (gnus-group-get-parameter defaultgroup 'indexed-servers t) | |
1252 (list (gnus-method-to-server server))))) | |
1253 mairixserver) | |
1254 ;; If there is just one (or none) nnmairix server: | |
1255 (gnus-server-to-method (caar allservers))))) | |
1256 | |
1257 (defun nnmairix-update-and-clear-marks (group &optional method) | |
1258 "Update group and clear all marks from GROUP using METHOD." | |
1259 (when method | |
1260 (setq group (gnus-group-prefixed-name group method))) | |
1261 (let ((method (or method | |
1262 (gnus-find-method-for-group group))) | |
1263 (folder (gnus-group-get-parameter group 'folder)) | |
1264 (corr (gnus-group-get-parameter group 'numcorr t)) | |
1265 info) | |
1266 (if (eq (nth 0 method) 'nnmairix) | |
1267 (save-excursion | |
1268 (nnmairix-open-server (nth 1 method)) | |
1269 (set-buffer gnus-group-buffer) | |
1270 (setq info (gnus-get-info group)) | |
1271 ;; Clear active and info | |
1272 (gnus-set-active group nil) | |
1273 (gnus-info-clear-data info) | |
1274 ;; Delete and re-create group if needed | |
1275 (when (member nnmairix-backend nnmairix-delete-and-create-on-change) | |
1276 (if (string-match nnmairix-group-regexp folder) | |
1277 (progn | |
1278 (nnmairix-call-backend "open-server" | |
1279 nnmairix-backend-server) | |
1280 (nnmairix-call-backend "request-delete-group" | |
1281 folder t nnmairix-backend-server) | |
1282 (nnmairix-call-backend "request-create-group" | |
1283 folder nnmairix-backend-server) | |
1284 ;; set flag that group has changed for article number correction | |
1285 (when corr | |
1286 (setcar corr t) | |
1287 (gnus-group-set-parameter group 'numcorr corr))) | |
1288 (error "Nnmairix-update-and-clear-marks - delete/create with\ | |
1289 non-mairix group!! - check folder parameter"))) | |
1290 (when (gnus-group-jump-to-group group) | |
1291 (gnus-group-get-new-news-this-group))) | |
1292 (error "Nnmairix-update-and-clear-marks - Called with non-nnmairix group")))) | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1293 |
92255 | 1294 |
1295 (defun nnmairix-sentinel-mairix-update-finished (proc status) | |
1296 "Sentinel for mairix update process PROC with STATUS." | |
1297 (if (equal status "finished\n") | |
1298 (nnheader-message 7 "Updating mairix database for %s... done" proc) | |
1299 (error "There was an error updating the mairix database for server %s. \ | |
1300 See %s for details" proc nnmairix-mairix-output-buffer))) | |
1301 | |
1302 (defun nnmairix-create-message-line-for-search () | |
1303 "Create message line for interactive query in minibuffer." | |
1304 (mapconcat | |
1305 (function | |
1306 (lambda (cur) | |
1307 (format "%c=%s" (car cur) (nth 3 cur)))) | |
1308 nnmairix-interactive-query-parameters ",")) | |
1309 | |
1310 (defun nnmairix-replace-illegal-chars (header) | |
1311 "Replace illegal characters in HEADER for mairix query." | |
1312 (when header | |
1313 (if (> emacs-major-version 20) | |
1314 (while (string-match "[^-.@/,& [:alnum:]]" header) | |
1315 (setq header (replace-match "" t t header))) | |
1316 (while (string-match "[[]{}:<>]" header) | |
1317 (setq header (replace-match "" t t header)))) | |
1318 (while (string-match "[-& ]" header) | |
1319 (setq header (replace-match "," t t header))) | |
1320 header)) | |
1321 | |
1322 (defun nnmairix-show-original-article (group mid) | |
1323 "Switch to GROUP and display Article with message-id MID." | |
1324 (when (string-match "Summary" (buffer-name (current-buffer))) | |
1325 (gnus-summary-exit)) | |
1326 (pop-to-buffer gnus-group-buffer) | |
1327 (gnus-group-jump-to-group group) | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1328 (gnus-summary-read-group group 1 t) |
92255 | 1329 (gnus-summary-refer-article mid) |
1330 (gnus-summary-limit-to-headers (format "message-id: <%s>" mid)) | |
1331 (gnus-summary-select-article) | |
1332 ;; Force redisplay | |
1333 (gnus-summary-show-article) | |
1334 (nnheader-message 5 "Switched to group %s." group)) | |
1335 | |
1336 | |
1337 ;; ==== Widget stuff | |
1338 | |
1339 (defvar nnmairix-widgets) | |
1340 (defvar nnmairix-widgets-values nil) | |
1341 | |
1342 (defun nnmairix-widget-search-from-this-article () | |
1343 "Create mairix query based on current article using graphical widgets." | |
1344 (interactive) | |
1345 (nnmairix-widget-search | |
1346 (nnmairix-widget-get-values))) | |
1347 | |
1348 | |
1349 (defun nnmairix-widget-get-values () | |
1350 "Create values for editable fields from current article." | |
1351 (if (not (gnus-buffer-live-p gnus-article-buffer)) | |
1352 (error "No article buffer available") | |
1353 (save-excursion | |
1354 (gnus-summary-toggle-header 1) | |
1355 (set-buffer gnus-article-buffer) | |
1356 (mapcar | |
1357 (function | |
1358 (lambda (field) | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1359 (list (car (cddr field)) |
92255 | 1360 (if (car field) |
1361 (nnmairix-replace-illegal-chars | |
1362 (gnus-fetch-field (car field))) | |
1363 nil)))) | |
1364 nnmairix-widget-fields-list)))) | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1365 |
92255 | 1366 |
1367 (defun nnmairix-widget-search (&optional mvalues) | |
1368 "Create mairix query interactively using graphical widgets. | |
1369 MVALUES may contain values from current article." | |
1370 (interactive) | |
1371 ;; Select window for mairix customization | |
1372 (funcall nnmairix-widget-select-window-function) | |
1373 ;; generate widgets | |
1374 (nnmairix-widget-create-query mvalues) | |
1375 ;; generate Buttons | |
1376 (widget-create 'push-button | |
1377 :notify | |
1378 (if mvalues | |
1379 (lambda (&rest ignore) | |
1380 (nnmairix-widget-send-query nnmairix-widgets | |
1381 t)) | |
1382 (lambda (&rest ignore) | |
1383 (nnmairix-widget-send-query nnmairix-widgets | |
1384 nil))) | |
1385 "Send Query") | |
1386 (widget-insert " ") | |
1387 (widget-create 'push-button | |
1388 :notify | |
1389 (if mvalues | |
1390 (lambda (&rest ignore) | |
1391 (nnmairix-widget-create-group nnmairix-widgets | |
1392 t)) | |
1393 (lambda (&rest ignore) | |
1394 (nnmairix-widget-create-group nnmairix-widgets | |
1395 nil))) | |
1396 "Create permanent group") | |
1397 (widget-insert " ") | |
1398 (widget-create 'push-button | |
1399 :notify (lambda (&rest ignore) | |
1400 (kill-buffer nnmairix-customize-query-buffer)) | |
1401 "Cancel") | |
1402 (use-local-map widget-keymap) | |
1403 (widget-setup) | |
1404 (goto-char (point-min))) | |
1405 | |
1406 (defun nnmairix-widget-send-query (widgets &optional withvalues) | |
1407 "Send query from WIDGETS to mairix binary. | |
1408 If WITHVALUES is t, query is based on current article." | |
1409 (nnmairix-search | |
1410 (nnmairix-widget-make-query-from-widgets widgets) | |
1411 (if withvalues | |
1412 (gnus-method-to-server | |
1413 (nnmairix-backend-to-server gnus-current-select-method)) | |
1414 (car (nnmairix-get-server))) | |
1415 (if (widget-value (cadr (assoc "Threads" widgets))) | |
1416 t | |
1417 -1)) | |
1418 (kill-buffer nnmairix-customize-query-buffer)) | |
1419 | |
1420 (defun nnmairix-widget-create-group (widgets &optional withvalues) | |
1421 "Create nnmairix group based on current widget values WIDGETS. | |
1422 If WITHVALUES is t, query is based on current article." | |
1423 (let ((group (read-string "Name of the group: "))) | |
1424 (when (not (zerop (length group))) | |
1425 (nnmairix-create-search-group | |
1426 (if withvalues | |
1427 (gnus-method-to-server | |
1428 (nnmairix-backend-to-server gnus-current-select-method)) | |
1429 (car (nnmairix-get-server))) | |
1430 group | |
1431 (nnmairix-widget-make-query-from-widgets widgets) | |
1432 (widget-value (cadr (assoc "Threads" widgets)))))) | |
1433 (kill-buffer nnmairix-customize-query-buffer)) | |
1434 | |
1435 | |
1436 (defun nnmairix-widget-make-query-from-widgets (widgets) | |
1437 "Create mairix query from widget values WIDGETS." | |
1438 (let (query temp flag) | |
1439 ;; first we do the editable fields | |
1440 (dolist (cur nnmairix-widget-fields-list) | |
1441 ;; See if checkbox is checked | |
1442 (when (widget-value | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1443 (cadr (assoc (concat "c" (car (cddr cur))) widgets))) |
92255 | 1444 ;; create query for the field |
1445 (push | |
1446 (concat | |
1447 (nth 1 cur) | |
1448 ":" | |
1449 (nnmairix-replace-illegal-chars | |
1450 (widget-value | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1451 (cadr (assoc (concat "e" (car (cddr cur))) widgets))))) |
92255 | 1452 query))) |
1453 ;; Flags | |
1454 (when (member 'flags nnmairix-widget-other) | |
1455 (setq flag | |
1456 (mapconcat | |
1457 (function | |
1458 (lambda (flag) | |
1459 (setq temp | |
1460 (widget-value (cadr (assoc (car flag) nnmairix-widgets)))) | |
1461 (if (string= "yes" temp) | |
1462 (cadr flag) | |
1463 (if (string= "no" temp) | |
1464 (concat "-" (cadr flag)))))) | |
1465 '(("seen" "s") ("replied" "r") ("flagged" "f")) "")) | |
1466 (when (not (zerop (length flag))) | |
1467 (push (concat "F:" flag) query))) | |
1468 ;; return query string | |
1469 (mapconcat 'identity query " "))) | |
1470 | |
1471 | |
1472 (defun nnmairix-widget-create-query (&optional values) | |
1473 "Create widgets for creating mairix queries. | |
1474 Fill in VALUES if based on an article." | |
1475 (let (allwidgets) | |
1476 (when (get-buffer nnmairix-customize-query-buffer) | |
1477 (kill-buffer nnmairix-customize-query-buffer)) | |
1478 (switch-to-buffer nnmairix-customize-query-buffer) | |
1479 (kill-all-local-variables) | |
1480 (erase-buffer) | |
1481 (widget-insert "Specify your query for Mairix (check boxes for activating fields):\n\n") | |
1482 (widget-insert "(Whitespaces will be converted to ',' (i.e. AND). Use '/' for OR.)\n\n") | |
1483 ; (make-local-variable 'nnmairix-widgets) | |
1484 (setq nnmairix-widgets (nnmairix-widget-build-editable-fields values)) | |
1485 (when (member 'flags nnmairix-widget-other) | |
1486 (widget-insert "\nFlags:\n Seen: ") | |
1487 (nnmairix-widget-add "seen" | |
1488 'menu-choice | |
1489 :value "ignore" | |
1490 '(item "yes") '(item "no") '(item "ignore")) | |
1491 (widget-insert " Replied: ") | |
1492 (nnmairix-widget-add "replied" | |
1493 'menu-choice | |
1494 :value "ignore" | |
1495 '(item "yes") '(item "no") '(item "ignore")) | |
1496 (widget-insert " Ticked: ") | |
1497 (nnmairix-widget-add "flagged" | |
1498 'menu-choice | |
1499 :value "ignore" | |
1500 '(item "yes") '(item "no") '(item "ignore"))) | |
1501 (when (member 'threads nnmairix-widget-other) | |
1502 (widget-insert "\n") | |
1503 (nnmairix-widget-add "Threads" 'checkbox nil)) | |
1504 (widget-insert " Show full threads\n\n"))) | |
1505 | |
1506 | |
1507 (defun nnmairix-widget-build-editable-fields (values) | |
1508 "Build editable field widgets in `nnmairix-widget-fields-list'. | |
1509 VALUES may contain values for editable fields from current article." | |
1510 ;; how can this be done less ugly? | |
1511 (let ((ret)) | |
1512 (mapc | |
1513 (function | |
1514 (lambda (field) | |
92259
5f51e1a51413
(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
Glenn Morris <rgm@gnu.org>
parents:
92257
diff
changeset
|
1515 (setq field (car (cddr field))) |
92255 | 1516 (setq ret |
1517 (nconc | |
1518 (list | |
1519 (list | |
1520 (concat "c" field) | |
1521 (widget-create 'checkbox | |
1522 :tag field | |
1523 :notify (lambda (widget &rest ignore) | |
1524 (nnmairix-widget-toggle-activate widget)) | |
1525 nil))) | |
1526 (list | |
1527 (list | |
1528 (concat "e" field) | |
1529 (widget-create 'editable-field | |
1530 :size 60 | |
1531 :format (concat " " field ":" | |
1532 (make-string (- 11 (length field)) ?\ ) | |
1533 "%v") | |
1534 :value (or (cadr (assoc field values)) "")))) | |
1535 ret)) | |
1536 (widget-insert "\n") | |
1537 ;; Deactivate editable field | |
1538 (widget-apply (cadr (nth 1 ret)) :deactivate))) | |
1539 nnmairix-widget-fields-list) | |
1540 ret)) | |
1541 | |
1542 (defun nnmairix-widget-add (name &rest args) | |
1543 "Add a widget NAME with optional ARGS." | |
1544 (push | |
1545 (list name | |
1546 (apply 'widget-create args)) | |
1547 nnmairix-widgets)) | |
1548 | |
1549 (defun nnmairix-widget-toggle-activate (widget) | |
1550 "Toggle activation status of WIDGET dependent on corresponding checkbox value." | |
1551 (let ((field (widget-get widget :tag))) | |
1552 (if (widget-value widget) | |
1553 (widget-apply | |
1554 (cadr (assoc (concat "e" field) nnmairix-widgets)) | |
1555 :activate) | |
1556 (widget-apply | |
1557 (cadr (assoc (concat "e" field) nnmairix-widgets)) | |
1558 :deactivate))) | |
1559 (widget-setup)) | |
1560 | |
1561 (provide 'nnmairix) | |
1562 | |
1563 ;; arch-tag: bb187498-b229-4a55-8c07-6d3f80713e94 | |
1564 ;;; nnmairix.el ends here |