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