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