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