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