Synch to No Gnus 200401220636.
[elisp/gnus.git-] / lisp / gnus-group.el
1 ;;; gnus-group.el --- group mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl)
31   (defvar tool-bar-map))
32
33 (require 'gnus)
34 (require 'gnus-start)
35 (require 'nnmail)
36 (require 'gnus-spec)
37 (require 'gnus-int)
38 (require 'gnus-range)
39 (require 'gnus-win)
40 (require 'gnus-undo)
41 (require 'time-date)
42 (require 'gnus-ems)
43
44 (eval-when-compile (require 'mm-url))
45
46 (defcustom gnus-group-archive-directory
47   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
48   "*The address of the (ding) archives."
49   :group 'gnus-group-foreign
50   :type 'directory)
51
52 (defcustom gnus-group-recent-archive-directory
53   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
54   "*The address of the most recent (ding) articles."
55   :group 'gnus-group-foreign
56   :type 'directory)
57
58 (defcustom gnus-no-groups-message "No Gnus is good news"
59   "*Message displayed by Gnus when no groups are available."
60   :group 'gnus-start
61   :type 'string)
62
63 (defcustom gnus-keep-same-level nil
64   "*Non-nil means that the next newsgroup after the current will be on the same level.
65 When you type, for instance, `n' after reading the last article in the
66 current newsgroup, you will go to the next newsgroup.  If this variable
67 is nil, the next newsgroup will be the next from the group
68 buffer.
69 If this variable is non-nil, Gnus will either put you in the
70 next newsgroup with the same level, or, if no such newsgroup is
71 available, the next newsgroup with the lowest possible level higher
72 than the current level.
73 If this variable is `best', Gnus will make the next newsgroup the one
74 with the best level."
75   :group 'gnus-group-levels
76   :type '(choice (const nil)
77                  (const best)
78                  (sexp :tag "other" t)))
79
80 (defcustom gnus-group-goto-unread t
81   "*If non-nil, movement commands will go to the next unread and subscribed group."
82   :link '(custom-manual "(gnus)Group Maneuvering")
83   :group 'gnus-group-various
84   :type 'boolean)
85
86 (defcustom gnus-goto-next-group-when-activating t
87   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group."
88   :link '(custom-manual "(gnus)Scanning New Messages")
89   :group 'gnus-group-various
90   :type 'boolean)
91
92 (defcustom gnus-permanently-visible-groups nil
93   "*Regexp to match groups that should always be listed in the group buffer.
94 This means that they will still be listed even when there are no
95 unread articles in the groups.
96
97 If nil, no groups are permanently visible."
98   :group 'gnus-group-listing
99   :type '(choice regexp (const nil)))
100
101 (defcustom gnus-list-groups-with-ticked-articles t
102   "*If non-nil, list groups that have only ticked articles.
103 If nil, only list groups that have unread articles."
104   :group 'gnus-group-listing
105   :type 'boolean)
106
107 (defcustom gnus-group-default-list-level gnus-level-subscribed
108   "*Default listing level.
109 Ignored if `gnus-group-use-permanent-levels' is non-nil."
110   :group 'gnus-group-listing
111   :type 'integer)
112
113 (defcustom gnus-group-list-inactive-groups t
114   "*If non-nil, inactive groups will be listed."
115   :group 'gnus-group-listing
116   :group 'gnus-group-levels
117   :type 'boolean)
118
119 (defcustom gnus-group-sort-function 'gnus-group-sort-by-alphabet
120   "*Function used for sorting the group buffer.
121 This function will be called with group info entries as the arguments
122 for the groups to be sorted.  Pre-made functions include
123 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-real-name',
124 `gnus-group-sort-by-unread', `gnus-group-sort-by-level',
125 `gnus-group-sort-by-score', `gnus-group-sort-by-method',
126 `gnus-group-sort-by-server', and `gnus-group-sort-by-rank'.
127
128 This variable can also be a list of sorting functions.  In that case,
129 the most significant sort function should be the last function in the
130 list."
131   :group 'gnus-group-listing
132   :link '(custom-manual "(gnus)Sorting Groups")
133   :type '(repeat :value-to-internal (lambda (widget value)
134                                       (if (listp value) value (list value)))
135                  :match (lambda (widget value)
136                           (or (symbolp value)
137                               (widget-editable-list-match widget value)))
138                  (choice (function-item gnus-group-sort-by-alphabet)
139                          (function-item gnus-group-sort-by-real-name)
140                          (function-item gnus-group-sort-by-unread)
141                          (function-item gnus-group-sort-by-level)
142                          (function-item gnus-group-sort-by-score)
143                          (function-item gnus-group-sort-by-method)
144                          (function-item gnus-group-sort-by-server)
145                          (function-item gnus-group-sort-by-rank)
146                          (function :tag "other" nil))))
147
148 (defcustom gnus-group-line-format "%M\%S\%p\%P\%5y:%B%(%g%)%l %O\n"
149   "*Format of group lines.
150 It works along the same lines as a normal formatting string,
151 with some simple extensions.
152
153 %M    Only marked articles (character, \"*\" or \" \")
154 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
155 %L    Level of subscribedness (integer)
156 %N    Number of unread articles (integer)
157 %I    Number of dormant articles (integer)
158 %i    Number of ticked and dormant (integer)
159 %T    Number of ticked articles (integer)
160 %R    Number of read articles (integer)
161 %U    Number of unseen articles (integer)
162 %t    Estimated total number of articles (integer)
163 %y    Number of unread, unticked articles (integer)
164 %G    Group name (string)
165 %g    Qualified group name (string)
166 %c    Short (collapsed) group name.  See `gnus-group-uncollapsed-levels'.
167 %C    Group comment (string)
168 %D    Group description (string)
169 %s    Select method (string)
170 %o    Moderated group (char, \"m\")
171 %p    Process mark (char)
172 %B    Whether a summary buffer for the group is open (char, \"*\")
173 %O    Moderated group (string, \"(m)\" or \"\")
174 %P    Topic indentation (string)
175 %m    Whether there is new(ish) mail in the group (char, \"%\")
176 %w    Number of new(ish) mails in the group (integer)
177 %l    Whether there are GroupLens predictions for this group (string)
178 %n    Select from where (string)
179 %z    A string that look like `<%s:%n>' if a foreign select method is used
180 %d    The date the group was last entered.
181 %E    Icon as defined by `gnus-group-icon-list'.
182 %u    User defined specifier.  The next character in the format string should
183       be a letter.  Gnus will call the function gnus-user-format-function-X,
184       where X is the letter following %u.  The function will be passed a
185       single dummy parameter as argument.  The function should return a
186       string, which will be inserted into the buffer just like information
187       from any other group specifier.
188
189 Note that this format specification is not always respected.  For
190 reasons of efficiency, when listing killed groups, this specification
191 is ignored altogether.  If the spec is changed considerably, your
192 output may end up looking strange when listing both alive and killed
193 groups.
194
195 If you use %o or %O, reading the active file will be slower and quite
196 a bit of extra memory will be used.  %D will also worsen performance.
197 Also note that if you change the format specification to include any
198 of these specs, you must probably re-start Gnus to see them go into
199 effect.
200
201 General format specifiers can also be used.
202 See Info node `(gnus)Formatting Variables'."
203   :link '(custom-manual "(gnus)Formatting Variables")
204   :group 'gnus-group-visual
205   :type 'string)
206
207 (defcustom gnus-group-mode-line-format "Gnus: %%b {%M\%:%S}"
208   "*The format specification for the group mode line.
209 It works along the same lines as a normal formatting string,
210 with some simple extensions:
211
212 %S   The native news server.
213 %M   The native select method.
214 %:   \":\" if %S isn't \"\"."
215   :group 'gnus-group-visual
216   :type 'string)
217
218 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
219 (when (featurep 'xemacs)
220   (add-hook 'gnus-group-mode-hook 'gnus-xmas-group-menu-add)
221   (add-hook 'gnus-group-mode-hook 'gnus-xmas-setup-group-toolbar))
222
223 (defcustom gnus-group-menu-hook nil
224   "Hook run after the creation of the group mode menu."
225   :group 'gnus-group-various
226   :type 'hook)
227
228 (defcustom gnus-group-catchup-group-hook nil
229   "Hook run when catching up a group from the group buffer."
230   :group 'gnus-group-various
231   :link '(custom-manual "(gnus)Group Data")
232   :type 'hook)
233
234 (defcustom gnus-group-update-group-hook nil
235   "Hook called when updating group lines."
236   :group 'gnus-group-visual
237   :type 'hook)
238
239 (defcustom gnus-group-prepare-function 'gnus-group-prepare-flat
240   "*A function that is called to generate the group buffer.
241 The function is called with three arguments: The first is a number;
242 all group with a level less or equal to that number should be listed,
243 if the second is non-nil, empty groups should also be displayed.  If
244 the third is non-nil, it is a number.  No groups with a level lower
245 than this number should be displayed.
246
247 The only current function implemented is `gnus-group-prepare-flat'."
248   :group 'gnus-group-listing
249   :type 'function)
250
251 (defcustom gnus-group-prepare-hook nil
252   "Hook called after the group buffer has been generated.
253 If you want to modify the group buffer, you can use this hook."
254   :group 'gnus-group-listing
255   :type 'hook)
256
257 (defcustom gnus-suspend-gnus-hook nil
258   "Hook called when suspending (not exiting) Gnus."
259   :group 'gnus-exit
260   :type 'hook)
261
262 (defcustom gnus-exit-gnus-hook nil
263   "Hook called when exiting Gnus."
264   :group 'gnus-exit
265   :type 'hook)
266
267 (defcustom gnus-after-exiting-gnus-hook nil
268   "Hook called after exiting Gnus."
269   :group 'gnus-exit
270   :type 'hook)
271
272 (defcustom gnus-group-update-hook '(gnus-group-highlight-line)
273   "Hook called when a group line is changed.
274 The hook will not be called if `gnus-visual' is nil.
275
276 The default function `gnus-group-highlight-line' will
277 highlight the line according to the `gnus-group-highlight'
278 variable."
279   :group 'gnus-group-visual
280   :type 'hook)
281
282 (defcustom gnus-useful-groups
283   '(("(ding) mailing list mirrored at sunsite.auc.dk"
284      "emacs.ding"
285      (nntp "sunsite.auc.dk"
286            (nntp-address "sunsite.auc.dk")))
287     ("gnus-bug archive"
288      "gnus-bug"
289      (nndir "/ftp@ftp.ifi.uio.no:/pub/emacs/gnus/gnus-bug/"))
290     ("Gnus help group"
291      "gnus-help"
292      (nndoc "gnus-help"
293             (nndoc-article-type mbox)
294             (eval `(nndoc-address
295                     ,(let ((file (nnheader-find-etc-directory
296                                   "gnus-tut.txt" t)))
297                        (unless file
298                          (error "Couldn't find doc group"))
299                        file))))))
300   "*Alist of useful group-server pairs."
301   :group 'gnus-group-listing
302   :type '(repeat (list (string :tag "Description")
303                        (string :tag "Name")
304                        (sexp :tag "Method"))))
305
306 (defcustom gnus-group-highlight
307   '(;; Mail.
308     ((and mailp (= unread 0) (eq level 1)) .
309      gnus-group-mail-1-empty-face)
310     ((and mailp (eq level 1)) .
311      gnus-group-mail-1-face)
312     ((and mailp (= unread 0) (eq level 2)) .
313      gnus-group-mail-2-empty-face)
314     ((and mailp (eq level 2)) .
315      gnus-group-mail-2-face)
316     ((and mailp (= unread 0) (eq level 3)) .
317      gnus-group-mail-3-empty-face)
318     ((and mailp (eq level 3)) .
319      gnus-group-mail-3-face)
320     ((and mailp (= unread 0)) .
321      gnus-group-mail-low-empty-face)
322     ((and mailp) .
323      gnus-group-mail-low-face)
324     ;; News.
325     ((and (= unread 0) (eq level 1)) .
326      gnus-group-news-1-empty-face)
327     ((and (eq level 1)) .
328      gnus-group-news-1-face)
329     ((and (= unread 0) (eq level 2)) .
330      gnus-group-news-2-empty-face)
331     ((and (eq level 2)) .
332      gnus-group-news-2-face)
333     ((and (= unread 0) (eq level 3)) .
334      gnus-group-news-3-empty-face)
335     ((and (eq level 3)) .
336      gnus-group-news-3-face)
337     ((and (= unread 0) (eq level 4)) .
338      gnus-group-news-4-empty-face)
339     ((and (eq level 4)) .
340      gnus-group-news-4-face)
341     ((and (= unread 0) (eq level 5)) .
342      gnus-group-news-5-empty-face)
343     ((and (eq level 5)) .
344      gnus-group-news-5-face)
345     ((and (= unread 0) (eq level 6)) .
346      gnus-group-news-6-empty-face)
347     ((and (eq level 6)) .
348      gnus-group-news-6-face)
349     ((and (= unread 0)) .
350      gnus-group-news-low-empty-face)
351     (t .
352        gnus-group-news-low-face))
353   "*Controls the highlighting of group buffer lines.
354
355 Below is a list of `Form'/`Face' pairs.  When deciding how a a
356 particular group line should be displayed, each form is
357 evaluated.  The content of the face field after the first true form is
358 used.  You can change how those group lines are displayed by
359 editing the face field.
360
361 It is also possible to change and add form fields, but currently that
362 requires an understanding of Lisp expressions.  Hopefully this will
363 change in a future release.  For now, you can use the following
364 variables in the Lisp expression:
365
366 group: The name of the group.
367 unread: The number of unread articles in the group.
368 method: The select method used.
369 mailp: Whether it's a mail group or not.
370 level: The level of the group.
371 score: The score of the group.
372 ticked: The number of ticked articles."
373   :group 'gnus-group-visual
374   :type '(repeat (cons (sexp :tag "Form") face)))
375
376 (defcustom gnus-new-mail-mark ?%
377   "Mark used for groups with new mail."
378   :group 'gnus-group-visual
379   :type 'character)
380
381 (defgroup gnus-group-icons nil
382   "Add Icons to your group buffer.  "
383   :group 'gnus-group-visual)
384
385 (defcustom gnus-group-icon-list
386   nil
387   "*Controls the insertion of icons into group buffer lines.
388
389 Below is a list of `Form'/`File' pairs.  When deciding how a
390 particular group line should be displayed, each form is evaluated.
391 The icon from the file field after the first true form is used.  You
392 can change how those group lines are displayed by editing the file
393 field.  The File will either be found in the
394 `gnus-group-glyph-directory' or by designating absolute name of the
395 file.
396
397 It is also possible to change and add form fields, but currently that
398 requires an understanding of Lisp expressions.  Hopefully this will
399 change in a future release.  For now, you can use the following
400 variables in the Lisp expression:
401
402 group: The name of the group.
403 unread: The number of unread articles in the group.
404 method: The select method used.
405 mailp: Whether it's a mail group or not.
406 newsp: Whether it's a news group or not
407 level: The level of the group.
408 score: The score of the group.
409 ticked: The number of ticked articles."
410   :group 'gnus-group-icons
411   :type '(repeat (cons (sexp :tag "Form") file)))
412
413 (defcustom gnus-group-name-charset-method-alist nil
414   "Alist of method and the charset for group names.
415
416 For example:
417     (((nntp \"news.com.cn\") . cn-gb-2312))"
418   :version "21.1"
419   :group 'gnus-charset
420   :type '(repeat (cons (sexp :tag "Method") (symbol :tag "Charset"))))
421
422 (defcustom gnus-group-name-charset-group-alist
423   (if (mm-coding-system-p 'utf-8)
424       '(("[^\000-\177]" . utf-8))
425     nil)
426   "Alist of group regexp and the charset for group names.
427
428 For example:
429     ((\"\\.com\\.cn:\" . cn-gb-2312))"
430   :group 'gnus-charset
431   :type '(repeat (cons (regexp :tag "Group") (symbol :tag "Charset"))))
432
433 (defcustom gnus-group-jump-to-group-prompt nil
434   "Default prompt for `gnus-group-jump-to-group'.
435 If non-nil, the value should be a string, e.g. \"nnml:\",
436 in which case `gnus-group-jump-to-group' offers \"Group: nnml:\"
437 in the minibuffer prompt."
438   :group 'gnus-group-various
439   :type '(choice (string :tag "Prompt string")
440                  (const :tag "Empty" nil)))
441
442 (defvar gnus-group-listing-limit 1000
443   "*A limit of the number of groups when listing.
444 If the number of groups is larger than the limit, list them in a
445 simple manner.")
446
447 ;;; Internal variables
448
449 (defvar gnus-group-is-exiting-p nil)
450 (defvar gnus-group-is-exiting-without-update-p nil)
451 (defvar gnus-group-sort-alist-function 'gnus-group-sort-flat
452   "Function for sorting the group buffer.")
453
454 (defvar gnus-group-sort-selected-function 'gnus-group-sort-selected-flat
455   "Function for sorting the selected groups in the group buffer.")
456
457 (defvar gnus-group-indentation-function nil)
458 (defvar gnus-goto-missing-group-function nil)
459 (defvar gnus-group-update-group-function nil)
460 (defvar gnus-group-goto-next-group-function nil
461   "Function to override finding the next group after listing groups.")
462
463 (defvar gnus-group-edit-buffer nil)
464
465 (defvar gnus-group-line-format-alist
466   `((?M gnus-tmp-marked-mark ?c)
467     (?S gnus-tmp-subscribed ?c)
468     (?L gnus-tmp-level ?d)
469     (?N (cond ((eq number t) "*" )
470               ((numberp number)
471                (int-to-string
472                 (+ number
473                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
474                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
475               (t number)) ?s)
476     (?R gnus-tmp-number-of-read ?s)
477     (?U (gnus-number-of-unseen-articles-in-group gnus-tmp-group) ?d)
478     (?t gnus-tmp-number-total ?d)
479     (?y gnus-tmp-number-of-unread ?s)
480     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
481     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
482     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
483            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
484     (?g gnus-tmp-group ?s)
485     (?G gnus-tmp-qualified-group ?s)
486     (?c (gnus-short-group-name gnus-tmp-group) ?s)
487     (?C gnus-tmp-comment ?s)
488     (?D gnus-tmp-newsgroup-description ?s)
489     (?o gnus-tmp-moderated ?c)
490     (?O gnus-tmp-moderated-string ?s)
491     (?p gnus-tmp-process-marked ?c)
492     (?s gnus-tmp-news-server ?s)
493     (?n gnus-tmp-news-method ?s)
494     (?P gnus-group-indentation ?s)
495     (?E gnus-tmp-group-icon ?s)
496     (?B gnus-tmp-summary-live ?c)
497     (?l gnus-tmp-grouplens ?s)
498     (?z gnus-tmp-news-method-string ?s)
499     (?m (gnus-group-new-mail gnus-tmp-group) ?c)
500     (?w (if (gnus-news-group-p gnus-tmp-group)
501             ""
502           (int-to-string
503            (length
504             (nnmail-new-mail-numbers (gnus-group-real-name gnus-tmp-group))
505             )))
506         ?s)
507     (?d (gnus-group-timestamp-string gnus-tmp-group) ?s)
508     (?u gnus-tmp-user-defined ?s)))
509
510 (defvar gnus-group-mode-line-format-alist
511   `((?S gnus-tmp-news-server ?s)
512     (?M gnus-tmp-news-method ?s)
513     (?u gnus-tmp-user-defined ?s)
514     (?: gnus-tmp-colon ?s)))
515
516 (defvar gnus-topic-topology nil
517   "The complete topic hierarchy.")
518
519 (defvar gnus-topic-alist nil
520   "The complete topic-group alist.")
521
522 (defvar gnus-group-marked nil)
523
524 (defvar gnus-group-list-mode nil)
525
526
527 (defvar gnus-group-icon-cache nil)
528
529 (defvar gnus-group-listed-groups nil)
530 (defvar gnus-group-list-option nil)
531
532 ;;;
533 ;;; Gnus group mode
534 ;;;
535
536 (put 'gnus-group-mode 'mode-class 'special)
537
538 (gnus-define-keys gnus-group-mode-map
539   " " gnus-group-read-group
540   "=" gnus-group-select-group
541   "\r" gnus-group-select-group
542   "\M-\r" gnus-group-quick-select-group
543   "\M- " gnus-group-visible-select-group
544   [(meta control return)] gnus-group-select-group-ephemerally
545   "j" gnus-group-jump-to-group
546   "n" gnus-group-next-unread-group
547   "p" gnus-group-prev-unread-group
548   "\177" gnus-group-prev-unread-group
549   [delete] gnus-group-prev-unread-group
550   [backspace] gnus-group-prev-unread-group
551   "N" gnus-group-next-group
552   "P" gnus-group-prev-group
553   "\M-n" gnus-group-next-unread-group-same-level
554   "\M-p" gnus-group-prev-unread-group-same-level
555   "," gnus-group-best-unread-group
556   "." gnus-group-first-unread-group
557   "u" gnus-group-unsubscribe-current-group
558   "U" gnus-group-unsubscribe-group
559   "c" gnus-group-catchup-current
560   "C" gnus-group-catchup-current-all
561   "\M-c" gnus-group-clear-data
562   "l" gnus-group-list-groups
563   "L" gnus-group-list-all-groups
564   "m" gnus-group-mail
565   "i" gnus-group-news
566   "g" gnus-group-get-new-news
567   "\M-g" gnus-group-get-new-news-this-group
568   "R" gnus-group-restart
569   "r" gnus-group-read-init-file
570   "B" gnus-group-browse-foreign-server
571   "b" gnus-group-check-bogus-groups
572   "F" gnus-group-find-new-groups
573   "\C-c\C-d" gnus-group-describe-group
574   "\M-d" gnus-group-describe-all-groups
575   "\C-c\C-a" gnus-group-apropos
576   "\C-c\M-\C-a" gnus-group-description-apropos
577   "a" gnus-group-post-news
578   "\ek" gnus-group-edit-local-kill
579   "\eK" gnus-group-edit-global-kill
580   "\C-k" gnus-group-kill-group
581   "\C-y" gnus-group-yank-group
582   "\C-w" gnus-group-kill-region
583   "\C-x\C-t" gnus-group-transpose-groups
584   "\C-c\C-l" gnus-group-list-killed
585   "\C-c\C-x" gnus-group-expire-articles
586   "\C-c\M-\C-x" gnus-group-expire-all-groups
587   "V" gnus-version
588   "s" gnus-group-save-newsrc
589   "z" gnus-group-suspend
590   "q" gnus-group-exit
591   "Q" gnus-group-quit
592   "?" gnus-group-describe-briefly
593   "\C-c\C-i" gnus-info-find-node
594   "\M-e" gnus-group-edit-group-method
595   "^" gnus-group-enter-server-mode
596   gnus-mouse-2 gnus-mouse-pick-group
597   "<" beginning-of-buffer
598   ">" end-of-buffer
599   "\C-c\C-b" gnus-bug
600   "\C-c\C-n" gnus-namazu-search
601   "\C-c\C-s" gnus-group-sort-groups
602   "t" gnus-topic-mode
603   "\C-c\M-g" gnus-activate-all-groups
604   "\M-&" gnus-group-universal-argument
605   "#" gnus-group-mark-group
606   "\M-#" gnus-group-unmark-group)
607
608 (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
609   "m" gnus-group-mark-group
610   "u" gnus-group-unmark-group
611   "w" gnus-group-mark-region
612   "b" gnus-group-mark-buffer
613   "r" gnus-group-mark-regexp
614   "U" gnus-group-unmark-all-groups)
615
616 (gnus-define-keys (gnus-group-sieve-map "D" gnus-group-mode-map)
617   "u" gnus-sieve-update
618   "g" gnus-sieve-generate)
619
620 (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
621   "d" gnus-group-make-directory-group
622   "h" gnus-group-make-help-group
623   "u" gnus-group-make-useful-group
624   "a" gnus-group-make-archive-group
625   "k" gnus-group-make-kiboze-group
626   "l" gnus-group-nnimap-edit-acl
627   "m" gnus-group-make-group
628   "n" gnus-group-make-shimbun-group
629   "E" gnus-group-edit-group
630   "e" gnus-group-edit-group-method
631   "p" gnus-group-edit-group-parameters
632   "v" gnus-group-add-to-virtual
633   "V" gnus-group-make-empty-virtual
634   "D" gnus-group-enter-directory
635   "f" gnus-group-make-doc-group
636   "G" gnus-group-make-nnir-group
637   "w" gnus-group-make-web-group
638   "M" gnus-group-read-ephemeral-group
639   "r" gnus-group-rename-group
640   "R" gnus-group-make-rss-group
641   "c" gnus-group-customize
642   "x" gnus-group-nnimap-expunge
643   "\177" gnus-group-delete-group
644   [delete] gnus-group-delete-group)
645
646 (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
647   "b" gnus-group-brew-soup
648   "w" gnus-soup-save-areas
649   "s" gnus-soup-send-replies
650   "p" gnus-soup-pack-packet
651   "r" nnsoup-pack-replies)
652
653 (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
654   "s" gnus-group-sort-groups
655   "a" gnus-group-sort-groups-by-alphabet
656   "u" gnus-group-sort-groups-by-unread
657   "l" gnus-group-sort-groups-by-level
658   "v" gnus-group-sort-groups-by-score
659   "r" gnus-group-sort-groups-by-rank
660   "m" gnus-group-sort-groups-by-method
661   "n" gnus-group-sort-groups-by-real-name)
662
663 (gnus-define-keys (gnus-group-sort-selected-map "P" gnus-group-group-map)
664   "s" gnus-group-sort-selected-groups
665   "a" gnus-group-sort-selected-groups-by-alphabet
666   "u" gnus-group-sort-selected-groups-by-unread
667   "l" gnus-group-sort-selected-groups-by-level
668   "v" gnus-group-sort-selected-groups-by-score
669   "r" gnus-group-sort-selected-groups-by-rank
670   "m" gnus-group-sort-selected-groups-by-method
671   "n" gnus-group-sort-selected-groups-by-real-name)
672
673 (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
674   "k" gnus-group-list-killed
675   "z" gnus-group-list-zombies
676   "s" gnus-group-list-groups
677   "u" gnus-group-list-all-groups
678   "A" gnus-group-list-active
679   "a" gnus-group-apropos
680   "d" gnus-group-description-apropos
681   "m" gnus-group-list-matching
682   "M" gnus-group-list-all-matching
683   "l" gnus-group-list-level
684   "c" gnus-group-list-cached
685   "?" gnus-group-list-dormant)
686
687 (gnus-define-keys (gnus-group-list-limit-map "/" gnus-group-list-map)
688   "k"  gnus-group-list-limit
689   "z"  gnus-group-list-limit
690   "s"  gnus-group-list-limit
691   "u"  gnus-group-list-limit
692   "A"  gnus-group-list-limit
693   "m"  gnus-group-list-limit
694   "M"  gnus-group-list-limit
695   "l"  gnus-group-list-limit
696   "c"  gnus-group-list-limit
697   "?"  gnus-group-list-limit)
698
699 (gnus-define-keys (gnus-group-list-flush-map "f" gnus-group-list-map)
700   "k"  gnus-group-list-flush
701   "z"  gnus-group-list-flush
702   "s"  gnus-group-list-flush
703   "u"  gnus-group-list-flush
704   "A"  gnus-group-list-flush
705   "m"  gnus-group-list-flush
706   "M"  gnus-group-list-flush
707   "l"  gnus-group-list-flush
708   "c"  gnus-group-list-flush
709   "?"  gnus-group-list-flush)
710
711 (gnus-define-keys (gnus-group-list-plus-map "p" gnus-group-list-map)
712   "k"  gnus-group-list-plus
713   "z"  gnus-group-list-plus
714   "s"  gnus-group-list-plus
715   "u"  gnus-group-list-plus
716   "A"  gnus-group-list-plus
717   "m"  gnus-group-list-plus
718   "M"  gnus-group-list-plus
719   "l"  gnus-group-list-plus
720   "c"  gnus-group-list-plus
721   "?"  gnus-group-list-plus)
722
723 (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
724   "f" gnus-score-flush-cache
725   "e" gnus-score-edit-all-score)
726
727 (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
728   "c" gnus-group-fetch-charter
729   "C" gnus-group-fetch-control
730   "d" gnus-group-describe-group
731   "f" gnus-group-fetch-faq
732   "v" gnus-version)
733
734 (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
735   "l" gnus-group-set-current-level
736   "t" gnus-group-unsubscribe-current-group
737   "s" gnus-group-unsubscribe-group
738   "k" gnus-group-kill-group
739   "y" gnus-group-yank-group
740   "w" gnus-group-kill-region
741   "\C-k" gnus-group-kill-level
742   "z" gnus-group-kill-all-zombies)
743
744 (defun gnus-topic-mode-p ()
745   "Return non-nil in `gnus-topic-mode'."
746   (and (boundp 'gnus-topic-mode)
747        (symbol-value 'gnus-topic-mode)))
748
749 (defun gnus-group-make-menu-bar ()
750   (gnus-turn-off-edit-menu 'group)
751   (unless (boundp 'gnus-group-reading-menu)
752
753     (easy-menu-define
754      gnus-group-reading-menu gnus-group-mode-map ""
755      `("Group"
756        ["Read" gnus-group-read-group
757         :included (not (gnus-topic-mode-p))
758         :active (gnus-group-group-name)]
759        ["Read " gnus-topic-read-group
760         :included (gnus-topic-mode-p)]
761        ["Select" gnus-group-select-group
762         :included (not (gnus-topic-mode-p))
763         :active (gnus-group-group-name)]
764        ["Select " gnus-topic-select-group
765         :included (gnus-topic-mode-p)]
766        ["See old articles" (gnus-group-select-group 'all)
767         :keys "C-u SPC" :active (gnus-group-group-name)]
768        ["Catch up" gnus-group-catchup-current
769         :included (not (gnus-topic-mode-p))
770         :active (gnus-group-group-name)
771         ,@(if (featurep 'xemacs) nil
772             '(:help "Mark unread articles in the current group as read"))]
773        ["Catch up " gnus-topic-catchup-articles
774         :included (gnus-topic-mode-p)
775         ,@(if (featurep 'xemacs) nil
776             '(:help "Mark unread articles in the current group or topic as read"))]
777        ["Catch up all articles" gnus-group-catchup-current-all
778         (gnus-group-group-name)]
779        ["Check for new articles" gnus-group-get-new-news-this-group
780         :included (not (gnus-topic-mode-p))
781         :active (gnus-group-group-name)
782         ,@(if (featurep 'xemacs) nil
783             '(:help "Check for new messages in current group"))]
784        ["Check for new articles " gnus-topic-get-new-news-this-topic
785         :included (gnus-topic-mode-p)
786         ,@(if (featurep 'xemacs) nil
787             '(:help "Check for new messages in current group or topic"))]
788        ["Toggle subscription" gnus-group-unsubscribe-current-group
789         (gnus-group-group-name)]
790        ["Kill" gnus-group-kill-group :active (gnus-group-group-name)
791         ,@(if (featurep 'xemacs) nil
792             '(:help "Kill (remove) current group"))]
793        ["Yank" gnus-group-yank-group gnus-list-of-killed-groups]
794        ["Describe" gnus-group-describe-group :active (gnus-group-group-name)
795         ,@(if (featurep 'xemacs) nil
796             '(:help "Display description of the current group"))]
797        ["Fetch FAQ" gnus-group-fetch-faq (gnus-group-group-name)]
798        ["Fetch charter" gnus-group-fetch-charter
799         :active (gnus-group-group-name)
800         ,@(if (featurep 'xemacs) nil
801             '(:help "Display the charter of the current group"))]
802        ["Fetch control message" gnus-group-fetch-control
803         :active (gnus-group-group-name)
804         ,@(if (featurep 'xemacs) nil
805             '(:help "Display the archived control message for the current group"))]
806        ;; Actually one should check, if any of the marked groups gives t for
807        ;; (gnus-check-backend-function 'request-expire-articles ...)
808        ["Expire articles" gnus-group-expire-articles
809         :included (not (gnus-topic-mode-p))
810         :active (or (and (gnus-group-group-name)
811                          (gnus-check-backend-function
812                           'request-expire-articles
813                           (gnus-group-group-name))) gnus-group-marked)]
814        ["Expire articles " gnus-topic-expire-articles
815         :included (gnus-topic-mode-p)]
816        ["Set group level..." gnus-group-set-current-level
817         (gnus-group-group-name)]
818        ["Select quick" gnus-group-quick-select-group (gnus-group-group-name)]
819        ["Customize" gnus-group-customize (gnus-group-group-name)]
820        ("Edit"
821         ["Parameters" gnus-group-edit-group-parameters
822          :included (not (gnus-topic-mode-p))
823          :active (gnus-group-group-name)]
824         ["Parameters " gnus-topic-edit-parameters
825          :included (gnus-topic-mode-p)]
826         ["Select method" gnus-group-edit-group-method
827          (gnus-group-group-name)]
828         ["Info" gnus-group-edit-group (gnus-group-group-name)]
829         ["Local kill file" gnus-group-edit-local-kill (gnus-group-group-name)]
830         ["Global kill file" gnus-group-edit-global-kill t])))
831
832     (easy-menu-define
833      gnus-group-group-menu gnus-group-mode-map ""
834      '("Groups"
835        ("Listing"
836         ["List unread subscribed groups" gnus-group-list-groups t]
837         ["List (un)subscribed groups" gnus-group-list-all-groups t]
838         ["List killed groups" gnus-group-list-killed gnus-killed-list]
839         ["List zombie groups" gnus-group-list-zombies gnus-zombie-list]
840         ["List level..." gnus-group-list-level t]
841         ["Describe all groups" gnus-group-describe-all-groups t]
842         ["Group apropos..." gnus-group-apropos t]
843         ["Group and description apropos..." gnus-group-description-apropos t]
844         ["List groups matching..." gnus-group-list-matching t]
845         ["List all groups matching..." gnus-group-list-all-matching t]
846         ["List active file" gnus-group-list-active t]
847         ["List groups with cached" gnus-group-list-cached t]
848         ["List groups with dormant" gnus-group-list-dormant t])
849        ("Sort"
850         ["Default sort" gnus-group-sort-groups t]
851         ["Sort by method" gnus-group-sort-groups-by-method t]
852         ["Sort by rank" gnus-group-sort-groups-by-rank t]
853         ["Sort by score" gnus-group-sort-groups-by-score t]
854         ["Sort by level" gnus-group-sort-groups-by-level t]
855         ["Sort by unread" gnus-group-sort-groups-by-unread t]
856         ["Sort by name" gnus-group-sort-groups-by-alphabet t]
857         ["Sort by real name" gnus-group-sort-groups-by-real-name t])
858        ("Sort process/prefixed"
859         ["Default sort" gnus-group-sort-selected-groups
860          (not (gnus-topic-mode-p))]
861         ["Sort by method" gnus-group-sort-selected-groups-by-method
862          (not (gnus-topic-mode-p))]
863         ["Sort by rank" gnus-group-sort-selected-groups-by-rank
864          (not (gnus-topic-mode-p))]
865         ["Sort by score" gnus-group-sort-selected-groups-by-score
866          (not (gnus-topic-mode-p))]
867         ["Sort by level" gnus-group-sort-selected-groups-by-level
868          (not (gnus-topic-mode-p))]
869         ["Sort by unread" gnus-group-sort-selected-groups-by-unread
870          (not (gnus-topic-mode-p))]
871         ["Sort by name" gnus-group-sort-selected-groups-by-alphabet
872          (not (gnus-topic-mode-p))]
873         ["Sort by real name" gnus-group-sort-selected-groups-by-real-name
874          (not (gnus-topic-mode-p))])
875        ("Mark"
876         ["Mark group" gnus-group-mark-group
877          (and (gnus-group-group-name)
878               (not (memq (gnus-group-group-name) gnus-group-marked)))]
879         ["Unmark group" gnus-group-unmark-group
880          (and (gnus-group-group-name)
881               (memq (gnus-group-group-name) gnus-group-marked))]
882         ["Unmark all" gnus-group-unmark-all-groups gnus-group-marked]
883         ["Mark regexp..." gnus-group-mark-regexp t]
884         ["Mark region" gnus-group-mark-region :active (gnus-mark-active-p)]
885         ["Mark buffer" gnus-group-mark-buffer t]
886         ["Execute command" gnus-group-universal-argument
887          (or gnus-group-marked (gnus-group-group-name))])
888        ("Subscribe"
889         ["Subscribe to a group..." gnus-group-unsubscribe-group t]
890         ["Kill all newsgroups in region" gnus-group-kill-region
891          :active (gnus-mark-active-p)]
892         ["Kill all zombie groups" gnus-group-kill-all-zombies
893          gnus-zombie-list]
894         ["Kill all groups on level..." gnus-group-kill-level t])
895        ("Foreign groups"
896         ["Make a foreign group..." gnus-group-make-group t]
897         ["Make a shimbun group..." gnus-group-make-shimbun-group t]
898         ["Add a directory group..." gnus-group-make-directory-group t]
899         ["Add the help group" gnus-group-make-help-group t]
900         ["Add the archive group" gnus-group-make-archive-group t]
901         ["Make a doc group..." gnus-group-make-doc-group t]
902         ["Make a web group..." gnus-group-make-web-group t]
903         ["Make a kiboze group..." gnus-group-make-kiboze-group t]
904         ["Make a virtual group..." gnus-group-make-empty-virtual t]
905         ["Add a group to a virtual..." gnus-group-add-to-virtual t]
906         ["Make an RSS group..." gnus-group-make-rss-group t]
907         ["Rename group..." gnus-group-rename-group
908          (gnus-check-backend-function
909           'request-rename-group (gnus-group-group-name))]
910         ["Delete group" gnus-group-delete-group
911          (gnus-check-backend-function
912           'request-delete-group (gnus-group-group-name))])
913        ("Move"
914         ["Next" gnus-group-next-group t]
915         ["Previous" gnus-group-prev-group t]
916         ["Next unread" gnus-group-next-unread-group t]
917         ["Previous unread" gnus-group-prev-unread-group t]
918         ["Next unread same level" gnus-group-next-unread-group-same-level t]
919         ["Previous unread same level"
920          gnus-group-prev-unread-group-same-level t]
921         ["Jump to group..." gnus-group-jump-to-group t]
922         ["First unread group" gnus-group-first-unread-group t]
923         ["Best unread group" gnus-group-best-unread-group t])
924        ("Sieve"
925         ["Generate" gnus-sieve-generate t]
926         ["Generate and update" gnus-sieve-update t])
927        ["Delete bogus groups" gnus-group-check-bogus-groups t]
928        ["Find new newsgroups" gnus-group-find-new-groups t]
929        ["Transpose" gnus-group-transpose-groups
930         (gnus-group-group-name)]
931        ["Read a directory as a group..." gnus-group-enter-directory t]))
932
933     (easy-menu-define
934      gnus-group-misc-menu gnus-group-mode-map ""
935      `("Gnus"
936        ("SOUP"
937         ["Pack replies" nnsoup-pack-replies (fboundp 'nnsoup-request-group)]
938         ["Send replies" gnus-soup-send-replies
939          (fboundp 'gnus-soup-pack-packet)]
940         ["Pack packet" gnus-soup-pack-packet (fboundp 'gnus-soup-pack-packet)]
941         ["Save areas" gnus-soup-save-areas (fboundp 'gnus-soup-pack-packet)]
942         ["Brew SOUP" gnus-group-brew-soup (fboundp 'gnus-soup-pack-packet)])
943        ["Send a mail" gnus-group-mail t]
944        ["Send a message (mail or news)" gnus-group-post-news t]
945        ["Create a local message" gnus-group-news t]
946        ["Check for new news" gnus-group-get-new-news
947         ,@(if (featurep 'xemacs) '(t)
948             '(:help "Get newly arrived articles"))
949         ]
950        ["Send queued messages" gnus-delay-send-queue
951         ,@(if (featurep 'xemacs) '(t)
952             '(:help "Send all messages that are scheduled to be sent now"))
953         ]
954        ["Activate all groups" gnus-activate-all-groups t]
955        ["Restart Gnus" gnus-group-restart t]
956        ["Read init file" gnus-group-read-init-file t]
957        ["Browse foreign server..." gnus-group-browse-foreign-server t]
958        ["Enter server buffer" gnus-group-enter-server-mode t]
959        ["Expire all expirable articles" gnus-group-expire-all-groups t]
960        ["Generate any kiboze groups" nnkiboze-generate-groups t]
961        ["Gnus version" gnus-version t]
962        ["Save .newsrc files" gnus-group-save-newsrc t]
963        ["Suspend Gnus" gnus-group-suspend t]
964        ["Clear dribble buffer" gnus-group-clear-dribble t]
965        ["Read manual" gnus-info-find-node t]
966        ["Flush score cache" gnus-score-flush-cache t]
967        ["Toggle topics" gnus-topic-mode t]
968        ["Send a bug report" gnus-bug t]
969        ["Exit from Gnus" gnus-group-exit
970         ,@(if (featurep 'xemacs) '(t)
971             '(:help "Quit reading news"))]
972        ["Exit without saving" gnus-group-quit t]))
973
974     (gnus-run-hooks 'gnus-group-menu-hook)))
975
976 (defvar gnus-group-toolbar-map nil)
977
978 ;; Emacs 21 tool bar.  Should be no-op otherwise.
979 (defun gnus-group-make-tool-bar ()
980   (if (and
981        (condition-case nil (require 'tool-bar) (error nil))
982        (fboundp 'tool-bar-add-item-from-menu)
983        (default-value 'tool-bar-mode)
984        (not gnus-group-toolbar-map))
985       (setq gnus-group-toolbar-map
986             (let ((tool-bar-map (make-sparse-keymap))
987                   (load-path (mm-image-load-path)))
988               (tool-bar-add-item-from-menu
989                'gnus-group-get-new-news "get-news" gnus-group-mode-map)
990               (tool-bar-add-item-from-menu
991                'gnus-group-get-new-news-this-group "gnntg" gnus-group-mode-map)
992               (tool-bar-add-item-from-menu
993                'gnus-group-catchup-current "catchup" gnus-group-mode-map)
994               (tool-bar-add-item-from-menu
995                'gnus-group-describe-group "describe-group" gnus-group-mode-map)
996               (tool-bar-add-item "subscribe" 'gnus-group-subscribe 'subscribe
997                                  :help "Subscribe to the current group")
998               (tool-bar-add-item "unsubscribe" 'gnus-group-unsubscribe
999                                  'unsubscribe
1000                                  :help "Unsubscribe from the current group")
1001               (tool-bar-add-item-from-menu
1002                'gnus-group-exit "exit-gnus" gnus-group-mode-map)
1003               tool-bar-map)))
1004   (if gnus-group-toolbar-map
1005       (set (make-local-variable 'tool-bar-map) gnus-group-toolbar-map)))
1006
1007 (defun gnus-group-mode ()
1008   "Major mode for reading news.
1009
1010 All normal editing commands are switched off.
1011 \\<gnus-group-mode-map>
1012 The group buffer lists (some of) the groups available.  For instance,
1013 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
1014 lists all zombie groups.
1015
1016 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
1017 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
1018
1019 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
1020
1021 The following commands are available:
1022
1023 \\{gnus-group-mode-map}"
1024   (interactive)
1025   (kill-all-local-variables)
1026   (when (gnus-visual-p 'group-menu 'menu)
1027     (gnus-group-make-menu-bar)
1028     (gnus-group-make-tool-bar))
1029   (gnus-simplify-mode-line)
1030   (setq major-mode 'gnus-group-mode)
1031   (setq mode-name "Group")
1032   (gnus-group-set-mode-line)
1033   (setq mode-line-process nil)
1034   (use-local-map gnus-group-mode-map)
1035   (buffer-disable-undo)
1036   (setq truncate-lines t)
1037   (setq buffer-read-only t)
1038   (gnus-set-default-directory)
1039   (gnus-update-format-specifications nil 'group 'group-mode)
1040   (gnus-update-group-mark-positions)
1041   (when gnus-use-undo
1042     (gnus-undo-mode 1))
1043   (when gnus-slave
1044     (gnus-slave-mode))
1045   (gnus-run-hooks 'gnus-group-mode-hook))
1046
1047 (defun gnus-update-group-mark-positions ()
1048   (save-excursion
1049     (let ((gnus-process-mark ?\200)
1050           (gnus-group-update-hook nil)
1051           (gnus-group-marked '("dummy.group"))
1052           (gnus-active-hashtb (make-vector 10 0))
1053           (topic ""))
1054       (gnus-set-active "dummy.group" '(0 . 0))
1055       (gnus-set-work-buffer)
1056       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
1057       (goto-char (point-min))
1058       (setq gnus-group-mark-positions
1059             (list (cons 'process (and (search-forward "\200" nil t)
1060                                       (- (point) 2))))))))
1061
1062 (defun gnus-mouse-pick-group (e)
1063   "Enter the group under the mouse pointer."
1064   (interactive "e")
1065   (mouse-set-point e)
1066   (gnus-group-read-group nil))
1067
1068 ;; Look at LEVEL and find out what the level is really supposed to be.
1069 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
1070 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
1071 (defun gnus-group-default-level (&optional level number-or-nil)
1072   (cond
1073    (gnus-group-use-permanent-levels
1074     (or (setq gnus-group-use-permanent-levels
1075               (or level (if (numberp gnus-group-use-permanent-levels)
1076                             gnus-group-use-permanent-levels
1077                           (or gnus-group-default-list-level
1078                               gnus-level-subscribed))))
1079         gnus-group-default-list-level gnus-level-subscribed))
1080    (number-or-nil
1081     level)
1082    (t
1083     (or level gnus-group-default-list-level gnus-level-subscribed))))
1084
1085 (defun gnus-group-setup-buffer ()
1086   (set-buffer (gnus-get-buffer-create gnus-group-buffer))
1087   (unless (eq major-mode 'gnus-group-mode)
1088     (gnus-group-mode)
1089     (when gnus-carpal
1090       (gnus-carpal-setup-buffer 'group))))
1091
1092 (defun gnus-group-name-charset (method group)
1093   (if (null method)
1094       (setq method (gnus-find-method-for-group group)))
1095   (let ((item (assoc method gnus-group-name-charset-method-alist))
1096         (alist gnus-group-name-charset-group-alist)
1097         result)
1098     (if item
1099         (cdr item)
1100       (while (setq item (pop alist))
1101         (if (string-match (car item) group)
1102             (setq alist nil
1103                   result (cdr item))))
1104       result)))
1105
1106 (defun gnus-group-name-decode (string charset)
1107   ;; Fixme: Don't decode in unibyte mode.
1108   (if (and string charset (featurep 'mule))
1109       (decode-coding-string string charset)
1110     string))
1111
1112 (defun gnus-group-decoded-name (string)
1113   (let ((charset (gnus-group-name-charset nil string)))
1114     (gnus-group-name-decode string charset)))
1115
1116 (defun gnus-group-name-encode (string charset)
1117   (if (and string charset (featurep 'mule))
1118       (encode-coding-string string charset)
1119     string))
1120
1121 (defun gnus-group-encoded-name (string)
1122   (let ((charset (gnus-group-name-charset nil string)))
1123     (gnus-group-name-encode string charset)))
1124
1125 (defun gnus-group-completing-read-group-name
1126   (prompt table &optional predicate require-match initial-contents history)
1127   (if (vectorp table)
1128       (mapatoms
1129        (lambda (group)
1130          (push (list (gnus-group-decoded-name (symbol-name group))) table))
1131        (prog1
1132            table
1133          (setq table nil)))
1134     (dolist (entry (prog1
1135                        table
1136                      (setq table nil)))
1137       (push (list (gnus-group-decoded-name (car entry))) table)))
1138   (gnus-group-encoded-name
1139    (completing-read
1140     prompt table predicate
1141     require-match
1142     initial-contents
1143     history)))
1144
1145 (defun gnus-group-list-groups (&optional level unread lowest)
1146   "List newsgroups with level LEVEL or lower that have unread articles.
1147 Default is all subscribed groups.
1148 If argument UNREAD is non-nil, groups with no unread articles are also
1149 listed.
1150
1151 Also see the `gnus-group-use-permanent-levels' variable."
1152   (interactive
1153    (list (if current-prefix-arg
1154              (prefix-numeric-value current-prefix-arg)
1155            (or
1156             (gnus-group-default-level nil t)
1157             gnus-group-default-list-level
1158             gnus-level-subscribed))))
1159   (unless level
1160     (setq level (car gnus-group-list-mode)
1161           unread (cdr gnus-group-list-mode)))
1162   (setq level (gnus-group-default-level level))
1163   (gnus-group-setup-buffer)
1164   (gnus-update-format-specifications nil 'group 'group-mode)
1165   (let ((case-fold-search nil)
1166         (props (text-properties-at (point-at-bol)))
1167         (empty (= (point-min) (point-max)))
1168         (group (gnus-group-group-name))
1169         number)
1170     (set-buffer gnus-group-buffer)
1171     (setq number (funcall gnus-group-prepare-function level unread lowest))
1172     (when (or (and (numberp number)
1173                    (zerop number))
1174               (zerop (buffer-size)))
1175       ;; No groups in the buffer.
1176       (gnus-message 5 gnus-no-groups-message))
1177     ;; We have some groups displayed.
1178     (goto-char (point-max))
1179     (when (or (not gnus-group-goto-next-group-function)
1180               (not (funcall gnus-group-goto-next-group-function
1181                             group props)))
1182       (cond
1183        (empty
1184         (goto-char (point-min)))
1185        ((not group)
1186         ;; Go to the first group with unread articles.
1187         (gnus-group-search-forward t))
1188        (t
1189         ;; Find the right group to put point on.  If the current group
1190         ;; has disappeared in the new listing, try to find the next
1191         ;; one.  If no next one can be found, just leave point at the
1192         ;; first newsgroup in the buffer.
1193         (when (not (gnus-goto-char
1194                     (text-property-any
1195                      (point-min) (point-max)
1196                      'gnus-group (gnus-intern-safe
1197                                   group gnus-active-hashtb))))
1198           (let ((newsrc (cdddr (gnus-group-entry group))))
1199             (while (and newsrc
1200                         (not (gnus-goto-char
1201                               (text-property-any
1202                                (point-min) (point-max) 'gnus-group
1203                                (gnus-intern-safe
1204                                 (caar newsrc) gnus-active-hashtb)))))
1205               (setq newsrc (cdr newsrc)))
1206             (unless newsrc
1207               (goto-char (point-max))
1208               (forward-line -1)))))))
1209     ;; Adjust cursor point.
1210     (gnus-group-position-point)))
1211
1212 (defun gnus-group-list-level (level &optional all)
1213   "List groups on LEVEL.
1214 If ALL (the prefix), also list groups that have no unread articles."
1215   (interactive "nList groups on level: \nP")
1216   (gnus-group-list-groups level all level))
1217
1218 (defun gnus-group-prepare-logic (group test)
1219   (or (and gnus-group-listed-groups
1220            (null gnus-group-list-option)
1221            (member group gnus-group-listed-groups))
1222       (cond
1223        ((null gnus-group-listed-groups) test)
1224        ((null gnus-group-list-option) test)
1225        (t (and (member group gnus-group-listed-groups)
1226                (if (eq gnus-group-list-option 'flush)
1227                    (not test)
1228                  test))))))
1229
1230 (defun gnus-group-prepare-flat (level &optional predicate lowest regexp)
1231   "List all newsgroups with unread articles of level LEVEL or lower.
1232 If PREDICATE is a function, list groups that the function returns non-nil;
1233 if it is t, list groups that have no unread articles.
1234 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
1235 If REGEXP is a function, list dead groups that the function returns non-nil;
1236 if it is a string, only list groups matching REGEXP."
1237   (set-buffer gnus-group-buffer)
1238   (let ((buffer-read-only nil)
1239         (newsrc (cdr gnus-newsrc-alist))
1240         (lowest (or lowest 1))
1241         (not-in-list (and gnus-group-listed-groups
1242                           (copy-sequence gnus-group-listed-groups)))
1243         info clevel unread group params)
1244     (erase-buffer)
1245     (when (or (< lowest gnus-level-zombie)
1246               gnus-group-listed-groups)
1247       ;; List living groups.
1248       (while newsrc
1249         (setq info (car newsrc)
1250               group (gnus-info-group info)
1251               params (gnus-info-params info)
1252               newsrc (cdr newsrc)
1253               unread (gnus-group-unread group))
1254         (when not-in-list
1255           (setq not-in-list (delete group not-in-list)))
1256         (when (gnus-group-prepare-logic
1257                group
1258                (and unread              ; This group might be unchecked
1259                     (or (not (stringp regexp))
1260                         (string-match regexp group))
1261                     (<= (setq clevel (gnus-info-level info)) level)
1262                     (>= clevel lowest)
1263                     (cond
1264                      ((functionp predicate)
1265                       (funcall predicate info))
1266                      (predicate t)      ; We list all groups?
1267                      (t
1268                       (or
1269                        (if (eq unread t) ; Unactivated?
1270                            gnus-group-list-inactive-groups
1271                                         ; We list unactivated
1272                          (> unread 0))
1273                                         ; We list groups with unread articles
1274                        (and gnus-list-groups-with-ticked-articles
1275                             (cdr (assq 'tick (gnus-info-marks info))))
1276                                         ; And groups with tickeds
1277                        ;; Check for permanent visibility.
1278                        (and gnus-permanently-visible-groups
1279                             (string-match gnus-permanently-visible-groups
1280                                           group))
1281                        (memq 'visible params)
1282                        (cdr (assq 'visible params)))))))
1283           (gnus-group-insert-group-line
1284            group (gnus-info-level info)
1285            (gnus-info-marks info) unread (gnus-info-method info)))))
1286
1287     ;; List dead groups.
1288     (when (or gnus-group-listed-groups
1289               (and (>= level gnus-level-zombie)
1290                    (<= lowest gnus-level-zombie)))
1291       (gnus-group-prepare-flat-list-dead
1292        (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
1293        gnus-level-zombie ?Z
1294        regexp))
1295     (when not-in-list
1296       (dolist (group gnus-zombie-list)
1297         (setq not-in-list (delete group not-in-list))))
1298     (when (or gnus-group-listed-groups
1299               (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)))
1300       (gnus-group-prepare-flat-list-dead
1301        (gnus-union
1302         not-in-list
1303         (setq gnus-killed-list (sort gnus-killed-list 'string<)))
1304        gnus-level-killed ?K regexp))
1305
1306     (gnus-group-set-mode-line)
1307     (setq gnus-group-list-mode (cons level predicate))
1308     (gnus-run-hooks 'gnus-group-prepare-hook)
1309     t))
1310
1311 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
1312   ;; List zombies and killed lists somewhat faster, which was
1313   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
1314   ;; this by ignoring the group format specification altogether.
1315   (let (group)
1316     (if (> (length groups) gnus-group-listing-limit)
1317         (while groups
1318           (setq group (pop groups))
1319           (when (gnus-group-prepare-logic
1320                  group
1321                  (or (not regexp)
1322                      (and (stringp regexp) (string-match regexp group))
1323                      (and (functionp regexp) (funcall regexp group))))
1324             (gnus-add-text-properties
1325              (point) (prog1 (1+ (point))
1326                        (insert " " mark "     *: "
1327                                (gnus-group-decoded-name group)
1328                                "\n"))
1329              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
1330                    'gnus-unread t
1331                    'gnus-level level))))
1332       (while groups
1333         (setq group (pop groups))
1334         (when (gnus-group-prepare-logic
1335                group
1336                (or (not regexp)
1337                    (and (stringp regexp) (string-match regexp group))
1338                    (and (functionp regexp) (funcall regexp group))))
1339           (gnus-group-insert-group-line
1340            group level nil
1341            (let ((active (gnus-active group)))
1342              (if active
1343                  (if (zerop (cdr active))
1344                      0
1345                    (- (1+ (cdr active)) (car active)))
1346                nil))
1347            (gnus-method-simplify (gnus-find-method-for-group group))))))))
1348
1349 (defun gnus-group-update-group-line ()
1350   "Update the current line in the group buffer."
1351   (let* ((buffer-read-only nil)
1352          (group (gnus-group-group-name))
1353          (entry (and group (gnus-group-entry group)))
1354          gnus-group-indentation)
1355     (when group
1356       (and entry
1357            (not (gnus-ephemeral-group-p group))
1358            (gnus-dribble-enter
1359             (concat "(gnus-group-set-info '"
1360                     (gnus-prin1-to-string (nth 2 entry))
1361                     ")")))
1362       (setq gnus-group-indentation (gnus-group-group-indentation))
1363       (gnus-delete-line)
1364       (gnus-group-insert-group-line-info group)
1365       (forward-line -1)
1366       (gnus-group-position-point))))
1367
1368 (defun gnus-group-insert-group-line-info (group)
1369   "Insert GROUP on the current line."
1370   (let ((entry (gnus-group-entry group))
1371         (gnus-group-indentation (gnus-group-group-indentation))
1372         active info)
1373     (if entry
1374         (progn
1375           ;; (Un)subscribed group.
1376           (setq info (nth 2 entry))
1377           (gnus-group-insert-group-line
1378            group (gnus-info-level info) (gnus-info-marks info)
1379            (or (car entry) t) (gnus-info-method info)))
1380       ;; This group is dead.
1381       (gnus-group-insert-group-line
1382        group
1383        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
1384        nil
1385        (if (setq active (gnus-active group))
1386            (if (zerop (cdr active))
1387                0
1388              (- (1+ (cdr active)) (car active)))
1389          nil)
1390        (gnus-method-simplify (gnus-find-method-for-group group))))))
1391
1392 (defun gnus-number-of-unseen-articles-in-group (group)
1393   (let* ((info (nth 2 (gnus-group-entry group)))
1394          (marked (gnus-info-marks info))
1395          (seen (cdr (assq 'seen marked)))
1396          (active (gnus-active group)))
1397     (if (not active)
1398         0
1399       (length (gnus-uncompress-range
1400                (gnus-range-difference
1401                 (gnus-range-difference (list active) (gnus-info-read info))
1402                 seen))))))
1403
1404 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level
1405                                                     gnus-tmp-marked number
1406                                                     gnus-tmp-method)
1407   "Insert a group line in the group buffer."
1408   (let* ((gnus-tmp-method
1409           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
1410          (group-name-charset (gnus-group-name-charset gnus-tmp-method
1411                                                       gnus-tmp-group))
1412          (gnus-tmp-active (gnus-active gnus-tmp-group))
1413          (gnus-tmp-number-total
1414           (if gnus-tmp-active
1415               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
1416             0))
1417          (gnus-tmp-number-of-unread
1418           (if (numberp number) (int-to-string (max 0 number))
1419             "*"))
1420          (gnus-tmp-number-of-read
1421           (if (numberp number)
1422               (int-to-string (max 0 (- gnus-tmp-number-total number)))
1423             "*"))
1424          (gnus-tmp-subscribed
1425           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
1426                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
1427                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
1428                 (t ?K)))
1429          (gnus-tmp-qualified-group
1430           (gnus-group-name-decode (gnus-group-real-name gnus-tmp-group)
1431                                   group-name-charset))
1432          (gnus-tmp-comment
1433           (or (gnus-group-get-parameter gnus-tmp-group 'comment t)
1434               gnus-tmp-group))
1435          (gnus-tmp-newsgroup-description
1436           (if gnus-description-hashtb
1437               (or (gnus-group-name-decode
1438                    (gnus-gethash gnus-tmp-group gnus-description-hashtb)
1439                    group-name-charset) "")
1440             ""))
1441          (gnus-tmp-moderated
1442           (if (and gnus-moderated-hashtb
1443                    (gnus-gethash gnus-tmp-group gnus-moderated-hashtb))
1444               ?m ? ))
1445          (gnus-tmp-moderated-string
1446           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
1447          (gnus-tmp-group-icon "==&&==")
1448          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
1449          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
1450          (gnus-tmp-news-method-string
1451           (if gnus-tmp-method
1452               (format "(%s:%s)" (car gnus-tmp-method)
1453                       (cadr gnus-tmp-method)) ""))
1454          (gnus-tmp-marked-mark
1455           (if (and (numberp number)
1456                    (zerop number)
1457                    (cdr (assq 'tick gnus-tmp-marked)))
1458               ?* ? ))
1459          (gnus-tmp-summary-live
1460           (if (and (not gnus-group-is-exiting-p)
1461                    (gnus-buffer-live-p (gnus-summary-buffer-name
1462                                         gnus-tmp-group)))
1463               ?* ? ))
1464          (gnus-tmp-process-marked
1465           (if (member gnus-tmp-group gnus-group-marked)
1466               gnus-process-mark ? ))
1467          (gnus-tmp-grouplens
1468           (or (and gnus-use-grouplens
1469                    (bbb-grouplens-group-p gnus-tmp-group))
1470               ""))
1471          (buffer-read-only nil)
1472          header gnus-tmp-header)        ; passed as parameter to user-funcs.
1473     (beginning-of-line)
1474     (gnus-add-text-properties
1475      (point)
1476      (prog1 (1+ (point))
1477        ;; Insert the text.
1478        (let ((gnus-tmp-group (gnus-group-name-decode
1479                               gnus-tmp-group group-name-charset)))
1480          (eval gnus-group-line-format-spec)))
1481      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
1482                   gnus-unread ,(if (numberp number)
1483                                    (string-to-int gnus-tmp-number-of-unread)
1484                                  t)
1485                   gnus-marked ,gnus-tmp-marked-mark
1486                   gnus-indentation ,gnus-group-indentation
1487                   gnus-level ,gnus-tmp-level))
1488     (forward-line -1)
1489     (when (inline (gnus-visual-p 'group-highlight 'highlight))
1490       (gnus-run-hooks 'gnus-group-update-hook))
1491     (forward-line)
1492     ;; Allow XEmacs to remove front-sticky text properties.
1493     (gnus-group-remove-excess-properties)))
1494
1495 (defun gnus-group-highlight-line ()
1496   "Highlight the current line according to `gnus-group-highlight'."
1497   (let* ((list gnus-group-highlight)
1498          (p (point))
1499          (end (point-at-eol))
1500          ;; now find out where the line starts and leave point there.
1501          (beg (progn (beginning-of-line) (point)))
1502          (group (gnus-group-group-name))
1503          (entry (gnus-group-entry group))
1504          (unread (if (numberp (car entry)) (car entry) 0))
1505          (active (gnus-active group))
1506          (total (if active (1+ (- (cdr active) (car active))) 0))
1507          (info (nth 2 entry))
1508          (method (inline (gnus-server-get-method group (gnus-info-method info))))
1509          (marked (gnus-info-marks info))
1510          (mailp (apply 'append
1511                        (mapcar
1512                         (lambda (x)
1513                           (memq x (assoc (symbol-name
1514                                           (car (or method gnus-select-method)))
1515                                          gnus-valid-select-methods)))
1516                         '(mail post-mail))))
1517          (level (or (gnus-info-level info) gnus-level-killed))
1518          (score (or (gnus-info-score info) 0))
1519          (ticked (gnus-range-length (cdr (assq 'tick marked))))
1520          (group-age (gnus-group-timestamp-delta group))
1521          (inhibit-read-only t))
1522     ;; Eval the cars of the lists until we find a match.
1523     (while (and list
1524                 (not (eval (caar list))))
1525       (setq list (cdr list)))
1526     (let ((face (cdar list)))
1527       (unless (eq face (get-text-property beg 'face))
1528         (gnus-put-text-property-excluding-characters-with-faces
1529          beg end 'face
1530          (setq face (if (boundp face) (symbol-value face) face)))
1531         (gnus-extent-start-open beg)))
1532     (goto-char p)))
1533
1534 (defun gnus-group-update-group (group &optional visible-only)
1535   "Update all lines where GROUP appear.
1536 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
1537 already."
1538   ;; Can't use `save-excursion' here, so we do it manually.
1539   (let ((buf (current-buffer))
1540         mark)
1541     (set-buffer gnus-group-buffer)
1542     (setq mark (point-marker))
1543     ;; The buffer may be narrowed.
1544     (save-restriction
1545       (widen)
1546       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
1547             (loc (point-min))
1548             found buffer-read-only)
1549         ;; Enter the current status into the dribble buffer.
1550         (let ((entry (gnus-group-entry group)))
1551           (when (and entry
1552                      (not (gnus-ephemeral-group-p group)))
1553             (gnus-dribble-enter
1554              (concat "(gnus-group-set-info '"
1555                      (gnus-prin1-to-string (nth 2 entry))
1556                      ")"))))
1557         ;; Find all group instances.  If topics are in use, each group
1558         ;; may be listed in more than once.
1559         (while (setq loc (text-property-any
1560                           loc (point-max) 'gnus-group ident))
1561           (setq found t)
1562           (goto-char loc)
1563           (let ((gnus-group-indentation (gnus-group-group-indentation)))
1564             (gnus-delete-line)
1565             (gnus-group-insert-group-line-info group)
1566             (save-excursion
1567               (forward-line -1)
1568               (gnus-run-hooks 'gnus-group-update-group-hook)))
1569           (setq loc (1+ loc)))
1570         (unless (or found visible-only)
1571           ;; No such line in the buffer, find out where it's supposed to
1572           ;; go, and insert it there (or at the end of the buffer).
1573           (if gnus-goto-missing-group-function
1574               (funcall gnus-goto-missing-group-function group)
1575             (let ((entry (cddr (gnus-group-entry group))))
1576               (while (and entry (car entry)
1577                           (not
1578                            (gnus-goto-char
1579                             (text-property-any
1580                              (point-min) (point-max)
1581                              'gnus-group (gnus-intern-safe
1582                                           (caar entry) gnus-active-hashtb)))))
1583                 (setq entry (cdr entry)))
1584               (or entry (goto-char (point-max)))))
1585           ;; Finally insert the line.
1586           (let ((gnus-group-indentation (gnus-group-group-indentation)))
1587             (gnus-group-insert-group-line-info group)
1588             (save-excursion
1589               (forward-line -1)
1590               (gnus-run-hooks 'gnus-group-update-group-hook))))
1591         (when gnus-group-update-group-function
1592           (funcall gnus-group-update-group-function group))
1593         (gnus-group-set-mode-line)))
1594     (goto-char mark)
1595     (set-marker mark nil)
1596     (set-buffer buf)))
1597
1598 (defun gnus-group-set-mode-line ()
1599   "Update the mode line in the group buffer."
1600   (when (memq 'group gnus-updated-mode-lines)
1601     ;; Yes, we want to keep this mode line updated.
1602     (save-excursion
1603       (set-buffer gnus-group-buffer)
1604       (let* ((gformat (or gnus-group-mode-line-format-spec
1605                           (gnus-set-format 'group-mode)))
1606              (gnus-tmp-news-server (cadr gnus-select-method))
1607              (gnus-tmp-news-method (car gnus-select-method))
1608              (gnus-tmp-colon (if (equal gnus-tmp-news-server "") "" ":"))
1609              (max-len 60)
1610              gnus-tmp-header            ;Dummy binding for user-defined formats
1611              ;; Get the resulting string.
1612              (modified
1613               (and gnus-dribble-buffer
1614                    (buffer-name gnus-dribble-buffer)
1615                    (buffer-modified-p gnus-dribble-buffer)
1616                    (save-excursion
1617                      (set-buffer gnus-dribble-buffer)
1618                      (not (zerop (buffer-size))))))
1619              (mode-string (eval gformat)))
1620         ;; Say whether the dribble buffer has been modified.
1621         (setq mode-line-modified
1622               (if modified (car gnus-mode-line-modified)
1623                 (cdr gnus-mode-line-modified)))
1624         ;; If the line is too long, we chop it off.
1625         (when (> (length mode-string) max-len)
1626           (setq mode-string (substring mode-string 0 (- max-len 4))))
1627         (prog1
1628             (setq mode-line-buffer-identification
1629                   (gnus-mode-line-buffer-identification
1630                    (list mode-string)))
1631           (set-buffer-modified-p modified))))))
1632
1633 (defun gnus-group-group-name ()
1634   "Get the name of the newsgroup on the current line."
1635   (let ((group (get-text-property (point-at-bol) 'gnus-group)))
1636     (when group
1637       (symbol-name group))))
1638
1639 (defun gnus-group-group-level ()
1640   "Get the level of the newsgroup on the current line."
1641   (get-text-property (point-at-bol) 'gnus-level))
1642
1643 (defun gnus-group-group-indentation ()
1644   "Get the indentation of the newsgroup on the current line."
1645   (or (get-text-property (point-at-bol) 'gnus-indentation)
1646       (and gnus-group-indentation-function
1647            (funcall gnus-group-indentation-function))
1648       ""))
1649
1650 (defun gnus-group-group-unread ()
1651   "Get the number of unread articles of the newsgroup on the current line."
1652   (get-text-property (point-at-bol) 'gnus-unread))
1653
1654 (defun gnus-group-new-mail (group)
1655   (if (nnmail-new-mail-p (gnus-group-real-name group))
1656       gnus-new-mail-mark
1657     ? ))
1658
1659 (defun gnus-group-level (group)
1660   "Return the estimated level of GROUP."
1661   (or (gnus-info-level (gnus-get-info group))
1662       (and (member group gnus-zombie-list) gnus-level-zombie)
1663       gnus-level-killed))
1664
1665 (defun gnus-group-search-forward (&optional backward all level first-too)
1666   "Find the next newsgroup with unread articles.
1667 If BACKWARD is non-nil, find the previous newsgroup instead.
1668 If ALL is non-nil, just find any newsgroup.
1669 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
1670 group exists.
1671 If FIRST-TOO, the current line is also eligible as a target."
1672   (let ((way (if backward -1 1))
1673         (low gnus-level-killed)
1674         (beg (point))
1675         pos found lev)
1676     (if (and backward (progn (beginning-of-line)) (bobp))
1677         nil
1678       (unless first-too
1679         (forward-line way))
1680       (while (and
1681               (not (eobp))
1682               (not (setq
1683                     found
1684                     (and
1685                      (get-text-property (point) 'gnus-group)
1686                      (or all
1687                          (and
1688                           (let ((unread
1689                                  (get-text-property (point) 'gnus-unread)))
1690                             (and (numberp unread) (> unread 0)))
1691                           (setq lev (get-text-property (point)
1692                                                        'gnus-level))
1693                           (<= lev gnus-level-subscribed)))
1694                      (or (not level)
1695                          (and (setq lev (get-text-property (point)
1696                                                            'gnus-level))
1697                               (or (= lev level)
1698                                   (and (< lev low)
1699                                        (< level lev)
1700                                        (progn
1701                                          (setq low lev)
1702                                          (setq pos (point))
1703                                          nil))))))))
1704               (zerop (forward-line way)))))
1705     (if found
1706         (progn (gnus-group-position-point) t)
1707       (goto-char (or pos beg))
1708       (and pos t))))
1709
1710 ;;; Gnus group mode commands
1711
1712 ;; Group marking.
1713
1714 (defun gnus-group-mark-line-p ()
1715   (save-excursion
1716     (beginning-of-line)
1717     (forward-char (or (cdr (assq 'process gnus-group-mark-positions)) 2))
1718     (eq (char-after) gnus-process-mark)))
1719
1720 (defun gnus-group-mark-group (n &optional unmark no-advance)
1721   "Mark the current group."
1722   (interactive "p")
1723   (let ((buffer-read-only nil)
1724         group)
1725     (while (and (> n 0)
1726                 (not (eobp)))
1727       (when (setq group (gnus-group-group-name))
1728         ;; Go to the mark position.
1729         (beginning-of-line)
1730         (forward-char (or (cdr (assq 'process gnus-group-mark-positions)) 2))
1731         (delete-char 1)
1732         (if unmark
1733             (progn
1734               (setq gnus-group-marked (delete group gnus-group-marked))
1735               (insert-char ? 1 t))
1736            (setq gnus-group-marked
1737                  (cons group (delete group gnus-group-marked)))
1738            (insert-char gnus-process-mark 1 t)))
1739       (unless no-advance
1740         (gnus-group-next-group 1))
1741       (decf n))
1742     (gnus-summary-position-point)
1743     n))
1744
1745 (defun gnus-group-unmark-group (n)
1746   "Remove the mark from the current group."
1747   (interactive "p")
1748   (gnus-group-mark-group n 'unmark)
1749   (gnus-group-position-point))
1750
1751 (defun gnus-group-unmark-all-groups ()
1752   "Unmark all groups."
1753   (interactive)
1754   (let ((groups gnus-group-marked))
1755     (save-excursion
1756       (while groups
1757         (gnus-group-remove-mark (pop groups)))))
1758   (gnus-group-position-point))
1759
1760 (defun gnus-group-mark-region (unmark beg end)
1761   "Mark all groups between point and mark.
1762 If UNMARK, remove the mark instead."
1763   (interactive "P\nr")
1764   (let ((num (count-lines beg end)))
1765     (save-excursion
1766       (goto-char beg)
1767       (- num (gnus-group-mark-group num unmark)))))
1768
1769 (defun gnus-group-mark-buffer (&optional unmark)
1770   "Mark all groups in the buffer.
1771 If UNMARK, remove the mark instead."
1772   (interactive "P")
1773   (gnus-group-mark-region unmark (point-min) (point-max)))
1774
1775 (defun gnus-group-mark-regexp (regexp)
1776   "Mark all groups that match some regexp."
1777   (interactive "sMark (regexp): ")
1778   (let ((alist (cdr gnus-newsrc-alist))
1779         group)
1780     (save-excursion
1781       (while alist
1782         (when (string-match regexp (setq group (gnus-info-group (pop alist))))
1783           (gnus-group-jump-to-group group)
1784           (gnus-group-set-mark group)))))
1785   (gnus-group-position-point))
1786
1787 (defun gnus-group-remove-mark (group &optional test-marked)
1788   "Remove the process mark from GROUP and move point there.
1789 Return nil if the group isn't displayed."
1790   (if (gnus-group-goto-group group nil test-marked)
1791       (save-excursion
1792         (gnus-group-mark-group 1 'unmark t)
1793         t)
1794     (setq gnus-group-marked
1795           (delete group gnus-group-marked))
1796     nil))
1797
1798 (defun gnus-group-set-mark (group)
1799   "Set the process mark on GROUP."
1800   (if (gnus-group-goto-group group)
1801       (save-excursion
1802         (gnus-group-mark-group 1 nil t))
1803     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
1804
1805 (defun gnus-group-universal-argument (arg &optional groups func)
1806   "Perform any command on all groups according to the process/prefix convention."
1807   (interactive "P")
1808   (if (eq (setq func (or func
1809                          (key-binding
1810                           (read-key-sequence
1811                            (substitute-command-keys
1812                             "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
1813           'undefined)
1814       (gnus-error 1 "Undefined key")
1815     (gnus-group-iterate arg
1816       (lambda (group)
1817         (command-execute func))))
1818   (gnus-group-position-point))
1819
1820 (defun gnus-group-process-prefix (n)
1821   "Return a list of groups to work on.
1822 Take into consideration N (the prefix) and the list of marked groups."
1823   (cond
1824    (n
1825     (setq n (prefix-numeric-value n))
1826     ;; There is a prefix, so we return a list of the N next
1827     ;; groups.
1828     (let ((way (if (< n 0) -1 1))
1829           (n (abs n))
1830           group groups)
1831       (save-excursion
1832         (while (> n 0)
1833           (if (setq group (gnus-group-group-name))
1834               (push group groups))
1835           (setq n (1- n))
1836           (gnus-group-next-group way)))
1837       (nreverse groups)))
1838    ((and (gnus-region-active-p) (mark))
1839     ;; Work on the region between point and mark.
1840     (let ((max (max (point) (mark)))
1841           groups)
1842       (save-excursion
1843         (goto-char (min (point) (mark)))
1844         (while
1845             (and
1846              (push (gnus-group-group-name) groups)
1847              (zerop (gnus-group-next-group 1))
1848              (< (point) max)))
1849         (nreverse groups))))
1850    (gnus-group-marked
1851     ;; No prefix, but a list of marked articles.
1852     (reverse gnus-group-marked))
1853    (t
1854     ;; Neither marked articles or a prefix, so we return the
1855     ;; current group.
1856     (let ((group (gnus-group-group-name)))
1857       (and group (list group))))))
1858
1859 ;;; !!!Surely gnus-group-iterate should be a macro instead?  I can't
1860 ;;; imagine why I went through these contortions...
1861 (eval-and-compile
1862   (let ((function (make-symbol "gnus-group-iterate-function"))
1863         (window (make-symbol "gnus-group-iterate-window"))
1864         (groups (make-symbol "gnus-group-iterate-groups"))
1865         (group (make-symbol "gnus-group-iterate-group")))
1866     (eval
1867      `(defun gnus-group-iterate (arg ,function)
1868         "Iterate FUNCTION over all process/prefixed groups.
1869 FUNCTION will be called with the group name as the parameter
1870 and with point over the group in question."
1871         (let ((,groups (gnus-group-process-prefix arg))
1872               (,window (selected-window))
1873               ,group)
1874           (while ,groups
1875             (setq ,group (car ,groups)
1876                   ,groups (cdr ,groups))
1877             (select-window ,window)
1878             (gnus-group-remove-mark ,group)
1879             (save-selected-window
1880               (save-excursion
1881                 (funcall ,function ,group)))))))))
1882
1883 (put 'gnus-group-iterate 'lisp-indent-function 1)
1884
1885 ;; Selecting groups.
1886
1887 (defun gnus-group-read-group (&optional all no-article group select-articles)
1888   "Read news in this newsgroup.
1889 If the prefix argument ALL is non-nil, already read articles become
1890 readable.  IF ALL is a number, fetch this number of articles.  If the
1891 optional argument NO-ARTICLE is non-nil, no article will be
1892 auto-selected upon group entry.  If GROUP is non-nil, fetch that
1893 group."
1894   (interactive "P")
1895   (let ((no-display (eq all 0))
1896         (group (or group (gnus-group-group-name)))
1897         number active marked entry)
1898     (when (eq all 0)
1899       (setq all nil))
1900     (unless group
1901       (error "No group on current line"))
1902     (setq marked (gnus-info-marks
1903                   (nth 2 (setq entry (gnus-group-entry group)))))
1904     ;; This group might be a dead group.  In that case we have to get
1905     ;; the number of unread articles from `gnus-active-hashtb'.
1906     (setq number
1907           (cond ((numberp all) all)
1908                 (entry (car entry))
1909                 ((setq active (gnus-active group))
1910                  (- (1+ (cdr active)) (car active)))))
1911     (gnus-summary-read-group
1912      group (or all (and (numberp number)
1913                         (zerop (+ number (gnus-range-length
1914                                           (cdr (assq 'tick marked)))
1915                                   (gnus-range-length
1916                                    (cdr (assq 'dormant marked)))))))
1917      no-article nil no-display nil select-articles)))
1918
1919 (defun gnus-group-select-group (&optional all)
1920   "Select this newsgroup.
1921 No article is selected automatically.
1922 If the group is opened, just switch the summary buffer.
1923 If ALL is non-nil, already read articles become readable.
1924 If ALL is a number, fetch this number of articles."
1925   (interactive "P")
1926   (when (and (eobp) (not (gnus-group-group-name)))
1927     (forward-line -1))
1928   (gnus-group-read-group all t))
1929
1930 (defun gnus-group-quick-select-group (&optional all)
1931   "Select the current group \"quickly\".
1932 This means that no highlighting or scoring will be performed.
1933 If ALL (the prefix argument) is 0, don't even generate the summary
1934 buffer.
1935
1936 This might be useful if you want to toggle threading
1937 before entering the group."
1938   (interactive "P")
1939   (require 'gnus-score)
1940   (let (gnus-visual
1941         gnus-score-find-score-files-function
1942         gnus-home-score-file
1943         gnus-apply-kill-hook
1944         gnus-summary-expunge-below)
1945     (gnus-group-read-group all t)))
1946
1947 (defun gnus-group-visible-select-group (&optional all)
1948   "Select the current group without hiding any articles."
1949   (interactive "P")
1950   (let ((gnus-inhibit-limiting t))
1951     (gnus-group-read-group all t)))
1952
1953 (defun gnus-group-select-group-ephemerally ()
1954   "Select the current group without doing any processing whatsoever.
1955 You will actually be entered into a group that's a copy of
1956 the current group; no changes you make while in this group will
1957 be permanent."
1958   (interactive)
1959   (require 'gnus-score)
1960   (let* (gnus-visual
1961          gnus-score-find-score-files-function gnus-apply-kill-hook
1962          gnus-summary-expunge-below gnus-show-threads gnus-suppress-duplicates
1963          gnus-summary-mode-hook gnus-select-group-hook
1964          (group (gnus-group-group-name))
1965          (method (gnus-find-method-for-group group)))
1966     (gnus-group-read-ephemeral-group
1967      (gnus-group-prefixed-name group method) method)))
1968
1969 ;;;###autoload
1970 (defun gnus-fetch-group (group &optional articles)
1971   "Start Gnus if necessary and enter GROUP.
1972 Returns whether the fetching was successful or not."
1973   (interactive (list (gnus-group-completing-read-group-name
1974                       "Group name: " gnus-active-hashtb)))
1975   (unless (get-buffer gnus-group-buffer)
1976     (gnus-no-server))
1977   (gnus-group-read-group articles nil group))
1978
1979 ;;;###autoload
1980 (defun gnus-fetch-group-other-frame (group)
1981   "Pop up a frame and enter GROUP."
1982   (interactive "P")
1983   (let ((window (get-buffer-window gnus-group-buffer)))
1984     (cond (window
1985            (select-frame (window-frame window)))
1986           ((= (length (frame-list)) 1)
1987            (select-frame (make-frame)))
1988           (t
1989            (other-frame 1))))
1990   (gnus-fetch-group group))
1991
1992 (defvar gnus-ephemeral-group-server 0)
1993
1994 (defcustom gnus-large-ephemeral-newsgroup 200
1995   "The number of articles which indicates a large ephemeral newsgroup.
1996 Same as `gnus-large-newsgroup', but only used for ephemeral newsgroups.
1997
1998 If the number of articles in a newsgroup is greater than this value,
1999 confirmation is required for selecting the newsgroup.  If it is nil, no
2000 confirmation is required."
2001   :group 'gnus-group-select
2002   :type '(choice (const :tag "No limit" nil)
2003                  integer))
2004
2005 (defcustom gnus-fetch-old-ephemeral-headers nil
2006   "Same as `gnus-fetch-old-headers', but only used for ephemeral newsgroups."
2007   :group 'gnus-thread
2008   :type '(choice (const :tag "off" nil)
2009                  (const some)
2010                  number
2011                  (sexp :menu-tag "other" t)))
2012
2013 ;; Enter a group that is not in the group buffer.  Non-nil is returned
2014 ;; if selection was successful.
2015 (defun gnus-group-read-ephemeral-group (group method &optional activate
2016                                               quit-config request-only
2017                                               select-articles
2018                                               parameters)
2019   "Read GROUP from METHOD as an ephemeral group.
2020 If ACTIVATE, request the group first.
2021 If QUIT-CONFIG, use that window configuration when exiting from the
2022 ephemeral group.
2023 If REQUEST-ONLY, don't actually read the group; just request it.
2024 If SELECT-ARTICLES, only select those articles.
2025 If PARAMETERS, use those as the group parameters.
2026
2027 Return the name of the group if selection was successful."
2028   (interactive
2029    (list
2030     ;; (gnus-read-group "Group name: ")
2031     (completing-read
2032      "Group: " gnus-active-hashtb
2033      nil nil nil
2034      'gnus-group-history)
2035     (gnus-read-method "From method: ")))
2036   ;; Transform the select method into a unique server.
2037   (when (stringp method)
2038     (setq method (gnus-server-to-method method)))
2039   (setq method
2040         `(,(car method) ,(concat (cadr method) "-ephemeral")
2041           (,(intern (format "%s-address" (car method))) ,(cadr method))
2042           ,@(cddr method)))
2043   (let ((group (if (gnus-group-foreign-p group) group
2044                  (gnus-group-prefixed-name (gnus-group-real-name group)
2045                                            method))))
2046     (gnus-sethash
2047      group
2048      `(-1 nil (,group
2049                ,gnus-level-default-subscribed nil nil ,method
2050                ,(cons
2051                  (if quit-config
2052                      (cons 'quit-config quit-config)
2053                    (cons 'quit-config
2054                          (cons gnus-summary-buffer
2055                                gnus-current-window-configuration)))
2056                  parameters)))
2057      gnus-newsrc-hashtb)
2058     (push method gnus-ephemeral-servers)
2059     (set-buffer gnus-group-buffer)
2060     (unless (gnus-check-server method)
2061       (error "Unable to contact server: %s" (gnus-status-message method)))
2062     (when activate
2063       (gnus-activate-group group 'scan)
2064       (unless (gnus-request-group group)
2065         (error "Couldn't request group: %s"
2066                (nnheader-get-report (car method)))))
2067     (if request-only
2068         group
2069       (condition-case ()
2070           (when (let ((gnus-large-newsgroup gnus-large-ephemeral-newsgroup)
2071                       (gnus-fetch-old-headers
2072                        gnus-fetch-old-ephemeral-headers))
2073                   (gnus-group-read-group t t group select-articles))
2074             group)
2075         ;;(error nil)
2076         (quit
2077          (message "Quit reading the ephemeral group")
2078          nil)))))
2079
2080 (defun gnus-group-jump-to-group (group)
2081   "Jump to newsgroup GROUP."
2082   (interactive
2083    (list (gnus-group-completing-read-group-name
2084           "Group: " gnus-active-hashtb nil
2085           (gnus-read-active-file-p)
2086           gnus-group-jump-to-group-prompt
2087           'gnus-group-history)))
2088
2089   (when (equal group "")
2090     (error "Empty group name"))
2091
2092   (unless (gnus-ephemeral-group-p group)
2093     ;; Either go to the line in the group buffer...
2094     (unless (gnus-group-goto-group group)
2095       ;; ... or insert the line.
2096       (gnus-group-update-group group)
2097       (gnus-group-goto-group group)))
2098   ;; Adjust cursor point.
2099   (gnus-group-position-point))
2100
2101 (defun gnus-group-goto-group (group &optional far test-marked)
2102   "Goto to newsgroup GROUP.
2103 If FAR, it is likely that the group is not on the current line.
2104 If TEST-MARKED, the line must be marked."
2105   (when group
2106     (beginning-of-line)
2107     (cond
2108      ;; It's quite likely that we are on the right line, so
2109      ;; we check the current line first.
2110      ((and (not far)
2111            (eq (get-text-property (point) 'gnus-group)
2112                (gnus-intern-safe group gnus-active-hashtb))
2113            (or (not test-marked) (gnus-group-mark-line-p)))
2114       (point))
2115      ;; Previous and next line are also likely, so we check them as well.
2116      ((and (not far)
2117            (save-excursion
2118              (forward-line -1)
2119              (and (eq (get-text-property (point) 'gnus-group)
2120                       (gnus-intern-safe group gnus-active-hashtb))
2121                   (or (not test-marked) (gnus-group-mark-line-p)))))
2122       (forward-line -1)
2123       (point))
2124      ((and (not far)
2125            (save-excursion
2126              (forward-line 1)
2127              (and (eq (get-text-property (point) 'gnus-group)
2128                       (gnus-intern-safe group gnus-active-hashtb))
2129                   (or (not test-marked) (gnus-group-mark-line-p)))))
2130       (forward-line 1)
2131       (point))
2132      (test-marked
2133       (goto-char (point-min))
2134       (let (found)
2135         (while (and (not found)
2136                     (gnus-goto-char
2137                      (text-property-any
2138                       (point) (point-max)
2139                       'gnus-group
2140                       (gnus-intern-safe group gnus-active-hashtb))))
2141           (if (gnus-group-mark-line-p)
2142               (setq found t)
2143             (forward-line 1)))
2144         found))
2145      (t
2146       ;; Search through the entire buffer.
2147       (gnus-goto-char
2148        (text-property-any
2149         (point-min) (point-max)
2150         'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))))
2151
2152 (defun gnus-group-next-group (n &optional silent)
2153   "Go to next N'th newsgroup.
2154 If N is negative, search backward instead.
2155 Returns the difference between N and the number of skips actually
2156 done."
2157   (interactive "p")
2158   (gnus-group-next-unread-group n t nil silent))
2159
2160 (defun gnus-group-next-unread-group (n &optional all level silent)
2161   "Go to next N'th unread newsgroup.
2162 If N is negative, search backward instead.
2163 If ALL is non-nil, choose any newsgroup, unread or not.
2164 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
2165 such group can be found, the next group with a level higher than
2166 LEVEL.
2167 Returns the difference between N and the number of skips actually
2168 made."
2169   (interactive "p")
2170   (let ((backward (< n 0))
2171         (n (abs n)))
2172     (while (and (> n 0)
2173                 (gnus-group-search-forward
2174                  backward (or (not gnus-group-goto-unread) all) level))
2175       (setq n (1- n)))
2176     (when (and (/= 0 n)
2177                (not silent))
2178       (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
2179                     (if level " on this level or higher" "")))
2180     n))
2181
2182 (defun gnus-group-prev-group (n)
2183   "Go to previous N'th newsgroup.
2184 Returns the difference between N and the number of skips actually
2185 done."
2186   (interactive "p")
2187   (gnus-group-next-unread-group (- n) t))
2188
2189 (defun gnus-group-prev-unread-group (n)
2190   "Go to previous N'th unread newsgroup.
2191 Returns the difference between N and the number of skips actually
2192 done."
2193   (interactive "p")
2194   (gnus-group-next-unread-group (- n)))
2195
2196 (defun gnus-group-next-unread-group-same-level (n)
2197   "Go to next N'th unread newsgroup on the same level.
2198 If N is negative, search backward instead.
2199 Returns the difference between N and the number of skips actually
2200 done."
2201   (interactive "p")
2202   (gnus-group-next-unread-group n t (gnus-group-group-level))
2203   (gnus-group-position-point))
2204
2205 (defun gnus-group-prev-unread-group-same-level (n)
2206   "Go to next N'th unread newsgroup on the same level.
2207 Returns the difference between N and the number of skips actually
2208 done."
2209   (interactive "p")
2210   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
2211   (gnus-group-position-point))
2212
2213 (defun gnus-group-best-unread-group (&optional exclude-group)
2214   "Go to the group with the highest level.
2215 If EXCLUDE-GROUP, do not go to that group."
2216   (interactive)
2217   (goto-char (point-min))
2218   (let ((best 100000)
2219         unread best-point)
2220     (while (not (eobp))
2221       (setq unread (get-text-property (point) 'gnus-unread))
2222       (when (and (numberp unread) (> unread 0))
2223         (when (and (get-text-property (point) 'gnus-level)
2224                    (< (get-text-property (point) 'gnus-level) best)
2225                    (or (not exclude-group)
2226                        (not (equal exclude-group (gnus-group-group-name)))))
2227           (setq best (get-text-property (point) 'gnus-level))
2228           (setq best-point (point))))
2229       (forward-line 1))
2230     (when best-point
2231       (goto-char best-point))
2232     (gnus-group-position-point)
2233     (and best-point (gnus-group-group-name))))
2234
2235 (defun gnus-group-first-unread-group ()
2236   "Go to the first group with unread articles."
2237   (interactive)
2238   (prog1
2239       (let ((opoint (point))
2240             unread)
2241         (goto-char (point-min))
2242         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
2243                 (and (numberp unread)   ; Not a topic.
2244                      (not (zerop unread))) ; Has unread articles.
2245                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
2246             (point)                     ; Success.
2247           (goto-char opoint)
2248           nil))                         ; Not success.
2249     (gnus-group-position-point)))
2250
2251 (defun gnus-group-enter-server-mode ()
2252   "Jump to the server buffer."
2253   (interactive)
2254   (gnus-enter-server-buffer))
2255
2256 (defun gnus-group-make-group (name &optional method address args)
2257   "Add a new newsgroup.
2258 The user will be prompted for a NAME, for a select METHOD, and an
2259 ADDRESS."
2260   (interactive
2261    (list
2262     (gnus-read-group "Group name: ")
2263     (gnus-read-method "From method: ")))
2264
2265   (when (stringp method)
2266     (setq method (or (gnus-server-to-method method) method)))
2267   (let* ((meth (gnus-method-simplify
2268                 (when (and method
2269                            (not (gnus-server-equal method gnus-select-method)))
2270                   (if address (list (intern method) address)
2271                     method))))
2272          (nname (if method (gnus-group-prefixed-name name meth) name))
2273          backend info)
2274     (when (gnus-group-entry nname)
2275       (error "Group %s already exists" nname))
2276     ;; Subscribe to the new group.
2277     (gnus-group-change-level
2278      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
2279      gnus-level-default-subscribed gnus-level-killed
2280      (and (gnus-group-group-name)
2281           (gnus-group-entry (gnus-group-group-name)))
2282      t)
2283     ;; Make it active.
2284     (gnus-set-active nname (cons 1 0))
2285     (unless (gnus-ephemeral-group-p name)
2286       (gnus-dribble-enter
2287        (concat "(gnus-group-set-info '"
2288                (gnus-prin1-to-string (cdr info)) ")")))
2289     ;; Insert the line.
2290     (gnus-group-insert-group-line-info nname)
2291     (forward-line -1)
2292     (gnus-group-position-point)
2293
2294     ;; Load the back end and try to make the back end create
2295     ;; the group as well.
2296     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
2297                                                   nil meth))))
2298                  gnus-valid-select-methods)
2299       (require backend))
2300     (gnus-check-server meth)
2301     (when (gnus-check-backend-function 'request-create-group nname)
2302       (unless (gnus-request-create-group nname nil args)
2303         (error "Could not create group on server: %s"
2304                (nnheader-get-report backend))))
2305     t))
2306
2307 (defun gnus-group-delete-groups (&optional arg)
2308   "Delete the current group.  Only meaningful with editable groups."
2309   (interactive "P")
2310   (let ((n (length (gnus-group-process-prefix arg))))
2311     (when (gnus-yes-or-no-p
2312            (if (= n 1)
2313                "Delete this 1 group? "
2314              (format "Delete these %d groups? " n)))
2315       (gnus-group-iterate arg
2316         (lambda (group)
2317           (gnus-group-delete-group group nil t))))))
2318
2319 (defvar gnus-cache-active-altered)
2320
2321 (defun gnus-group-delete-group (group &optional force no-prompt)
2322   "Delete the current group.  Only meaningful with editable groups.
2323 If FORCE (the prefix) is non-nil, all the articles in the group will
2324 be deleted.  This is \"deleted\" as in \"removed forever from the face
2325 of the Earth\".  There is no undo.  The user will be prompted before
2326 doing the deletion.
2327 Note that you also have to specify FORCE if you want the group to
2328 be removed from the server, even when it's empty."
2329   (interactive
2330    (list (gnus-group-group-name)
2331          current-prefix-arg))
2332   (unless group
2333     (error "No group to delete"))
2334   (unless (gnus-check-backend-function 'request-delete-group group)
2335     (error "This back end does not support group deletion"))
2336   (prog1
2337       (if (and (not no-prompt)
2338                (not (gnus-yes-or-no-p
2339                      (format
2340                       "Do you really want to delete %s%s? "
2341                       group (if force " and all its contents" "")))))
2342           ()                            ; Whew!
2343         (gnus-message 6 "Deleting group %s..." group)
2344         (if (not (gnus-request-delete-group group force))
2345             (gnus-error 3 "Couldn't delete group %s" group)
2346           (gnus-message 6 "Deleting group %s...done" group)
2347           (gnus-group-goto-group group)
2348           (gnus-group-kill-group 1 t)
2349           (gnus-set-active group nil)
2350           (if (boundp 'gnus-cache-active-hashtb)
2351               (when gnus-cache-active-hashtb
2352                 (gnus-sethash group nil gnus-cache-active-hashtb)
2353                 (setq gnus-cache-active-altered t)))
2354           t))
2355     (gnus-group-position-point)))
2356
2357 (defun gnus-group-rename-group (group new-name)
2358   "Rename group from GROUP to NEW-NAME.
2359 When used interactively, GROUP is the group under point
2360 and NEW-NAME will be prompted for."
2361   (interactive
2362    (list
2363     (gnus-group-group-name)
2364     (progn
2365       (unless (gnus-check-backend-function
2366                'request-rename-group (gnus-group-group-name))
2367         (error "This back end does not support renaming groups"))
2368       (gnus-read-group "Rename group to: "
2369                        (gnus-group-real-name (gnus-group-group-name))))))
2370
2371   (unless (gnus-check-backend-function 'request-rename-group group)
2372     (error "This back end does not support renaming groups"))
2373   (unless group
2374     (error "No group to rename"))
2375   (when (equal (gnus-group-real-name group) new-name)
2376     (error "Can't rename to the same name"))
2377
2378   ;; We find the proper prefixed name.
2379   (setq new-name
2380         (if (gnus-group-native-p group)
2381             ;; Native group.
2382             new-name
2383           ;; Foreign group.
2384           (gnus-group-prefixed-name
2385            (gnus-group-real-name new-name)
2386            (gnus-info-method (gnus-get-info group)))))
2387
2388   (when (gnus-active new-name)
2389     (error "The group %s already exists" new-name))
2390
2391   (gnus-message 6 "Renaming group %s to %s..." group new-name)
2392   (prog1
2393       (if (progn
2394             (gnus-group-goto-group group)
2395             (not (when (< (gnus-group-group-level) gnus-level-zombie)
2396                    (gnus-request-rename-group group new-name))))
2397           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
2398         ;; We rename the group internally by killing it...
2399         (gnus-group-kill-group)
2400         ;; ... changing its name ...
2401         (setcar (cdar gnus-list-of-killed-groups) new-name)
2402         ;; ... and then yanking it.  Magic!
2403         (gnus-group-yank-group)
2404         (gnus-set-active new-name (gnus-active group))
2405         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
2406         new-name)
2407     (setq gnus-killed-list (delete group gnus-killed-list))
2408     (gnus-set-active group nil)
2409     (gnus-dribble-touch)
2410     (gnus-group-position-point)))
2411
2412 (defun gnus-group-edit-group (group &optional part)
2413   "Edit the group on the current line."
2414   (interactive (list (gnus-group-group-name)))
2415   (let ((part (or part 'info))
2416         info)
2417     (unless group
2418       (error "No group on current line"))
2419     (unless (setq info (gnus-get-info group))
2420       (error "Killed group; can't be edited"))
2421     (ignore-errors
2422       (gnus-close-group group))
2423     (gnus-edit-form
2424      ;; Find the proper form to edit.
2425      (cond ((eq part 'method)
2426             (or (gnus-info-method info) "native"))
2427            ((eq part 'params)
2428             (gnus-info-params info))
2429            (t info))
2430      ;; The proper documentation.
2431      (format
2432       "Editing the %s for `%s'."
2433       (cond
2434        ((eq part 'method) "select method")
2435        ((eq part 'params) "group parameters")
2436        (t "group info"))
2437       (gnus-group-decoded-name group))
2438      `(lambda (form)
2439         (gnus-group-edit-group-done ',part ,group form)))
2440     (local-set-key
2441      "\C-c\C-i"
2442      (gnus-create-info-command
2443       (format "(%s)%s"
2444               (or gnus-info-filename
2445                   (get-language-info current-language-environment 'gnus-info)
2446                   "gnus")
2447               (cond ((eq part 'method)
2448                      "Select Methods")
2449                     ((eq part 'params)
2450                      "Group Parameters")
2451                     (t
2452                      "Group Info")))))))
2453
2454 (defun gnus-group-edit-group-method (group)
2455   "Edit the select method of GROUP."
2456   (interactive (list (gnus-group-group-name)))
2457   (gnus-group-edit-group group 'method))
2458
2459 (defun gnus-group-edit-group-parameters (group)
2460   "Edit the group parameters of GROUP."
2461   (interactive (list (gnus-group-group-name)))
2462   (gnus-group-edit-group group 'params))
2463
2464 (defun gnus-group-edit-group-done (part group form)
2465   "Update variables."
2466   (let* ((method (cond ((eq part 'info) (nth 4 form))
2467                        ((eq part 'method) form)
2468                        (t nil)))
2469          (info (cond ((eq part 'info) form)
2470                      ((eq part 'method) (gnus-get-info group))
2471                      (t nil)))
2472          (new-group (if info
2473                         (if (or (not method)
2474                                 (gnus-server-equal
2475                                  gnus-select-method method))
2476                             (gnus-group-real-name (car info))
2477                           (gnus-group-prefixed-name
2478                            (gnus-group-real-name (car info)) method))
2479                       nil)))
2480     (when (and new-group
2481                (not (equal new-group group)))
2482       (when (gnus-group-goto-group group)
2483         (gnus-group-kill-group 1))
2484       (gnus-activate-group new-group))
2485     ;; Set the info.
2486     (if (not (and info new-group))
2487         (gnus-group-set-info form (or new-group group) part)
2488       (setq info (gnus-copy-sequence info))
2489       (setcar info new-group)
2490       (unless (gnus-server-equal method "native")
2491         (unless (nthcdr 3 info)
2492           (nconc info (list nil nil)))
2493         (unless (nthcdr 4 info)
2494           (nconc info (list nil)))
2495         (gnus-info-set-method info method))
2496       (gnus-group-set-info info))
2497     (gnus-group-update-group (or new-group group))
2498     (gnus-group-position-point)))
2499
2500 (defun gnus-group-make-useful-group (group method)
2501   "Create one of the groups described in `gnus-useful-groups'."
2502   (interactive
2503    (let ((entry (assoc (completing-read "Create group: " gnus-useful-groups
2504                                         nil t)
2505                        gnus-useful-groups)))
2506      (list (cadr entry) (caddr entry))))
2507   (setq method (gnus-copy-sequence method))
2508   (let (entry)
2509     (while (setq entry (memq (assq 'eval method) method))
2510       (setcar entry (eval (cadar entry)))))
2511   (gnus-group-make-group group method))
2512
2513 (defun gnus-group-make-help-group (&optional noerror)
2514   "Create the Gnus documentation group.
2515 Optional argument NOERROR modifies the behavior of this function when the
2516 group already exists:
2517 - if not given, and error is signaled,
2518 - if t, stay silent,
2519 - if anything else, just print a message."
2520   (interactive)
2521   (let ((name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
2522         (file (nnheader-find-etc-directory "gnus-tut.txt" t)))
2523     (if (gnus-group-entry name)
2524         (cond ((eq noerror nil)
2525                (error "Documentation group already exists"))
2526               ((eq noerror t)
2527                ;; stay silent
2528                )
2529               (t
2530                (gnus-message 1 "Documentation group already exists")))
2531       ;; else:
2532       (if (not file)
2533           (gnus-message 1 "Couldn't find doc group")
2534         (gnus-group-make-group
2535          (gnus-group-real-name name)
2536          (list 'nndoc "gnus-help"
2537                (list 'nndoc-address file)
2538                (list 'nndoc-article-type 'mbox))))
2539       ))
2540   (gnus-group-position-point))
2541
2542 (defun gnus-group-make-doc-group (file type)
2543   "Create a group that uses a single file as the source."
2544   (interactive
2545    (list (read-file-name "File name: ")
2546          (and current-prefix-arg 'ask)))
2547   (when (eq type 'ask)
2548     (let ((err "")
2549           char found)
2550       (while (not found)
2551         (message
2552          "%sFile type (mbox, babyl, digest, forward, mmdf, guess) [mbdfag]: "
2553          err)
2554         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
2555                           ((= char ?b) 'babyl)
2556                           ((= char ?d) 'digest)
2557                           ((= char ?f) 'forward)
2558                           ((= char ?a) 'mmfd)
2559                           ((= char ?g) 'guess)
2560                           (t (setq err (format "%c unknown. " char))
2561                              nil))))
2562       (setq type found)))
2563   (let* ((file (expand-file-name file))
2564          (name (gnus-generate-new-group-name
2565                 (gnus-group-prefixed-name
2566                  (file-name-nondirectory file) '(nndoc "")))))
2567     (gnus-group-make-group
2568      (gnus-group-real-name name)
2569      (list 'nndoc file
2570            (list 'nndoc-address file)
2571            (list 'nndoc-article-type (or type 'guess))))))
2572
2573 (defvar nnweb-type-definition)
2574 (defvar gnus-group-web-type-history nil)
2575 (defvar gnus-group-web-search-history nil)
2576 (defun gnus-group-make-web-group (&optional solid)
2577   "Create an ephemeral nnweb group.
2578 If SOLID (the prefix), create a solid group."
2579   (interactive "P")
2580   (require 'nnweb)
2581   (let* ((group
2582           (if solid (gnus-read-group "Group name: ")
2583             (message-unique-id)))
2584          (default-type (or (car gnus-group-web-type-history)
2585                            (symbol-name (caar nnweb-type-definition))))
2586          (type
2587           (gnus-string-or
2588            (completing-read
2589             (format "Search engine type (default %s): " default-type)
2590             (mapcar (lambda (elem) (list (symbol-name (car elem))))
2591                     nnweb-type-definition)
2592             nil t nil 'gnus-group-web-type-history)
2593            default-type))
2594          (search
2595           (read-string
2596            "Search string: "
2597            (cons (or (car gnus-group-web-search-history) "") 0)
2598            'gnus-group-web-search-history))
2599          (method
2600           `(nnweb ,group (nnweb-search ,search)
2601                   (nnweb-type ,(intern type))
2602                   (nnweb-ephemeral-p t))))
2603     (if solid
2604         (progn
2605           (gnus-pull 'nnweb-ephemeral-p method)
2606           (gnus-group-make-group group method))
2607       (gnus-group-read-ephemeral-group
2608        group method t
2609        (cons (current-buffer)
2610              (if (eq major-mode 'gnus-summary-mode) 'summary 'group))))))
2611
2612 (eval-when-compile
2613   (defvar nnrss-group-alist)
2614   (defun nnrss-discover-feed (arg))
2615   (defun nnrss-save-server-data (arg)))
2616 (defun gnus-group-make-rss-group (&optional url)
2617   "Given a URL, discover if there is an RSS feed.
2618 If there is, use Gnus to create an nnrss group"
2619   (interactive)
2620   (require 'nnrss)
2621   (if (not url)
2622       (setq url (read-from-minibuffer "URL to Search for RSS: ")))
2623   (let ((feedinfo (nnrss-discover-feed url)))
2624     (if feedinfo
2625         (let ((title (read-from-minibuffer "Title: "
2626                                            (cdr (assoc 'title
2627                                                        feedinfo))))
2628               (desc  (read-from-minibuffer "Description: "
2629                                            (cdr (assoc 'description
2630                                                        feedinfo))))
2631               (href (cdr (assoc 'href feedinfo))))
2632           (push (list title href desc)
2633                 nnrss-group-alist)
2634           (gnus-group-unsubscribe-group
2635            (concat "nnrss:" title))
2636           (nnrss-save-server-data nil))
2637       (error "No feeds found for %s" url))))
2638
2639 (defvar nnwarchive-type-definition)
2640 (defvar gnus-group-warchive-type-history nil)
2641 (defvar gnus-group-warchive-login-history nil)
2642 (defvar gnus-group-warchive-address-history nil)
2643
2644 (defun gnus-group-make-warchive-group ()
2645   "Create a nnwarchive group."
2646   (interactive)
2647   (require 'nnwarchive)
2648   (let* ((group (gnus-read-group "Group name: "))
2649          (default-type (or (car gnus-group-warchive-type-history)
2650                            (symbol-name (caar nnwarchive-type-definition))))
2651          (type
2652           (gnus-string-or
2653            (completing-read
2654             (format "Warchive type (default %s): " default-type)
2655             (mapcar (lambda (elem) (list (symbol-name (car elem))))
2656                     nnwarchive-type-definition)
2657             nil t nil 'gnus-group-warchive-type-history)
2658            default-type))
2659          (address (read-string "Warchive address: "
2660                                nil 'gnus-group-warchive-address-history))
2661          (default-login (or (car gnus-group-warchive-login-history)
2662                             user-mail-address))
2663          (login
2664           (gnus-string-or
2665            (read-string
2666             (format "Warchive login (default %s): " user-mail-address)
2667             default-login 'gnus-group-warchive-login-history)
2668            user-mail-address))
2669          (method
2670           `(nnwarchive ,address
2671                        (nnwarchive-type ,(intern type))
2672                        (nnwarchive-login ,login))))
2673     (gnus-group-make-group group method)))
2674
2675 (defun gnus-group-make-archive-group (&optional all)
2676   "Create the (ding) Gnus archive group of the most recent articles.
2677 Given a prefix, create a full group."
2678   (interactive "P")
2679   (let ((group (gnus-group-prefixed-name
2680                 (if all "ding.archives" "ding.recent") '(nndir ""))))
2681     (when (gnus-group-entry group)
2682       (error "Archive group already exists"))
2683     (gnus-group-make-group
2684      (gnus-group-real-name group)
2685      (list 'nndir (if all "hpc" "edu")
2686            (list 'nndir-directory
2687                  (if all gnus-group-archive-directory
2688                    gnus-group-recent-archive-directory))))
2689     (gnus-group-add-parameter group (cons 'to-address "ding@gnus.org"))))
2690
2691 (defun gnus-group-make-directory-group (dir)
2692   "Create an nndir group.
2693 The user will be prompted for a directory.  The contents of this
2694 directory will be used as a newsgroup.  The directory should contain
2695 mail messages or news articles in files that have numeric names."
2696   (interactive
2697    (list (read-file-name "Create group from directory: ")))
2698   (unless (file-exists-p dir)
2699     (error "No such directory"))
2700   (unless (file-directory-p dir)
2701     (error "Not a directory"))
2702   (let ((ext "")
2703         (i 0)
2704         group)
2705     (while (or (not group) (gnus-group-entry group))
2706       (setq group
2707             (gnus-group-prefixed-name
2708              (expand-file-name ext dir)
2709              '(nndir "")))
2710       (setq ext (format "<%d>" (setq i (1+ i)))))
2711     (gnus-group-make-group
2712      (gnus-group-real-name group)
2713      (list 'nndir (gnus-group-real-name group) (list 'nndir-directory dir)))))
2714
2715 (defvar nnkiboze-score-file)
2716 (defun gnus-group-make-kiboze-group (group address scores)
2717   "Create an nnkiboze group.
2718 The user will be prompted for a name, a regexp to match groups, and
2719 score file entries for articles to include in the group."
2720   (interactive
2721    (list
2722     (read-string "nnkiboze group name: ")
2723     (read-string "Source groups (regexp): ")
2724     (let ((headers (mapcar (lambda (group) (list group))
2725                            '("subject" "from" "number" "date" "message-id"
2726                              "references" "chars" "lines" "xref"
2727                              "followup" "all" "body" "head")))
2728           scores header regexp regexps)
2729       (while (not (equal "" (setq header (completing-read
2730                                           "Match on header: " headers nil t))))
2731         (setq regexps nil)
2732         (while (not (equal "" (setq regexp (read-string
2733                                             (format "Match on %s (regexp): "
2734                                                     header)))))
2735           (push (list regexp nil nil 'r) regexps))
2736         (push (cons header regexps) scores))
2737       scores)))
2738   (gnus-group-make-group group "nnkiboze" address)
2739   (let* ((nnkiboze-current-group group)
2740          (score-file (car (nnkiboze-score-file "")))
2741          (score-dir (file-name-directory score-file)))
2742     (unless (file-exists-p score-dir)
2743       (make-directory score-dir))
2744     (with-temp-file score-file
2745       (let (emacs-lisp-mode-hook)
2746         (pp scores (current-buffer))))))
2747
2748 (defun gnus-group-add-to-virtual (n vgroup)
2749   "Add the current group to a virtual group."
2750   (interactive
2751    (list current-prefix-arg
2752          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
2753                           "nnvirtual:")))
2754   (unless (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
2755     (error "%s is not an nnvirtual group" vgroup))
2756   (gnus-close-group vgroup)
2757   (let* ((groups (gnus-group-process-prefix n))
2758          (method (gnus-info-method (gnus-get-info vgroup))))
2759     (setcar (cdr method)
2760             (concat
2761              (nth 1 method) "\\|"
2762              (mapconcat
2763               (lambda (s)
2764                 (gnus-group-remove-mark s)
2765                 (concat "\\(^" (regexp-quote s) "$\\)"))
2766               groups "\\|"))))
2767   (gnus-group-position-point))
2768
2769 (defun gnus-group-make-empty-virtual (group)
2770   "Create a new, fresh, empty virtual group."
2771   (interactive "sCreate new, empty virtual group: ")
2772   (let* ((method (list 'nnvirtual "^$"))
2773          (pgroup (gnus-group-prefixed-name group method)))
2774     ;; Check whether it exists already.
2775     (when (gnus-group-entry pgroup)
2776       (error "Group %s already exists" pgroup))
2777     ;; Subscribe the new group after the group on the current line.
2778     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
2779     (gnus-group-update-group pgroup)
2780     (forward-line -1)
2781     (gnus-group-position-point)))
2782
2783 (defun gnus-group-enter-directory (dir)
2784   "Enter an ephemeral nneething group."
2785   (interactive "DDirectory to read: ")
2786   (let* ((method (list 'nneething dir '(nneething-read-only t)))
2787          (leaf (gnus-group-prefixed-name
2788                 (file-name-nondirectory (directory-file-name dir))
2789                 method))
2790          (name (gnus-generate-new-group-name leaf)))
2791     (unless (gnus-group-read-ephemeral-group
2792              name method t
2793              (cons (current-buffer)
2794                    (if (eq major-mode 'gnus-summary-mode)
2795                        'summary 'group)))
2796       (error "Couldn't enter %s" dir))))
2797
2798 (eval-and-compile
2799   (autoload 'nnimap-expunge "nnimap")
2800   (autoload 'nnimap-acl-get "nnimap")
2801   (autoload 'nnimap-acl-edit "nnimap"))
2802
2803 (defun gnus-group-nnimap-expunge (group)
2804   "Expunge deleted articles in current nnimap GROUP."
2805   (interactive (list (gnus-group-group-name)))
2806   (let ((mailbox (gnus-group-real-name group)) method)
2807     (unless group
2808       (error "No group on current line"))
2809     (unless (gnus-get-info group)
2810       (error "Killed group; can't be edited"))
2811     (unless (eq 'nnimap (car (setq method (gnus-find-method-for-group group))))
2812       (error "%s is not an nnimap group" group))
2813     (nnimap-expunge mailbox (cadr method))))
2814
2815 (defun gnus-group-nnimap-edit-acl (group)
2816   "Edit the Access Control List of current nnimap GROUP."
2817   (interactive (list (gnus-group-group-name)))
2818   (let ((mailbox (gnus-group-real-name group)) method acl)
2819     (unless group
2820       (error "No group on current line"))
2821     (unless (gnus-get-info group)
2822       (error "Killed group; can't be edited"))
2823     (unless (eq (car (setq method (gnus-find-method-for-group group))) 'nnimap)
2824       (error "%s is not an nnimap group" group))
2825     (unless (setq acl (nnimap-acl-get mailbox (cadr method)))
2826       (error "Server does not support ACL's"))
2827     (gnus-edit-form acl (format "Editing the access control list for `%s'.
2828
2829    An access control list is a list of (identifier . rights) elements.
2830
2831    The identifier string specifies the corresponding user.  The
2832    identifier \"anyone\" is reserved to refer to the universal identity.
2833
2834    Rights is a string listing a (possibly empty) set of alphanumeric
2835    characters, each character listing a set of operations which is being
2836    controlled.  Letters are reserved for ``standard'' rights, listed
2837    below.  Digits are reserved for implementation or site defined rights.
2838
2839    l - lookup (mailbox is visible to LIST/LSUB commands)
2840    r - read (SELECT the mailbox, perform CHECK, FETCH, PARTIAL,
2841        SEARCH, COPY from mailbox)
2842    s - keep seen/unseen information across sessions (STORE \\SEEN flag)
2843    w - write (STORE flags other than \\SEEN and \\DELETED)
2844    i - insert (perform APPEND, COPY into mailbox)
2845    p - post (send mail to submission address for mailbox,
2846        not enforced by IMAP4 itself)
2847    c - create and delete mailbox (CREATE new sub-mailboxes in any
2848        implementation-defined hierarchy, RENAME or DELETE mailbox)
2849    d - delete messages (STORE \\DELETED flag, perform EXPUNGE)
2850    a - administer (perform SETACL)" group)
2851                     `(lambda (form)
2852                        (nnimap-acl-edit
2853                         ,mailbox ',method ',acl form)))))
2854
2855 ;; Group sorting commands
2856 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
2857
2858 (defun gnus-group-sort-groups (func &optional reverse)
2859   "Sort the group buffer according to FUNC.
2860 When used interactively, the sorting function used will be
2861 determined by the `gnus-group-sort-function' variable.
2862 If REVERSE (the prefix), reverse the sorting order."
2863   (interactive (list gnus-group-sort-function current-prefix-arg))
2864   (funcall gnus-group-sort-alist-function
2865            (gnus-make-sort-function func) reverse)
2866   (gnus-group-unmark-all-groups)
2867   (gnus-group-list-groups)
2868   (gnus-dribble-touch))
2869
2870 (defun gnus-group-sort-flat (func reverse)
2871   ;; We peel off the dummy group from the alist.
2872   (when func
2873     (when (equal (gnus-info-group (car gnus-newsrc-alist)) "dummy.group")
2874       (pop gnus-newsrc-alist))
2875     ;; Do the sorting.
2876     (setq gnus-newsrc-alist
2877           (sort gnus-newsrc-alist func))
2878     (when reverse
2879       (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
2880     ;; Regenerate the hash table.
2881     (gnus-make-hashtable-from-newsrc-alist)))
2882
2883 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
2884   "Sort the group buffer alphabetically by group name.
2885 If REVERSE, sort in reverse order."
2886   (interactive "P")
2887   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
2888
2889 (defun gnus-group-sort-groups-by-real-name (&optional reverse)
2890   "Sort the group buffer alphabetically by real (unprefixed) group name.
2891 If REVERSE, sort in reverse order."
2892   (interactive "P")
2893   (gnus-group-sort-groups 'gnus-group-sort-by-real-name reverse))
2894
2895 (defun gnus-group-sort-groups-by-unread (&optional reverse)
2896   "Sort the group buffer by number of unread articles.
2897 If REVERSE, sort in reverse order."
2898   (interactive "P")
2899   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
2900
2901 (defun gnus-group-sort-groups-by-level (&optional reverse)
2902   "Sort the group buffer by group level.
2903 If REVERSE, sort in reverse order."
2904   (interactive "P")
2905   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
2906
2907 (defun gnus-group-sort-groups-by-score (&optional reverse)
2908   "Sort the group buffer by group score.
2909 If REVERSE, sort in reverse order."
2910   (interactive "P")
2911   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
2912
2913 (defun gnus-group-sort-groups-by-rank (&optional reverse)
2914   "Sort the group buffer by group rank.
2915 If REVERSE, sort in reverse order."
2916   (interactive "P")
2917   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
2918
2919 (defun gnus-group-sort-groups-by-method (&optional reverse)
2920   "Sort the group buffer alphabetically by back end name.
2921 If REVERSE, sort in reverse order."
2922   (interactive "P")
2923   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
2924
2925 (defun gnus-group-sort-groups-by-server (&optional reverse)
2926   "Sort the group buffer alphabetically by server name.
2927 If REVERSE, sort in reverse order."
2928   (interactive "P")
2929   (gnus-group-sort-groups 'gnus-group-sort-by-server reverse))
2930
2931 ;;; Selected group sorting.
2932
2933 (defun gnus-group-sort-selected-groups (n func &optional reverse)
2934   "Sort the process/prefixed groups."
2935   (interactive (list current-prefix-arg gnus-group-sort-function))
2936   (let ((groups (gnus-group-process-prefix n)))
2937     (funcall gnus-group-sort-selected-function
2938              groups (gnus-make-sort-function func) reverse)
2939     (gnus-group-unmark-all-groups)
2940     (gnus-group-list-groups)
2941     (gnus-dribble-touch)))
2942
2943 (defun gnus-group-sort-selected-flat (groups func reverse)
2944   (let (entries infos)
2945     ;; First find all the group entries for these groups.
2946     (while groups
2947       (push (nthcdr 2 (gnus-group-entry (pop groups)))
2948             entries))
2949     ;; Then sort the infos.
2950     (setq infos
2951           (sort
2952            (mapcar
2953             (lambda (entry) (car entry))
2954             (setq entries (nreverse entries)))
2955            func))
2956     (when reverse
2957       (setq infos (nreverse infos)))
2958     ;; Go through all the infos and replace the old entries
2959     ;; with the new infos.
2960     (while infos
2961       (setcar (car entries) (pop infos))
2962       (pop entries))
2963     ;; Update the hashtable.
2964     (gnus-make-hashtable-from-newsrc-alist)))
2965
2966 (defun gnus-group-sort-selected-groups-by-alphabet (&optional n reverse)
2967   "Sort the group buffer alphabetically by group name.
2968 Obeys the process/prefix convention.  If REVERSE (the symbolic prefix),
2969 sort in reverse order."
2970   (interactive (gnus-interactive "P\ny"))
2971   (gnus-group-sort-selected-groups n 'gnus-group-sort-by-alphabet reverse))
2972
2973 (defun gnus-group-sort-selected-groups-by-real-name (&optional n reverse)
2974   "Sort the group buffer alphabetically by real group name.
2975 Obeys the process/prefix convention.  If REVERSE (the symbolic prefix),
2976 sort in reverse order."
2977   (interactive (gnus-interactive "P\ny"))
2978   (gnus-group-sort-selected-groups n 'gnus-group-sort-by-real-name reverse))
2979
2980 (defun gnus-group-sort-selected-groups-by-unread (&optional n reverse)
2981   "Sort the group buffer by number of unread articles.
2982 Obeys the process/prefix convention.  If REVERSE (the symbolic prefix),
2983 sort in reverse order."
2984   (interactive (gnus-interactive "P\ny"))
2985   (gnus-group-sort-selected-groups n 'gnus-group-sort-by-unread reverse))
2986
2987 (defun gnus-group-sort-selected-groups-by-level (&optional n reverse)
2988   "Sort the group buffer by group level.
2989 Obeys the process/prefix convention.  If REVERSE (the symbolic prefix),
2990 sort in reverse order."
2991   (interactive (gnus-interactive "P\ny"))
2992   (gnus-group-sort-selected-groups n 'gnus-group-sort-by-level reverse))
2993
2994 (defun gnus-group-sort-selected-groups-by-score (&optional n reverse)
2995   "Sort the group buffer by group score.
2996 Obeys the process/prefix convention.  If REVERSE (the symbolic prefix),
2997 sort in reverse order."
2998   (interactive (gnus-interactive "P\ny"))
2999   (gnus-group-sort-selected-groups n 'gnus-group-sort-by-score reverse))
3000
3001 (defun gnus-group-sort-selected-groups-by-rank (&optional n reverse)
3002   "Sort the group buffer by group rank.
3003 Obeys the process/prefix convention.  If REVERSE (the symbolic prefix),
3004 sort in reverse order."
3005   (interactive (gnus-interactive "P\ny"))
3006   (gnus-group-sort-selected-groups n 'gnus-group-sort-by-rank reverse))
3007
3008 (defun gnus-group-sort-selected-groups-by-method (&optional n reverse)
3009   "Sort the group buffer alphabetically by back end name.
3010 Obeys the process/prefix convention.  If REVERSE (the symbolic prefix),
3011 sort in reverse order."
3012   (interactive (gnus-interactive "P\ny"))
3013   (gnus-group-sort-selected-groups n 'gnus-group-sort-by-method reverse))
3014
3015 ;;; Sorting predicates.
3016
3017 (defun gnus-group-sort-by-alphabet (info1 info2)
3018   "Sort alphabetically."
3019   (string< (gnus-info-group info1) (gnus-info-group info2)))
3020
3021 (defun gnus-group-sort-by-real-name (info1 info2)
3022   "Sort alphabetically on real (unprefixed) names."
3023   (string< (gnus-group-real-name (gnus-info-group info1))
3024            (gnus-group-real-name (gnus-info-group info2))))
3025
3026 (defun gnus-group-sort-by-unread (info1 info2)
3027   "Sort by number of unread articles."
3028   (let ((n1 (gnus-group-unread (gnus-info-group info1)))
3029         (n2 (gnus-group-unread (gnus-info-group info1))))
3030     (< (or (and (numberp n1) n1) 0)
3031        (or (and (numberp n2) n2) 0))))
3032
3033 (defun gnus-group-sort-by-level (info1 info2)
3034   "Sort by level."
3035   (< (gnus-info-level info1) (gnus-info-level info2)))
3036
3037 (defun gnus-group-sort-by-method (info1 info2)
3038   "Sort alphabetically by back end name."
3039   (string< (car (gnus-find-method-for-group
3040                  (gnus-info-group info1) info1))
3041            (car (gnus-find-method-for-group
3042                  (gnus-info-group info2) info2))))
3043
3044 (defun gnus-group-sort-by-server (info1 info2)
3045   "Sort alphabetically by server name."
3046   (string< (gnus-method-to-full-server-name
3047             (gnus-find-method-for-group
3048              (gnus-info-group info1) info1))
3049            (gnus-method-to-full-server-name
3050             (gnus-find-method-for-group
3051              (gnus-info-group info2) info2))))
3052
3053 (defun gnus-group-sort-by-score (info1 info2)
3054   "Sort by group score."
3055   (> (gnus-info-score info1) (gnus-info-score info2)))
3056
3057 (defun gnus-group-sort-by-rank (info1 info2)
3058   "Sort by level and score."
3059   (let ((level1 (gnus-info-level info1))
3060         (level2 (gnus-info-level info2)))
3061     (or (< level1 level2)
3062         (and (= level1 level2)
3063              (> (gnus-info-score info1) (gnus-info-score info2))))))
3064
3065 ;;; Clearing data
3066
3067 (defun gnus-group-clear-data (&optional arg)
3068   "Clear all marks and read ranges from the current group."
3069   (interactive "P")
3070   (gnus-group-iterate arg
3071     (lambda (group)
3072       (let (info)
3073         (gnus-info-clear-data (setq info (gnus-get-info group)))
3074         (gnus-get-unread-articles-in-group info (gnus-active group) t)
3075         (when (gnus-group-goto-group group)
3076           (gnus-group-update-group-line))))))
3077
3078 (defun gnus-group-clear-data-on-native-groups ()
3079   "Clear all marks and read ranges from all native groups."
3080   (interactive)
3081   (when (gnus-yes-or-no-p "Really clear all data from almost all groups? ")
3082     (let ((alist (cdr gnus-newsrc-alist))
3083           info)
3084       (while (setq info (pop alist))
3085         (when (gnus-group-native-p (gnus-info-group info))
3086           (gnus-info-clear-data info)))
3087       (gnus-get-unread-articles)
3088       (gnus-dribble-touch)
3089       (when (gnus-y-or-n-p
3090              "Move the cache away to avoid problems in the future? ")
3091         (call-interactively 'gnus-cache-move-cache)))))
3092
3093 (defun gnus-info-clear-data (info)
3094   "Clear all marks and read ranges from INFO."
3095   (let ((group (gnus-info-group info))
3096         action)
3097     (dolist (el (gnus-info-marks info))
3098       (push `(,(cdr el) add (,(car el))) action))
3099     (push `(,(gnus-info-read info) add (read)) action)
3100     (gnus-undo-register
3101       `(progn
3102          (gnus-request-set-mark ,group ',action)
3103          (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
3104          (gnus-info-set-read ',info ',(gnus-info-read info))
3105          (when (gnus-group-goto-group ,group)
3106            (gnus-get-unread-articles-in-group ',info ',(gnus-active group) t)
3107            (gnus-group-update-group-line))))
3108     (setq action (mapcar (lambda (el) (list (nth 0 el) 'del (nth 2 el)))
3109                          action))
3110     (gnus-request-set-mark group action)
3111     (gnus-info-set-read info nil)
3112     (when (gnus-info-marks info)
3113       (gnus-info-set-marks info nil))))
3114
3115 ;; Group catching up.
3116
3117 (defun gnus-group-catchup-current (&optional n all)
3118   "Mark all unread articles in the current newsgroup as read.
3119 If prefix argument N is numeric, the next N newsgroups will be
3120 caught up.  If ALL is non-nil, marked articles will also be marked as
3121 read.  Cross references (Xref: header) of articles are ignored.
3122 The number of newsgroups that this function was unable to catch
3123 up is returned."
3124   (interactive "P")
3125   (let ((groups (gnus-group-process-prefix n))
3126         (ret 0)
3127         group)
3128     (unless groups (error "No groups selected"))
3129     (if (not
3130          (or (not gnus-interactive-catchup) ;Without confirmation?
3131              gnus-expert-user
3132              (gnus-y-or-n-p
3133               (format
3134                (if all
3135                    "Do you really want to mark all articles in %s as read? "
3136                  "Mark all unread articles in %s as read? ")
3137                (if (= (length groups) 1)
3138                    (car groups)
3139                  (format "these %d groups" (length groups)))))))
3140         n
3141       (while (setq group (pop groups))
3142         (gnus-group-remove-mark group)
3143         ;; Virtual groups have to be given special treatment.
3144         (let ((method (gnus-find-method-for-group group)))
3145           (when (eq 'nnvirtual (car method))
3146             (nnvirtual-catchup-group
3147              (gnus-group-real-name group) (nth 1 method) all)))
3148         (if (>= (gnus-group-level group) gnus-level-zombie)
3149             (gnus-message 2 "Dead groups can't be caught up")
3150           (if (prog1
3151                   (gnus-group-goto-group group)
3152                 (gnus-group-catchup group all))
3153               (gnus-group-update-group-line)
3154             (setq ret (1+ ret)))))
3155       (gnus-group-next-unread-group 1)
3156       ret)))
3157
3158 (defun gnus-group-catchup-current-all (&optional n)
3159   "Mark all articles in current newsgroup as read.
3160 Cross references (Xref: header) of articles are ignored."
3161   (interactive "P")
3162   (gnus-group-catchup-current n 'all))
3163
3164 (defun gnus-group-catchup (group &optional all)
3165   "Mark all articles in GROUP as read.
3166 If ALL is non-nil, all articles are marked as read.
3167 The return value is the number of articles that were marked as read,
3168 or nil if no action could be taken."
3169   (let* ((entry (gnus-group-entry group))
3170          (num (car entry))
3171          (marks (gnus-info-marks (nth 2 entry)))
3172          (unread (gnus-sequence-of-unread-articles group)))
3173     ;; Remove entries for this group.
3174     (nnmail-purge-split-history (gnus-group-real-name group))
3175     ;; Do the updating only if the newsgroup isn't killed.
3176     (if (not (numberp (car entry)))
3177         (gnus-message 1 "Can't catch up %s; non-active group" group)
3178       (gnus-update-read-articles group nil)
3179       (when all
3180         ;; Nix out the lists of marks and dormants.
3181         (gnus-request-set-mark group (list (list (cdr (assq 'tick marks))
3182                                                  'del '(tick))
3183                                            (list (cdr (assq 'dormant marks))
3184                                                  'del '(dormant))))
3185         (setq unread (gnus-range-add (gnus-range-add
3186                                       unread (cdr (assq 'dormant marks)))
3187                                      (cdr (assq 'tick marks))))
3188         (gnus-add-marked-articles group 'tick nil nil 'force)
3189         (gnus-add-marked-articles group 'dormant nil nil 'force))
3190       ;; Do auto-expirable marks if that's required.
3191       (when (gnus-group-auto-expirable-p group)
3192         (gnus-range-map (lambda (article)
3193                           (gnus-add-marked-articles group 'expire (list article))
3194                           (gnus-request-set-mark group (list (list (list article) 'add '(expire)))))
3195                         unread))
3196       (let ((gnus-newsgroup-name group))
3197         (gnus-run-hooks 'gnus-group-catchup-group-hook))
3198       num)))
3199
3200 (defun gnus-group-expire-articles (&optional n)
3201   "Expire all expirable articles in the current newsgroup.
3202 Uses the process/prefix convention."
3203   (interactive "P")
3204   (let ((groups (gnus-group-process-prefix n))
3205         group)
3206     (unless groups
3207       (error "No groups to expire"))
3208     (while (setq group (pop groups))
3209       (gnus-group-remove-mark group)
3210       (gnus-group-expire-articles-1 group)
3211       (gnus-dribble-touch)
3212       (gnus-group-position-point))))
3213
3214 (defun gnus-group-expire-articles-1 (group)
3215   (when (gnus-check-backend-function 'request-expire-articles group)
3216     (gnus-message 6 "Expiring articles in %s..." group)
3217     (let* ((info (gnus-get-info group))
3218            (expirable (if (gnus-group-total-expirable-p group)
3219                           (cons nil (gnus-list-of-read-articles group))
3220                         (assq 'expire (gnus-info-marks info))))
3221            (expiry-wait (gnus-group-find-parameter group 'expiry-wait))
3222            (nnmail-expiry-target
3223             (or (gnus-group-find-parameter group 'expiry-target)
3224                 nnmail-expiry-target)))
3225       (when expirable
3226         (gnus-check-group group)
3227         (setcdr
3228          expirable
3229          (gnus-compress-sequence
3230           (if expiry-wait
3231               ;; We set the expiry variables to the group
3232               ;; parameter.
3233               (let ((nnmail-expiry-wait-function nil)
3234                     (nnmail-expiry-wait expiry-wait))
3235                 (gnus-request-expire-articles
3236                  (gnus-uncompress-sequence (cdr expirable)) group))
3237             ;; Just expire using the normal expiry values.
3238             (gnus-request-expire-articles
3239              (gnus-uncompress-sequence (cdr expirable)) group))))
3240         (gnus-close-group group))
3241       (gnus-message 6 "Expiring articles in %s...done" group)
3242       ;; Return the list of un-expired articles.
3243       (cdr expirable))))
3244
3245 (defun gnus-group-expire-all-groups ()
3246   "Expire all expirable articles in all newsgroups."
3247   (interactive)
3248   (save-excursion
3249     (gnus-message 5 "Expiring...")
3250     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
3251                                      (cdr gnus-newsrc-alist))))
3252       (gnus-group-expire-articles nil)))
3253   (gnus-group-position-point)
3254   (gnus-message 5 "Expiring...done"))
3255
3256 (defun gnus-group-set-current-level (n level)
3257   "Set the level of the next N groups to LEVEL."
3258   (interactive
3259    (list
3260     current-prefix-arg
3261     (progn
3262       (unless (gnus-group-process-prefix current-prefix-arg)
3263         (error "No group on the current line"))
3264       (string-to-int
3265        (let ((s (read-string
3266                  (format "Level (default %s): "
3267                          (or (gnus-group-group-level)
3268                              gnus-level-default-subscribed)))))
3269          (if (string-match "^\\s-*$" s)
3270              (int-to-string (or (gnus-group-group-level)
3271                                 gnus-level-default-subscribed))
3272            s))))))
3273   (unless (and (>= level 1) (<= level gnus-level-killed))
3274     (error "Invalid level: %d" level))
3275   (let ((groups (gnus-group-process-prefix n))
3276         group)
3277     (while (setq group (pop groups))
3278       (gnus-group-remove-mark group)
3279       (gnus-message 6 "Changed level of %s from %d to %d"
3280                     group (or (gnus-group-group-level) gnus-level-killed)
3281                     level)
3282       (gnus-group-change-level
3283        group level (or (gnus-group-group-level) gnus-level-killed))
3284       (gnus-group-update-group-line)))
3285   (gnus-group-position-point))
3286
3287 (defun gnus-group-unsubscribe (&optional n)
3288   "Unsubscribe the current group."
3289   (interactive "P")
3290   (gnus-group-unsubscribe-current-group n 'unsubscribe))
3291
3292 (defun gnus-group-subscribe (&optional n)
3293   "Subscribe the current group."
3294   (interactive "P")
3295   (gnus-group-unsubscribe-current-group n 'subscribe))
3296
3297 (defun gnus-group-unsubscribe-current-group (&optional n do-sub)
3298   "Toggle subscription of the current group.
3299 If given numerical prefix, toggle the N next groups."
3300   (interactive "P")
3301   (dolist (group (gnus-group-process-prefix n))
3302     (gnus-group-remove-mark group)
3303     (gnus-group-unsubscribe-group
3304      group
3305      (cond
3306       ((eq do-sub 'unsubscribe)
3307        gnus-level-default-unsubscribed)
3308       ((eq do-sub 'subscribe)
3309        gnus-level-default-subscribed)
3310       ((<= (gnus-group-group-level) gnus-level-subscribed)
3311        gnus-level-default-unsubscribed)
3312       (t
3313        gnus-level-default-subscribed))
3314      t)
3315     (gnus-group-update-group-line))
3316   (gnus-group-next-group 1))
3317
3318 (defun gnus-group-unsubscribe-group (group &optional level silent)
3319   "Toggle subscription to GROUP.
3320 Killed newsgroups are subscribed.  If SILENT, don't try to update the
3321 group line."
3322   (interactive
3323    (list (gnus-group-completing-read-group-name
3324           "Group: " gnus-active-hashtb nil
3325           (gnus-read-active-file-p)
3326           nil
3327           'gnus-group-history)))
3328   (let ((newsrc (gnus-group-entry group)))
3329     (cond
3330      ((string-match "^[ \t]*$" group)
3331       (error "Empty group name"))
3332      (newsrc
3333       ;; Toggle subscription flag.
3334       (gnus-group-change-level
3335        newsrc (if level level (if (<= (gnus-info-level (nth 2 newsrc))
3336                                       gnus-level-subscribed)
3337                                   (1+ gnus-level-subscribed)
3338                                 gnus-level-default-subscribed)))
3339       (unless silent
3340         (gnus-group-update-group group)))
3341      ((and (stringp group)
3342            (or (not (gnus-read-active-file-p))
3343                (gnus-active group)))
3344       ;; Add new newsgroup.
3345       (gnus-group-change-level
3346        group
3347        (if level level gnus-level-default-subscribed)
3348        (or (and (member group gnus-zombie-list)
3349                 gnus-level-zombie)
3350            gnus-level-killed)
3351        (when (gnus-group-group-name)
3352          (gnus-group-entry (gnus-group-group-name))))
3353       (unless silent
3354         (gnus-group-update-group group)))
3355      (t (error "No such newsgroup: %s" group)))
3356     (gnus-group-position-point)))
3357
3358 (defun gnus-group-transpose-groups (n)
3359   "Move the current newsgroup up N places.
3360 If given a negative prefix, move down instead.  The difference between
3361 N and the number of steps taken is returned."
3362   (interactive "p")
3363   (unless (gnus-group-group-name)
3364     (error "No group on current line"))
3365   (gnus-group-kill-group 1)
3366   (prog1
3367       (forward-line (- n))
3368     (gnus-group-yank-group)
3369     (gnus-group-position-point)))
3370
3371 (defun gnus-group-kill-all-zombies (&optional dummy)
3372   "Kill all zombie newsgroups.
3373 The optional DUMMY should always be nil."
3374   (interactive (list (not (gnus-yes-or-no-p "Really kill all zombies? "))))
3375   (unless dummy
3376     (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
3377     (setq gnus-zombie-list nil)
3378     (gnus-dribble-touch)
3379     (gnus-group-list-groups)))
3380
3381 (defun gnus-group-kill-region (begin end)
3382   "Kill newsgroups in current region (excluding current point).
3383 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
3384   (interactive "r")
3385   (let ((lines
3386          ;; Count lines.
3387          (save-excursion
3388            (count-lines
3389             (progn
3390               (goto-char begin)
3391               (beginning-of-line)
3392               (point))
3393             (progn
3394               (goto-char end)
3395               (beginning-of-line)
3396               (point))))))
3397     (goto-char begin)
3398     (beginning-of-line)                 ;Important when LINES < 1
3399     (gnus-group-kill-group lines)))
3400
3401 (defun gnus-group-kill-group (&optional n discard)
3402   "Kill the next N groups.
3403 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
3404 However, only groups that were alive can be yanked; already killed
3405 groups or zombie groups can't be yanked.
3406 The return value is the name of the group that was killed, or a list
3407 of groups killed."
3408   (interactive "P")
3409   (let ((buffer-read-only nil)
3410         (groups (gnus-group-process-prefix n))
3411         group entry level out)
3412     (if (< (length groups) 10)
3413         ;; This is faster when there are few groups.
3414         (while groups
3415           (push (setq group (pop groups)) out)
3416           (gnus-group-remove-mark group)
3417           (setq level (gnus-group-group-level))
3418           (gnus-delete-line)
3419           (when (and (not discard)
3420                      (setq entry (gnus-group-entry group)))
3421             (gnus-undo-register
3422               `(progn
3423                  (gnus-group-goto-group ,(gnus-group-group-name))
3424                  (gnus-group-yank-group)))
3425             (push (cons (car entry) (nth 2 entry))
3426                   gnus-list-of-killed-groups))
3427           (gnus-group-change-level
3428            (if entry entry group) gnus-level-killed (if entry nil level))
3429           (message "Killed group %s" group))
3430       ;; If there are lots and lots of groups to be killed, we use
3431       ;; this thing instead.
3432       (dolist (group (nreverse groups))
3433         (gnus-group-remove-mark group)
3434         (gnus-delete-line)
3435         (push group gnus-killed-list)
3436         (setq gnus-newsrc-alist
3437               (delq (assoc group gnus-newsrc-alist)
3438                     gnus-newsrc-alist))
3439         (when gnus-group-change-level-function
3440           (funcall gnus-group-change-level-function
3441                    group gnus-level-killed 3))
3442         (cond
3443          ((setq entry (gnus-group-entry group))
3444           (push (cons (car entry) (nth 2 entry))
3445                 gnus-list-of-killed-groups)
3446           (setcdr (cdr entry) (cdddr entry)))
3447          ((member group gnus-zombie-list)
3448           (setq gnus-zombie-list (delete group gnus-zombie-list))))
3449         ;; There may be more than one instance displayed.
3450         (while (gnus-group-goto-group group)
3451           (gnus-delete-line)))
3452       (gnus-make-hashtable-from-newsrc-alist))
3453
3454     (gnus-group-position-point)
3455     (if (< (length out) 2) (car out) (nreverse out))))
3456
3457 (defun gnus-group-yank-group (&optional arg)
3458   "Yank the last newsgroups killed with \\[gnus-group-kill-group], inserting it before the current newsgroup.
3459 The numeric ARG specifies how many newsgroups are to be yanked.  The
3460 name of the newsgroup yanked is returned, or (if several groups are
3461 yanked) a list of yanked groups is returned."
3462   (interactive "p")
3463   (setq arg (or arg 1))
3464   (let (info group prev out)
3465     (while (>= (decf arg) 0)
3466       (when (not (setq info (pop gnus-list-of-killed-groups)))
3467         (error "No more newsgroups to yank"))
3468       (push (setq group (nth 1 info)) out)
3469       ;; Find which newsgroup to insert this one before - search
3470       ;; backward until something suitable is found.  If there are no
3471       ;; other newsgroups in this buffer, just make this newsgroup the
3472       ;; first newsgroup.
3473       (setq prev (gnus-group-group-name))
3474       (gnus-group-change-level
3475        info (gnus-info-level (cdr info)) gnus-level-killed
3476        (and prev (gnus-group-entry prev))
3477        t)
3478       (gnus-group-insert-group-line-info group)
3479       (gnus-undo-register
3480         `(when (gnus-group-goto-group ,group)
3481            (gnus-group-kill-group 1))))
3482     (forward-line -1)
3483     (gnus-group-position-point)
3484     (if (< (length out) 2) (car out) (nreverse out))))
3485
3486 (defun gnus-group-kill-level (level)
3487   "Kill all groups that is on a certain LEVEL."
3488   (interactive "nKill all groups on level: ")
3489   (cond
3490    ((= level gnus-level-zombie)
3491     (setq gnus-killed-list
3492           (nconc gnus-zombie-list gnus-killed-list))
3493     (setq gnus-zombie-list nil))
3494    ((and (< level gnus-level-zombie)
3495          (> level 0)
3496          (or gnus-expert-user
3497              (gnus-yes-or-no-p
3498               (format
3499                "Do you really want to kill all groups on level %d? "
3500                level))))
3501     (let* ((prev gnus-newsrc-alist)
3502            (alist (cdr prev)))
3503       (while alist
3504         (if (= (gnus-info-level (car alist)) level)
3505             (progn
3506               (push (gnus-info-group (car alist)) gnus-killed-list)
3507               (setcdr prev (cdr alist)))
3508           (setq prev alist))
3509         (setq alist (cdr alist)))
3510       (gnus-make-hashtable-from-newsrc-alist)
3511       (gnus-group-list-groups)))
3512    (t
3513     (error "Can't kill; invalid level: %d" level))))
3514
3515 (defun gnus-group-list-all-groups (&optional arg)
3516   "List all newsgroups with level ARG or lower.
3517 Default is `gnus-level-unsubscribed', which lists all subscribed and most
3518 unsubscribed groups."
3519   (interactive "P")
3520   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
3521
3522 ;; Redefine this to list ALL killed groups if prefix arg used.
3523 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
3524 (defun gnus-group-list-killed (&optional arg)
3525   "List all killed newsgroups in the group buffer.
3526 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
3527 entail asking the server for the groups."
3528   (interactive "P")
3529   ;; Find all possible killed newsgroups if arg.
3530   (when arg
3531     (gnus-get-killed-groups))
3532   (if (not gnus-killed-list)
3533       (gnus-message 6 "No killed groups")
3534     (let (gnus-group-list-mode)
3535       (funcall gnus-group-prepare-function
3536                gnus-level-killed t gnus-level-killed))
3537     (goto-char (point-min)))
3538   (gnus-group-position-point))
3539
3540 (defun gnus-group-list-zombies ()
3541   "List all zombie newsgroups in the group buffer."
3542   (interactive)
3543   (if (not gnus-zombie-list)
3544       (gnus-message 6 "No zombie groups")
3545     (let (gnus-group-list-mode)
3546       (funcall gnus-group-prepare-function
3547                gnus-level-zombie t gnus-level-zombie))
3548     (goto-char (point-min)))
3549   (gnus-group-position-point))
3550
3551 (defun gnus-group-list-active ()
3552   "List all groups that are available from the server(s)."
3553   (interactive)
3554   ;; First we make sure that we have really read the active file.
3555   (unless (gnus-read-active-file-p)
3556     (let ((gnus-read-active-file t)
3557           (gnus-agent nil))             ; Trick the agent into ignoring the active file.
3558       (gnus-read-active-file)))
3559   ;; Find all groups and sort them.
3560   (let ((groups
3561          (sort
3562           (let (list)
3563             (mapatoms
3564              (lambda (sym)
3565                (and (boundp sym)
3566                     (symbol-value sym)
3567                     (push (symbol-name sym) list)))
3568              gnus-active-hashtb)
3569             list)
3570           'string<))
3571         (buffer-read-only nil)
3572         group)
3573     (erase-buffer)
3574     (while groups
3575       (setq group (pop groups))
3576       (gnus-add-text-properties
3577        (point) (prog1 (1+ (point))
3578                  (insert "       *: "
3579                          (gnus-group-decoded-name group)
3580                          "\n"))
3581        (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
3582              'gnus-unread t
3583              'gnus-level (inline (gnus-group-level group)))))
3584     (goto-char (point-min))))
3585
3586 (defun gnus-activate-all-groups (level)
3587   "Activate absolutely all groups."
3588   (interactive (list gnus-level-unsubscribed))
3589   (let ((gnus-activate-level level)
3590         (gnus-activate-foreign-newsgroups level))
3591     (gnus-group-get-new-news)))
3592
3593 (defun gnus-group-get-new-news (&optional arg)
3594   "Get newly arrived articles.
3595 If ARG is a number, it specifies which levels you are interested in
3596 re-scanning.  If ARG is non-nil and not a number, this will force
3597 \"hard\" re-reading of the active files from all servers."
3598   (interactive "P")
3599   (require 'nnmail)
3600   (let ((gnus-inhibit-demon t)
3601         ;; Binding this variable will inhibit multiple fetchings
3602         ;; of the same mail source.
3603         (nnmail-fetched-sources (list t)))
3604     (gnus-run-hooks 'gnus-get-top-new-news-hook)
3605     (gnus-run-hooks 'gnus-get-new-news-hook)
3606
3607     ;; Read any slave files.
3608     (unless gnus-slave
3609       (gnus-master-read-slave-newsrc))
3610
3611     ;; We might read in new NoCeM messages here.
3612     (when (and gnus-use-nocem
3613                (null arg))
3614       (gnus-nocem-scan-groups))
3615     ;; If ARG is not a number, then we read the active file.
3616     (when (and arg (not (numberp arg)))
3617       (let ((gnus-read-active-file t))
3618         (gnus-read-active-file))
3619       (setq arg nil)
3620
3621       ;; If the user wants it, we scan for new groups.
3622       (when (eq gnus-check-new-newsgroups 'always)
3623         (gnus-find-new-newsgroups)))
3624
3625     (setq arg (gnus-group-default-level arg t))
3626     (if (and gnus-read-active-file (not arg))
3627         (progn
3628           (gnus-read-active-file)
3629           (gnus-get-unread-articles arg))
3630       (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
3631         (gnus-get-unread-articles arg)))
3632     (gnus-run-hooks 'gnus-after-getting-new-news-hook)
3633     (gnus-group-list-groups (and (numberp arg)
3634                                  (max (car gnus-group-list-mode) arg))))
3635   ;; Update modeline.
3636   (when (and gnus-agent (not (interactive-p)))
3637     (gnus-agent-toggle-plugged gnus-plugged)))
3638
3639 (defun gnus-group-get-new-news-this-group (&optional n dont-scan)
3640   "Check for newly arrived news in the current group (and the N-1 next groups).
3641 The difference between N and the number of newsgroup checked is returned.
3642 If N is negative, this group and the N-1 previous groups will be checked."
3643   (interactive "P")
3644   (let* ((groups (gnus-group-process-prefix n))
3645          (ret (if (numberp n) (- n (length groups)) 0))
3646          (beg (unless n
3647                 (point)))
3648          group method
3649          (gnus-inhibit-demon t)
3650          ;; Binding this variable will inhibit multiple fetchings
3651          ;; of the same mail source.
3652          (nnmail-fetched-sources (list t)))
3653     (gnus-run-hooks 'gnus-get-new-news-hook)
3654     (while (setq group (pop groups))
3655       (gnus-group-remove-mark group)
3656       ;; Bypass any previous denials from the server.
3657       (gnus-remove-denial (setq method (gnus-find-method-for-group group)))
3658       (if (gnus-activate-group group (if dont-scan nil 'scan))
3659           (progn
3660             (gnus-get-unread-articles-in-group
3661              (gnus-get-info group) (gnus-active group) t)
3662             (unless (gnus-virtual-group-p group)
3663               (gnus-close-group group))
3664             (when gnus-agent
3665               (gnus-agent-save-group-info
3666                method (gnus-group-real-name group) (gnus-active group)))
3667             (gnus-group-update-group group))
3668         (if (eq (gnus-server-status (gnus-find-method-for-group group))
3669                 'denied)
3670             (gnus-error 3 "Server denied access")
3671           (gnus-error 3 "%s error: %s" group (gnus-status-message group)))))
3672     (when beg
3673       (goto-char beg))
3674     (when gnus-goto-next-group-when-activating
3675       (gnus-group-next-unread-group 1 t))
3676     (gnus-summary-position-point)
3677     ret))
3678
3679 (defun gnus-group-fetch-faq (group &optional faq-dir)
3680   "Fetch the FAQ for the current group.
3681 If given a prefix argument, prompt for the FAQ dir
3682 to use."
3683   (interactive
3684    (list
3685     (gnus-group-group-name)
3686     (when current-prefix-arg
3687       (completing-read
3688        "FAQ dir: " (and (listp gnus-group-faq-directory)
3689                         (mapcar #'list
3690                                 gnus-group-faq-directory))))))
3691   (unless group
3692     (error "No group name given"))
3693   (let ((dirs (or faq-dir gnus-group-faq-directory))
3694         dir found file)
3695     (unless (listp dirs)
3696       (setq dirs (list dirs)))
3697     (while (and (not found)
3698                 (setq dir (pop dirs)))
3699       (let ((name (gnus-group-real-name group)))
3700         (setq file (expand-file-name name dir)))
3701       (if (not (file-exists-p file))
3702           (gnus-message 1 "No such file: %s" file)
3703         (let ((enable-local-variables nil))
3704           (find-file file)
3705           (setq found t))))))
3706
3707 (defun gnus-group-fetch-charter (group)
3708   "Fetch the charter for the current group.
3709 If given a prefix argument, prompt for a group."
3710   (interactive
3711    (list (or (when current-prefix-arg
3712                (completing-read "Group: " gnus-active-hashtb))
3713              (gnus-group-group-name)
3714              gnus-newsgroup-name)))
3715   (unless group
3716     (error "No group name given"))
3717   (require 'mm-url)
3718   (condition-case nil (require 'url-http) (error nil))
3719   (let ((name (mm-url-form-encode-xwfu (gnus-group-real-name group)))
3720         url hierarchy)
3721     (when (string-match "\\(^[^\\.]+\\)\\..*" name)
3722       (setq hierarchy (match-string 1 name))
3723       (if (and (setq url (cdr (assoc hierarchy gnus-group-charter-alist)))
3724                (if (fboundp 'url-http-file-exists-p)
3725                    (url-http-file-exists-p (eval url))
3726                  t))
3727           (browse-url (eval url))
3728         (setq url (concat "http://" hierarchy
3729                           ".news-admin.org/charters/" name))
3730         (if (and (fboundp 'url-http-file-exists-p)
3731                  (url-http-file-exists-p url))
3732             (browse-url url)
3733           (gnus-group-fetch-control group))))))
3734
3735 (defun gnus-group-fetch-control (group)
3736   "Fetch the archived control messages for the current group.
3737 If given a prefix argument, prompt for a group."
3738   (interactive
3739    (list (or (when current-prefix-arg
3740                (completing-read "Group: " gnus-active-hashtb))
3741              (gnus-group-group-name)
3742              gnus-newsgroup-name)))
3743   (unless group
3744     (error "No group name given"))
3745   (let ((name (gnus-group-real-name group))
3746         hierarchy)
3747     (when (string-match "\\(^[^\\.]+\\)\\..*" name)
3748       (setq hierarchy (match-string 1 name))
3749       (if gnus-group-fetch-control-use-browse-url
3750           (browse-url (concat "ftp://ftp.isc.org/usenet/control/"
3751                               hierarchy "/" name ".gz"))
3752         (let ((enable-local-variables nil))
3753           (gnus-group-read-ephemeral-group
3754            group
3755            `(nndoc ,group (nndoc-address
3756                            ,(find-file-noselect
3757                              (concat "/ftp@ftp.isc.org:/usenet/control/"
3758                                      hierarchy "/" name ".gz")))
3759                    (nndoc-article-type mbox)) t nil nil))))))
3760
3761 (defun gnus-group-describe-group (force &optional group)
3762   "Display a description of the current newsgroup."
3763   (interactive (list current-prefix-arg (gnus-group-group-name)))
3764   (let* ((method (gnus-find-method-for-group group))
3765          (mname (gnus-group-prefixed-name "" method))
3766          desc)
3767     (when (and force
3768                gnus-description-hashtb)
3769       (gnus-sethash mname nil gnus-description-hashtb))
3770     (unless group
3771       (error "No group name given"))
3772     (when (or (and gnus-description-hashtb
3773                    ;; We check whether this group's method has been
3774                    ;; queried for a description file.
3775                    (gnus-gethash mname gnus-description-hashtb))
3776               (setq desc (gnus-group-get-description group))
3777               (gnus-read-descriptions-file method))
3778       (gnus-message 1
3779                     (or desc (gnus-gethash group gnus-description-hashtb)
3780                         "No description available")))))
3781
3782 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
3783 (defun gnus-group-describe-all-groups (&optional force)
3784   "Pop up a buffer with descriptions of all newsgroups."
3785   (interactive "P")
3786   (when force
3787     (setq gnus-description-hashtb nil))
3788   (when (not (or gnus-description-hashtb
3789                  (gnus-read-all-descriptions-files)))
3790     (error "Couldn't request descriptions file"))
3791   (let ((buffer-read-only nil)
3792         b)
3793     (erase-buffer)
3794     (mapatoms
3795      (lambda (group)
3796        (setq b (point))
3797        (let ((charset (gnus-group-name-charset nil (symbol-name group))))
3798          (insert (format "      *: %-20s %s\n"
3799                          (gnus-group-name-decode
3800                           (symbol-name group) charset)
3801                          (gnus-group-name-decode
3802                           (symbol-value group) charset))))
3803        (gnus-add-text-properties
3804         b (1+ b) (list 'gnus-group group
3805                        'gnus-unread t 'gnus-marked nil
3806                        'gnus-level (1+ gnus-level-subscribed))))
3807      gnus-description-hashtb)
3808     (goto-char (point-min))
3809     (gnus-group-position-point)))
3810
3811 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
3812 (defun gnus-group-apropos (regexp &optional search-description)
3813   "List all newsgroups that have names that match a regexp."
3814   (interactive "sGnus apropos (regexp): ")
3815   (let ((prev "")
3816         (obuf (current-buffer))
3817         groups des)
3818     ;; Go through all newsgroups that are known to Gnus.
3819     (mapatoms
3820      (lambda (group)
3821        (and (symbol-name group)
3822             (string-match regexp (symbol-name group))
3823             (symbol-value group)
3824             (push (symbol-name group) groups)))
3825      gnus-active-hashtb)
3826     ;; Also go through all descriptions that are known to Gnus.
3827     (when search-description
3828       (mapatoms
3829        (lambda (group)
3830          (and (string-match regexp (symbol-value group))
3831               (push (symbol-name group) groups)))
3832        gnus-description-hashtb))
3833     (if (not groups)
3834         (gnus-message 3 "No groups matched \"%s\"." regexp)
3835       ;; Print out all the groups.
3836       (save-excursion
3837         (pop-to-buffer "*Gnus Help*")
3838         (buffer-disable-undo)
3839         (erase-buffer)
3840         (setq groups (sort groups 'string<))
3841         (while groups
3842           ;; Groups may be entered twice into the list of groups.
3843           (when (not (string= (car groups) prev))
3844             (setq prev (car groups))
3845             (let ((charset (gnus-group-name-charset nil prev)))
3846               (insert (gnus-group-name-decode prev charset) "\n")
3847               (when (and gnus-description-hashtb
3848                          (setq des (gnus-gethash (car groups)
3849                                                  gnus-description-hashtb)))
3850                 (insert "  " (gnus-group-name-decode des charset) "\n"))))
3851           (setq groups (cdr groups)))
3852         (goto-char (point-min))))
3853     (pop-to-buffer obuf)))
3854
3855 (defun gnus-group-description-apropos (regexp)
3856   "List all newsgroups that have names or descriptions that match REGEXP."
3857   (interactive "sGnus description apropos (regexp): ")
3858   (when (not (or gnus-description-hashtb
3859                  (gnus-read-all-descriptions-files)))
3860     (error "Couldn't request descriptions file"))
3861   (gnus-group-apropos regexp t))
3862
3863 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
3864 (defun gnus-group-list-matching (level regexp &optional all lowest)
3865   "List all groups with unread articles that match REGEXP.
3866 If the prefix LEVEL is non-nil, it should be a number that says which
3867 level to cut off listing groups.
3868 If ALL, also list groups with no unread articles.
3869 If LOWEST, don't list groups with level lower than LOWEST.
3870
3871 This command may read the active file."
3872   (interactive "P\nsList newsgroups matching: ")
3873   ;; First make sure active file has been read.
3874   (when (and level
3875              (> (prefix-numeric-value level) gnus-level-killed))
3876     (gnus-get-killed-groups))
3877   (funcall gnus-group-prepare-function
3878            (or level gnus-level-subscribed) (and all t) (or lowest 1) regexp)
3879   (goto-char (point-min))
3880   (gnus-group-position-point))
3881
3882 (defun gnus-group-list-all-matching (level regexp &optional lowest)
3883   "List all groups that match REGEXP.
3884 If the prefix LEVEL is non-nil, it should be a number that says which
3885 level to cut off listing groups.
3886 If LOWEST, don't list groups with level lower than LOWEST."
3887   (interactive "P\nsList newsgroups matching: ")
3888   (when level
3889     (setq level (prefix-numeric-value level)))
3890   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
3891
3892 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
3893 (defun gnus-group-save-newsrc (&optional force)
3894   "Save the Gnus startup files.
3895 If FORCE, force saving whether it is necessary or not."
3896   (interactive "P")
3897   (gnus-save-newsrc-file force))
3898
3899 (defun gnus-group-restart (&optional arg)
3900   "Force Gnus to read the .newsrc file."
3901   (interactive "P")
3902   (when (gnus-yes-or-no-p
3903          (format "Are you sure you want to restart Gnus? "))
3904     (gnus-save-newsrc-file)
3905     (gnus-clear-system)
3906     (gnus)))
3907
3908 (defun gnus-group-read-init-file ()
3909   "Read the Gnus elisp init file."
3910   (interactive)
3911   (gnus-read-init-file)
3912   (gnus-message 5 "Read %s" gnus-init-file))
3913
3914 (defun gnus-group-check-bogus-groups (&optional silent)
3915   "Check bogus newsgroups.
3916 If given a prefix, don't ask for confirmation before removing a bogus
3917 group."
3918   (interactive "P")
3919   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
3920   (gnus-group-list-groups))
3921
3922 (defun gnus-group-find-new-groups (&optional arg)
3923   "Search for new groups and add them.
3924 Each new group will be treated with `gnus-subscribe-newsgroup-method'.
3925 With 1 C-u, use the `ask-server' method to query the server for new
3926 groups.
3927 With 2 C-u's, use most complete method possible to query the server
3928 for new groups, and subscribe the new groups as zombies."
3929   (interactive "p")
3930   (gnus-find-new-newsgroups (or arg 1))
3931   (gnus-group-list-groups))
3932
3933 (defun gnus-group-edit-global-kill (&optional article group)
3934   "Edit the global kill file.
3935 If GROUP, edit that local kill file instead."
3936   (interactive "P")
3937   (setq gnus-current-kill-article article)
3938   (gnus-kill-file-edit-file group)
3939   (gnus-message
3940    6
3941    (substitute-command-keys
3942     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
3943             (if group "local" "global")))))
3944
3945 (defun gnus-group-edit-local-kill (article group)
3946   "Edit a local kill file."
3947   (interactive (list nil (gnus-group-group-name)))
3948   (gnus-group-edit-global-kill article group))
3949
3950 (defun gnus-group-force-update ()
3951   "Update `.newsrc' file."
3952   (interactive)
3953   (gnus-save-newsrc-file))
3954
3955 (defvar gnus-backlog-articles)
3956
3957 (defun gnus-group-suspend ()
3958   "Suspend the current Gnus session.
3959 In fact, cleanup buffers except for group mode buffer.
3960 The hook `gnus-suspend-gnus-hook' is called before actually suspending."
3961   (interactive)
3962   (gnus-run-hooks 'gnus-suspend-gnus-hook)
3963   (gnus-offer-save-summaries)
3964   ;; Kill Gnus buffers except for group mode buffer.
3965   (let ((group-buf (get-buffer gnus-group-buffer)))
3966     (mapcar (lambda (buf)
3967               (unless (or (member buf (list group-buf gnus-dribble-buffer))
3968                           (progn
3969                             (save-excursion
3970                               (set-buffer buf)
3971                               (eq major-mode 'message-mode))))
3972                 (gnus-kill-buffer buf)))
3973             (gnus-buffers))
3974     (setq gnus-backlog-articles nil)
3975     (gnus-kill-gnus-frames)
3976     (when group-buf
3977       (bury-buffer group-buf)
3978       (delete-windows-on group-buf t))))
3979
3980 (defun gnus-group-clear-dribble ()
3981   "Clear all information from the dribble buffer."
3982   (interactive)
3983   (gnus-dribble-clear)
3984   (gnus-message 7 "Cleared dribble buffer"))
3985
3986 (defun gnus-group-exit ()
3987   "Quit reading news after updating .newsrc.eld and .newsrc.
3988 The hook `gnus-exit-gnus-hook' is called before actually exiting."
3989   (interactive)
3990   (when
3991       (or noninteractive                ;For gnus-batch-kill
3992           (not gnus-interactive-exit)   ;Without confirmation
3993           gnus-expert-user
3994           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
3995     (gnus-run-hooks 'gnus-exit-gnus-hook)
3996     ;; Offer to save data from non-quitted summary buffers.
3997     (gnus-offer-save-summaries)
3998     ;; Save the newsrc file(s).
3999     (gnus-save-newsrc-file)
4000     ;; Kill-em-all.
4001     (gnus-close-backends)
4002     ;; Reset everything.
4003     (gnus-clear-system)
4004     ;; Allow the user to do things after cleaning up.
4005     (gnus-run-hooks 'gnus-after-exiting-gnus-hook)))
4006
4007 (defun gnus-group-quit ()
4008   "Quit reading news without updating .newsrc.eld or .newsrc.
4009 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4010   (interactive)
4011   (when (or noninteractive              ;For gnus-batch-kill
4012             (zerop (buffer-size))
4013             (not (gnus-server-opened gnus-select-method))
4014             gnus-expert-user
4015             (not gnus-current-startup-file)
4016             (gnus-yes-or-no-p
4017              (format "Quit reading news without saving %s? "
4018                      (file-name-nondirectory gnus-current-startup-file))))
4019     (gnus-run-hooks 'gnus-exit-gnus-hook)
4020     (gnus-configure-windows 'group t)
4021     (when (and (gnus-buffer-live-p gnus-dribble-buffer)
4022                (not (zerop (save-excursion
4023                              (set-buffer gnus-dribble-buffer)
4024                              (buffer-size)))))
4025       (gnus-dribble-enter
4026        ";;; Gnus was exited on purpose without saving the .newsrc files."))
4027     (gnus-dribble-save)
4028     (gnus-close-backends)
4029     (gnus-clear-system)
4030     (gnus-kill-buffer gnus-group-buffer)
4031     ;; Allow the user to do things after cleaning up.
4032     (gnus-run-hooks 'gnus-after-exiting-gnus-hook)))
4033
4034 (defun gnus-group-describe-briefly ()
4035   "Give a one line description of the group mode commands."
4036   (interactive)
4037   (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")))
4038
4039 (defun gnus-group-browse-foreign-server (method)
4040   "Browse a foreign news server.
4041 If called interactively, this function will ask for a select method
4042  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
4043 If not, METHOD should be a list where the first element is the method
4044 and the second element is the address."
4045   (interactive
4046    (list (let ((how (completing-read
4047                      "Which back end: "
4048                      (append gnus-valid-select-methods gnus-server-alist)
4049                      nil t (cons "nntp" 0) 'gnus-method-history)))
4050            ;; We either got a back end name or a virtual server name.
4051            ;; If the first, we also need an address.
4052            (if (assoc how gnus-valid-select-methods)
4053                (list (intern how)
4054                      ;; Suggested by mapjph@bath.ac.uk.
4055                      (completing-read
4056                       "Address: "
4057                       (mapcar (lambda (server) (list server))
4058                               gnus-secondary-servers)))
4059              ;; We got a server name.
4060              how))))
4061   (gnus-browse-foreign-server method))
4062
4063 (defun gnus-group-set-info (info &optional method-only-group part)
4064   (when (or info part)
4065     (let* ((entry (gnus-group-entry
4066                    (or method-only-group (gnus-info-group info))))
4067            (part-info info)
4068            (info (if method-only-group (nth 2 entry) info))
4069            method)
4070       (when method-only-group
4071         (unless entry
4072           (error "Trying to change non-existent group %s" method-only-group))
4073         ;; We have received parts of the actual group info - either the
4074         ;; select method or the group parameters.        We first check
4075         ;; whether we have to extend the info, and if so, do that.
4076         (let ((len (length info))
4077               (total (if (eq part 'method) 5 6)))
4078           (when (< len total)
4079             (setcdr (nthcdr (1- len) info)
4080                     (make-list (- total len) nil)))
4081           ;; Then we enter the new info.
4082           (setcar (nthcdr (1- total) info) part-info)))
4083       (unless entry
4084         ;; This is a new group, so we just create it.
4085         (save-excursion
4086           (set-buffer gnus-group-buffer)
4087           (setq method (gnus-info-method info))
4088           (when (gnus-server-equal method "native")
4089             (setq method nil))
4090           (save-excursion
4091             (set-buffer gnus-group-buffer)
4092             (if method
4093                 ;; It's a foreign group...
4094                 (gnus-group-make-group
4095                  (gnus-group-real-name (gnus-info-group info))
4096                  (if (stringp method) method
4097                    (prin1-to-string (car method)))
4098                  (and (consp method)
4099                       (nth 1 (gnus-info-method info))))
4100               ;; It's a native group.
4101               (gnus-group-make-group (gnus-info-group info))))
4102           (gnus-message 6 "Note: New group created")
4103           (setq entry
4104                 (gnus-group-entry (gnus-group-prefixed-name
4105                                    (gnus-group-real-name (gnus-info-group info))
4106                                    (or (gnus-info-method info) gnus-select-method))))))
4107       ;; Whether it was a new group or not, we now have the entry, so we
4108       ;; can do the update.
4109       (if entry
4110           (progn
4111             (setcar (nthcdr 2 entry) info)
4112             (when (and (not (eq (car entry) t))
4113                        (gnus-active (gnus-info-group info)))
4114               (setcar entry (length
4115                              (gnus-list-of-unread-articles (car info))))))
4116         (error "No such group: %s" (gnus-info-group info))))))
4117
4118 (defun gnus-group-set-method-info (group select-method)
4119   (gnus-group-set-info select-method group 'method))
4120
4121 (defun gnus-group-set-params-info (group params)
4122   (gnus-group-set-info params group 'params))
4123
4124 (defun gnus-add-marked-articles (group type articles &optional info force)
4125   ;; Add ARTICLES of TYPE to the info of GROUP.
4126   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
4127   ;; add, but replace marked articles of TYPE with ARTICLES.
4128   (let ((info (or info (gnus-get-info group)))
4129         marked m)
4130     (or (not info)
4131         (and (not (setq marked (nthcdr 3 info)))
4132              (or (null articles)
4133                  (setcdr (nthcdr 2 info)
4134                          (list (list (cons type (gnus-compress-sequence
4135                                                  articles t)))))))
4136         (and (not (setq m (assq type (car marked))))
4137              (or (null articles)
4138                  (setcar marked
4139                          (cons (cons type (gnus-compress-sequence articles t) )
4140                                (car marked)))))
4141         (if force
4142             (if (null articles)
4143                 (setcar (nthcdr 3 info)
4144                         (gnus-delete-alist type (car marked)))
4145               (setcdr m (gnus-compress-sequence articles t)))
4146           (setcdr m (gnus-compress-sequence
4147                      (sort (nconc (gnus-uncompress-range (cdr m))
4148                                   (copy-sequence articles)) '<) t))))))
4149
4150 (defun gnus-add-mark (group mark article)
4151   "Mark ARTICLE in GROUP with MARK, whether the group is displayed or not."
4152   (let ((buffer (gnus-summary-buffer-name group)))
4153     (if (gnus-buffer-live-p buffer)
4154         (save-excursion
4155           (set-buffer (get-buffer buffer))
4156           (gnus-summary-add-mark article mark))
4157       (gnus-add-marked-articles group (cdr (assq mark gnus-article-mark-lists))
4158                                 (list article)))))
4159
4160 ;;;
4161 ;;; Group timestamps
4162 ;;;
4163
4164 (defun gnus-group-set-timestamp ()
4165   "Change the timestamp of the current group to the current time.
4166 This function can be used in hooks like `gnus-select-group-hook'
4167 or `gnus-group-catchup-group-hook'."
4168   (when gnus-newsgroup-name
4169     (let ((time (current-time)))
4170       (setcdr (cdr time) nil)
4171       (gnus-group-set-parameter gnus-newsgroup-name 'timestamp time))))
4172
4173 (defsubst gnus-group-timestamp (group)
4174   "Return the timestamp for GROUP."
4175   (gnus-group-get-parameter group 'timestamp t))
4176
4177 (defun gnus-group-timestamp-delta (group)
4178   "Return the offset in seconds from the timestamp for GROUP to the current time, as a floating point number."
4179   (let* ((time (or (gnus-group-timestamp group)
4180                    (list 0 0)))
4181          (delta (subtract-time (current-time) time)))
4182     (+ (* (nth 0 delta) 65536.0)
4183        (nth 1 delta))))
4184
4185 (defun gnus-group-timestamp-string (group)
4186   "Return a string of the timestamp for GROUP."
4187   (let ((time (gnus-group-timestamp group)))
4188     (if (not time)
4189         ""
4190       (gnus-time-iso8601 time))))
4191
4192 (defun gnus-group-list-cached (level &optional lowest)
4193   "List all groups with cached articles.
4194 If the prefix LEVEL is non-nil, it should be a number that says which
4195 level to cut off listing groups.
4196 If LOWEST, don't list groups with level lower than LOWEST.
4197
4198 This command may read the active file."
4199   (interactive "P")
4200   (when level
4201     (setq level (prefix-numeric-value level)))
4202   (when (or (not level) (>= level gnus-level-zombie))
4203     (gnus-cache-open))
4204   (funcall gnus-group-prepare-function
4205            (or level gnus-level-subscribed)
4206            #'(lambda (info)
4207                (let ((marks (gnus-info-marks info)))
4208                  (assq 'cache marks)))
4209            lowest
4210            #'(lambda (group)
4211                (or (gnus-gethash group
4212                                  gnus-cache-active-hashtb)
4213                    ;; Cache active file might use "."
4214                    ;; instead of ":".
4215                    (gnus-gethash
4216                     (mapconcat 'identity
4217                                (split-string group ":")
4218                                ".")
4219                     gnus-cache-active-hashtb))))
4220   (goto-char (point-min))
4221   (gnus-group-position-point))
4222
4223 (defun gnus-group-list-dormant (level &optional lowest)
4224   "List all groups with dormant articles.
4225 If the prefix LEVEL is non-nil, it should be a number that says which
4226 level to cut off listing groups.
4227 If LOWEST, don't list groups with level lower than LOWEST.
4228
4229 This command may read the active file."
4230   (interactive "P")
4231   (when level
4232     (setq level (prefix-numeric-value level)))
4233   (when (or (not level) (>= level gnus-level-zombie))
4234     (gnus-cache-open))
4235   (funcall gnus-group-prepare-function
4236            (or level gnus-level-subscribed)
4237            #'(lambda (info)
4238                (let ((marks (gnus-info-marks info)))
4239                  (assq 'dormant marks)))
4240            lowest
4241            'ignore)
4242   (goto-char (point-min))
4243   (gnus-group-position-point))
4244
4245 (defun gnus-group-listed-groups ()
4246   "Return a list of listed groups."
4247   (let (point groups)
4248     (goto-char (point-min))
4249     (while (setq point (text-property-not-all (point) (point-max)
4250                                               'gnus-group nil))
4251       (goto-char point)
4252       (push (symbol-name (get-text-property point 'gnus-group)) groups)
4253       (forward-char 1))
4254     groups))
4255
4256 (defun gnus-group-list-plus (&optional args)
4257   "List groups plus the current selection."
4258   (interactive "P")
4259   (let ((gnus-group-listed-groups (gnus-group-listed-groups))
4260         (gnus-group-list-mode gnus-group-list-mode) ;; Save it.
4261         func)
4262     (push last-command-event unread-command-events)
4263     (if (featurep 'xemacs)
4264         (push (make-event 'key-press '(key ?A)) unread-command-events)
4265       (push ?A unread-command-events))
4266     (let (gnus-pick-mode keys)
4267       (setq keys (if (featurep 'xemacs)
4268                      (events-to-keys (read-key-sequence nil))
4269                    (read-key-sequence nil)))
4270       (setq func (lookup-key (current-local-map) keys)))
4271     (if (or (not func)
4272             (numberp func))
4273         (ding)
4274       (call-interactively func))))
4275
4276 (defun gnus-group-list-flush (&optional args)
4277   "Flush groups from the current selection."
4278   (interactive "P")
4279   (let ((gnus-group-list-option 'flush))
4280     (gnus-group-list-plus args)))
4281
4282 (defun gnus-group-list-limit (&optional args)
4283   "List groups limited within the current selection."
4284   (interactive "P")
4285   (let ((gnus-group-list-option 'limit))
4286     (gnus-group-list-plus args)))
4287
4288 (defun gnus-group-mark-article-read (group article)
4289   "Mark ARTICLE read."
4290   (let ((buffer (gnus-summary-buffer-name group))
4291         (mark gnus-read-mark)
4292         active n)
4293     (if (get-buffer buffer)
4294         (with-current-buffer buffer
4295           (setq active gnus-newsgroup-active)
4296           (gnus-activate-group group)
4297           (when gnus-newsgroup-prepared
4298             (when (and gnus-newsgroup-auto-expire
4299                        (memq mark gnus-auto-expirable-marks))
4300               (setq mark gnus-expirable-mark))
4301             (setq mark (gnus-request-update-mark
4302                         group article mark))
4303             (gnus-mark-article-as-read article mark)
4304             (setq gnus-newsgroup-active (gnus-active group))
4305             (when active
4306               (setq n (1+ (cdr active)))
4307               (while (<= n (cdr gnus-newsgroup-active))
4308                 (unless (eq n article)
4309                   (push n gnus-newsgroup-unselected))
4310                 (setq n (1+ n)))
4311               (setq gnus-newsgroup-unselected
4312                     (nreverse gnus-newsgroup-unselected)))))
4313       (gnus-activate-group group)
4314       (gnus-group-make-articles-read group (list article))
4315       (when (gnus-group-auto-expirable-p group)
4316         (gnus-add-marked-articles
4317          group 'expire (list article))))))
4318
4319 (provide 'gnus-group)
4320
4321 ;;; gnus-group.el ends here