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