Synch with Oort Gnus.
[elisp/gnus.git-] / lisp / gnus-group.el
1 ;;; gnus-group.el --- group mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-start)
33 (require 'nnmail)
34 (require 'gnus-spec)
35 (require 'gnus-int)
36 (require 'gnus-range)
37 (require 'gnus-win)
38 (require 'gnus-undo)
39 (require 'time-date)
40 (require 'gnus-ems)
41
42 (defcustom gnus-group-archive-directory
43   "*ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
44   "*The address of the (ding) archives."
45   :group 'gnus-group-foreign
46   :type 'directory)
47
48 (defcustom gnus-group-recent-archive-directory
49   "*ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
50   "*The address of the most recent (ding) articles."
51   :group 'gnus-group-foreign
52   :type 'directory)
53
54 (defcustom gnus-no-groups-message "No gnus is bad news"
55   "*Message displayed by Gnus when no groups are available."
56   :group 'gnus-start
57   :type 'string)
58
59 (defcustom gnus-keep-same-level nil
60   "*Non-nil means that the next newsgroup after the current will be on the same level.
61 When you type, for instance, `n' after reading the last article in the
62 current newsgroup, you will go to the next newsgroup.  If this variable
63 is nil, the next newsgroup will be the next from the group
64 buffer.
65 If this variable is non-nil, Gnus will either put you in the
66 next newsgroup with the same level, or, if no such newsgroup is
67 available, the next newsgroup with the lowest possible level higher
68 than the current level.
69 If this variable is `best', Gnus will make the next newsgroup the one
70 with the best level."
71   :group 'gnus-group-levels
72   :type '(choice (const nil)
73                  (const best)
74                  (sexp :tag "other" t)))
75
76 (defcustom gnus-group-goto-unread t
77   "*If non-nil, movement commands will go to the next unread and subscribed group."
78   :link '(custom-manual "(gnus)Group Maneuvering")
79   :group 'gnus-group-various
80   :type 'boolean)
81
82 (defcustom gnus-goto-next-group-when-activating t
83   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group."
84   :link '(custom-manual "(gnus)Scanning New Messages")
85   :group 'gnus-group-various
86   :type 'boolean)
87
88 (defcustom gnus-permanently-visible-groups nil
89   "*Regexp to match groups that should always be listed in the group buffer.
90 This means that they will still be listed even when there are no
91 unread articles in the groups.
92
93 If nil, no groups are permanently visible."
94   :group 'gnus-group-listing
95   :type '(choice regexp (const nil)))
96
97 (defcustom gnus-list-groups-with-ticked-articles t
98   "*If non-nil, list groups that have only ticked articles.
99 If nil, only list groups that have unread articles."
100   :group 'gnus-group-listing
101   :type 'boolean)
102
103 (defcustom gnus-group-default-list-level gnus-level-subscribed
104   "*Default listing level.
105 Ignored if `gnus-group-use-permanent-levels' is non-nil."
106   :group 'gnus-group-listing
107   :type 'integer)
108
109 (defcustom gnus-group-list-inactive-groups t
110   "*If non-nil, inactive groups will be listed."
111   :group 'gnus-group-listing
112   :group 'gnus-group-levels
113   :type 'boolean)
114
115 (defcustom gnus-group-sort-function 'gnus-group-sort-by-alphabet
116   "*Function used for sorting the group buffer.
117 This function will be called with group info entries as the arguments
118 for the groups to be sorted.  Pre-made functions include
119 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-real-name',
120 `gnus-group-sort-by-unread', `gnus-group-sort-by-level',
121 `gnus-group-sort-by-score', `gnus-group-sort-by-method',
122 `gnus-group-sort-by-server', and `gnus-group-sort-by-rank'.
123
124 This variable can also be a list of sorting functions.  In that case,
125 the most significant sort function should be the last function in the
126 list."
127   :group 'gnus-group-listing
128   :link '(custom-manual "(gnus)Sorting Groups")
129   :type '(radio (function-item gnus-group-sort-by-alphabet)
130                 (function-item gnus-group-sort-by-real-name)
131                 (function-item gnus-group-sort-by-unread)
132                 (function-item gnus-group-sort-by-level)
133                 (function-item gnus-group-sort-by-score)
134                 (function-item gnus-group-sort-by-method)
135                 (function-item gnus-group-sort-by-server)
136                 (function-item gnus-group-sort-by-rank)
137                 (function :tag "other" nil)))
138
139 (defcustom gnus-group-line-format "%M\%S\%p\%P\%5y: %(%g%)%l\n"
140   "*Format of group lines.
141 It works along the same lines as a normal formatting string,
142 with some simple extensions.
143
144 %M    Only marked articles (character, \"*\" or \" \")
145 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
146 %L    Level of subscribedness (integer)
147 %N    Number of unread articles (integer)
148 %I    Number of dormant articles (integer)
149 %i    Number of ticked and dormant (integer)
150 %T    Number of ticked articles (integer)
151 %R    Number of read articles (integer)
152 %t    Estimated total number of articles (integer)
153 %y    Number of unread, unticked articles (integer)
154 %G    Group name (string)
155 %g    Qualified group name (string)
156 %D    Group description (string)
157 %s    Select method (string)
158 %o    Moderated group (char, \"m\")
159 %p    Process mark (char)
160 %O    Moderated group (string, \"(m)\" or \"\")
161 %P    Topic indentation (string)
162 %m    Whether there is new(ish) mail in the group (char, \"%\")
163 %w    Number of new(ish) mails in the group (integer)
164 %l    Whether there are GroupLens predictions for this group (string)
165 %n    Select from where (string)
166 %z    A string that look like `<%s:%n>' if a foreign select method is used
167 %d    The date the group was last entered.
168 %E    Icon as defined by `gnus-group-icon-list'.
169 %u    User defined specifier.  The next character in the format string should
170       be a letter.  Gnus will call the function gnus-user-format-function-X,
171       where X is the letter following %u.  The function will be passed the
172       current header as argument.  The function should return a string, which
173       will be inserted into the buffer just like information from any other
174       group specifier.
175
176 Text between %( and %) will be highlighted with `gnus-mouse-face' when
177 the mouse point move inside the area.  There can only be one such area.
178
179 Note that this format specification is not always respected.  For
180 reasons of efficiency, when listing killed groups, this specification
181 is ignored altogether.  If the spec is changed considerably, your
182 output may end up looking strange when listing both alive and killed
183 groups.
184
185 If you use %o or %O, reading the active file will be slower and quite
186 a bit of extra memory will be used.  %D will also worsen performance.
187 Also note that if you change the format specification to include any
188 of these specs, you must probably re-start Gnus to see them go into
189 effect."
190   :group 'gnus-group-visual
191   :type 'string)
192
193 (defcustom gnus-group-mode-line-format "Gnus: %%b {%M\%:%S}"
194   "*The format specification for the group mode line.
195 It works along the same lines as a normal formatting string,
196 with some simple extensions:
197
198 %S   The native news server.
199 %M   The native select method.
200 %:   \":\" if %S isn't \"\"."
201   :group 'gnus-group-visual
202   :type 'string)
203
204 (defcustom gnus-group-mode-hook nil
205   "Hook for Gnus group mode."
206   :group 'gnus-group-various
207   :options '(gnus-topic-mode)
208   :type 'hook)
209
210 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
211 (when (featurep 'xemacs)
212   (add-hook 'gnus-group-mode-hook 'gnus-xmas-group-menu-add)
213   (add-hook 'gnus-group-mode-hook 'gnus-xmas-setup-group-toolbar))
214
215 (defcustom gnus-group-menu-hook nil
216   "Hook run after the creation of the group mode menu."
217   :group 'gnus-group-various
218   :type 'hook)
219
220 (defcustom gnus-group-catchup-group-hook nil
221   "Hook run when catching up a group from the group buffer."
222   :group 'gnus-group-various
223   :link '(custom-manual "(gnus)Group Data")
224   :type 'hook)
225
226 (defcustom gnus-group-update-group-hook nil
227   "Hook called when updating group lines."
228   :group 'gnus-group-visual
229   :type 'hook)
230
231 (defcustom gnus-group-prepare-function 'gnus-group-prepare-flat
232   "*A function that is called to generate the group buffer.
233 The function is called with three arguments: The first is a number;
234 all group with a level less or equal to that number should be listed,
235 if the second is non-nil, empty groups should also be displayed.  If
236 the third is non-nil, it is a number.  No groups with a level lower
237 than this number should be displayed.
238
239 The only current function implemented is `gnus-group-prepare-flat'."
240   :group 'gnus-group-listing
241   :type 'function)
242
243 (defcustom gnus-group-prepare-hook nil
244   "Hook called after the group buffer has been generated.
245 If you want to modify the group buffer, you can use this hook."
246   :group 'gnus-group-listing
247   :type 'hook)
248
249 (defcustom gnus-suspend-gnus-hook nil
250   "Hook called when suspending (not exiting) Gnus."
251   :group 'gnus-exit
252   :type 'hook)
253
254 (defcustom gnus-exit-gnus-hook nil
255   "Hook called when exiting Gnus."
256   :group 'gnus-exit
257   :type 'hook)
258
259 (defcustom gnus-after-exiting-gnus-hook nil
260   "Hook called after exiting Gnus."
261   :group 'gnus-exit
262   :type 'hook)
263
264 (defcustom gnus-group-update-hook '(gnus-group-highlight-line)
265   "Hook called when a group line is changed.
266 The hook will not be called if `gnus-visual' is nil.
267
268 The default function `gnus-group-highlight-line' will
269 highlight the line according to the `gnus-group-highlight'
270 variable."
271   :group 'gnus-group-visual
272   :type 'hook)
273
274 (defcustom gnus-useful-groups
275   '(("(ding) mailing list mirrored at sunsite.auc.dk"
276      "emacs.ding"
277      (nntp "sunsite.auc.dk"
278            (nntp-address "sunsite.auc.dk")))
279     ("gnus-bug archive"
280      "gnus-bug"
281      (nndir "/ftp@ftp.ifi.uio.no:/pub/emacs/gnus/gnus-bug/"))
282     ("Gnus help group"
283      "gnus-help"
284      (nndoc "gnus-help"
285             (nndoc-article-type mbox)
286             (eval `(nndoc-address
287                     ,(let ((file (nnheader-find-etc-directory
288                                   "gnus-tut.txt" t)))
289                        (unless file
290                          (error "Couldn't find doc group"))
291                        file))))))
292   "*Alist of useful group-server pairs."
293   :group 'gnus-group-listing
294   :type '(repeat (list (string :tag "Description")
295                        (string :tag "Name")
296                        (sexp :tag "Method"))))
297
298 (defcustom gnus-group-highlight
299   '(;; Mail.
300     ((and mailp (= unread 0) (eq level 1)) .
301      gnus-group-mail-1-empty-face)
302     ((and mailp (eq level 1)) .
303      gnus-group-mail-1-face)
304     ((and mailp (= unread 0) (eq level 2)) .
305      gnus-group-mail-2-empty-face)
306     ((and mailp (eq level 2)) .
307      gnus-group-mail-2-face)
308     ((and mailp (= unread 0) (eq level 3)) .
309      gnus-group-mail-3-empty-face)
310     ((and mailp (eq level 3)) .
311      gnus-group-mail-3-face)
312     ((and mailp (= unread 0)) .
313      gnus-group-mail-low-empty-face)
314     ((and mailp) .
315      gnus-group-mail-low-face)
316     ;; News.
317     ((and (= unread 0) (eq level 1)) .
318      gnus-group-news-1-empty-face)
319     ((and (eq level 1)) .
320      gnus-group-news-1-face)
321     ((and (= unread 0) (eq level 2)) .
322      gnus-group-news-2-empty-face)
323     ((and (eq level 2)) .
324      gnus-group-news-2-face)
325     ((and (= unread 0) (eq level 3)) .
326      gnus-group-news-3-empty-face)
327     ((and (eq level 3)) .
328      gnus-group-news-3-face)
329     ((and (= unread 0) (eq level 4)) .
330      gnus-group-news-4-empty-face)
331     ((and (eq level 4)) .
332      gnus-group-news-4-face)
333     ((and (= unread 0) (eq level 5)) .
334      gnus-group-news-5-empty-face)
335     ((and (eq level 5)) .
336      gnus-group-news-5-face)
337     ((and (= unread 0) (eq level 6)) .
338      gnus-group-news-6-empty-face)
339     ((and (eq level 6)) .
340      gnus-group-news-6-face)
341     ((and (= unread 0)) .
342      gnus-group-news-low-empty-face)
343     (t .
344        gnus-group-news-low-face))
345   "*Controls the highlighting of group buffer lines.
346
347 Below is a list of `Form'/`Face' pairs.  When deciding how a a
348 particular group line should be displayed, each form is
349 evaluated.  The content of the face field after the first true form is
350 used.  You can change how those group lines are displayed by
351 editing the face field.
352
353 It is also possible to change and add form fields, but currently that
354 requires an understanding of Lisp expressions.  Hopefully this will
355 change in a future release.  For now, you can use the following
356 variables in the Lisp expression:
357
358 group: The name of the group.
359 unread: The number of unread articles in the group.
360 method: The select method used.
361 mailp: Whether it's a mail group or not.
362 level: The level of the group.
363 score: The score of the group.
364 ticked: The number of ticked articles."
365   :group 'gnus-group-visual
366   :type '(repeat (cons (sexp :tag "Form") face)))
367
368 (defcustom gnus-new-mail-mark ?%
369   "Mark used for groups with new mail."
370   :group 'gnus-group-visual
371   :type 'character)
372
373 (defgroup gnus-group-icons nil
374   "Add Icons to your group buffer.  "
375   :group 'gnus-group-visual)
376
377 (defcustom gnus-group-icon-list
378   nil
379   "*Controls the insertion of icons into group buffer lines.
380
381 Below is a list of `Form'/`File' pairs.  When deciding how a
382 particular group line should be displayed, each form is evaluated.
383 The icon from the file field after the first true form is used.  You
384 can change how those group lines are displayed by editing the file
385 field.  The File will either be found in the
386 `gnus-group-glyph-directory' or by designating absolute path to the
387 file.
388
389 It is also possible to change and add form fields, but currently that
390 requires an understanding of Lisp expressions.  Hopefully this will
391 change in a future release.  For now, you can use the following
392 variables in the Lisp expression:
393
394 group: The name of the group.
395 unread: The number of unread articles in the group.
396 method: The select method used.
397 mailp: Whether it's a mail group or not.
398 newsp: Whether it's a news group or not
399 level: The level of the group.
400 score: The score of the group.
401 ticked: The number of ticked articles."
402   :group 'gnus-group-icons
403   :type '(repeat (cons (sexp :tag "Form") file)))
404
405 (defcustom gnus-group-name-charset-method-alist nil
406   "*Alist of method and the charset for group names.
407
408 For example:
409     (((nntp \"news.com.cn\") . cn-gb-2312))
410 "
411   :version "21.1"
412   :group 'gnus-charset
413   :type '(repeat (cons (sexp :tag "Method") (symbol :tag "Charset"))))
414
415 (defcustom gnus-group-name-charset-group-alist nil
416   "*Alist of group regexp and the charset for group names.
417
418 For example:
419     ((\"\\.com\\.cn:\" . cn-gb-2312))
420 "
421   :group 'gnus-charset
422   :type '(repeat (cons (regexp :tag "Group") (symbol :tag "Charset"))))
423
424 (defcustom gnus-group-jump-to-group-prompt nil
425   "Default prompt for `gnus-group-jump-to-group'.
426 If non-nil, the value should be a string, e.g. \"nnml:\",
427 in which case `gnus-group-jump-to-group' offers \"Group: nnml:\"
428 in the minibuffer prompt."
429   :group 'gnus-group-various
430   :type '(choice (string :tag "Prompt string")
431                  (const :tag "Empty" nil)))
432
433 (defvar gnus-group-listing-limit 1000
434   "*A limit of the number of groups when listing.
435 If the number of groups is larger than the limit, list them in a
436 simple manner.")
437
438 ;;; Internal variables
439
440 (defvar gnus-group-sort-alist-function 'gnus-group-sort-flat
441   "Function for sorting the group buffer.")
442
443 (defvar gnus-group-sort-selected-function 'gnus-group-sort-selected-flat
444   "Function for sorting the selected groups in the group buffer.")
445
446 (defvar gnus-group-indentation-function nil)
447 (defvar gnus-goto-missing-group-function nil)
448 (defvar gnus-group-update-group-function nil)
449 (defvar gnus-group-goto-next-group-function nil
450   "Function to override finding the next group after listing groups.")
451
452 (defvar gnus-group-edit-buffer nil)
453
454 (defvar gnus-group-line-format-alist
455   `((?M gnus-tmp-marked-mark ?c)
456     (?S gnus-tmp-subscribed ?c)
457     (?L gnus-tmp-level ?d)
458     (?N (cond ((eq number t) "*" )
459               ((numberp number)
460                (int-to-string
461                 (+ number
462                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
463                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
464               (t number)) ?s)
465     (?R gnus-tmp-number-of-read ?s)
466     (?t gnus-tmp-number-total ?d)
467     (?y gnus-tmp-number-of-unread ?s)
468     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
469     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
470     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
471            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
472     (?g gnus-tmp-group ?s)
473     (?G gnus-tmp-qualified-group ?s)
474     (?c (gnus-short-group-name gnus-tmp-group) ?s)
475     (?D gnus-tmp-newsgroup-description ?s)
476     (?o gnus-tmp-moderated ?c)
477     (?O gnus-tmp-moderated-string ?s)
478     (?p gnus-tmp-process-marked ?c)
479     (?s gnus-tmp-news-server ?s)
480     (?n gnus-tmp-news-method ?s)
481     (?P gnus-group-indentation ?s)
482     (?E gnus-tmp-group-icon ?s)
483     (?l gnus-tmp-grouplens ?s)
484     (?z gnus-tmp-news-method-string ?s)
485     (?m (gnus-group-new-mail gnus-tmp-group) ?c)
486     (?w (if (gnus-news-group-p gnus-tmp-group)
487             ""
488           (int-to-string
489            (length
490             (nnmail-new-mail-numbers (gnus-group-real-name gnus-tmp-group))
491             )))
492         ?s)
493     (?d (gnus-group-timestamp-string gnus-tmp-group) ?s)
494     (?u gnus-tmp-user-defined ?s)))
495
496 (defvar gnus-group-mode-line-format-alist
497   `((?S gnus-tmp-news-server ?s)
498     (?M gnus-tmp-news-method ?s)
499     (?u gnus-tmp-user-defined ?s)
500     (?: gnus-tmp-colon ?s)))
501
502 (defvar gnus-topic-topology nil
503   "The complete topic hierarchy.")
504
505 (defvar gnus-topic-alist nil
506   "The complete topic-group alist.")
507
508 (defvar gnus-group-marked nil)
509
510 (defvar gnus-group-list-mode nil)
511
512
513 (defvar gnus-group-icon-cache nil)
514
515 (defvar gnus-group-listed-groups nil)
516 (defvar gnus-group-list-option nil)
517
518 ;;;
519 ;;; Gnus group mode
520 ;;;
521
522 (put 'gnus-group-mode 'mode-class 'special)
523
524 (when t
525   (gnus-define-keys gnus-group-mode-map
526     " " gnus-group-read-group
527     "=" gnus-group-select-group
528     "\r" gnus-group-select-group
529     "\M-\r" gnus-group-quick-select-group
530     "\M- " gnus-group-visible-select-group
531     [(meta control return)] gnus-group-select-group-ephemerally
532     "j" gnus-group-jump-to-group
533     "n" gnus-group-next-unread-group
534     "p" gnus-group-prev-unread-group
535     "\177" gnus-group-prev-unread-group
536     [delete] gnus-group-prev-unread-group
537     [backspace] gnus-group-prev-unread-group
538     "N" gnus-group-next-group
539     "P" gnus-group-prev-group
540     "\M-n" gnus-group-next-unread-group-same-level
541     "\M-p" gnus-group-prev-unread-group-same-level
542     "," gnus-group-best-unread-group
543     "." gnus-group-first-unread-group
544     "u" gnus-group-unsubscribe-current-group
545     "U" gnus-group-unsubscribe-group
546     "c" gnus-group-catchup-current
547     "C" gnus-group-catchup-current-all
548     "\M-c" gnus-group-clear-data
549     "l" gnus-group-list-groups
550     "L" gnus-group-list-all-groups
551     "m" gnus-group-mail
552     "g" gnus-group-get-new-news
553     "\M-g" gnus-group-get-new-news-this-group
554     "R" gnus-group-restart
555     "r" gnus-group-read-init-file
556     "B" gnus-group-browse-foreign-server
557     "b" gnus-group-check-bogus-groups
558     "F" gnus-group-find-new-groups
559     "\C-c\C-d" gnus-group-describe-group
560     "\M-d" gnus-group-describe-all-groups
561     "\C-c\C-a" gnus-group-apropos
562     "\C-c\M-\C-a" gnus-group-description-apropos
563     "a" gnus-group-post-news
564     "\ek" gnus-group-edit-local-kill
565     "\eK" gnus-group-edit-global-kill
566     "\C-k" gnus-group-kill-group
567     "\C-y" gnus-group-yank-group
568     "\C-w" gnus-group-kill-region
569     "\C-x\C-t" gnus-group-transpose-groups
570     "\C-c\C-l" gnus-group-list-killed
571     "\C-c\C-x" gnus-group-expire-articles
572     "\C-c\M-\C-x" gnus-group-expire-all-groups
573     "V" gnus-version
574     "s" gnus-group-save-newsrc
575     "z" gnus-group-suspend
576     "q" gnus-group-exit
577     "Q" gnus-group-quit
578     "?" gnus-group-describe-briefly
579     "\C-c\C-i" gnus-info-find-node
580     "\M-e" gnus-group-edit-group-method
581     "^" gnus-group-enter-server-mode
582     gnus-mouse-2 gnus-mouse-pick-group
583     "<" beginning-of-buffer
584     ">" end-of-buffer
585     "\C-c\C-b" gnus-bug
586     "\C-c\C-s" gnus-group-sort-groups
587     "t" gnus-topic-mode
588     "\C-c\M-g" gnus-activate-all-groups
589     "\M-&" gnus-group-universal-argument
590     "#" gnus-group-mark-group
591     "\M-#" gnus-group-unmark-group)
592
593   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
594     "m" gnus-group-mark-group
595     "u" gnus-group-unmark-group
596     "w" gnus-group-mark-region
597     "b" gnus-group-mark-buffer
598     "r" gnus-group-mark-regexp
599     "U" gnus-group-unmark-all-groups)
600
601   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
602     "d" gnus-group-make-directory-group
603     "h" gnus-group-make-help-group
604     "u" gnus-group-make-useful-group
605     "a" gnus-group-make-archive-group
606     "k" gnus-group-make-kiboze-group
607     "l" gnus-group-nnimap-edit-acl
608     "m" gnus-group-make-group
609     "E" gnus-group-edit-group
610     "e" gnus-group-edit-group-method
611     "p" gnus-group-edit-group-parameters
612     "v" gnus-group-add-to-virtual
613     "V" gnus-group-make-empty-virtual
614     "D" gnus-group-enter-directory
615     "f" gnus-group-make-doc-group
616     "w" gnus-group-make-web-group
617     "r" gnus-group-rename-group
618     "c" gnus-group-customize
619     "x" gnus-group-nnimap-expunge
620     "\177" gnus-group-delete-group
621     [delete] gnus-group-delete-group)
622
623   (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
624     "b" gnus-group-brew-soup
625     "w" gnus-soup-save-areas
626     "s" gnus-soup-send-replies
627     "p" gnus-soup-pack-packet
628     "r" nnsoup-pack-replies)
629
630   (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
631     "s" gnus-group-sort-groups
632     "a" gnus-group-sort-groups-by-alphabet
633     "u" gnus-group-sort-groups-by-unread
634     "l" gnus-group-sort-groups-by-level
635     "v" gnus-group-sort-groups-by-score
636     "r" gnus-group-sort-groups-by-rank
637     "m" gnus-group-sort-groups-by-method)
638
639   (gnus-define-keys (gnus-group-sort-selected-map "P" gnus-group-group-map)
640     "s" gnus-group-sort-selected-groups
641     "a" gnus-group-sort-selected-groups-by-alphabet
642     "u" gnus-group-sort-selected-groups-by-unread
643     "l" gnus-group-sort-selected-groups-by-level
644     "v" gnus-group-sort-selected-groups-by-score
645     "r" gnus-group-sort-selected-groups-by-rank
646     "m" gnus-group-sort-selected-groups-by-method)
647
648   (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
649     "k" gnus-group-list-killed
650     "z" gnus-group-list-zombies
651     "s" gnus-group-list-groups
652     "u" gnus-group-list-all-groups
653     "A" gnus-group-list-active
654     "a" gnus-group-apropos
655     "d" gnus-group-description-apropos
656     "m" gnus-group-list-matching
657     "M" gnus-group-list-all-matching
658     "l" gnus-group-list-level
659     "c" gnus-group-list-cached
660     "?" gnus-group-list-dormant)
661
662   (gnus-define-keys (gnus-group-list-limit-map "/" gnus-group-list-map)
663     "k"  gnus-group-list-limit
664     "z"  gnus-group-list-limit
665     "s"  gnus-group-list-limit
666     "u"  gnus-group-list-limit
667     "A"  gnus-group-list-limit
668     "m"  gnus-group-list-limit
669     "M"  gnus-group-list-limit
670     "l"  gnus-group-list-limit
671     "c"  gnus-group-list-limit
672     "?"  gnus-group-list-limit)
673
674   (gnus-define-keys (gnus-group-list-flush-map "f" gnus-group-list-map)
675     "k"  gnus-group-list-flush
676     "z"  gnus-group-list-flush
677     "s"  gnus-group-list-flush
678     "u"  gnus-group-list-flush
679     "A"  gnus-group-list-flush
680     "m"  gnus-group-list-flush
681     "M"  gnus-group-list-flush
682     "l"  gnus-group-list-flush
683     "c"  gnus-group-list-flush
684     "?"  gnus-group-list-flush)
685
686   (gnus-define-keys (gnus-group-list-plus-map "p" gnus-group-list-map)
687     "k"  gnus-group-list-plus
688     "z"  gnus-group-list-plus
689     "s"  gnus-group-list-plus
690     "u"  gnus-group-list-plus
691     "A"  gnus-group-list-plus
692     "m"  gnus-group-list-plus
693     "M"  gnus-group-list-plus
694     "l"  gnus-group-list-plus
695     "c"  gnus-group-list-plus
696     "?"  gnus-group-list-plus)
697
698   (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
699     "f" gnus-score-flush-cache)
700
701   (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
702     "d" gnus-group-describe-group
703     "f" gnus-group-fetch-faq
704     "v" gnus-version)
705
706   (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
707     "l" gnus-group-set-current-level
708     "t" gnus-group-unsubscribe-current-group
709     "s" gnus-group-unsubscribe-group
710     "k" gnus-group-kill-group
711     "y" gnus-group-yank-group
712     "w" gnus-group-kill-region
713     "\C-k" gnus-group-kill-level
714     "z" gnus-group-kill-all-zombies))
715
716 (defun gnus-group-make-menu-bar ()
717   (gnus-turn-off-edit-menu 'group)
718   (unless (boundp 'gnus-group-reading-menu)
719
720     (easy-menu-define
721      gnus-group-reading-menu gnus-group-mode-map ""
722      `("Group"
723        ["Read" gnus-group-read-group (gnus-group-group-name)]
724        ["Select" gnus-group-select-group (gnus-group-group-name)]
725        ["See old articles" (gnus-group-select-group 'all)
726         :keys "C-u SPC" :active (gnus-group-group-name)]
727        ["Catch up" gnus-group-catchup-current :active (gnus-group-group-name)
728         ,@(if (featurep 'xemacs) nil
729             '(:help "Mark unread articles in the current group as read"))]
730        ["Catch up all articles" gnus-group-catchup-current-all
731         (gnus-group-group-name)]
732        ["Check for new articles" gnus-group-get-new-news-this-group
733         :active (gnus-group-group-name)
734         ,@(if (featurep 'xemacs) nil
735             '(:help "Check for new messages in current group"))]
736        ["Toggle subscription" gnus-group-unsubscribe-current-group
737         (gnus-group-group-name)]
738        ["Kill" gnus-group-kill-group :active (gnus-group-group-name)
739         ,@(if (featurep 'xemacs) nil
740             '(:help "Kill (remove) current group"))]
741        ["Yank" gnus-group-yank-group gnus-list-of-killed-groups]
742        ["Describe" gnus-group-describe-group :active (gnus-group-group-name)
743         ,@(if (featurep 'xemacs) nil
744             '(:help "Display description of the current group"))]
745        ["Fetch FAQ" gnus-group-fetch-faq (gnus-group-group-name)]
746        ;; Actually one should check, if any of the marked groups gives t for
747        ;; (gnus-check-backend-function 'request-expire-articles ...)
748        ["Expire articles" gnus-group-expire-articles
749         (or (and (gnus-group-group-name)
750                  (gnus-check-backend-function
751                   'request-expire-articles
752                   (gnus-group-group-name))) gnus-group-marked)]
753        ["Set group level" gnus-group-set-current-level
754         (gnus-group-group-name)]
755        ["Select quick" gnus-group-quick-select-group (gnus-group-group-name)]
756        ["Customize" gnus-group-customize (gnus-group-group-name)]
757        ("Edit"
758         ["Parameters" gnus-group-edit-group-parameters
759          (gnus-group-group-name)]
760         ["Select method" gnus-group-edit-group-method
761          (gnus-group-group-name)]
762         ["Info" gnus-group-edit-group (gnus-group-group-name)]
763         ["Local kill file" gnus-group-edit-local-kill (gnus-group-group-name)]
764         ["Global kill file" gnus-group-edit-global-kill t])))
765
766     (easy-menu-define
767      gnus-group-group-menu gnus-group-mode-map ""
768      '("Groups"
769        ("Listing"
770         ["List unread subscribed groups" gnus-group-list-groups t]
771         ["List (un)subscribed groups" gnus-group-list-all-groups t]
772         ["List killed groups" gnus-group-list-killed gnus-killed-list]
773         ["List zombie groups" gnus-group-list-zombies gnus-zombie-list]
774         ["List level..." gnus-group-list-level t]
775         ["Describe all groups" gnus-group-describe-all-groups t]
776         ["Group apropos..." gnus-group-apropos t]
777         ["Group and description apropos..." gnus-group-description-apropos t]
778         ["List groups matching..." gnus-group-list-matching t]
779         ["List all groups matching..." gnus-group-list-all-matching t]
780         ["List active file" gnus-group-list-active t]
781         ["List groups with cached" gnus-group-list-cached t]
782         ["List groups with dormant" gnus-group-list-dormant t])
783        ("Sort"
784         ["Default sort" gnus-group-sort-groups t]
785         ["Sort by method" gnus-group-sort-groups-by-method t]
786         ["Sort by rank" gnus-group-sort-groups-by-rank t]
787         ["Sort by score" gnus-group-sort-groups-by-score t]
788         ["Sort by level" gnus-group-sort-groups-by-level t]
789         ["Sort by unread" gnus-group-sort-groups-by-unread t]
790         ["Sort by name" gnus-group-sort-groups-by-alphabet t])
791        ("Sort process/prefixed"
792         ["Default sort" gnus-group-sort-selected-groups
793          (or (not (boundp 'gnus-topic-mode)) (not gnus-topic-mode))]
794         ["Sort by method" gnus-group-sort-selected-groups-by-method
795          (or (not (boundp 'gnus-topic-mode)) (not gnus-topic-mode))]
796         ["Sort by rank" gnus-group-sort-selected-groups-by-rank
797          (or (not (boundp 'gnus-topic-mode)) (not gnus-topic-mode))]
798         ["Sort by score" gnus-group-sort-selected-groups-by-score
799          (or (not (boundp 'gnus-topic-mode)) (not gnus-topic-mode))]
800         ["Sort by level" gnus-group-sort-selected-groups-by-level
801          (or (not (boundp 'gnus-topic-mode)) (not gnus-topic-mode))]
802         ["Sort by unread" gnus-group-sort-selected-groups-by-unread
803          (or (not (boundp 'gnus-topic-mode)) (not gnus-topic-mode))]
804         ["Sort by name" gnus-group-sort-selected-groups-by-alphabet
805          (or (not (boundp 'gnus-topic-mode)) (not gnus-topic-mode))])
806        ("Mark"
807         ["Mark group" gnus-group-mark-group
808          (and (gnus-group-group-name)
809               (not (memq (gnus-group-group-name) gnus-group-marked)))]
810         ["Unmark group" gnus-group-unmark-group
811          (and (gnus-group-group-name)
812               (memq (gnus-group-group-name) gnus-group-marked))]
813         ["Unmark all" gnus-group-unmark-all-groups gnus-group-marked]
814         ["Mark regexp..." gnus-group-mark-regexp t]
815         ["Mark region" gnus-group-mark-region t]
816         ["Mark buffer" gnus-group-mark-buffer t]
817         ["Execute command" gnus-group-universal-argument
818          (or gnus-group-marked (gnus-group-group-name))])
819        ("Subscribe"
820         ["Subscribe to a group" gnus-group-unsubscribe-group t]
821         ["Kill all newsgroups in region" gnus-group-kill-region t]
822         ["Kill all zombie groups" gnus-group-kill-all-zombies
823          gnus-zombie-list]
824         ["Kill all groups on level..." gnus-group-kill-level t])
825        ("Foreign groups"
826         ["Make a foreign group" gnus-group-make-group t]
827         ["Add a directory group" gnus-group-make-directory-group t]
828         ["Add the help group" gnus-group-make-help-group t]
829         ["Add the archive group" gnus-group-make-archive-group t]
830         ["Make a doc group" gnus-group-make-doc-group t]
831         ["Make a web group" gnus-group-make-web-group t]
832         ["Make a kiboze group" gnus-group-make-kiboze-group t]
833         ["Make a virtual group" gnus-group-make-empty-virtual t]
834         ["Add a group to a virtual" gnus-group-add-to-virtual t]
835         ["Rename group" gnus-group-rename-group
836          (gnus-check-backend-function
837           'request-rename-group (gnus-group-group-name))]
838         ["Delete group" gnus-group-delete-group
839          (gnus-check-backend-function
840           'request-delete-group (gnus-group-group-name))])
841        ("Move"
842         ["Next" gnus-group-next-group t]
843         ["Previous" gnus-group-prev-group t]
844         ["Next unread" gnus-group-next-unread-group t]
845         ["Previous unread" gnus-group-prev-unread-group t]
846         ["Next unread same level" gnus-group-next-unread-group-same-level t]
847         ["Previous unread same level"
848          gnus-group-prev-unread-group-same-level t]
849         ["Jump to group" gnus-group-jump-to-group t]
850         ["First unread group" gnus-group-first-unread-group t]
851         ["Best unread group" gnus-group-best-unread-group t])
852        ["Delete bogus groups" gnus-group-check-bogus-groups t]
853        ["Find new newsgroups" gnus-group-find-new-groups t]
854        ["Transpose" gnus-group-transpose-groups
855         (gnus-group-group-name)]
856        ["Read a directory as a group..." gnus-group-enter-directory t]))
857
858     (easy-menu-define
859      gnus-group-misc-menu gnus-group-mode-map ""
860      `("Gnus"
861        ("SOUP"
862         ["Pack replies" nnsoup-pack-replies (fboundp 'nnsoup-request-group)]
863         ["Send replies" gnus-soup-send-replies
864          (fboundp 'gnus-soup-pack-packet)]
865         ["Pack packet" gnus-soup-pack-packet (fboundp 'gnus-soup-pack-packet)]
866         ["Save areas" gnus-soup-save-areas (fboundp 'gnus-soup-pack-packet)]
867         ["Brew SOUP" gnus-group-brew-soup (fboundp 'gnus-soup-pack-packet)])
868        ["Send a mail" gnus-group-mail t]
869        ["Post an article..." gnus-group-post-news t]
870        ["Check for new news" gnus-group-get-new-news
871         ,@(if (featurep 'xemacs) '(t)
872             '(:help "Get newly arrived articles"))
873         ]
874        ["Activate all groups" gnus-activate-all-groups t]
875        ["Restart Gnus" gnus-group-restart t]
876        ["Read init file" gnus-group-read-init-file t]
877        ["Browse foreign server" gnus-group-browse-foreign-server t]
878        ["Enter server buffer" gnus-group-enter-server-mode t]
879        ["Expire all expirable articles" gnus-group-expire-all-groups t]
880        ["Generate any kiboze groups" nnkiboze-generate-groups t]
881        ["Gnus version" gnus-version t]
882        ["Save .newsrc files" gnus-group-save-newsrc t]
883        ["Suspend Gnus" gnus-group-suspend t]
884        ["Clear dribble buffer" gnus-group-clear-dribble t]
885        ["Read manual" gnus-info-find-node t]
886        ["Flush score cache" gnus-score-flush-cache t]
887        ["Toggle topics" gnus-topic-mode t]
888        ["Send a bug report" gnus-bug t]
889        ["Exit from Gnus" gnus-group-exit
890         ,@(if (featurep 'xemacs) '(t)
891             '(:help "Quit reading news"))]
892        ["Exit without saving" gnus-group-quit t]))
893
894     (gnus-run-hooks 'gnus-group-menu-hook)))
895
896 (defvar gnus-group-toolbar-map nil)
897
898 ;; Emacs 21 tool bar.  Should be no-op otherwise.
899 (defun gnus-group-make-tool-bar ()
900   (if (and (fboundp 'tool-bar-add-item-from-menu)
901            (default-value 'tool-bar-mode)
902            (not gnus-group-toolbar-map))
903       (setq gnus-group-toolbar-map
904             (let ((tool-bar-map (make-sparse-keymap))
905                   (load-path (mm-image-load-path)))
906               (tool-bar-add-item-from-menu
907                'gnus-group-get-new-news "get-news" gnus-group-mode-map)
908               (tool-bar-add-item-from-menu
909                'gnus-group-get-new-news-this-group "gnntg" gnus-group-mode-map)
910               (tool-bar-add-item-from-menu
911                'gnus-group-catchup-current "catchup" gnus-group-mode-map)
912               (tool-bar-add-item-from-menu
913                'gnus-group-describe-group "describe-group" gnus-group-mode-map)
914               (tool-bar-add-item "subscribe" 'gnus-group-subscribe 'subscribe
915                                  :help "Subscribe to the current group")
916               (tool-bar-add-item "unsubscribe" 'gnus-group-unsubscribe
917                                  'unsubscribe
918                                  :help "Unsubscribe from the current group")
919               (tool-bar-add-item-from-menu
920                'gnus-group-exit "exit-gnus" gnus-group-mode-map)
921               tool-bar-map)))
922   (if gnus-group-toolbar-map
923       (set (make-local-variable 'tool-bar-map) gnus-group-toolbar-map)))
924
925 (defun gnus-group-mode ()
926   "Major mode for reading news.
927
928 All normal editing commands are switched off.
929 \\<gnus-group-mode-map>
930 The group buffer lists (some of) the groups available.  For instance,
931 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
932 lists all zombie groups.
933
934 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
935 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
936
937 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
938
939 The following commands are available:
940
941 \\{gnus-group-mode-map}"
942   (interactive)
943   (kill-all-local-variables)
944   (when (gnus-visual-p 'group-menu 'menu)
945     (gnus-group-make-menu-bar)
946     (gnus-group-make-tool-bar))
947   (gnus-simplify-mode-line)
948   (setq major-mode 'gnus-group-mode)
949   (setq mode-name "Group")
950   (gnus-group-set-mode-line)
951   (setq mode-line-process nil)
952   (use-local-map gnus-group-mode-map)
953   (buffer-disable-undo)
954   (setq truncate-lines t)
955   (setq buffer-read-only t)
956   (gnus-set-default-directory)
957   (gnus-update-format-specifications nil 'group 'group-mode)
958   (gnus-update-group-mark-positions)
959   (when gnus-use-undo
960     (gnus-undo-mode 1))
961   (when gnus-slave
962     (gnus-slave-mode))
963   (gnus-run-hooks 'gnus-group-mode-hook))
964
965 (defun gnus-update-group-mark-positions ()
966   (save-excursion
967     (let ((gnus-process-mark ?\200)
968           (gnus-group-update-hook nil)
969           (gnus-group-marked '("dummy.group"))
970           (gnus-active-hashtb (make-vector 10 0))
971           (topic ""))
972       (gnus-set-active "dummy.group" '(0 . 0))
973       (gnus-set-work-buffer)
974       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
975       (goto-char (point-min))
976       (setq gnus-group-mark-positions
977             (list (cons 'process (and (search-forward "\200" nil t)
978                                       (- (point) 2))))))))
979
980 (defun gnus-mouse-pick-group (e)
981   "Enter the group under the mouse pointer."
982   (interactive "e")
983   (mouse-set-point e)
984   (gnus-group-read-group nil))
985
986 ;; Look at LEVEL and find out what the level is really supposed to be.
987 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
988 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
989 (defun gnus-group-default-level (&optional level number-or-nil)
990   (cond
991    (gnus-group-use-permanent-levels
992     (or (setq gnus-group-use-permanent-levels
993               (or level (if (numberp gnus-group-use-permanent-levels)
994                             gnus-group-use-permanent-levels
995                           (or gnus-group-default-list-level
996                               gnus-level-subscribed))))
997         gnus-group-default-list-level gnus-level-subscribed))
998    (number-or-nil
999     level)
1000    (t
1001     (or level gnus-group-default-list-level gnus-level-subscribed))))
1002
1003 (defun gnus-group-setup-buffer ()
1004   (set-buffer (gnus-get-buffer-create gnus-group-buffer))
1005   (unless (eq major-mode 'gnus-group-mode)
1006     (gnus-group-mode)
1007     (when gnus-carpal
1008       (gnus-carpal-setup-buffer 'group))))
1009
1010 (defsubst gnus-group-name-charset (method group)
1011   (if (null method)
1012       (setq method (gnus-find-method-for-group group)))
1013   (let ((item (assoc method gnus-group-name-charset-method-alist))
1014         (alist gnus-group-name-charset-group-alist)
1015         result)
1016     (if item
1017         (cdr item)
1018       (while (setq item (pop alist))
1019         (if (string-match (car item) group)
1020             (setq alist nil
1021                   result (cdr item))))
1022       result)))
1023
1024 (defsubst gnus-group-name-decode (string charset)
1025   (if (and string charset (featurep 'mule))
1026       (decode-coding-string string charset)
1027     string))
1028
1029 (defun gnus-group-decoded-name (string)
1030   (let ((charset (gnus-group-name-charset nil string)))
1031     (gnus-group-name-decode string charset)))
1032
1033 (defun gnus-group-list-groups (&optional level unread lowest)
1034   "List newsgroups with level LEVEL or lower that have unread articles.
1035 Default is all subscribed groups.
1036 If argument UNREAD is non-nil, groups with no unread articles are also
1037 listed.
1038
1039 Also see the `gnus-group-use-permanent-levels' variable."
1040   (interactive
1041    (list (if current-prefix-arg
1042              (prefix-numeric-value current-prefix-arg)
1043            (or
1044             (gnus-group-default-level nil t)
1045             gnus-group-default-list-level
1046             gnus-level-subscribed))))
1047   (unless level
1048     (setq level (car gnus-group-list-mode)
1049           unread (cdr gnus-group-list-mode)))
1050   (setq level (gnus-group-default-level level))
1051   (gnus-group-setup-buffer)
1052   (gnus-update-format-specifications nil 'group 'group-mode)
1053   (let ((case-fold-search nil)
1054         (props (text-properties-at (gnus-point-at-bol)))
1055         (empty (= (point-min) (point-max)))
1056         (group (gnus-group-group-name))
1057         number)
1058     (set-buffer gnus-group-buffer)
1059     (setq number (funcall gnus-group-prepare-function level unread lowest))
1060     (when (or (and (numberp number)
1061                    (zerop number))
1062               (zerop (buffer-size)))
1063       ;; No groups in the buffer.
1064       (gnus-message 5 gnus-no-groups-message))
1065     ;; We have some groups displayed.
1066     (goto-char (point-max))
1067     (when (or (not gnus-group-goto-next-group-function)
1068               (not (funcall gnus-group-goto-next-group-function
1069                             group props)))
1070       (cond
1071        (empty
1072         (goto-char (point-min)))
1073        ((not group)
1074         ;; Go to the first group with unread articles.
1075         (gnus-group-search-forward t))
1076        (t
1077         ;; Find the right group to put point on.  If the current group
1078         ;; has disappeared in the new listing, try to find the next
1079         ;; one.  If no next one can be found, just leave point at the
1080         ;; first newsgroup in the buffer.
1081         (when (not (gnus-goto-char
1082                     (text-property-any
1083                      (point-min) (point-max)
1084                      'gnus-group (gnus-intern-safe
1085                                   group gnus-active-hashtb))))
1086           (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
1087             (while (and newsrc
1088                         (not (gnus-goto-char
1089                               (text-property-any
1090                                (point-min) (point-max) 'gnus-group
1091                                (gnus-intern-safe
1092                                 (caar newsrc) gnus-active-hashtb)))))
1093               (setq newsrc (cdr newsrc)))
1094             (unless newsrc
1095               (goto-char (point-max))
1096               (forward-line -1)))))))
1097     ;; Adjust cursor point.
1098     (gnus-group-position-point)))
1099
1100 (defun gnus-group-list-level (level &optional all)
1101   "List groups on LEVEL.
1102 If ALL (the prefix), also list groups that have no unread articles."
1103   (interactive "nList groups on level: \nP")
1104   (gnus-group-list-groups level all level))
1105
1106 (defun gnus-group-prepare-logic (group test)
1107   (or (and gnus-group-listed-groups
1108            (null gnus-group-list-option)
1109            (member group gnus-group-listed-groups))
1110       (cond
1111        ((null gnus-group-listed-groups) test)
1112        ((null gnus-group-list-option) test)
1113        (t (and (member group gnus-group-listed-groups)
1114                (if (eq gnus-group-list-option 'flush)
1115                    (not test)
1116                  test))))))
1117
1118 (defun gnus-group-prepare-flat (level &optional predicate lowest regexp)
1119   "List all newsgroups with unread articles of level LEVEL or lower.
1120 If PREDICATE is a function, list groups that the function returns non-nil;
1121 if it is t, list groups that have no unread articles.
1122 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
1123 If REGEXP is a function, list dead groups that the function returns non-nil;
1124 if it is a string, only list groups matching REGEXP."
1125   (set-buffer gnus-group-buffer)
1126   (let ((buffer-read-only nil)
1127         (newsrc (cdr gnus-newsrc-alist))
1128         (lowest (or lowest 1))
1129         (not-in-list (and gnus-group-listed-groups
1130                           (copy-sequence gnus-group-listed-groups)))
1131         info clevel unread group params)
1132     (erase-buffer)
1133     (when (or (< lowest gnus-level-zombie)
1134               gnus-group-listed-groups)
1135       ;; List living groups.
1136       (while newsrc
1137         (setq info (car newsrc)
1138               group (gnus-info-group info)
1139               params (gnus-info-params info)
1140               newsrc (cdr newsrc)
1141               unread (car (gnus-gethash group gnus-newsrc-hashtb)))
1142         (if not-in-list
1143             (setq not-in-list (delete group not-in-list)))
1144         (and
1145          (gnus-group-prepare-logic
1146           group
1147           (and unread           ; This group might be unchecked
1148                (or (not (stringp regexp))
1149                    (string-match regexp group))
1150                (<= (setq clevel (gnus-info-level info)) level)
1151                (>= clevel lowest)
1152                (cond
1153                 ((functionp predicate)
1154                  (funcall predicate info))
1155                 (predicate t)           ; We list all groups?
1156                 (t
1157                  (or
1158                   (if (eq unread t)     ; Unactivated?
1159                       gnus-group-list-inactive-groups
1160                                         ; We list unactivated
1161                     (> unread 0))
1162                                         ; We list groups with unread articles
1163                   (and gnus-list-groups-with-ticked-articles
1164                        (cdr (assq 'tick (gnus-info-marks info))))
1165                                         ; And groups with tickeds
1166                   ;; Check for permanent visibility.
1167                   (and gnus-permanently-visible-groups
1168                        (string-match gnus-permanently-visible-groups group))
1169                   (memq 'visible params)
1170                   (cdr (assq 'visible params)))))))
1171          (gnus-group-insert-group-line
1172           group (gnus-info-level info)
1173           (gnus-info-marks info) unread (gnus-info-method info)))))
1174
1175     ;; List dead groups.
1176     (if (or gnus-group-listed-groups
1177             (and (>= level gnus-level-zombie)
1178                  (<= lowest gnus-level-zombie)))
1179         (gnus-group-prepare-flat-list-dead
1180          (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
1181          gnus-level-zombie ?Z
1182          regexp))
1183     (if not-in-list
1184         (dolist (group gnus-zombie-list)
1185           (setq not-in-list (delete group not-in-list))))
1186     (if (or gnus-group-listed-groups
1187             (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)))
1188         (gnus-group-prepare-flat-list-dead
1189          (gnus-union
1190           not-in-list
1191           (setq gnus-killed-list (sort gnus-killed-list 'string<)))
1192          gnus-level-killed ?K regexp))
1193
1194     (gnus-group-set-mode-line)
1195     (setq gnus-group-list-mode (cons level predicate))
1196     (gnus-run-hooks 'gnus-group-prepare-hook)
1197     t))
1198
1199 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
1200   ;; List zombies and killed lists somewhat faster, which was
1201   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
1202   ;; this by ignoring the group format specification altogether.
1203   (let (group)
1204     (if (> (length groups) gnus-group-listing-limit)
1205         (while groups
1206           (setq group (pop groups))
1207           (when (gnus-group-prepare-logic
1208                  group
1209                  (or (not regexp)
1210                      (and (stringp regexp) (string-match regexp group))
1211                      (and (functionp regexp) (funcall regexp group))))
1212             (gnus-add-text-properties
1213              (point) (prog1 (1+ (point))
1214                        (insert " " mark "     *: "
1215                                (gnus-group-decoded-name group)
1216                                "\n"))
1217              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
1218                    'gnus-unread t
1219                    'gnus-level level))))
1220       (while groups
1221         (setq group (pop groups))
1222         (when (gnus-group-prepare-logic
1223                group
1224                (or (not regexp)
1225                    (and (stringp regexp) (string-match regexp group))
1226                    (and (functionp regexp) (funcall regexp group))))
1227           (gnus-group-insert-group-line
1228            group level nil
1229            (let ((active (gnus-active group)))
1230              (if active
1231                  (if (zerop (cdr active))
1232                      0
1233                    (- (1+ (cdr active)) (car active)))
1234                nil))
1235            (gnus-method-simplify (gnus-find-method-for-group group))))))))
1236
1237 (defun gnus-group-update-group-line ()
1238   "Update the current line in the group buffer."
1239   (let* ((buffer-read-only nil)
1240          (group (gnus-group-group-name))
1241          (entry (and group (gnus-gethash group gnus-newsrc-hashtb)))
1242          gnus-group-indentation)
1243     (when group
1244       (and entry
1245            (not (gnus-ephemeral-group-p group))
1246            (gnus-dribble-enter
1247             (concat "(gnus-group-set-info '"
1248                     (gnus-prin1-to-string (nth 2 entry))
1249                     ")")))
1250       (setq gnus-group-indentation (gnus-group-group-indentation))
1251       (gnus-delete-line)
1252       (gnus-group-insert-group-line-info group)
1253       (forward-line -1)
1254       (gnus-group-position-point))))
1255
1256 (defun gnus-group-insert-group-line-info (group)
1257   "Insert GROUP on the current line."
1258   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
1259         (gnus-group-indentation (gnus-group-group-indentation))
1260         active info)
1261     (if entry
1262         (progn
1263           ;; (Un)subscribed group.
1264           (setq info (nth 2 entry))
1265           (gnus-group-insert-group-line
1266            group (gnus-info-level info) (gnus-info-marks info)
1267            (or (car entry) t) (gnus-info-method info)))
1268       ;; This group is dead.
1269       (gnus-group-insert-group-line
1270        group
1271        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
1272        nil
1273        (if (setq active (gnus-active group))
1274            (if (zerop (cdr active))
1275                0
1276              (- (1+ (cdr active)) (car active)))
1277          nil)
1278        (gnus-method-simplify (gnus-find-method-for-group group))))))
1279
1280 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level
1281                                                     gnus-tmp-marked number
1282                                                     gnus-tmp-method)
1283   "Insert a group line in the group buffer."
1284   (let* ((gnus-tmp-method
1285           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
1286          (group-name-charset (gnus-group-name-charset gnus-tmp-method
1287                                                       gnus-tmp-group))
1288          (gnus-tmp-active (gnus-active gnus-tmp-group))
1289          (gnus-tmp-number-total
1290           (if gnus-tmp-active
1291               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
1292             0))
1293          (gnus-tmp-number-of-unread
1294           (if (numberp number) (int-to-string (max 0 number))
1295             "*"))
1296          (gnus-tmp-number-of-read
1297           (if (numberp number)
1298               (int-to-string (max 0 (- gnus-tmp-number-total number)))
1299             "*"))
1300          (gnus-tmp-subscribed
1301           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
1302                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
1303                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
1304                 (t ?K)))
1305          (gnus-tmp-qualified-group
1306           (gnus-group-name-decode (gnus-group-real-name gnus-tmp-group)
1307                                   group-name-charset))
1308          (gnus-tmp-newsgroup-description
1309           (if gnus-description-hashtb
1310               (or (gnus-group-name-decode
1311                    (gnus-gethash gnus-tmp-group gnus-description-hashtb)
1312                    group-name-charset) "")
1313             ""))
1314          (gnus-tmp-moderated
1315           (if (and gnus-moderated-hashtb
1316                    (gnus-gethash gnus-tmp-group gnus-moderated-hashtb))
1317               ?m ? ))
1318          (gnus-tmp-moderated-string
1319           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
1320          (gnus-tmp-group-icon "==&&==")
1321          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
1322          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
1323          (gnus-tmp-news-method-string
1324           (if gnus-tmp-method
1325               (format "(%s:%s)" (car gnus-tmp-method)
1326                       (cadr gnus-tmp-method)) ""))
1327          (gnus-tmp-marked-mark
1328           (if (and (numberp number)
1329                    (zerop number)
1330                    (cdr (assq 'tick gnus-tmp-marked)))
1331               ?* ? ))
1332          (gnus-tmp-process-marked
1333           (if (member gnus-tmp-group gnus-group-marked)
1334               gnus-process-mark ? ))
1335          (gnus-tmp-grouplens
1336           (or (and gnus-use-grouplens
1337                    (bbb-grouplens-group-p gnus-tmp-group))
1338               ""))
1339          (buffer-read-only nil)
1340          header gnus-tmp-header)        ; passed as parameter to user-funcs.
1341     (beginning-of-line)
1342     (gnus-add-text-properties
1343      (point)
1344      (prog1 (1+ (point))
1345        ;; Insert the text.
1346        (eval gnus-group-line-format-spec))
1347      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
1348                   gnus-unread ,(if (numberp number)
1349                                    (string-to-int gnus-tmp-number-of-unread)
1350                                  t)
1351                   gnus-marked ,gnus-tmp-marked-mark
1352                   gnus-indentation ,gnus-group-indentation
1353                   gnus-level ,gnus-tmp-level))
1354     (forward-line -1)
1355     (when (inline (gnus-visual-p 'group-highlight 'highlight))
1356       (gnus-run-hooks 'gnus-group-update-hook))
1357     (forward-line)
1358     ;; Allow XEmacs to remove front-sticky text properties.
1359     (gnus-group-remove-excess-properties)))
1360
1361 (defun gnus-group-highlight-line ()
1362   "Highlight the current line according to `gnus-group-highlight'."
1363   (let* ((list gnus-group-highlight)
1364          (p (point))
1365          (end (progn (end-of-line) (point)))
1366          ;; now find out where the line starts and leave point there.
1367          (beg (progn (beginning-of-line) (point)))
1368          (group (gnus-group-group-name))
1369          (entry (gnus-group-entry group))
1370          (unread (if (numberp (car entry)) (car entry) 0))
1371          (active (gnus-active group))
1372          (total (if active (1+ (- (cdr active) (car active))) 0))
1373          (info (nth 2 entry))
1374          (method (gnus-server-get-method group (gnus-info-method info)))
1375          (marked (gnus-info-marks info))
1376          (mailp (apply 'append
1377                        (mapcar
1378                         (lambda (x)
1379                           (memq x (assoc (symbol-name
1380                                           (car (or method gnus-select-method)))
1381                                          gnus-valid-select-methods)))
1382                         '(mail post-mail))))
1383          (level (or (gnus-info-level info) gnus-level-killed))
1384          (score (or (gnus-info-score info) 0))
1385          (ticked (gnus-range-length (cdr (assq 'tick marked))))
1386          (group-age (gnus-group-timestamp-delta group))
1387          (inhibit-read-only t))
1388     ;; Eval the cars of the lists until we find a match.
1389     (while (and list
1390                 (not (eval (caar list))))
1391       (setq list (cdr list)))
1392     (let ((face (cdar list)))
1393       (unless (eq face (get-text-property beg 'face))
1394         (gnus-put-text-property-excluding-characters-with-faces
1395          beg end 'face
1396          (setq face (if (boundp face) (symbol-value face) face)))
1397         (gnus-extent-start-open beg)))
1398     (goto-char p)))
1399
1400 (defun gnus-group-update-group (group &optional visible-only)
1401   "Update all lines where GROUP appear.
1402 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
1403 already."
1404   ;; Can't use `save-excursion' here, so we do it manually.
1405   (let ((buf (current-buffer))
1406         mark)
1407     (set-buffer gnus-group-buffer)
1408     (setq mark (point-marker))
1409     ;; The buffer may be narrowed.
1410     (save-restriction
1411       (widen)
1412       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
1413             (loc (point-min))
1414             found buffer-read-only)
1415         ;; Enter the current status into the dribble buffer.
1416         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
1417           (when (and entry
1418                      (not (gnus-ephemeral-group-p group)))
1419             (gnus-dribble-enter
1420              (concat "(gnus-group-set-info '"
1421                      (gnus-prin1-to-string (nth 2 entry))
1422                      ")"))))
1423         ;; Find all group instances.  If topics are in use, each group
1424         ;; may be listed in more than once.
1425         (while (setq loc (text-property-any
1426                           loc (point-max) 'gnus-group ident))
1427           (setq found t)
1428           (goto-char loc)
1429           (let ((gnus-group-indentation (gnus-group-group-indentation)))
1430             (gnus-delete-line)
1431             (gnus-group-insert-group-line-info group)
1432             (save-excursion
1433               (forward-line -1)
1434               (gnus-run-hooks 'gnus-group-update-group-hook)))
1435           (setq loc (1+ loc)))
1436         (unless (or found visible-only)
1437           ;; No such line in the buffer, find out where it's supposed to
1438           ;; go, and insert it there (or at the end of the buffer).
1439           (if gnus-goto-missing-group-function
1440               (funcall gnus-goto-missing-group-function group)
1441             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
1442               (while (and entry (car entry)
1443                           (not
1444                            (gnus-goto-char
1445                             (text-property-any
1446                              (point-min) (point-max)
1447                              'gnus-group (gnus-intern-safe
1448                                           (caar entry) gnus-active-hashtb)))))
1449                 (setq entry (cdr entry)))
1450               (or entry (goto-char (point-max)))))
1451           ;; Finally insert the line.
1452           (let ((gnus-group-indentation (gnus-group-group-indentation)))
1453             (gnus-group-insert-group-line-info group)
1454             (save-excursion
1455               (forward-line -1)
1456               (gnus-run-hooks 'gnus-group-update-group-hook))))
1457         (when gnus-group-update-group-function
1458           (funcall gnus-group-update-group-function group))
1459         (gnus-group-set-mode-line)))
1460     (goto-char mark)
1461     (set-marker mark nil)
1462     (set-buffer buf)))
1463
1464 (defun gnus-group-set-mode-line ()
1465   "Update the mode line in the group buffer."
1466   (when (memq 'group gnus-updated-mode-lines)
1467     ;; Yes, we want to keep this mode line updated.
1468     (save-excursion
1469       (set-buffer gnus-group-buffer)
1470       (let* ((gformat (or gnus-group-mode-line-format-spec
1471                           (gnus-set-format 'group-mode)))
1472              (gnus-tmp-news-server (cadr gnus-select-method))
1473              (gnus-tmp-news-method (car gnus-select-method))
1474              (gnus-tmp-colon (if (equal gnus-tmp-news-server "") "" ":"))
1475              (max-len 60)
1476              gnus-tmp-header            ;Dummy binding for user-defined formats
1477              ;; Get the resulting string.
1478              (modified
1479               (and gnus-dribble-buffer
1480                    (buffer-name gnus-dribble-buffer)
1481                    (buffer-modified-p gnus-dribble-buffer)
1482                    (save-excursion
1483                      (set-buffer gnus-dribble-buffer)
1484                      (not (zerop (buffer-size))))))
1485              (mode-string (eval gformat)))
1486         ;; Say whether the dribble buffer has been modified.
1487         (setq mode-line-modified
1488               (if modified (car gnus-mode-line-modified)
1489                 (cdr gnus-mode-line-modified)))
1490         ;; If the line is too long, we chop it off.
1491         (when (> (length mode-string) max-len)
1492           (setq mode-string (substring mode-string 0 (- max-len 4))))
1493         (prog1
1494             (setq mode-line-buffer-identification
1495                   (gnus-mode-line-buffer-identification
1496                    (list mode-string)))
1497           (set-buffer-modified-p modified))))))
1498
1499 (defun gnus-group-group-name ()
1500   "Get the name of the newsgroup on the current line."
1501   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
1502     (when group
1503       (symbol-name group))))
1504
1505 (defun gnus-group-group-level ()
1506   "Get the level of the newsgroup on the current line."
1507   (get-text-property (gnus-point-at-bol) 'gnus-level))
1508
1509 (defun gnus-group-group-indentation ()
1510   "Get the indentation of the newsgroup on the current line."
1511   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
1512       (and gnus-group-indentation-function
1513            (funcall gnus-group-indentation-function))
1514       ""))
1515
1516 (defun gnus-group-group-unread ()
1517   "Get the number of unread articles of the newsgroup on the current line."
1518   (get-text-property (gnus-point-at-bol) 'gnus-unread))
1519
1520 (defun gnus-group-new-mail (group)
1521   (if (nnmail-new-mail-p (gnus-group-real-name group))
1522       gnus-new-mail-mark
1523     ? ))
1524
1525 (defun gnus-group-level (group)
1526   "Return the estimated level of GROUP."
1527   (or (gnus-info-level (gnus-get-info group))
1528       (and (member group gnus-zombie-list) gnus-level-zombie)
1529       gnus-level-killed))
1530
1531 (defun gnus-group-search-forward (&optional backward all level first-too)
1532   "Find the next newsgroup with unread articles.
1533 If BACKWARD is non-nil, find the previous newsgroup instead.
1534 If ALL is non-nil, just find any newsgroup.
1535 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
1536 group exists.
1537 If FIRST-TOO, the current line is also eligible as a target."
1538   (let ((way (if backward -1 1))
1539         (low gnus-level-killed)
1540         (beg (point))
1541         pos found lev)
1542     (if (and backward (progn (beginning-of-line)) (bobp))
1543         nil
1544       (unless first-too
1545         (forward-line way))
1546       (while (and
1547               (not (eobp))
1548               (not (setq
1549                     found
1550                     (and
1551                      (get-text-property (point) 'gnus-group)
1552                      (or all
1553                          (and
1554                           (let ((unread
1555                                  (get-text-property (point) 'gnus-unread)))
1556                             (and (numberp unread) (> unread 0)))
1557                           (setq lev (get-text-property (point)
1558                                                        'gnus-level))
1559                           (<= lev gnus-level-subscribed)))
1560                      (or (not level)
1561                          (and (setq lev (get-text-property (point)
1562                                                            'gnus-level))
1563                               (or (= lev level)
1564                                   (and (< lev low)
1565                                        (< level lev)
1566                                        (progn
1567                                          (setq low lev)
1568                                          (setq pos (point))
1569                                          nil))))))))
1570               (zerop (forward-line way)))))
1571     (if found
1572         (progn (gnus-group-position-point) t)
1573       (goto-char (or pos beg))
1574       (and pos t))))
1575
1576 ;;; Gnus group mode commands
1577
1578 ;; Group marking.
1579
1580 (defun gnus-group-mark-line-p ()
1581   (save-excursion
1582     (beginning-of-line)
1583     (forward-char (or (cdr (assq 'process gnus-group-mark-positions)) 2))
1584     (eq (char-after) gnus-process-mark)))
1585
1586 (defun gnus-group-mark-group (n &optional unmark no-advance)
1587   "Mark the current group."
1588   (interactive "p")
1589   (let ((buffer-read-only nil)
1590         group)
1591     (while (and (> n 0)
1592                 (not (eobp)))
1593       (when (setq group (gnus-group-group-name))
1594         ;; Go to the mark position.
1595         (beginning-of-line)
1596         (forward-char (or (cdr (assq 'process gnus-group-mark-positions)) 2))
1597         (subst-char-in-region
1598          (point) (1+ (point)) (char-after)
1599          (if unmark
1600              (progn
1601                (setq gnus-group-marked (delete group gnus-group-marked))
1602                ? )
1603            (setq gnus-group-marked
1604                  (cons group (delete group gnus-group-marked)))
1605            gnus-process-mark)))
1606       (unless no-advance
1607         (gnus-group-next-group 1))
1608       (decf n))
1609     (gnus-summary-position-point)
1610     n))
1611
1612 (defun gnus-group-unmark-group (n)
1613   "Remove the mark from the current group."
1614   (interactive "p")
1615   (gnus-group-mark-group n 'unmark)
1616   (gnus-group-position-point))
1617
1618 (defun gnus-group-unmark-all-groups ()
1619   "Unmark all groups."
1620   (interactive)
1621   (let ((groups gnus-group-marked))
1622     (save-excursion
1623       (while groups
1624         (gnus-group-remove-mark (pop groups)))))
1625   (gnus-group-position-point))
1626
1627 (defun gnus-group-mark-region (unmark beg end)
1628   "Mark all groups between point and mark.
1629 If UNMARK, remove the mark instead."
1630   (interactive "P\nr")
1631   (let ((num (count-lines beg end)))
1632     (save-excursion
1633       (goto-char beg)
1634       (- num (gnus-group-mark-group num unmark)))))
1635
1636 (defun gnus-group-mark-buffer (&optional unmark)
1637   "Mark all groups in the buffer.
1638 If UNMARK, remove the mark instead."
1639   (interactive "P")
1640   (gnus-group-mark-region unmark (point-min) (point-max)))
1641
1642 (defun gnus-group-mark-regexp (regexp)
1643   "Mark all groups that match some regexp."
1644   (interactive "sMark (regexp): ")
1645   (let ((alist (cdr gnus-newsrc-alist))
1646         group)
1647     (while alist
1648       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
1649         (gnus-group-set-mark group))))
1650   (gnus-group-position-point))
1651
1652 (defun gnus-group-remove-mark (group &optional test-marked)
1653   "Remove the process mark from GROUP and move point there.
1654 Return nil if the group isn't displayed."
1655   (if (gnus-group-goto-group group nil test-marked)
1656       (save-excursion
1657         (gnus-group-mark-group 1 'unmark t)
1658         t)
1659     (setq gnus-group-marked
1660           (delete group gnus-group-marked))
1661     nil))
1662
1663 (defun gnus-group-set-mark (group)
1664   "Set the process mark on GROUP."
1665   (if (gnus-group-goto-group group)
1666       (save-excursion
1667         (gnus-group-mark-group 1 nil t))
1668     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
1669
1670 (defun gnus-group-universal-argument (arg &optional groups func)
1671   "Perform any command on all groups according to the process/prefix convention."
1672   (interactive "P")
1673   (if (eq (setq func (or func
1674                          (key-binding
1675                           (read-key-sequence
1676                            (substitute-command-keys
1677                             "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
1678           'undefined)
1679       (gnus-error 1 "Undefined key")
1680     (gnus-group-iterate arg
1681       (lambda (group)
1682         (command-execute func))))
1683   (gnus-group-position-point))
1684
1685 (defun gnus-group-process-prefix (n)
1686   "Return a list of groups to work on.
1687 Take into consideration N (the prefix) and the list of marked groups."
1688   (cond
1689    (n
1690     (setq n (prefix-numeric-value n))
1691     ;; There is a prefix, so we return a list of the N next
1692     ;; groups.
1693     (let ((way (if (< n 0) -1 1))
1694           (n (abs n))
1695           group groups)
1696       (save-excursion
1697         (while (> n 0)
1698           (if (setq group (gnus-group-group-name))
1699               (push group groups))
1700           (setq n (1- n))
1701           (gnus-group-next-group way)))
1702       (nreverse groups)))
1703    ((gnus-region-active-p)
1704     ;; Work on the region between point and mark.
1705     (let ((max (max (point) (mark)))
1706           groups)
1707       (save-excursion
1708         (goto-char (min (point) (mark)))
1709         (while
1710             (and
1711              (push (gnus-group-group-name) groups)
1712              (zerop (gnus-group-next-group 1))
1713              (< (point) max)))
1714         (nreverse groups))))
1715    (gnus-group-marked
1716     ;; No prefix, but a list of marked articles.
1717     (reverse gnus-group-marked))
1718    (t
1719     ;; Neither marked articles or a prefix, so we return the
1720     ;; current group.
1721     (let ((group (gnus-group-group-name)))
1722       (and group (list group))))))
1723
1724 ;;; !!!Surely gnus-group-iterate should be a macro instead?  I can't
1725 ;;; imagine why I went through these contortions...
1726 (eval-and-compile
1727   (let ((function (make-symbol "gnus-group-iterate-function"))
1728         (window (make-symbol "gnus-group-iterate-window"))
1729         (groups (make-symbol "gnus-group-iterate-groups"))
1730         (group (make-symbol "gnus-group-iterate-group")))
1731     (eval
1732      `(defun gnus-group-iterate (arg ,function)
1733         "Iterate FUNCTION over all process/prefixed groups.
1734 FUNCTION will be called with the group name as the parameter
1735 and with point over the group in question."
1736         (let ((,groups (gnus-group-process-prefix arg))
1737               (,window (selected-window))
1738               ,group)
1739           (while ,groups
1740             (setq ,group (car ,groups)
1741                   ,groups (cdr ,groups))
1742             (select-window ,window)
1743             (gnus-group-remove-mark ,group)
1744             (save-selected-window
1745               (save-excursion
1746                 (funcall ,function ,group)))))))))
1747
1748 (put 'gnus-group-iterate 'lisp-indent-function 1)
1749
1750 ;; Selecting groups.
1751
1752 (defun gnus-group-read-group (&optional all no-article group select-articles)
1753   "Read news in this newsgroup.
1754 If the prefix argument ALL is non-nil, already read articles become
1755 readable.  IF ALL is a number, fetch this number of articles.  If the
1756 optional argument NO-ARTICLE is non-nil, no article will be
1757 auto-selected upon group entry.  If GROUP is non-nil, fetch that
1758 group."
1759   (interactive "P")
1760   (let ((no-display (eq all 0))
1761         (group (or group (gnus-group-group-name)))
1762         number active marked entry)
1763     (when (eq all 0)
1764       (setq all nil))
1765     (unless group
1766       (error "No group on current line"))
1767     (setq marked (gnus-info-marks
1768                   (nth 2 (setq entry (gnus-gethash
1769                                       group gnus-newsrc-hashtb)))))
1770     ;; This group might be a dead group.  In that case we have to get
1771     ;; the number of unread articles from `gnus-active-hashtb'.
1772     (setq number
1773           (cond ((numberp all) all)
1774                 (entry (car entry))
1775                 ((setq active (gnus-active group))
1776                  (- (1+ (cdr active)) (car active)))))
1777     (gnus-summary-read-group
1778      group (or all (and (numberp number)
1779                         (zerop (+ number (gnus-range-length
1780                                           (cdr (assq 'tick marked)))
1781                                   (gnus-range-length
1782                                    (cdr (assq 'dormant marked)))))))
1783      no-article nil no-display nil select-articles)))
1784
1785 (defun gnus-group-select-group (&optional all)
1786   "Select this newsgroup.
1787 No article is selected automatically.
1788 If the group is opened, just switch the summary buffer.
1789 If ALL is non-nil, already read articles become readable.
1790 If ALL is a number, fetch this number of articles."
1791   (interactive "P")
1792   (gnus-group-read-group all t))
1793
1794 (defun gnus-group-quick-select-group (&optional all)
1795   "Select the current group \"quickly\".
1796 This means that no highlighting or scoring will be performed.
1797 If ALL (the prefix argument) is 0, don't even generate the summary
1798 buffer.
1799
1800 This might be useful if you want to toggle threading
1801 before entering the group."
1802   (interactive "P")
1803   (require 'gnus-score)
1804   (let (gnus-visual
1805         gnus-score-find-score-files-function
1806         gnus-home-score-file
1807         gnus-apply-kill-hook
1808         gnus-summary-expunge-below)
1809     (gnus-group-read-group all t)))
1810
1811 (defun gnus-group-visible-select-group (&optional all)
1812   "Select the current group without hiding any articles."
1813   (interactive "P")
1814   (let ((gnus-inhibit-limiting t))
1815     (gnus-group-read-group all t)))
1816
1817 (defun gnus-group-select-group-ephemerally ()
1818   "Select the current group without doing any processing whatsoever.
1819 You will actually be entered into a group that's a copy of
1820 the current group; no changes you make while in this group will
1821 be permanent."
1822   (interactive)
1823   (require 'gnus-score)
1824   (let* (gnus-visual
1825          gnus-score-find-score-files-function gnus-apply-kill-hook
1826          gnus-summary-expunge-below gnus-show-threads gnus-suppress-duplicates
1827          gnus-summary-mode-hook gnus-select-group-hook
1828          (group (gnus-group-group-name))
1829          (method (gnus-find-method-for-group group)))
1830     (gnus-group-read-ephemeral-group
1831      (gnus-group-prefixed-name group method) method)))
1832
1833 ;;;###autoload
1834 (defun gnus-fetch-group (group)
1835   "Start Gnus if necessary and enter GROUP.
1836 Returns whether the fetching was successful or not."
1837   (interactive (list (completing-read "Group name: " gnus-active-hashtb)))
1838   (unless (get-buffer gnus-group-buffer)
1839     (gnus-no-server))
1840   (gnus-group-read-group nil nil group))
1841
1842 ;;;###autoload
1843 (defun gnus-fetch-group-other-frame (group)
1844   "Pop up a frame and enter GROUP."
1845   (interactive "P")
1846   (let ((window (get-buffer-window gnus-group-buffer)))
1847     (cond (window
1848            (select-frame (window-frame window)))
1849           ((= (length (frame-list)) 1)
1850            (select-frame (make-frame)))
1851           (t
1852            (other-frame 1))))
1853   (gnus-fetch-group group))
1854
1855 (defvar gnus-ephemeral-group-server 0)
1856
1857 ;; Enter a group that is not in the group buffer.  Non-nil is returned
1858 ;; if selection was successful.
1859 (defun gnus-group-read-ephemeral-group (group method &optional activate
1860                                               quit-config request-only
1861                                               select-articles)
1862   "Read GROUP from METHOD as an ephemeral group.
1863 If ACTIVATE, request the group first.
1864 If QUIT-CONFIG, use that window configuration when exiting from the
1865 ephemeral group.
1866 If REQUEST-ONLY, don't actually read the group; just request it.
1867 If SELECT-ARTICLES, only select those articles.
1868
1869 Return the name of the group if selection was successful."
1870   ;; Transform the select method into a unique server.
1871   (when (stringp method)
1872     (setq method (gnus-server-to-method method)))
1873   (setq method
1874         `(,(car method) ,(concat (cadr method) "-ephemeral")
1875           (,(intern (format "%s-address" (car method))) ,(cadr method))
1876           ,@(cddr method)))
1877   (let ((group (if (gnus-group-foreign-p group) group
1878                  (gnus-group-prefixed-name group method))))
1879     (gnus-sethash
1880      group
1881      `(-1 nil (,group
1882                ,gnus-level-default-subscribed nil nil ,method
1883                ((quit-config .
1884                              ,(if quit-config quit-config
1885                                 (cons gnus-summary-buffer
1886                                       gnus-current-window-configuration))))))
1887      gnus-newsrc-hashtb)
1888     (push method gnus-ephemeral-servers)
1889     (set-buffer gnus-group-buffer)
1890     (unless (gnus-check-server method)
1891       (error "Unable to contact server: %s" (gnus-status-message method)))
1892     (when activate
1893       (gnus-activate-group group 'scan)
1894       (unless (gnus-request-group group)
1895         (error "Couldn't request group: %s"
1896                (nnheader-get-report (car method)))))
1897     (if request-only
1898         group
1899       (condition-case ()
1900           (when (gnus-group-read-group t t group select-articles)
1901             group)
1902         ;;(error nil)
1903         (quit
1904          (message "Quit reading the ephemeral group")
1905          nil)))))
1906
1907 (defun gnus-group-jump-to-group (group)
1908   "Jump to newsgroup GROUP."
1909   (interactive
1910    (list (completing-read
1911           "Group: " gnus-active-hashtb nil
1912           (gnus-read-active-file-p)
1913           gnus-group-jump-to-group-prompt
1914           'gnus-group-history)))
1915
1916   (when (equal group "")
1917     (error "Empty group name"))
1918
1919   (unless (gnus-ephemeral-group-p group)
1920     ;; Either go to the line in the group buffer...
1921     (unless (gnus-group-goto-group group)
1922       ;; ... or insert the line.
1923       (gnus-group-update-group group)
1924       (gnus-group-goto-group group)))
1925   ;; Adjust cursor point.
1926   (gnus-group-position-point))
1927
1928 (defun gnus-group-goto-group (group &optional far test-marked)
1929   "Goto to newsgroup GROUP.
1930 If FAR, it is likely that the group is not on the current line.
1931 If TEST-MARKED, the line must be marked."
1932   (when group
1933     (beginning-of-line)
1934     (cond
1935      ;; It's quite likely that we are on the right line, so
1936      ;; we check the current line first.
1937      ((and (not far)
1938            (eq (get-text-property (point) 'gnus-group)
1939                (gnus-intern-safe group gnus-active-hashtb))
1940            (or (not test-marked) (gnus-group-mark-line-p)))
1941       (point))
1942      ;; Previous and next line are also likely, so we check them as well.
1943      ((and (not far)
1944            (save-excursion
1945              (forward-line -1)
1946              (and (eq (get-text-property (point) 'gnus-group)
1947                       (gnus-intern-safe group gnus-active-hashtb))
1948                   (or (not test-marked) (gnus-group-mark-line-p)))))
1949       (forward-line -1)
1950       (point))
1951      ((and (not far)
1952            (save-excursion
1953              (forward-line 1)
1954              (and (eq (get-text-property (point) 'gnus-group)
1955                       (gnus-intern-safe group gnus-active-hashtb))
1956                   (or (not test-marked) (gnus-group-mark-line-p)))))
1957       (forward-line 1)
1958       (point))
1959      (test-marked
1960       (goto-char (point-min))
1961       (let (found)
1962         (while (and (not found)
1963                     (gnus-goto-char
1964                      (text-property-any
1965                       (point) (point-max)
1966                       'gnus-group
1967                       (gnus-intern-safe group gnus-active-hashtb))))
1968           (if (gnus-group-mark-line-p)
1969               (setq found t)
1970             (forward-line 1)))
1971         found))
1972      (t
1973       ;; Search through the entire buffer.
1974       (gnus-goto-char
1975        (text-property-any
1976         (point-min) (point-max)
1977         'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))))
1978
1979 (defun gnus-group-next-group (n &optional silent)
1980   "Go to next N'th newsgroup.
1981 If N is negative, search backward instead.
1982 Returns the difference between N and the number of skips actually
1983 done."
1984   (interactive "p")
1985   (gnus-group-next-unread-group n t nil silent))
1986
1987 (defun gnus-group-next-unread-group (n &optional all level silent)
1988   "Go to next N'th unread newsgroup.
1989 If N is negative, search backward instead.
1990 If ALL is non-nil, choose any newsgroup, unread or not.
1991 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
1992 such group can be found, the next group with a level higher than
1993 LEVEL.
1994 Returns the difference between N and the number of skips actually
1995 made."
1996   (interactive "p")
1997   (let ((backward (< n 0))
1998         (n (abs n)))
1999     (while (and (> n 0)
2000                 (gnus-group-search-forward
2001                  backward (or (not gnus-group-goto-unread) all) level))
2002       (setq n (1- n)))
2003     (when (and (/= 0 n)
2004                (not silent))
2005       (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
2006                     (if level " on this level or higher" "")))
2007     n))
2008
2009 (defun gnus-group-prev-group (n)
2010   "Go to previous N'th newsgroup.
2011 Returns the difference between N and the number of skips actually
2012 done."
2013   (interactive "p")
2014   (gnus-group-next-unread-group (- n) t))
2015
2016 (defun gnus-group-prev-unread-group (n)
2017   "Go to previous N'th unread newsgroup.
2018 Returns the difference between N and the number of skips actually
2019 done."
2020   (interactive "p")
2021   (gnus-group-next-unread-group (- n)))
2022
2023 (defun gnus-group-next-unread-group-same-level (n)
2024   "Go to next N'th unread newsgroup on the same level.
2025 If N is negative, search backward instead.
2026 Returns the difference between N and the number of skips actually
2027 done."
2028   (interactive "p")
2029   (gnus-group-next-unread-group n t (gnus-group-group-level))
2030   (gnus-group-position-point))
2031
2032 (defun gnus-group-prev-unread-group-same-level (n)
2033   "Go to next N'th unread newsgroup on the same level.
2034 Returns the difference between N and the number of skips actually
2035 done."
2036   (interactive "p")
2037   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
2038   (gnus-group-position-point))
2039
2040 (defun gnus-group-best-unread-group (&optional exclude-group)
2041   "Go to the group with the highest level.
2042 If EXCLUDE-GROUP, do not go to that group."
2043   (interactive)
2044   (goto-char (point-min))
2045   (let ((best 100000)
2046         unread best-point)
2047     (while (not (eobp))
2048       (setq unread (get-text-property (point) 'gnus-unread))
2049       (when (and (numberp unread) (> unread 0))
2050         (when (and (get-text-property (point) 'gnus-level)
2051                    (< (get-text-property (point) 'gnus-level) best)
2052                    (or (not exclude-group)
2053                        (not (equal exclude-group (gnus-group-group-name)))))
2054           (setq best (get-text-property (point) 'gnus-level))
2055           (setq best-point (point))))
2056       (forward-line 1))
2057     (when best-point
2058       (goto-char best-point))
2059     (gnus-summary-position-point)
2060     (and best-point (gnus-group-group-name))))
2061
2062 (defun gnus-group-first-unread-group ()
2063   "Go to the first group with unread articles."
2064   (interactive)
2065   (prog1
2066       (let ((opoint (point))
2067             unread)
2068         (goto-char (point-min))
2069         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
2070                 (and (numberp unread)   ; Not a topic.
2071                      (not (zerop unread))) ; Has unread articles.
2072                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
2073             (point)                     ; Success.
2074           (goto-char opoint)
2075           nil))                         ; Not success.
2076     (gnus-group-position-point)))
2077
2078 (defun gnus-group-enter-server-mode ()
2079   "Jump to the server buffer."
2080   (interactive)
2081   (gnus-enter-server-buffer))
2082
2083 (defun gnus-group-make-group (name &optional method address args)
2084   "Add a new newsgroup.
2085 The user will be prompted for a NAME, for a select METHOD, and an
2086 ADDRESS."
2087   (interactive
2088    (list
2089     (gnus-read-group "Group name: ")
2090     (gnus-read-method "From method: ")))
2091
2092   (when (stringp method)
2093     (setq method (or (gnus-server-to-method method) method)))
2094   (let* ((meth (gnus-method-simplify
2095                 (when (and method
2096                            (not (gnus-server-equal method gnus-select-method)))
2097                   (if address (list (intern method) address)
2098                     method))))
2099          (nname (if method (gnus-group-prefixed-name name meth) name))
2100          backend info)
2101     (when (gnus-gethash nname gnus-newsrc-hashtb)
2102       (error "Group %s already exists" nname))
2103     ;; Subscribe to the new group.
2104     (gnus-group-change-level
2105      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
2106      gnus-level-default-subscribed gnus-level-killed
2107      (and (gnus-group-group-name)
2108           (gnus-gethash (gnus-group-group-name)
2109                         gnus-newsrc-hashtb))
2110      t)
2111     ;; Make it active.
2112     (gnus-set-active nname (cons 1 0))
2113     (unless (gnus-ephemeral-group-p name)
2114       (gnus-dribble-enter
2115        (concat "(gnus-group-set-info '"
2116                (gnus-prin1-to-string (cdr info)) ")")))
2117     ;; Insert the line.
2118     (gnus-group-insert-group-line-info nname)
2119     (forward-line -1)
2120     (gnus-group-position-point)
2121
2122     ;; Load the backend and try to make the backend create
2123     ;; the group as well.
2124     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
2125                                                   nil meth))))
2126                  gnus-valid-select-methods)
2127       (require backend))
2128     (gnus-check-server meth)
2129     (when (gnus-check-backend-function 'request-create-group nname)
2130       (gnus-request-create-group nname nil args))
2131     t))
2132
2133 (defun gnus-group-delete-groups (&optional arg)
2134   "Delete the current group.  Only meaningful with editable groups."
2135   (interactive "P")
2136   (let ((n (length (gnus-group-process-prefix arg))))
2137     (when (gnus-yes-or-no-p
2138            (if (= n 1)
2139                "Delete this 1 group? "
2140              (format "Delete these %d groups? " n)))
2141       (gnus-group-iterate arg
2142         (lambda (group)
2143           (gnus-group-delete-group group nil t))))))
2144
2145 (defun gnus-group-delete-group (group &optional force no-prompt)
2146   "Delete the current group.  Only meaningful with editable groups.
2147 If FORCE (the prefix) is non-nil, all the articles in the group will
2148 be deleted.  This is \"deleted\" as in \"removed forever from the face
2149 of the Earth\".  There is no undo.  The user will be prompted before
2150 doing the deletion."
2151   (interactive
2152    (list (gnus-group-group-name)
2153          current-prefix-arg))
2154   (unless group
2155     (error "No group to rename"))
2156   (unless (gnus-check-backend-function 'request-delete-group group)
2157     (error "This backend does not support group deletion"))
2158   (prog1
2159       (if (and (not no-prompt)
2160                (not (gnus-yes-or-no-p
2161                      (format
2162                       "Do you really want to delete %s%s? "
2163                       group (if force " and all its contents" "")))))
2164           ()                            ; Whew!
2165         (gnus-message 6 "Deleting group %s..." group)
2166         (if (not (gnus-request-delete-group group force))
2167             (gnus-error 3 "Couldn't delete group %s" group)
2168           (gnus-message 6 "Deleting group %s...done" group)
2169           (gnus-group-goto-group group)
2170           (gnus-group-kill-group 1 t)
2171           (gnus-sethash group nil gnus-active-hashtb)
2172           t))
2173     (gnus-group-position-point)))
2174
2175 (defun gnus-group-rename-group (group new-name)
2176   "Rename group from GROUP to NEW-NAME.
2177 When used interactively, GROUP is the group under point
2178 and NEW-NAME will be prompted for."
2179   (interactive
2180    (list
2181     (gnus-group-group-name)
2182     (progn
2183       (unless (gnus-check-backend-function
2184                'request-rename-group (gnus-group-group-name))
2185         (error "This backend does not support renaming groups"))
2186       (gnus-read-group "Rename group to: "
2187                        (gnus-group-real-name (gnus-group-group-name))))))
2188
2189   (unless (gnus-check-backend-function 'request-rename-group group)
2190     (error "This backend does not support renaming groups"))
2191   (unless group
2192     (error "No group to rename"))
2193   (when (equal (gnus-group-real-name group) new-name)
2194     (error "Can't rename to the same name"))
2195
2196   ;; We find the proper prefixed name.
2197   (setq new-name
2198         (if (gnus-group-native-p group)
2199             ;; Native group.
2200             new-name
2201           ;; Foreign group.
2202           (gnus-group-prefixed-name
2203            (gnus-group-real-name new-name)
2204            (gnus-info-method (gnus-get-info group)))))
2205
2206   (gnus-message 6 "Renaming group %s to %s..." group new-name)
2207   (prog1
2208       (if (progn
2209             (gnus-group-goto-group group)
2210             (not (when (< (gnus-group-group-level) gnus-level-zombie)
2211                    (gnus-request-rename-group group new-name))))
2212           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
2213         ;; We rename the group internally by killing it...
2214         (gnus-group-kill-group)
2215         ;; ... changing its name ...
2216         (setcar (cdar gnus-list-of-killed-groups) new-name)
2217         ;; ... and then yanking it.  Magic!
2218         (gnus-group-yank-group)
2219         (gnus-set-active new-name (gnus-active group))
2220         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
2221         new-name)
2222     (setq gnus-killed-list (delete group gnus-killed-list))
2223     (gnus-set-active group nil)
2224     (gnus-dribble-touch)
2225     (gnus-group-position-point)))
2226
2227 (defun gnus-group-edit-group (group &optional part)
2228   "Edit the group on the current line."
2229   (interactive (list (gnus-group-group-name)))
2230   (let ((part (or part 'info))
2231         info)
2232     (unless group
2233       (error "No group on current line"))
2234     (unless (setq info (gnus-get-info group))
2235       (error "Killed group; can't be edited"))
2236     (ignore-errors
2237       (gnus-close-group group))
2238     (gnus-edit-form
2239      ;; Find the proper form to edit.
2240      (cond ((eq part 'method)
2241             (or (gnus-info-method info) "native"))
2242            ((eq part 'params)
2243             (gnus-info-params info))
2244            (t info))
2245      ;; The proper documentation.
2246      (format
2247       "Editing the %s for `%s'."
2248       (cond
2249        ((eq part 'method) "select method")
2250        ((eq part 'params) "group parameters")
2251        (t "group info"))
2252       (gnus-group-decoded-name group))
2253      `(lambda (form)
2254         (gnus-group-edit-group-done ',part ,group form)))
2255     (local-set-key
2256      "\C-c\C-i"
2257      (gnus-create-info-command
2258       (cond
2259        ((eq part 'method)
2260         "(gnus)Select Methods")
2261        ((eq part 'params)
2262         "(gnus)Group Parameters")
2263        (t
2264         "(gnus)Group Info"))))))
2265
2266 (defun gnus-group-edit-group-method (group)
2267   "Edit the select method of GROUP."
2268   (interactive (list (gnus-group-group-name)))
2269   (gnus-group-edit-group group 'method))
2270
2271 (defun gnus-group-edit-group-parameters (group)
2272   "Edit the group parameters of GROUP."
2273   (interactive (list (gnus-group-group-name)))
2274   (gnus-group-edit-group group 'params))
2275
2276 (defun gnus-group-edit-group-done (part group form)
2277   "Update variables."
2278   (let* ((method (cond ((eq part 'info) (nth 4 form))
2279                        ((eq part 'method) form)
2280                        (t nil)))
2281          (info (cond ((eq part 'info) form)
2282                      ((eq part 'method) (gnus-get-info group))
2283                      (t nil)))
2284          (new-group (if info
2285                         (if (or (not method)
2286                                 (gnus-server-equal
2287                                  gnus-select-method method))
2288                             (gnus-group-real-name (car info))
2289                           (gnus-group-prefixed-name
2290                            (gnus-group-real-name (car info)) method))
2291                       nil)))
2292     (when (and new-group
2293                (not (equal new-group group)))
2294       (when (gnus-group-goto-group group)
2295         (gnus-group-kill-group 1))
2296       (gnus-activate-group new-group))
2297     ;; Set the info.
2298     (if (not (and info new-group))
2299         (gnus-group-set-info form (or new-group group) part)
2300       (setq info (gnus-copy-sequence info))
2301       (setcar info new-group)
2302       (unless (gnus-server-equal method "native")
2303         (unless (nthcdr 3 info)
2304           (nconc info (list nil nil)))
2305         (unless (nthcdr 4 info)
2306           (nconc info (list nil)))
2307         (gnus-info-set-method info method))
2308       (gnus-group-set-info info))
2309     (gnus-group-update-group (or new-group group))
2310     (gnus-group-position-point)))
2311
2312 (defun gnus-group-make-useful-group (group method)
2313   "Create one of the groups described in `gnus-useful-groups'."
2314   (interactive
2315    (let ((entry (assoc (completing-read "Create group: " gnus-useful-groups
2316                                         nil t)
2317                        gnus-useful-groups)))
2318      (list (cadr entry) (caddr entry))))
2319   (setq method (gnus-copy-sequence method))
2320   (let (entry)
2321     (while (setq entry (memq (assq 'eval method) method))
2322       (setcar entry (eval (cadar entry)))))
2323   (gnus-group-make-group group method))
2324
2325 (defun gnus-group-make-help-group ()
2326   "Create the Gnus documentation group."
2327   (interactive)
2328   (let ((name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
2329         (file (nnheader-find-etc-directory "gnus-tut.txt" t)))
2330     (when (gnus-gethash name gnus-newsrc-hashtb)
2331       (error "Documentation group already exists"))
2332     (if (not file)
2333         (gnus-message 1 "Couldn't find doc group")
2334       (gnus-group-make-group
2335        (gnus-group-real-name name)
2336        (list 'nndoc "gnus-help"
2337              (list 'nndoc-address file)
2338              (list 'nndoc-article-type 'mbox)))))
2339   (gnus-group-position-point))
2340
2341 (defun gnus-group-make-doc-group (file type)
2342   "Create a group that uses a single file as the source."
2343   (interactive
2344    (list (read-file-name "File name: ")
2345          (and current-prefix-arg 'ask)))
2346   (when (eq type 'ask)
2347     (let ((err "")
2348           char found)
2349       (while (not found)
2350         (message
2351          "%sFile type (mbox, babyl, digest, forward, mmdf, guess) [mbdfag]: "
2352          err)
2353         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
2354                           ((= char ?b) 'babyl)
2355                           ((= char ?d) 'digest)
2356                           ((= char ?f) 'forward)
2357                           ((= char ?a) 'mmfd)
2358                           ((= char ?g) 'guess)
2359                           (t (setq err (format "%c unknown. " char))
2360                              nil))))
2361       (setq type found)))
2362   (let* ((file (expand-file-name file))
2363          (name (gnus-generate-new-group-name
2364                 (gnus-group-prefixed-name
2365                  (file-name-nondirectory file) '(nndoc "")))))
2366     (gnus-group-make-group
2367      (gnus-group-real-name name)
2368      (list 'nndoc file
2369            (list 'nndoc-address file)
2370            (list 'nndoc-article-type (or type 'guess))))))
2371
2372 (defvar nnweb-type-definition)
2373 (defvar gnus-group-web-type-history nil)
2374 (defvar gnus-group-web-search-history nil)
2375 (defun gnus-group-make-web-group (&optional solid)
2376   "Create an ephemeral nnweb group.
2377 If SOLID (the prefix), create a solid group."
2378   (interactive "P")
2379   (require 'nnweb)
2380   (let* ((group
2381           (if solid (gnus-read-group "Group name: ")
2382             (message-unique-id)))
2383          (default-type (or (car gnus-group-web-type-history)
2384                            (symbol-name (caar nnweb-type-definition))))
2385          (type
2386           (gnus-string-or
2387            (completing-read
2388             (format "Search engine type (default %s): " default-type)
2389             (mapcar (lambda (elem) (list (symbol-name (car elem))))
2390                     nnweb-type-definition)
2391             nil t nil 'gnus-group-web-type-history)
2392            default-type))
2393          (search
2394           (read-string
2395            "Search string: "
2396            (cons (or (car gnus-group-web-search-history) "") 0)
2397            'gnus-group-web-search-history))
2398          (method
2399           `(nnweb ,group (nnweb-search ,search)
2400                   (nnweb-type ,(intern type))
2401                   (nnweb-ephemeral-p t))))
2402     (if solid
2403         (gnus-group-make-group group "nnweb" "" `(,(intern type) ,search))
2404       (gnus-group-read-ephemeral-group
2405        group method t
2406        (cons (current-buffer)
2407              (if (eq major-mode 'gnus-summary-mode) 'summary 'group))))))
2408
2409 (defvar nnwarchive-type-definition)
2410 (defvar gnus-group-warchive-type-history nil)
2411 (defvar gnus-group-warchive-login-history nil)
2412 (defvar gnus-group-warchive-address-history nil)
2413
2414 (defun gnus-group-make-warchive-group ()
2415   "Create a nnwarchive group."
2416   (interactive)
2417   (require 'nnwarchive)
2418   (let* ((group (gnus-read-group "Group name: "))
2419          (default-type (or (car gnus-group-warchive-type-history)
2420                            (symbol-name (caar nnwarchive-type-definition))))
2421          (type
2422           (gnus-string-or
2423            (completing-read
2424             (format "Warchive type (default %s): " default-type)
2425             (mapcar (lambda (elem) (list (symbol-name (car elem))))
2426                     nnwarchive-type-definition)
2427             nil t nil 'gnus-group-warchive-type-history)
2428            default-type))
2429          (address (read-string "Warchive address: "
2430                                nil 'gnus-group-warchive-address-history))
2431          (default-login (or (car gnus-group-warchive-login-history)
2432                             user-mail-address))
2433          (login
2434           (gnus-string-or
2435            (read-string
2436             (format "Warchive login (default %s): " user-mail-address)
2437             default-login 'gnus-group-warchive-login-history)
2438            user-mail-address))
2439          (method
2440           `(nnwarchive ,address
2441                        (nnwarchive-type ,(intern type))
2442                        (nnwarchive-login ,login))))
2443     (gnus-group-make-group group method)))
2444
2445 (defun gnus-group-make-archive-group (&optional all)
2446   "Create the (ding) Gnus archive group of the most recent articles.
2447 Given a prefix, create a full group."
2448   (interactive "P")
2449   (let ((group (gnus-group-prefixed-name
2450                 (if all "ding.archives" "ding.recent") '(nndir ""))))
2451     (when (gnus-gethash group gnus-newsrc-hashtb)
2452       (error "Archive group already exists"))
2453     (gnus-group-make-group
2454      (gnus-group-real-name group)
2455      (list 'nndir (if all "hpc" "edu")
2456            (list 'nndir-directory
2457                  (if all gnus-group-archive-directory
2458                    gnus-group-recent-archive-directory))))
2459     (gnus-group-add-parameter group (cons 'to-address "ding@gnus.org"))))
2460
2461 (defun gnus-group-make-directory-group (dir)
2462   "Create an nndir group.
2463 The user will be prompted for a directory.  The contents of this
2464 directory will be used as a newsgroup.  The directory should contain
2465 mail messages or news articles in files that have numeric names."
2466   (interactive
2467    (list (read-file-name "Create group from directory: ")))
2468   (unless (file-exists-p dir)
2469     (error "No such directory"))
2470   (unless (file-directory-p dir)
2471     (error "Not a directory"))
2472   (let ((ext "")
2473         (i 0)
2474         group)
2475     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
2476       (setq group
2477             (gnus-group-prefixed-name
2478              (expand-file-name ext dir)
2479              '(nndir "")))
2480       (setq ext (format "<%d>" (setq i (1+ i)))))
2481     (gnus-group-make-group
2482      (gnus-group-real-name group)
2483      (list 'nndir (gnus-group-real-name group) (list 'nndir-directory dir)))))
2484
2485 (eval-when-compile (defvar nnkiboze-score-file))
2486 (defun gnus-group-make-kiboze-group (group address scores)
2487   "Create an nnkiboze group.
2488 The user will be prompted for a name, a regexp to match groups, and
2489 score file entries for articles to include in the group."
2490   (interactive
2491    (list
2492     (read-string "nnkiboze group name: ")
2493     (read-string "Source groups (regexp): ")
2494     (let ((headers (mapcar (lambda (group) (list group))
2495                            '("subject" "from" "number" "date" "message-id"
2496                              "references" "chars" "lines" "xref"
2497                              "followup" "all" "body" "head")))
2498           scores header regexp regexps)
2499       (while (not (equal "" (setq header (completing-read
2500                                           "Match on header: " headers nil t))))
2501         (setq regexps nil)
2502         (while (not (equal "" (setq regexp (read-string
2503                                             (format "Match on %s (regexp): "
2504                                                     header)))))
2505           (push (list regexp nil nil 'r) regexps))
2506         (push (cons header regexps) scores))
2507       scores)))
2508   (gnus-group-make-group group "nnkiboze" address)
2509   (let* ((nnkiboze-current-group group)
2510          (score-file (car (nnkiboze-score-file "")))
2511          (score-dir (file-name-directory score-file)))
2512     (unless (file-exists-p score-dir)
2513       (make-directory score-dir))
2514     (with-temp-file score-file
2515       (let (emacs-lisp-mode-hook)
2516         (pp scores (current-buffer))))))
2517
2518 (defun gnus-group-add-to-virtual (n vgroup)
2519   "Add the current group to a virtual group."
2520   (interactive
2521    (list current-prefix-arg
2522          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
2523                           "nnvirtual:")))
2524   (unless (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
2525     (error "%s is not an nnvirtual group" vgroup))
2526   (gnus-close-group vgroup)
2527   (let* ((groups (gnus-group-process-prefix n))
2528          (method (gnus-info-method (gnus-get-info vgroup))))
2529     (setcar (cdr method)
2530             (concat
2531              (nth 1 method) "\\|"
2532              (mapconcat
2533               (lambda (s)
2534                 (gnus-group-remove-mark s)
2535                 (concat "\\(^" (regexp-quote s) "$\\)"))
2536               groups "\\|"))))
2537   (gnus-group-position-point))
2538
2539 (defun gnus-group-make-empty-virtual (group)
2540   "Create a new, fresh, empty virtual group."
2541   (interactive "sCreate new, empty virtual group: ")
2542   (let* ((method (list 'nnvirtual "^$"))
2543          (pgroup (gnus-group-prefixed-name group method)))
2544     ;; Check whether it exists already.
2545     (when (gnus-gethash pgroup gnus-newsrc-hashtb)
2546       (error "Group %s already exists" pgroup))
2547     ;; Subscribe the new group after the group on the current line.
2548     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
2549     (gnus-group-update-group pgroup)
2550     (forward-line -1)
2551     (gnus-group-position-point)))
2552
2553 (defun gnus-group-enter-directory (dir)
2554   "Enter an ephemeral nneething group."
2555   (interactive "DDirectory to read: ")
2556   (let* ((method (list 'nneething dir '(nneething-read-only t)))
2557          (leaf (gnus-group-prefixed-name
2558                 (file-name-nondirectory (directory-file-name dir))
2559                 method))
2560          (name (gnus-generate-new-group-name leaf)))
2561     (unless (gnus-group-read-ephemeral-group
2562              name method t
2563              (cons (current-buffer)
2564                    (if (eq major-mode 'gnus-summary-mode)
2565                        'summary 'group)))
2566       (error "Couldn't enter %s" dir))))
2567
2568 (eval-and-compile
2569   (autoload 'nnimap-expunge "nnimap")
2570   (autoload 'nnimap-acl-get "nnimap")
2571   (autoload 'nnimap-acl-edit "nnimap"))
2572
2573 (defun gnus-group-nnimap-expunge (group)
2574   "Expunge deleted articles in current nnimap GROUP."
2575   (interactive (list (gnus-group-group-name)))
2576   (let ((mailbox (gnus-group-real-name group)) method)
2577     (unless group
2578       (error "No group on current line"))
2579     (unless (gnus-get-info group)
2580       (error "Killed group; can't be edited"))
2581     (unless (eq 'nnimap (car (setq method (gnus-find-method-for-group group))))
2582       (error "%s is not an nnimap group" group))
2583     (nnimap-expunge mailbox (cadr method))))
2584
2585 (defun gnus-group-nnimap-edit-acl (group)
2586   "Edit the Access Control List of current nnimap GROUP."
2587   (interactive (list (gnus-group-group-name)))
2588   (let ((mailbox (gnus-group-real-name group)) method acl)
2589     (unless group
2590       (error "No group on current line"))
2591     (unless (gnus-get-info group)
2592       (error "Killed group; can't be edited"))
2593     (unless (eq (car (setq method (gnus-find-method-for-group group))) 'nnimap)
2594       (error "%s is not an nnimap group" group))
2595     (unless (setq acl (nnimap-acl-get mailbox (cadr method)))
2596       (error "Server does not support ACL's"))
2597     (gnus-edit-form acl (format "Editing the access control list for `%s'.
2598
2599    An access control list is a list of (identifier . rights) elements.
2600
2601    The identifier string specifies the corresponding user.  The
2602    identifier \"anyone\" is reserved to refer to the universal identity.
2603
2604    Rights is a string listing a (possibly empty) set of alphanumeric
2605    characters, each character listing a set of operations which is being
2606    controlled.  Letters are reserved for ``standard'' rights, listed
2607    below.  Digits are reserved for implementation or site defined rights.
2608
2609    l - lookup (mailbox is visible to LIST/LSUB commands)
2610    r - read (SELECT the mailbox, perform CHECK, FETCH, PARTIAL,
2611        SEARCH, COPY from mailbox)
2612    s - keep seen/unseen information across sessions (STORE \\SEEN flag)
2613    w - write (STORE flags other than \\SEEN and \\DELETED)
2614    i - insert (perform APPEND, COPY into mailbox)
2615    p - post (send mail to submission address for mailbox,
2616        not enforced by IMAP4 itself)
2617    c - create and delete mailbox (CREATE new sub-mailboxes in any
2618        implementation-defined hierarchy, RENAME or DELETE mailbox)
2619    d - delete messages (STORE \\DELETED flag, perform EXPUNGE)
2620    a - administer (perform SETACL)" group)
2621                     `(lambda (form)
2622                        (nnimap-acl-edit
2623                         ,mailbox ',method ',acl form)))))
2624
2625 ;; Group sorting commands
2626 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
2627
2628 (defun gnus-group-sort-groups (func &optional reverse)
2629   "Sort the group buffer according to FUNC.
2630 When used interactively, the sorting function used will be
2631 determined by the `gnus-group-sort-function' variable.
2632 If REVERSE (the prefix), reverse the sorting order."
2633   (interactive (list gnus-group-sort-function current-prefix-arg))
2634   (funcall gnus-group-sort-alist-function
2635            (gnus-make-sort-function func) reverse)
2636   (gnus-group-list-groups)
2637   (gnus-dribble-touch))
2638
2639 (defun gnus-group-sort-flat (func reverse)
2640   ;; We peel off the dummy group from the alist.
2641   (when func
2642     (when (equal (gnus-info-group (car gnus-newsrc-alist)) "dummy.group")
2643       (pop gnus-newsrc-alist))
2644     ;; Do the sorting.
2645     (setq gnus-newsrc-alist
2646           (sort gnus-newsrc-alist func))
2647     (when reverse
2648       (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
2649     ;; Regenerate the hash table.
2650     (gnus-make-hashtable-from-newsrc-alist)))
2651
2652 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
2653   "Sort the group buffer alphabetically by group name.
2654 If REVERSE, sort in reverse order."
2655   (interactive "P")
2656   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
2657
2658 (defun gnus-group-sort-groups-by-unread (&optional reverse)
2659   "Sort the group buffer by number of unread articles.
2660 If REVERSE, sort in reverse order."
2661   (interactive "P")
2662   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
2663
2664 (defun gnus-group-sort-groups-by-level (&optional reverse)
2665   "Sort the group buffer by group level.
2666 If REVERSE, sort in reverse order."
2667   (interactive "P")
2668   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
2669
2670 (defun gnus-group-sort-groups-by-score (&optional reverse)
2671   "Sort the group buffer by group score.
2672 If REVERSE, sort in reverse order."
2673   (interactive "P")
2674   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
2675
2676 (defun gnus-group-sort-groups-by-rank (&optional reverse)
2677   "Sort the group buffer by group rank.
2678 If REVERSE, sort in reverse order."
2679   (interactive "P")
2680   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
2681
2682 (defun gnus-group-sort-groups-by-method (&optional reverse)
2683   "Sort the group buffer alphabetically by backend name.
2684 If REVERSE, sort in reverse order."
2685   (interactive "P")
2686   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
2687
2688 (defun gnus-group-sort-groups-by-server (&optional reverse)
2689   "Sort the group buffer alphabetically by server name.
2690 If REVERSE, sort in reverse order."
2691   (interactive "P")
2692   (gnus-group-sort-groups 'gnus-group-sort-by-server reverse))
2693
2694 ;;; Selected group sorting.
2695
2696 (defun gnus-group-sort-selected-groups (n func &optional reverse)
2697   "Sort the process/prefixed groups."
2698   (interactive (list current-prefix-arg gnus-group-sort-function))
2699   (let ((groups (gnus-group-process-prefix n)))
2700     (funcall gnus-group-sort-selected-function
2701              groups (gnus-make-sort-function func) reverse)
2702     (gnus-group-list-groups)))
2703
2704 (defun gnus-group-sort-selected-flat (groups func reverse)
2705   (let (entries infos)
2706     ;; First find all the group entries for these groups.
2707     (while groups
2708       (push (nthcdr 2 (gnus-gethash (pop groups) gnus-newsrc-hashtb))
2709             entries))
2710     ;; Then sort the infos.
2711     (setq infos
2712           (sort
2713            (mapcar
2714             (lambda (entry) (car entry))
2715             (setq entries (nreverse entries)))
2716            func))
2717     (when reverse
2718       (setq infos (nreverse infos)))
2719     ;; Go through all the infos and replace the old entries
2720     ;; with the new infos.
2721     (while infos
2722       (setcar (car entries) (pop infos))
2723       (pop entries))
2724     ;; Update the hashtable.
2725     (gnus-make-hashtable-from-newsrc-alist)))
2726
2727 (defun gnus-group-sort-selected-groups-by-alphabet (&optional n reverse)
2728   "Sort the group buffer alphabetically by group name.
2729 Obeys the process/prefix convention.  If REVERSE (the symbolic prefix),
2730 sort in reverse order."
2731   (interactive (gnus-interactive "P\ny"))
2732   (gnus-group-sort-selected-groups n 'gnus-group-sort-by-alphabet reverse))
2733
2734 (defun gnus-group-sort-selected-groups-by-unread (&optional n reverse)
2735   "Sort the group buffer by number of unread articles.
2736 Obeys the process/prefix convention.  If REVERSE (the symbolic prefix),
2737 sort in reverse order."
2738   (interactive (gnus-interactive "P\ny"))
2739   (gnus-group-sort-selected-groups n 'gnus-group-sort-by-unread reverse))
2740
2741 (defun gnus-group-sort-selected-groups-by-level (&optional n reverse)
2742   "Sort the group buffer by group level.
2743 Obeys the process/prefix convention.  If REVERSE (the symbolic prefix),
2744 sort in reverse order."
2745   (interactive (gnus-interactive "P\ny"))
2746   (gnus-group-sort-selected-groups n 'gnus-group-sort-by-level reverse))
2747
2748 (defun gnus-group-sort-selected-groups-by-score (&optional n reverse)
2749   "Sort the group buffer by group score.
2750 Obeys the process/prefix convention.  If REVERSE (the symbolic prefix),
2751 sort in reverse order."
2752   (interactive (gnus-interactive "P\ny"))
2753   (gnus-group-sort-selected-groups n 'gnus-group-sort-by-score reverse))
2754
2755 (defun gnus-group-sort-selected-groups-by-rank (&optional n reverse)
2756   "Sort the group buffer by group rank.
2757 Obeys the process/prefix convention.  If REVERSE (the symbolic prefix),
2758 sort in reverse order."
2759   (interactive (gnus-interactive "P\ny"))
2760   (gnus-group-sort-selected-groups n 'gnus-group-sort-by-rank reverse))
2761
2762 (defun gnus-group-sort-selected-groups-by-method (&optional n reverse)
2763   "Sort the group buffer alphabetically by backend name.
2764 Obeys the process/prefix convention.  If REVERSE (the symbolic prefix),
2765 sort in reverse order."
2766   (interactive (gnus-interactive "P\ny"))
2767   (gnus-group-sort-selected-groups n 'gnus-group-sort-by-method reverse))
2768
2769 ;;; Sorting predicates.
2770
2771 (defun gnus-group-sort-by-alphabet (info1 info2)
2772   "Sort alphabetically."
2773   (string< (gnus-info-group info1) (gnus-info-group info2)))
2774
2775 (defun gnus-group-sort-by-real-name (info1 info2)
2776   "Sort alphabetically on real (unprefixed) names."
2777   (string< (gnus-group-real-name (gnus-info-group info1))
2778            (gnus-group-real-name (gnus-info-group info2))))
2779
2780 (defun gnus-group-sort-by-unread (info1 info2)
2781   "Sort by number of unread articles."
2782   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
2783         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
2784     (< (or (and (numberp n1) n1) 0)
2785        (or (and (numberp n2) n2) 0))))
2786
2787 (defun gnus-group-sort-by-level (info1 info2)
2788   "Sort by level."
2789   (< (gnus-info-level info1) (gnus-info-level info2)))
2790
2791 (defun gnus-group-sort-by-method (info1 info2)
2792   "Sort alphabetically by backend name."
2793   (string< (symbol-name (car (gnus-find-method-for-group
2794                               (gnus-info-group info1) info1)))
2795            (symbol-name (car (gnus-find-method-for-group
2796                               (gnus-info-group info2) info2)))))
2797
2798 (defun gnus-group-sort-by-server (info1 info2)
2799   "Sort alphabetically by server name."
2800   (string< (gnus-method-to-server-name
2801             (gnus-find-method-for-group
2802              (gnus-info-group info1) info1))
2803            (gnus-method-to-server-name
2804             (gnus-find-method-for-group
2805              (gnus-info-group info2) info2))))
2806
2807 (defun gnus-group-sort-by-score (info1 info2)
2808   "Sort by group score."
2809   (> (gnus-info-score info1) (gnus-info-score info2)))
2810
2811 (defun gnus-group-sort-by-rank (info1 info2)
2812   "Sort by level and score."
2813   (let ((level1 (gnus-info-level info1))
2814         (level2 (gnus-info-level info2)))
2815     (or (< level1 level2)
2816         (and (= level1 level2)
2817              (> (gnus-info-score info1) (gnus-info-score info2))))))
2818
2819 ;;; Clearing data
2820
2821 (defun gnus-group-clear-data (&optional arg)
2822   "Clear all marks and read ranges from the current group."
2823   (interactive "P")
2824   (gnus-group-iterate arg
2825     (lambda (group)
2826       (let (info)
2827         (gnus-info-clear-data (setq info (gnus-get-info group)))
2828         (gnus-get-unread-articles-in-group info (gnus-active group) t)
2829         (when (gnus-group-goto-group group)
2830           (gnus-group-update-group-line))))))
2831
2832 (defun gnus-group-clear-data-on-native-groups ()
2833   "Clear all marks and read ranges from all native groups."
2834   (interactive)
2835   (when (gnus-yes-or-no-p "Really clear all data from almost all groups? ")
2836     (let ((alist (cdr gnus-newsrc-alist))
2837           info)
2838       (while (setq info (pop alist))
2839         (when (gnus-group-native-p (gnus-info-group info))
2840           (gnus-info-clear-data info)))
2841       (gnus-get-unread-articles)
2842       (gnus-dribble-touch)
2843       (when (gnus-y-or-n-p
2844              "Move the cache away to avoid problems in the future? ")
2845         (call-interactively 'gnus-cache-move-cache)))))
2846
2847 (defun gnus-info-clear-data (info)
2848   "Clear all marks and read ranges from INFO."
2849   (let ((group (gnus-info-group info))
2850         action)
2851     (dolist (el (gnus-info-marks info))
2852       (push `(,(cdr el) add (,(car el))) action))
2853     (push `(,(gnus-info-read info) add (read)) action)
2854     (gnus-undo-register
2855       `(progn
2856          (gnus-request-set-mark ,group ',action)
2857          (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
2858          (gnus-info-set-read ',info ',(gnus-info-read info))
2859          (when (gnus-group-goto-group ,group)
2860            (gnus-get-unread-articles-in-group ',info ',(gnus-active group) t)
2861            (gnus-group-update-group-line))))
2862     (setq action (mapcar (lambda (el) (list (nth 0 el) 'del (nth 2 el)))
2863                          action))
2864     (gnus-request-set-mark group action)
2865     (gnus-info-set-read info nil)
2866     (when (gnus-info-marks info)
2867       (gnus-info-set-marks info nil))))
2868
2869 ;; Group catching up.
2870
2871 (defun gnus-group-catchup-current (&optional n all)
2872   "Mark all unread articles in the current newsgroup as read.
2873 If prefix argument N is numeric, the next N newsgroups will be
2874 caught up.  If ALL is non-nil, marked articles will also be marked as
2875 read.  Cross references (Xref: header) of articles are ignored.
2876 The number of newsgroups that this function was unable to catch
2877 up is returned."
2878   (interactive "P")
2879   (let ((groups (gnus-group-process-prefix n))
2880         (ret 0)
2881         group)
2882     (unless groups (error "No groups selected"))
2883     (if (not
2884          (or (not gnus-interactive-catchup) ;Without confirmation?
2885              gnus-expert-user
2886              (gnus-y-or-n-p
2887               (format
2888                (if all
2889                    "Do you really want to mark all articles in %s as read? "
2890                  "Mark all unread articles in %s as read? ")
2891                (if (= (length groups) 1)
2892                    (car groups)
2893                  (format "these %d groups" (length groups)))))))
2894         n
2895       (while (setq group (pop groups))
2896         (gnus-group-remove-mark group)
2897         ;; Virtual groups have to be given special treatment.
2898         (let ((method (gnus-find-method-for-group group)))
2899           (when (eq 'nnvirtual (car method))
2900             (nnvirtual-catchup-group
2901              (gnus-group-real-name group) (nth 1 method) all)))
2902         (if (>= (gnus-group-level group) gnus-level-zombie)
2903             (gnus-message 2 "Dead groups can't be caught up")
2904           (if (prog1
2905                   (gnus-group-goto-group group)
2906                 (gnus-group-catchup group all))
2907               (gnus-group-update-group-line)
2908             (setq ret (1+ ret)))))
2909       (gnus-group-next-unread-group 1)
2910       ret)))
2911
2912 (defun gnus-group-catchup-current-all (&optional n)
2913   "Mark all articles in current newsgroup as read.
2914 Cross references (Xref: header) of articles are ignored."
2915   (interactive "P")
2916   (gnus-group-catchup-current n 'all))
2917
2918 (defun gnus-group-catchup (group &optional all)
2919   "Mark all articles in GROUP as read.
2920 If ALL is non-nil, all articles are marked as read.
2921 The return value is the number of articles that were marked as read,
2922 or nil if no action could be taken."
2923   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
2924          (num (car entry)))
2925     ;; Remove entries for this group.
2926     (nnmail-purge-split-history (gnus-group-real-name group))
2927     ;; Do the updating only if the newsgroup isn't killed.
2928     (if (not (numberp (car entry)))
2929         (gnus-message 1 "Can't catch up %s; non-active group" group)
2930       ;; Do auto-expirable marks if that's required.
2931       (when (gnus-group-auto-expirable-p group)
2932         (gnus-add-marked-articles
2933          group 'expire (gnus-list-of-unread-articles group))
2934         (when all
2935           (let ((marks (nth 3 (nth 2 entry))))
2936             (gnus-add-marked-articles
2937              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
2938             (gnus-add-marked-articles
2939              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
2940       (when entry
2941         (gnus-update-read-articles group nil)
2942         ;; Also nix out the lists of marks and dormants.
2943         (when all
2944           (gnus-add-marked-articles group 'tick nil nil 'force)
2945           (gnus-add-marked-articles group 'dormant nil nil 'force))
2946         (let ((gnus-newsgroup-name group))
2947           (gnus-run-hooks 'gnus-group-catchup-group-hook))
2948         num))))
2949
2950 (defun gnus-group-expire-articles (&optional n)
2951   "Expire all expirable articles in the current newsgroup."
2952   (interactive "P")
2953   (let ((groups (gnus-group-process-prefix n))
2954         group)
2955     (unless groups
2956       (error "No groups to expire"))
2957     (while (setq group (pop groups))
2958       (gnus-group-remove-mark group)
2959       (gnus-group-expire-articles-1 group)
2960       (gnus-dribble-touch)
2961       (gnus-group-position-point))))
2962
2963 (defun gnus-group-expire-articles-1 (group)
2964   (when (gnus-check-backend-function 'request-expire-articles group)
2965     (gnus-message 6 "Expiring articles in %s..." group)
2966     (let* ((info (gnus-get-info group))
2967            (expirable (if (gnus-group-total-expirable-p group)
2968                           (cons nil (gnus-list-of-read-articles group))
2969                         (assq 'expire (gnus-info-marks info))))
2970            (expiry-wait (gnus-group-find-parameter group 'expiry-wait))
2971            (nnmail-expiry-target
2972             (or (gnus-group-find-parameter group 'expiry-target)
2973                 nnmail-expiry-target)))
2974       (when expirable
2975         (gnus-check-group group)
2976         (setcdr
2977          expirable
2978          (gnus-compress-sequence
2979           (if expiry-wait
2980               ;; We set the expiry variables to the group
2981               ;; parameter.
2982               (let ((nnmail-expiry-wait-function nil)
2983                     (nnmail-expiry-wait expiry-wait))
2984                 (gnus-request-expire-articles
2985                  (gnus-uncompress-sequence (cdr expirable)) group))
2986             ;; Just expire using the normal expiry values.
2987             (gnus-request-expire-articles
2988              (gnus-uncompress-sequence (cdr expirable)) group))))
2989         (gnus-close-group group))
2990       (gnus-message 6 "Expiring articles in %s...done" group)
2991       ;; Return the list of un-expired articles.
2992       (cdr expirable))))
2993
2994 (defun gnus-group-expire-all-groups ()
2995   "Expire all expirable articles in all newsgroups."
2996   (interactive)
2997   (save-excursion
2998     (gnus-message 5 "Expiring...")
2999     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
3000                                      (cdr gnus-newsrc-alist))))
3001       (gnus-group-expire-articles nil)))
3002   (gnus-group-position-point)
3003   (gnus-message 5 "Expiring...done"))
3004
3005 (defun gnus-group-set-current-level (n level)
3006   "Set the level of the next N groups to LEVEL."
3007   (interactive
3008    (list
3009     current-prefix-arg
3010     (string-to-int
3011      (let ((s (read-string
3012                (format "Level (default %s): "
3013                        (or (gnus-group-group-level)
3014                            gnus-level-default-subscribed)))))
3015        (if (string-match "^\\s-*$" s)
3016            (int-to-string (or (gnus-group-group-level)
3017                               gnus-level-default-subscribed))
3018          s)))))
3019   (unless (and (>= level 1) (<= level gnus-level-killed))
3020     (error "Invalid level: %d" level))
3021   (let ((groups (gnus-group-process-prefix n))
3022         group)
3023     (while (setq group (pop groups))
3024       (gnus-group-remove-mark group)
3025       (gnus-message 6 "Changed level of %s from %d to %d"
3026                     group (or (gnus-group-group-level) gnus-level-killed)
3027                     level)
3028       (gnus-group-change-level
3029        group level (or (gnus-group-group-level) gnus-level-killed))
3030       (gnus-group-update-group-line)))
3031   (gnus-group-position-point))
3032
3033 (defun gnus-group-unsubscribe (&optional n)
3034   "Unsubscribe the current group."
3035   (interactive "P")
3036   (gnus-group-unsubscribe-current-group n 'unsubscribe))
3037
3038 (defun gnus-group-subscribe (&optional n)
3039   "Subscribe the current group."
3040   (interactive "P")
3041   (gnus-group-unsubscribe-current-group n 'subscribe))
3042
3043 (defun gnus-group-unsubscribe-current-group (&optional n do-sub)
3044   "Toggle subscription of the current group.
3045 If given numerical prefix, toggle the N next groups."
3046   (interactive "P")
3047   (let ((groups (gnus-group-process-prefix n))
3048         group)
3049     (while groups
3050       (setq group (car groups)
3051             groups (cdr groups))
3052       (gnus-group-remove-mark group)
3053       (gnus-group-unsubscribe-group
3054        group
3055        (cond
3056         ((eq do-sub 'unsubscribe)
3057          gnus-level-default-unsubscribed)
3058         ((eq do-sub 'subscribe)
3059          gnus-level-default-subscribed)
3060         ((<= (gnus-group-group-level) gnus-level-subscribed)
3061          gnus-level-default-unsubscribed)
3062         (t
3063          gnus-level-default-subscribed))
3064        t)
3065       (gnus-group-update-group-line))
3066     (gnus-group-next-group 1)))
3067
3068 (defun gnus-group-unsubscribe-group (group &optional level silent)
3069   "Toggle subscription to GROUP.
3070 Killed newsgroups are subscribed.  If SILENT, don't try to update the
3071 group line."
3072   (interactive
3073    (list (completing-read
3074           "Group: " gnus-active-hashtb nil
3075           (gnus-read-active-file-p)
3076           nil
3077           'gnus-group-history)))
3078   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
3079     (cond
3080      ((string-match "^[ \t]*$" group)
3081       (error "Empty group name"))
3082      (newsrc
3083       ;; Toggle subscription flag.
3084       (gnus-group-change-level
3085        newsrc (if level level (if (<= (gnus-info-level (nth 2 newsrc))
3086                                       gnus-level-subscribed)
3087                                   (1+ gnus-level-subscribed)
3088                                 gnus-level-default-subscribed)))
3089       (unless silent
3090         (gnus-group-update-group group)))
3091      ((and (stringp group)
3092            (or (not (gnus-read-active-file-p))
3093                (gnus-active group)))
3094       ;; Add new newsgroup.
3095       (gnus-group-change-level
3096        group
3097        (if level level gnus-level-default-subscribed)
3098        (or (and (member group gnus-zombie-list)
3099                 gnus-level-zombie)
3100            gnus-level-killed)
3101        (when (gnus-group-group-name)
3102          (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
3103       (unless silent
3104         (gnus-group-update-group group)))
3105      (t (error "No such newsgroup: %s" group)))
3106     (gnus-group-position-point)))
3107
3108 (defun gnus-group-transpose-groups (n)
3109   "Move the current newsgroup up N places.
3110 If given a negative prefix, move down instead.  The difference between
3111 N and the number of steps taken is returned."
3112   (interactive "p")
3113   (unless (gnus-group-group-name)
3114     (error "No group on current line"))
3115   (gnus-group-kill-group 1)
3116   (prog1
3117       (forward-line (- n))
3118     (gnus-group-yank-group)
3119     (gnus-group-position-point)))
3120
3121 (defun gnus-group-kill-all-zombies (&optional dummy)
3122   "Kill all zombie newsgroups.
3123 The optional DUMMY should always be nil."
3124   (interactive (list (not (gnus-yes-or-no-p "Really kill all zombies? "))))
3125   (unless dummy
3126     (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
3127     (setq gnus-zombie-list nil)
3128     (gnus-dribble-touch)
3129     (gnus-group-list-groups)))
3130
3131 (defun gnus-group-kill-region (begin end)
3132   "Kill newsgroups in current region (excluding current point).
3133 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
3134   (interactive "r")
3135   (let ((lines
3136          ;; Count lines.
3137          (save-excursion
3138            (count-lines
3139             (progn
3140               (goto-char begin)
3141               (beginning-of-line)
3142               (point))
3143             (progn
3144               (goto-char end)
3145               (beginning-of-line)
3146               (point))))))
3147     (goto-char begin)
3148     (beginning-of-line)                 ;Important when LINES < 1
3149     (gnus-group-kill-group lines)))
3150
3151 (defun gnus-group-kill-group (&optional n discard)
3152   "Kill the next N groups.
3153 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
3154 However, only groups that were alive can be yanked; already killed
3155 groups or zombie groups can't be yanked.
3156 The return value is the name of the group that was killed, or a list
3157 of groups killed."
3158   (interactive "P")
3159   (let ((buffer-read-only nil)
3160         (groups (gnus-group-process-prefix n))
3161         group entry level out)
3162     (if (< (length groups) 10)
3163         ;; This is faster when there are few groups.
3164         (while groups
3165           (push (setq group (pop groups)) out)
3166           (gnus-group-remove-mark group)
3167           (setq level (gnus-group-group-level))
3168           (gnus-delete-line)
3169           (when (and (not discard)
3170                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
3171             (gnus-undo-register
3172               `(progn
3173                  (gnus-group-goto-group ,(gnus-group-group-name))
3174                  (gnus-group-yank-group)))
3175             (push (cons (car entry) (nth 2 entry))
3176                   gnus-list-of-killed-groups))
3177           (gnus-group-change-level
3178            (if entry entry group) gnus-level-killed (if entry nil level))
3179           (message "Killed group %s" group))
3180       ;; If there are lots and lots of groups to be killed, we use
3181       ;; this thing instead.
3182       (let (entry)
3183         (setq groups (nreverse groups))
3184         (while groups
3185           (gnus-group-remove-mark (setq group (pop groups)))
3186           (gnus-delete-line)
3187           (push group gnus-killed-list)
3188           (setq gnus-newsrc-alist
3189                 (delq (assoc group gnus-newsrc-alist)
3190                       gnus-newsrc-alist))
3191           (when gnus-group-change-level-function
3192             (funcall gnus-group-change-level-function
3193                      group gnus-level-killed 3))
3194           (cond
3195            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
3196             (push (cons (car entry) (nth 2 entry))
3197                   gnus-list-of-killed-groups)
3198             (setcdr (cdr entry) (cdddr entry)))
3199            ((member group gnus-zombie-list)
3200             (setq gnus-zombie-list (delete group gnus-zombie-list))))
3201           ;; There may be more than one instance displayed.
3202           (while (gnus-group-goto-group group)
3203             (gnus-delete-line)))
3204         (gnus-make-hashtable-from-newsrc-alist)))
3205
3206     (gnus-group-position-point)
3207     (if (< (length out) 2) (car out) (nreverse out))))
3208
3209 (defun gnus-group-yank-group (&optional arg)
3210   "Yank the last newsgroups killed with \\[gnus-group-kill-group], inserting it before the current newsgroup.
3211 The numeric ARG specifies how many newsgroups are to be yanked.  The
3212 name of the newsgroup yanked is returned, or (if several groups are
3213 yanked) a list of yanked groups is returned."
3214   (interactive "p")
3215   (setq arg (or arg 1))
3216   (let (info group prev out)
3217     (while (>= (decf arg) 0)
3218       (when (not (setq info (pop gnus-list-of-killed-groups)))
3219         (error "No more newsgroups to yank"))
3220       (push (setq group (nth 1 info)) out)
3221       ;; Find which newsgroup to insert this one before - search
3222       ;; backward until something suitable is found.  If there are no
3223       ;; other newsgroups in this buffer, just make this newsgroup the
3224       ;; first newsgroup.
3225       (setq prev (gnus-group-group-name))
3226       (gnus-group-change-level
3227        info (gnus-info-level (cdr info)) gnus-level-killed
3228        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
3229        t)
3230       (gnus-group-insert-group-line-info group)
3231       (gnus-undo-register
3232         `(when (gnus-group-goto-group ,group)
3233            (gnus-group-kill-group 1))))
3234     (forward-line -1)
3235     (gnus-group-position-point)
3236     (if (< (length out) 2) (car out) (nreverse out))))
3237
3238 (defun gnus-group-kill-level (level)
3239   "Kill all groups that is on a certain LEVEL."
3240   (interactive "nKill all groups on level: ")
3241   (cond
3242    ((= level gnus-level-zombie)
3243     (setq gnus-killed-list
3244           (nconc gnus-zombie-list gnus-killed-list))
3245     (setq gnus-zombie-list nil))
3246    ((and (< level gnus-level-zombie)
3247          (> level 0)
3248          (or gnus-expert-user
3249              (gnus-yes-or-no-p
3250               (format
3251                "Do you really want to kill all groups on level %d? "
3252                level))))
3253     (let* ((prev gnus-newsrc-alist)
3254            (alist (cdr prev)))
3255       (while alist
3256         (if (= (gnus-info-level (car alist)) level)
3257             (progn
3258               (push (gnus-info-group (car alist)) gnus-killed-list)
3259               (setcdr prev (cdr alist)))
3260           (setq prev alist))
3261         (setq alist (cdr alist)))
3262       (gnus-make-hashtable-from-newsrc-alist)
3263       (gnus-group-list-groups)))
3264    (t
3265     (error "Can't kill; invalid level: %d" level))))
3266
3267 (defun gnus-group-list-all-groups (&optional arg)
3268   "List all newsgroups with level ARG or lower.
3269 Default is gnus-level-unsubscribed, which lists all subscribed and most
3270 unsubscribed groups."
3271   (interactive "P")
3272   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
3273
3274 ;; Redefine this to list ALL killed groups if prefix arg used.
3275 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
3276 (defun gnus-group-list-killed (&optional arg)
3277   "List all killed newsgroups in the group buffer.
3278 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
3279 entail asking the server for the groups."
3280   (interactive "P")
3281   ;; Find all possible killed newsgroups if arg.
3282   (when arg
3283     (gnus-get-killed-groups))
3284   (if (not gnus-killed-list)
3285       (gnus-message 6 "No killed groups")
3286     (let (gnus-group-list-mode)
3287       (funcall gnus-group-prepare-function
3288                gnus-level-killed t gnus-level-killed))
3289     (goto-char (point-min)))
3290   (gnus-group-position-point))
3291
3292 (defun gnus-group-list-zombies ()
3293   "List all zombie newsgroups in the group buffer."
3294   (interactive)
3295   (if (not gnus-zombie-list)
3296       (gnus-message 6 "No zombie groups")
3297     (let (gnus-group-list-mode)
3298       (funcall gnus-group-prepare-function
3299                gnus-level-zombie t gnus-level-zombie))
3300     (goto-char (point-min)))
3301   (gnus-group-position-point))
3302
3303 (defun gnus-group-list-active ()
3304   "List all groups that are available from the server(s)."
3305   (interactive)
3306   ;; First we make sure that we have really read the active file.
3307   (unless (gnus-read-active-file-p)
3308     (let ((gnus-read-active-file t)
3309           (gnus-agent nil))             ; Trick the agent into ignoring the active file.
3310       (gnus-read-active-file)))
3311   ;; Find all groups and sort them.
3312   (let ((groups
3313          (sort
3314           (let (list)
3315             (mapatoms
3316              (lambda (sym)
3317                (and (boundp sym)
3318                     (symbol-value sym)
3319                     (push (symbol-name sym) list)))
3320              gnus-active-hashtb)
3321             list)
3322           'string<))
3323         (buffer-read-only nil)
3324         group)
3325     (erase-buffer)
3326     (while groups
3327       (setq group (pop groups))
3328       (gnus-add-text-properties
3329        (point) (prog1 (1+ (point))
3330                  (insert "       *: "
3331                          (gnus-group-decoded-name group)
3332                          "\n"))
3333        (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
3334              'gnus-unread t
3335              'gnus-level (inline (gnus-group-level group)))))
3336     (goto-char (point-min))))
3337
3338 (defun gnus-activate-all-groups (level)
3339   "Activate absolutely all groups."
3340   (interactive (list gnus-level-unsubscribed))
3341   (let ((gnus-activate-level level)
3342         (gnus-activate-foreign-newsgroups level))
3343     (gnus-group-get-new-news)))
3344
3345 (defun gnus-group-get-new-news (&optional arg)
3346   "Get newly arrived articles.
3347 If ARG is a number, it specifies which levels you are interested in
3348 re-scanning.  If ARG is non-nil and not a number, this will force
3349 \"hard\" re-reading of the active files from all servers."
3350   (interactive "P")
3351   (require 'nnmail)
3352   (let ((gnus-inhibit-demon t)
3353         ;; Binding this variable will inhibit multiple fetchings
3354         ;; of the same mail source.
3355         (nnmail-fetched-sources (list t)))
3356     (gnus-run-hooks 'gnus-get-new-news-hook)
3357
3358     ;; Read any slave files.
3359     (unless gnus-slave
3360       (gnus-master-read-slave-newsrc))
3361
3362     ;; We might read in new NoCeM messages here.
3363     (when (and gnus-use-nocem
3364                (null arg))
3365       (gnus-nocem-scan-groups))
3366     ;; If ARG is not a number, then we read the active file.
3367     (when (and arg (not (numberp arg)))
3368       (let ((gnus-read-active-file t))
3369         (gnus-read-active-file))
3370       (setq arg nil)
3371
3372       ;; If the user wants it, we scan for new groups.
3373       (when (eq gnus-check-new-newsgroups 'always)
3374         (gnus-find-new-newsgroups)))
3375
3376     (setq arg (gnus-group-default-level arg t))
3377     (if (and gnus-read-active-file (not arg))
3378         (progn
3379           (gnus-read-active-file)
3380           (gnus-get-unread-articles arg))
3381       (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
3382         (gnus-get-unread-articles arg)))
3383     (gnus-run-hooks 'gnus-after-getting-new-news-hook)
3384     (gnus-group-list-groups (and (numberp arg)
3385                                  (max (car gnus-group-list-mode) arg))))
3386   ;; Update modeline.
3387   (when (and gnus-agent (not (interactive-p)))
3388     (gnus-agent-toggle-plugged gnus-plugged)))
3389
3390 (defun gnus-group-get-new-news-this-group (&optional n dont-scan)
3391   "Check for newly arrived news in the current group (and the N-1 next groups).
3392 The difference between N and the number of newsgroup checked is returned.
3393 If N is negative, this group and the N-1 previous groups will be checked."
3394   (interactive "P")
3395   (let* ((groups (gnus-group-process-prefix n))
3396          (ret (if (numberp n) (- n (length groups)) 0))
3397          (beg (unless n
3398                 (point)))
3399          group method
3400          (gnus-inhibit-demon t)
3401          ;; Binding this variable will inhibit multiple fetchings
3402          ;; of the same mail source.
3403          (nnmail-fetched-sources (list t)))
3404     (gnus-run-hooks 'gnus-get-new-news-hook)
3405     (while (setq group (pop groups))
3406       (gnus-group-remove-mark group)
3407       ;; Bypass any previous denials from the server.
3408       (gnus-remove-denial (setq method (gnus-find-method-for-group group)))
3409       (if (gnus-activate-group group (if dont-scan nil 'scan))
3410           (progn
3411             (gnus-get-unread-articles-in-group
3412              (gnus-get-info group) (gnus-active group) t)
3413             (unless (gnus-virtual-group-p group)
3414               (gnus-close-group group))
3415             (when gnus-agent
3416               (gnus-agent-save-group-info
3417                method (gnus-group-real-name group) (gnus-active group)))
3418             (gnus-group-update-group group))
3419         (if (eq (gnus-server-status (gnus-find-method-for-group group))
3420                 'denied)
3421             (gnus-error 3 "Server denied access")
3422           (gnus-error 3 "%s error: %s" group (gnus-status-message group)))))
3423     (when beg
3424       (goto-char beg))
3425     (when gnus-goto-next-group-when-activating
3426       (gnus-group-next-unread-group 1 t))
3427     (gnus-summary-position-point)
3428     ret))
3429
3430 (defun gnus-group-fetch-faq (group &optional faq-dir)
3431   "Fetch the FAQ for the current group.
3432 If given a prefix argument, prompt for the FAQ dir
3433 to use."
3434   (interactive
3435    (list
3436     (gnus-group-group-name)
3437     (when current-prefix-arg
3438       (completing-read
3439        "Faq dir: " (and (listp gnus-group-faq-directory)
3440                         (mapcar #'list
3441                                 gnus-group-faq-directory))))))
3442   (unless group
3443     (error "No group name given"))
3444   (let ((dirs (or faq-dir gnus-group-faq-directory))
3445         dir found file)
3446     (unless (listp dirs)
3447       (setq dirs (list dirs)))
3448     (while (and (not found)
3449                 (setq dir (pop dirs)))
3450       (let ((name (gnus-group-real-name group)))
3451         (setq file (expand-file-name name dir)))
3452       (if (not (file-exists-p file))
3453           (gnus-message 1 "No such file: %s" file)
3454         (let ((enable-local-variables nil))
3455           (find-file file)
3456           (setq found t))))))
3457
3458 (defun gnus-group-describe-group (force &optional group)
3459   "Display a description of the current newsgroup."
3460   (interactive (list current-prefix-arg (gnus-group-group-name)))
3461   (let* ((method (gnus-find-method-for-group group))
3462          (mname (gnus-group-prefixed-name "" method))
3463          desc)
3464     (when (and force
3465                gnus-description-hashtb)
3466       (gnus-sethash mname nil gnus-description-hashtb))
3467     (unless group
3468       (error "No group name given"))
3469     (when (or (and gnus-description-hashtb
3470                    ;; We check whether this group's method has been
3471                    ;; queried for a description file.
3472                    (gnus-gethash mname gnus-description-hashtb))
3473               (setq desc (gnus-group-get-description group))
3474               (gnus-read-descriptions-file method))
3475       (gnus-message 1
3476                     (or desc (gnus-gethash group gnus-description-hashtb)
3477                         "No description available")))))
3478
3479 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
3480 (defun gnus-group-describe-all-groups (&optional force)
3481   "Pop up a buffer with descriptions of all newsgroups."
3482   (interactive "P")
3483   (when force
3484     (setq gnus-description-hashtb nil))
3485   (when (not (or gnus-description-hashtb
3486                  (gnus-read-all-descriptions-files)))
3487     (error "Couldn't request descriptions file"))
3488   (let ((buffer-read-only nil)
3489         b)
3490     (erase-buffer)
3491     (mapatoms
3492      (lambda (group)
3493        (setq b (point))
3494        (let ((charset (gnus-group-name-charset nil (symbol-name group))))
3495          (insert (format "      *: %-20s %s\n"
3496                          (gnus-group-name-decode
3497                           (symbol-name group) charset)
3498                          (gnus-group-name-decode
3499                           (symbol-value group) charset))))
3500        (gnus-add-text-properties
3501         b (1+ b) (list 'gnus-group group
3502                        'gnus-unread t 'gnus-marked nil
3503                        'gnus-level (1+ gnus-level-subscribed))))
3504      gnus-description-hashtb)
3505     (goto-char (point-min))
3506     (gnus-group-position-point)))
3507
3508 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
3509 (defun gnus-group-apropos (regexp &optional search-description)
3510   "List all newsgroups that have names that match a regexp."
3511   (interactive "sGnus apropos (regexp): ")
3512   (let ((prev "")
3513         (obuf (current-buffer))
3514         groups des)
3515     ;; Go through all newsgroups that are known to Gnus.
3516     (mapatoms
3517      (lambda (group)
3518        (and (symbol-name group)
3519             (string-match regexp (symbol-name group))
3520             (symbol-value group)
3521             (push (symbol-name group) groups)))
3522      gnus-active-hashtb)
3523     ;; Also go through all descriptions that are known to Gnus.
3524     (when search-description
3525       (mapatoms
3526        (lambda (group)
3527          (and (string-match regexp (symbol-value group))
3528               (push (symbol-name group) groups)))
3529        gnus-description-hashtb))
3530     (if (not groups)
3531         (gnus-message 3 "No groups matched \"%s\"." regexp)
3532       ;; Print out all the groups.
3533       (save-excursion
3534         (pop-to-buffer "*Gnus Help*")
3535         (buffer-disable-undo)
3536         (erase-buffer)
3537         (setq groups (sort groups 'string<))
3538         (while groups
3539           ;; Groups may be entered twice into the list of groups.
3540           (when (not (string= (car groups) prev))
3541             (setq prev (car groups))
3542             (let ((charset (gnus-group-name-charset nil prev)))
3543               (insert (gnus-group-name-decode prev charset) "\n")
3544               (when (and gnus-description-hashtb
3545                          (setq des (gnus-gethash (car groups)
3546                                                  gnus-description-hashtb)))
3547                 (insert "  " (gnus-group-name-decode des charset) "\n"))))
3548           (setq groups (cdr groups)))
3549         (goto-char (point-min))))
3550     (pop-to-buffer obuf)))
3551
3552 (defun gnus-group-description-apropos (regexp)
3553   "List all newsgroups that have names or descriptions that match a regexp."
3554   (interactive "sGnus description apropos (regexp): ")
3555   (when (not (or gnus-description-hashtb
3556                  (gnus-read-all-descriptions-files)))
3557     (error "Couldn't request descriptions file"))
3558   (gnus-group-apropos regexp t))
3559
3560 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
3561 (defun gnus-group-list-matching (level regexp &optional all lowest)
3562   "List all groups with unread articles that match REGEXP.
3563 If the prefix LEVEL is non-nil, it should be a number that says which
3564 level to cut off listing groups.
3565 If ALL, also list groups with no unread articles.
3566 If LOWEST, don't list groups with level lower than LOWEST.
3567
3568 This command may read the active file."
3569   (interactive "P\nsList newsgroups matching: ")
3570   ;; First make sure active file has been read.
3571   (when (and level
3572              (> (prefix-numeric-value level) gnus-level-killed))
3573     (gnus-get-killed-groups))
3574   (funcall gnus-group-prepare-function
3575            (or level gnus-level-subscribed) (and all t) (or lowest 1) regexp)
3576   (goto-char (point-min))
3577   (gnus-group-position-point))
3578
3579 (defun gnus-group-list-all-matching (level regexp &optional lowest)
3580   "List all groups that match REGEXP.
3581 If the prefix LEVEL is non-nil, it should be a number that says which
3582 level to cut off listing groups.
3583 If LOWEST, don't list groups with level lower than LOWEST."
3584   (interactive "P\nsList newsgroups matching: ")
3585   (when level
3586     (setq level (prefix-numeric-value level)))
3587   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
3588
3589 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
3590 (defun gnus-group-save-newsrc (&optional force)
3591   "Save the Gnus startup files.
3592 If FORCE, force saving whether it is necessary or not."
3593   (interactive "P")
3594   (gnus-save-newsrc-file force))
3595
3596 (defun gnus-group-restart (&optional arg)
3597   "Force Gnus to read the .newsrc file."
3598   (interactive "P")
3599   (when (gnus-yes-or-no-p
3600          (format "Are you sure you want to restart Gnus? "))
3601     (gnus-save-newsrc-file)
3602     (gnus-clear-system)
3603     (gnus)))
3604
3605 (defun gnus-group-read-init-file ()
3606   "Read the Gnus elisp init file."
3607   (interactive)
3608   (gnus-read-init-file)
3609   (gnus-message 5 "Read %s" gnus-init-file))
3610
3611 (defun gnus-group-check-bogus-groups (&optional silent)
3612   "Check bogus newsgroups.
3613 If given a prefix, don't ask for confirmation before removing a bogus
3614 group."
3615   (interactive "P")
3616   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
3617   (gnus-group-list-groups))
3618
3619 (defun gnus-group-find-new-groups (&optional arg)
3620   "Search for new groups and add them.
3621 Each new group will be treated with `gnus-subscribe-newsgroup-method.'
3622 With 1 C-u, use the `ask-server' method to query the server for new
3623 groups.
3624 With 2 C-u's, use most complete method possible to query the server
3625 for new groups, and subscribe the new groups as zombies."
3626   (interactive "p")
3627   (gnus-find-new-newsgroups (or arg 1))
3628   (gnus-group-list-groups))
3629
3630 (defun gnus-group-edit-global-kill (&optional article group)
3631   "Edit the global kill file.
3632 If GROUP, edit that local kill file instead."
3633   (interactive "P")
3634   (setq gnus-current-kill-article article)
3635   (gnus-kill-file-edit-file group)
3636   (gnus-message
3637    6
3638    (substitute-command-keys
3639     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
3640             (if group "local" "global")))))
3641
3642 (defun gnus-group-edit-local-kill (article group)
3643   "Edit a local kill file."
3644   (interactive (list nil (gnus-group-group-name)))
3645   (gnus-group-edit-global-kill article group))
3646
3647 (defun gnus-group-force-update ()
3648   "Update `.newsrc' file."
3649   (interactive)
3650   (gnus-save-newsrc-file))
3651
3652 (defun gnus-group-suspend ()
3653   "Suspend the current Gnus session.
3654 In fact, cleanup buffers except for group mode buffer.
3655 The hook gnus-suspend-gnus-hook is called before actually suspending."
3656   (interactive)
3657   (gnus-run-hooks 'gnus-suspend-gnus-hook)
3658   (gnus-offer-save-summaries)
3659   ;; Kill Gnus buffers except for group mode buffer.
3660   (let ((group-buf (get-buffer gnus-group-buffer)))
3661     (mapcar (lambda (buf)
3662               (unless (member buf (list group-buf gnus-dribble-buffer))
3663                 (gnus-kill-buffer buf)))
3664             (gnus-buffers))
3665     (gnus-kill-gnus-frames)
3666     (when group-buf
3667       (bury-buffer group-buf)
3668       (delete-windows-on group-buf t))))
3669
3670 (defun gnus-group-clear-dribble ()
3671   "Clear all information from the dribble buffer."
3672   (interactive)
3673   (gnus-dribble-clear)
3674   (gnus-message 7 "Cleared dribble buffer"))
3675
3676 (defun gnus-group-exit ()
3677   "Quit reading news after updating .newsrc.eld and .newsrc.
3678 The hook `gnus-exit-gnus-hook' is called before actually exiting."
3679   (interactive)
3680   (when
3681       (or noninteractive                ;For gnus-batch-kill
3682           (not gnus-interactive-exit)   ;Without confirmation
3683           gnus-expert-user
3684           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
3685     (gnus-run-hooks 'gnus-exit-gnus-hook)
3686     ;; Offer to save data from non-quitted summary buffers.
3687     (gnus-offer-save-summaries)
3688     ;; Save the newsrc file(s).
3689     (gnus-save-newsrc-file)
3690     ;; Kill-em-all.
3691     (gnus-close-backends)
3692     ;; Reset everything.
3693     (gnus-clear-system)
3694     ;; Allow the user to do things after cleaning up.
3695     (gnus-run-hooks 'gnus-after-exiting-gnus-hook)))
3696
3697 (defun gnus-group-quit ()
3698   "Quit reading news without updating .newsrc.eld or .newsrc.
3699 The hook `gnus-exit-gnus-hook' is called before actually exiting."
3700   (interactive)
3701   (when (or noninteractive              ;For gnus-batch-kill
3702             (zerop (buffer-size))
3703             (not (gnus-server-opened gnus-select-method))
3704             gnus-expert-user
3705             (not gnus-current-startup-file)
3706             (gnus-yes-or-no-p
3707              (format "Quit reading news without saving %s? "
3708                      (file-name-nondirectory gnus-current-startup-file))))
3709     (gnus-run-hooks 'gnus-exit-gnus-hook)
3710     (gnus-configure-windows 'group t)
3711     (when (and (gnus-buffer-live-p gnus-dribble-buffer)
3712                (not (zerop (save-excursion
3713                              (set-buffer gnus-dribble-buffer)
3714                              (buffer-size)))))
3715       (gnus-dribble-enter
3716        ";;; Gnus was exited on purpose without saving the .newsrc files."))
3717     (gnus-dribble-save)
3718     (gnus-close-backends)
3719     (gnus-clear-system)
3720     (gnus-kill-buffer gnus-group-buffer)
3721     ;; Allow the user to do things after cleaning up.
3722     (gnus-run-hooks 'gnus-after-exiting-gnus-hook)))
3723
3724 (defun gnus-group-describe-briefly ()
3725   "Give a one line description of the group mode commands."
3726   (interactive)
3727   (gnus-message 7 (substitute-command-keys "\\<gnus-group-mode-map>\\[gnus-group-read-group]:Select  \\[gnus-group-next-unread-group]:Forward  \\[gnus-group-prev-unread-group]:Backward  \\[gnus-group-exit]:Exit  \\[gnus-info-find-node]:Run Info  \\[gnus-group-describe-briefly]:This help")))
3728
3729 (defun gnus-group-browse-foreign-server (method)
3730   "Browse a foreign news server.
3731 If called interactively, this function will ask for a select method
3732  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
3733 If not, METHOD should be a list where the first element is the method
3734 and the second element is the address."
3735   (interactive
3736    (list (let ((how (completing-read
3737                      "Which backend: "
3738                      (append gnus-valid-select-methods gnus-server-alist)
3739                      nil t (cons "nntp" 0) 'gnus-method-history)))
3740            ;; We either got a backend name or a virtual server name.
3741            ;; If the first, we also need an address.
3742            (if (assoc how gnus-valid-select-methods)
3743                (list (intern how)
3744                      ;; Suggested by mapjph@bath.ac.uk.
3745                      (completing-read
3746                       "Address: "
3747                       (mapcar (lambda (server) (list server))
3748                               gnus-secondary-servers)))
3749              ;; We got a server name.
3750              how))))
3751   (gnus-browse-foreign-server method))
3752
3753 (defun gnus-group-set-info (info &optional method-only-group part)
3754   (when (or info part)
3755     (let* ((entry (gnus-gethash
3756                    (or method-only-group (gnus-info-group info))
3757                    gnus-newsrc-hashtb))
3758            (part-info info)
3759            (info (if method-only-group (nth 2 entry) info))
3760            method)
3761       (when method-only-group
3762         (unless entry
3763           (error "Trying to change non-existent group %s" method-only-group))
3764         ;; We have received parts of the actual group info - either the
3765         ;; select method or the group parameters.        We first check
3766         ;; whether we have to extend the info, and if so, do that.
3767         (let ((len (length info))
3768               (total (if (eq part 'method) 5 6)))
3769           (when (< len total)
3770             (setcdr (nthcdr (1- len) info)
3771                     (make-list (- total len) nil)))
3772           ;; Then we enter the new info.
3773           (setcar (nthcdr (1- total) info) part-info)))
3774       (unless entry
3775         ;; This is a new group, so we just create it.
3776         (save-excursion
3777           (set-buffer gnus-group-buffer)
3778           (setq method (gnus-info-method info))
3779           (when (gnus-server-equal method "native")
3780             (setq method nil))
3781           (save-excursion
3782             (set-buffer gnus-group-buffer)
3783             (if method
3784                 ;; It's a foreign group...
3785                 (gnus-group-make-group
3786                  (gnus-group-real-name (gnus-info-group info))
3787                  (if (stringp method) method
3788                    (prin1-to-string (car method)))
3789                  (and (consp method)
3790                       (nth 1 (gnus-info-method info))))
3791               ;; It's a native group.
3792               (gnus-group-make-group (gnus-info-group info))))
3793           (gnus-message 6 "Note: New group created")
3794           (setq entry
3795                 (gnus-gethash (gnus-group-prefixed-name
3796                                (gnus-group-real-name (gnus-info-group info))
3797                                (or (gnus-info-method info) gnus-select-method))
3798                               gnus-newsrc-hashtb))))
3799       ;; Whether it was a new group or not, we now have the entry, so we
3800       ;; can do the update.
3801       (if entry
3802           (progn
3803             (setcar (nthcdr 2 entry) info)
3804             (when (and (not (eq (car entry) t))
3805                        (gnus-active (gnus-info-group info)))
3806               (setcar entry (length
3807                              (gnus-list-of-unread-articles (car info))))))
3808         (error "No such group: %s" (gnus-info-group info))))))
3809
3810 (defun gnus-group-set-method-info (group select-method)
3811   (gnus-group-set-info select-method group 'method))
3812
3813 (defun gnus-group-set-params-info (group params)
3814   (gnus-group-set-info params group 'params))
3815
3816 (defun gnus-add-marked-articles (group type articles &optional info force)
3817   ;; Add ARTICLES of TYPE to the info of GROUP.
3818   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
3819   ;; add, but replace marked articles of TYPE with ARTICLES.
3820   (let ((info (or info (gnus-get-info group)))
3821         marked m)
3822     (or (not info)
3823         (and (not (setq marked (nthcdr 3 info)))
3824              (or (null articles)
3825                  (setcdr (nthcdr 2 info)
3826                          (list (list (cons type (gnus-compress-sequence
3827                                                  articles t)))))))
3828         (and (not (setq m (assq type (car marked))))
3829              (or (null articles)
3830                  (setcar marked
3831                          (cons (cons type (gnus-compress-sequence articles t) )
3832                                (car marked)))))
3833         (if force
3834             (if (null articles)
3835                 (setcar (nthcdr 3 info)
3836                         (gnus-delete-alist type (car marked)))
3837               (setcdr m (gnus-compress-sequence articles t)))
3838           (setcdr m (gnus-compress-sequence
3839                      (sort (nconc (gnus-uncompress-range (cdr m))
3840                                   (copy-sequence articles)) '<) t))))))
3841
3842 (defun gnus-add-mark (group mark article)
3843   "Mark ARTICLE in GROUP with MARK, whether the group is displayed or not."
3844   (let ((buffer (gnus-summary-buffer-name group)))
3845     (if (gnus-buffer-live-p buffer)
3846         (save-excursion
3847           (set-buffer (get-buffer buffer))
3848           (gnus-summary-add-mark article mark))
3849       (gnus-add-marked-articles group (cdr (assq mark gnus-article-mark-lists))
3850                                 (list article)))))
3851
3852 ;;;
3853 ;;; Group timestamps
3854 ;;;
3855
3856 (defun gnus-group-set-timestamp ()
3857   "Change the timestamp of the current group to the current time.
3858 This function can be used in hooks like `gnus-select-group-hook'
3859 or `gnus-group-catchup-group-hook'."
3860   (when gnus-newsgroup-name
3861     (let ((time (current-time)))
3862       (setcdr (cdr time) nil)
3863       (gnus-group-set-parameter gnus-newsgroup-name 'timestamp time))))
3864
3865 (defsubst gnus-group-timestamp (group)
3866   "Return the timestamp for GROUP."
3867   (gnus-group-get-parameter group 'timestamp t))
3868
3869 (defun gnus-group-timestamp-delta (group)
3870   "Return the offset in seconds from the timestamp for GROUP to the current time, as a floating point number."
3871   (let* ((time (or (gnus-group-timestamp group)
3872                    (list 0 0)))
3873          (delta (subtract-time (current-time) time)))
3874     (+ (* (nth 0 delta) 65536.0)
3875        (nth 1 delta))))
3876
3877 (defun gnus-group-timestamp-string (group)
3878   "Return a string of the timestamp for GROUP."
3879   (let ((time (gnus-group-timestamp group)))
3880     (if (not time)
3881         ""
3882       (gnus-time-iso8601 time))))
3883
3884 (defun gnus-group-list-cached (level &optional lowest)
3885   "List all groups with cached articles.
3886 If the prefix LEVEL is non-nil, it should be a number that says which
3887 level to cut off listing groups.
3888 If LOWEST, don't list groups with level lower than LOWEST.
3889
3890 This command may read the active file."
3891   (interactive "P")
3892   (when level
3893     (setq level (prefix-numeric-value level)))
3894   (when (or (not level) (>= level gnus-level-zombie))
3895     (gnus-cache-open))
3896   (funcall gnus-group-prepare-function
3897            (or level gnus-level-subscribed)
3898            #'(lambda (info)
3899                (let ((marks (gnus-info-marks info)))
3900                  (assq 'cache marks)))
3901            lowest
3902            #'(lambda (group)
3903                (or (gnus-gethash group
3904                                  gnus-cache-active-hashtb)
3905                    ;; Cache active file might use "."
3906                    ;; instead of ":".
3907                    (gnus-gethash
3908                     (mapconcat 'identity
3909                                (split-string group ":")
3910                                ".")
3911                     gnus-cache-active-hashtb))))
3912   (goto-char (point-min))
3913   (gnus-group-position-point))
3914
3915 (defun gnus-group-list-dormant (level &optional lowest)
3916   "List all groups with dormant articles.
3917 If the prefix LEVEL is non-nil, it should be a number that says which
3918 level to cut off listing groups.
3919 If LOWEST, don't list groups with level lower than LOWEST.
3920
3921 This command may read the active file."
3922   (interactive "P")
3923   (when level
3924     (setq level (prefix-numeric-value level)))
3925   (when (or (not level) (>= level gnus-level-zombie))
3926     (gnus-cache-open))
3927   (funcall gnus-group-prepare-function
3928            (or level gnus-level-subscribed)
3929            #'(lambda (info)
3930                (let ((marks (gnus-info-marks info)))
3931                  (assq 'dormant marks)))
3932            lowest
3933            'ignore)
3934   (goto-char (point-min))
3935   (gnus-group-position-point))
3936
3937 (defun gnus-group-listed-groups ()
3938   "Return a list of listed groups."
3939   (let (point groups)
3940     (goto-char (point-min))
3941     (while (setq point (text-property-not-all (point) (point-max)
3942                                               'gnus-group nil))
3943       (goto-char point)
3944       (push (symbol-name (get-text-property point 'gnus-group)) groups)
3945       (forward-char 1))
3946     groups))
3947
3948 (defun gnus-group-list-plus (&optional args)
3949   "List groups plus the current selection."
3950   (interactive "P")
3951   (let ((gnus-group-listed-groups (gnus-group-listed-groups))
3952         (gnus-group-list-mode gnus-group-list-mode) ;; Save it.
3953         func)
3954     (push last-command-event unread-command-events)
3955     (if (featurep 'xemacs)
3956         (push (make-event 'key-press '(key ?A)) unread-command-events)
3957       (push ?A unread-command-events))
3958     (let (gnus-pick-mode keys)
3959       (setq keys (if (featurep 'xemacs)
3960                      (events-to-keys (read-key-sequence nil))
3961                    (read-key-sequence nil)))
3962       (setq func (lookup-key (current-local-map) keys)))
3963     (if (or (not func)
3964             (numberp func))
3965         (ding)
3966       (call-interactively func))))
3967
3968 (defun gnus-group-list-flush (&optional args)
3969   "Flush groups from the current selection."
3970   (interactive "P")
3971   (let ((gnus-group-list-option 'flush))
3972     (gnus-group-list-plus args)))
3973
3974 (defun gnus-group-list-limit (&optional args)
3975   "List groups limited within the current selection."
3976   (interactive "P")
3977   (let ((gnus-group-list-option 'limit))
3978     (gnus-group-list-plus args)))
3979
3980 (defun gnus-group-mark-article-read (group article)
3981   "Mark ARTICLE read."
3982   (gnus-activate-group group)
3983   (let ((buffer (gnus-summary-buffer-name group))
3984         (mark gnus-read-mark))
3985     (unless
3986         (and
3987          (get-buffer buffer)
3988          (with-current-buffer buffer
3989            (when gnus-newsgroup-prepared
3990              (when (and gnus-newsgroup-auto-expire
3991                         (memq mark gnus-auto-expirable-marks))
3992                (setq mark gnus-expirable-mark))
3993              (setq mark (gnus-request-update-mark
3994                          group article mark))
3995              (gnus-mark-article-as-read article mark)
3996              (setq gnus-newsgroup-active (gnus-active group))
3997              t)))
3998       (gnus-group-make-articles-read group
3999                                      (list article))
4000       (when (gnus-group-auto-expirable-p group)
4001         (gnus-add-marked-articles
4002          group 'expire (list article))))))
4003
4004 (provide 'gnus-group)
4005
4006 ;;; gnus-group.el ends here