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