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