*** empty log message ***
[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        ["Edit kill file" gnus-group-edit-local-kill
762         (gnus-group-group-name)]
763        ;; Actually one should check, if any of the marked groups gives t for
764        ;; (gnus-check-backend-function 'request-expire-articles ...)
765        ["Expire articles" gnus-group-expire-articles
766         (or (and (gnus-group-group-name)
767                  (gnus-check-backend-function
768                   'request-expire-articles
769                   (gnus-group-group-name))) gnus-group-marked)]
770        ["Set group level" gnus-group-set-current-level
771         (gnus-group-group-name)]
772        ["Select quick" gnus-group-quick-select-group (gnus-group-group-name)]
773        ))
774   
775     (easy-menu-define
776      gnus-group-group-menu gnus-group-mode-map ""
777      '("Groups"
778        ("Listing"
779         ["List unread subscribed groups" gnus-group-list-groups t]
780         ["List (un)subscribed groups" gnus-group-list-all-groups t]
781         ["List killed groups" gnus-group-list-killed gnus-killed-list]
782         ["List zombie groups" gnus-group-list-zombies gnus-zombie-list]
783         ["List level..." gnus-group-list-level t]
784         ["Describe all groups" gnus-group-describe-all-groups t]
785         ["Group apropos..." gnus-group-apropos t]
786         ["Group and description apropos..." gnus-group-description-apropos t]
787         ["List groups matching..." gnus-group-list-matching t]
788         ["List all groups matching..." gnus-group-list-all-matching t]
789         ["List active file" gnus-group-list-active t])
790        ("Sort"
791         ["Default sort" gnus-group-sort-groups t]
792         ["Sort by method" gnus-group-sort-groups-by-method t]
793         ["Sort by rank" gnus-group-sort-groups-by-rank t]
794         ["Sort by score" gnus-group-sort-groups-by-score t]
795         ["Sort by level" gnus-group-sort-groups-by-level t]
796         ["Sort by unread" gnus-group-sort-groups-by-unread t]
797         ["Sort by name" gnus-group-sort-groups-by-alphabet t])
798        ("Sort process/prefixed"
799         ["Default sort" gnus-group-sort-selected-groups
800          (or (not (boundp 'gnus-topic-mode)) (not gnus-topic-mode))]
801         ["Sort by method" gnus-group-sort-selected-groups-by-method
802          (or (not (boundp 'gnus-topic-mode)) (not gnus-topic-mode))]
803         ["Sort by rank" gnus-group-sort-selected-groups-by-rank
804          (or (not (boundp 'gnus-topic-mode)) (not gnus-topic-mode))]
805         ["Sort by score" gnus-group-sort-selected-groups-by-score
806          (or (not (boundp 'gnus-topic-mode)) (not gnus-topic-mode))]
807         ["Sort by level" gnus-group-sort-selected-groups-by-level
808          (or (not (boundp 'gnus-topic-mode)) (not gnus-topic-mode))]
809         ["Sort by unread" gnus-group-sort-selected-groups-by-unread
810          (or (not (boundp 'gnus-topic-mode)) (not gnus-topic-mode))]
811         ["Sort by name" gnus-group-sort-selected-groups-by-alphabet
812          (or (not (boundp 'gnus-topic-mode)) (not gnus-topic-mode))])
813        ("Mark"
814         ["Mark group" gnus-group-mark-group
815          (and (gnus-group-group-name)
816               (not (memq (gnus-group-group-name) gnus-group-marked)))]
817         ["Unmark group" gnus-group-unmark-group
818          (and (gnus-group-group-name)
819               (memq (gnus-group-group-name) gnus-group-marked))]
820         ["Unmark all" gnus-group-unmark-all-groups gnus-group-marked]
821         ["Mark regexp..." gnus-group-mark-regexp t]
822         ["Mark region" gnus-group-mark-region t]
823         ["Mark buffer" gnus-group-mark-buffer t]
824         ["Execute command" gnus-group-universal-argument
825          (or gnus-group-marked (gnus-group-group-name))])
826        ("Subscribe"
827         ["Subscribe to a group" gnus-group-unsubscribe-group t]
828         ["Kill all newsgroups in region" gnus-group-kill-region t]
829         ["Kill all zombie groups" gnus-group-kill-all-zombies
830          gnus-zombie-list]
831         ["Kill all groups on level..." gnus-group-kill-level t])
832        ("Foreign groups"
833         ["Make a foreign group" gnus-group-make-group t]
834         ["Add a directory group" gnus-group-make-directory-group t]
835         ["Add the help group" gnus-group-make-help-group t]
836         ["Add the archive group" gnus-group-make-archive-group t]
837         ["Make a doc group" gnus-group-make-doc-group t]
838         ["Make a web group" gnus-group-make-web-group t]
839         ["Make a kiboze group" gnus-group-make-kiboze-group t]
840         ["Make a virtual group" gnus-group-make-empty-virtual t]
841         ["Add a group to a virtual" gnus-group-add-to-virtual t]
842         ["Rename group" gnus-group-rename-group
843          (gnus-check-backend-function
844           'request-rename-group (gnus-group-group-name))]
845         ["Delete group" gnus-group-delete-group
846          (gnus-check-backend-function
847           'request-delete-group (gnus-group-group-name))])
848        ("Editing groups"
849         ["Parameters" gnus-group-edit-group-parameters
850          (gnus-group-group-name)]
851         ["Select method" gnus-group-edit-group-method
852          (gnus-group-group-name)]
853         ["Info" gnus-group-edit-group (gnus-group-group-name)])
854        ("Score file"
855         ["Flush cache" gnus-score-flush-cache t])
856        ("Move"
857         ["Next" gnus-group-next-group t]
858         ["Previous" gnus-group-prev-group t]
859         ["Next unread" gnus-group-next-unread-group t]
860         ["Previous unread" gnus-group-prev-unread-group t]
861         ["Next unread same level" gnus-group-next-unread-group-same-level t]
862         ["Previous unread same level"
863          gnus-group-prev-unread-group-same-level t]
864         ["Jump to group" gnus-group-jump-to-group t]
865         ["First unread group" gnus-group-first-unread-group t]
866         ["Best unread group" gnus-group-best-unread-group t])
867        ["Transpose" gnus-group-transpose-groups
868         (gnus-group-group-name)]
869        ["Read a directory as a group..." gnus-group-enter-directory t]
870        ))
871
872     (easy-menu-define
873      gnus-group-misc-menu gnus-group-mode-map ""
874      '("Misc"
875        ["Send a bug report" gnus-bug t]
876        ["Customize" gnus-group-customize t]
877        ["Send a mail" gnus-group-mail t]
878        ["Post an article..." gnus-group-post-news t]
879        ["Check for new news" gnus-group-get-new-news t]     
880        ["Activate all groups" gnus-activate-all-groups t]
881        ["Delete bogus groups" gnus-group-check-bogus-groups t]
882        ["Find new newsgroups" gnus-find-new-newsgroups t]
883        ["Restart Gnus" gnus-group-restart t]
884        ["Read init file" gnus-group-read-init-file t]
885        ["Browse foreign server" gnus-group-browse-foreign-server t]
886        ["Enter server buffer" gnus-group-enter-server-mode t]
887        ["Expire all expirable articles" gnus-group-expire-all-groups t]
888        ["Generate any kiboze groups" nnkiboze-generate-groups t]
889        ["Gnus version" gnus-version t]
890        ["Save .newsrc files" gnus-group-save-newsrc t]
891        ["Suspend Gnus" gnus-group-suspend t]
892        ["Clear dribble buffer" gnus-group-clear-dribble t]
893        ["Edit global kill file" gnus-group-edit-global-kill t]
894        ["Read manual" gnus-info-find-node t]
895        ["Toggle topics" gnus-topic-mode t]
896        ("SOUP"
897         ["Pack replies" nnsoup-pack-replies (fboundp 'nnsoup-request-group)]
898         ["Send replies" gnus-soup-send-replies
899          (fboundp 'gnus-soup-pack-packet)]
900         ["Pack packet" gnus-soup-pack-packet (fboundp 'gnus-soup-pack-packet)]
901         ["Save areas" gnus-soup-save-areas (fboundp 'gnus-soup-pack-packet)]
902         ["Brew SOUP" gnus-soup-brew-soup (fboundp 'gnus-soup-pack-packet)])
903        ["Exit from Gnus" gnus-group-exit t]
904        ["Exit without saving" gnus-group-quit t]
905        ))
906
907     (run-hooks 'gnus-group-menu-hook)
908     ))
909
910 (defun gnus-group-mode ()
911   "Major mode for reading news.
912
913 All normal editing commands are switched off.
914 \\<gnus-group-mode-map>
915 The group buffer lists (some of) the groups available.  For instance,
916 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
917 lists all zombie groups.
918
919 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
920 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
921
922 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
923
924 The following commands are available:
925
926 \\{gnus-group-mode-map}"
927   (interactive)
928   (when (and menu-bar-mode
929              (gnus-visual-p 'group-menu 'menu))
930     (gnus-group-make-menu-bar))
931   (kill-all-local-variables)
932   (gnus-simplify-mode-line)
933   (setq major-mode 'gnus-group-mode)
934   (setq mode-name "Group")
935   (gnus-group-set-mode-line)
936   (setq mode-line-process nil)
937   (use-local-map gnus-group-mode-map)
938   (buffer-disable-undo (current-buffer))
939   (setq truncate-lines t)
940   (setq buffer-read-only t)
941   (gnus-set-default-directory)
942   (gnus-update-format-specifications nil 'group 'group-mode)
943   (gnus-update-group-mark-positions)
944   (gnus-make-local-hook 'post-command-hook)
945   (gnus-add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
946   (when gnus-use-undo
947     (gnus-undo-mode 1))
948   (run-hooks 'gnus-group-mode-hook))
949
950 (defun gnus-update-group-mark-positions ()
951   (save-excursion
952     (let ((gnus-process-mark 128)
953           (gnus-group-marked '("dummy.group"))
954           (gnus-active-hashtb (make-vector 10 0)))
955       (gnus-set-active "dummy.group" '(0 . 0))
956       (gnus-set-work-buffer)
957       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
958       (goto-char (point-min))
959       (setq gnus-group-mark-positions
960             (list (cons 'process (and (search-forward "\200" nil t)
961                                       (- (point) 2))))))))
962
963 (defun gnus-clear-inboxes-moved ()
964   (setq nnmail-moved-inboxes nil))
965
966 (defun gnus-mouse-pick-group (e)
967   "Enter the group under the mouse pointer."
968   (interactive "e")
969   (mouse-set-point e)
970   (gnus-group-read-group nil))
971
972 ;; Look at LEVEL and find out what the level is really supposed to be.
973 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
974 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
975 (defun gnus-group-default-level (&optional level number-or-nil)
976   (cond
977    (gnus-group-use-permanent-levels
978     (or (setq gnus-group-use-permanent-levels
979               (or level (if (numberp gnus-group-use-permanent-levels)
980                             gnus-group-use-permanent-levels
981                           (or gnus-group-default-list-level
982                               gnus-level-subscribed))))
983         gnus-group-default-list-level gnus-level-subscribed))
984    (number-or-nil
985     level)
986    (t
987     (or level gnus-group-default-list-level gnus-level-subscribed))))
988
989 (defun gnus-group-setup-buffer ()
990   (switch-to-buffer gnus-group-buffer)
991   (unless (eq major-mode 'gnus-group-mode)
992     (gnus-add-current-to-buffer-list)
993     (gnus-group-mode)
994     (when gnus-carpal
995       (gnus-carpal-setup-buffer 'group))))
996
997 (defun gnus-group-list-groups (&optional level unread lowest)
998   "List newsgroups with level LEVEL or lower that have unread articles.
999 Default is all subscribed groups.
1000 If argument UNREAD is non-nil, groups with no unread articles are also
1001 listed."
1002   (interactive
1003    (list (if current-prefix-arg
1004              (prefix-numeric-value current-prefix-arg)
1005            (or
1006             (gnus-group-default-level nil t)
1007             gnus-group-default-list-level
1008             gnus-level-subscribed))))
1009   ;; Just do this here, for no particular good reason.
1010   (gnus-clear-inboxes-moved)
1011   (unless level
1012     (setq level (car gnus-group-list-mode)
1013           unread (cdr gnus-group-list-mode)))
1014   (setq level (gnus-group-default-level level))
1015   (gnus-group-setup-buffer)             ;May call from out of group buffer
1016   (gnus-update-format-specifications nil 'group 'group-mode)
1017   (let ((case-fold-search nil)
1018         (props (text-properties-at (gnus-point-at-bol)))
1019         (group (gnus-group-group-name)))
1020     (set-buffer gnus-group-buffer)
1021     (funcall gnus-group-prepare-function level unread lowest)
1022     (if (zerop (buffer-size))
1023         (gnus-message 5 gnus-no-groups-message)
1024       (goto-char (point-max))
1025       (when (or (not gnus-group-goto-next-group-function)
1026                 (not (funcall gnus-group-goto-next-group-function 
1027                               group props)))
1028         (if (not group)
1029             ;; Go to the first group with unread articles.
1030             (gnus-group-search-forward t)
1031           ;; Find the right group to put point on.  If the current group
1032           ;; has disappeared in the new listing, try to find the next
1033           ;; one.        If no next one can be found, just leave point at the
1034           ;; first newsgroup in the buffer.
1035           (when (not (gnus-goto-char
1036                       (text-property-any
1037                        (point-min) (point-max)
1038                        'gnus-group (gnus-intern-safe
1039                                     group gnus-active-hashtb))))
1040             (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
1041               (while (and newsrc
1042                           (not (gnus-goto-char
1043                                 (text-property-any
1044                                  (point-min) (point-max) 'gnus-group
1045                                  (gnus-intern-safe
1046                                   (caar newsrc) gnus-active-hashtb)))))
1047                 (setq newsrc (cdr newsrc)))
1048               (unless newsrc
1049                 (goto-char (point-max))
1050                 (forward-line -1))))))
1051       ;; Adjust cursor point.
1052       (gnus-group-position-point))))
1053
1054 (defun gnus-group-list-level (level &optional all)
1055   "List groups on LEVEL.
1056 If ALL (the prefix), also list groups that have no unread articles."
1057   (interactive "nList groups on level: \nP")
1058   (gnus-group-list-groups level all level))
1059
1060 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
1061   "List all newsgroups with unread articles of level LEVEL or lower.
1062 If ALL is non-nil, list groups that have no unread articles.
1063 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
1064 If REGEXP, only list groups matching REGEXP."
1065   (set-buffer gnus-group-buffer)
1066   (let ((buffer-read-only nil)
1067         (newsrc (cdr gnus-newsrc-alist))
1068         (lowest (or lowest 1))
1069         info clevel unread group params)
1070     (erase-buffer)
1071     (when (< lowest gnus-level-zombie)
1072       ;; List living groups.
1073       (while newsrc
1074         (setq info (car newsrc)
1075               group (gnus-info-group info)
1076               params (gnus-info-params info)
1077               newsrc (cdr newsrc)
1078               unread (car (gnus-gethash group gnus-newsrc-hashtb)))
1079         (and unread                     ; This group might be bogus
1080              (or (not regexp)
1081                  (string-match regexp group))
1082              (<= (setq clevel (gnus-info-level info)) level)
1083              (>= clevel lowest)
1084              (or all                    ; We list all groups?
1085                  (if (eq unread t)      ; Unactivated?
1086                      gnus-group-list-inactive-groups ; We list unactivated 
1087                    (> unread 0))        ; We list groups with unread articles
1088                  (and gnus-list-groups-with-ticked-articles
1089                       (cdr (assq 'tick (gnus-info-marks info))))
1090                                         ; And groups with tickeds
1091                  ;; Check for permanent visibility.
1092                  (and gnus-permanently-visible-groups
1093                       (string-match gnus-permanently-visible-groups
1094                                     group))
1095                  (memq 'visible params)
1096                  (cdr (assq 'visible params)))
1097              (gnus-group-insert-group-line
1098               group (gnus-info-level info)
1099               (gnus-info-marks info) unread (gnus-info-method info)))))
1100
1101     ;; List dead groups.
1102     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
1103          (gnus-group-prepare-flat-list-dead
1104           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
1105           gnus-level-zombie ?Z
1106           regexp))
1107     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
1108          (gnus-group-prepare-flat-list-dead
1109           (setq gnus-killed-list (sort gnus-killed-list 'string<))
1110           gnus-level-killed ?K regexp))
1111
1112     (gnus-group-set-mode-line)
1113     (setq gnus-group-list-mode (cons level all))
1114     (run-hooks 'gnus-group-prepare-hook)))
1115
1116 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
1117   ;; List zombies and killed lists somewhat faster, which was
1118   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
1119   ;; this by ignoring the group format specification altogether.
1120   (let (group)
1121     (if regexp
1122         ;; This loop is used when listing groups that match some
1123         ;; regexp.
1124         (while groups
1125           (setq group (pop groups))
1126           (when (string-match regexp group)
1127             (gnus-add-text-properties
1128              (point) (prog1 (1+ (point))
1129                        (insert " " mark "     *: " group "\n"))
1130              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
1131                    'gnus-unread t
1132                    'gnus-level level))))
1133       ;; This loop is used when listing all groups.
1134       (while groups
1135         (gnus-add-text-properties
1136          (point) (prog1 (1+ (point))
1137                    (insert " " mark "     *: "
1138                            (setq group (pop groups)) "\n"))
1139          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
1140                'gnus-unread t
1141                'gnus-level level))))))
1142
1143 (defun gnus-group-update-group-line ()
1144   "Update the current line in the group buffer."
1145   (let* ((buffer-read-only nil)
1146          (group (gnus-group-group-name))
1147          (entry (and group (gnus-gethash group gnus-newsrc-hashtb)))
1148          gnus-group-indentation)
1149     (when group
1150       (and entry
1151            (not (gnus-ephemeral-group-p group))
1152            (gnus-dribble-enter
1153             (concat "(gnus-group-set-info '"
1154                     (gnus-prin1-to-string (nth 2 entry))
1155                     ")")))
1156       (setq gnus-group-indentation (gnus-group-group-indentation))
1157       (gnus-delete-line)
1158       (gnus-group-insert-group-line-info group)
1159       (forward-line -1)
1160       (gnus-group-position-point))))
1161
1162 (defun gnus-group-insert-group-line-info (group)
1163   "Insert GROUP on the current line."
1164   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
1165         (gnus-group-indentation (gnus-group-group-indentation))
1166         active info)
1167     (if entry
1168         (progn
1169           ;; (Un)subscribed group.
1170           (setq info (nth 2 entry))
1171           (gnus-group-insert-group-line
1172            group (gnus-info-level info) (gnus-info-marks info)
1173            (or (car entry) t) (gnus-info-method info)))
1174       ;; This group is dead.
1175       (gnus-group-insert-group-line
1176        group
1177        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
1178        nil
1179        (if (setq active (gnus-active group))
1180            (if (zerop (cdr active))
1181                0
1182              (- (1+ (cdr active)) (car active)))
1183          0)
1184        nil))))
1185
1186 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
1187                                                     gnus-tmp-marked number
1188                                                     gnus-tmp-method)
1189   "Insert a group line in the group buffer."
1190   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
1191          (gnus-tmp-number-total
1192           (if gnus-tmp-active
1193               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
1194             0))
1195          (gnus-tmp-number-of-unread
1196           (if (numberp number) (int-to-string (max 0 number))
1197             "*"))
1198          (gnus-tmp-number-of-read
1199           (if (numberp number)
1200               (int-to-string (max 0 (- gnus-tmp-number-total number)))
1201             "*"))
1202          (gnus-tmp-subscribed
1203           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
1204                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
1205                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
1206                 (t ?K)))
1207          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
1208          (gnus-tmp-newsgroup-description
1209           (if gnus-description-hashtb
1210               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
1211             ""))
1212          (gnus-tmp-moderated
1213           (if (and gnus-moderated-hashtb
1214                    (gnus-gethash gnus-tmp-group gnus-moderated-hashtb))
1215               ?m ? ))
1216          (gnus-tmp-moderated-string
1217           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
1218          (gnus-tmp-method
1219           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
1220          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
1221          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
1222          (gnus-tmp-news-method-string
1223           (if gnus-tmp-method
1224               (format "(%s:%s)" (car gnus-tmp-method)
1225                       (cadr gnus-tmp-method)) ""))
1226          (gnus-tmp-marked-mark
1227           (if (and (numberp number)
1228                    (zerop number)
1229                    (cdr (assq 'tick gnus-tmp-marked)))
1230               ?* ? ))
1231          (gnus-tmp-process-marked
1232           (if (member gnus-tmp-group gnus-group-marked)
1233               gnus-process-mark ? ))
1234          (gnus-tmp-grouplens
1235           (or (and gnus-use-grouplens
1236                    (bbb-grouplens-group-p gnus-tmp-group))
1237               ""))
1238          (buffer-read-only nil)
1239          header gnus-tmp-header)        ; passed as parameter to user-funcs.
1240     (beginning-of-line)
1241     (gnus-add-text-properties
1242      (point)
1243      (prog1 (1+ (point))
1244        ;; Insert the text.
1245        (eval gnus-group-line-format-spec))
1246      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
1247                   gnus-unread ,(if (numberp number)
1248                                    (string-to-int gnus-tmp-number-of-unread)
1249                                  t)
1250                   gnus-marked ,gnus-tmp-marked-mark
1251                   gnus-indentation ,gnus-group-indentation
1252                   gnus-level ,gnus-tmp-level))
1253     (when (inline (gnus-visual-p 'group-highlight 'highlight))
1254       (forward-line -1)
1255       (run-hooks 'gnus-group-update-hook)
1256       (forward-line))
1257     ;; Allow XEmacs to remove front-sticky text properties.
1258     (gnus-group-remove-excess-properties)))
1259
1260 (defun gnus-group-highlight-line ()
1261   "Highlight the current line according to `gnus-group-highlight'."
1262   (let* ((list gnus-group-highlight)
1263          (p (point))
1264          (end (progn (end-of-line) (point)))
1265          ;; now find out where the line starts and leave point there.
1266          (beg (progn (beginning-of-line) (point)))
1267          (group (gnus-group-group-name))
1268          (entry (gnus-group-entry group))
1269          (unread (if (numberp (car entry)) (car entry) 0))
1270          (active (gnus-active group))
1271          (total (if active (1+ (- (cdr active) (car active))) 0))
1272          (info (nth 2 entry))
1273          (method (gnus-server-get-method group (gnus-info-method info)))
1274          (marked (gnus-info-marks info))
1275          (mailp (memq 'mail (assoc (symbol-name
1276                                     (car (or method gnus-select-method)))
1277                                    gnus-valid-select-methods)))
1278          (level (or (gnus-info-level info) 9))
1279          (score (or (gnus-info-score info) 0))
1280          (ticked (gnus-range-length (cdr (assq 'tick marked))))
1281          (group-age (gnus-group-timestamp-delta group))
1282          (inhibit-read-only t))
1283     ;; Eval the cars of the lists until we find a match.
1284     (while (and list
1285                 (not (eval (caar list))))
1286       (setq list (cdr list)))
1287     (let ((face (cdar list)))
1288       (unless (eq face (get-text-property beg 'face))
1289         (gnus-put-text-property 
1290          beg end 'face 
1291          (setq face (if (boundp face) (symbol-value face) face)))
1292         (gnus-extent-start-open beg)))
1293     (goto-char p)))
1294
1295 (defun gnus-group-update-group (group &optional visible-only)
1296   "Update all lines where GROUP appear.
1297 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
1298 already."
1299   (save-excursion
1300     (set-buffer gnus-group-buffer)
1301     ;; The buffer may be narrowed.
1302     (save-restriction
1303       (widen)
1304       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
1305             (loc (point-min))
1306             found buffer-read-only)
1307         ;; Enter the current status into the dribble buffer.
1308         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
1309           (when (and entry (not (gnus-ephemeral-group-p group)))
1310             (gnus-dribble-enter
1311              (concat "(gnus-group-set-info '" 
1312                      (gnus-prin1-to-string (nth 2 entry))
1313                      ")"))))
1314         ;; Find all group instances.  If topics are in use, each group
1315         ;; may be listed in more than once.
1316         (while (setq loc (text-property-any
1317                           loc (point-max) 'gnus-group ident))
1318           (setq found t)
1319           (goto-char loc)
1320           (let ((gnus-group-indentation (gnus-group-group-indentation)))
1321             (gnus-delete-line)
1322             (gnus-group-insert-group-line-info group)
1323             (save-excursion
1324               (forward-line -1)
1325               (run-hooks 'gnus-group-update-group-hook)))
1326           (setq loc (1+ loc)))
1327         (unless (or found visible-only)
1328           ;; No such line in the buffer, find out where it's supposed to
1329           ;; go, and insert it there (or at the end of the buffer).
1330           (if gnus-goto-missing-group-function
1331               (funcall gnus-goto-missing-group-function group)
1332             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
1333               (while (and entry (car entry)
1334                           (not
1335                            (gnus-goto-char
1336                             (text-property-any
1337                              (point-min) (point-max)
1338                              'gnus-group (gnus-intern-safe
1339                                           (caar entry) gnus-active-hashtb)))))
1340                 (setq entry (cdr entry)))
1341               (or entry (goto-char (point-max)))))
1342           ;; Finally insert the line.
1343           (let ((gnus-group-indentation (gnus-group-group-indentation)))
1344             (gnus-group-insert-group-line-info group)
1345             (save-excursion
1346               (forward-line -1)
1347               (run-hooks 'gnus-group-update-group-hook))))
1348         (when gnus-group-update-group-function
1349           (funcall gnus-group-update-group-function group))
1350         (gnus-group-set-mode-line)))))
1351
1352 (defun gnus-group-set-mode-line ()
1353   "Update the mode line in the group buffer."
1354   (when (memq 'group gnus-updated-mode-lines)
1355     ;; Yes, we want to keep this mode line updated.
1356     (save-excursion
1357       (set-buffer gnus-group-buffer)
1358       (let* ((gformat (or gnus-group-mode-line-format-spec
1359                           (setq gnus-group-mode-line-format-spec
1360                                 (gnus-parse-format
1361                                  gnus-group-mode-line-format
1362                                  gnus-group-mode-line-format-alist))))
1363              (gnus-tmp-news-server (cadr gnus-select-method))
1364              (gnus-tmp-news-method (car gnus-select-method))
1365              (gnus-tmp-colon (if (equal gnus-tmp-news-server "") "" ":"))
1366              (max-len 60)
1367              gnus-tmp-header            ;Dummy binding for user-defined formats
1368              ;; Get the resulting string.
1369              (modified 
1370               (and gnus-dribble-buffer
1371                    (buffer-name gnus-dribble-buffer)
1372                    (buffer-modified-p gnus-dribble-buffer)
1373                    (save-excursion
1374                      (set-buffer gnus-dribble-buffer)
1375                      (not (zerop (buffer-size))))))
1376              (mode-string (eval gformat)))
1377         ;; Say whether the dribble buffer has been modified.
1378         (setq mode-line-modified
1379               (if modified "---*- " "----- "))
1380         ;; If the line is too long, we chop it off.
1381         (when (> (length mode-string) max-len)
1382           (setq mode-string (substring mode-string 0 (- max-len 4))))
1383         (prog1
1384             (setq mode-line-buffer-identification 
1385                   (gnus-mode-line-buffer-identification
1386                    (list mode-string)))
1387           (set-buffer-modified-p modified))))))
1388
1389 (defun gnus-group-group-name ()
1390   "Get the name of the newsgroup on the current line."
1391   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
1392     (and group (symbol-name group))))
1393
1394 (defun gnus-group-group-level ()
1395   "Get the level of the newsgroup on the current line."
1396   (get-text-property (gnus-point-at-bol) 'gnus-level))
1397
1398 (defun gnus-group-group-indentation ()
1399   "Get the indentation of the newsgroup on the current line."
1400   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
1401       (and gnus-group-indentation-function
1402            (funcall gnus-group-indentation-function))
1403       ""))
1404
1405 (defun gnus-group-group-unread ()
1406   "Get the number of unread articles of the newsgroup on the current line."
1407   (get-text-property (gnus-point-at-bol) 'gnus-unread))
1408
1409 (defun gnus-group-new-mail (group)
1410   (if (nnmail-new-mail-p (gnus-group-real-name group))
1411       gnus-new-mail-mark
1412     ? ))
1413
1414 (defun gnus-group-search-forward (&optional backward all level first-too)
1415   "Find the next newsgroup with unread articles.
1416 If BACKWARD is non-nil, find the previous newsgroup instead.
1417 If ALL is non-nil, just find any newsgroup.
1418 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
1419 group exists.
1420 If FIRST-TOO, the current line is also eligible as a target."
1421   (let ((way (if backward -1 1))
1422         (low gnus-level-killed)
1423         (beg (point))
1424         pos found lev)
1425     (if (and backward (progn (beginning-of-line)) (bobp))
1426         nil
1427       (unless first-too
1428         (forward-line way))
1429       (while (and
1430               (not (eobp))
1431               (not (setq
1432                     found
1433                     (and (or all
1434                              (and
1435                               (let ((unread
1436                                      (get-text-property (point) 'gnus-unread)))
1437                                 (and (numberp unread) (> unread 0)))
1438                               (setq lev (get-text-property (point)
1439                                                            'gnus-level))
1440                               (<= lev gnus-level-subscribed)))
1441                          (or (not level)
1442                              (and (setq lev (get-text-property (point)
1443                                                                'gnus-level))
1444                                   (or (= lev level)
1445                                       (and (< lev low)
1446                                            (< level lev)
1447                                            (progn
1448                                              (setq low lev)
1449                                              (setq pos (point))
1450                                              nil))))))))
1451               (zerop (forward-line way)))))
1452     (if found
1453         (progn (gnus-group-position-point) t)
1454       (goto-char (or pos beg))
1455       (and pos t))))
1456
1457 ;;; Gnus group mode commands
1458
1459 ;; Group marking.
1460
1461 (defun gnus-group-mark-group (n &optional unmark no-advance)
1462   "Mark the current group."
1463   (interactive "p")
1464   (let ((buffer-read-only nil)
1465         group)
1466     (while (and (> n 0)
1467                 (not (eobp)))
1468       (when (setq group (gnus-group-group-name))
1469         ;; Go to the mark position.
1470         (beginning-of-line)
1471         (forward-char (or (cdr (assq 'process gnus-group-mark-positions)) 2))
1472         (subst-char-in-region
1473          (point) (1+ (point)) (following-char)
1474          (if unmark
1475              (progn
1476                (setq gnus-group-marked (delete group gnus-group-marked))
1477                ? )
1478            (setq gnus-group-marked
1479                  (cons group (delete group gnus-group-marked)))
1480            gnus-process-mark)))
1481       (unless no-advance
1482         (gnus-group-next-group 1))
1483       (decf n))
1484     (gnus-summary-position-point)
1485     n))
1486
1487 (defun gnus-group-unmark-group (n)
1488   "Remove the mark from the current group."
1489   (interactive "p")
1490   (gnus-group-mark-group n 'unmark)
1491   (gnus-group-position-point))
1492
1493 (defun gnus-group-unmark-all-groups ()
1494   "Unmark all groups."
1495   (interactive)
1496   (let ((groups gnus-group-marked))
1497     (save-excursion
1498       (while groups
1499         (gnus-group-remove-mark (pop groups)))))
1500   (gnus-group-position-point))
1501
1502 (defun gnus-group-mark-region (unmark beg end)
1503   "Mark all groups between point and mark.
1504 If UNMARK, remove the mark instead."
1505   (interactive "P\nr")
1506   (let ((num (count-lines beg end)))
1507     (save-excursion
1508       (goto-char beg)
1509       (- num (gnus-group-mark-group num unmark)))))
1510
1511 (defun gnus-group-mark-buffer (&optional unmark)
1512   "Mark all groups in the buffer.
1513 If UNMARK, remove the mark instead."
1514   (interactive "P")
1515   (gnus-group-mark-region unmark (point-min) (point-max)))
1516
1517 (defun gnus-group-mark-regexp (regexp)
1518   "Mark all groups that match some regexp."
1519   (interactive "sMark (regexp): ")
1520   (let ((alist (cdr gnus-newsrc-alist))
1521         group)
1522     (while alist
1523       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
1524         (gnus-group-set-mark group))))
1525   (gnus-group-position-point))
1526
1527 (defun gnus-group-remove-mark (group)
1528   "Remove the process mark from GROUP and move point there.
1529 Return nil if the group isn't displayed."
1530   (if (gnus-group-goto-group group)
1531       (save-excursion
1532         (gnus-group-mark-group 1 'unmark t)
1533         t)
1534     (setq gnus-group-marked
1535           (delete group gnus-group-marked))
1536     nil))
1537
1538 (defun gnus-group-set-mark (group)
1539   "Set the process mark on GROUP."
1540   (if (gnus-group-goto-group group)
1541       (save-excursion
1542         (gnus-group-mark-group 1 nil t))
1543     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
1544
1545 (defun gnus-group-universal-argument (arg &optional groups func)
1546   "Perform any command on all groups according to the process/prefix convention."
1547   (interactive "P")
1548   (if (eq (setq func (or func
1549                          (key-binding
1550                           (read-key-sequence
1551                            (substitute-command-keys
1552                             "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
1553           'undefined)
1554       (gnus-error 1 "Undefined key")
1555     (gnus-group-iterate arg
1556       (lambda (group)
1557         (command-execute func))))
1558   (gnus-group-position-point))
1559
1560 (defun gnus-group-process-prefix (n)
1561   "Return a list of groups to work on.
1562 Take into consideration N (the prefix) and the list of marked groups."
1563   (cond
1564    (n
1565     (setq n (prefix-numeric-value n))
1566     ;; There is a prefix, so we return a list of the N next
1567     ;; groups.
1568     (let ((way (if (< n 0) -1 1))
1569           (n (abs n))
1570           group groups)
1571       (save-excursion
1572         (while (and (> n 0)
1573                     (setq group (gnus-group-group-name)))
1574           (push group groups)
1575           (setq n (1- n))
1576           (gnus-group-next-group way)))
1577       (nreverse groups)))
1578    ((and (boundp 'transient-mark-mode)
1579          transient-mark-mode
1580          (boundp 'mark-active)
1581          mark-active)
1582     ;; Work on the region between point and mark.
1583     (let ((max (max (point) (mark)))
1584           groups)
1585       (save-excursion
1586         (goto-char (min (point) (mark)))
1587         (while
1588             (and
1589              (push (gnus-group-group-name) groups)
1590              (zerop (gnus-group-next-group 1))
1591              (< (point) max)))
1592         (nreverse groups))))
1593    (gnus-group-marked
1594     ;; No prefix, but a list of marked articles.
1595     (reverse gnus-group-marked))
1596    (t
1597     ;; Neither marked articles or a prefix, so we return the
1598     ;; current group.
1599     (let ((group (gnus-group-group-name)))
1600       (and group (list group))))))
1601
1602 (defun gnus-group-iterate (arg function)
1603   "Iterate FUNCTION over all process/prefixed groups.
1604 FUNCTION will be called with the group name as the paremeter
1605 and with point over the group in question."
1606   (let ((groups (gnus-group-process-prefix arg))
1607         group)
1608     (while (setq group (pop groups))
1609       (gnus-group-remove-mark group)
1610       (funcall function group))))
1611
1612 (put 'gnus-group-iterate 'lisp-indent-function 1)
1613
1614 ;; Selecting groups.
1615
1616 (defun gnus-group-read-group (&optional all no-article group)
1617   "Read news in this newsgroup.
1618 If the prefix argument ALL is non-nil, already read articles become
1619 readable.  IF ALL is a number, fetch this number of articles.  If the
1620 optional argument NO-ARTICLE is non-nil, no article will be
1621 auto-selected upon group entry.  If GROUP is non-nil, fetch that
1622 group."
1623   (interactive "P")
1624   (let ((no-display (eq all 0))
1625         (group (or group (gnus-group-group-name)))
1626         number active marked entry)
1627     (when (eq all 0)
1628       (setq all nil))
1629     (unless group
1630       (error "No group on current line"))
1631     (setq marked (gnus-info-marks
1632                   (nth 2 (setq entry (gnus-gethash
1633                                       group gnus-newsrc-hashtb)))))
1634     ;; This group might be a dead group.  In that case we have to get
1635     ;; the number of unread articles from `gnus-active-hashtb'.
1636     (setq number
1637           (cond ((numberp all) all)
1638                 (entry (car entry))
1639                 ((setq active (gnus-active group))
1640                  (- (1+ (cdr active)) (car active)))))
1641     (gnus-summary-read-group
1642      group (or all (and (numberp number)
1643                         (zerop (+ number (gnus-range-length 
1644                                           (cdr (assq 'tick marked)))
1645                                   (gnus-range-length
1646                                    (cdr (assq 'dormant marked)))))))
1647      no-article nil no-display)))
1648
1649 (defun gnus-group-select-group (&optional all)
1650   "Select this newsgroup.
1651 No article is selected automatically.
1652 If ALL is non-nil, already read articles become readable.
1653 If ALL is a number, fetch this number of articles."
1654   (interactive "P")
1655   (gnus-group-read-group all t))
1656
1657 (defun gnus-group-quick-select-group (&optional all)
1658   "Select the current group \"quickly\".
1659 This means that no highlighting or scoring will be performed.
1660 If ALL (the prefix argument) is 0, don't even generate the summary
1661 buffer."
1662   (interactive "P")
1663   (require 'gnus-score)
1664   (let (gnus-visual
1665         gnus-score-find-score-files-function
1666         gnus-apply-kill-hook
1667         gnus-summary-expunge-below)
1668     (gnus-group-read-group all t)))
1669
1670 (defun gnus-group-visible-select-group (&optional all)
1671   "Select the current group without hiding any articles."
1672   (interactive "P")
1673   (let ((gnus-inhibit-limiting t))
1674     (gnus-group-read-group all t)))
1675
1676 ;;;###autoload
1677 (defun gnus-fetch-group (group)
1678   "Start Gnus if necessary and enter GROUP.
1679 Returns whether the fetching was successful or not."
1680   (interactive "sGroup name: ")
1681   (unless (get-buffer gnus-group-buffer)
1682     (gnus))
1683   (gnus-group-read-group nil nil group))
1684
1685 ;; Enter a group that is not in the group buffer.  Non-nil is returned
1686 ;; if selection was successful.
1687 (defun gnus-group-read-ephemeral-group (group method &optional activate 
1688                                               quit-config request-only)
1689   "Read GROUP from METHOD as an ephemeral group.
1690 If ACTIVATE, request the group first.
1691 If QUIT-CONFIG, use that window configuration when exiting from the
1692 ephemeral group.
1693 If REQUEST-ONLY, don't actually read the group; just request it.
1694
1695 Return the name of the group is selection was successful."
1696   (let ((group (if (gnus-group-foreign-p group) group
1697                  (gnus-group-prefixed-name group method))))
1698     (gnus-sethash
1699      group
1700      `(-1 nil (,group 
1701                ,gnus-level-default-subscribed nil nil ,method
1702                ((quit-config .
1703                              ,(if quit-config quit-config
1704                                 (cons gnus-summary-buffer
1705                                       gnus-current-window-configuration))))))
1706      gnus-newsrc-hashtb)
1707     (set-buffer gnus-group-buffer)
1708     (unless (gnus-check-server method)
1709       (error "Unable to contact server: %s" (gnus-status-message method)))
1710     (when activate
1711       (gnus-activate-group group 'scan)
1712       (unless (gnus-request-group group)
1713         (error "Couldn't request group: %s" 
1714                (nnheader-get-report (car method)))))
1715     (if request-only
1716         group
1717       (condition-case ()
1718           (when (gnus-group-read-group t t group)
1719             group)
1720         ;;(error nil)
1721         (quit nil)))))
1722
1723 (defun gnus-group-jump-to-group (group)
1724   "Jump to newsgroup GROUP."
1725   (interactive
1726    (list (completing-read
1727           "Group: " gnus-active-hashtb nil
1728           (gnus-read-active-file-p)
1729           nil
1730           'gnus-group-history)))
1731
1732   (when (equal group "")
1733     (error "Empty group name"))
1734
1735   (unless (gnus-ephemeral-group-p group)
1736     ;; Either go to the line in the group buffer...
1737     (unless (gnus-group-goto-group group)
1738       ;; ... or insert the line.
1739       (gnus-group-update-group group)
1740       (gnus-group-goto-group group)))
1741   ;; Adjust cursor point.
1742   (gnus-group-position-point))
1743
1744 (defun gnus-group-goto-group (group &optional far)
1745   "Goto to newsgroup GROUP.
1746 If FAR, it is likely that the group is not on the current line."
1747   (when group
1748     (if far
1749         (gnus-goto-char
1750          (text-property-any 
1751           (point-min) (point-max)
1752           'gnus-group (gnus-intern-safe group gnus-active-hashtb)))
1753       (beginning-of-line)
1754       (cond
1755        ;; It's quite likely that we are on the right line, so
1756        ;; we check the current line first.
1757        ((eq (get-text-property (point) 'gnus-group)
1758             (gnus-intern-safe group gnus-active-hashtb))
1759         (point))
1760        ;; Previous and next line are also likely, so we check them as well.
1761        ((save-excursion
1762           (forward-line -1)
1763           (eq (get-text-property (point) 'gnus-group)
1764               (gnus-intern-safe group gnus-active-hashtb)))
1765         (forward-line -1)
1766         (point))
1767        ((save-excursion
1768           (forward-line 1)
1769           (eq (get-text-property (point) 'gnus-group)
1770               (gnus-intern-safe group gnus-active-hashtb)))
1771         (forward-line 1)
1772         (point))
1773        (t
1774         ;; Search through the entire buffer.
1775         (gnus-goto-char
1776          (text-property-any 
1777           (point-min) (point-max)
1778           'gnus-group (gnus-intern-safe group gnus-active-hashtb))))))))
1779
1780 (defun gnus-group-next-group (n &optional silent)
1781   "Go to next N'th newsgroup.
1782 If N is negative, search backward instead.
1783 Returns the difference between N and the number of skips actually
1784 done."
1785   (interactive "p")
1786   (gnus-group-next-unread-group n t nil silent))
1787
1788 (defun gnus-group-next-unread-group (n &optional all level silent)
1789   "Go to next N'th unread newsgroup.
1790 If N is negative, search backward instead.
1791 If ALL is non-nil, choose any newsgroup, unread or not.
1792 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
1793 such group can be found, the next group with a level higher than
1794 LEVEL.
1795 Returns the difference between N and the number of skips actually
1796 made."
1797   (interactive "p")
1798   (let ((backward (< n 0))
1799         (n (abs n)))
1800     (while (and (> n 0)
1801                 (gnus-group-search-forward
1802                  backward (or (not gnus-group-goto-unread) all) level))
1803       (setq n (1- n)))
1804     (when (and (/= 0 n)
1805                (not silent))
1806       (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
1807                     (if level " on this level or higher" "")))
1808     n))
1809
1810 (defun gnus-group-prev-group (n)
1811   "Go to previous N'th newsgroup.
1812 Returns the difference between N and the number of skips actually
1813 done."
1814   (interactive "p")
1815   (gnus-group-next-unread-group (- n) t))
1816
1817 (defun gnus-group-prev-unread-group (n)
1818   "Go to previous N'th unread newsgroup.
1819 Returns the difference between N and the number of skips actually
1820 done."
1821   (interactive "p")
1822   (gnus-group-next-unread-group (- n)))
1823
1824 (defun gnus-group-next-unread-group-same-level (n)
1825   "Go to next N'th unread newsgroup on the same level.
1826 If N is negative, search backward instead.
1827 Returns the difference between N and the number of skips actually
1828 done."
1829   (interactive "p")
1830   (gnus-group-next-unread-group n t (gnus-group-group-level))
1831   (gnus-group-position-point))
1832
1833 (defun gnus-group-prev-unread-group-same-level (n)
1834   "Go to next N'th unread newsgroup on the same level.
1835 Returns the difference between N and the number of skips actually
1836 done."
1837   (interactive "p")
1838   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
1839   (gnus-group-position-point))
1840
1841 (defun gnus-group-best-unread-group (&optional exclude-group)
1842   "Go to the group with the highest level.
1843 If EXCLUDE-GROUP, do not go to that group."
1844   (interactive)
1845   (goto-char (point-min))
1846   (let ((best 100000)
1847         unread best-point)
1848     (while (not (eobp))
1849       (setq unread (get-text-property (point) 'gnus-unread))
1850       (when (and (numberp unread) (> unread 0))
1851         (when (and (get-text-property (point) 'gnus-level)
1852                    (< (get-text-property (point) 'gnus-level) best)
1853                    (or (not exclude-group)
1854                        (not (equal exclude-group (gnus-group-group-name)))))
1855           (setq best (get-text-property (point) 'gnus-level))
1856           (setq best-point (point))))
1857       (forward-line 1))
1858     (when best-point
1859       (goto-char best-point))
1860     (gnus-summary-position-point)
1861     (and best-point (gnus-group-group-name))))
1862
1863 (defun gnus-group-first-unread-group ()
1864   "Go to the first group with unread articles."
1865   (interactive)
1866   (prog1
1867       (let ((opoint (point))
1868             unread)
1869         (goto-char (point-min))
1870         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
1871                 (and (numberp unread)   ; Not a topic.
1872                      (not (zerop unread))) ; Has unread articles.
1873                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
1874             (point)                     ; Success.
1875           (goto-char opoint)
1876           nil))                         ; Not success.
1877     (gnus-group-position-point)))
1878
1879 (defun gnus-group-enter-server-mode ()
1880   "Jump to the server buffer."
1881   (interactive)
1882   (gnus-enter-server-buffer))
1883
1884 (defun gnus-group-make-group (name &optional method address args)
1885   "Add a new newsgroup.
1886 The user will be prompted for a NAME, for a select METHOD, and an
1887 ADDRESS."
1888   (interactive
1889    (list
1890     (gnus-read-group "Group name: ")
1891     (gnus-read-method "From method: ")))
1892
1893   (let* ((meth (when (and method
1894                           (not (gnus-server-equal method gnus-select-method)))
1895                  (if address (list (intern method) address)
1896                    method)))
1897          (nname (if method (gnus-group-prefixed-name name meth) name))
1898          backend info)
1899     (when (gnus-gethash nname gnus-newsrc-hashtb)
1900       (error "Group %s already exists" nname))
1901     ;; Subscribe to the new group.
1902     (gnus-group-change-level
1903      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
1904      gnus-level-default-subscribed gnus-level-killed
1905      (and (gnus-group-group-name)
1906           (gnus-gethash (gnus-group-group-name)
1907                         gnus-newsrc-hashtb))
1908      t)
1909     ;; Make it active.
1910     (gnus-set-active nname (cons 1 0))
1911     (unless (gnus-ephemeral-group-p name)
1912       (gnus-dribble-enter
1913        (concat "(gnus-group-set-info '" 
1914                (gnus-prin1-to-string (cdr info)) ")")))
1915     ;; Insert the line.
1916     (gnus-group-insert-group-line-info nname)
1917     (forward-line -1)
1918     (gnus-group-position-point)
1919
1920     ;; Load the backend and try to make the backend create
1921     ;; the group as well.
1922     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
1923                                                   nil meth))))
1924                  gnus-valid-select-methods)
1925       (require backend))
1926     (gnus-check-server meth)
1927     (when (gnus-check-backend-function 'request-create-group nname)
1928       (gnus-request-create-group nname nil args))
1929     t))
1930
1931 (defun gnus-group-delete-group (group &optional force)
1932   "Delete the current group.  Only meaningful with mail groups.
1933 If FORCE (the prefix) is non-nil, all the articles in the group will
1934 be deleted.  This is \"deleted\" as in \"removed forever from the face
1935 of the Earth\".  There is no undo.  The user will be prompted before
1936 doing the deletion."
1937   (interactive
1938    (list (gnus-group-group-name)
1939          current-prefix-arg))
1940   (unless group
1941     (error "No group to rename"))
1942   (unless (gnus-check-backend-function 'request-delete-group group)
1943     (error "This backend does not support group deletion"))
1944   (prog1
1945       (if (not (gnus-yes-or-no-p
1946                 (format
1947                  "Do you really want to delete %s%s? "
1948                  group (if force " and all its contents" ""))))
1949           ()                            ; Whew!
1950         (gnus-message 6 "Deleting group %s..." group)
1951         (if (not (gnus-request-delete-group group force))
1952             (gnus-error 3 "Couldn't delete group %s" group)
1953           (gnus-message 6 "Deleting group %s...done" group)
1954           (gnus-group-goto-group group)
1955           (gnus-group-kill-group 1 t)
1956           (gnus-sethash group nil gnus-active-hashtb)
1957           t))
1958     (gnus-group-position-point)))
1959
1960 (defun gnus-group-rename-group (group new-name)
1961   "Rename group from GROUP to NEW-NAME.
1962 When used interactively, GROUP is the group under point
1963 and NEW-NAME will be prompted for."
1964   (interactive
1965    (list
1966     (gnus-group-group-name)
1967     (progn
1968       (unless (gnus-check-backend-function
1969                'request-rename-group (gnus-group-group-name))
1970         (error "This backend does not support renaming groups"))
1971       (read-string "New group name: " (gnus-group-group-name)))))
1972
1973   (unless (gnus-check-backend-function 'request-rename-group group)
1974     (error "This backend does not support renaming groups"))
1975   (unless group 
1976     (error "No group to rename"))
1977   (when (string-match "^[ \t]*$" new-name)
1978     (error "Not a valid group name"))
1979   (when (equal group new-name)
1980     (error "Can't rename to the same name"))
1981
1982   ;; We find the proper prefixed name.
1983   (setq new-name
1984         (if (equal (gnus-group-real-name new-name) new-name)
1985             ;; Native group.
1986             new-name
1987           ;; Foreign group.
1988           (gnus-group-prefixed-name
1989            (gnus-group-real-name new-name)
1990            (gnus-info-method (gnus-get-info group)))))
1991
1992   (gnus-message 6 "Renaming group %s to %s..." group new-name)
1993   (prog1
1994       (if (not (gnus-request-rename-group group new-name))
1995           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
1996         ;; We rename the group internally by killing it...
1997         (gnus-group-goto-group group)
1998         (gnus-group-kill-group)
1999         ;; ... changing its name ...
2000         (setcar (cdar gnus-list-of-killed-groups) new-name)
2001         ;; ... and then yanking it.  Magic!
2002         (gnus-group-yank-group)
2003         (gnus-set-active new-name (gnus-active group))
2004         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
2005         new-name)
2006     (gnus-group-position-point)))
2007
2008 (defun gnus-group-edit-group (group &optional part)
2009   "Edit the group on the current line."
2010   (interactive (list (gnus-group-group-name)))
2011   (let ((part (or part 'info))
2012         info)
2013     (unless group
2014       (error "No group on current line"))
2015     (unless (setq info (gnus-get-info group))
2016       (error "Killed group; can't be edited"))
2017     (gnus-edit-form
2018      ;; Find the proper form to edit.
2019      (cond ((eq part 'method)
2020             (or (gnus-info-method info) "native"))
2021            ((eq part 'params)
2022             (gnus-info-params info))
2023            (t info))
2024      ;; The proper documentation.
2025      (format
2026       "Editing the %s."
2027       (cond
2028        ((eq part 'method) "select method")
2029        ((eq part 'params) "group parameters")
2030        (t "group info")))
2031      `(lambda (form)
2032         (gnus-group-edit-group-done ',part ,group form)))))
2033
2034 (defun gnus-group-edit-group-method (group)
2035   "Edit the select method of GROUP."
2036   (interactive (list (gnus-group-group-name)))
2037   (gnus-group-edit-group group 'method))
2038
2039 (defun gnus-group-edit-group-parameters (group)
2040   "Edit the group parameters of GROUP."
2041   (interactive (list (gnus-group-group-name)))
2042   (gnus-group-edit-group group 'params))
2043
2044 (defun gnus-group-edit-group-done (part group form)
2045   "Update variables."
2046   (let* ((method (cond ((eq part 'info) (nth 4 form))
2047                        ((eq part 'method) form)
2048                        (t nil)))
2049          (info (cond ((eq part 'info) form)
2050                      ((eq part 'method) (gnus-get-info group))
2051                      (t nil)))
2052          (new-group (if info
2053                         (if (or (not method)
2054                                 (gnus-server-equal
2055                                  gnus-select-method method))
2056                             (gnus-group-real-name (car info))
2057                           (gnus-group-prefixed-name
2058                            (gnus-group-real-name (car info)) method))
2059                       nil)))
2060     (when (and new-group
2061                (not (equal new-group group)))
2062       (when (gnus-group-goto-group group)
2063         (gnus-group-kill-group 1))
2064       (gnus-activate-group new-group))
2065     ;; Set the info.
2066     (if (not (and info new-group))
2067         (gnus-group-set-info form (or new-group group) part)
2068       (setq info (gnus-copy-sequence info))
2069       (setcar info new-group)
2070       (unless (gnus-server-equal method "native")
2071         (unless (nthcdr 3 info)
2072           (nconc info (list nil nil)))
2073         (unless (nthcdr 4 info)
2074           (nconc info (list nil)))
2075         (gnus-info-set-method info method))
2076       (gnus-group-set-info info))
2077     (gnus-group-update-group (or new-group group))
2078     (gnus-group-position-point)))
2079
2080 (defun gnus-group-make-useful-group (group method)
2081   (interactive
2082    (let ((entry (assoc (completing-read "Create group: " gnus-useful-groups
2083                                         nil t)
2084                        gnus-useful-groups)))
2085      (list (cadr entry) (caddr entry))))
2086   (setq method (gnus-copy-sequence method))
2087   (let (entry)
2088     (while (setq entry (memq (assq 'eval method) method))
2089       (setcar entry (eval (cadar entry)))))
2090   (gnus-group-make-group group method))
2091
2092 (defun gnus-group-make-help-group ()
2093   "Create the Gnus documentation group."
2094   (interactive)
2095   (let ((name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
2096         (file (nnheader-find-etc-directory "gnus-tut.txt" t))
2097         dir)
2098     (when (gnus-gethash name gnus-newsrc-hashtb)
2099       (error "Documentation group already exists"))
2100     (if (not file)
2101         (gnus-message 1 "Couldn't find doc group")
2102       (gnus-group-make-group
2103        (gnus-group-real-name name)
2104        (list 'nndoc "gnus-help"
2105              (list 'nndoc-address file)
2106              (list 'nndoc-article-type 'mbox)))))
2107   (gnus-group-position-point))
2108
2109 (defun gnus-group-make-doc-group (file type)
2110   "Create a group that uses a single file as the source."
2111   (interactive
2112    (list (read-file-name "File name: ")
2113          (and current-prefix-arg 'ask)))
2114   (when (eq type 'ask)
2115     (let ((err "")
2116           char found)
2117       (while (not found)
2118         (message
2119          "%sFile type (mbox, babyl, digest, forward, mmdf, guess) [mbdfag]: "
2120          err)
2121         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
2122                           ((= char ?b) 'babyl)
2123                           ((= char ?d) 'digest)
2124                           ((= char ?f) 'forward)
2125                           ((= char ?a) 'mmfd)
2126                           (t (setq err (format "%c unknown. " char))
2127                              nil))))
2128       (setq type found)))
2129   (let* ((file (expand-file-name file))
2130          (name (gnus-generate-new-group-name
2131                 (gnus-group-prefixed-name
2132                  (file-name-nondirectory file) '(nndoc "")))))
2133     (gnus-group-make-group
2134      (gnus-group-real-name name)
2135      (list 'nndoc file
2136            (list 'nndoc-address file)
2137            (list 'nndoc-article-type (or type 'guess))))))
2138
2139 (defvar nnweb-type-definition)
2140 (defvar gnus-group-web-type-history nil)
2141 (defvar gnus-group-web-search-history nil)
2142 (defun gnus-group-make-web-group (&optional solid)
2143   "Create an ephemeral nnweb group.
2144 If SOLID (the prefix), create a solid group."
2145   (interactive "P")
2146   (require 'nnweb)
2147   (let* ((group
2148           (if solid (gnus-read-group "Group name: ")
2149             (message-unique-id)))
2150          (type
2151           (completing-read
2152            "Search engine type: "
2153            (mapcar (lambda (elem) (list (symbol-name (car elem))))
2154                    nnweb-type-definition)
2155            nil t (cons (or (car gnus-group-web-type-history)
2156                            (symbol-name (caar nnweb-type-definition)))
2157                        0)
2158            'gnus-group-web-type-history))
2159          (search
2160           (read-string 
2161            "Search string: " 
2162            (cons (or (car gnus-group-web-search-history) "") 0)
2163            'gnus-group-web-search-history))
2164          (method
2165           `(nnweb ,group (nnweb-search ,search)
2166                   (nnweb-type ,(intern type))
2167                   (nnweb-ephemeral-p t))))
2168     (if solid
2169         (gnus-group-make-group group "nnweb" "" `(,(intern type) ,search))
2170       (gnus-group-read-ephemeral-group
2171        group method t
2172        (cons (current-buffer)
2173              (if (eq major-mode 'gnus-summary-mode) 'summary 'group))))))
2174
2175 (defun gnus-group-make-archive-group (&optional all)
2176   "Create the (ding) Gnus archive group of the most recent articles.
2177 Given a prefix, create a full group."
2178   (interactive "P")
2179   (let ((group (gnus-group-prefixed-name
2180                 (if all "ding.archives" "ding.recent") '(nndir ""))))
2181     (when (gnus-gethash group gnus-newsrc-hashtb)
2182       (error "Archive group already exists"))
2183     (gnus-group-make-group
2184      (gnus-group-real-name group)
2185      (list 'nndir (if all "hpc" "edu")
2186            (list 'nndir-directory
2187                  (if all gnus-group-archive-directory
2188                    gnus-group-recent-archive-directory))))
2189     (gnus-group-add-parameter group (cons 'to-address "ding@ifi.uio.no"))))
2190
2191 (defun gnus-group-make-directory-group (dir)
2192   "Create an nndir group.
2193 The user will be prompted for a directory.  The contents of this
2194 directory will be used as a newsgroup.  The directory should contain
2195 mail messages or news articles in files that have numeric names."
2196   (interactive
2197    (list (read-file-name "Create group from directory: ")))
2198   (unless (file-exists-p dir)
2199     (error "No such directory"))
2200   (unless (file-directory-p dir)
2201     (error "Not a directory"))
2202   (let ((ext "")
2203         (i 0)
2204         group)
2205     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
2206       (setq group
2207             (gnus-group-prefixed-name
2208              (concat (file-name-as-directory (directory-file-name dir))
2209                      ext)
2210              '(nndir "")))
2211       (setq ext (format "<%d>" (setq i (1+ i)))))
2212     (gnus-group-make-group
2213      (gnus-group-real-name group)
2214      (list 'nndir (gnus-group-real-name group) (list 'nndir-directory dir)))))
2215
2216 (defun gnus-group-make-kiboze-group (group address scores)
2217   "Create an nnkiboze group.
2218 The user will be prompted for a name, a regexp to match groups, and
2219 score file entries for articles to include in the group."
2220   (interactive
2221    (list
2222     (read-string "nnkiboze group name: ")
2223     (read-string "Source groups (regexp): ")
2224     (let ((headers (mapcar (lambda (group) (list group))
2225                            '("subject" "from" "number" "date" "message-id"
2226                              "references" "chars" "lines" "xref"
2227                              "followup" "all" "body" "head")))
2228           scores header regexp regexps)
2229       (while (not (equal "" (setq header (completing-read
2230                                           "Match on header: " headers nil t))))
2231         (setq regexps nil)
2232         (while (not (equal "" (setq regexp (read-string
2233                                             (format "Match on %s (string): "
2234                                                     header)))))
2235           (push (list regexp nil nil 'r) regexps))
2236         (push (cons header regexps) scores))
2237       scores)))
2238   (gnus-group-make-group group "nnkiboze" address)
2239   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
2240     (let (emacs-lisp-mode-hook)
2241       (pp scores (current-buffer)))))
2242
2243 (defun gnus-group-add-to-virtual (n vgroup)
2244   "Add the current group to a virtual group."
2245   (interactive
2246    (list current-prefix-arg
2247          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
2248                           "nnvirtual:")))
2249   (unless (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
2250     (error "%s is not an nnvirtual group" vgroup))
2251   (let* ((groups (gnus-group-process-prefix n))
2252          (method (gnus-info-method (gnus-get-info vgroup))))
2253     (setcar (cdr method)
2254             (concat
2255              (nth 1 method) "\\|"
2256              (mapconcat
2257               (lambda (s)
2258                 (gnus-group-remove-mark s)
2259                 (concat "\\(^" (regexp-quote s) "$\\)"))
2260               groups "\\|"))))
2261   (gnus-group-position-point))
2262
2263 (defun gnus-group-make-empty-virtual (group)
2264   "Create a new, fresh, empty virtual group."
2265   (interactive "sCreate new, empty virtual group: ")
2266   (let* ((method (list 'nnvirtual "^$"))
2267          (pgroup (gnus-group-prefixed-name group method)))
2268     ;; Check whether it exists already.
2269     (when (gnus-gethash pgroup gnus-newsrc-hashtb)
2270       (error "Group %s already exists." pgroup))
2271     ;; Subscribe the new group after the group on the current line.
2272     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
2273     (gnus-group-update-group pgroup)
2274     (forward-line -1)
2275     (gnus-group-position-point)))
2276
2277 (defun gnus-group-enter-directory (dir)
2278   "Enter an ephemeral nneething group."
2279   (interactive "DDirectory to read: ")
2280   (let* ((method (list 'nneething dir '(nneething-read-only t)))
2281          (leaf (gnus-group-prefixed-name
2282                 (file-name-nondirectory (directory-file-name dir))
2283                 method))
2284          (name (gnus-generate-new-group-name leaf)))
2285     (unless (gnus-group-read-ephemeral-group
2286              name method t
2287              (cons (current-buffer)
2288                    (if (eq major-mode 'gnus-summary-mode)
2289                        'summary 'group)))
2290       (error "Couldn't enter %s" dir))))
2291
2292 ;; Group sorting commands
2293 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
2294
2295 (defun gnus-group-sort-groups (func &optional reverse)
2296   "Sort the group buffer according to FUNC.
2297 If REVERSE, reverse the sorting order."
2298   (interactive (list gnus-group-sort-function current-prefix-arg))
2299   (funcall gnus-group-sort-alist-function
2300            (gnus-make-sort-function func) reverse)
2301   (gnus-group-list-groups))
2302
2303 (defun gnus-group-sort-flat (func reverse)
2304   ;; We peel off the dummy group from the alist.
2305   (when func
2306     (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
2307       (pop gnus-newsrc-alist))
2308     ;; Do the sorting.
2309     (setq gnus-newsrc-alist
2310           (sort gnus-newsrc-alist func))
2311     (when reverse
2312       (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
2313     ;; Regenerate the hash table.
2314     (gnus-make-hashtable-from-newsrc-alist)))
2315
2316 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
2317   "Sort the group buffer alphabetically by group name.
2318 If REVERSE, sort in reverse order."
2319   (interactive "P")
2320   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
2321
2322 (defun gnus-group-sort-groups-by-unread (&optional reverse)
2323   "Sort the group buffer by number of unread articles.
2324 If REVERSE, sort in reverse order."
2325   (interactive "P")
2326   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
2327
2328 (defun gnus-group-sort-groups-by-level (&optional reverse)
2329   "Sort the group buffer by group level.
2330 If REVERSE, sort in reverse order."
2331   (interactive "P")
2332   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
2333
2334 (defun gnus-group-sort-groups-by-score (&optional reverse)
2335   "Sort the group buffer by group score.
2336 If REVERSE, sort in reverse order."
2337   (interactive "P")
2338   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
2339
2340 (defun gnus-group-sort-groups-by-rank (&optional reverse)
2341   "Sort the group buffer by group rank.
2342 If REVERSE, sort in reverse order."
2343   (interactive "P")
2344   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
2345
2346 (defun gnus-group-sort-groups-by-method (&optional reverse)
2347   "Sort the group buffer alphabetically by backend name.
2348 If REVERSE, sort in reverse order."
2349   (interactive "P")
2350   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
2351
2352 ;;; Selected group sorting.
2353
2354 (defun gnus-group-sort-selected-groups (n func &optional reverse)
2355   "Sort the process/prefixed groups."
2356   (interactive (list current-prefix-arg gnus-group-sort-function))
2357   (let ((groups (gnus-group-process-prefix n)))
2358     (funcall gnus-group-sort-selected-function
2359              groups (gnus-make-sort-function func) reverse)
2360     (gnus-group-list-groups)))
2361
2362 (defun gnus-group-sort-selected-flat (groups func reverse)
2363   (let (entries infos)
2364     ;; First find all the group entries for these groups.
2365     (while groups
2366       (push (nthcdr 2 (gnus-gethash (pop groups) gnus-newsrc-hashtb))
2367             entries))
2368     ;; Then sort the infos.
2369     (setq infos
2370           (sort
2371            (mapcar
2372             (lambda (entry) (car entry))
2373             (setq entries (nreverse entries)))
2374            func))
2375     (when reverse
2376       (setq infos (nreverse infos)))
2377     ;; Go through all the infos and replace the old entries
2378     ;; with the new infos.
2379     (while infos
2380       (setcar entries (pop infos))
2381       (pop entries))
2382     ;; Update the hashtable.
2383     (gnus-make-hashtable-from-newsrc-alist)))
2384
2385 (defun gnus-group-sort-selected-groups-by-alphabet (&optional reverse)
2386   "Sort the group buffer alphabetically by group name.
2387 If REVERSE, sort in reverse order."
2388   (interactive "P")
2389   (gnus-group-sort-selected-groups 'gnus-group-sort-by-alphabet reverse))
2390
2391 (defun gnus-group-sort-selected-groups-by-unread (&optional reverse)
2392   "Sort the group buffer by number of unread articles.
2393 If REVERSE, sort in reverse order."
2394   (interactive "P")
2395   (gnus-group-sort-selected-groups 'gnus-group-sort-by-unread reverse))
2396
2397 (defun gnus-group-sort-selected-groups-by-level (&optional reverse)
2398   "Sort the group buffer by group level.
2399 If REVERSE, sort in reverse order."
2400   (interactive "P")
2401   (gnus-group-sort-selected-groups 'gnus-group-sort-by-level reverse))
2402
2403 (defun gnus-group-sort-selected-groups-by-score (&optional reverse)
2404   "Sort the group buffer by group score.
2405 If REVERSE, sort in reverse order."
2406   (interactive "P")
2407   (gnus-group-sort-selected-groups 'gnus-group-sort-by-score reverse))
2408
2409 (defun gnus-group-sort-selected-groups-by-rank (&optional reverse)
2410   "Sort the group buffer by group rank.
2411 If REVERSE, sort in reverse order."
2412   (interactive "P")
2413   (gnus-group-sort-selected-groups 'gnus-group-sort-by-rank reverse))
2414
2415 (defun gnus-group-sort-selected-groups-by-method (&optional reverse)
2416   "Sort the group buffer alphabetically by backend name.
2417 If REVERSE, sort in reverse order."
2418   (interactive "P")
2419   (gnus-group-sort-selected-groups 'gnus-group-sort-by-method reverse))
2420
2421 ;;; Sorting predicates.
2422
2423 (defun gnus-group-sort-by-alphabet (info1 info2)
2424   "Sort alphabetically."
2425   (string< (gnus-info-group info1) (gnus-info-group info2)))
2426
2427 (defun gnus-group-sort-by-real-name (info1 info2)
2428   "Sort alphabetically on real (unprefixed) names."
2429   (string< (gnus-group-real-name (gnus-info-group info1))
2430            (gnus-group-real-name (gnus-info-group info2))))
2431
2432 (defun gnus-group-sort-by-unread (info1 info2)
2433   "Sort by number of unread articles."
2434   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
2435         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
2436     (< (or (and (numberp n1) n1) 0)
2437        (or (and (numberp n2) n2) 0))))
2438
2439 (defun gnus-group-sort-by-level (info1 info2)
2440   "Sort by level."
2441   (< (gnus-info-level info1) (gnus-info-level info2)))
2442
2443 (defun gnus-group-sort-by-method (info1 info2)
2444   "Sort alphabetically by backend name."
2445   (string< (symbol-name (car (gnus-find-method-for-group
2446                               (gnus-info-group info1) info1)))
2447            (symbol-name (car (gnus-find-method-for-group
2448                               (gnus-info-group info2) info2)))))
2449
2450 (defun gnus-group-sort-by-score (info1 info2)
2451   "Sort by group score."
2452   (< (gnus-info-score info1) (gnus-info-score info2)))
2453
2454 (defun gnus-group-sort-by-rank (info1 info2)
2455   "Sort by level and score."
2456   (let ((level1 (gnus-info-level info1))
2457         (level2 (gnus-info-level info2)))
2458     (or (< level1 level2)
2459         (and (= level1 level2)
2460              (> (gnus-info-score info1) (gnus-info-score info2))))))
2461
2462 ;;; Clearing data
2463
2464 (defun gnus-group-clear-data (&optional arg)
2465   "Clear all marks and read ranges from the current group."
2466   (interactive "P")
2467   (gnus-group-iterate arg
2468     (lambda (group)
2469       (let (info)
2470         (gnus-info-clear-data (setq info (gnus-get-info group)))
2471         (gnus-get-unread-articles-in-group info (gnus-active group) t)
2472         (when (gnus-group-goto-group group)
2473           (gnus-group-update-group-line))))))
2474
2475 (defun gnus-group-clear-data-on-native-groups ()
2476   "Clear all marks and read ranges from all native groups."
2477   (interactive)
2478   (when (gnus-yes-or-no-p "Really clear all data from almost all groups? ")
2479     (let ((alist (cdr gnus-newsrc-alist))
2480           info)
2481       (while (setq info (pop alist))
2482         (when (gnus-group-native-p (gnus-info-group info))
2483           (gnus-info-clear-data info)))
2484       (gnus-get-unread-articles)
2485       (gnus-dribble-enter "")
2486       (when (gnus-y-or-n-p 
2487              "Move the cache away to avoid problems in the future? ")
2488         (call-interactively 'gnus-cache-move-cache)))))
2489
2490 (defun gnus-info-clear-data (info)
2491   "Clear all marks and read ranges from INFO."
2492   (let ((group (gnus-info-group info)))
2493     (gnus-undo-register
2494       `(progn
2495          (gnus-info-set-marks ',info ',(gnus-info-marks info))
2496          (gnus-info-set-read ',info ',(gnus-info-read info))
2497          (when (gnus-group-goto-group ,group)
2498            (gnus-group-update-group-line))))
2499     (gnus-info-set-read info nil)
2500     (when (gnus-info-marks info)
2501       (gnus-info-set-marks info nil))))
2502
2503 ;; Group catching up.
2504
2505 (defun gnus-group-catchup-current (&optional n all)
2506   "Mark all articles not marked as unread in current newsgroup as read.
2507 If prefix argument N is numeric, the ARG next newsgroups will be
2508 caught up.  If ALL is non-nil, marked articles will also be marked as
2509 read.  Cross references (Xref: header) of articles are ignored.
2510 The difference between N and actual number of newsgroups that were
2511 caught up is returned."
2512   (interactive "P")
2513   (unless (gnus-group-group-name)
2514     (error "No group on the current line"))
2515   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
2516                gnus-expert-user
2517                (gnus-y-or-n-p
2518                 (if all
2519                     "Do you really want to mark all articles as read? "
2520                   "Mark all unread articles as read? "))))
2521       n
2522     (let ((groups (gnus-group-process-prefix n))
2523           (ret 0))
2524       (while groups
2525         ;; Virtual groups have to be given special treatment.
2526         (let ((method (gnus-find-method-for-group (car groups))))
2527           (when (eq 'nnvirtual (car method))
2528             (nnvirtual-catchup-group
2529              (gnus-group-real-name (car groups)) (nth 1 method) all)))
2530         (gnus-group-remove-mark (car groups))
2531         (if (>= (gnus-group-group-level) gnus-level-zombie)
2532             (gnus-message 2 "Dead groups can't be caught up")
2533           (if (prog1
2534                   (gnus-group-goto-group (car groups))
2535                 (gnus-group-catchup (car groups) all))
2536               (gnus-group-update-group-line)
2537             (setq ret (1+ ret))))
2538         (setq groups (cdr groups)))
2539       (gnus-group-next-unread-group 1)
2540       ret)))
2541
2542 (defun gnus-group-catchup-current-all (&optional n)
2543   "Mark all articles in current newsgroup as read.
2544 Cross references (Xref: header) of articles are ignored."
2545   (interactive "P")
2546   (gnus-group-catchup-current n 'all))
2547
2548 (defun gnus-group-catchup (group &optional all)
2549   "Mark all articles in GROUP as read.
2550 If ALL is non-nil, all articles are marked as read.
2551 The return value is the number of articles that were marked as read,
2552 or nil if no action could be taken."
2553   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
2554          (num (car entry)))
2555     ;; Do the updating only if the newsgroup isn't killed.
2556     (if (not (numberp (car entry)))
2557         (gnus-message 1 "Can't catch up %s; non-active group" group)
2558       ;; Do auto-expirable marks if that's required.
2559       (when (gnus-group-auto-expirable-p group)
2560         (gnus-add-marked-articles
2561          group 'expire (gnus-list-of-unread-articles group))
2562         (when all
2563           (let ((marks (nth 3 (nth 2 entry))))
2564             (gnus-add-marked-articles
2565              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
2566             (gnus-add-marked-articles
2567              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
2568       (when entry
2569         (gnus-update-read-articles group nil)
2570         ;; Also nix out the lists of marks and dormants.
2571         (when all
2572           (gnus-add-marked-articles group 'tick nil nil 'force)
2573           (gnus-add-marked-articles group 'dormant nil nil 'force))
2574         (let ((gnus-newsgroup-name group))
2575           (run-hooks 'gnus-group-catchup-group-hook))
2576         num))))
2577
2578 (defun gnus-group-expire-articles (&optional n)
2579   "Expire all expirable articles in the current newsgroup."
2580   (interactive "P")
2581   (let ((groups (gnus-group-process-prefix n))
2582         group)
2583     (unless groups
2584       (error "No groups to expire"))
2585     (while (setq group (pop groups))
2586       (gnus-group-remove-mark group)
2587       (when (gnus-check-backend-function 'request-expire-articles group)
2588         (gnus-message 6 "Expiring articles in %s..." group)
2589         (let* ((info (gnus-get-info group))
2590                (expirable (if (gnus-group-total-expirable-p group)
2591                               (cons nil (gnus-list-of-read-articles group))
2592                             (assq 'expire (gnus-info-marks info))))
2593                (expiry-wait (gnus-group-find-parameter group 'expiry-wait)))
2594           (when expirable
2595             (setcdr
2596              expirable
2597              (gnus-compress-sequence
2598               (if expiry-wait
2599                   ;; We set the expiry variables to the group
2600                   ;; parameter. 
2601                   (let ((nnmail-expiry-wait-function nil)
2602                         (nnmail-expiry-wait expiry-wait))
2603                     (gnus-request-expire-articles
2604                      (gnus-uncompress-sequence (cdr expirable)) group))
2605                 ;; Just expire using the normal expiry values.
2606                 (gnus-request-expire-articles
2607                  (gnus-uncompress-sequence (cdr expirable)) group))))
2608             (gnus-close-group group))
2609           (gnus-message 6 "Expiring articles in %s...done" group)))
2610       (gnus-group-position-point))))
2611
2612 (defun gnus-group-expire-all-groups ()
2613   "Expire all expirable articles in all newsgroups."
2614   (interactive)
2615   (save-excursion
2616     (gnus-message 5 "Expiring...")
2617     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
2618                                      (cdr gnus-newsrc-alist))))
2619       (gnus-group-expire-articles nil)))
2620   (gnus-group-position-point)
2621   (gnus-message 5 "Expiring...done"))
2622
2623 (defun gnus-group-set-current-level (n level)
2624   "Set the level of the next N groups to LEVEL."
2625   (interactive
2626    (list
2627     current-prefix-arg
2628     (string-to-int
2629      (let ((s (read-string
2630                (format "Level (default %s): "
2631                        (or (gnus-group-group-level)
2632                            gnus-level-default-subscribed)))))
2633        (if (string-match "^\\s-*$" s)
2634            (int-to-string (or (gnus-group-group-level)
2635                               gnus-level-default-subscribed))
2636          s)))))
2637   (unless (and (>= level 1) (<= level gnus-level-killed))
2638     (error "Illegal level: %d" level))
2639   (let ((groups (gnus-group-process-prefix n))
2640         group)
2641     (while (setq group (pop groups))
2642       (gnus-group-remove-mark group)
2643       (gnus-message 6 "Changed level of %s from %d to %d"
2644                     group (or (gnus-group-group-level) gnus-level-killed)
2645                     level)
2646       (gnus-group-change-level
2647        group level (or (gnus-group-group-level) gnus-level-killed))
2648       (gnus-group-update-group-line)))
2649   (gnus-group-position-point))
2650
2651 (defun gnus-group-unsubscribe (&optional n)
2652   "Unsubscribe the current group."
2653   (interactive "P")
2654   (gnus-group-unsubscribe-current-group n 'unsubscribe))
2655
2656 (defun gnus-group-subscribe (&optional n)
2657   "Unsubscribe the current group."
2658   (interactive "P")
2659   (gnus-group-unsubscribe-current-group n 'subscribe))
2660
2661 (defun gnus-group-unsubscribe-current-group (&optional n do-sub)
2662   "Toggle subscription of the current group.
2663 If given numerical prefix, toggle the N next groups."
2664   (interactive "P")
2665   (let ((groups (gnus-group-process-prefix n))
2666         group)
2667     (while groups
2668       (setq group (car groups)
2669             groups (cdr groups))
2670       (gnus-group-remove-mark group)
2671       (gnus-group-unsubscribe-group
2672        group 
2673        (cond
2674         ((eq do-sub 'unsubscribe)
2675          gnus-level-default-unsubscribed)
2676         ((eq do-sub 'subscribe)
2677          gnus-level-default-subscribed)
2678         ((<= (gnus-group-group-level) gnus-level-subscribed)
2679          gnus-level-default-unsubscribed)
2680         (t
2681          gnus-level-default-subscribed))
2682        t)
2683       (gnus-group-update-group-line))
2684     (gnus-group-next-group 1)))
2685
2686 (defun gnus-group-unsubscribe-group (group &optional level silent)
2687   "Toggle subscription to GROUP.
2688 Killed newsgroups are subscribed.  If SILENT, don't try to update the
2689 group line."
2690   (interactive
2691    (list (completing-read
2692           "Group: " gnus-active-hashtb nil
2693           (gnus-read-active-file-p)
2694           nil 
2695           'gnus-group-history)))
2696   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
2697     (cond
2698      ((string-match "^[ \t]$" group)
2699       (error "Empty group name"))
2700      (newsrc
2701       ;; Toggle subscription flag.
2702       (gnus-group-change-level
2703        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
2704                                       gnus-level-subscribed)
2705                                   (1+ gnus-level-subscribed)
2706                                 gnus-level-default-subscribed)))
2707       (unless silent
2708         (gnus-group-update-group group)))
2709      ((and (stringp group)
2710            (or (not (gnus-read-active-file-p))
2711                (gnus-active group)))
2712       ;; Add new newsgroup.
2713       (gnus-group-change-level
2714        group
2715        (if level level gnus-level-default-subscribed)
2716        (or (and (member group gnus-zombie-list)
2717                 gnus-level-zombie)
2718            gnus-level-killed)
2719        (when (gnus-group-group-name)
2720          (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
2721       (unless silent
2722         (gnus-group-update-group group)))
2723      (t (error "No such newsgroup: %s" group)))
2724     (gnus-group-position-point)))
2725
2726 (defun gnus-group-transpose-groups (n)
2727   "Move the current newsgroup up N places.
2728 If given a negative prefix, move down instead.  The difference between
2729 N and the number of steps taken is returned."
2730   (interactive "p")
2731   (unless (gnus-group-group-name)
2732     (error "No group on current line"))
2733   (gnus-group-kill-group 1)
2734   (prog1
2735       (forward-line (- n))
2736     (gnus-group-yank-group)
2737     (gnus-group-position-point)))
2738
2739 (defun gnus-group-kill-all-zombies ()
2740   "Kill all zombie newsgroups."
2741   (interactive)
2742   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
2743   (setq gnus-zombie-list nil)
2744   (gnus-group-list-groups))
2745
2746 (defun gnus-group-kill-region (begin end)
2747   "Kill newsgroups in current region (excluding current point).
2748 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
2749   (interactive "r")
2750   (let ((lines
2751          ;; Count lines.
2752          (save-excursion
2753            (count-lines
2754             (progn
2755               (goto-char begin)
2756               (beginning-of-line)
2757               (point))
2758             (progn
2759               (goto-char end)
2760               (beginning-of-line)
2761               (point))))))
2762     (goto-char begin)
2763     (beginning-of-line)                 ;Important when LINES < 1
2764     (gnus-group-kill-group lines)))
2765
2766 (defun gnus-group-kill-group (&optional n discard)
2767   "Kill the next N groups.
2768 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
2769 However, only groups that were alive can be yanked; already killed
2770 groups or zombie groups can't be yanked.
2771 The return value is the name of the group that was killed, or a list
2772 of groups killed."
2773   (interactive "P")
2774   (let ((buffer-read-only nil)
2775         (groups (gnus-group-process-prefix n))
2776         group entry level out)
2777     (if (< (length groups) 10)
2778         ;; This is faster when there are few groups.
2779         (while groups
2780           (push (setq group (pop groups)) out)
2781           (gnus-group-remove-mark group)
2782           (setq level (gnus-group-group-level))
2783           (gnus-delete-line)
2784           (when (and (not discard)
2785                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
2786             (gnus-undo-register
2787               `(progn
2788                  (gnus-group-goto-group ,(gnus-group-group-name))
2789                  (gnus-group-yank-group)))
2790             (push (cons (car entry) (nth 2 entry))
2791                   gnus-list-of-killed-groups))
2792           (gnus-group-change-level
2793            (if entry entry group) gnus-level-killed (if entry nil level)))
2794       ;; If there are lots and lots of groups to be killed, we use
2795       ;; this thing instead.
2796       (let (entry)
2797         (setq groups (nreverse groups))
2798         (while groups
2799           (gnus-group-remove-mark (setq group (pop groups)))
2800           (gnus-delete-line)
2801           (push group gnus-killed-list)
2802           (setq gnus-newsrc-alist
2803                 (delq (assoc group gnus-newsrc-alist)
2804                       gnus-newsrc-alist))
2805           (when gnus-group-change-level-function
2806             (funcall gnus-group-change-level-function group 9 3))
2807           (cond
2808            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
2809             (push (cons (car entry) (nth 2 entry))
2810                   gnus-list-of-killed-groups)
2811             (setcdr (cdr entry) (cdddr entry)))
2812            ((member group gnus-zombie-list)
2813             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
2814         (gnus-make-hashtable-from-newsrc-alist)))
2815
2816     (gnus-group-position-point)
2817     (if (< (length out) 2) (car out) (nreverse out))))
2818
2819 (defun gnus-group-yank-group (&optional arg)
2820   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
2821 inserting it before the current newsgroup.  The numeric ARG specifies
2822 how many newsgroups are to be yanked.  The name of the newsgroup yanked
2823 is returned, or (if several groups are yanked) a list of yanked groups
2824 is returned."
2825   (interactive "p")
2826   (setq arg (or arg 1))
2827   (let (info group prev out)
2828     (while (>= (decf arg) 0)
2829       (when (not (setq info (pop gnus-list-of-killed-groups)))
2830         (error "No more newsgroups to yank"))
2831       (push (setq group (nth 1 info)) out)
2832       ;; Find which newsgroup to insert this one before - search
2833       ;; backward until something suitable is found.  If there are no
2834       ;; other newsgroups in this buffer, just make this newsgroup the
2835       ;; first newsgroup.
2836       (setq prev (gnus-group-group-name))
2837       (gnus-group-change-level
2838        info (gnus-info-level (cdr info)) gnus-level-killed
2839        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
2840        t)
2841       (gnus-group-insert-group-line-info group)
2842       (gnus-undo-register
2843         `(when (gnus-group-goto-group ,group)
2844            (gnus-group-kill-group 1))))
2845     (forward-line -1)
2846     (gnus-group-position-point)
2847     (if (< (length out) 2) (car out) (nreverse out))))
2848
2849 (defun gnus-group-kill-level (level)
2850   "Kill all groups that is on a certain LEVEL."
2851   (interactive "nKill all groups on level: ")
2852   (cond
2853    ((= level gnus-level-zombie)
2854     (setq gnus-killed-list
2855           (nconc gnus-zombie-list gnus-killed-list))
2856     (setq gnus-zombie-list nil))
2857    ((and (< level gnus-level-zombie)
2858          (> level 0)
2859          (or gnus-expert-user
2860              (gnus-yes-or-no-p
2861               (format
2862                "Do you really want to kill all groups on level %d? "
2863                level))))
2864     (let* ((prev gnus-newsrc-alist)
2865            (alist (cdr prev)))
2866       (while alist
2867         (if (= (gnus-info-level (car alist)) level)
2868             (progn
2869               (push (gnus-info-group (car alist)) gnus-killed-list)
2870               (setcdr prev (cdr alist)))
2871           (setq prev alist))
2872         (setq alist (cdr alist)))
2873       (gnus-make-hashtable-from-newsrc-alist)
2874       (gnus-group-list-groups)))
2875    (t
2876     (error "Can't kill; illegal level: %d" level))))
2877
2878 (defun gnus-group-list-all-groups (&optional arg)
2879   "List all newsgroups with level ARG or lower.
2880 Default is gnus-level-unsubscribed, which lists all subscribed and most
2881 unsubscribed groups."
2882   (interactive "P")
2883   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
2884
2885 ;; Redefine this to list ALL killed groups if prefix arg used.
2886 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
2887 (defun gnus-group-list-killed (&optional arg)
2888   "List all killed newsgroups in the group buffer.
2889 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
2890 entail asking the server for the groups."
2891   (interactive "P")
2892   ;; Find all possible killed newsgroups if arg.
2893   (when arg
2894     (gnus-get-killed-groups))
2895   (if (not gnus-killed-list)
2896       (gnus-message 6 "No killed groups")
2897     (let (gnus-group-list-mode)
2898       (funcall gnus-group-prepare-function
2899                gnus-level-killed t gnus-level-killed))
2900     (goto-char (point-min)))
2901   (gnus-group-position-point))
2902
2903 (defun gnus-group-list-zombies ()
2904   "List all zombie newsgroups in the group buffer."
2905   (interactive)
2906   (if (not gnus-zombie-list)
2907       (gnus-message 6 "No zombie groups")
2908     (let (gnus-group-list-mode)
2909       (funcall gnus-group-prepare-function
2910                gnus-level-zombie t gnus-level-zombie))
2911     (goto-char (point-min)))
2912   (gnus-group-position-point))
2913
2914 (defun gnus-group-list-active ()
2915   "List all groups that are available from the server(s)."
2916   (interactive)
2917   ;; First we make sure that we have really read the active file.
2918   (unless (gnus-read-active-file-p)
2919     (let ((gnus-read-active-file t))
2920       (gnus-read-active-file)))
2921   ;; Find all groups and sort them.
2922   (let ((groups
2923          (sort
2924           (let (list)
2925             (mapatoms
2926              (lambda (sym)
2927                (and (boundp sym)
2928                     (symbol-value sym)
2929                     (push (symbol-name sym) list)))
2930              gnus-active-hashtb)
2931             list)
2932           'string<))
2933         (buffer-read-only nil))
2934     (erase-buffer)
2935     (while groups
2936       (gnus-group-insert-group-line-info (pop groups)))
2937     (goto-char (point-min))))
2938
2939 (defun gnus-activate-all-groups (level)
2940   "Activate absolutely all groups."
2941   (interactive (list 7))
2942   (let ((gnus-activate-level level)
2943         (gnus-activate-foreign-newsgroups level))
2944     (gnus-group-get-new-news)))
2945
2946 (defun gnus-group-get-new-news (&optional arg)
2947   "Get newly arrived articles.
2948 If ARG is a number, it specifies which levels you are interested in
2949 re-scanning.  If ARG is non-nil and not a number, this will force
2950 \"hard\" re-reading of the active files from all servers."
2951   (interactive "P")
2952   (run-hooks 'gnus-get-new-news-hook)
2953
2954   ;; Read any slave files.
2955   (unless gnus-slave
2956     (gnus-master-read-slave-newsrc))
2957
2958   ;; We might read in new NoCeM messages here.
2959   (when (and gnus-use-nocem 
2960              (null arg))
2961     (gnus-nocem-scan-groups))
2962   ;; If ARG is not a number, then we read the active file.
2963   (when (and arg (not (numberp arg)))
2964     (let ((gnus-read-active-file t))
2965       (gnus-read-active-file))
2966     (setq arg nil))
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-setup-news 'force)
3175     (gnus-group-list-groups arg)))
3176
3177 (defun gnus-group-read-init-file ()
3178   "Read the Gnus elisp init file."
3179   (interactive)
3180   (gnus-read-init-file))
3181
3182 (defun gnus-group-check-bogus-groups (&optional silent)
3183   "Check bogus newsgroups.
3184 If given a prefix, don't ask for confirmation before removing a bogus
3185 group."
3186   (interactive "P")
3187   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
3188   (gnus-group-list-groups))
3189
3190 (defun gnus-group-edit-global-kill (&optional article group)
3191   "Edit the global kill file.
3192 If GROUP, edit that local kill file instead."
3193   (interactive "P")
3194   (setq gnus-current-kill-article article)
3195   (gnus-kill-file-edit-file group)
3196   (gnus-message
3197    6
3198    (substitute-command-keys
3199     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
3200             (if group "local" "global")))))
3201
3202 (defun gnus-group-edit-local-kill (article group)
3203   "Edit a local kill file."
3204   (interactive (list nil (gnus-group-group-name)))
3205   (gnus-group-edit-global-kill article group))
3206
3207 (defun gnus-group-force-update ()
3208   "Update `.newsrc' file."
3209   (interactive)
3210   (gnus-save-newsrc-file))
3211
3212 (defun gnus-group-suspend ()
3213   "Suspend the current Gnus session.
3214 In fact, cleanup buffers except for group mode buffer.
3215 The hook gnus-suspend-gnus-hook is called before actually suspending."
3216   (interactive)
3217   (run-hooks 'gnus-suspend-gnus-hook)
3218   ;; Kill Gnus buffers except for group mode buffer.
3219   (let* ((group-buf (get-buffer gnus-group-buffer))
3220          ;; Do this on a separate list in case the user does a ^G before we finish
3221          (gnus-buffer-list
3222           (delete group-buf (delete gnus-dribble-buffer
3223                                     (append gnus-buffer-list nil)))))
3224     (while gnus-buffer-list
3225       (gnus-kill-buffer (pop gnus-buffer-list)))
3226     (gnus-kill-gnus-frames)
3227     (when group-buf
3228       (setq gnus-buffer-list (list group-buf))
3229       (bury-buffer group-buf)
3230       (delete-windows-on group-buf t))))
3231
3232 (defun gnus-group-clear-dribble ()
3233   "Clear all information from the dribble buffer."
3234   (interactive)
3235   (gnus-dribble-clear)
3236   (gnus-message 7 "Cleared dribble buffer"))
3237
3238 (defun gnus-group-exit ()
3239   "Quit reading news after updating .newsrc.eld and .newsrc.
3240 The hook `gnus-exit-gnus-hook' is called before actually exiting."
3241   (interactive)
3242   (when 
3243       (or noninteractive                ;For gnus-batch-kill
3244           (not gnus-interactive-exit)   ;Without confirmation
3245           gnus-expert-user
3246           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
3247     (run-hooks 'gnus-exit-gnus-hook)
3248     ;; Offer to save data from non-quitted summary buffers.
3249     (gnus-offer-save-summaries)
3250     ;; Save the newsrc file(s).
3251     (gnus-save-newsrc-file)
3252     ;; Kill-em-all.
3253     (gnus-close-backends)
3254     ;; Reset everything.
3255     (gnus-clear-system)
3256     ;; Allow the user to do things after cleaning up.
3257     (run-hooks 'gnus-after-exiting-gnus-hook)))
3258
3259 (defun gnus-group-quit ()
3260   "Quit reading news without updating .newsrc.eld or .newsrc.
3261 The hook `gnus-exit-gnus-hook' is called before actually exiting."
3262   (interactive)
3263   (when (or noninteractive              ;For gnus-batch-kill
3264             (zerop (buffer-size))
3265             (not (gnus-server-opened gnus-select-method))
3266             gnus-expert-user
3267             (not gnus-current-startup-file)
3268             (gnus-yes-or-no-p
3269              (format "Quit reading news without saving %s? "
3270                      (file-name-nondirectory gnus-current-startup-file))))
3271     (run-hooks 'gnus-exit-gnus-hook)
3272     (gnus-configure-windows 'group t)
3273     (gnus-dribble-save)
3274     (gnus-close-backends)
3275     (gnus-clear-system)
3276     (gnus-kill-buffer gnus-group-buffer)
3277     ;; Allow the user to do things after cleaning up.
3278     (run-hooks 'gnus-after-exiting-gnus-hook)))
3279
3280 (defun gnus-group-describe-briefly ()
3281   "Give a one line description of the group mode commands."
3282   (interactive)
3283   (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")))
3284
3285 (defun gnus-group-browse-foreign-server (method)
3286   "Browse a foreign news server.
3287 If called interactively, this function will ask for a select method
3288  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
3289 If not, METHOD should be a list where the first element is the method
3290 and the second element is the address."
3291   (interactive
3292    (list (let ((how (completing-read
3293                      "Which backend: "
3294                      (append gnus-valid-select-methods gnus-server-alist)
3295                      nil t (cons "nntp" 0) 'gnus-method-history)))
3296            ;; We either got a backend name or a virtual server name.
3297            ;; If the first, we also need an address.
3298            (if (assoc how gnus-valid-select-methods)
3299                (list (intern how)
3300                      ;; Suggested by mapjph@bath.ac.uk.
3301                      (completing-read
3302                       "Address: "
3303                       (mapcar (lambda (server) (list server))
3304                               gnus-secondary-servers)))
3305              ;; We got a server name, so we find the method.
3306              (gnus-server-to-method how)))))
3307   (gnus-browse-foreign-server method))
3308
3309 (defun gnus-group-set-info (info &optional method-only-group part)
3310   (let* ((entry (gnus-gethash
3311                  (or method-only-group (gnus-info-group info))
3312                  gnus-newsrc-hashtb))
3313          (part-info info)
3314          (info (if method-only-group (nth 2 entry) info))
3315          method)
3316     (when method-only-group
3317       (unless entry
3318         (error "Trying to change non-existent group %s" method-only-group))
3319       ;; We have received parts of the actual group info - either the
3320       ;; select method or the group parameters.  We first check
3321       ;; whether we have to extend the info, and if so, do that.
3322       (let ((len (length info))
3323             (total (if (eq part 'method) 5 6)))
3324         (when (< len total)
3325           (setcdr (nthcdr (1- len) info)
3326                   (make-list (- total len) nil)))
3327         ;; Then we enter the new info.
3328         (setcar (nthcdr (1- total) info) part-info)))
3329     (unless entry
3330       ;; This is a new group, so we just create it.
3331       (save-excursion
3332         (set-buffer gnus-group-buffer)
3333         (setq method (gnus-info-method info))
3334         (when (gnus-server-equal method "native")
3335           (setq method nil))
3336         (save-excursion
3337           (set-buffer gnus-group-buffer)
3338           (if method
3339               ;; It's a foreign group...
3340               (gnus-group-make-group
3341                (gnus-group-real-name (gnus-info-group info))
3342                (if (stringp method) method
3343                  (prin1-to-string (car method)))
3344                (and (consp method)
3345                     (nth 1 (gnus-info-method info))))
3346             ;; It's a native group.
3347             (gnus-group-make-group (gnus-info-group info))))
3348         (gnus-message 6 "Note: New group created")
3349         (setq entry
3350               (gnus-gethash (gnus-group-prefixed-name
3351                              (gnus-group-real-name (gnus-info-group info))
3352                              (or (gnus-info-method info) gnus-select-method))
3353                             gnus-newsrc-hashtb))))
3354     ;; Whether it was a new group or not, we now have the entry, so we
3355     ;; can do the update.
3356     (if entry
3357         (progn
3358           (setcar (nthcdr 2 entry) info)
3359           (when (and (not (eq (car entry) t))
3360                      (gnus-active (gnus-info-group info)))
3361             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
3362       (error "No such group: %s" (gnus-info-group info)))))
3363
3364 (defun gnus-group-set-method-info (group select-method)
3365   (gnus-group-set-info select-method group 'method))
3366
3367 (defun gnus-group-set-params-info (group params)
3368   (gnus-group-set-info params group 'params))
3369
3370 (defun gnus-add-marked-articles (group type articles &optional info force)
3371   ;; Add ARTICLES of TYPE to the info of GROUP.
3372   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
3373   ;; add, but replace marked articles of TYPE with ARTICLES.
3374   (let ((info (or info (gnus-get-info group)))
3375         (uncompressed '(score bookmark killed))
3376         marked m)
3377     (or (not info)
3378         (and (not (setq marked (nthcdr 3 info)))
3379              (or (null articles)
3380                  (setcdr (nthcdr 2 info)
3381                          (list (list (cons type (gnus-compress-sequence
3382                                                  articles t)))))))
3383         (and (not (setq m (assq type (car marked))))
3384              (or (null articles)
3385                  (setcar marked
3386                          (cons (cons type (gnus-compress-sequence articles t) )
3387                                (car marked)))))
3388         (if force
3389             (if (null articles)
3390                 (setcar (nthcdr 3 info)
3391                         (delq (assq type (car marked)) (car marked)))
3392               (setcdr m (gnus-compress-sequence articles t)))
3393           (setcdr m (gnus-compress-sequence
3394                      (sort (nconc (gnus-uncompress-range (cdr m))
3395                                   (copy-sequence articles)) '<) t))))))
3396
3397 ;;;
3398 ;;; Group timestamps
3399 ;;;
3400
3401 (defun gnus-group-set-timestamp ()
3402   "Change the timestamp of the current group to the current time.
3403 This function can be used in hooks like `gnus-select-group-hook'
3404 or `gnus-group-catchup-group-hook'."
3405   (when gnus-newsgroup-name
3406     (let ((time (current-time)))
3407       (setcdr (cdr time) nil)
3408       (gnus-group-set-parameter gnus-newsgroup-name 'timestamp time))))
3409
3410 (defsubst gnus-group-timestamp (group)
3411   "Return the timestamp for GROUP."
3412   (gnus-group-get-parameter group 'timestamp))
3413
3414 (defun gnus-group-timestamp-delta (group)
3415   "Return the offset in seconds from the timestamp for GROUP to the current time, as a floating point number."
3416   (let* ((time (or (gnus-group-timestamp group)
3417                   (list 0 0)))
3418          (delta (gnus-time-minus (current-time) time)))
3419     (+ (* (nth 0 delta) 65536.0)
3420        (nth 1 delta))))
3421
3422 (defun gnus-group-timestamp-string (group)
3423   "Return a string of the timestamp for GROUP."
3424   (let ((time (gnus-group-timestamp group)))
3425     (if (not time)
3426         ""
3427       (gnus-time-iso8601 time))))
3428
3429 (provide 'gnus-group)
3430
3431 ;;; gnus-group.el ends here