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