*** 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-load)
29 (require 'gnus-start)
30 (require 'nnmail)
31 (require 'gnus-spec)
32 (require 'gnus-int)
33 (require 'gnus-range)
34 (require 'gnus-win)
35
36 (defvar gnus-group-archive-directory
37   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
38   "*The address of the (ding) archives.")
39
40 (defvar gnus-group-recent-archive-directory
41   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
42   "*The address of the most recent (ding) articles.")
43
44 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
45 (defvar gnus-no-groups-message "No news is horrible news"
46   "*Message displayed by Gnus when no groups are available.")
47
48 (defvar gnus-keep-same-level nil
49   "*Non-nil means that the next newsgroup after the current will be on the same level.
50 When you type, for instance, `n' after reading the last article in the
51 current newsgroup, you will go to the next newsgroup.  If this variable
52 is nil, the next newsgroup will be the next from the group
53 buffer.
54 If this variable is non-nil, Gnus will either put you in the
55 next newsgroup with the same level, or, if no such newsgroup is
56 available, the next newsgroup with the lowest possible level higher
57 than the current level.
58 If this variable is `best', Gnus will make the next newsgroup the one
59 with the best level.")
60
61 (defvar gnus-group-goto-unread t
62   "*If non-nil, movement commands will go to the next unread and subscribed group.")
63
64 (defvar gnus-goto-next-group-when-activating t
65   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
66
67 (defvar gnus-permanently-visible-groups nil
68   "*Regexp to match groups that should always be listed in the group buffer.
69 This means that they will still be listed when there are no unread
70 articles in the groups.")
71
72 (defvar gnus-list-groups-with-ticked-articles t
73   "*If non-nil, list groups that have only ticked articles.
74 If nil, only list groups that have unread articles.")
75
76 (defvar gnus-group-default-list-level gnus-level-subscribed
77   "*Default listing level.
78 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
79
80 (defvar gnus-group-list-inactive-groups t
81   "*If non-nil, inactive groups will be listed.")
82
83 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
84   "*Function used for sorting the group buffer.
85 This function will be called with group info entries as the arguments
86 for the groups to be sorted.  Pre-made functions include
87 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
88 `gnus-group-sort-by-level', `gnus-group-sort-by-score',
89 `gnus-group-sort-by-method', and `gnus-group-sort-by-rank'.
90
91 This variable can also be a list of sorting functions.  In that case,
92 the most significant sort function should be the last function in the
93 list.")
94
95 (defvar gnus-group-line-format "%M\%S\%p\%P\%5y: %(%g%)%l\n"
96   "*Format of group lines.
97 It works along the same lines as a normal formatting string,
98 with some simple extensions.
99
100 %M    Only marked articles (character, \"*\" or \" \")
101 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
102 %L    Level of subscribedness (integer)
103 %N    Number of unread articles (integer)
104 %I    Number of dormant articles (integer)
105 %i    Number of ticked and dormant (integer)
106 %T    Number of ticked articles (integer)
107 %R    Number of read articles (integer)
108 %t    Estimated total number of articles (integer)
109 %y    Number of unread, unticked articles (integer)
110 %G    Group name (string)
111 %g    Qualified group name (string)
112 %D    Group description (string)
113 %s    Select method (string)
114 %o    Moderated group (char, \"m\")
115 %p    Process mark (char)
116 %O    Moderated group (string, \"(m)\" or \"\")
117 %P    Topic indentation (string)
118 %l    Whether there are GroupLens predictions for this group (string)
119 %n    Select from where (string)
120 %z    A string that look like `<%s:%n>' if a foreign select method is used
121 %u    User defined specifier.  The next character in the format string should
122       be a letter.  Gnus will call the function gnus-user-format-function-X,
123       where X is the letter following %u.  The function will be passed the
124       current header as argument.  The function should return a string, which
125       will be inserted into the buffer just like information from any other
126       group specifier.
127
128 Text between %( and %) will be highlighted with `gnus-mouse-face' when
129 the mouse point move inside the area.  There can only be one such area.
130
131 Note that this format specification is not always respected.  For
132 reasons of efficiency, when listing killed groups, this specification
133 is ignored altogether.  If the spec is changed considerably, your
134 output may end up looking strange when listing both alive and killed
135 groups.
136
137 If you use %o or %O, reading the active file will be slower and quite
138 a bit of extra memory will be used. %D will also worsen performance.
139 Also note that if you change the format specification to include any
140 of these specs, you must probably re-start Gnus to see them go into
141 effect.")
142
143 (defvar gnus-group-mode-line-format "Gnus: %%b {%M\%:%S}"
144   "*The format specification for the group mode line.
145 It works along the same lines as a normal formatting string,
146 with some simple extensions:
147
148 %S   The native news server.
149 %M   The native select method.
150 %:   \":\" if %S isn't \"\".")
151
152 (defvar gnus-group-mode-hook nil
153   "*A hook for Gnus group mode.")
154
155 (defvar gnus-group-catchup-group-hook nil
156   "*A hook run when catching up a group from the group buffer.")
157
158 (defvar gnus-group-update-group-hook nil
159   "*A hook called when updating group lines.")
160
161 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
162   "*A function that is called to generate the group buffer.
163 The function is called with three arguments: The first is a number;
164 all group with a level less or equal to that number should be listed,
165 if the second is non-nil, empty groups should also be displayed.  If
166 the third is non-nil, it is a number.  No groups with a level lower
167 than this number should be displayed.
168
169 The only current function implemented is `gnus-group-prepare-flat'.")
170
171 (defvar gnus-group-prepare-hook nil
172   "*A hook called after the group buffer has been generated.
173 If you want to modify the group buffer, you can use this hook.")
174
175 (defvar gnus-suspend-gnus-hook nil
176   "*A hook called when suspending (not exiting) Gnus.")
177
178 (defvar gnus-exit-gnus-hook nil
179   "*A hook called when exiting Gnus.")
180
181 (defvar gnus-after-exiting-gnus-hook nil
182   "*A hook called after exiting Gnus.")
183
184 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
185   "*A hook called when a group line is changed.
186 The hook will not be called if `gnus-visual' is nil.
187
188 The default function `gnus-group-highlight-line' will
189 highlight the line according to the `gnus-group-highlight'
190 variable.")
191
192 ;;; Internal variables
193
194 (defvar gnus-group-indentation-function nil)
195 (defvar gnus-goto-missing-group-function nil)
196 (defvar gnus-group-goto-next-group-function nil
197   "Function to override finding the next group after listing groups.")
198
199 (defvar gnus-group-edit-buffer nil)
200
201 (defvar gnus-group-line-format-alist
202   `((?M gnus-tmp-marked-mark ?c)
203     (?S gnus-tmp-subscribed ?c)
204     (?L gnus-tmp-level ?d)
205     (?N (cond ((eq number t) "*" )
206               ((numberp number) 
207                (int-to-string
208                 (+ number
209                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
210                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
211               (t number)) ?s)
212     (?R gnus-tmp-number-of-read ?s)
213     (?t gnus-tmp-number-total ?d)
214     (?y gnus-tmp-number-of-unread ?s)
215     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
216     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
217     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
218            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
219     (?g gnus-tmp-group ?s)
220     (?G gnus-tmp-qualified-group ?s)
221     (?c (gnus-short-group-name gnus-tmp-group) ?s)
222     (?D gnus-tmp-newsgroup-description ?s)
223     (?o gnus-tmp-moderated ?c)
224     (?O gnus-tmp-moderated-string ?s)
225     (?p gnus-tmp-process-marked ?c)
226     (?s gnus-tmp-news-server ?s)
227     (?n gnus-tmp-news-method ?s)
228     (?P gnus-group-indentation ?s)
229     (?l gnus-tmp-grouplens ?s)
230     (?z gnus-tmp-news-method-string ?s)
231     (?u gnus-tmp-user-defined ?s)))
232
233 (defvar gnus-group-mode-line-format-alist
234   `((?S gnus-tmp-news-server ?s)
235     (?M gnus-tmp-news-method ?s)
236     (?u gnus-tmp-user-defined ?s)
237     (?: gnus-tmp-colon ?s)))
238
239 (defvar gnus-topic-topology nil
240   "The complete topic hierarchy.")
241
242 (defvar gnus-topic-alist nil
243   "The complete topic-group alist.")
244
245 (defvar gnus-group-marked nil)
246
247 (defvar gnus-group-list-mode nil)
248
249 ;;;
250 ;;; Gnus group mode
251 ;;;
252
253 (put 'gnus-group-mode 'mode-class 'special)
254
255 (when t
256   (gnus-define-keys gnus-group-mode-map
257     " " gnus-group-read-group
258     "=" gnus-group-select-group
259     "\r" gnus-group-select-group
260     "\M-\r" gnus-group-quick-select-group
261     "j" gnus-group-jump-to-group
262     "n" gnus-group-next-unread-group
263     "p" gnus-group-prev-unread-group
264     "\177" gnus-group-prev-unread-group
265     [delete] gnus-group-prev-unread-group
266     "N" gnus-group-next-group
267     "P" gnus-group-prev-group
268     "\M-n" gnus-group-next-unread-group-same-level
269     "\M-p" gnus-group-prev-unread-group-same-level
270     "," gnus-group-best-unread-group
271     "." gnus-group-first-unread-group
272     "u" gnus-group-unsubscribe-current-group
273     "U" gnus-group-unsubscribe-group
274     "c" gnus-group-catchup-current
275     "C" gnus-group-catchup-current-all
276     "l" gnus-group-list-groups
277     "L" gnus-group-list-all-groups
278     "m" gnus-group-mail
279     "g" gnus-group-get-new-news
280     "\M-g" gnus-group-get-new-news-this-group
281     "R" gnus-group-restart
282     "r" gnus-group-read-init-file
283     "B" gnus-group-browse-foreign-server
284     "b" gnus-group-check-bogus-groups
285     "F" gnus-find-new-newsgroups
286     "\C-c\C-d" gnus-group-describe-group
287     "\M-d" gnus-group-describe-all-groups
288     "\C-c\C-a" gnus-group-apropos
289     "\C-c\M-\C-a" gnus-group-description-apropos
290     "a" gnus-group-post-news
291     "\ek" gnus-group-edit-local-kill
292     "\eK" gnus-group-edit-global-kill
293     "\C-k" gnus-group-kill-group
294     "\C-y" gnus-group-yank-group
295     "\C-w" gnus-group-kill-region
296     "\C-x\C-t" gnus-group-transpose-groups
297     "\C-c\C-l" gnus-group-list-killed
298     "\C-c\C-x" gnus-group-expire-articles
299     "\C-c\M-\C-x" gnus-group-expire-all-groups
300     "V" gnus-version
301     "s" gnus-group-save-newsrc
302     "z" gnus-group-suspend
303                                         ;    "Z" gnus-group-clear-dribble
304     "q" gnus-group-exit
305     "Q" gnus-group-quit
306     "?" gnus-group-describe-briefly
307     "\C-c\C-i" gnus-info-find-node
308     "\M-e" gnus-group-edit-group-method
309     "^" gnus-group-enter-server-mode
310     gnus-mouse-2 gnus-mouse-pick-group
311     "<" beginning-of-buffer
312     ">" end-of-buffer
313     "\C-c\C-b" gnus-bug
314     "\C-c\C-s" gnus-group-sort-groups
315     "t" gnus-topic-mode
316     "\C-c\M-g" gnus-activate-all-groups
317     "\M-&" gnus-group-universal-argument
318     "#" gnus-group-mark-group
319     "\M-#" gnus-group-unmark-group)
320
321   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
322     "m" gnus-group-mark-group
323     "u" gnus-group-unmark-group
324     "w" gnus-group-mark-region
325     "m" gnus-group-mark-buffer
326     "r" gnus-group-mark-regexp
327     "U" gnus-group-unmark-all-groups)
328
329   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
330     "d" gnus-group-make-directory-group
331     "h" gnus-group-make-help-group
332     "a" gnus-group-make-archive-group
333     "k" gnus-group-make-kiboze-group
334     "m" gnus-group-make-group
335     "E" gnus-group-edit-group
336     "e" gnus-group-edit-group-method
337     "p" gnus-group-edit-group-parameters
338     "v" gnus-group-add-to-virtual
339     "V" gnus-group-make-empty-virtual
340     "D" gnus-group-enter-directory
341     "f" gnus-group-make-doc-group
342     "r" gnus-group-rename-group
343     "\177" gnus-group-delete-group
344     [delete] gnus-group-delete-group)
345
346   (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
347     "b" gnus-group-brew-soup
348     "w" gnus-soup-save-areas
349     "s" gnus-soup-send-replies
350     "p" gnus-soup-pack-packet
351     "r" nnsoup-pack-replies)
352
353   (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
354     "s" gnus-group-sort-groups
355     "a" gnus-group-sort-groups-by-alphabet
356     "u" gnus-group-sort-groups-by-unread
357     "l" gnus-group-sort-groups-by-level
358     "v" gnus-group-sort-groups-by-score
359     "r" gnus-group-sort-groups-by-rank
360     "m" gnus-group-sort-groups-by-method)
361
362   (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
363     "k" gnus-group-list-killed
364     "z" gnus-group-list-zombies
365     "s" gnus-group-list-groups
366     "u" gnus-group-list-all-groups
367     "A" gnus-group-list-active
368     "a" gnus-group-apropos
369     "d" gnus-group-description-apropos
370     "m" gnus-group-list-matching
371     "M" gnus-group-list-all-matching
372     "l" gnus-group-list-level)
373
374   (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
375     "f" gnus-score-flush-cache)
376
377   (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
378     "f" gnus-group-fetch-faq)
379
380   (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
381     "l" gnus-group-set-current-level
382     "t" gnus-group-unsubscribe-current-group
383     "s" gnus-group-unsubscribe-group
384     "k" gnus-group-kill-group
385     "y" gnus-group-yank-group
386     "w" gnus-group-kill-region
387     "\C-k" gnus-group-kill-level
388     "z" gnus-group-kill-all-zombies))
389
390 (defun gnus-group-mode ()
391   "Major mode for reading news.
392
393 All normal editing commands are switched off.
394 \\<gnus-group-mode-map>
395 The group buffer lists (some of) the groups available.  For instance,
396 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
397 lists all zombie groups.
398
399 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
400 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
401
402 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
403
404 The following commands are available:
405
406 \\{gnus-group-mode-map}"
407   (interactive)
408   (when (and menu-bar-mode
409              (gnus-visual-p 'group-menu 'menu))
410     (gnus-group-make-menu-bar))
411   (kill-all-local-variables)
412   (gnus-simplify-mode-line)
413   (setq major-mode 'gnus-group-mode)
414   (setq mode-name "Group")
415   (gnus-group-set-mode-line)
416   (setq mode-line-process nil)
417   (use-local-map gnus-group-mode-map)
418   (buffer-disable-undo (current-buffer))
419   (setq truncate-lines t)
420   (setq buffer-read-only t)
421   (gnus-update-format-specifications nil 'group 'group-mode)
422   (gnus-update-group-mark-positions)
423   (gnus-make-local-hook 'post-command-hook)
424   (gnus-add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
425   (when gnus-use-undo
426     (gnus-undo-mode 1))
427   (run-hooks 'gnus-group-mode-hook))
428
429 (defun gnus-update-group-mark-positions ()
430   (save-excursion
431     (let ((gnus-process-mark 128)
432           (gnus-group-marked '("dummy.group"))
433           (gnus-active-hashtb (make-vector 10 0)))
434       (gnus-set-active "dummy.group" '(0 . 0))
435       (gnus-set-work-buffer)
436       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
437       (goto-char (point-min))
438       (setq gnus-group-mark-positions
439             (list (cons 'process (and (search-forward "\200" nil t)
440                                       (- (point) 2))))))))
441
442 (defun gnus-clear-inboxes-moved ()
443   (setq nnmail-moved-inboxes nil))
444
445 (defun gnus-mouse-pick-group (e)
446   "Enter the group under the mouse pointer."
447   (interactive "e")
448   (mouse-set-point e)
449   (gnus-group-read-group nil))
450
451 ;; Look at LEVEL and find out what the level is really supposed to be.
452 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
453 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
454 (defun gnus-group-default-level (&optional level number-or-nil)
455   (cond
456    (gnus-group-use-permanent-levels
457     (or (setq gnus-group-use-permanent-levels
458               (or level (if (numberp gnus-group-use-permanent-levels)
459                             gnus-group-use-permanent-levels
460                           (or gnus-group-default-list-level
461                               gnus-level-subscribed))))
462         gnus-group-default-list-level gnus-level-subscribed))
463    (number-or-nil
464     level)
465    (t
466     (or level gnus-group-default-list-level gnus-level-subscribed))))
467
468 (defun gnus-group-setup-buffer ()
469   (switch-to-buffer gnus-group-buffer)
470   (unless (eq major-mode 'gnus-group-mode)
471     (gnus-add-current-to-buffer-list)
472     (gnus-group-mode)
473     (when gnus-carpal
474       (gnus-carpal-setup-buffer 'group))))
475
476 (defun gnus-group-list-groups (&optional level unread lowest)
477   "List newsgroups with level LEVEL or lower that have unread articles.
478 Default is all subscribed groups.
479 If argument UNREAD is non-nil, groups with no unread articles are also
480 listed."
481   (interactive (list (if current-prefix-arg
482                          (prefix-numeric-value current-prefix-arg)
483                        (or
484                         (gnus-group-default-level nil t)
485                         gnus-group-default-list-level
486                         gnus-level-subscribed))))
487   (or level
488       (setq level (car gnus-group-list-mode)
489             unread (cdr gnus-group-list-mode)))
490   (setq level (gnus-group-default-level level))
491   (gnus-group-setup-buffer)             ;May call from out of group buffer
492   (gnus-update-format-specifications)
493   (let ((case-fold-search nil)
494         (props (text-properties-at (gnus-point-at-bol)))
495         (group (gnus-group-group-name)))
496     (set-buffer gnus-group-buffer)
497     (funcall gnus-group-prepare-function level unread lowest)
498     (if (zerop (buffer-size))
499         (gnus-message 5 gnus-no-groups-message)
500       (goto-char (point-max))
501       (when (or (not gnus-group-goto-next-group-function)
502                 (not (funcall gnus-group-goto-next-group-function 
503                               group props)))
504         (if (not group)
505             ;; Go to the first group with unread articles.
506             (gnus-group-search-forward t)
507           ;; Find the right group to put point on.  If the current group
508           ;; has disappeared in the new listing, try to find the next
509           ;; one.        If no next one can be found, just leave point at the
510           ;; first newsgroup in the buffer.
511           (if (not (gnus-goto-char
512                     (text-property-any
513                      (point-min) (point-max)
514                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
515               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
516                 (while (and newsrc
517                             (not (gnus-goto-char
518                                   (text-property-any
519                                    (point-min) (point-max) 'gnus-group
520                                    (gnus-intern-safe
521                                     (caar newsrc) gnus-active-hashtb)))))
522                   (setq newsrc (cdr newsrc)))
523                 (or newsrc (progn (goto-char (point-max))
524                                   (forward-line -1)))))))
525       ;; Adjust cursor point.
526       (gnus-group-position-point))))
527
528 (defun gnus-group-list-level (level &optional all)
529   "List groups on LEVEL.
530 If ALL (the prefix), also list groups that have no unread articles."
531   (interactive "nList groups on level: \nP")
532   (gnus-group-list-groups level all level))
533
534 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
535   "List all newsgroups with unread articles of level LEVEL or lower.
536 If ALL is non-nil, list groups that have no unread articles.
537 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
538 If REGEXP, only list groups matching REGEXP."
539   (set-buffer gnus-group-buffer)
540   (let ((buffer-read-only nil)
541         (newsrc (cdr gnus-newsrc-alist))
542         (lowest (or lowest 1))
543         info clevel unread group params)
544     (erase-buffer)
545     (if (< lowest gnus-level-zombie)
546         ;; List living groups.
547         (while newsrc
548           (setq info (car newsrc)
549                 group (gnus-info-group info)
550                 params (gnus-info-params info)
551                 newsrc (cdr newsrc)
552                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
553           (and unread                   ; This group might be bogus
554                (or (not regexp)
555                    (string-match regexp group))
556                (<= (setq clevel (gnus-info-level info)) level)
557                (>= clevel lowest)
558                (or all                  ; We list all groups?
559                    (if (eq unread t)    ; Unactivated?
560                        gnus-group-list-inactive-groups ; We list unactivated 
561                      (> unread 0))      ; We list groups with unread articles
562                    (and gnus-list-groups-with-ticked-articles
563                         (cdr (assq 'tick (gnus-info-marks info))))
564                                         ; And groups with tickeds
565                    ;; Check for permanent visibility.
566                    (and gnus-permanently-visible-groups
567                         (string-match gnus-permanently-visible-groups
568                                       group))
569                    (memq 'visible params)
570                    (cdr (assq 'visible params)))
571                (gnus-group-insert-group-line
572                 group (gnus-info-level info)
573                 (gnus-info-marks info) unread (gnus-info-method info)))))
574
575     ;; List dead groups.
576     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
577          (gnus-group-prepare-flat-list-dead
578           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
579           gnus-level-zombie ?Z
580           regexp))
581     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
582          (gnus-group-prepare-flat-list-dead
583           (setq gnus-killed-list (sort gnus-killed-list 'string<))
584           gnus-level-killed ?K regexp))
585
586     (gnus-group-set-mode-line)
587     (setq gnus-group-list-mode (cons level all))
588     (run-hooks 'gnus-group-prepare-hook)))
589
590 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
591   ;; List zombies and killed lists somewhat faster, which was
592   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
593   ;; this by ignoring the group format specification altogether.
594   (let (group)
595     (if regexp
596         ;; This loop is used when listing groups that match some
597         ;; regexp.
598         (while groups
599           (setq group (pop groups))
600           (when (string-match regexp group)
601             (gnus-add-text-properties
602              (point) (prog1 (1+ (point))
603                        (insert " " mark "     *: " group "\n"))
604              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
605                    'gnus-unread t
606                    'gnus-level level))))
607       ;; This loop is used when listing all groups.
608       (while groups
609         (gnus-add-text-properties
610          (point) (prog1 (1+ (point))
611                    (insert " " mark "     *: "
612                            (setq group (pop groups)) "\n"))
613          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
614                'gnus-unread t
615                'gnus-level level))))))
616
617 (defun gnus-group-update-group-line ()
618   "Update the current line in the group buffer."
619   (let* ((buffer-read-only nil)
620          (group (gnus-group-group-name))
621          (entry (and group (gnus-gethash group gnus-newsrc-hashtb)))
622          gnus-group-indentation)
623     (when group
624       (and entry
625            (not (gnus-ephemeral-group-p group))
626            (gnus-dribble-enter
627             (concat "(gnus-group-set-info '"
628                     (prin1-to-string (nth 2 entry)) ")")))
629       (setq gnus-group-indentation (gnus-group-group-indentation))
630       (gnus-delete-line)
631       (gnus-group-insert-group-line-info group)
632       (forward-line -1)
633       (gnus-group-position-point))))
634
635 (defun gnus-group-insert-group-line-info (group)
636   "Insert GROUP on the current line."
637   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
638         active info)
639     (if entry
640         (progn
641           ;; (Un)subscribed group.
642           (setq info (nth 2 entry))
643           (gnus-group-insert-group-line
644            group (gnus-info-level info) (gnus-info-marks info)
645            (or (car entry) t) (gnus-info-method info)))
646       ;; This group is dead.
647       (gnus-group-insert-group-line
648        group
649        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
650        nil
651        (if (setq active (gnus-active group))
652            (- (1+ (cdr active)) (car active)) 0)
653        nil))))
654
655 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
656                                                     gnus-tmp-marked number
657                                                     gnus-tmp-method)
658   "Insert a group line in the group buffer."
659   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
660          (gnus-tmp-number-total
661           (if gnus-tmp-active
662               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
663             0))
664          (gnus-tmp-number-of-unread
665           (if (numberp number) (int-to-string (max 0 number))
666             "*"))
667          (gnus-tmp-number-of-read
668           (if (numberp number)
669               (int-to-string (max 0 (- gnus-tmp-number-total number)))
670             "*"))
671          (gnus-tmp-subscribed
672           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
673                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
674                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
675                 (t ?K)))
676          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
677          (gnus-tmp-newsgroup-description
678           (if gnus-description-hashtb
679               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
680             ""))
681          (gnus-tmp-moderated
682           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
683          (gnus-tmp-moderated-string
684           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
685          (gnus-tmp-method
686           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
687          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
688          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
689          (gnus-tmp-news-method-string
690           (if gnus-tmp-method
691               (format "(%s:%s)" (car gnus-tmp-method)
692                       (cadr gnus-tmp-method)) ""))
693          (gnus-tmp-marked-mark
694           (if (and (numberp number)
695                    (zerop number)
696                    (cdr (assq 'tick gnus-tmp-marked)))
697               ?* ? ))
698          (gnus-tmp-process-marked
699           (if (member gnus-tmp-group gnus-group-marked)
700               gnus-process-mark ? ))
701          (gnus-tmp-grouplens
702           (or (and gnus-use-grouplens
703                    (bbb-grouplens-group-p gnus-tmp-group))
704               ""))
705          (buffer-read-only nil)
706          header gnus-tmp-header)        ; passed as parameter to user-funcs.
707     (beginning-of-line)
708     (gnus-add-text-properties
709      (point)
710      (prog1 (1+ (point))
711        ;; Insert the text.
712        (eval gnus-group-line-format-spec))
713      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
714        gnus-unread ,(if (numberp number)
715                         (string-to-int gnus-tmp-number-of-unread)
716                       t)
717        gnus-marked ,gnus-tmp-marked-mark
718        gnus-indentation ,gnus-group-indentation
719        gnus-level ,gnus-tmp-level))
720     (when (inline (gnus-visual-p 'group-highlight 'highlight))
721       (forward-line -1)
722       (run-hooks 'gnus-group-update-hook)
723       (forward-line))
724     ;; Allow XEmacs to remove front-sticky text properties.
725     (gnus-group-remove-excess-properties)))
726
727 (defun gnus-group-update-group (group &optional visible-only)
728   "Update all lines where GROUP appear.
729 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
730 already."
731   (save-excursion
732     (set-buffer gnus-group-buffer)
733     ;; The buffer may be narrowed.
734     (save-restriction
735       (widen)
736       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
737             (loc (point-min))
738             found buffer-read-only)
739         ;; Enter the current status into the dribble buffer.
740         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
741           (if (and entry (not (gnus-ephemeral-group-p group)))
742               (gnus-dribble-enter
743                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
744                        ")"))))
745         ;; Find all group instances.  If topics are in use, each group
746         ;; may be listed in more than once.
747         (while (setq loc (text-property-any
748                           loc (point-max) 'gnus-group ident))
749           (setq found t)
750           (goto-char loc)
751           (let ((gnus-group-indentation (gnus-group-group-indentation)))
752             (gnus-delete-line)
753             (gnus-group-insert-group-line-info group)
754             (save-excursion
755               (forward-line -1)
756               (run-hooks 'gnus-group-update-group-hook)))
757           (setq loc (1+ loc)))
758         (unless (or found visible-only)
759           ;; No such line in the buffer, find out where it's supposed to
760           ;; go, and insert it there (or at the end of the buffer).
761           (if gnus-goto-missing-group-function
762               (funcall gnus-goto-missing-group-function group)
763             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
764               (while (and entry (car entry)
765                           (not
766                            (gnus-goto-char
767                             (text-property-any
768                              (point-min) (point-max)
769                              'gnus-group (gnus-intern-safe
770                                           (caar entry) gnus-active-hashtb)))))
771                 (setq entry (cdr entry)))
772               (or entry (goto-char (point-max)))))
773           ;; Finally insert the line.
774           (let ((gnus-group-indentation (gnus-group-group-indentation)))
775             (gnus-group-insert-group-line-info group)
776             (save-excursion
777               (forward-line -1)
778               (run-hooks 'gnus-group-update-group-hook))))
779         (gnus-group-set-mode-line)))))
780
781 (defun gnus-group-set-mode-line ()
782   "Update the mode line in the group buffer."
783   (when (memq 'group gnus-updated-mode-lines)
784     ;; Yes, we want to keep this mode line updated.
785     (save-excursion
786       (set-buffer gnus-group-buffer)
787       (let* ((gformat (or gnus-group-mode-line-format-spec
788                           (setq gnus-group-mode-line-format-spec
789                                 (gnus-parse-format
790                                  gnus-group-mode-line-format
791                                  gnus-group-mode-line-format-alist))))
792              (gnus-tmp-news-server (cadr gnus-select-method))
793              (gnus-tmp-news-method (car gnus-select-method))
794              (gnus-tmp-colon (if (equal gnus-tmp-news-server "") "" ":"))
795              (max-len 60)
796              gnus-tmp-header            ;Dummy binding for user-defined formats
797              ;; Get the resulting string.
798              (modified 
799               (and gnus-dribble-buffer
800                    (buffer-name gnus-dribble-buffer)
801                    (buffer-modified-p gnus-dribble-buffer)
802                    (save-excursion
803                      (set-buffer gnus-dribble-buffer)
804                      (not (zerop (buffer-size))))))
805              (mode-string (eval gformat)))
806         ;; Say whether the dribble buffer has been modified.
807         (setq mode-line-modified
808               (if modified "---*- " "----- "))
809         ;; If the line is too long, we chop it off.
810         (when (> (length mode-string) max-len)
811           (setq mode-string (substring mode-string 0 (- max-len 4))))
812         (prog1
813             (setq mode-line-buffer-identification 
814                   (gnus-mode-line-buffer-identification
815                    (list mode-string)))
816           (set-buffer-modified-p modified))))))
817
818 (defun gnus-group-group-name ()
819   "Get the name of the newsgroup on the current line."
820   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
821     (and group (symbol-name group))))
822
823 (defun gnus-group-group-level ()
824   "Get the level of the newsgroup on the current line."
825   (get-text-property (gnus-point-at-bol) 'gnus-level))
826
827 (defun gnus-group-group-indentation ()
828   "Get the indentation of the newsgroup on the current line."
829   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
830       (and gnus-group-indentation-function
831            (funcall gnus-group-indentation-function))
832       ""))
833
834 (defun gnus-group-group-unread ()
835   "Get the number of unread articles of the newsgroup on the current line."
836   (get-text-property (gnus-point-at-bol) 'gnus-unread))
837
838 (defun gnus-group-search-forward (&optional backward all level first-too)
839   "Find the next newsgroup with unread articles.
840 If BACKWARD is non-nil, find the previous newsgroup instead.
841 If ALL is non-nil, just find any newsgroup.
842 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
843 group exists.
844 If FIRST-TOO, the current line is also eligible as a target."
845   (let ((way (if backward -1 1))
846         (low gnus-level-killed)
847         (beg (point))
848         pos found lev)
849     (if (and backward (progn (beginning-of-line)) (bobp))
850         nil
851       (or first-too (forward-line way))
852       (while (and
853               (not (eobp))
854               (not (setq
855                     found
856                     (and (or all
857                              (and
858                               (let ((unread
859                                      (get-text-property (point) 'gnus-unread)))
860                                 (and (numberp unread) (> unread 0)))
861                               (setq lev (get-text-property (point)
862                                                            'gnus-level))
863                               (<= lev gnus-level-subscribed)))
864                          (or (not level)
865                              (and (setq lev (get-text-property (point)
866                                                                'gnus-level))
867                                   (or (= lev level)
868                                       (and (< lev low)
869                                            (< level lev)
870                                            (progn
871                                              (setq low lev)
872                                              (setq pos (point))
873                                              nil))))))))
874               (zerop (forward-line way)))))
875     (if found
876         (progn (gnus-group-position-point) t)
877       (goto-char (or pos beg))
878       (and pos t))))
879
880 ;;; Gnus group mode commands
881
882 ;; Group marking.
883
884 (defun gnus-group-mark-group (n &optional unmark no-advance)
885   "Mark the current group."
886   (interactive "p")
887   (let ((buffer-read-only nil)
888         group)
889     (while (and (> n 0)
890                 (not (eobp)))
891       (when (setq group (gnus-group-group-name))
892         ;; Go to the mark position.
893         (beginning-of-line)
894         (forward-char (or (cdr (assq 'process gnus-group-mark-positions)) 2))
895         (subst-char-in-region
896          (point) (1+ (point)) (following-char) 
897          (if unmark
898              (progn
899                (setq gnus-group-marked (delete group gnus-group-marked))
900                ? )
901            (setq gnus-group-marked
902                  (cons group (delete group gnus-group-marked)))
903            gnus-process-mark)))
904       (unless no-advance
905         (gnus-group-next-group 1))
906       (decf n))
907     (gnus-summary-position-point)
908     n))
909
910 (defun gnus-group-unmark-group (n)
911   "Remove the mark from the current group."
912   (interactive "p")
913   (gnus-group-mark-group n 'unmark)
914   (gnus-group-position-point))
915
916 (defun gnus-group-unmark-all-groups ()
917   "Unmark all groups."
918   (interactive)
919   (let ((groups gnus-group-marked))
920     (save-excursion
921       (while groups
922         (gnus-group-remove-mark (pop groups)))))
923   (gnus-group-position-point))
924
925 (defun gnus-group-mark-region (unmark beg end)
926   "Mark all groups between point and mark.
927 If UNMARK, remove the mark instead."
928   (interactive "P\nr")
929   (let ((num (count-lines beg end)))
930     (save-excursion
931       (goto-char beg)
932       (- num (gnus-group-mark-group num unmark)))))
933
934 (defun gnus-group-mark-buffer (&optional unmark)
935   "Mark all groups in the buffer.
936 If UNMARK, remove the mark instead."
937   (interactive "P")
938   (gnus-group-mark-region unmark (point-min) (point-max)))
939
940 (defun gnus-group-mark-regexp (regexp)
941   "Mark all groups that match some regexp."
942   (interactive "sMark (regexp): ")
943   (let ((alist (cdr gnus-newsrc-alist))
944         group)
945     (while alist
946       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
947         (gnus-group-set-mark group))))
948   (gnus-group-position-point))
949
950 (defun gnus-group-remove-mark (group)
951   "Remove the process mark from GROUP and move point there.
952 Return nil if the group isn't displayed."
953   (if (gnus-group-goto-group group)
954       (save-excursion
955         (gnus-group-mark-group 1 'unmark t)
956         t)
957     (setq gnus-group-marked
958           (delete group gnus-group-marked))
959     nil))
960
961 (defun gnus-group-set-mark (group)
962   "Set the process mark on GROUP."
963   (if (gnus-group-goto-group group) 
964       (save-excursion
965         (gnus-group-mark-group 1 nil t))
966     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
967
968 (defun gnus-group-universal-argument (arg &optional groups func)
969   "Perform any command on all groups accoring to the process/prefix convention."
970   (interactive "P")
971   (let ((groups (or groups (gnus-group-process-prefix arg)))
972         group func)
973     (if (eq (setq func (or func
974                            (key-binding
975                             (read-key-sequence
976                              (substitute-command-keys
977                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
978             'undefined)
979         (gnus-error 1 "Undefined key")
980       (while groups
981         (gnus-group-remove-mark (setq group (pop groups)))
982         (command-execute func))))
983   (gnus-group-position-point))
984
985 (defun gnus-group-process-prefix (n)
986   "Return a list of groups to work on.
987 Take into consideration N (the prefix) and the list of marked groups."
988   (cond
989    (n
990     (setq n (prefix-numeric-value n))
991     ;; There is a prefix, so we return a list of the N next
992     ;; groups.
993     (let ((way (if (< n 0) -1 1))
994           (n (abs n))
995           group groups)
996       (save-excursion
997         (while (and (> n 0)
998                     (setq group (gnus-group-group-name)))
999           (setq groups (cons group groups))
1000           (setq n (1- n))
1001           (gnus-group-next-group way)))
1002       (nreverse groups)))
1003    ((and (boundp 'transient-mark-mode)
1004          transient-mark-mode
1005          (boundp 'mark-active)
1006          mark-active)
1007     ;; Work on the region between point and mark.
1008     (let ((max (max (point) (mark)))
1009           groups)
1010       (save-excursion
1011         (goto-char (min (point) (mark)))
1012         (while
1013             (and
1014              (push (gnus-group-group-name) groups)
1015              (zerop (gnus-group-next-group 1))
1016              (< (point) max)))
1017         (nreverse groups))))
1018    (gnus-group-marked
1019     ;; No prefix, but a list of marked articles.
1020     (reverse gnus-group-marked))
1021    (t
1022     ;; Neither marked articles or a prefix, so we return the
1023     ;; current group.
1024     (let ((group (gnus-group-group-name)))
1025       (and group (list group))))))
1026
1027 ;; Selecting groups.
1028
1029 (defun gnus-group-read-group (&optional all no-article group)
1030   "Read news in this newsgroup.
1031 If the prefix argument ALL is non-nil, already read articles become
1032 readable.  IF ALL is a number, fetch this number of articles.  If the
1033 optional argument NO-ARTICLE is non-nil, no article will be
1034 auto-selected upon group entry.  If GROUP is non-nil, fetch that
1035 group."
1036   (interactive "P")
1037   (let ((group (or group (gnus-group-group-name)))
1038         number active marked entry)
1039     (or group (error "No group on current line"))
1040     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
1041                                             group gnus-newsrc-hashtb)))))
1042     ;; This group might be a dead group.  In that case we have to get
1043     ;; the number of unread articles from `gnus-active-hashtb'.
1044     (setq number
1045           (cond ((numberp all) all)
1046                 (entry (car entry))
1047                 ((setq active (gnus-active group))
1048                  (- (1+ (cdr active)) (car active)))))
1049     (gnus-summary-read-group
1050      group (or all (and (numberp number)
1051                         (zerop (+ number (gnus-range-length 
1052                                           (cdr (assq 'tick marked)))
1053                                   (gnus-range-length
1054                                    (cdr (assq 'dormant marked)))))))
1055      no-article)))
1056
1057 (defun gnus-group-select-group (&optional all)
1058   "Select this newsgroup.
1059 No article is selected automatically.
1060 If ALL is non-nil, already read articles become readable.
1061 If ALL is a number, fetch this number of articles."
1062   (interactive "P")
1063   (gnus-group-read-group all t))
1064
1065 (defun gnus-group-quick-select-group (&optional all)
1066   "Select the current group \"quickly\".
1067 This means that no highlighting or scoring will be performed."
1068   (interactive "P")
1069   (let (gnus-visual
1070         gnus-score-find-score-files-function
1071         gnus-apply-kill-hook
1072         gnus-summary-expunge-below)
1073     (gnus-group-read-group all t)))
1074
1075 (defun gnus-group-visible-select-group (&optional all)
1076   "Select the current group without hiding any articles."
1077   (interactive "P")
1078   (let ((gnus-inhibit-limiting t))
1079     (gnus-group-read-group all t)))
1080
1081 ;;;###autoload
1082 (defun gnus-fetch-group (group)
1083   "Start Gnus if necessary and enter GROUP.
1084 Returns whether the fetching was successful or not."
1085   (interactive "sGroup name: ")
1086   (or (get-buffer gnus-group-buffer)
1087       (gnus))
1088   (gnus-group-read-group nil nil group))
1089
1090 ;; Enter a group that is not in the group buffer.  Non-nil is returned
1091 ;; if selection was successful.
1092 (defun gnus-group-read-ephemeral-group
1093   (group method &optional activate quit-config)
1094   (let ((group (if (gnus-group-foreign-p group) group
1095                  (gnus-group-prefixed-name group method))))
1096     (gnus-sethash
1097      group
1098      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
1099                      ((quit-config . ,(if quit-config quit-config
1100                                         (cons (current-buffer) 'summary))))))
1101      gnus-newsrc-hashtb)
1102     (set-buffer gnus-group-buffer)
1103     (unless (gnus-check-server method)
1104       (error "Unable to contact server: %s" (gnus-status-message method)))
1105     (if activate (or (gnus-request-group group)
1106                      (error "Couldn't request group")))
1107     (condition-case ()
1108         (gnus-group-read-group t t group)
1109       ;(error nil)
1110       (quit nil))))
1111
1112 (defun gnus-group-jump-to-group (group)
1113   "Jump to newsgroup GROUP."
1114   (interactive
1115    (list (completing-read
1116           "Group: " gnus-active-hashtb nil
1117           (gnus-read-active-file-p)
1118           nil
1119           'gnus-group-history)))
1120
1121   (when (equal group "")
1122     (error "Empty group name"))
1123
1124   (when (string-match "[\000-\032]" group)
1125     (error "Control characters in group: %s" group))
1126
1127   (let ((b (text-property-any
1128             (point-min) (point-max)
1129             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
1130     (unless (gnus-ephemeral-group-p group)
1131       (if b
1132           ;; Either go to the line in the group buffer...
1133           (goto-char b)
1134         ;; ... or insert the line.
1135         (or
1136          t ;; Don't activate group.
1137          (gnus-active group)
1138          (gnus-activate-group group)
1139          (error "%s error: %s" group (gnus-status-message group)))
1140
1141         (gnus-group-update-group group)
1142         (goto-char (text-property-any
1143                     (point-min) (point-max)
1144                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
1145     ;; Adjust cursor point.
1146     (gnus-group-position-point)))
1147
1148 (defun gnus-group-goto-group (group)
1149   "Goto to newsgroup GROUP."
1150   (when group
1151     (beginning-of-line)
1152     (cond
1153      ;; It's quite likely that we are on the right line, so
1154      ;; we check the current line first.
1155      ((eq (get-text-property (point) 'gnus-group)
1156           (gnus-intern-safe group gnus-active-hashtb))
1157       (point))
1158      ;; Previous and next line are also likely, so we check them as well.
1159      ((save-excursion
1160         (forward-line -1)
1161         (eq (get-text-property (point) 'gnus-group)
1162             (gnus-intern-safe group gnus-active-hashtb)))
1163       (forward-line -1)
1164       (point))
1165      ((save-excursion
1166         (forward-line 1)
1167         (eq (get-text-property (point) 'gnus-group)
1168             (gnus-intern-safe group gnus-active-hashtb)))
1169       (forward-line 1)
1170       (point))
1171      (t
1172       ;; Search through the entire buffer.
1173       (gnus-goto-char
1174        (text-property-any 
1175         (point-min) (point-max)
1176         'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))))
1177
1178 (defun gnus-group-next-group (n &optional silent)
1179   "Go to next N'th newsgroup.
1180 If N is negative, search backward instead.
1181 Returns the difference between N and the number of skips actually
1182 done."
1183   (interactive "p")
1184   (gnus-group-next-unread-group n t nil silent))
1185
1186 (defun gnus-group-next-unread-group (n &optional all level silent)
1187   "Go to next N'th unread newsgroup.
1188 If N is negative, search backward instead.
1189 If ALL is non-nil, choose any newsgroup, unread or not.
1190 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
1191 such group can be found, the next group with a level higher than
1192 LEVEL.
1193 Returns the difference between N and the number of skips actually
1194 made."
1195   (interactive "p")
1196   (let ((backward (< n 0))
1197         (n (abs n)))
1198     (while (and (> n 0)
1199                 (gnus-group-search-forward
1200                  backward (or (not gnus-group-goto-unread) all) level))
1201       (setq n (1- n)))
1202     (when (and (/= 0 n)
1203                (not silent))
1204       (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
1205                     (if level " on this level or higher" "")))
1206     n))
1207
1208 (defun gnus-group-prev-group (n)
1209   "Go to previous N'th newsgroup.
1210 Returns the difference between N and the number of skips actually
1211 done."
1212   (interactive "p")
1213   (gnus-group-next-unread-group (- n) t))
1214
1215 (defun gnus-group-prev-unread-group (n)
1216   "Go to previous N'th unread newsgroup.
1217 Returns the difference between N and the number of skips actually
1218 done."
1219   (interactive "p")
1220   (gnus-group-next-unread-group (- n)))
1221
1222 (defun gnus-group-next-unread-group-same-level (n)
1223   "Go to next N'th unread newsgroup on the same level.
1224 If N is negative, search backward instead.
1225 Returns the difference between N and the number of skips actually
1226 done."
1227   (interactive "p")
1228   (gnus-group-next-unread-group n t (gnus-group-group-level))
1229   (gnus-group-position-point))
1230
1231 (defun gnus-group-prev-unread-group-same-level (n)
1232   "Go to next N'th unread newsgroup on the same level.
1233 Returns the difference between N and the number of skips actually
1234 done."
1235   (interactive "p")
1236   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
1237   (gnus-group-position-point))
1238
1239 (defun gnus-group-best-unread-group (&optional exclude-group)
1240   "Go to the group with the highest level.
1241 If EXCLUDE-GROUP, do not go to that group."
1242   (interactive)
1243   (goto-char (point-min))
1244   (let ((best 100000)
1245         unread best-point)
1246     (while (not (eobp))
1247       (setq unread (get-text-property (point) 'gnus-unread))
1248       (if (and (numberp unread) (> unread 0))
1249           (progn
1250             (if (and (get-text-property (point) 'gnus-level)
1251                      (< (get-text-property (point) 'gnus-level) best)
1252                      (or (not exclude-group)
1253                          (not (equal exclude-group (gnus-group-group-name)))))
1254                 (progn
1255                   (setq best (get-text-property (point) 'gnus-level))
1256                   (setq best-point (point))))))
1257       (forward-line 1))
1258     (if best-point (goto-char best-point))
1259     (gnus-summary-position-point)
1260     (and best-point (gnus-group-group-name))))
1261
1262 (defun gnus-group-first-unread-group ()
1263   "Go to the first group with unread articles."
1264   (interactive)
1265   (prog1
1266       (let ((opoint (point))
1267             unread)
1268         (goto-char (point-min))
1269         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
1270                 (and (numberp unread)   ; Not a topic.
1271                      (not (zerop unread))) ; Has unread articles.
1272                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
1273             (point)                     ; Success.
1274           (goto-char opoint)
1275           nil))                         ; Not success.
1276     (gnus-group-position-point)))
1277
1278 (defun gnus-group-enter-server-mode ()
1279   "Jump to the server buffer."
1280   (interactive)
1281   (gnus-enter-server-buffer))
1282
1283 (defun gnus-group-make-group (name &optional method address)
1284   "Add a new newsgroup.
1285 The user will be prompted for a NAME, for a select METHOD, and an
1286 ADDRESS."
1287   (interactive
1288    (list
1289     (read-string "Group name: ")
1290     (gnus-read-method "From method: ")))
1291
1292   (let* ((meth (when (and method
1293                           (not (gnus-server-equal method gnus-select-method)))
1294                  (if address (list (intern method) address)
1295                    method)))
1296          (nname (if method (gnus-group-prefixed-name name meth) name))
1297          backend info)
1298     (when (gnus-gethash nname gnus-newsrc-hashtb)
1299       (error "Group %s already exists" nname))
1300     ;; Subscribe to the new group.
1301     (gnus-group-change-level
1302      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
1303      gnus-level-default-subscribed gnus-level-killed
1304      (and (gnus-group-group-name)
1305           (gnus-gethash (gnus-group-group-name)
1306                         gnus-newsrc-hashtb))
1307      t)
1308     ;; Make it active.
1309     (gnus-set-active nname (cons 1 0))
1310     (or (gnus-ephemeral-group-p name)
1311         (gnus-dribble-enter
1312          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
1313     ;; Insert the line.
1314     (gnus-group-insert-group-line-info nname)
1315     (forward-line -1)
1316     (gnus-group-position-point)
1317
1318     ;; Load the backend and try to make the backend create
1319     ;; the group as well.
1320     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
1321                                                   nil meth))))
1322                  gnus-valid-select-methods)
1323       (require backend))
1324     (gnus-check-server meth)
1325     (and (gnus-check-backend-function 'request-create-group nname)
1326          (gnus-request-create-group nname))
1327     t))
1328
1329 (defun gnus-group-delete-group (group &optional force)
1330   "Delete the current group.  Only meaningful with mail groups.
1331 If FORCE (the prefix) is non-nil, all the articles in the group will
1332 be deleted.  This is \"deleted\" as in \"removed forever from the face
1333 of the Earth\".  There is no undo.  The user will be prompted before
1334 doing the deletion."
1335   (interactive
1336    (list (gnus-group-group-name)
1337          current-prefix-arg))
1338   (or group (error "No group to rename"))
1339   (or (gnus-check-backend-function 'request-delete-group group)
1340       (error "This backend does not support group deletion"))
1341   (prog1
1342       (if (not (gnus-yes-or-no-p
1343                 (format
1344                  "Do you really want to delete %s%s? "
1345                  group (if force " and all its contents" ""))))
1346           () ; Whew!
1347         (gnus-message 6 "Deleting group %s..." group)
1348         (if (not (gnus-request-delete-group group force))
1349             (gnus-error 3 "Couldn't delete group %s" group)
1350           (gnus-message 6 "Deleting group %s...done" group)
1351           (gnus-group-goto-group group)
1352           (gnus-group-kill-group 1 t)
1353           (gnus-sethash group nil gnus-active-hashtb)
1354           t))
1355     (gnus-group-position-point)))
1356
1357 (defun gnus-group-rename-group (group new-name)
1358   "Rename group from GROUP to NEW-NAME.
1359 When used interactively, GROUP is the group under point
1360 and NEW-NAME will be prompted for."
1361   (interactive
1362    (list
1363     (gnus-group-group-name)
1364     (progn
1365       (unless (gnus-check-backend-function
1366                'request-rename-group (gnus-group-group-name))
1367         (error "This backend does not support renaming groups"))
1368       (read-string "New group name: " (gnus-group-group-name)))))
1369
1370   (unless (gnus-check-backend-function 'request-rename-group group)
1371     (error "This backend does not support renaming groups"))
1372   (unless group 
1373     (error "No group to rename"))
1374   (when (string-match "^[ \t]*$" new-name)
1375     (error "Not a valid group name"))
1376   (when (equal group new-name)
1377     (error "Can't rename to the same name"))
1378
1379   ;; We find the proper prefixed name.
1380   (setq new-name
1381         (if (equal (gnus-group-real-name new-name) new-name)
1382             ;; Native group.
1383             new-name
1384           ;; Foreign group.
1385           (gnus-group-prefixed-name
1386            (gnus-group-real-name new-name)
1387            (gnus-info-method (gnus-get-info group)))))
1388
1389   (gnus-message 6 "Renaming group %s to %s..." group new-name)
1390   (prog1
1391       (if (not (gnus-request-rename-group group new-name))
1392           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
1393         ;; We rename the group internally by killing it...
1394         (gnus-group-goto-group group)
1395         (gnus-group-kill-group)
1396         ;; ... changing its name ...
1397         (setcar (cdar gnus-list-of-killed-groups) new-name)
1398         ;; ... and then yanking it.  Magic!
1399         (gnus-group-yank-group)
1400         (gnus-set-active new-name (gnus-active group))
1401         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
1402         new-name)
1403     (gnus-group-position-point)))
1404
1405 (defun gnus-group-edit-group (group &optional part)
1406   "Edit the group on the current line."
1407   (interactive (list (gnus-group-group-name)))
1408   (let ((part (or part 'info))
1409         info)
1410     (unless group
1411       (error "No group on current line"))
1412     (unless (setq info (gnus-get-info group))
1413       (error "Killed group; can't be edited"))
1414     (gnus-edit-form
1415      ;; Find the proper form to edit.
1416      (cond ((eq part 'method)
1417             (or (gnus-info-method info) "native"))
1418            ((eq part 'params)
1419             (gnus-info-params info))
1420            (t info))
1421      ;; The proper documentation.
1422      (format
1423       "Editing the %s."
1424       (cond
1425        ((eq part 'method) "select method")
1426        ((eq part 'params) "group parameters")
1427        (t "group info")))
1428      `(lambda (form)
1429         (gnus-group-edit-group-done ',part ,group form)))))
1430
1431 (defun gnus-group-edit-group-method (group)
1432   "Edit the select method of GROUP."
1433   (interactive (list (gnus-group-group-name)))
1434   (gnus-group-edit-group group 'method))
1435
1436 (defun gnus-group-edit-group-parameters (group)
1437   "Edit the group parameters of GROUP."
1438   (interactive (list (gnus-group-group-name)))
1439   (gnus-group-edit-group group 'params))
1440
1441 (defun gnus-group-edit-group-done (part group form)
1442   "Update variables."
1443   (let* ((method (cond ((eq part 'info) (nth 4 form))
1444                        ((eq part 'method) form)
1445                        (t nil)))
1446          (info (cond ((eq part 'info) form)
1447                      ((eq part 'method) (gnus-get-info group))
1448                      (t nil)))
1449          (new-group (if info
1450                         (if (or (not method)
1451                                 (gnus-server-equal
1452                                  gnus-select-method method))
1453                             (gnus-group-real-name (car info))
1454                           (gnus-group-prefixed-name
1455                            (gnus-group-real-name (car info)) method))
1456                       nil)))
1457     (when (and new-group
1458                (not (equal new-group group)))
1459       (when (gnus-group-goto-group group)
1460         (gnus-group-kill-group 1))
1461       (gnus-activate-group new-group))
1462     ;; Set the info.
1463     (if (not (and info new-group))
1464         (gnus-group-set-info form (or new-group group) part)
1465       (setq info (gnus-copy-sequence info))
1466       (setcar info new-group)
1467       (unless (gnus-server-equal method "native")
1468         (unless (nthcdr 3 info)
1469           (nconc info (list nil nil)))
1470         (unless (nthcdr 4 info)
1471           (nconc info (list nil)))
1472         (gnus-info-set-method info method))
1473       (gnus-group-set-info info))
1474     (gnus-group-update-group (or new-group group))
1475     (gnus-group-position-point)))
1476
1477 (defun gnus-group-make-help-group ()
1478   "Create the Gnus documentation group."
1479   (interactive)
1480   (let ((path load-path)
1481         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
1482         file dir)
1483     (and (gnus-gethash name gnus-newsrc-hashtb)
1484          (error "Documentation group already exists"))
1485     (while path
1486       (setq dir (file-name-as-directory (expand-file-name (pop path)))
1487             file nil)
1488       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
1489                 (file-exists-p
1490                  (setq file (concat (file-name-directory
1491                                      (directory-file-name dir))
1492                                     "etc/gnus-tut.txt"))))
1493         (setq path nil)))
1494     (if (not file)
1495         (gnus-message 1 "Couldn't find doc group")
1496       (gnus-group-make-group
1497        (gnus-group-real-name name)
1498        (list 'nndoc "gnus-help"
1499              (list 'nndoc-address file)
1500              (list 'nndoc-article-type 'mbox)))))
1501   (gnus-group-position-point))
1502
1503 (defun gnus-group-make-doc-group (file type)
1504   "Create a group that uses a single file as the source."
1505   (interactive
1506    (list (read-file-name "File name: ")
1507          (and current-prefix-arg 'ask)))
1508   (when (eq type 'ask)
1509     (let ((err "")
1510           char found)
1511       (while (not found)
1512         (message
1513          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
1514          err)
1515         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
1516                           ((= char ?b) 'babyl)
1517                           ((= char ?d) 'digest)
1518                           ((= char ?f) 'forward)
1519                           ((= char ?a) 'mmfd)
1520                           (t (setq err (format "%c unknown. " char))
1521                              nil))))
1522       (setq type found)))
1523   (let* ((file (expand-file-name file))
1524          (name (gnus-generate-new-group-name
1525                 (gnus-group-prefixed-name
1526                  (file-name-nondirectory file) '(nndoc "")))))
1527     (gnus-group-make-group
1528      (gnus-group-real-name name)
1529      (list 'nndoc file
1530            (list 'nndoc-address file)
1531            (list 'nndoc-article-type (or type 'guess))))))
1532
1533 (defun gnus-group-make-archive-group (&optional all)
1534   "Create the (ding) Gnus archive group of the most recent articles.
1535 Given a prefix, create a full group."
1536   (interactive "P")
1537   (let ((group (gnus-group-prefixed-name
1538                 (if all "ding.archives" "ding.recent") '(nndir ""))))
1539     (when (gnus-gethash group gnus-newsrc-hashtb)
1540       (error "Archive group already exists"))
1541     (gnus-group-make-group
1542      (gnus-group-real-name group)
1543      (list 'nndir (if all "hpc" "edu")
1544            (list 'nndir-directory
1545                  (if all gnus-group-archive-directory
1546                    gnus-group-recent-archive-directory))))
1547     (gnus-group-add-parameter group (cons 'to-address "ding@ifi.uio.no"))))
1548
1549 (defun gnus-group-make-directory-group (dir)
1550   "Create an nndir group.
1551 The user will be prompted for a directory.  The contents of this
1552 directory will be used as a newsgroup.  The directory should contain
1553 mail messages or news articles in files that have numeric names."
1554   (interactive
1555    (list (read-file-name "Create group from directory: ")))
1556   (or (file-exists-p dir) (error "No such directory"))
1557   (or (file-directory-p dir) (error "Not a directory"))
1558   (let ((ext "")
1559         (i 0)
1560         group)
1561     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
1562       (setq group
1563             (gnus-group-prefixed-name
1564              (concat (file-name-as-directory (directory-file-name dir))
1565                      ext)
1566              '(nndir "")))
1567       (setq ext (format "<%d>" (setq i (1+ i)))))
1568     (gnus-group-make-group
1569      (gnus-group-real-name group)
1570      (list 'nndir (gnus-group-real-name group) (list 'nndir-directory dir)))))
1571
1572 (defun gnus-group-make-kiboze-group (group address scores)
1573   "Create an nnkiboze group.
1574 The user will be prompted for a name, a regexp to match groups, and
1575 score file entries for articles to include in the group."
1576   (interactive
1577    (list
1578     (read-string "nnkiboze group name: ")
1579     (read-string "Source groups (regexp): ")
1580     (let ((headers (mapcar (lambda (group) (list group))
1581                            '("subject" "from" "number" "date" "message-id"
1582                              "references" "chars" "lines" "xref"
1583                              "followup" "all" "body" "head")))
1584           scores header regexp regexps)
1585       (while (not (equal "" (setq header (completing-read
1586                                           "Match on header: " headers nil t))))
1587         (setq regexps nil)
1588         (while (not (equal "" (setq regexp (read-string
1589                                             (format "Match on %s (string): "
1590                                                     header)))))
1591           (setq regexps (cons (list regexp nil nil 'r) regexps)))
1592         (setq scores (cons (cons header regexps) scores)))
1593       scores)))
1594   (gnus-group-make-group group "nnkiboze" address)
1595   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
1596     (let (emacs-lisp-mode-hook)
1597       (pp scores (current-buffer)))))
1598
1599 (defun gnus-group-add-to-virtual (n vgroup)
1600   "Add the current group to a virtual group."
1601   (interactive
1602    (list current-prefix-arg
1603          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
1604                           "nnvirtual:")))
1605   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
1606       (error "%s is not an nnvirtual group" vgroup))
1607   (let* ((groups (gnus-group-process-prefix n))
1608          (method (gnus-info-method (gnus-get-info vgroup))))
1609     (setcar (cdr method)
1610             (concat
1611              (nth 1 method) "\\|"
1612              (mapconcat
1613               (lambda (s)
1614                 (gnus-group-remove-mark s)
1615                 (concat "\\(^" (regexp-quote s) "$\\)"))
1616               groups "\\|"))))
1617   (gnus-group-position-point))
1618
1619 (defun gnus-group-make-empty-virtual (group)
1620   "Create a new, fresh, empty virtual group."
1621   (interactive "sCreate new, empty virtual group: ")
1622   (let* ((method (list 'nnvirtual "^$"))
1623          (pgroup (gnus-group-prefixed-name group method)))
1624     ;; Check whether it exists already.
1625     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
1626          (error "Group %s already exists." pgroup))
1627     ;; Subscribe the new group after the group on the current line.
1628     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
1629     (gnus-group-update-group pgroup)
1630     (forward-line -1)
1631     (gnus-group-position-point)))
1632
1633 (defun gnus-group-enter-directory (dir)
1634   "Enter an ephemeral nneething group."
1635   (interactive "DDirectory to read: ")
1636   (let* ((method (list 'nneething dir))
1637          (leaf (gnus-group-prefixed-name
1638                 (file-name-nondirectory (directory-file-name dir))
1639                 method))
1640          (name (gnus-generate-new-group-name leaf)))
1641     (let ((nneething-read-only t))
1642       (or (gnus-group-read-ephemeral-group
1643            name method t
1644            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
1645                                       'summary 'group)))
1646           (error "Couldn't enter %s" dir)))))
1647
1648 ;; Group sorting commands
1649 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
1650
1651 (defun gnus-group-sort-groups (func &optional reverse)
1652   "Sort the group buffer according to FUNC.
1653 If REVERSE, reverse the sorting order."
1654   (interactive (list gnus-group-sort-function
1655                      current-prefix-arg))
1656   (let ((func (cond 
1657                ((not (listp func)) func)
1658                ((null func) func)
1659                ((= 1 (length func)) (car func))
1660                (t `(lambda (t1 t2)
1661                      ,(gnus-make-sort-function 
1662                        (reverse func)))))))
1663     ;; We peel off the dummy group from the alist.
1664     (when func
1665       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
1666         (pop gnus-newsrc-alist))
1667       ;; Do the sorting.
1668       (setq gnus-newsrc-alist
1669             (sort gnus-newsrc-alist func))
1670       (when reverse
1671         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
1672       ;; Regenerate the hash table.
1673       (gnus-make-hashtable-from-newsrc-alist)
1674       (gnus-group-list-groups))))
1675
1676 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
1677   "Sort the group buffer alphabetically by group name.
1678 If REVERSE, sort in reverse order."
1679   (interactive "P")
1680   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
1681
1682 (defun gnus-group-sort-groups-by-unread (&optional reverse)
1683   "Sort the group buffer by number of unread articles.
1684 If REVERSE, sort in reverse order."
1685   (interactive "P")
1686   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
1687
1688 (defun gnus-group-sort-groups-by-level (&optional reverse)
1689   "Sort the group buffer by group level.
1690 If REVERSE, sort in reverse order."
1691   (interactive "P")
1692   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
1693
1694 (defun gnus-group-sort-groups-by-score (&optional reverse)
1695   "Sort the group buffer by group score.
1696 If REVERSE, sort in reverse order."
1697   (interactive "P")
1698   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
1699
1700 (defun gnus-group-sort-groups-by-rank (&optional reverse)
1701   "Sort the group buffer by group rank.
1702 If REVERSE, sort in reverse order."
1703   (interactive "P")
1704   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
1705
1706 (defun gnus-group-sort-groups-by-method (&optional reverse)
1707   "Sort the group buffer alphabetically by backend name.
1708 If REVERSE, sort in reverse order."
1709   (interactive "P")
1710   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
1711
1712 (defun gnus-group-sort-by-alphabet (info1 info2)
1713   "Sort alphabetically."
1714   (string< (gnus-info-group info1) (gnus-info-group info2)))
1715
1716 (defun gnus-group-sort-by-unread (info1 info2)
1717   "Sort by number of unread articles."
1718   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
1719         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
1720     (< (or (and (numberp n1) n1) 0)
1721        (or (and (numberp n2) n2) 0))))
1722
1723 (defun gnus-group-sort-by-level (info1 info2)
1724   "Sort by level."
1725   (< (gnus-info-level info1) (gnus-info-level info2)))
1726
1727 (defun gnus-group-sort-by-method (info1 info2)
1728   "Sort alphabetically by backend name."
1729   (string< (symbol-name (car (gnus-find-method-for-group
1730                               (gnus-info-group info1) info1)))
1731            (symbol-name (car (gnus-find-method-for-group
1732                               (gnus-info-group info2) info2)))))
1733
1734 (defun gnus-group-sort-by-score (info1 info2)
1735   "Sort by group score."
1736   (< (gnus-info-score info1) (gnus-info-score info2)))
1737
1738 (defun gnus-group-sort-by-rank (info1 info2)
1739   "Sort by level and score."
1740   (let ((level1 (gnus-info-level info1))
1741         (level2 (gnus-info-level info2)))
1742     (or (< level1 level2)
1743         (and (= level1 level2)
1744              (> (gnus-info-score info1) (gnus-info-score info2))))))
1745
1746 ;; Group catching up.
1747
1748 (defun gnus-group-clear-data (n)
1749   "Clear all marks and read ranges from the current group."
1750   (interactive "P")
1751   (let ((groups (gnus-group-process-prefix n))
1752         group info)
1753     (while (setq group (pop groups))
1754       (setq info (gnus-get-info group))
1755       (gnus-info-set-read info nil)
1756       (when (gnus-info-marks info)
1757         (gnus-info-set-marks info nil))
1758       (gnus-get-unread-articles-in-group info (gnus-active group) t)
1759       (when (gnus-group-goto-group group)
1760         (gnus-group-remove-mark group)
1761         (gnus-group-update-group-line)))))
1762
1763 (defun gnus-group-catchup-current (&optional n all)
1764   "Mark all articles not marked as unread in current newsgroup as read.
1765 If prefix argument N is numeric, the ARG next newsgroups will be
1766 caught up.  If ALL is non-nil, marked articles will also be marked as
1767 read.  Cross references (Xref: header) of articles are ignored.
1768 The difference between N and actual number of newsgroups that were
1769 caught up is returned."
1770   (interactive "P")
1771   (unless (gnus-group-group-name)
1772     (error "No group on the current line"))
1773   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
1774                gnus-expert-user
1775                (gnus-y-or-n-p
1776                 (if all
1777                     "Do you really want to mark all articles as read? "
1778                   "Mark all unread articles as read? "))))
1779       n
1780     (let ((groups (gnus-group-process-prefix n))
1781           (ret 0))
1782       (while groups
1783         ;; Virtual groups have to be given special treatment.
1784         (let ((method (gnus-find-method-for-group (car groups))))
1785           (if (eq 'nnvirtual (car method))
1786               (nnvirtual-catchup-group
1787                (gnus-group-real-name (car groups)) (nth 1 method) all)))
1788         (gnus-group-remove-mark (car groups))
1789         (if (>= (gnus-group-group-level) gnus-level-zombie)
1790             (gnus-message 2 "Dead groups can't be caught up")
1791           (if (prog1
1792                   (gnus-group-goto-group (car groups))
1793                 (gnus-group-catchup (car groups) all))
1794               (gnus-group-update-group-line)
1795             (setq ret (1+ ret))))
1796         (setq groups (cdr groups)))
1797       (gnus-group-next-unread-group 1)
1798       ret)))
1799
1800 (defun gnus-group-catchup-current-all (&optional n)
1801   "Mark all articles in current newsgroup as read.
1802 Cross references (Xref: header) of articles are ignored."
1803   (interactive "P")
1804   (gnus-group-catchup-current n 'all))
1805
1806 (defun gnus-group-catchup (group &optional all)
1807   "Mark all articles in GROUP as read.
1808 If ALL is non-nil, all articles are marked as read.
1809 The return value is the number of articles that were marked as read,
1810 or nil if no action could be taken."
1811   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
1812          (num (car entry)))
1813     ;; Do the updating only if the newsgroup isn't killed.
1814     (if (not (numberp (car entry)))
1815         (gnus-message 1 "Can't catch up; non-active group")
1816       ;; Do auto-expirable marks if that's required.
1817       (when (gnus-group-auto-expirable-p group)
1818         (gnus-add-marked-articles
1819          group 'expire (gnus-list-of-unread-articles group))
1820         (when all
1821           (let ((marks (nth 3 (nth 2 entry))))
1822             (gnus-add-marked-articles
1823              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
1824             (gnus-add-marked-articles
1825              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
1826       (when entry
1827         (gnus-update-read-articles group nil)
1828         ;; Also nix out the lists of marks and dormants.
1829         (when all
1830           (gnus-add-marked-articles group 'tick nil nil 'force)
1831           (gnus-add-marked-articles group 'dormant nil nil 'force))
1832         (run-hooks 'gnus-group-catchup-group-hook)
1833         num))))
1834
1835 (defun gnus-group-expire-articles (&optional n)
1836   "Expire all expirable articles in the current newsgroup."
1837   (interactive "P")
1838   (let ((groups (gnus-group-process-prefix n))
1839         group)
1840     (unless groups
1841       (error "No groups to expire"))
1842     (while (setq group (pop groups))
1843       (gnus-group-remove-mark group)
1844       (when (gnus-check-backend-function 'request-expire-articles group)
1845         (gnus-message 6 "Expiring articles in %s..." group)
1846         (let* ((info (gnus-get-info group))
1847                (expirable (if (gnus-group-total-expirable-p group)
1848                               (cons nil (gnus-list-of-read-articles group))
1849                             (assq 'expire (gnus-info-marks info))))
1850                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
1851           (when expirable
1852             (setcdr
1853              expirable
1854              (gnus-compress-sequence
1855               (if expiry-wait
1856                   ;; We set the expiry variables to the groupp
1857                   ;; parameter. 
1858                   (let ((nnmail-expiry-wait-function nil)
1859                         (nnmail-expiry-wait expiry-wait))
1860                     (gnus-request-expire-articles
1861                      (gnus-uncompress-sequence (cdr expirable)) group))
1862                 ;; Just expire using the normal expiry values.
1863                 (gnus-request-expire-articles
1864                  (gnus-uncompress-sequence (cdr expirable)) group))))
1865             (gnus-close-group group))
1866           (gnus-message 6 "Expiring articles in %s...done" group)))
1867       (gnus-group-position-point))))
1868
1869 (defun gnus-group-expire-all-groups ()
1870   "Expire all expirable articles in all newsgroups."
1871   (interactive)
1872   (save-excursion
1873     (gnus-message 5 "Expiring...")
1874     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
1875                                      (cdr gnus-newsrc-alist))))
1876       (gnus-group-expire-articles nil)))
1877   (gnus-group-position-point)
1878   (gnus-message 5 "Expiring...done"))
1879
1880 (defun gnus-group-set-current-level (n level)
1881   "Set the level of the next N groups to LEVEL."
1882   (interactive
1883    (list
1884     current-prefix-arg
1885     (string-to-int
1886      (let ((s (read-string
1887                (format "Level (default %s): "
1888                        (or (gnus-group-group-level) 
1889                            gnus-level-default-subscribed)))))
1890        (if (string-match "^\\s-*$" s)
1891            (int-to-string (or (gnus-group-group-level) 
1892                               gnus-level-default-subscribed))
1893          s)))))
1894   (or (and (>= level 1) (<= level gnus-level-killed))
1895       (error "Illegal level: %d" level))
1896   (let ((groups (gnus-group-process-prefix n))
1897         group)
1898     (while (setq group (pop groups))
1899       (gnus-group-remove-mark group)
1900       (gnus-message 6 "Changed level of %s from %d to %d"
1901                     group (or (gnus-group-group-level) gnus-level-killed)
1902                     level)
1903       (gnus-group-change-level
1904        group level (or (gnus-group-group-level) gnus-level-killed))
1905       (gnus-group-update-group-line)))
1906   (gnus-group-position-point))
1907
1908 (defun gnus-group-unsubscribe-current-group (&optional n)
1909   "Toggle subscription of the current group.
1910 If given numerical prefix, toggle the N next groups."
1911   (interactive "P")
1912   (let ((groups (gnus-group-process-prefix n))
1913         group)
1914     (while groups
1915       (setq group (car groups)
1916             groups (cdr groups))
1917       (gnus-group-remove-mark group)
1918       (gnus-group-unsubscribe-group
1919        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
1920                  gnus-level-default-unsubscribed
1921                gnus-level-default-subscribed) t)
1922       (gnus-group-update-group-line))
1923     (gnus-group-next-group 1)))
1924
1925 (defun gnus-group-unsubscribe-group (group &optional level silent)
1926   "Toggle subscription to GROUP.
1927 Killed newsgroups are subscribed.  If SILENT, don't try to update the
1928 group line."
1929   (interactive
1930    (list (completing-read
1931           "Group: " gnus-active-hashtb nil
1932           (gnus-read-active-file-p)
1933           nil 
1934           'gnus-group-history)))
1935   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
1936     (cond
1937      ((string-match "^[ \t]$" group)
1938       (error "Empty group name"))
1939      (newsrc
1940       ;; Toggle subscription flag.
1941       (gnus-group-change-level
1942        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
1943                                       gnus-level-subscribed)
1944                                   (1+ gnus-level-subscribed)
1945                                 gnus-level-default-subscribed)))
1946       (unless silent
1947         (gnus-group-update-group group)))
1948      ((and (stringp group)
1949            (or (not (gnus-read-active-file-p))
1950                (gnus-active group)))
1951       ;; Add new newsgroup.
1952       (gnus-group-change-level
1953        group
1954        (if level level gnus-level-default-subscribed)
1955        (or (and (member group gnus-zombie-list)
1956                 gnus-level-zombie)
1957            gnus-level-killed)
1958        (and (gnus-group-group-name)
1959             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
1960       (unless silent
1961         (gnus-group-update-group group)))
1962      (t (error "No such newsgroup: %s" group)))
1963     (gnus-group-position-point)))
1964
1965 (defun gnus-group-transpose-groups (n)
1966   "Move the current newsgroup up N places.
1967 If given a negative prefix, move down instead.  The difference between
1968 N and the number of steps taken is returned."
1969   (interactive "p")
1970   (or (gnus-group-group-name)
1971       (error "No group on current line"))
1972   (gnus-group-kill-group 1)
1973   (prog1
1974       (forward-line (- n))
1975     (gnus-group-yank-group)
1976     (gnus-group-position-point)))
1977
1978 (defun gnus-group-kill-all-zombies ()
1979   "Kill all zombie newsgroups."
1980   (interactive)
1981   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
1982   (setq gnus-zombie-list nil)
1983   (gnus-group-list-groups))
1984
1985 (defun gnus-group-kill-region (begin end)
1986   "Kill newsgroups in current region (excluding current point).
1987 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
1988   (interactive "r")
1989   (let ((lines
1990          ;; Count lines.
1991          (save-excursion
1992            (count-lines
1993             (progn
1994               (goto-char begin)
1995               (beginning-of-line)
1996               (point))
1997             (progn
1998               (goto-char end)
1999               (beginning-of-line)
2000               (point))))))
2001     (goto-char begin)
2002     (beginning-of-line)                 ;Important when LINES < 1
2003     (gnus-group-kill-group lines)))
2004
2005 (defun gnus-group-kill-group (&optional n discard)
2006   "Kill the next N groups.
2007 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
2008 However, only groups that were alive can be yanked; already killed
2009 groups or zombie groups can't be yanked.
2010 The return value is the name of the group that was killed, or a list
2011 of groups killed."
2012   (interactive "P")
2013   (let ((buffer-read-only nil)
2014         (groups (gnus-group-process-prefix n))
2015         group entry level out)
2016     (if (< (length groups) 10)
2017         ;; This is faster when there are few groups.
2018         (while groups
2019           (push (setq group (pop groups)) out)
2020           (gnus-group-remove-mark group)
2021           (setq level (gnus-group-group-level))
2022           (gnus-delete-line)
2023           (when (and (not discard)
2024                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
2025             (push (cons (car entry) (nth 2 entry))
2026                   gnus-list-of-killed-groups))
2027           (gnus-group-change-level
2028            (if entry entry group) gnus-level-killed (if entry nil level)))
2029       ;; If there are lots and lots of groups to be killed, we use
2030       ;; this thing instead.
2031       (let (entry)
2032         (setq groups (nreverse groups))
2033         (while groups
2034           (gnus-group-remove-mark (setq group (pop groups)))
2035           (gnus-delete-line)
2036           (push group gnus-killed-list)
2037           (setq gnus-newsrc-alist
2038                 (delq (assoc group gnus-newsrc-alist)
2039                       gnus-newsrc-alist))
2040           (when gnus-group-change-level-function
2041             (funcall gnus-group-change-level-function group 9 3))
2042           (cond
2043            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
2044             (push (cons (car entry) (nth 2 entry))
2045                   gnus-list-of-killed-groups)
2046             (setcdr (cdr entry) (cdddr entry)))
2047            ((member group gnus-zombie-list)
2048             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
2049         (gnus-make-hashtable-from-newsrc-alist)))
2050
2051     (gnus-group-position-point)
2052     (if (< (length out) 2) (car out) (nreverse out))))
2053
2054 (defun gnus-group-yank-group (&optional arg)
2055   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
2056 inserting it before the current newsgroup.  The numeric ARG specifies
2057 how many newsgroups are to be yanked.  The name of the newsgroup yanked
2058 is returned, or (if several groups are yanked) a list of yanked groups
2059 is returned."
2060   (interactive "p")
2061   (setq arg (or arg 1))
2062   (let (info group prev out)
2063     (while (>= (decf arg) 0)
2064       (if (not (setq info (pop gnus-list-of-killed-groups)))
2065           (error "No more newsgroups to yank"))
2066       (push (setq group (nth 1 info)) out)
2067       ;; Find which newsgroup to insert this one before - search
2068       ;; backward until something suitable is found.  If there are no
2069       ;; other newsgroups in this buffer, just make this newsgroup the
2070       ;; first newsgroup.
2071       (setq prev (gnus-group-group-name))
2072       (gnus-group-change-level
2073        info (gnus-info-level (cdr info)) gnus-level-killed
2074        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
2075        t)
2076       (gnus-group-insert-group-line-info group))
2077     (forward-line -1)
2078     (gnus-group-position-point)
2079     (if (< (length out) 2) (car out) (nreverse out))))
2080
2081 (defun gnus-group-kill-level (level)
2082   "Kill all groups that is on a certain LEVEL."
2083   (interactive "nKill all groups on level: ")
2084   (cond
2085    ((= level gnus-level-zombie)
2086     (setq gnus-killed-list
2087           (nconc gnus-zombie-list gnus-killed-list))
2088     (setq gnus-zombie-list nil))
2089    ((and (< level gnus-level-zombie)
2090          (> level 0)
2091          (or gnus-expert-user
2092              (gnus-yes-or-no-p
2093               (format
2094                "Do you really want to kill all groups on level %d? "
2095                level))))
2096     (let* ((prev gnus-newsrc-alist)
2097            (alist (cdr prev)))
2098       (while alist
2099         (if (= (gnus-info-level (car alist)) level)
2100             (progn
2101               (push (gnus-info-group (car alist)) gnus-killed-list)
2102               (setcdr prev (cdr alist)))
2103           (setq prev alist))
2104         (setq alist (cdr alist)))
2105       (gnus-make-hashtable-from-newsrc-alist)
2106       (gnus-group-list-groups)))
2107    (t
2108     (error "Can't kill; illegal level: %d" level))))
2109
2110 (defun gnus-group-list-all-groups (&optional arg)
2111   "List all newsgroups with level ARG or lower.
2112 Default is gnus-level-unsubscribed, which lists all subscribed and most
2113 unsubscribed groups."
2114   (interactive "P")
2115   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
2116
2117 ;; Redefine this to list ALL killed groups if prefix arg used.
2118 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
2119 (defun gnus-group-list-killed (&optional arg)
2120   "List all killed newsgroups in the group buffer.
2121 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
2122 entail asking the server for the groups."
2123   (interactive "P")
2124   ;; Find all possible killed newsgroups if arg.
2125   (when arg
2126     (gnus-get-killed-groups))
2127   (if (not gnus-killed-list)
2128       (gnus-message 6 "No killed groups")
2129     (let (gnus-group-list-mode)
2130       (funcall gnus-group-prepare-function
2131                gnus-level-killed t gnus-level-killed))
2132     (goto-char (point-min)))
2133   (gnus-group-position-point))
2134
2135 (defun gnus-group-list-zombies ()
2136   "List all zombie newsgroups in the group buffer."
2137   (interactive)
2138   (if (not gnus-zombie-list)
2139       (gnus-message 6 "No zombie groups")
2140     (let (gnus-group-list-mode)
2141       (funcall gnus-group-prepare-function
2142                gnus-level-zombie t gnus-level-zombie))
2143     (goto-char (point-min)))
2144   (gnus-group-position-point))
2145
2146 (defun gnus-group-list-active ()
2147   "List all groups that are available from the server(s)."
2148   (interactive)
2149   ;; First we make sure that we have really read the active file.
2150   (unless (gnus-read-active-file-p)
2151     (let ((gnus-read-active-file t))
2152       (gnus-read-active-file)))
2153   ;; Find all groups and sort them.
2154   (let ((groups
2155          (sort
2156           (let (list)
2157             (mapatoms
2158              (lambda (sym)
2159                (and (boundp sym)
2160                     (symbol-value sym)
2161                     (setq list (cons (symbol-name sym) list))))
2162              gnus-active-hashtb)
2163             list)
2164           'string<))
2165         (buffer-read-only nil))
2166     (erase-buffer)
2167     (while groups
2168       (gnus-group-insert-group-line-info (pop groups)))
2169     (goto-char (point-min))))
2170
2171 (defun gnus-activate-all-groups (level)
2172   "Activate absolutely all groups."
2173   (interactive (list 7))
2174   (let ((gnus-activate-level level)
2175         (gnus-activate-foreign-newsgroups level))
2176     (gnus-group-get-new-news)))
2177
2178 (defun gnus-group-get-new-news (&optional arg)
2179   "Get newly arrived articles.
2180 If ARG is a number, it specifies which levels you are interested in
2181 re-scanning.  If ARG is non-nil and not a number, this will force
2182 \"hard\" re-reading of the active files from all servers."
2183   (interactive "P")
2184   (run-hooks 'gnus-get-new-news-hook)
2185   ;; We might read in new NoCeM messages here.
2186   (when (and gnus-use-nocem 
2187              (null arg))
2188     (gnus-nocem-scan-groups))
2189   ;; If ARG is not a number, then we read the active file.
2190   (when (and arg (not (numberp arg)))
2191     (let ((gnus-read-active-file t))
2192       (gnus-read-active-file))
2193     (setq arg nil))
2194
2195   (setq arg (gnus-group-default-level arg t))
2196   (if (and gnus-read-active-file (not arg))
2197       (progn
2198         (gnus-read-active-file)
2199         (gnus-get-unread-articles arg))
2200     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
2201       (gnus-get-unread-articles arg)))
2202   (run-hooks 'gnus-after-getting-new-news-hook)
2203   (gnus-group-list-groups))
2204
2205 (defun gnus-group-get-new-news-this-group (&optional n)
2206   "Check for newly arrived news in the current group (and the N-1 next groups).
2207 The difference between N and the number of newsgroup checked is returned.
2208 If N is negative, this group and the N-1 previous groups will be checked."
2209   (interactive "P")
2210   (let* ((groups (gnus-group-process-prefix n))
2211          (ret (if (numberp n) (- n (length groups)) 0))
2212          (beg (unless n (point)))
2213          group)
2214     (while (setq group (pop groups))
2215       (gnus-group-remove-mark group)
2216       (if (gnus-activate-group group 'scan)
2217           (progn
2218             (gnus-get-unread-articles-in-group
2219              (gnus-get-info group) (gnus-active group) t)
2220             (unless (gnus-virtual-group-p group)
2221               (gnus-close-group group))
2222             (gnus-group-update-group group))
2223         (if (eq (gnus-server-status (gnus-find-method-for-group group))
2224                 'denied)
2225             (gnus-error "Server denied access")
2226           (gnus-error 3 "%s error: %s" group (gnus-status-message group)))))
2227     (when beg (goto-char beg))
2228     (when gnus-goto-next-group-when-activating
2229       (gnus-group-next-unread-group 1 t))
2230     (gnus-summary-position-point)
2231     ret))
2232
2233 (defun gnus-group-fetch-faq (group &optional faq-dir)
2234   "Fetch the FAQ for the current group."
2235   (interactive
2236    (list
2237     (and (gnus-group-group-name)
2238          (gnus-group-real-name (gnus-group-group-name)))
2239     (cond (current-prefix-arg
2240            (completing-read
2241             "Faq dir: " (and (listp gnus-group-faq-directory)
2242                              (mapcar (lambda (file) (list file))
2243                                      gnus-group-faq-directory)))))))
2244   (or faq-dir
2245       (setq faq-dir (if (listp gnus-group-faq-directory)
2246                         (car gnus-group-faq-directory)
2247                       gnus-group-faq-directory)))
2248   (or group (error "No group name given"))
2249   (let ((file (concat (file-name-as-directory faq-dir)
2250                       (gnus-group-real-name group))))
2251     (if (not (file-exists-p file))
2252         (error "No such file: %s" file)
2253       (find-file file))))
2254
2255 (defun gnus-group-describe-group (force &optional group)
2256   "Display a description of the current newsgroup."
2257   (interactive (list current-prefix-arg (gnus-group-group-name)))
2258   (let* ((method (gnus-find-method-for-group group))
2259          (mname (gnus-group-prefixed-name "" method))
2260          desc)
2261     (when (and force
2262                gnus-description-hashtb)
2263       (gnus-sethash mname nil gnus-description-hashtb))
2264     (or group (error "No group name given"))
2265     (and (or (and gnus-description-hashtb
2266                   ;; We check whether this group's method has been
2267                   ;; queried for a description file.
2268                   (gnus-gethash mname gnus-description-hashtb))
2269              (setq desc (gnus-group-get-description group))
2270              (gnus-read-descriptions-file method))
2271          (gnus-message 1
2272           (or desc (gnus-gethash group gnus-description-hashtb)
2273               "No description available")))))
2274
2275 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
2276 (defun gnus-group-describe-all-groups (&optional force)
2277   "Pop up a buffer with descriptions of all newsgroups."
2278   (interactive "P")
2279   (and force (setq gnus-description-hashtb nil))
2280   (if (not (or gnus-description-hashtb
2281                (gnus-read-all-descriptions-files)))
2282       (error "Couldn't request descriptions file"))
2283   (let ((buffer-read-only nil)
2284         b)
2285     (erase-buffer)
2286     (mapatoms
2287      (lambda (group)
2288        (setq b (point))
2289        (insert (format "      *: %-20s %s\n" (symbol-name group)
2290                        (symbol-value group)))
2291        (gnus-add-text-properties
2292         b (1+ b) (list 'gnus-group group
2293                        'gnus-unread t 'gnus-marked nil
2294                        'gnus-level (1+ gnus-level-subscribed))))
2295      gnus-description-hashtb)
2296     (goto-char (point-min))
2297     (gnus-group-position-point)))
2298
2299 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
2300 (defun gnus-group-apropos (regexp &optional search-description)
2301   "List all newsgroups that have names that match a regexp."
2302   (interactive "sGnus apropos (regexp): ")
2303   (let ((prev "")
2304         (obuf (current-buffer))
2305         groups des)
2306     ;; Go through all newsgroups that are known to Gnus.
2307     (mapatoms
2308      (lambda (group)
2309        (and (symbol-name group)
2310             (string-match regexp (symbol-name group))
2311             (setq groups (cons (symbol-name group) groups))))
2312      gnus-active-hashtb)
2313     ;; Also go through all descriptions that are known to Gnus.
2314     (when search-description
2315       (mapatoms
2316        (lambda (group)
2317          (and (string-match regexp (symbol-value group))
2318               (gnus-active (symbol-name group))
2319               (setq groups (cons (symbol-name group) groups))))
2320        gnus-description-hashtb))
2321     (if (not groups)
2322         (gnus-message 3 "No groups matched \"%s\"." regexp)
2323       ;; Print out all the groups.
2324       (save-excursion
2325         (pop-to-buffer "*Gnus Help*")
2326         (buffer-disable-undo (current-buffer))
2327         (erase-buffer)
2328         (setq groups (sort groups 'string<))
2329         (while groups
2330           ;; Groups may be entered twice into the list of groups.
2331           (if (not (string= (car groups) prev))
2332               (progn
2333                 (insert (setq prev (car groups)) "\n")
2334                 (if (and gnus-description-hashtb
2335                          (setq des (gnus-gethash (car groups)
2336                                                  gnus-description-hashtb)))
2337                     (insert "  " des "\n"))))
2338           (setq groups (cdr groups)))
2339         (goto-char (point-min))))
2340     (pop-to-buffer obuf)))
2341
2342 (defun gnus-group-description-apropos (regexp)
2343   "List all newsgroups that have names or descriptions that match a regexp."
2344   (interactive "sGnus description apropos (regexp): ")
2345   (if (not (or gnus-description-hashtb
2346                (gnus-read-all-descriptions-files)))
2347       (error "Couldn't request descriptions file"))
2348   (gnus-group-apropos regexp t))
2349
2350 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
2351 (defun gnus-group-list-matching (level regexp &optional all lowest)
2352   "List all groups with unread articles that match REGEXP.
2353 If the prefix LEVEL is non-nil, it should be a number that says which
2354 level to cut off listing groups.
2355 If ALL, also list groups with no unread articles.
2356 If LOWEST, don't list groups with level lower than LOWEST.
2357
2358 This command may read the active file."
2359   (interactive "P\nsList newsgroups matching: ")
2360   ;; First make sure active file has been read.
2361   (when (and level
2362              (> (prefix-numeric-value level) gnus-level-killed))
2363     (gnus-get-killed-groups))
2364   (gnus-group-prepare-flat (or level gnus-level-subscribed)
2365                            all (or lowest 1) regexp)
2366   (goto-char (point-min))
2367   (gnus-group-position-point))
2368
2369 (defun gnus-group-list-all-matching (level regexp &optional lowest)
2370   "List all groups that match REGEXP.
2371 If the prefix LEVEL is non-nil, it should be a number that says which
2372 level to cut off listing groups.
2373 If LOWEST, don't list groups with level lower than LOWEST."
2374   (interactive "P\nsList newsgroups matching: ")
2375   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
2376
2377 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
2378 (defun gnus-group-save-newsrc (&optional force)
2379   "Save the Gnus startup files.
2380 If FORCE, force saving whether it is necessary or not."
2381   (interactive "P")
2382   (gnus-save-newsrc-file force))
2383
2384 (defun gnus-group-restart (&optional arg)
2385   "Force Gnus to read the .newsrc file."
2386   (interactive "P")
2387   (when (gnus-yes-or-no-p
2388          (format "Are you sure you want to read %s? "
2389                  gnus-current-startup-file))
2390     (gnus-save-newsrc-file)
2391     (gnus-setup-news 'force)
2392     (gnus-group-list-groups arg)))
2393
2394 (defun gnus-group-read-init-file ()
2395   "Read the Gnus elisp init file."
2396   (interactive)
2397   (gnus-read-init-file))
2398
2399 (defun gnus-group-check-bogus-groups (&optional silent)
2400   "Check bogus newsgroups.
2401 If given a prefix, don't ask for confirmation before removing a bogus
2402 group."
2403   (interactive "P")
2404   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
2405   (gnus-group-list-groups))
2406
2407 (defun gnus-group-edit-global-kill (&optional article group)
2408   "Edit the global kill file.
2409 If GROUP, edit that local kill file instead."
2410   (interactive "P")
2411   (setq gnus-current-kill-article article)
2412   (gnus-kill-file-edit-file group)
2413   (gnus-message
2414    6
2415    (substitute-command-keys
2416     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
2417             (if group "local" "global")))))
2418
2419 (defun gnus-group-edit-local-kill (article group)
2420   "Edit a local kill file."
2421   (interactive (list nil (gnus-group-group-name)))
2422   (gnus-group-edit-global-kill article group))
2423
2424 (defun gnus-group-force-update ()
2425   "Update `.newsrc' file."
2426   (interactive)
2427   (gnus-save-newsrc-file))
2428
2429 (defun gnus-group-suspend ()
2430   "Suspend the current Gnus session.
2431 In fact, cleanup buffers except for group mode buffer.
2432 The hook gnus-suspend-gnus-hook is called before actually suspending."
2433   (interactive)
2434   (run-hooks 'gnus-suspend-gnus-hook)
2435   ;; Kill Gnus buffers except for group mode buffer.
2436   (let* ((group-buf (get-buffer gnus-group-buffer))
2437          ;; Do this on a separate list in case the user does a ^G before we finish
2438          (gnus-buffer-list
2439           (delete group-buf (delete gnus-dribble-buffer
2440                                     (append gnus-buffer-list nil)))))
2441     (while gnus-buffer-list
2442       (gnus-kill-buffer (pop gnus-buffer-list)))
2443     (gnus-kill-gnus-frames)
2444     (when group-buf
2445       (setq gnus-buffer-list (list group-buf))
2446       (bury-buffer group-buf)
2447       (delete-windows-on group-buf t))))
2448
2449 (defun gnus-group-clear-dribble ()
2450   "Clear all information from the dribble buffer."
2451   (interactive)
2452   (gnus-dribble-clear)
2453   (gnus-message 7 "Cleared dribble buffer"))
2454
2455 (defun gnus-group-exit ()
2456   "Quit reading news after updating .newsrc.eld and .newsrc.
2457 The hook `gnus-exit-gnus-hook' is called before actually exiting."
2458   (interactive)
2459   (when 
2460       (or noninteractive                ;For gnus-batch-kill
2461           (not gnus-interactive-exit)   ;Without confirmation
2462           gnus-expert-user
2463           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
2464     (run-hooks 'gnus-exit-gnus-hook)
2465     ;; Offer to save data from non-quitted summary buffers.
2466     (gnus-offer-save-summaries)
2467     ;; Save the newsrc file(s).
2468     (gnus-save-newsrc-file)
2469     ;; Kill-em-all.
2470     (gnus-close-backends)
2471     ;; Reset everything.
2472     (gnus-clear-system)
2473     ;; Allow the user to do things after cleaning up.
2474     (run-hooks 'gnus-after-exiting-gnus-hook)))
2475
2476 (defun gnus-group-quit ()
2477   "Quit reading news without updating .newsrc.eld or .newsrc.
2478 The hook `gnus-exit-gnus-hook' is called before actually exiting."
2479   (interactive)
2480   (when (or noninteractive              ;For gnus-batch-kill
2481             (zerop (buffer-size))
2482             (not (gnus-server-opened gnus-select-method))
2483             gnus-expert-user
2484             (not gnus-current-startup-file)
2485             (gnus-yes-or-no-p
2486              (format "Quit reading news without saving %s? "
2487                      (file-name-nondirectory gnus-current-startup-file))))
2488     (run-hooks 'gnus-exit-gnus-hook)
2489     (gnus-configure-windows 'group t)
2490     (gnus-dribble-save)
2491     (gnus-close-backends)
2492     (gnus-clear-system)
2493     (gnus-kill-buffer gnus-group-buffer)
2494     ;; Allow the user to do things after cleaning up.
2495     (run-hooks 'gnus-after-exiting-gnus-hook)))
2496
2497 (defun gnus-group-describe-briefly ()
2498   "Give a one line description of the group mode commands."
2499   (interactive)
2500   (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")))
2501
2502 (defun gnus-group-browse-foreign-server (method)
2503   "Browse a foreign news server.
2504 If called interactively, this function will ask for a select method
2505  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
2506 If not, METHOD should be a list where the first element is the method
2507 and the second element is the address."
2508   (interactive
2509    (list (let ((how (completing-read
2510                      "Which backend: "
2511                      (append gnus-valid-select-methods gnus-server-alist)
2512                      nil t (cons "nntp" 0) 'gnus-method-history)))
2513            ;; We either got a backend name or a virtual server name.
2514            ;; If the first, we also need an address.
2515            (if (assoc how gnus-valid-select-methods)
2516                (list (intern how)
2517                      ;; Suggested by mapjph@bath.ac.uk.
2518                      (completing-read
2519                       "Address: "
2520                       (mapcar (lambda (server) (list server))
2521                               gnus-secondary-servers)))
2522              ;; We got a server name, so we find the method.
2523              (gnus-server-to-method how)))))
2524   (gnus-browse-foreign-server method))
2525
2526 (defun gnus-group-set-info (info &optional method-only-group part)
2527   (let* ((entry (gnus-gethash
2528                  (or method-only-group (gnus-info-group info))
2529                  gnus-newsrc-hashtb))
2530          (part-info info)
2531          (info (if method-only-group (nth 2 entry) info))
2532          method)
2533     (when method-only-group
2534       (unless entry
2535         (error "Trying to change non-existent group %s" method-only-group))
2536       ;; We have received parts of the actual group info - either the
2537       ;; select method or the group parameters.  We first check
2538       ;; whether we have to extend the info, and if so, do that.
2539       (let ((len (length info))
2540             (total (if (eq part 'method) 5 6)))
2541         (when (< len total)
2542           (setcdr (nthcdr (1- len) info)
2543                   (make-list (- total len) nil)))
2544         ;; Then we enter the new info.
2545         (setcar (nthcdr (1- total) info) part-info)))
2546     (unless entry
2547       ;; This is a new group, so we just create it.
2548       (save-excursion
2549         (set-buffer gnus-group-buffer)
2550         (setq method (gnus-info-method info))
2551         (when (gnus-server-equal method "native")
2552           (setq method nil))
2553         (save-excursion
2554           (set-buffer gnus-group-buffer)
2555           (if method
2556               ;; It's a foreign group...
2557               (gnus-group-make-group
2558                (gnus-group-real-name (gnus-info-group info))
2559                (if (stringp method) method
2560                  (prin1-to-string (car method)))
2561                (and (consp method)
2562                     (nth 1 (gnus-info-method info))))
2563             ;; It's a native group.
2564             (gnus-group-make-group (gnus-info-group info))))
2565         (gnus-message 6 "Note: New group created")
2566         (setq entry
2567               (gnus-gethash (gnus-group-prefixed-name
2568                              (gnus-group-real-name (gnus-info-group info))
2569                              (or (gnus-info-method info) gnus-select-method))
2570                             gnus-newsrc-hashtb))))
2571     ;; Whether it was a new group or not, we now have the entry, so we
2572     ;; can do the update.
2573     (if entry
2574         (progn
2575           (setcar (nthcdr 2 entry) info)
2576           (when (and (not (eq (car entry) t))
2577                      (gnus-active (gnus-info-group info)))
2578             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
2579       (error "No such group: %s" (gnus-info-group info)))))
2580
2581 (defun gnus-group-set-method-info (group select-method)
2582   (gnus-group-set-info select-method group 'method))
2583
2584 (defun gnus-group-set-params-info (group params)
2585   (gnus-group-set-info params group 'params))
2586
2587 (defun gnus-add-marked-articles (group type articles &optional info force)
2588   ;; Add ARTICLES of TYPE to the info of GROUP.
2589   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
2590   ;; add, but replace marked articles of TYPE with ARTICLES.
2591   (let ((info (or info (gnus-get-info group)))
2592         (uncompressed '(score bookmark killed))
2593         marked m)
2594     (or (not info)
2595         (and (not (setq marked (nthcdr 3 info)))
2596              (or (null articles)
2597                  (setcdr (nthcdr 2 info)
2598                          (list (list (cons type (gnus-compress-sequence
2599                                                  articles t)))))))
2600         (and (not (setq m (assq type (car marked))))
2601              (or (null articles)
2602                  (setcar marked
2603                          (cons (cons type (gnus-compress-sequence articles t) )
2604                                (car marked)))))
2605         (if force
2606             (if (null articles)
2607                 (setcar (nthcdr 3 info)
2608                         (delq (assq type (car marked)) (car marked)))
2609               (setcdr m (gnus-compress-sequence articles t)))
2610           (setcdr m (gnus-compress-sequence
2611                      (sort (nconc (gnus-uncompress-range (cdr m))
2612                                   (copy-sequence articles)) '<) t))))))
2613
2614 (defun gnus-update-read-articles (group unread)
2615   "Update the list of read and ticked articles in GROUP using the
2616 UNREAD and TICKED lists.
2617 Note: UNSELECTED has to be sorted over `<'.
2618 Returns whether the updating was successful."
2619   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
2620          (entry (gnus-gethash group gnus-newsrc-hashtb))
2621          (info (nth 2 entry))
2622          (prev 1)
2623          (unread (sort (copy-sequence unread) '<))
2624          read)
2625     (if (or (not info) (not active))
2626         ;; There is no info on this group if it was, in fact,
2627         ;; killed.  Gnus stores no information on killed groups, so
2628         ;; there's nothing to be done.
2629         ;; One could store the information somewhere temporarily,
2630         ;; perhaps...  Hmmm...
2631         ()
2632       ;; Remove any negative articles numbers.
2633       (while (and unread (< (car unread) 0))
2634         (setq unread (cdr unread)))
2635       ;; Remove any expired article numbers
2636       (while (and unread (< (car unread) (car active)))
2637         (setq unread (cdr unread)))
2638       ;; Compute the ranges of read articles by looking at the list of
2639       ;; unread articles.
2640       (while unread
2641         (if (/= (car unread) prev)
2642             (setq read (cons (if (= prev (1- (car unread))) prev
2643                                (cons prev (1- (car unread)))) read)))
2644         (setq prev (1+ (car unread)))
2645         (setq unread (cdr unread)))
2646       (when (<= prev (cdr active))
2647         (setq read (cons (cons prev (cdr active)) read)))
2648       ;; Enter this list into the group info.
2649       (gnus-info-set-read
2650        info (if (> (length read) 1) (nreverse read) read))
2651       ;; Set the number of unread articles in gnus-newsrc-hashtb.
2652       (gnus-get-unread-articles-in-group info (gnus-active group))
2653       t)))
2654
2655 (provide 'gnus-group)
2656
2657 ;;; gnus-group.el ends here