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