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   '(;; News.
300     ((and (= unread 0) (not mailp) (eq level 1)) .
301      gnus-group-news-1-empty-face)
302     ((and (not mailp) (eq level 1)) .
303      gnus-group-news-1-face)
304     ((and (= unread 0) (not mailp) (eq level 2)) .
305      gnus-group-news-2-empty-face)
306     ((and (not mailp) (eq level 2)) .
307      gnus-group-news-2-face)
308     ((and (= unread 0) (not mailp) (eq level 3)) .
309      gnus-group-news-3-empty-face)
310     ((and (not mailp) (eq level 3)) .
311      gnus-group-news-3-face)
312     ((and (= unread 0) (not mailp) (eq level 4)) .
313      gnus-group-news-4-empty-face)
314     ((and (not mailp) (eq level 4)) .
315      gnus-group-news-4-face)
316     ((and (= unread 0) (not mailp) (eq level 5)) .
317      gnus-group-news-5-empty-face)
318     ((and (not mailp) (eq level 5)) .
319      gnus-group-news-5-face)
320     ((and (= unread 0) (not mailp) (eq level 6)) .
321      gnus-group-news-6-empty-face)
322     ((and (not mailp) (eq level 6)) .
323      gnus-group-news-6-face)
324     ((and (= unread 0) (not mailp)) .
325      gnus-group-news-low-empty-face)
326     ((and (not mailp)) .
327      gnus-group-news-low-face)
328     ;; Mail.
329     ((and (= unread 0) (eq level 1)) .
330      gnus-group-mail-1-empty-face)
331     ((eq level 1) .
332      gnus-group-mail-1-face)
333     ((and (= unread 0) (eq level 2)) .
334      gnus-group-mail-2-empty-face)
335     ((eq level 2) .
336      gnus-group-mail-2-face)
337     ((and (= unread 0) (eq level 3)) .
338      gnus-group-mail-3-empty-face)
339     ((eq level 3) .
340      gnus-group-mail-3-face)
341     ((= unread 0) .
342      gnus-group-mail-low-empty-face)
343     (t .
344        gnus-group-mail-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      `("Misc"
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-marked '("dummy.group"))
969           (gnus-active-hashtb (make-vector 10 0))
970           (topic ""))
971       (gnus-set-active "dummy.group" '(0 . 0))
972       (gnus-set-work-buffer)
973       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
974       (goto-char (point-min))
975       (setq gnus-group-mark-positions
976             (list (cons 'process (and (search-forward "\200" nil t)
977                                       (- (point) 2))))))))
978
979 (defun gnus-mouse-pick-group (e)
980   "Enter the group under the mouse pointer."
981   (interactive "e")
982   (mouse-set-point e)
983   (gnus-group-read-group nil))
984
985 ;; Look at LEVEL and find out what the level is really supposed to be.
986 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
987 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
988 (defun gnus-group-default-level (&optional level number-or-nil)
989   (cond
990    (gnus-group-use-permanent-levels
991     (or (setq gnus-group-use-permanent-levels
992               (or level (if (numberp gnus-group-use-permanent-levels)
993                             gnus-group-use-permanent-levels
994                           (or gnus-group-default-list-level
995                               gnus-level-subscribed))))
996         gnus-group-default-list-level gnus-level-subscribed))
997    (number-or-nil
998     level)
999    (t
1000     (or level gnus-group-default-list-level gnus-level-subscribed))))
1001
1002 (defun gnus-group-setup-buffer ()
1003   (set-buffer (gnus-get-buffer-create gnus-group-buffer))
1004   (unless (eq major-mode 'gnus-group-mode)
1005     (gnus-group-mode)
1006     (when gnus-carpal
1007       (gnus-carpal-setup-buffer 'group))))
1008
1009 (defsubst gnus-group-name-charset (method group)
1010   (if (null method)
1011       (setq method (gnus-find-method-for-group group)))
1012   (let ((item (assoc method gnus-group-name-charset-method-alist))
1013         (alist gnus-group-name-charset-group-alist)
1014         result)
1015     (if item
1016         (cdr item)
1017       (while (setq item (pop alist))
1018         (if (string-match (car item) group)
1019             (setq alist nil
1020                   result (cdr item))))
1021       result)))
1022
1023 (defsubst gnus-group-name-decode (string charset)
1024   (if (and string charset (featurep 'mule))
1025       (decode-coding-string string charset)
1026     string))
1027
1028 (defun gnus-group-decoded-name (string)
1029   (let ((charset (gnus-group-name-charset nil string)))
1030     (gnus-group-name-decode string charset)))
1031
1032 (defun gnus-group-list-groups (&optional level unread lowest)
1033   "List newsgroups with level LEVEL or lower that have unread articles.
1034 Default is all subscribed groups.
1035 If argument UNREAD is non-nil, groups with no unread articles are also
1036 listed.
1037
1038 Also see the `gnus-group-use-permanent-levels' variable."
1039   (interactive
1040    (list (if current-prefix-arg
1041              (prefix-numeric-value current-prefix-arg)
1042            (or
1043             (gnus-group-default-level nil t)
1044             gnus-group-default-list-level
1045             gnus-level-subscribed))))
1046   (unless level
1047     (setq level (car gnus-group-list-mode)
1048           unread (cdr gnus-group-list-mode)))
1049   (setq level (gnus-group-default-level level))
1050   (gnus-group-setup-buffer)
1051   (gnus-update-format-specifications nil 'group 'group-mode)
1052   (let ((case-fold-search nil)
1053         (props (text-properties-at (gnus-point-at-bol)))
1054         (empty (= (point-min) (point-max)))
1055         (group (gnus-group-group-name))
1056         number)
1057     (set-buffer gnus-group-buffer)
1058     (setq number (funcall gnus-group-prepare-function level unread lowest))
1059     (when (or (and (numberp number)
1060                    (zerop number))
1061               (zerop (buffer-size)))
1062       ;; No groups in the buffer.
1063       (gnus-message 5 gnus-no-groups-message))
1064     ;; We have some groups displayed.
1065     (goto-char (point-max))
1066     (when (or (not gnus-group-goto-next-group-function)
1067               (not (funcall gnus-group-goto-next-group-function
1068                             group props)))
1069       (cond
1070        (empty
1071         (goto-char (point-min)))
1072        ((not group)
1073         ;; Go to the first group with unread articles.
1074         (gnus-group-search-forward t))
1075        (t
1076         ;; Find the right group to put point on.  If the current group
1077         ;; has disappeared in the new listing, try to find the next
1078         ;; one.  If no next one can be found, just leave point at the
1079         ;; first newsgroup in the buffer.
1080         (when (not (gnus-goto-char
1081                     (text-property-any
1082                      (point-min) (point-max)
1083                      'gnus-group (gnus-intern-safe
1084                                   group gnus-active-hashtb))))
1085           (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
1086             (while (and newsrc
1087                         (not (gnus-goto-char
1088                               (text-property-any
1089                                (point-min) (point-max) 'gnus-group
1090                                (gnus-intern-safe
1091                                 (caar newsrc) gnus-active-hashtb)))))
1092               (setq newsrc (cdr newsrc)))
1093             (unless newsrc
1094               (goto-char (point-max))
1095               (forward-line -1)))))))
1096     ;; Adjust cursor point.
1097     (gnus-group-position-point)))
1098
1099 (defun gnus-group-list-level (level &optional all)
1100   "List groups on LEVEL.
1101 If ALL (the prefix), also list groups that have no unread articles."
1102   (interactive "nList groups on level: \nP")
1103   (gnus-group-list-groups level all level))
1104
1105 (defun gnus-group-prepare-logic (group test)
1106   (or (and gnus-group-listed-groups
1107            (null gnus-group-list-option)
1108            (member group gnus-group-listed-groups))
1109       (cond
1110        ((null gnus-group-listed-groups) test)
1111        ((null gnus-group-list-option) test)
1112        (t (and (member group gnus-group-listed-groups)
1113                (if (eq gnus-group-list-option 'flush)
1114                    (not test)
1115                  test))))))
1116
1117 (defun gnus-group-prepare-flat (level &optional predicate lowest regexp)
1118   "List all newsgroups with unread articles of level LEVEL or lower.
1119 If PREDICATE is a function, list groups that the function returns non-nil;
1120 if it is t, list groups that have no unread articles.
1121 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
1122 If REGEXP is a function, list dead groups that the function returns non-nil;
1123 if it is a string, only list groups matching REGEXP."
1124   (set-buffer gnus-group-buffer)
1125   (let ((buffer-read-only nil)
1126         (newsrc (cdr gnus-newsrc-alist))
1127         (lowest (or lowest 1))
1128         (not-in-list (and gnus-group-listed-groups
1129                           (copy-sequence gnus-group-listed-groups)))
1130         info clevel unread group params)
1131     (erase-buffer)
1132     (when (or (< lowest gnus-level-zombie)
1133               gnus-group-listed-groups)
1134       ;; List living groups.
1135       (while newsrc
1136         (setq info (car newsrc)
1137               group (gnus-info-group info)
1138               params (gnus-info-params info)
1139               newsrc (cdr newsrc)
1140               unread (car (gnus-gethash group gnus-newsrc-hashtb)))
1141         (if not-in-list
1142             (setq not-in-list (delete group not-in-list)))
1143         (and
1144          (gnus-group-prepare-logic
1145           group
1146           (and unread           ; This group might be unchecked
1147                (or (not (stringp regexp))
1148                    (string-match regexp group))
1149                (<= (setq clevel (gnus-info-level info)) level)
1150                (>= clevel lowest)
1151                (cond
1152                 ((functionp predicate)
1153                  (funcall predicate info))
1154                 (predicate t)           ; We list all groups?
1155                 (t
1156                  (or
1157                   (if (eq unread t)     ; Unactivated?
1158                       gnus-group-list-inactive-groups
1159                                         ; We list unactivated
1160                     (> unread 0))
1161                                         ; We list groups with unread articles
1162                   (and gnus-list-groups-with-ticked-articles
1163                        (cdr (assq 'tick (gnus-info-marks info))))
1164                                         ; And groups with tickeds
1165                   ;; Check for permanent visibility.
1166                   (and gnus-permanently-visible-groups
1167                        (string-match gnus-permanently-visible-groups group))
1168                   (memq 'visible params)
1169                   (cdr (assq 'visible params)))))))
1170          (gnus-group-insert-group-line
1171           group (gnus-info-level info)
1172           (gnus-info-marks info) unread (gnus-info-method info)))))
1173
1174     ;; List dead groups.
1175     (if (or gnus-group-listed-groups
1176             (and (>= level gnus-level-zombie)
1177                  (<= lowest gnus-level-zombie)))
1178         (gnus-group-prepare-flat-list-dead
1179          (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
1180          gnus-level-zombie ?Z
1181          regexp))
1182     (if not-in-list
1183         (dolist (group gnus-zombie-list)
1184           (setq not-in-list (delete group not-in-list))))
1185     (if (or gnus-group-listed-groups
1186             (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)))
1187         (gnus-group-prepare-flat-list-dead
1188          (gnus-union
1189           not-in-list
1190           (setq gnus-killed-list (sort gnus-killed-list 'string<)))
1191          gnus-level-killed ?K regexp))
1192
1193     (gnus-group-set-mode-line)
1194     (setq gnus-group-list-mode (cons level predicate))
1195     (gnus-run-hooks 'gnus-group-prepare-hook)
1196     t))
1197
1198 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
1199   ;; List zombies and killed lists somewhat faster, which was
1200   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
1201   ;; this by ignoring the group format specification altogether.
1202   (let (group)
1203     (if (> (length groups) gnus-group-listing-limit)
1204         (while groups
1205           (setq group (pop groups))
1206           (when (gnus-group-prepare-logic
1207                  group
1208                  (or (not regexp)
1209                      (and (stringp regexp) (string-match regexp group))
1210                      (and (functionp regexp) (funcall regexp group))))
1211             (gnus-add-text-properties
1212              (point) (prog1 (1+ (point))
1213                        (insert " " mark "     *: "
1214                                (gnus-group-name-decode group
1215                                                        (gnus-group-name-charset
1216                                                         nil group))
1217                                "\n"))
1218              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
1219                    'gnus-unread t
1220                    'gnus-level level))))
1221       (while groups
1222         (setq group (pop groups))
1223         (when (gnus-group-prepare-logic
1224                group
1225                (or (not regexp)
1226                    (and (stringp regexp) (string-match regexp group))
1227                    (and (functionp regexp) (funcall regexp group))))
1228           (gnus-group-insert-group-line
1229            group level nil
1230            (let ((active (gnus-active group)))
1231              (if active
1232                  (if (zerop (cdr active))
1233                      0
1234                    (- (1+ (cdr active)) (car active)))
1235                nil))
1236            (gnus-method-simplify (gnus-find-method-for-group group))))))))
1237
1238 (defun gnus-group-update-group-line ()
1239   "Update the current line in the group buffer."
1240   (let* ((buffer-read-only nil)
1241          (group (gnus-group-group-name))
1242          (entry (and group (gnus-gethash group gnus-newsrc-hashtb)))
1243          gnus-group-indentation)
1244     (when group
1245       (and entry
1246            (not (gnus-ephemeral-group-p group))
1247            (gnus-dribble-enter
1248             (concat "(gnus-group-set-info '"
1249                     (gnus-prin1-to-string (nth 2 entry))
1250                     ")")))
1251       (setq gnus-group-indentation (gnus-group-group-indentation))
1252       (gnus-delete-line)
1253       (gnus-group-insert-group-line-info group)
1254       (forward-line -1)
1255       (gnus-group-position-point))))
1256
1257 (defun gnus-group-insert-group-line-info (group)
1258   "Insert GROUP on the current line."
1259   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
1260         (gnus-group-indentation (gnus-group-group-indentation))
1261         active info)
1262     (if entry
1263         (progn
1264           ;; (Un)subscribed group.
1265           (setq info (nth 2 entry))
1266           (gnus-group-insert-group-line
1267            group (gnus-info-level info) (gnus-info-marks info)
1268            (or (car entry) t) (gnus-info-method info)))
1269       ;; This group is dead.
1270       (gnus-group-insert-group-line
1271        group
1272        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
1273        nil
1274        (if (setq active (gnus-active group))
1275            (if (zerop (cdr active))
1276                0
1277              (- (1+ (cdr active)) (car active)))
1278          nil)
1279        (gnus-method-simplify (gnus-find-method-for-group group))))))
1280
1281 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level
1282                                                     gnus-tmp-marked number
1283                                                     gnus-tmp-method)
1284   "Insert a group line in the group buffer."
1285   (let* ((gnus-tmp-method
1286           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
1287          (group-name-charset (gnus-group-name-charset gnus-tmp-method
1288                                                       gnus-tmp-group))
1289          (gnus-tmp-active (gnus-active gnus-tmp-group))
1290          (gnus-tmp-number-total
1291           (if gnus-tmp-active
1292               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
1293             0))
1294          (gnus-tmp-number-of-unread
1295           (if (numberp number) (int-to-string (max 0 number))
1296             "*"))
1297          (gnus-tmp-number-of-read
1298           (if (numberp number)
1299               (int-to-string (max 0 (- gnus-tmp-number-total number)))
1300             "*"))
1301          (gnus-tmp-subscribed
1302           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
1303                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
1304                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
1305                 (t ?K)))
1306          (gnus-tmp-qualified-group
1307           (gnus-group-name-decode (gnus-group-real-name gnus-tmp-group)
1308                                   group-name-charset))
1309          (gnus-tmp-newsgroup-description
1310           (if gnus-description-hashtb
1311               (or (gnus-group-name-decode
1312                    (gnus-gethash gnus-tmp-group gnus-description-hashtb)
1313                    group-name-charset) "")
1314             ""))
1315          (gnus-tmp-moderated
1316           (if (and gnus-moderated-hashtb
1317                    (gnus-gethash gnus-tmp-group gnus-moderated-hashtb))
1318               ?m ? ))
1319          (gnus-tmp-moderated-string
1320           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
1321          (gnus-tmp-group-icon "==&&==")
1322          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
1323          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
1324          (gnus-tmp-news-method-string
1325           (if gnus-tmp-method
1326               (format "(%s:%s)" (car gnus-tmp-method)
1327                       (cadr gnus-tmp-method)) ""))
1328          (gnus-tmp-marked-mark
1329           (if (and (numberp number)
1330                    (zerop number)
1331                    (cdr (assq 'tick gnus-tmp-marked)))
1332               ?* ? ))
1333          (gnus-tmp-process-marked
1334           (if (member gnus-tmp-group gnus-group-marked)
1335               gnus-process-mark ? ))
1336          (gnus-tmp-grouplens
1337           (or (and gnus-use-grouplens
1338                    (bbb-grouplens-group-p gnus-tmp-group))
1339               ""))
1340          (buffer-read-only nil)
1341          header gnus-tmp-header)        ; passed as parameter to user-funcs.
1342     (beginning-of-line)
1343     (gnus-add-text-properties
1344      (point)
1345      (prog1 (1+ (point))
1346        ;; Insert the text.
1347        (eval gnus-group-line-format-spec))
1348      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
1349                   gnus-unread ,(if (numberp number)
1350                                    (string-to-int gnus-tmp-number-of-unread)
1351                                  t)
1352                   gnus-marked ,gnus-tmp-marked-mark
1353                   gnus-indentation ,gnus-group-indentation
1354                   gnus-level ,gnus-tmp-level))
1355     (forward-line -1)
1356     (when (inline (gnus-visual-p 'group-highlight 'highlight))
1357       (gnus-run-hooks 'gnus-group-update-hook))
1358     (forward-line)
1359     ;; Allow XEmacs to remove front-sticky text properties.
1360     (gnus-group-remove-excess-properties)))
1361
1362 (defun gnus-group-highlight-line ()
1363   "Highlight the current line according to `gnus-group-highlight'."
1364   (let* ((list gnus-group-highlight)
1365          (p (point))
1366          (end (progn (end-of-line) (point)))
1367          ;; now find out where the line starts and leave point there.
1368          (beg (progn (beginning-of-line) (point)))
1369          (group (gnus-group-group-name))
1370          (entry (gnus-group-entry group))
1371          (unread (if (numberp (car entry)) (car entry) 0))
1372          (active (gnus-active group))
1373          (total (if active (1+ (- (cdr active) (car active))) 0))
1374          (info (nth 2 entry))
1375          (method (gnus-server-get-method group (gnus-info-method info)))
1376          (marked (gnus-info-marks info))
1377          (mailp (memq 'mail (assoc (symbol-name
1378                                     (car (or method gnus-select-method)))
1379                                    gnus-valid-select-methods)))
1380          (level (or (gnus-info-level info) gnus-level-killed))
1381          (score (or (gnus-info-score info) 0))
1382          (ticked (gnus-range-length (cdr (assq 'tick marked))))
1383          (group-age (gnus-group-timestamp-delta group))
1384          (inhibit-read-only t))
1385     ;; Eval the cars of the lists until we find a match.
1386     (while (and list
1387                 (not (eval (caar list))))
1388       (setq list (cdr list)))
1389     (let ((face (cdar list)))
1390       (unless (eq face (get-text-property beg 'face))
1391         (gnus-put-text-property-excluding-characters-with-faces
1392          beg end 'face
1393          (setq face (if (boundp face) (symbol-value face) face)))
1394         (gnus-extent-start-open beg)))
1395     (goto-char p)))
1396
1397 (defun gnus-group-update-group (group &optional visible-only)
1398   "Update all lines where GROUP appear.
1399 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
1400 already."
1401   ;; Can't use `save-excursion' here, so we do it manually.
1402   (let ((buf (current-buffer))
1403         mark)
1404     (set-buffer gnus-group-buffer)
1405     (setq mark (point-marker))
1406     ;; The buffer may be narrowed.
1407     (save-restriction
1408       (widen)
1409       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
1410             (loc (point-min))
1411             found buffer-read-only)
1412         ;; Enter the current status into the dribble buffer.
1413         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
1414           (when (and entry
1415                      (not (gnus-ephemeral-group-p group)))
1416             (gnus-dribble-enter
1417              (concat "(gnus-group-set-info '"
1418                      (gnus-prin1-to-string (nth 2 entry))
1419                      ")"))))
1420         ;; Find all group instances.  If topics are in use, each group
1421         ;; may be listed in more than once.
1422         (while (setq loc (text-property-any
1423                           loc (point-max) 'gnus-group ident))
1424           (setq found t)
1425           (goto-char loc)
1426           (let ((gnus-group-indentation (gnus-group-group-indentation)))
1427             (gnus-delete-line)
1428             (gnus-group-insert-group-line-info group)
1429             (save-excursion
1430               (forward-line -1)
1431               (gnus-run-hooks 'gnus-group-update-group-hook)))
1432           (setq loc (1+ loc)))
1433         (unless (or found visible-only)
1434           ;; No such line in the buffer, find out where it's supposed to
1435           ;; go, and insert it there (or at the end of the buffer).
1436           (if gnus-goto-missing-group-function
1437               (funcall gnus-goto-missing-group-function group)
1438             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
1439               (while (and entry (car entry)
1440                           (not
1441                            (gnus-goto-char
1442                             (text-property-any
1443                              (point-min) (point-max)
1444                              'gnus-group (gnus-intern-safe
1445                                           (caar entry) gnus-active-hashtb)))))
1446                 (setq entry (cdr entry)))
1447               (or entry (goto-char (point-max)))))
1448           ;; Finally insert the line.
1449           (let ((gnus-group-indentation (gnus-group-group-indentation)))
1450             (gnus-group-insert-group-line-info group)
1451             (save-excursion
1452               (forward-line -1)
1453               (gnus-run-hooks 'gnus-group-update-group-hook))))
1454         (when gnus-group-update-group-function
1455           (funcall gnus-group-update-group-function group))
1456         (gnus-group-set-mode-line)))
1457     (goto-char mark)
1458     (set-marker mark nil)
1459     (set-buffer buf)))
1460
1461 (defun gnus-group-set-mode-line ()
1462   "Update the mode line in the group buffer."
1463   (when (memq 'group gnus-updated-mode-lines)
1464     ;; Yes, we want to keep this mode line updated.
1465     (save-excursion
1466       (set-buffer gnus-group-buffer)
1467       (let* ((gformat (or gnus-group-mode-line-format-spec
1468                           (gnus-set-format 'group-mode)))
1469              (gnus-tmp-news-server (cadr gnus-select-method))
1470              (gnus-tmp-news-method (car gnus-select-method))
1471              (gnus-tmp-colon (if (equal gnus-tmp-news-server "") "" ":"))
1472              (max-len 60)
1473              gnus-tmp-header            ;Dummy binding for user-defined formats
1474              ;; Get the resulting string.
1475              (modified
1476               (and gnus-dribble-buffer
1477                    (buffer-name gnus-dribble-buffer)
1478                    (buffer-modified-p gnus-dribble-buffer)
1479                    (save-excursion
1480                      (set-buffer gnus-dribble-buffer)
1481                      (not (zerop (buffer-size))))))
1482              (mode-string (eval gformat)))
1483         ;; Say whether the dribble buffer has been modified.
1484         (setq mode-line-modified
1485               (if modified (car gnus-mode-line-modified)
1486                 (cdr gnus-mode-line-modified)))
1487         ;; If the line is too long, we chop it off.
1488         (when (> (length mode-string) max-len)
1489           (setq mode-string (substring mode-string 0 (- max-len 4))))
1490         (prog1
1491             (setq mode-line-buffer-identification
1492                   (gnus-mode-line-buffer-identification
1493                    (list mode-string)))
1494           (set-buffer-modified-p modified))))))
1495
1496 (defun gnus-group-group-name ()
1497   "Get the name of the newsgroup on the current line."
1498   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
1499     (when group
1500       (symbol-name group))))
1501
1502 (defun gnus-group-group-level ()
1503   "Get the level of the newsgroup on the current line."
1504   (get-text-property (gnus-point-at-bol) 'gnus-level))
1505
1506 (defun gnus-group-group-indentation ()
1507   "Get the indentation of the newsgroup on the current line."
1508   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
1509       (and gnus-group-indentation-function
1510            (funcall gnus-group-indentation-function))
1511       ""))
1512
1513 (defun gnus-group-group-unread ()
1514   "Get the number of unread articles of the newsgroup on the current line."
1515   (get-text-property (gnus-point-at-bol) 'gnus-unread))
1516
1517 (defun gnus-group-new-mail (group)
1518   (if (nnmail-new-mail-p (gnus-group-real-name group))
1519       gnus-new-mail-mark
1520     ? ))
1521
1522 (defun gnus-group-level (group)
1523   "Return the estimated level of GROUP."
1524   (or (gnus-info-level (gnus-get-info group))
1525       (and (member group gnus-zombie-list) gnus-level-zombie)
1526       gnus-level-killed))
1527
1528 (defun gnus-group-search-forward (&optional backward all level first-too)
1529   "Find the next newsgroup with unread articles.
1530 If BACKWARD is non-nil, find the previous newsgroup instead.
1531 If ALL is non-nil, just find any newsgroup.
1532 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
1533 group exists.
1534 If FIRST-TOO, the current line is also eligible as a target."
1535   (let ((way (if backward -1 1))
1536         (low gnus-level-killed)
1537         (beg (point))
1538         pos found lev)
1539     (if (and backward (progn (beginning-of-line)) (bobp))
1540         nil
1541       (unless first-too
1542         (forward-line way))
1543       (while (and
1544               (not (eobp))
1545               (not (setq
1546                     found
1547                     (and
1548                      (get-text-property (point) 'gnus-group)
1549                      (or all
1550                          (and
1551                           (let ((unread
1552                                  (get-text-property (point) 'gnus-unread)))
1553                             (and (numberp unread) (> unread 0)))
1554                           (setq lev (get-text-property (point)
1555                                                        'gnus-level))
1556                           (<= lev gnus-level-subscribed)))
1557                      (or (not level)
1558                          (and (setq lev (get-text-property (point)
1559                                                            'gnus-level))
1560                               (or (= lev level)
1561                                   (and (< lev low)
1562                                        (< level lev)
1563                                        (progn
1564                                          (setq low lev)
1565                                          (setq pos (point))
1566                                          nil))))))))
1567               (zerop (forward-line way)))))
1568     (if found
1569         (progn (gnus-group-position-point) t)
1570       (goto-char (or pos beg))
1571       (and pos t))))
1572
1573 ;;; Gnus group mode commands
1574
1575 ;; Group marking.
1576
1577 (defun gnus-group-mark-line-p ()
1578   (save-excursion
1579     (beginning-of-line)
1580     (forward-char (or (cdr (assq 'process gnus-group-mark-positions)) 2))
1581     (eq (char-after) gnus-process-mark)))
1582
1583 (defun gnus-group-mark-group (n &optional unmark no-advance)
1584   "Mark the current group."
1585   (interactive "p")
1586   (let ((buffer-read-only nil)
1587         group)
1588     (while (and (> n 0)
1589                 (not (eobp)))
1590       (when (setq group (gnus-group-group-name))
1591         ;; Go to the mark position.
1592         (beginning-of-line)
1593         (forward-char (or (cdr (assq 'process gnus-group-mark-positions)) 2))
1594         (subst-char-in-region
1595          (point) (1+ (point)) (char-after)
1596          (if unmark
1597              (progn
1598                (setq gnus-group-marked (delete group gnus-group-marked))
1599                ? )
1600            (setq gnus-group-marked
1601                  (cons group (delete group gnus-group-marked)))
1602            gnus-process-mark)))
1603       (unless no-advance
1604         (gnus-group-next-group 1))
1605       (decf n))
1606     (gnus-summary-position-point)
1607     n))
1608
1609 (defun gnus-group-unmark-group (n)
1610   "Remove the mark from the current group."
1611   (interactive "p")
1612   (gnus-group-mark-group n 'unmark)
1613   (gnus-group-position-point))
1614
1615 (defun gnus-group-unmark-all-groups ()
1616   "Unmark all groups."
1617   (interactive)
1618   (let ((groups gnus-group-marked))
1619     (save-excursion
1620       (while groups
1621         (gnus-group-remove-mark (pop groups)))))
1622   (gnus-group-position-point))
1623
1624 (defun gnus-group-mark-region (unmark beg end)
1625   "Mark all groups between point and mark.
1626 If UNMARK, remove the mark instead."
1627   (interactive "P\nr")
1628   (let ((num (count-lines beg end)))
1629     (save-excursion
1630       (goto-char beg)
1631       (- num (gnus-group-mark-group num unmark)))))
1632
1633 (defun gnus-group-mark-buffer (&optional unmark)
1634   "Mark all groups in the buffer.
1635 If UNMARK, remove the mark instead."
1636   (interactive "P")
1637   (gnus-group-mark-region unmark (point-min) (point-max)))
1638
1639 (defun gnus-group-mark-regexp (regexp)
1640   "Mark all groups that match some regexp."
1641   (interactive "sMark (regexp): ")
1642   (let ((alist (cdr gnus-newsrc-alist))
1643         group)
1644     (while alist
1645       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
1646         (gnus-group-set-mark group))))
1647   (gnus-group-position-point))
1648
1649 (defun gnus-group-remove-mark (group &optional test-marked)
1650   "Remove the process mark from GROUP and move point there.
1651 Return nil if the group isn't displayed."
1652   (if (gnus-group-goto-group group nil test-marked)
1653       (save-excursion
1654         (gnus-group-mark-group 1 'unmark t)
1655         t)
1656     (setq gnus-group-marked
1657           (delete group gnus-group-marked))
1658     nil))
1659
1660 (defun gnus-group-set-mark (group)
1661   "Set the process mark on GROUP."
1662   (if (gnus-group-goto-group group)
1663       (save-excursion
1664         (gnus-group-mark-group 1 nil t))
1665     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
1666
1667 (defun gnus-group-universal-argument (arg &optional groups func)
1668   "Perform any command on all groups according to the process/prefix convention."
1669   (interactive "P")
1670   (if (eq (setq func (or func
1671                          (key-binding
1672                           (read-key-sequence
1673                            (substitute-command-keys
1674                             "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
1675           'undefined)
1676       (gnus-error 1 "Undefined key")
1677     (gnus-group-iterate arg
1678       (lambda (group)
1679         (command-execute func))))
1680   (gnus-group-position-point))
1681
1682 (defun gnus-group-process-prefix (n)
1683   "Return a list of groups to work on.
1684 Take into consideration N (the prefix) and the list of marked groups."
1685   (cond
1686    (n
1687     (setq n (prefix-numeric-value n))
1688     ;; There is a prefix, so we return a list of the N next
1689     ;; groups.
1690     (let ((way (if (< n 0) -1 1))
1691           (n (abs n))
1692           group groups)
1693       (save-excursion
1694         (while (> n 0)
1695           (if (setq group (gnus-group-group-name))
1696               (push group groups))
1697           (setq n (1- n))
1698           (gnus-group-next-group way)))
1699       (nreverse groups)))
1700    ((gnus-region-active-p)
1701     ;; Work on the region between point and mark.
1702     (let ((max (max (point) (mark)))
1703           groups)
1704       (save-excursion
1705         (goto-char (min (point) (mark)))
1706         (while
1707             (and
1708              (push (gnus-group-group-name) groups)
1709              (zerop (gnus-group-next-group 1))
1710              (< (point) max)))
1711         (nreverse groups))))
1712    (gnus-group-marked
1713     ;; No prefix, but a list of marked articles.
1714     (reverse gnus-group-marked))
1715    (t
1716     ;; Neither marked articles or a prefix, so we return the
1717     ;; current group.
1718     (let ((group (gnus-group-group-name)))
1719       (and group (list group))))))
1720
1721 ;;; !!!Surely gnus-group-iterate should be a macro instead?  I can't
1722 ;;; imagine why I went through these contortions...
1723 (eval-and-compile
1724   (let ((function (make-symbol "gnus-group-iterate-function"))
1725         (window (make-symbol "gnus-group-iterate-window"))
1726         (groups (make-symbol "gnus-group-iterate-groups"))
1727         (group (make-symbol "gnus-group-iterate-group")))
1728     (eval
1729      `(defun gnus-group-iterate (arg ,function)
1730         "Iterate FUNCTION over all process/prefixed groups.
1731 FUNCTION will be called with the group name as the parameter
1732 and with point over the group in question."
1733         (let ((,groups (gnus-group-process-prefix arg))
1734               (,window (selected-window))
1735               ,group)
1736           (while ,groups
1737             (setq ,group (car ,groups)
1738                   ,groups (cdr ,groups))
1739             (select-window ,window)
1740             (gnus-group-remove-mark ,group)
1741             (save-selected-window
1742               (save-excursion
1743                 (funcall ,function ,group)))))))))
1744
1745 (put 'gnus-group-iterate 'lisp-indent-function 1)
1746
1747 ;; Selecting groups.
1748
1749 (defun gnus-group-read-group (&optional all no-article group select-articles)
1750   "Read news in this newsgroup.
1751 If the prefix argument ALL is non-nil, already read articles become
1752 readable.  IF ALL is a number, fetch this number of articles.  If the
1753 optional argument NO-ARTICLE is non-nil, no article will be
1754 auto-selected upon group entry.  If GROUP is non-nil, fetch that
1755 group."
1756   (interactive "P")
1757   (let ((no-display (eq all 0))
1758         (group (or group (gnus-group-group-name)))
1759         number active marked entry)
1760     (when (eq all 0)
1761       (setq all nil))
1762     (unless group
1763       (error "No group on current line"))
1764     (setq marked (gnus-info-marks
1765                   (nth 2 (setq entry (gnus-gethash
1766                                       group gnus-newsrc-hashtb)))))
1767     ;; This group might be a dead group.  In that case we have to get
1768     ;; the number of unread articles from `gnus-active-hashtb'.
1769     (setq number
1770           (cond ((numberp all) all)
1771                 (entry (car entry))
1772                 ((setq active (gnus-active group))
1773                  (- (1+ (cdr active)) (car active)))))
1774     (gnus-summary-read-group
1775      group (or all (and (numberp number)
1776                         (zerop (+ number (gnus-range-length
1777                                           (cdr (assq 'tick marked)))
1778                                   (gnus-range-length
1779                                    (cdr (assq 'dormant marked)))))))
1780      no-article nil no-display nil select-articles)))
1781
1782 (defun gnus-group-select-group (&optional all)
1783   "Select this newsgroup.
1784 No article is selected automatically.
1785 If ALL is non-nil, already read articles become readable.
1786 If ALL is a number, fetch this number of articles."
1787   (interactive "P")
1788   (gnus-group-read-group all t))
1789
1790 (defun gnus-group-quick-select-group (&optional all)
1791   "Select the current group \"quickly\".
1792 This means that no highlighting or scoring will be performed.
1793 If ALL (the prefix argument) is 0, don't even generate the summary
1794 buffer.
1795
1796 This might be useful if you want to toggle threading
1797 before entering the group."
1798   (interactive "P")
1799   (require 'gnus-score)
1800   (let (gnus-visual
1801         gnus-score-find-score-files-function
1802         gnus-home-score-file
1803         gnus-apply-kill-hook
1804         gnus-summary-expunge-below)
1805     (gnus-group-read-group all t)))
1806
1807 (defun gnus-group-visible-select-group (&optional all)
1808   "Select the current group without hiding any articles."
1809   (interactive "P")
1810   (let ((gnus-inhibit-limiting t))
1811     (gnus-group-read-group all t)))
1812
1813 (defun gnus-group-select-group-ephemerally ()
1814   "Select the current group without doing any processing whatsoever.
1815 You will actually be entered into a group that's a copy of
1816 the current group; no changes you make while in this group will
1817 be permanent."
1818   (interactive)
1819   (require 'gnus-score)
1820   (let* (gnus-visual
1821          gnus-score-find-score-files-function gnus-apply-kill-hook
1822          gnus-summary-expunge-below gnus-show-threads gnus-suppress-duplicates
1823          gnus-summary-mode-hook gnus-select-group-hook
1824          (group (gnus-group-group-name))
1825          (method (gnus-find-method-for-group group)))
1826     (gnus-group-read-ephemeral-group
1827      (gnus-group-prefixed-name group method) method)))
1828
1829 ;;;###autoload
1830 (defun gnus-fetch-group (group)
1831   "Start Gnus if necessary and enter GROUP.
1832 Returns whether the fetching was successful or not."
1833   (interactive (list (completing-read "Group name: " gnus-active-hashtb)))
1834   (unless (get-buffer gnus-group-buffer)
1835     (gnus-no-server))
1836   (gnus-group-read-group nil nil group))
1837
1838 ;;;###autoload
1839 (defun gnus-fetch-group-other-frame (group)
1840   "Pop up a frame and enter GROUP."
1841   (interactive "P")
1842   (let ((window (get-buffer-window gnus-group-buffer)))
1843     (cond (window
1844            (select-frame (window-frame window)))
1845           ((= (length (frame-list)) 1)
1846            (select-frame (make-frame)))
1847           (t
1848            (other-frame 1))))
1849   (gnus-fetch-group group))
1850
1851 (defvar gnus-ephemeral-group-server 0)
1852
1853 ;; Enter a group that is not in the group buffer.  Non-nil is returned
1854 ;; if selection was successful.
1855 (defun gnus-group-read-ephemeral-group (group method &optional activate
1856                                               quit-config request-only
1857                                               select-articles)
1858   "Read GROUP from METHOD as an ephemeral group.
1859 If ACTIVATE, request the group first.
1860 If QUIT-CONFIG, use that window configuration when exiting from the
1861 ephemeral group.
1862 If REQUEST-ONLY, don't actually read the group; just request it.
1863 If SELECT-ARTICLES, only select those articles.
1864
1865 Return the name of the group if selection was successful."
1866   ;; Transform the select method into a unique server.
1867   (when (stringp method)
1868     (setq method (gnus-server-to-method method)))
1869   (setq method
1870         `(,(car method) ,(concat (cadr method) "-ephemeral")
1871           (,(intern (format "%s-address" (car method))) ,(cadr method))
1872           ,@(cddr method)))
1873   (let ((group (if (gnus-group-foreign-p group) group
1874                  (gnus-group-prefixed-name group method))))
1875     (gnus-sethash
1876      group
1877      `(-1 nil (,group
1878                ,gnus-level-default-subscribed nil nil ,method
1879                ((quit-config .
1880                              ,(if quit-config quit-config
1881                                 (cons gnus-summary-buffer
1882                                       gnus-current-window-configuration))))))
1883      gnus-newsrc-hashtb)
1884     (push method gnus-ephemeral-servers)
1885     (set-buffer gnus-group-buffer)
1886     (unless (gnus-check-server method)
1887       (error "Unable to contact server: %s" (gnus-status-message method)))
1888     (when activate
1889       (gnus-activate-group group 'scan)
1890       (unless (gnus-request-group group)
1891         (error "Couldn't request group: %s"
1892                (nnheader-get-report (car method)))))
1893     (if request-only
1894         group
1895       (condition-case ()
1896           (when (gnus-group-read-group t t group select-articles)
1897             group)
1898         ;;(error nil)
1899         (quit
1900          (message "Quit reading the ephemeral group")
1901          nil)))))
1902
1903 (defun gnus-group-jump-to-group (group)
1904   "Jump to newsgroup GROUP."
1905   (interactive
1906    (list (completing-read
1907           "Group: " gnus-active-hashtb nil
1908           (gnus-read-active-file-p)
1909           gnus-group-jump-to-group-prompt
1910           'gnus-group-history)))
1911
1912   (when (equal group "")
1913     (error "Empty group name"))
1914
1915   (unless (gnus-ephemeral-group-p group)
1916     ;; Either go to the line in the group buffer...
1917     (unless (gnus-group-goto-group group)
1918       ;; ... or insert the line.
1919       (gnus-group-update-group group)
1920       (gnus-group-goto-group group)))
1921   ;; Adjust cursor point.
1922   (gnus-group-position-point))
1923
1924 (defun gnus-group-goto-group (group &optional far test-marked)
1925   "Goto to newsgroup GROUP.
1926 If FAR, it is likely that the group is not on the current line.
1927 If TEST-MARKED, the line must be marked."
1928   (when group
1929     (beginning-of-line)
1930     (cond
1931      ;; It's quite likely that we are on the right line, so
1932      ;; we check the current line first.
1933      ((and (not far)
1934            (eq (get-text-property (point) 'gnus-group)
1935                (gnus-intern-safe group gnus-active-hashtb))
1936            (or (not test-marked) (gnus-group-mark-line-p)))
1937       (point))
1938      ;; Previous and next line are also likely, so we check them as well.
1939      ((and (not far)
1940            (save-excursion
1941              (forward-line -1)
1942              (and (eq (get-text-property (point) 'gnus-group)
1943                       (gnus-intern-safe group gnus-active-hashtb))
1944                   (or (not test-marked) (gnus-group-mark-line-p)))))
1945       (forward-line -1)
1946       (point))
1947      ((and (not far)
1948            (save-excursion
1949              (forward-line 1)
1950              (and (eq (get-text-property (point) 'gnus-group)
1951                       (gnus-intern-safe group gnus-active-hashtb))
1952                   (or (not test-marked) (gnus-group-mark-line-p)))))
1953       (forward-line 1)
1954       (point))
1955      (test-marked
1956       (goto-char (point-min))
1957       (let (found)
1958         (while (and (not found)
1959                     (gnus-goto-char
1960                      (text-property-any
1961                       (point) (point-max)
1962                       'gnus-group
1963                       (gnus-intern-safe group gnus-active-hashtb))))
1964           (if (gnus-group-mark-line-p)
1965               (setq found t)
1966             (forward-line 1)))
1967         found))
1968      (t
1969       ;; Search through the entire buffer.
1970       (gnus-goto-char
1971        (text-property-any
1972         (point-min) (point-max)
1973         'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))))
1974
1975 (defun gnus-group-next-group (n &optional silent)
1976   "Go to next N'th newsgroup.
1977 If N is negative, search backward instead.
1978 Returns the difference between N and the number of skips actually
1979 done."
1980   (interactive "p")
1981   (gnus-group-next-unread-group n t nil silent))
1982
1983 (defun gnus-group-next-unread-group (n &optional all level silent)
1984   "Go to next N'th unread newsgroup.
1985 If N is negative, search backward instead.
1986 If ALL is non-nil, choose any newsgroup, unread or not.
1987 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
1988 such group can be found, the next group with a level higher than
1989 LEVEL.
1990 Returns the difference between N and the number of skips actually
1991 made."
1992   (interactive "p")
1993   (let ((backward (< n 0))
1994         (n (abs n)))
1995     (while (and (> n 0)
1996                 (gnus-group-search-forward
1997                  backward (or (not gnus-group-goto-unread) all) level))
1998       (setq n (1- n)))
1999     (when (and (/= 0 n)
2000                (not silent))
2001       (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
2002                     (if level " on this level or higher" "")))
2003     n))
2004
2005 (defun gnus-group-prev-group (n)
2006   "Go to previous N'th newsgroup.
2007 Returns the difference between N and the number of skips actually
2008 done."
2009   (interactive "p")
2010   (gnus-group-next-unread-group (- n) t))
2011
2012 (defun gnus-group-prev-unread-group (n)
2013   "Go to previous N'th unread newsgroup.
2014 Returns the difference between N and the number of skips actually
2015 done."
2016   (interactive "p")
2017   (gnus-group-next-unread-group (- n)))
2018
2019 (defun gnus-group-next-unread-group-same-level (n)
2020   "Go to next N'th unread newsgroup on the same level.
2021 If N is negative, search backward instead.
2022 Returns the difference between N and the number of skips actually
2023 done."
2024   (interactive "p")
2025   (gnus-group-next-unread-group n t (gnus-group-group-level))
2026   (gnus-group-position-point))
2027
2028 (defun gnus-group-prev-unread-group-same-level (n)
2029   "Go to next N'th unread newsgroup on the same level.
2030 Returns the difference between N and the number of skips actually
2031 done."
2032   (interactive "p")
2033   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
2034   (gnus-group-position-point))
2035
2036 (defun gnus-group-best-unread-group (&optional exclude-group)
2037   "Go to the group with the highest level.
2038 If EXCLUDE-GROUP, do not go to that group."
2039   (interactive)
2040   (goto-char (point-min))
2041   (let ((best 100000)
2042         unread best-point)
2043     (while (not (eobp))
2044       (setq unread (get-text-property (point) 'gnus-unread))
2045       (when (and (numberp unread) (> unread 0))
2046         (when (and (get-text-property (point) 'gnus-level)
2047                    (< (get-text-property (point) 'gnus-level) best)
2048                    (or (not exclude-group)
2049                        (not (equal exclude-group (gnus-group-group-name)))))
2050           (setq best (get-text-property (point) 'gnus-level))
2051           (setq best-point (point))))
2052       (forward-line 1))
2053     (when best-point
2054       (goto-char best-point))
2055     (gnus-summary-position-point)
2056     (and best-point (gnus-group-group-name))))
2057
2058 (defun gnus-group-first-unread-group ()
2059   "Go to the first group with unread articles."
2060   (interactive)
2061   (prog1
2062       (let ((opoint (point))
2063             unread)
2064         (goto-char (point-min))
2065         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
2066                 (and (numberp unread)   ; Not a topic.
2067                      (not (zerop unread))) ; Has unread articles.
2068                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
2069             (point)                     ; Success.
2070           (goto-char opoint)
2071           nil))                         ; Not success.
2072     (gnus-group-position-point)))
2073
2074 (defun gnus-group-enter-server-mode ()
2075   "Jump to the server buffer."
2076   (interactive)
2077   (gnus-enter-server-buffer))
2078
2079 (defun gnus-group-make-group (name &optional method address args)
2080   "Add a new newsgroup.
2081 The user will be prompted for a NAME, for a select METHOD, and an
2082 ADDRESS."
2083   (interactive
2084    (list
2085     (gnus-read-group "Group name: ")
2086     (gnus-read-method "From method: ")))
2087
2088   (when (stringp method)
2089     (setq method (or (gnus-server-to-method method) method)))
2090   (let* ((meth (gnus-method-simplify
2091                 (when (and method
2092                            (not (gnus-server-equal method gnus-select-method)))
2093                   (if address (list (intern method) address)
2094                     method))))
2095          (nname (if method (gnus-group-prefixed-name name meth) name))
2096          backend info)
2097     (when (gnus-gethash nname gnus-newsrc-hashtb)
2098       (error "Group %s already exists" nname))
2099     ;; Subscribe to the new group.
2100     (gnus-group-change-level
2101      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
2102      gnus-level-default-subscribed gnus-level-killed
2103      (and (gnus-group-group-name)
2104           (gnus-gethash (gnus-group-group-name)
2105                         gnus-newsrc-hashtb))
2106      t)
2107     ;; Make it active.
2108     (gnus-set-active nname (cons 1 0))
2109     (unless (gnus-ephemeral-group-p name)
2110       (gnus-dribble-enter
2111        (concat "(gnus-group-set-info '"
2112                (gnus-prin1-to-string (cdr info)) ")")))
2113     ;; Insert the line.
2114     (gnus-group-insert-group-line-info nname)
2115     (forward-line -1)
2116     (gnus-group-position-point)
2117
2118     ;; Load the backend and try to make the backend create
2119     ;; the group as well.
2120     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
2121                                                   nil meth))))
2122                  gnus-valid-select-methods)
2123       (require backend))
2124     (gnus-check-server meth)
2125     (when (gnus-check-backend-function 'request-create-group nname)
2126       (gnus-request-create-group nname nil args))
2127     t))
2128
2129 (defun gnus-group-delete-groups (&optional arg)
2130   "Delete the current group.  Only meaningful with editable groups."
2131   (interactive "P")
2132   (let ((n (length (gnus-group-process-prefix arg))))
2133     (when (gnus-yes-or-no-p
2134            (if (= n 1)
2135                "Delete this 1 group? "
2136              (format "Delete these %d groups? " n)))
2137       (gnus-group-iterate arg
2138         (lambda (group)
2139           (gnus-group-delete-group group nil t))))))
2140
2141 (defun gnus-group-delete-group (group &optional force no-prompt)
2142   "Delete the current group.  Only meaningful with editable groups.
2143 If FORCE (the prefix) is non-nil, all the articles in the group will
2144 be deleted.  This is \"deleted\" as in \"removed forever from the face
2145 of the Earth\".  There is no undo.  The user will be prompted before
2146 doing the deletion."
2147   (interactive
2148    (list (gnus-group-group-name)
2149          current-prefix-arg))
2150   (unless group
2151     (error "No group to rename"))
2152   (unless (gnus-check-backend-function 'request-delete-group group)
2153     (error "This backend does not support group deletion"))
2154   (prog1
2155       (if (and (not no-prompt)
2156                (not (gnus-yes-or-no-p
2157                      (format
2158                       "Do you really want to delete %s%s? "
2159                       group (if force " and all its contents" "")))))
2160           ()                            ; Whew!
2161         (gnus-message 6 "Deleting group %s..." group)
2162         (if (not (gnus-request-delete-group group force))
2163             (gnus-error 3 "Couldn't delete group %s" group)
2164           (gnus-message 6 "Deleting group %s...done" group)
2165           (gnus-group-goto-group group)
2166           (gnus-group-kill-group 1 t)
2167           (gnus-sethash group nil gnus-active-hashtb)
2168           t))
2169     (gnus-group-position-point)))
2170
2171 (defun gnus-group-rename-group (group new-name)
2172   "Rename group from GROUP to NEW-NAME.
2173 When used interactively, GROUP is the group under point
2174 and NEW-NAME will be prompted for."
2175   (interactive
2176    (list
2177     (gnus-group-group-name)
2178     (progn
2179       (unless (gnus-check-backend-function
2180                'request-rename-group (gnus-group-group-name))
2181         (error "This backend does not support renaming groups"))
2182       (gnus-read-group "Rename group to: "
2183                        (gnus-group-real-name (gnus-group-group-name))))))
2184
2185   (unless (gnus-check-backend-function 'request-rename-group group)
2186     (error "This backend does not support renaming groups"))
2187   (unless group
2188     (error "No group to rename"))
2189   (when (equal (gnus-group-real-name group) new-name)
2190     (error "Can't rename to the same name"))
2191
2192   ;; We find the proper prefixed name.
2193   (setq new-name
2194         (if (gnus-group-native-p group)
2195             ;; Native group.
2196             new-name
2197           ;; Foreign group.
2198           (gnus-group-prefixed-name
2199            (gnus-group-real-name new-name)
2200            (gnus-info-method (gnus-get-info group)))))
2201
2202   (gnus-message 6 "Renaming group %s to %s..." group new-name)
2203   (prog1
2204       (if (progn
2205             (gnus-group-goto-group group)
2206             (not (when (< (gnus-group-group-level) gnus-level-zombie)
2207                    (gnus-request-rename-group group new-name))))
2208           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
2209         ;; We rename the group internally by killing it...
2210         (gnus-group-kill-group)
2211         ;; ... changing its name ...
2212         (setcar (cdar gnus-list-of-killed-groups) new-name)
2213         ;; ... and then yanking it.  Magic!
2214         (gnus-group-yank-group)
2215         (gnus-set-active new-name (gnus-active group))
2216         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
2217         new-name)
2218     (setq gnus-killed-list (delete group gnus-killed-list))
2219     (gnus-set-active group nil)
2220     (gnus-dribble-touch)
2221     (gnus-group-position-point)))
2222
2223 (defun gnus-group-edit-group (group &optional part)
2224   "Edit the group on the current line."
2225   (interactive (list (gnus-group-group-name)))
2226   (let ((part (or part 'info))
2227         info)
2228     (unless group
2229       (error "No group on current line"))
2230     (unless (setq info (gnus-get-info group))
2231       (error "Killed group; can't be edited"))
2232     (ignore-errors
2233       (gnus-close-group group))
2234     (gnus-edit-form
2235      ;; Find the proper form to edit.
2236      (cond ((eq part 'method)
2237             (or (gnus-info-method info) "native"))
2238            ((eq part 'params)
2239             (gnus-info-params info))
2240            (t info))
2241      ;; The proper documentation.
2242      (format
2243       "Editing the %s for `%s'."
2244       (cond
2245        ((eq part 'method) "select method")
2246        ((eq part 'params) "group parameters")
2247        (t "group info"))
2248       (gnus-group-decoded-name group))
2249      `(lambda (form)
2250         (gnus-group-edit-group-done ',part ,group form)))))
2251
2252 (defun gnus-group-edit-group-method (group)
2253   "Edit the select method of GROUP."
2254   (interactive (list (gnus-group-group-name)))
2255   (gnus-group-edit-group group 'method))
2256
2257 (defun gnus-group-edit-group-parameters (group)
2258   "Edit the group parameters of GROUP."
2259   (interactive (list (gnus-group-group-name)))
2260   (gnus-group-edit-group group 'params))
2261
2262 (defun gnus-group-edit-group-done (part group form)
2263   "Update variables."
2264   (let* ((method (cond ((eq part 'info) (nth 4 form))
2265                        ((eq part 'method) form)
2266                        (t nil)))
2267          (info (cond ((eq part 'info) form)
2268                      ((eq part 'method) (gnus-get-info group))
2269                      (t nil)))
2270          (new-group (if info
2271                         (if (or (not method)
2272                                 (gnus-server-equal
2273                                  gnus-select-method method))
2274                             (gnus-group-real-name (car info))
2275                           (gnus-group-prefixed-name
2276                            (gnus-group-real-name (car info)) method))
2277                       nil)))
2278     (when (and new-group
2279                (not (equal new-group group)))
2280       (when (gnus-group-goto-group group)
2281         (gnus-group-kill-group 1))
2282       (gnus-activate-group new-group))
2283     ;; Set the info.
2284     (if (not (and info new-group))
2285         (gnus-group-set-info form (or new-group group) part)
2286       (setq info (gnus-copy-sequence info))
2287       (setcar info new-group)
2288       (unless (gnus-server-equal method "native")
2289         (unless (nthcdr 3 info)
2290           (nconc info (list nil nil)))
2291         (unless (nthcdr 4 info)
2292           (nconc info (list nil)))
2293         (gnus-info-set-method info method))
2294       (gnus-group-set-info info))
2295     (gnus-group-update-group (or new-group group))
2296     (gnus-group-position-point)))
2297
2298 (defun gnus-group-make-useful-group (group method)
2299   "Create one of the groups described in `gnus-useful-groups'."
2300   (interactive
2301    (let ((entry (assoc (completing-read "Create group: " gnus-useful-groups
2302                                         nil t)
2303                        gnus-useful-groups)))
2304      (list (cadr entry) (caddr entry))))
2305   (setq method (gnus-copy-sequence method))
2306   (let (entry)
2307     (while (setq entry (memq (assq 'eval method) method))
2308       (setcar entry (eval (cadar entry)))))
2309   (gnus-group-make-group group method))
2310
2311 (defun gnus-group-make-help-group ()
2312   "Create the Gnus documentation group."
2313   (interactive)
2314   (let ((name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
2315         (file (nnheader-find-etc-directory "gnus-tut.txt" t)))
2316     (when (gnus-gethash name gnus-newsrc-hashtb)
2317       (error "Documentation group already exists"))
2318     (if (not file)
2319         (gnus-message 1 "Couldn't find doc group")
2320       (gnus-group-make-group
2321        (gnus-group-real-name name)
2322        (list 'nndoc "gnus-help"
2323              (list 'nndoc-address file)
2324              (list 'nndoc-article-type 'mbox)))))
2325   (gnus-group-position-point))
2326
2327 (defun gnus-group-make-doc-group (file type)
2328   "Create a group that uses a single file as the source."
2329   (interactive
2330    (list (read-file-name "File name: ")
2331          (and current-prefix-arg 'ask)))
2332   (when (eq type 'ask)
2333     (let ((err "")
2334           char found)
2335       (while (not found)
2336         (message
2337          "%sFile type (mbox, babyl, digest, forward, mmdf, guess) [mbdfag]: "
2338          err)
2339         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
2340                           ((= char ?b) 'babyl)
2341                           ((= char ?d) 'digest)
2342                           ((= char ?f) 'forward)
2343                           ((= char ?a) 'mmfd)
2344                           ((= char ?g) 'guess)
2345                           (t (setq err (format "%c unknown. " char))
2346                              nil))))
2347       (setq type found)))
2348   (let* ((file (expand-file-name file))
2349          (name (gnus-generate-new-group-name
2350                 (gnus-group-prefixed-name
2351                  (file-name-nondirectory file) '(nndoc "")))))
2352     (gnus-group-make-group
2353      (gnus-group-real-name name)
2354      (list 'nndoc file
2355            (list 'nndoc-address file)
2356            (list 'nndoc-article-type (or type 'guess))))))
2357
2358 (defvar nnweb-type-definition)
2359 (defvar gnus-group-web-type-history nil)
2360 (defvar gnus-group-web-search-history nil)
2361 (defun gnus-group-make-web-group (&optional solid)
2362   "Create an ephemeral nnweb group.
2363 If SOLID (the prefix), create a solid group."
2364   (interactive "P")
2365   (require 'nnweb)
2366   (let* ((group
2367           (if solid (gnus-read-group "Group name: ")
2368             (message-unique-id)))
2369          (default-type (or (car gnus-group-web-type-history)
2370                            (symbol-name (caar nnweb-type-definition))))
2371          (type
2372           (gnus-string-or
2373            (completing-read
2374             (format "Search engine type (default %s): " default-type)
2375             (mapcar (lambda (elem) (list (symbol-name (car elem))))
2376                     nnweb-type-definition)
2377             nil t nil 'gnus-group-web-type-history)
2378            default-type))
2379          (search
2380           (read-string
2381            "Search string: "
2382            (cons (or (car gnus-group-web-search-history) "") 0)
2383            'gnus-group-web-search-history))
2384          (method
2385           `(nnweb ,group (nnweb-search ,search)
2386                   (nnweb-type ,(intern type))
2387                   (nnweb-ephemeral-p t))))
2388     (if solid
2389         (gnus-group-make-group group "nnweb" "" `(,(intern type) ,search))
2390       (gnus-group-read-ephemeral-group
2391        group method t
2392        (cons (current-buffer)
2393              (if (eq major-mode 'gnus-summary-mode) 'summary 'group))))))
2394
2395 (defvar nnwarchive-type-definition)
2396 (defvar gnus-group-warchive-type-history nil)
2397 (defvar gnus-group-warchive-login-history nil)
2398 (defvar gnus-group-warchive-address-history nil)
2399
2400 (defun gnus-group-make-warchive-group ()
2401   "Create a nnwarchive group."
2402   (interactive)
2403   (require 'nnwarchive)
2404   (let* ((group (gnus-read-group "Group name: "))
2405          (default-type (or (car gnus-group-warchive-type-history)
2406                            (symbol-name (caar nnwarchive-type-definition))))
2407          (type
2408           (gnus-string-or
2409            (completing-read
2410             (format "Warchive type (default %s): " default-type)
2411             (mapcar (lambda (elem) (list (symbol-name (car elem))))
2412                     nnwarchive-type-definition)
2413             nil t nil 'gnus-group-warchive-type-history)
2414            default-type))
2415          (address (read-string "Warchive address: "
2416                                nil 'gnus-group-warchive-address-history))
2417          (default-login (or (car gnus-group-warchive-login-history)
2418                             user-mail-address))
2419          (login
2420           (gnus-string-or
2421            (read-string
2422             (format "Warchive login (default %s): " user-mail-address)
2423             default-login 'gnus-group-warchive-login-history)
2424            user-mail-address))
2425          (method
2426           `(nnwarchive ,address
2427                        (nnwarchive-type ,(intern type))
2428                        (nnwarchive-login ,login))))
2429     (gnus-group-make-group group method)))
2430
2431 (defvar nnshimbun-type-definition)
2432 (defvar gnus-group-shimbun-server-history nil)
2433
2434 (defun gnus-group-make-shimbun-group ()
2435   "Create a nnshimbun group."
2436   (interactive)
2437   (require 'nnshimbun)
2438   (let* ((minibuffer-setup-hook (append minibuffer-setup-hook
2439                                         '(beginning-of-line)))
2440          (server (completing-read
2441                   "Shimbun address: "
2442                   (mapcar (lambda (elem) (list (car elem)))
2443                           nnshimbun-type-definition)
2444                   nil t
2445                   (or (car gnus-group-shimbun-server-history)
2446                       (caar nnshimbun-type-definition))
2447                   'gnus-group-shimbun-server-history))
2448          (group (completing-read
2449                  "Group name: "
2450                  (mapcar (lambda (elem) (list elem))
2451                          (cdr (assq 'groups
2452                                     (cdr (assoc server nnshimbun-type-definition)))))
2453                  nil t nil))
2454          (nnshimbun-pre-fetch-article nil))
2455     (gnus-group-make-group group `(nnshimbun ,server))))
2456
2457 (defun gnus-group-make-archive-group (&optional all)
2458   "Create the (ding) Gnus archive group of the most recent articles.
2459 Given a prefix, create a full group."
2460   (interactive "P")
2461   (let ((group (gnus-group-prefixed-name
2462                 (if all "ding.archives" "ding.recent") '(nndir ""))))
2463     (when (gnus-gethash group gnus-newsrc-hashtb)
2464       (error "Archive group already exists"))
2465     (gnus-group-make-group
2466      (gnus-group-real-name group)
2467      (list 'nndir (if all "hpc" "edu")
2468            (list 'nndir-directory
2469                  (if all gnus-group-archive-directory
2470                    gnus-group-recent-archive-directory))))
2471     (gnus-group-add-parameter group (cons 'to-address "ding@gnus.org"))))
2472
2473 (defun gnus-group-make-directory-group (dir)
2474   "Create an nndir group.
2475 The user will be prompted for a directory.  The contents of this
2476 directory will be used as a newsgroup.  The directory should contain
2477 mail messages or news articles in files that have numeric names."
2478   (interactive
2479    (list (read-file-name "Create group from directory: ")))
2480   (unless (file-exists-p dir)
2481     (error "No such directory"))
2482   (unless (file-directory-p dir)
2483     (error "Not a directory"))
2484   (let ((ext "")
2485         (i 0)
2486         group)
2487     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
2488       (setq group
2489             (gnus-group-prefixed-name
2490              (expand-file-name ext dir)
2491              '(nndir "")))
2492       (setq ext (format "<%d>" (setq i (1+ i)))))
2493     (gnus-group-make-group
2494      (gnus-group-real-name group)
2495      (list 'nndir (gnus-group-real-name group) (list 'nndir-directory dir)))))
2496
2497 (eval-when-compile (defvar nnkiboze-score-file))
2498 (defun gnus-group-make-kiboze-group (group address scores)
2499   "Create an nnkiboze group.
2500 The user will be prompted for a name, a regexp to match groups, and
2501 score file entries for articles to include in the group."
2502   (interactive
2503    (list
2504     (read-string "nnkiboze group name: ")
2505     (read-string "Source groups (regexp): ")
2506     (let ((headers (mapcar (lambda (group) (list group))
2507                            '("subject" "from" "number" "date" "message-id"
2508                              "references" "chars" "lines" "xref"
2509                              "followup" "all" "body" "head")))
2510           scores header regexp regexps)
2511       (while (not (equal "" (setq header (completing-read
2512                                           "Match on header: " headers nil t))))
2513         (setq regexps nil)
2514         (while (not (equal "" (setq regexp (read-string
2515                                             (format "Match on %s (regexp): "
2516                                                     header)))))
2517           (push (list regexp nil nil 'r) regexps))
2518         (push (cons header regexps) scores))
2519       scores)))
2520   (gnus-group-make-group group "nnkiboze" address)
2521   (let* ((nnkiboze-current-group group)
2522          (score-file (car (nnkiboze-score-file "")))
2523          (score-dir (file-name-directory score-file)))
2524     (unless (file-exists-p score-dir)
2525       (make-directory score-dir))
2526     (with-temp-file score-file
2527       (let (emacs-lisp-mode-hook)
2528         (pp scores (current-buffer))))))
2529
2530 (defun gnus-group-add-to-virtual (n vgroup)
2531   "Add the current group to a virtual group."
2532   (interactive
2533    (list current-prefix-arg
2534          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
2535                           "nnvirtual:")))
2536   (unless (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
2537     (error "%s is not an nnvirtual group" vgroup))
2538   (gnus-close-group vgroup)
2539   (let* ((groups (gnus-group-process-prefix n))
2540          (method (gnus-info-method (gnus-get-info vgroup))))
2541     (setcar (cdr method)
2542             (concat
2543              (nth 1 method) "\\|"
2544              (mapconcat
2545               (lambda (s)
2546                 (gnus-group-remove-mark s)
2547                 (concat "\\(^" (regexp-quote s) "$\\)"))
2548               groups "\\|"))))
2549   (gnus-group-position-point))
2550
2551 (defun gnus-group-make-empty-virtual (group)
2552   "Create a new, fresh, empty virtual group."
2553   (interactive "sCreate new, empty virtual group: ")
2554   (let* ((method (list 'nnvirtual "^$"))
2555          (pgroup (gnus-group-prefixed-name group method)))
2556     ;; Check whether it exists already.
2557     (when (gnus-gethash pgroup gnus-newsrc-hashtb)
2558       (error "Group %s already exists" pgroup))
2559     ;; Subscribe the new group after the group on the current line.
2560     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
2561     (gnus-group-update-group pgroup)
2562     (forward-line -1)
2563     (gnus-group-position-point)))
2564
2565 (defun gnus-group-enter-directory (dir)
2566   "Enter an ephemeral nneething group."
2567   (interactive "DDirectory to read: ")
2568   (let* ((method (list 'nneething dir '(nneething-read-only t)))
2569          (leaf (gnus-group-prefixed-name
2570                 (file-name-nondirectory (directory-file-name dir))
2571                 method))
2572          (name (gnus-generate-new-group-name leaf)))
2573     (unless (gnus-group-read-ephemeral-group
2574              name method t
2575              (cons (current-buffer)
2576                    (if (eq major-mode 'gnus-summary-mode)
2577                        'summary 'group)))
2578       (error "Couldn't enter %s" dir))))
2579
2580 (eval-and-compile
2581   (autoload 'nnimap-expunge "nnimap")
2582   (autoload 'nnimap-acl-get "nnimap")
2583   (autoload 'nnimap-acl-edit "nnimap"))
2584
2585 (defun gnus-group-nnimap-expunge (group)
2586   "Expunge deleted articles in current nnimap GROUP."
2587   (interactive (list (gnus-group-group-name)))
2588   (let ((mailbox (gnus-group-real-name group)) method)
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 'nnimap (car (setq method (gnus-find-method-for-group group))))
2594       (error "%s is not an nnimap group" group))
2595     (nnimap-expunge mailbox (cadr method))))
2596
2597 (defun gnus-group-nnimap-edit-acl (group)
2598   "Edit the Access Control List of current nnimap GROUP."
2599   (interactive (list (gnus-group-group-name)))
2600   (let ((mailbox (gnus-group-real-name group)) method acl)
2601     (unless group
2602       (error "No group on current line"))
2603     (unless (gnus-get-info group)
2604       (error "Killed group; can't be edited"))
2605     (unless (eq (car (setq method (gnus-find-method-for-group group))) 'nnimap)
2606       (error "%s is not an nnimap group" group))
2607     (unless (setq acl (nnimap-acl-get mailbox (cadr method)))
2608       (error "Server does not support ACL's"))
2609     (gnus-edit-form acl (format "Editing the access control list for `%s'.
2610
2611    An access control list is a list of (identifier . rights) elements.
2612
2613    The identifier string specifies the corresponding user.  The
2614    identifier \"anyone\" is reserved to refer to the universal identity.
2615
2616    Rights is a string listing a (possibly empty) set of alphanumeric
2617    characters, each character listing a set of operations which is being
2618    controlled.  Letters are reserved for ``standard'' rights, listed
2619    below.  Digits are reserved for implementation or site defined rights.
2620
2621    l - lookup (mailbox is visible to LIST/LSUB commands)
2622    r - read (SELECT the mailbox, perform CHECK, FETCH, PARTIAL,
2623        SEARCH, COPY from mailbox)
2624    s - keep seen/unseen information across sessions (STORE \\SEEN flag)
2625    w - write (STORE flags other than \\SEEN and \\DELETED)
2626    i - insert (perform APPEND, COPY into mailbox)
2627    p - post (send mail to submission address for mailbox,
2628        not enforced by IMAP4 itself)
2629    c - create and delete mailbox (CREATE new sub-mailboxes in any
2630        implementation-defined hierarchy, RENAME or DELETE mailbox)
2631    d - delete messages (STORE \\DELETED flag, perform EXPUNGE)
2632    a - administer (perform SETACL)" group)
2633                     `(lambda (form)
2634                        (nnimap-acl-edit
2635                         ,mailbox ',method ',acl form)))))
2636
2637 ;; Group sorting commands
2638 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
2639
2640 (defun gnus-group-sort-groups (func &optional reverse)
2641   "Sort the group buffer according to FUNC.
2642 When used interactively, the sorting function used will be
2643 determined by the `gnus-group-sort-function' variable.
2644 If REVERSE (the prefix), reverse the sorting order."
2645   (interactive (list gnus-group-sort-function current-prefix-arg))
2646   (funcall gnus-group-sort-alist-function
2647            (gnus-make-sort-function func) reverse)
2648   (gnus-group-list-groups)
2649   (gnus-dribble-touch))
2650
2651 (defun gnus-group-sort-flat (func reverse)
2652   ;; We peel off the dummy group from the alist.
2653   (when func
2654     (when (equal (gnus-info-group (car gnus-newsrc-alist)) "dummy.group")
2655       (pop gnus-newsrc-alist))
2656     ;; Do the sorting.
2657     (setq gnus-newsrc-alist
2658           (sort gnus-newsrc-alist func))
2659     (when reverse
2660       (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
2661     ;; Regenerate the hash table.
2662     (gnus-make-hashtable-from-newsrc-alist)))
2663
2664 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
2665   "Sort the group buffer alphabetically by group name.
2666 If REVERSE, sort in reverse order."
2667   (interactive "P")
2668   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
2669
2670 (defun gnus-group-sort-groups-by-unread (&optional reverse)
2671   "Sort the group buffer by number of unread articles.
2672 If REVERSE, sort in reverse order."
2673   (interactive "P")
2674   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
2675
2676 (defun gnus-group-sort-groups-by-level (&optional reverse)
2677   "Sort the group buffer by group level.
2678 If REVERSE, sort in reverse order."
2679   (interactive "P")
2680   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
2681
2682 (defun gnus-group-sort-groups-by-score (&optional reverse)
2683   "Sort the group buffer by group score.
2684 If REVERSE, sort in reverse order."
2685   (interactive "P")
2686   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
2687
2688 (defun gnus-group-sort-groups-by-rank (&optional reverse)
2689   "Sort the group buffer by group rank.
2690 If REVERSE, sort in reverse order."
2691   (interactive "P")
2692   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
2693
2694 (defun gnus-group-sort-groups-by-method (&optional reverse)
2695   "Sort the group buffer alphabetically by backend name.
2696 If REVERSE, sort in reverse order."
2697   (interactive "P")
2698   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
2699
2700 (defun gnus-group-sort-groups-by-server (&optional reverse)
2701   "Sort the group buffer alphabetically by server name.
2702 If REVERSE, sort in reverse order."
2703   (interactive "P")
2704   (gnus-group-sort-groups 'gnus-group-sort-by-server reverse))
2705
2706 ;;; Selected group sorting.
2707
2708 (defun gnus-group-sort-selected-groups (n func &optional reverse)
2709   "Sort the process/prefixed groups."
2710   (interactive (list current-prefix-arg gnus-group-sort-function))
2711   (let ((groups (gnus-group-process-prefix n)))
2712     (funcall gnus-group-sort-selected-function
2713              groups (gnus-make-sort-function func) reverse)
2714     (gnus-group-list-groups)))
2715
2716 (defun gnus-group-sort-selected-flat (groups func reverse)
2717   (let (entries infos)
2718     ;; First find all the group entries for these groups.
2719     (while groups
2720       (push (nthcdr 2 (gnus-gethash (pop groups) gnus-newsrc-hashtb))
2721             entries))
2722     ;; Then sort the infos.
2723     (setq infos
2724           (sort
2725            (mapcar
2726             (lambda (entry) (car entry))
2727             (setq entries (nreverse entries)))
2728            func))
2729     (when reverse
2730       (setq infos (nreverse infos)))
2731     ;; Go through all the infos and replace the old entries
2732     ;; with the new infos.
2733     (while infos
2734       (setcar (car entries) (pop infos))
2735       (pop entries))
2736     ;; Update the hashtable.
2737     (gnus-make-hashtable-from-newsrc-alist)))
2738
2739 (defun gnus-group-sort-selected-groups-by-alphabet (&optional n reverse)
2740   "Sort the group buffer alphabetically by group name.
2741 Obeys the process/prefix convention.  If REVERSE (the symbolic prefix),
2742 sort in reverse order."
2743   (interactive (gnus-interactive "P\ny"))
2744   (gnus-group-sort-selected-groups n 'gnus-group-sort-by-alphabet reverse))
2745
2746 (defun gnus-group-sort-selected-groups-by-unread (&optional n reverse)
2747   "Sort the group buffer by number of unread articles.
2748 Obeys the process/prefix convention.  If REVERSE (the symbolic prefix),
2749 sort in reverse order."
2750   (interactive (gnus-interactive "P\ny"))
2751   (gnus-group-sort-selected-groups n 'gnus-group-sort-by-unread reverse))
2752
2753 (defun gnus-group-sort-selected-groups-by-level (&optional n reverse)
2754   "Sort the group buffer by group level.
2755 Obeys the process/prefix convention.  If REVERSE (the symbolic prefix),
2756 sort in reverse order."
2757   (interactive (gnus-interactive "P\ny"))
2758   (gnus-group-sort-selected-groups n 'gnus-group-sort-by-level reverse))
2759
2760 (defun gnus-group-sort-selected-groups-by-score (&optional n reverse)
2761   "Sort the group buffer by group score.
2762 Obeys the process/prefix convention.  If REVERSE (the symbolic prefix),
2763 sort in reverse order."
2764   (interactive (gnus-interactive "P\ny"))
2765   (gnus-group-sort-selected-groups n 'gnus-group-sort-by-score reverse))
2766
2767 (defun gnus-group-sort-selected-groups-by-rank (&optional n reverse)
2768   "Sort the group buffer by group rank.
2769 Obeys the process/prefix convention.  If REVERSE (the symbolic prefix),
2770 sort in reverse order."
2771   (interactive (gnus-interactive "P\ny"))
2772   (gnus-group-sort-selected-groups n 'gnus-group-sort-by-rank reverse))
2773
2774 (defun gnus-group-sort-selected-groups-by-method (&optional n reverse)
2775   "Sort the group buffer alphabetically by backend name.
2776 Obeys the process/prefix convention.  If REVERSE (the symbolic prefix),
2777 sort in reverse order."
2778   (interactive (gnus-interactive "P\ny"))
2779   (gnus-group-sort-selected-groups n 'gnus-group-sort-by-method reverse))
2780
2781 ;;; Sorting predicates.
2782
2783 (defun gnus-group-sort-by-alphabet (info1 info2)
2784   "Sort alphabetically."
2785   (string< (gnus-info-group info1) (gnus-info-group info2)))
2786
2787 (defun gnus-group-sort-by-real-name (info1 info2)
2788   "Sort alphabetically on real (unprefixed) names."
2789   (string< (gnus-group-real-name (gnus-info-group info1))
2790            (gnus-group-real-name (gnus-info-group info2))))
2791
2792 (defun gnus-group-sort-by-unread (info1 info2)
2793   "Sort by number of unread articles."
2794   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
2795         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
2796     (< (or (and (numberp n1) n1) 0)
2797        (or (and (numberp n2) n2) 0))))
2798
2799 (defun gnus-group-sort-by-level (info1 info2)
2800   "Sort by level."
2801   (< (gnus-info-level info1) (gnus-info-level info2)))
2802
2803 (defun gnus-group-sort-by-method (info1 info2)
2804   "Sort alphabetically by backend name."
2805   (string< (symbol-name (car (gnus-find-method-for-group
2806                               (gnus-info-group info1) info1)))
2807            (symbol-name (car (gnus-find-method-for-group
2808                               (gnus-info-group info2) info2)))))
2809
2810 (defun gnus-group-sort-by-server (info1 info2)
2811   "Sort alphabetically by server name."
2812   (string< (gnus-method-to-server-name
2813             (gnus-find-method-for-group
2814              (gnus-info-group info1) info1))
2815            (gnus-method-to-server-name
2816             (gnus-find-method-for-group
2817              (gnus-info-group info2) info2))))
2818
2819 (defun gnus-group-sort-by-score (info1 info2)
2820   "Sort by group score."
2821   (> (gnus-info-score info1) (gnus-info-score info2)))
2822
2823 (defun gnus-group-sort-by-rank (info1 info2)
2824   "Sort by level and score."
2825   (let ((level1 (gnus-info-level info1))
2826         (level2 (gnus-info-level info2)))
2827     (or (< level1 level2)
2828         (and (= level1 level2)
2829              (> (gnus-info-score info1) (gnus-info-score info2))))))
2830
2831 ;;; Clearing data
2832
2833 (defun gnus-group-clear-data (&optional arg)
2834   "Clear all marks and read ranges from the current group."
2835   (interactive "P")
2836   (gnus-group-iterate arg
2837     (lambda (group)
2838       (let (info)
2839         (gnus-info-clear-data (setq info (gnus-get-info group)))
2840         (gnus-get-unread-articles-in-group info (gnus-active group) t)
2841         (when (gnus-group-goto-group group)
2842           (gnus-group-update-group-line))))))
2843
2844 (defun gnus-group-clear-data-on-native-groups ()
2845   "Clear all marks and read ranges from all native groups."
2846   (interactive)
2847   (when (gnus-yes-or-no-p "Really clear all data from almost all groups? ")
2848     (let ((alist (cdr gnus-newsrc-alist))
2849           info)
2850       (while (setq info (pop alist))
2851         (when (gnus-group-native-p (gnus-info-group info))
2852           (gnus-info-clear-data info)))
2853       (gnus-get-unread-articles)
2854       (gnus-dribble-touch)
2855       (when (gnus-y-or-n-p
2856              "Move the cache away to avoid problems in the future? ")
2857         (call-interactively 'gnus-cache-move-cache)))))
2858
2859 (defun gnus-info-clear-data (info)
2860   "Clear all marks and read ranges from INFO."
2861   (let ((group (gnus-info-group info)))
2862     (gnus-undo-register
2863       `(progn
2864          (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
2865          (gnus-info-set-read ',info ',(gnus-info-read info))
2866          (when (gnus-group-goto-group ,group)
2867            (gnus-group-update-group-line))))
2868     (gnus-info-set-read info nil)
2869     (when (gnus-info-marks info)
2870       (gnus-info-set-marks info nil))))
2871
2872 ;; Group catching up.
2873
2874 (defun gnus-group-catchup-current (&optional n all)
2875   "Mark all unread articles in the current newsgroup as read.
2876 If prefix argument N is numeric, the next N newsgroups will be
2877 caught up.  If ALL is non-nil, marked articles will also be marked as
2878 read.  Cross references (Xref: header) of articles are ignored.
2879 The number of newsgroups that this function was unable to catch
2880 up is returned."
2881   (interactive "P")
2882   (let ((groups (gnus-group-process-prefix n))
2883         (ret 0)
2884         group)
2885     (unless groups (error "No groups selected"))
2886     (if (not
2887          (or (not gnus-interactive-catchup) ;Without confirmation?
2888              gnus-expert-user
2889              (gnus-y-or-n-p
2890               (format
2891                (if all
2892                    "Do you really want to mark all articles in %s as read? "
2893                  "Mark all unread articles in %s as read? ")
2894                (if (= (length groups) 1)
2895                    (car groups)
2896                  (format "these %d groups" (length groups)))))))
2897         n
2898       (while (setq group (pop groups))
2899         (gnus-group-remove-mark group)
2900         ;; Virtual groups have to be given special treatment.
2901         (let ((method (gnus-find-method-for-group group)))
2902           (when (eq 'nnvirtual (car method))
2903             (nnvirtual-catchup-group
2904              (gnus-group-real-name group) (nth 1 method) all)))
2905         (if (>= (gnus-group-level group) gnus-level-zombie)
2906             (gnus-message 2 "Dead groups can't be caught up")
2907           (if (prog1
2908                   (gnus-group-goto-group group)
2909                 (gnus-group-catchup group all))
2910               (gnus-group-update-group-line)
2911             (setq ret (1+ ret)))))
2912       (gnus-group-next-unread-group 1)
2913       ret)))
2914
2915 (defun gnus-group-catchup-current-all (&optional n)
2916   "Mark all articles in current newsgroup as read.
2917 Cross references (Xref: header) of articles are ignored."
2918   (interactive "P")
2919   (gnus-group-catchup-current n 'all))
2920
2921 (defun gnus-group-catchup (group &optional all)
2922   "Mark all articles in GROUP as read.
2923 If ALL is non-nil, all articles are marked as read.
2924 The return value is the number of articles that were marked as read,
2925 or nil if no action could be taken."
2926   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
2927          (num (car entry)))
2928     ;; Remove entries for this group.
2929     (nnmail-purge-split-history (gnus-group-real-name group))
2930     ;; Do the updating only if the newsgroup isn't killed.
2931     (if (not (numberp (car entry)))
2932         (gnus-message 1 "Can't catch up %s; non-active group" group)
2933       ;; Do auto-expirable marks if that's required.
2934       (when (gnus-group-auto-expirable-p group)
2935         (gnus-add-marked-articles
2936          group 'expire (gnus-list-of-unread-articles group))
2937         (when all
2938           (let ((marks (nth 3 (nth 2 entry))))
2939             (gnus-add-marked-articles
2940              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
2941             (gnus-add-marked-articles
2942              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
2943       (when entry
2944         (gnus-update-read-articles group nil)
2945         ;; Also nix out the lists of marks and dormants.
2946         (when all
2947           (gnus-add-marked-articles group 'tick nil nil 'force)
2948           (gnus-add-marked-articles group 'dormant nil nil 'force))
2949         (let ((gnus-newsgroup-name group))
2950           (gnus-run-hooks 'gnus-group-catchup-group-hook))
2951         num))))
2952
2953 (defun gnus-group-expire-articles (&optional n)
2954   "Expire all expirable articles in the current newsgroup."
2955   (interactive "P")
2956   (let ((groups (gnus-group-process-prefix n))
2957         group)
2958     (unless groups
2959       (error "No groups to expire"))
2960     (while (setq group (pop groups))
2961       (gnus-group-remove-mark group)
2962       (gnus-group-expire-articles-1 group)
2963       (gnus-dribble-touch)
2964       (gnus-group-position-point))))
2965
2966 (defun gnus-group-expire-articles-1 (group)
2967   (when (gnus-check-backend-function 'request-expire-articles group)
2968     (gnus-message 6 "Expiring articles in %s..." group)
2969     (let* ((info (gnus-get-info group))
2970            (expirable (if (gnus-group-total-expirable-p group)
2971                           (cons nil (gnus-list-of-read-articles group))
2972                         (assq 'expire (gnus-info-marks info))))
2973            (expiry-wait (gnus-group-find-parameter group 'expiry-wait))
2974            (nnmail-expiry-target
2975             (or (gnus-group-find-parameter group 'expiry-target)
2976                 nnmail-expiry-target)))
2977       (when expirable
2978         (gnus-check-group group)
2979         (setcdr
2980          expirable
2981          (gnus-compress-sequence
2982           (if expiry-wait
2983               ;; We set the expiry variables to the group
2984               ;; parameter.
2985               (let ((nnmail-expiry-wait-function nil)
2986                     (nnmail-expiry-wait expiry-wait))
2987                 (gnus-request-expire-articles
2988                  (gnus-uncompress-sequence (cdr expirable)) group))
2989             ;; Just expire using the normal expiry values.
2990             (gnus-request-expire-articles
2991              (gnus-uncompress-sequence (cdr expirable)) group))))
2992         (gnus-close-group group))
2993       (gnus-message 6 "Expiring articles in %s...done" group)
2994       ;; Return the list of un-expired articles.
2995       (cdr expirable))))
2996
2997 (defun gnus-group-expire-all-groups ()
2998   "Expire all expirable articles in all newsgroups."
2999   (interactive)
3000   (save-excursion
3001     (gnus-message 5 "Expiring...")
3002     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
3003                                      (cdr gnus-newsrc-alist))))
3004       (gnus-group-expire-articles nil)))
3005   (gnus-group-position-point)
3006   (gnus-message 5 "Expiring...done"))
3007
3008 (defun gnus-group-set-current-level (n level)
3009   "Set the level of the next N groups to LEVEL."
3010   (interactive
3011    (list
3012     current-prefix-arg
3013     (string-to-int
3014      (let ((s (read-string
3015                (format "Level (default %s): "
3016                        (or (gnus-group-group-level)
3017                            gnus-level-default-subscribed)))))
3018        (if (string-match "^\\s-*$" s)
3019            (int-to-string (or (gnus-group-group-level)
3020                               gnus-level-default-subscribed))
3021          s)))))
3022   (unless (and (>= level 1) (<= level gnus-level-killed))
3023     (error "Invalid level: %d" level))
3024   (let ((groups (gnus-group-process-prefix n))
3025         group)
3026     (while (setq group (pop groups))
3027       (gnus-group-remove-mark group)
3028       (gnus-message 6 "Changed level of %s from %d to %d"
3029                     group (or (gnus-group-group-level) gnus-level-killed)
3030                     level)
3031       (gnus-group-change-level
3032        group level (or (gnus-group-group-level) gnus-level-killed))
3033       (gnus-group-update-group-line)))
3034   (gnus-group-position-point))
3035
3036 (defun gnus-group-unsubscribe (&optional n)
3037   "Unsubscribe the current group."
3038   (interactive "P")
3039   (gnus-group-unsubscribe-current-group n 'unsubscribe))
3040
3041 (defun gnus-group-subscribe (&optional n)
3042   "Subscribe the current group."
3043   (interactive "P")
3044   (gnus-group-unsubscribe-current-group n 'subscribe))
3045
3046 (defun gnus-group-unsubscribe-current-group (&optional n do-sub)
3047   "Toggle subscription of the current group.
3048 If given numerical prefix, toggle the N next groups."
3049   (interactive "P")
3050   (let ((groups (gnus-group-process-prefix n))
3051         group)
3052     (while groups
3053       (setq group (car groups)
3054             groups (cdr groups))
3055       (gnus-group-remove-mark group)
3056       (gnus-group-unsubscribe-group
3057        group
3058        (cond
3059         ((eq do-sub 'unsubscribe)
3060          gnus-level-default-unsubscribed)
3061         ((eq do-sub 'subscribe)
3062          gnus-level-default-subscribed)
3063         ((<= (gnus-group-group-level) gnus-level-subscribed)
3064          gnus-level-default-unsubscribed)
3065         (t
3066          gnus-level-default-subscribed))
3067        t)
3068       (gnus-group-update-group-line))
3069     (gnus-group-next-group 1)))
3070
3071 (defun gnus-group-unsubscribe-group (group &optional level silent)
3072   "Toggle subscription to GROUP.
3073 Killed newsgroups are subscribed.  If SILENT, don't try to update the
3074 group line."
3075   (interactive
3076    (list (completing-read
3077           "Group: " gnus-active-hashtb nil
3078           (gnus-read-active-file-p)
3079           nil
3080           'gnus-group-history)))
3081   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
3082     (cond
3083      ((string-match "^[ \t]*$" group)
3084       (error "Empty group name"))
3085      (newsrc
3086       ;; Toggle subscription flag.
3087       (gnus-group-change-level
3088        newsrc (if level level (if (<= (gnus-info-level (nth 2 newsrc))
3089                                       gnus-level-subscribed)
3090                                   (1+ gnus-level-subscribed)
3091                                 gnus-level-default-subscribed)))
3092       (unless silent
3093         (gnus-group-update-group group)))
3094      ((and (stringp group)
3095            (or (not (gnus-read-active-file-p))
3096                (gnus-active group)))
3097       ;; Add new newsgroup.
3098       (gnus-group-change-level
3099        group
3100        (if level level gnus-level-default-subscribed)
3101        (or (and (member group gnus-zombie-list)
3102                 gnus-level-zombie)
3103            gnus-level-killed)
3104        (when (gnus-group-group-name)
3105          (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
3106       (unless silent
3107         (gnus-group-update-group group)))
3108      (t (error "No such newsgroup: %s" group)))
3109     (gnus-group-position-point)))
3110
3111 (defun gnus-group-transpose-groups (n)
3112   "Move the current newsgroup up N places.
3113 If given a negative prefix, move down instead.  The difference between
3114 N and the number of steps taken is returned."
3115   (interactive "p")
3116   (unless (gnus-group-group-name)
3117     (error "No group on current line"))
3118   (gnus-group-kill-group 1)
3119   (prog1
3120       (forward-line (- n))
3121     (gnus-group-yank-group)
3122     (gnus-group-position-point)))
3123
3124 (defun gnus-group-kill-all-zombies (&optional dummy)
3125   "Kill all zombie newsgroups.
3126 The optional DUMMY should always be nil."
3127   (interactive (list (not (gnus-yes-or-no-p "Really kill all zombies? "))))
3128   (unless dummy
3129     (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
3130     (setq gnus-zombie-list nil)
3131     (gnus-dribble-touch)
3132     (gnus-group-list-groups)))
3133
3134 (defun gnus-group-kill-region (begin end)
3135   "Kill newsgroups in current region (excluding current point).
3136 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
3137   (interactive "r")
3138   (let ((lines
3139          ;; Count lines.
3140          (save-excursion
3141            (count-lines
3142             (progn
3143               (goto-char begin)
3144               (beginning-of-line)
3145               (point))
3146             (progn
3147               (goto-char end)
3148               (beginning-of-line)
3149               (point))))))
3150     (goto-char begin)
3151     (beginning-of-line)                 ;Important when LINES < 1
3152     (gnus-group-kill-group lines)))
3153
3154 (defun gnus-group-kill-group (&optional n discard)
3155   "Kill the next N groups.
3156 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
3157 However, only groups that were alive can be yanked; already killed
3158 groups or zombie groups can't be yanked.
3159 The return value is the name of the group that was killed, or a list
3160 of groups killed."
3161   (interactive "P")
3162   (let ((buffer-read-only nil)
3163         (groups (gnus-group-process-prefix n))
3164         group entry level out)
3165     (if (< (length groups) 10)
3166         ;; This is faster when there are few groups.
3167         (while groups
3168           (push (setq group (pop groups)) out)
3169           (gnus-group-remove-mark group)
3170           (setq level (gnus-group-group-level))
3171           (gnus-delete-line)
3172           (when (and (not discard)
3173                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
3174             (gnus-undo-register
3175               `(progn
3176                  (gnus-group-goto-group ,(gnus-group-group-name))
3177                  (gnus-group-yank-group)))
3178             (push (cons (car entry) (nth 2 entry))
3179                   gnus-list-of-killed-groups))
3180           (gnus-group-change-level
3181            (if entry entry group) gnus-level-killed (if entry nil level))
3182           (message "Killed group %s" group))
3183       ;; If there are lots and lots of groups to be killed, we use
3184       ;; this thing instead.
3185       (let (entry)
3186         (setq groups (nreverse groups))
3187         (while groups
3188           (gnus-group-remove-mark (setq group (pop groups)))
3189           (gnus-delete-line)
3190           (push group gnus-killed-list)
3191           (setq gnus-newsrc-alist
3192                 (delq (assoc group gnus-newsrc-alist)
3193                       gnus-newsrc-alist))
3194           (when gnus-group-change-level-function
3195             (funcall gnus-group-change-level-function
3196                      group gnus-level-killed 3))
3197           (cond
3198            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
3199             (push (cons (car entry) (nth 2 entry))
3200                   gnus-list-of-killed-groups)
3201             (setcdr (cdr entry) (cdddr entry)))
3202            ((member group gnus-zombie-list)
3203             (setq gnus-zombie-list (delete group gnus-zombie-list))))
3204           ;; There may be more than one instance displayed.
3205           (while (gnus-group-goto-group group)
3206             (gnus-delete-line)))
3207         (gnus-make-hashtable-from-newsrc-alist)))
3208
3209     (gnus-group-position-point)
3210     (if (< (length out) 2) (car out) (nreverse out))))
3211
3212 (defun gnus-group-yank-group (&optional arg)
3213   "Yank the last newsgroups killed with \\[gnus-group-kill-group], inserting it before the current newsgroup.
3214 The numeric ARG specifies how many newsgroups are to be yanked.  The
3215 name of the newsgroup yanked is returned, or (if several groups are
3216 yanked) a list of yanked groups is returned."
3217   (interactive "p")
3218   (setq arg (or arg 1))
3219   (let (info group prev out)
3220     (while (>= (decf arg) 0)
3221       (when (not (setq info (pop gnus-list-of-killed-groups)))
3222         (error "No more newsgroups to yank"))
3223       (push (setq group (nth 1 info)) out)
3224       ;; Find which newsgroup to insert this one before - search
3225       ;; backward until something suitable is found.  If there are no
3226       ;; other newsgroups in this buffer, just make this newsgroup the
3227       ;; first newsgroup.
3228       (setq prev (gnus-group-group-name))
3229       (gnus-group-change-level
3230        info (gnus-info-level (cdr info)) gnus-level-killed
3231        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
3232        t)
3233       (gnus-group-insert-group-line-info group)
3234       (gnus-undo-register
3235         `(when (gnus-group-goto-group ,group)
3236            (gnus-group-kill-group 1))))
3237     (forward-line -1)
3238     (gnus-group-position-point)
3239     (if (< (length out) 2) (car out) (nreverse out))))
3240
3241 (defun gnus-group-kill-level (level)
3242   "Kill all groups that is on a certain LEVEL."
3243   (interactive "nKill all groups on level: ")
3244   (cond
3245    ((= level gnus-level-zombie)
3246     (setq gnus-killed-list
3247           (nconc gnus-zombie-list gnus-killed-list))
3248     (setq gnus-zombie-list nil))
3249    ((and (< level gnus-level-zombie)
3250          (> level 0)
3251          (or gnus-expert-user
3252              (gnus-yes-or-no-p
3253               (format
3254                "Do you really want to kill all groups on level %d? "
3255                level))))
3256     (let* ((prev gnus-newsrc-alist)
3257            (alist (cdr prev)))
3258       (while alist
3259         (if (= (gnus-info-level (car alist)) level)
3260             (progn
3261               (push (gnus-info-group (car alist)) gnus-killed-list)
3262               (setcdr prev (cdr alist)))
3263           (setq prev alist))
3264         (setq alist (cdr alist)))
3265       (gnus-make-hashtable-from-newsrc-alist)
3266       (gnus-group-list-groups)))
3267    (t
3268     (error "Can't kill; invalid level: %d" level))))
3269
3270 (defun gnus-group-list-all-groups (&optional arg)
3271   "List all newsgroups with level ARG or lower.
3272 Default is gnus-level-unsubscribed, which lists all subscribed and most
3273 unsubscribed groups."
3274   (interactive "P")
3275   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
3276
3277 ;; Redefine this to list ALL killed groups if prefix arg used.
3278 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
3279 (defun gnus-group-list-killed (&optional arg)
3280   "List all killed newsgroups in the group buffer.
3281 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
3282 entail asking the server for the groups."
3283   (interactive "P")
3284   ;; Find all possible killed newsgroups if arg.
3285   (when arg
3286     (gnus-get-killed-groups))
3287   (if (not gnus-killed-list)
3288       (gnus-message 6 "No killed groups")
3289     (let (gnus-group-list-mode)
3290       (funcall gnus-group-prepare-function
3291                gnus-level-killed t gnus-level-killed))
3292     (goto-char (point-min)))
3293   (gnus-group-position-point))
3294
3295 (defun gnus-group-list-zombies ()
3296   "List all zombie newsgroups in the group buffer."
3297   (interactive)
3298   (if (not gnus-zombie-list)
3299       (gnus-message 6 "No zombie groups")
3300     (let (gnus-group-list-mode)
3301       (funcall gnus-group-prepare-function
3302                gnus-level-zombie t gnus-level-zombie))
3303     (goto-char (point-min)))
3304   (gnus-group-position-point))
3305
3306 (defun gnus-group-list-active ()
3307   "List all groups that are available from the server(s)."
3308   (interactive)
3309   ;; First we make sure that we have really read the active file.
3310   (unless (gnus-read-active-file-p)
3311     (let ((gnus-read-active-file t)
3312           (gnus-agent nil))             ; Trick the agent into ignoring the active file.
3313       (gnus-read-active-file)))
3314   ;; Find all groups and sort them.
3315   (let ((groups
3316          (sort
3317           (let (list)
3318             (mapatoms
3319              (lambda (sym)
3320                (and (boundp sym)
3321                     (symbol-value sym)
3322                     (push (symbol-name sym) list)))
3323              gnus-active-hashtb)
3324             list)
3325           'string<))
3326         (buffer-read-only nil)
3327         group)
3328     (erase-buffer)
3329     (while groups
3330       (setq group (pop groups))
3331       (gnus-add-text-properties
3332        (point) (prog1 (1+ (point))
3333                  (insert "       *: "
3334                          (gnus-group-name-decode group
3335                                                  (gnus-group-name-charset
3336                                                   nil group))
3337                          "\n"))
3338        (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
3339              'gnus-unread t
3340              'gnus-level (inline (gnus-group-level group)))))
3341     (goto-char (point-min))))
3342
3343 (defun gnus-activate-all-groups (level)
3344   "Activate absolutely all groups."
3345   (interactive (list gnus-level-unsubscribed))
3346   (let ((gnus-activate-level level)
3347         (gnus-activate-foreign-newsgroups level))
3348     (gnus-group-get-new-news)))
3349
3350 (defun gnus-group-get-new-news (&optional arg)
3351   "Get newly arrived articles.
3352 If ARG is a number, it specifies which levels you are interested in
3353 re-scanning.  If ARG is non-nil and not a number, this will force
3354 \"hard\" re-reading of the active files from all servers."
3355   (interactive "P")
3356   (require 'nnmail)
3357   (let ((gnus-inhibit-demon t)
3358         ;; Binding this variable will inhibit multiple fetchings
3359         ;; of the same mail source.
3360         (nnmail-fetched-sources (list t)))
3361     (gnus-run-hooks 'gnus-get-new-news-hook)
3362
3363     ;; Read any slave files.
3364     (unless gnus-slave
3365       (gnus-master-read-slave-newsrc))
3366
3367     ;; We might read in new NoCeM messages here.
3368     (when (and gnus-use-nocem
3369                (null arg))
3370       (gnus-nocem-scan-groups))
3371     ;; If ARG is not a number, then we read the active file.
3372     (when (and arg (not (numberp arg)))
3373       (let ((gnus-read-active-file t))
3374         (gnus-read-active-file))
3375       (setq arg nil)
3376
3377       ;; If the user wants it, we scan for new groups.
3378       (when (eq gnus-check-new-newsgroups 'always)
3379         (gnus-find-new-newsgroups)))
3380
3381     (setq arg (gnus-group-default-level arg t))
3382     (if (and gnus-read-active-file (not arg))
3383         (progn
3384           (gnus-read-active-file)
3385           (gnus-get-unread-articles arg))
3386       (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
3387         (gnus-get-unread-articles arg)))
3388     (gnus-run-hooks 'gnus-after-getting-new-news-hook)
3389     (gnus-group-list-groups (and (numberp arg)
3390                                  (max (car gnus-group-list-mode) arg))))
3391   ;; Update modeline.
3392   (when (and gnus-agent (not (interactive-p)))
3393     (gnus-agent-toggle-plugged gnus-plugged)))
3394
3395 (defun gnus-group-get-new-news-this-group (&optional n dont-scan)
3396   "Check for newly arrived news in the current group (and the N-1 next groups).
3397 The difference between N and the number of newsgroup checked is returned.
3398 If N is negative, this group and the N-1 previous groups will be checked."
3399   (interactive "P")
3400   (let* ((groups (gnus-group-process-prefix n))
3401          (ret (if (numberp n) (- n (length groups)) 0))
3402          (beg (unless n
3403                 (point)))
3404          group method
3405          (gnus-inhibit-demon t)
3406          ;; Binding this variable will inhibit multiple fetchings
3407          ;; of the same mail source.
3408          (nnmail-fetched-sources (list t)))
3409     (gnus-run-hooks 'gnus-get-new-news-hook)
3410     (while (setq group (pop groups))
3411       (gnus-group-remove-mark group)
3412       ;; Bypass any previous denials from the server.
3413       (gnus-remove-denial (setq method (gnus-find-method-for-group group)))
3414       (if (gnus-activate-group group (if dont-scan nil 'scan))
3415           (progn
3416             (gnus-get-unread-articles-in-group
3417              (gnus-get-info group) (gnus-active group) t)
3418             (unless (gnus-virtual-group-p group)
3419               (gnus-close-group group))
3420             (when gnus-agent
3421               (gnus-agent-save-group-info
3422                method (gnus-group-real-name group) (gnus-active group)))
3423             (gnus-group-update-group group))
3424         (if (eq (gnus-server-status (gnus-find-method-for-group group))
3425                 'denied)
3426             (gnus-error 3 "Server denied access")
3427           (gnus-error 3 "%s error: %s" group (gnus-status-message group)))))
3428     (when beg
3429       (goto-char beg))
3430     (when gnus-goto-next-group-when-activating
3431       (gnus-group-next-unread-group 1 t))
3432     (gnus-summary-position-point)
3433     ret))
3434
3435 (defun gnus-group-fetch-faq (group &optional faq-dir)
3436   "Fetch the FAQ for the current group.
3437 If given a prefix argument, prompt for the FAQ dir
3438 to use."
3439   (interactive
3440    (list
3441     (gnus-group-group-name)
3442     (when current-prefix-arg
3443       (completing-read
3444        "Faq dir: " (and (listp gnus-group-faq-directory)
3445                         (mapcar #'list
3446                                 gnus-group-faq-directory))))))
3447   (unless group
3448     (error "No group name given"))
3449   (let ((dirs (or faq-dir gnus-group-faq-directory))
3450         dir found file)
3451     (unless (listp dirs)
3452       (setq dirs (list dirs)))
3453     (while (and (not found)
3454                 (setq dir (pop dirs)))
3455       (let ((name (gnus-group-real-name group)))
3456         (setq file (expand-file-name name dir)))
3457       (if (not (file-exists-p file))
3458           (gnus-message 1 "No such file: %s" file)
3459         (let ((enable-local-variables nil))
3460           (find-file file)
3461           (setq found t))))))
3462
3463 (defun gnus-group-describe-group (force &optional group)
3464   "Display a description of the current newsgroup."
3465   (interactive (list current-prefix-arg (gnus-group-group-name)))
3466   (let* ((method (gnus-find-method-for-group group))
3467          (mname (gnus-group-prefixed-name "" method))
3468          desc)
3469     (when (and force
3470                gnus-description-hashtb)
3471       (gnus-sethash mname nil gnus-description-hashtb))
3472     (unless group
3473       (error "No group name given"))
3474     (when (or (and gnus-description-hashtb
3475                    ;; We check whether this group's method has been
3476                    ;; queried for a description file.
3477                    (gnus-gethash mname gnus-description-hashtb))
3478               (setq desc (gnus-group-get-description group))
3479               (gnus-read-descriptions-file method))
3480       (gnus-message 1
3481                     (or desc (gnus-gethash group gnus-description-hashtb)
3482                         "No description available")))))
3483
3484 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
3485 (defun gnus-group-describe-all-groups (&optional force)
3486   "Pop up a buffer with descriptions of all newsgroups."
3487   (interactive "P")
3488   (when force
3489     (setq gnus-description-hashtb nil))
3490   (when (not (or gnus-description-hashtb
3491                  (gnus-read-all-descriptions-files)))
3492     (error "Couldn't request descriptions file"))
3493   (let ((buffer-read-only nil)
3494         b)
3495     (erase-buffer)
3496     (mapatoms
3497      (lambda (group)
3498        (setq b (point))
3499        (let ((charset (gnus-group-name-charset nil (symbol-name group))))
3500          (insert (format "      *: %-20s %s\n"
3501                          (gnus-group-name-decode
3502                           (symbol-name group) charset)
3503                          (gnus-group-name-decode
3504                           (symbol-value group) charset))))
3505        (gnus-add-text-properties
3506         b (1+ b) (list 'gnus-group group
3507                        'gnus-unread t 'gnus-marked nil
3508                        'gnus-level (1+ gnus-level-subscribed))))
3509      gnus-description-hashtb)
3510     (goto-char (point-min))
3511     (gnus-group-position-point)))
3512
3513 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
3514 (defun gnus-group-apropos (regexp &optional search-description)
3515   "List all newsgroups that have names that match a regexp."
3516   (interactive "sGnus apropos (regexp): ")
3517   (let ((prev "")
3518         (obuf (current-buffer))
3519         groups des)
3520     ;; Go through all newsgroups that are known to Gnus.
3521     (mapatoms
3522      (lambda (group)
3523        (and (symbol-name group)
3524             (string-match regexp (symbol-name group))
3525             (symbol-value group)
3526             (push (symbol-name group) groups)))
3527      gnus-active-hashtb)
3528     ;; Also go through all descriptions that are known to Gnus.
3529     (when search-description
3530       (mapatoms
3531        (lambda (group)
3532          (and (string-match regexp (symbol-value group))
3533               (push (symbol-name group) groups)))
3534        gnus-description-hashtb))
3535     (if (not groups)
3536         (gnus-message 3 "No groups matched \"%s\"." regexp)
3537       ;; Print out all the groups.
3538       (save-excursion
3539         (pop-to-buffer "*Gnus Help*")
3540         (buffer-disable-undo)
3541         (erase-buffer)
3542         (setq groups (sort groups 'string<))
3543         (while groups
3544           ;; Groups may be entered twice into the list of groups.
3545           (when (not (string= (car groups) prev))
3546             (setq prev (car groups))
3547             (let ((charset (gnus-group-name-charset nil prev)))
3548               (insert (gnus-group-name-decode prev charset) "\n")
3549               (when (and gnus-description-hashtb
3550                          (setq des (gnus-gethash (car groups)
3551                                                  gnus-description-hashtb)))
3552                 (insert "  " (gnus-group-name-decode des charset) "\n"))))
3553           (setq groups (cdr groups)))
3554         (goto-char (point-min))))
3555     (pop-to-buffer obuf)))
3556
3557 (defun gnus-group-description-apropos (regexp)
3558   "List all newsgroups that have names or descriptions that match a regexp."
3559   (interactive "sGnus description apropos (regexp): ")
3560   (when (not (or gnus-description-hashtb
3561                  (gnus-read-all-descriptions-files)))
3562     (error "Couldn't request descriptions file"))
3563   (gnus-group-apropos regexp t))
3564
3565 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
3566 (defun gnus-group-list-matching (level regexp &optional all lowest)
3567   "List all groups with unread articles that match REGEXP.
3568 If the prefix LEVEL is non-nil, it should be a number that says which
3569 level to cut off listing groups.
3570 If ALL, also list groups with no unread articles.
3571 If LOWEST, don't list groups with level lower than LOWEST.
3572
3573 This command may read the active file."
3574   (interactive "P\nsList newsgroups matching: ")
3575   ;; First make sure active file has been read.
3576   (when (and level
3577              (> (prefix-numeric-value level) gnus-level-killed))
3578     (gnus-get-killed-groups))
3579   (funcall gnus-group-prepare-function
3580    (or level gnus-level-subscribed) (and all t) (or lowest 1) regexp)
3581   (goto-char (point-min))
3582   (gnus-group-position-point))
3583
3584 (defun gnus-group-list-all-matching (level regexp &optional lowest)
3585   "List all groups that match REGEXP.
3586 If the prefix LEVEL is non-nil, it should be a number that says which
3587 level to cut off listing groups.
3588 If LOWEST, don't list groups with level lower than LOWEST."
3589   (interactive "P\nsList newsgroups matching: ")
3590   (when level
3591     (setq level (prefix-numeric-value level)))
3592   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
3593
3594 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
3595 (defun gnus-group-save-newsrc (&optional force)
3596   "Save the Gnus startup files.
3597 If FORCE, force saving whether it is necessary or not."
3598   (interactive "P")
3599   (gnus-save-newsrc-file force))
3600
3601 (defun gnus-group-restart (&optional arg)
3602   "Force Gnus to read the .newsrc file."
3603   (interactive "P")
3604   (when (gnus-yes-or-no-p
3605          (format "Are you sure you want to restart Gnus? "))
3606     (gnus-save-newsrc-file)
3607     (gnus-clear-system)
3608     (gnus)))
3609
3610 (defun gnus-group-read-init-file ()
3611   "Read the Gnus elisp init file."
3612   (interactive)
3613   (gnus-read-init-file)
3614   (gnus-message 5 "Read %s" gnus-init-file))
3615
3616 (defun gnus-group-check-bogus-groups (&optional silent)
3617   "Check bogus newsgroups.
3618 If given a prefix, don't ask for confirmation before removing a bogus
3619 group."
3620   (interactive "P")
3621   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
3622   (gnus-group-list-groups))
3623
3624 (defun gnus-group-find-new-groups (&optional arg)
3625   "Search for new groups and add them.
3626 Each new group will be treated with `gnus-subscribe-newsgroup-method.'
3627 With 1 C-u, use the `ask-server' method to query the server for new
3628 groups.
3629 With 2 C-u's, use most complete method possible to query the server
3630 for new groups, and subscribe the new groups as zombies."
3631   (interactive "p")
3632   (gnus-find-new-newsgroups (or arg 1))
3633   (gnus-group-list-groups))
3634
3635 (defun gnus-group-edit-global-kill (&optional article group)
3636   "Edit the global kill file.
3637 If GROUP, edit that local kill file instead."
3638   (interactive "P")
3639   (setq gnus-current-kill-article article)
3640   (gnus-kill-file-edit-file group)
3641   (gnus-message
3642    6
3643    (substitute-command-keys
3644     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
3645             (if group "local" "global")))))
3646
3647 (defun gnus-group-edit-local-kill (article group)
3648   "Edit a local kill file."
3649   (interactive (list nil (gnus-group-group-name)))
3650   (gnus-group-edit-global-kill article group))
3651
3652 (defun gnus-group-force-update ()
3653   "Update `.newsrc' file."
3654   (interactive)
3655   (gnus-save-newsrc-file))
3656
3657 (defun gnus-group-suspend ()
3658   "Suspend the current Gnus session.
3659 In fact, cleanup buffers except for group mode buffer.
3660 The hook gnus-suspend-gnus-hook is called before actually suspending."
3661   (interactive)
3662   (gnus-run-hooks 'gnus-suspend-gnus-hook)
3663   ;; Kill Gnus buffers except for group mode buffer.
3664   (let ((group-buf (get-buffer gnus-group-buffer)))
3665     (mapcar (lambda (buf)
3666               (unless (member buf (list group-buf gnus-dribble-buffer))
3667                 (kill-buffer buf)))
3668             (gnus-buffers))
3669     (gnus-kill-gnus-frames)
3670     (when group-buf
3671       (bury-buffer group-buf)
3672       (delete-windows-on group-buf t))))
3673
3674 (defun gnus-group-clear-dribble ()
3675   "Clear all information from the dribble buffer."
3676   (interactive)
3677   (gnus-dribble-clear)
3678   (gnus-message 7 "Cleared dribble buffer"))
3679
3680 (defun gnus-group-exit ()
3681   "Quit reading news after updating .newsrc.eld and .newsrc.
3682 The hook `gnus-exit-gnus-hook' is called before actually exiting."
3683   (interactive)
3684   (when
3685       (or noninteractive                ;For gnus-batch-kill
3686           (not gnus-interactive-exit)   ;Without confirmation
3687           gnus-expert-user
3688           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
3689     (gnus-run-hooks 'gnus-exit-gnus-hook)
3690     ;; Offer to save data from non-quitted summary buffers.
3691     (gnus-offer-save-summaries)
3692     ;; Save the newsrc file(s).
3693     (gnus-save-newsrc-file)
3694     ;; Kill-em-all.
3695     (gnus-close-backends)
3696     ;; Reset everything.
3697     (gnus-clear-system)
3698     ;; Allow the user to do things after cleaning up.
3699     (gnus-run-hooks 'gnus-after-exiting-gnus-hook)))
3700
3701 (defun gnus-group-quit ()
3702   "Quit reading news without updating .newsrc.eld or .newsrc.
3703 The hook `gnus-exit-gnus-hook' is called before actually exiting."
3704   (interactive)
3705   (when (or noninteractive              ;For gnus-batch-kill
3706             (zerop (buffer-size))
3707             (not (gnus-server-opened gnus-select-method))
3708             gnus-expert-user
3709             (not gnus-current-startup-file)
3710             (gnus-yes-or-no-p
3711              (format "Quit reading news without saving %s? "
3712                      (file-name-nondirectory gnus-current-startup-file))))
3713     (gnus-run-hooks 'gnus-exit-gnus-hook)
3714     (gnus-configure-windows 'group t)
3715     (gnus-dribble-save)
3716     (gnus-close-backends)
3717     (gnus-clear-system)
3718     (gnus-kill-buffer gnus-group-buffer)
3719     ;; Allow the user to do things after cleaning up.
3720     (gnus-run-hooks 'gnus-after-exiting-gnus-hook)))
3721
3722 (defun gnus-group-describe-briefly ()
3723   "Give a one line description of the group mode commands."
3724   (interactive)
3725   (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")))
3726
3727 (defun gnus-group-browse-foreign-server (method)
3728   "Browse a foreign news server.
3729 If called interactively, this function will ask for a select method
3730  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
3731 If not, METHOD should be a list where the first element is the method
3732 and the second element is the address."
3733   (interactive
3734    (list (let ((how (completing-read
3735                      "Which backend: "
3736                      (append gnus-valid-select-methods gnus-server-alist)
3737                      nil t (cons "nntp" 0) 'gnus-method-history)))
3738            ;; We either got a backend name or a virtual server name.
3739            ;; If the first, we also need an address.
3740            (if (assoc how gnus-valid-select-methods)
3741                (list (intern how)
3742                      ;; Suggested by mapjph@bath.ac.uk.
3743                      (completing-read
3744                       "Address: "
3745                       (mapcar (lambda (server) (list server))
3746                               gnus-secondary-servers)))
3747              ;; We got a server name.
3748              how))))
3749   (gnus-browse-foreign-server method))
3750
3751 (defun gnus-group-set-info (info &optional method-only-group part)
3752   (when (or info part)
3753     (let* ((entry (gnus-gethash
3754                    (or method-only-group (gnus-info-group info))
3755                    gnus-newsrc-hashtb))
3756            (part-info info)
3757            (info (if method-only-group (nth 2 entry) info))
3758            method)
3759       (when method-only-group
3760         (unless entry
3761           (error "Trying to change non-existent group %s" method-only-group))
3762         ;; We have received parts of the actual group info - either the
3763         ;; select method or the group parameters.        We first check
3764         ;; whether we have to extend the info, and if so, do that.
3765         (let ((len (length info))
3766               (total (if (eq part 'method) 5 6)))
3767           (when (< len total)
3768             (setcdr (nthcdr (1- len) info)
3769                     (make-list (- total len) nil)))
3770           ;; Then we enter the new info.
3771           (setcar (nthcdr (1- total) info) part-info)))
3772       (unless entry
3773         ;; This is a new group, so we just create it.
3774         (save-excursion
3775           (set-buffer gnus-group-buffer)
3776           (setq method (gnus-info-method info))
3777           (when (gnus-server-equal method "native")
3778             (setq method nil))
3779           (save-excursion
3780             (set-buffer gnus-group-buffer)
3781             (if method
3782                 ;; It's a foreign group...
3783                 (gnus-group-make-group
3784                  (gnus-group-real-name (gnus-info-group info))
3785                  (if (stringp method) method
3786                    (prin1-to-string (car method)))
3787                  (and (consp method)
3788                       (nth 1 (gnus-info-method info))))
3789               ;; It's a native group.
3790               (gnus-group-make-group (gnus-info-group info))))
3791           (gnus-message 6 "Note: New group created")
3792           (setq entry
3793                 (gnus-gethash (gnus-group-prefixed-name
3794                                (gnus-group-real-name (gnus-info-group info))
3795                                (or (gnus-info-method info) gnus-select-method))
3796                               gnus-newsrc-hashtb))))
3797       ;; Whether it was a new group or not, we now have the entry, so we
3798       ;; can do the update.
3799       (if entry
3800           (progn
3801             (setcar (nthcdr 2 entry) info)
3802             (when (and (not (eq (car entry) t))
3803                        (gnus-active (gnus-info-group info)))
3804               (setcar entry (length
3805                              (gnus-list-of-unread-articles (car info))))))
3806         (error "No such group: %s" (gnus-info-group info))))))
3807
3808 (defun gnus-group-set-method-info (group select-method)
3809   (gnus-group-set-info select-method group 'method))
3810
3811 (defun gnus-group-set-params-info (group params)
3812   (gnus-group-set-info params group 'params))
3813
3814 (defun gnus-add-marked-articles (group type articles &optional info force)
3815   ;; Add ARTICLES of TYPE to the info of GROUP.
3816   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
3817   ;; add, but replace marked articles of TYPE with ARTICLES.
3818   (let ((info (or info (gnus-get-info group)))
3819         marked m)
3820     (or (not info)
3821         (and (not (setq marked (nthcdr 3 info)))
3822              (or (null articles)
3823                  (setcdr (nthcdr 2 info)
3824                          (list (list (cons type (gnus-compress-sequence
3825                                                  articles t)))))))
3826         (and (not (setq m (assq type (car marked))))
3827              (or (null articles)
3828                  (setcar marked
3829                          (cons (cons type (gnus-compress-sequence articles t) )
3830                                (car marked)))))
3831         (if force
3832             (if (null articles)
3833                 (setcar (nthcdr 3 info)
3834                         (gnus-delete-alist type (car marked)))
3835               (setcdr m (gnus-compress-sequence articles t)))
3836           (setcdr m (gnus-compress-sequence
3837                      (sort (nconc (gnus-uncompress-range (cdr m))
3838                                   (copy-sequence articles)) '<) t))))))
3839
3840 (defun gnus-add-mark (group mark article)
3841   "Mark ARTICLE in GROUP with MARK, whether the group is displayed or not."
3842   (let ((buffer (gnus-summary-buffer-name group)))
3843     (if (gnus-buffer-live-p buffer)
3844         (save-excursion
3845           (set-buffer (get-buffer buffer))
3846           (gnus-summary-add-mark article mark))
3847       (gnus-add-marked-articles group (cdr (assq mark gnus-article-mark-lists))
3848                                 (list article)))))
3849
3850 ;;;
3851 ;;; Group timestamps
3852 ;;;
3853
3854 (defun gnus-group-set-timestamp ()
3855   "Change the timestamp of the current group to the current time.
3856 This function can be used in hooks like `gnus-select-group-hook'
3857 or `gnus-group-catchup-group-hook'."
3858   (when gnus-newsgroup-name
3859     (let ((time (current-time)))
3860       (setcdr (cdr time) nil)
3861       (gnus-group-set-parameter gnus-newsgroup-name 'timestamp time))))
3862
3863 (defsubst gnus-group-timestamp (group)
3864   "Return the timestamp for GROUP."
3865   (gnus-group-get-parameter group 'timestamp t))
3866
3867 (defun gnus-group-timestamp-delta (group)
3868   "Return the offset in seconds from the timestamp for GROUP to the current time, as a floating point number."
3869   (let* ((time (or (gnus-group-timestamp group)
3870                    (list 0 0)))
3871          (delta (subtract-time (current-time) time)))
3872     (+ (* (nth 0 delta) 65536.0)
3873        (nth 1 delta))))
3874
3875 (defun gnus-group-timestamp-string (group)
3876   "Return a string of the timestamp for GROUP."
3877   (let ((time (gnus-group-timestamp group)))
3878     (if (not time)
3879         ""
3880       (gnus-time-iso8601 time))))
3881
3882 (defun gnus-group-list-cached (level &optional lowest)
3883   "List all groups with cached articles.
3884 If the prefix LEVEL is non-nil, it should be a number that says which
3885 level to cut off listing groups.
3886 If LOWEST, don't list groups with level lower than LOWEST.
3887
3888 This command may read the active file."
3889   (interactive "P")
3890   (when level
3891     (setq level (prefix-numeric-value level)))
3892   (when (or (not level) (>= level gnus-level-zombie))
3893     (gnus-cache-open))
3894   (funcall gnus-group-prepare-function
3895            (or level gnus-level-subscribed)
3896            #'(lambda (info)
3897                (let ((marks (gnus-info-marks info)))
3898                  (assq 'cache marks)))
3899            lowest
3900            #'(lambda (group)
3901                (or (gnus-gethash group
3902                                  gnus-cache-active-hashtb)
3903                    ;; Cache active file might use "."
3904                    ;; instead of ":".
3905                    (gnus-gethash
3906                     (mapconcat 'identity
3907                                (split-string group ":")
3908                                ".")
3909                     gnus-cache-active-hashtb))))
3910   (goto-char (point-min))
3911   (gnus-group-position-point))
3912
3913 (defun gnus-group-list-dormant (level &optional lowest)
3914   "List all groups with dormant articles.
3915 If the prefix LEVEL is non-nil, it should be a number that says which
3916 level to cut off listing groups.
3917 If LOWEST, don't list groups with level lower than LOWEST.
3918
3919 This command may read the active file."
3920   (interactive "P")
3921   (when level
3922     (setq level (prefix-numeric-value level)))
3923   (when (or (not level) (>= level gnus-level-zombie))
3924     (gnus-cache-open))
3925   (funcall gnus-group-prepare-function
3926            (or level gnus-level-subscribed)
3927            #'(lambda (info)
3928                (let ((marks (gnus-info-marks info)))
3929                  (assq 'dormant marks)))
3930            lowest
3931            'ignore)
3932   (goto-char (point-min))
3933   (gnus-group-position-point))
3934
3935 (defun gnus-group-listed-groups ()
3936   "Return a list of listed groups."
3937   (let (point groups)
3938     (goto-char (point-min))
3939     (while (setq point (text-property-not-all (point) (point-max)
3940                                               'gnus-group nil))
3941       (goto-char point)
3942       (push (symbol-name (get-text-property point 'gnus-group)) groups)
3943       (forward-char 1))
3944     groups))
3945
3946 (defun gnus-group-list-plus (&optional args)
3947   "List groups plus the current selection."
3948   (interactive "P")
3949   (let ((gnus-group-listed-groups (gnus-group-listed-groups))
3950         (gnus-group-list-mode gnus-group-list-mode) ;; Save it.
3951         func)
3952     (push last-command-event unread-command-events)
3953     (if (featurep 'xemacs)
3954         (push (make-event 'key-press '(key ?A)) unread-command-events)
3955       (push ?A unread-command-events))
3956     (let (gnus-pick-mode keys)
3957       (setq keys (if (featurep 'xemacs)
3958                      (events-to-keys (read-key-sequence nil))
3959                    (read-key-sequence nil)))
3960       (setq func (lookup-key (current-local-map) keys)))
3961     (if (or (not func)
3962             (numberp func))
3963         (ding)
3964       (call-interactively func))))
3965
3966 (defun gnus-group-list-flush (&optional args)
3967   "Flush groups from the current selection."
3968   (interactive "P")
3969   (let ((gnus-group-list-option 'flush))
3970     (gnus-group-list-plus args)))
3971
3972 (defun gnus-group-list-limit (&optional args)
3973   "List groups limited within the current selection."
3974   (interactive "P")
3975   (let ((gnus-group-list-option 'limit))
3976     (gnus-group-list-plus args)))
3977
3978 (provide 'gnus-group)
3979
3980 ;;; gnus-group.el ends here