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