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