(gnus-group-make-rss-group): Use gnus-group-make-group instead of
[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
1071                                        (mm-string-as-multibyte "\200") nil t)
1072                                       (- (point) 2))))))))
1073
1074 (defun gnus-mouse-pick-group (e)
1075   "Enter the group under the mouse pointer."
1076   (interactive "e")
1077   (mouse-set-point e)
1078   (gnus-group-read-group nil))
1079
1080 ;; Look at LEVEL and find out what the level is really supposed to be.
1081 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
1082 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
1083 (defun gnus-group-default-level (&optional level number-or-nil)
1084   (cond
1085    (gnus-group-use-permanent-levels
1086     (or (setq gnus-group-use-permanent-levels
1087               (or level (if (numberp gnus-group-use-permanent-levels)
1088                             gnus-group-use-permanent-levels
1089                           (or gnus-group-default-list-level
1090                               gnus-level-subscribed))))
1091         gnus-group-default-list-level gnus-level-subscribed))
1092    (number-or-nil
1093     level)
1094    (t
1095     (or level gnus-group-default-list-level gnus-level-subscribed))))
1096
1097 (defun gnus-group-setup-buffer ()
1098   (set-buffer (gnus-get-buffer-create gnus-group-buffer))
1099   (unless (eq major-mode 'gnus-group-mode)
1100     (gnus-group-mode)
1101     (when gnus-carpal
1102       (gnus-carpal-setup-buffer 'group))))
1103
1104 (defun gnus-group-name-charset (method group)
1105   (if (null method)
1106       (setq method (gnus-find-method-for-group group)))
1107   (let ((item (assoc method gnus-group-name-charset-method-alist))
1108         (alist gnus-group-name-charset-group-alist)
1109         result)
1110     (if item
1111         (cdr item)
1112       (while (setq item (pop alist))
1113         (if (string-match (car item) group)
1114             (setq alist nil
1115                   result (cdr item))))
1116       result)))
1117
1118 (defun gnus-group-name-decode (string charset)
1119   ;; Fixme: Don't decode in unibyte mode.
1120   (if (and string charset (featurep 'mule))
1121       (mm-decode-coding-string string charset)
1122     string))
1123
1124 (defun gnus-group-decoded-name (string)
1125   (let ((charset (gnus-group-name-charset nil string)))
1126     (gnus-group-name-decode string charset)))
1127
1128 (defun gnus-group-list-groups (&optional level unread lowest)
1129   "List newsgroups with level LEVEL or lower that have unread articles.
1130 Default is all subscribed groups.
1131 If argument UNREAD is non-nil, groups with no unread articles are also
1132 listed.
1133
1134 Also see the `gnus-group-use-permanent-levels' variable."
1135   (interactive
1136    (list (if current-prefix-arg
1137              (prefix-numeric-value current-prefix-arg)
1138            (or
1139             (gnus-group-default-level nil t)
1140             gnus-group-default-list-level
1141             gnus-level-subscribed))))
1142   (unless level
1143     (setq level (car gnus-group-list-mode)
1144           unread (cdr gnus-group-list-mode)))
1145   (setq level (gnus-group-default-level level))
1146   (gnus-group-setup-buffer)
1147   (gnus-update-format-specifications nil 'group 'group-mode)
1148   (let ((case-fold-search nil)
1149         (props (text-properties-at (point-at-bol)))
1150         (empty (= (point-min) (point-max)))
1151         (group (gnus-group-group-name))
1152         number)
1153     (set-buffer gnus-group-buffer)
1154     (setq number (funcall gnus-group-prepare-function level unread lowest))
1155     (when (or (and (numberp number)
1156                    (zerop number))
1157               (zerop (buffer-size)))
1158       ;; No groups in the buffer.
1159       (gnus-message 5 gnus-no-groups-message))
1160     ;; We have some groups displayed.
1161     (goto-char (point-max))
1162     (when (or (not gnus-group-goto-next-group-function)
1163               (not (funcall gnus-group-goto-next-group-function
1164                             group props)))
1165       (cond
1166        (empty
1167         (goto-char (point-min)))
1168        ((not group)
1169         ;; Go to the first group with unread articles.
1170         (gnus-group-search-forward t))
1171        (t
1172         ;; Find the right group to put point on.  If the current group
1173         ;; has disappeared in the new listing, try to find the next
1174         ;; one.  If no next one can be found, just leave point at the
1175         ;; first newsgroup in the buffer.
1176         (when (not (gnus-goto-char
1177                     (text-property-any
1178                      (point-min) (point-max)
1179                      'gnus-group (gnus-intern-safe
1180                                   group gnus-active-hashtb))))
1181           (let ((newsrc (cdddr (gnus-group-entry group))))
1182             (while (and newsrc
1183                         (not (gnus-goto-char
1184                               (text-property-any
1185                                (point-min) (point-max) 'gnus-group
1186                                (gnus-intern-safe
1187                                 (caar newsrc) gnus-active-hashtb)))))
1188               (setq newsrc (cdr newsrc)))
1189             (unless newsrc
1190               (goto-char (point-max))
1191               (forward-line -1)))))))
1192     ;; Adjust cursor point.
1193     (gnus-group-position-point)))
1194
1195 (defun gnus-group-list-level (level &optional all)
1196   "List groups on LEVEL.
1197 If ALL (the prefix), also list groups that have no unread articles."
1198   (interactive "nList groups on level: \nP")
1199   (gnus-group-list-groups level all level))
1200
1201 (defun gnus-group-prepare-logic (group test)
1202   (or (and gnus-group-listed-groups
1203            (null gnus-group-list-option)
1204            (member group gnus-group-listed-groups))
1205       (cond
1206        ((null gnus-group-listed-groups) test)
1207        ((null gnus-group-list-option) test)
1208        (t (and (member group gnus-group-listed-groups)
1209                (if (eq gnus-group-list-option 'flush)
1210                    (not test)
1211                  test))))))
1212
1213 (defun gnus-group-prepare-flat (level &optional predicate lowest regexp)
1214   "List all newsgroups with unread articles of level LEVEL or lower.
1215 If PREDICATE is a function, list groups that the function returns non-nil;
1216 if it is t, list groups that have no unread articles.
1217 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
1218 If REGEXP is a function, list dead groups that the function returns non-nil;
1219 if it is a string, only list groups matching REGEXP."
1220   (set-buffer gnus-group-buffer)
1221   (let ((buffer-read-only nil)
1222         (newsrc (cdr gnus-newsrc-alist))
1223         (lowest (or lowest 1))
1224         (not-in-list (and gnus-group-listed-groups
1225                           (copy-sequence gnus-group-listed-groups)))
1226         info clevel unread group params)
1227     (erase-buffer)
1228     (when (or (< lowest gnus-level-zombie)
1229               gnus-group-listed-groups)
1230       ;; List living groups.
1231       (while newsrc
1232         (setq info (car newsrc)
1233               group (gnus-info-group info)
1234               params (gnus-info-params info)
1235               newsrc (cdr newsrc)
1236               unread (gnus-group-unread group))
1237         (when not-in-list
1238           (setq not-in-list (delete group not-in-list)))
1239         (when (gnus-group-prepare-logic
1240                group
1241                (and unread              ; This group might be unchecked
1242                     (or (not (stringp regexp))
1243                         (string-match regexp group))
1244                     (<= (setq clevel (gnus-info-level info)) level)
1245                     (>= clevel lowest)
1246                     (cond
1247                      ((functionp predicate)
1248                       (funcall predicate info))
1249                      (predicate t)      ; We list all groups?
1250                      (t
1251                       (or
1252                        (if (eq unread t) ; Unactivated?
1253                            gnus-group-list-inactive-groups
1254                                         ; We list unactivated
1255                          (> unread 0))
1256                                         ; We list groups with unread articles
1257                        (and gnus-list-groups-with-ticked-articles
1258                             (cdr (assq 'tick (gnus-info-marks info))))
1259                                         ; And groups with tickeds
1260                        ;; Check for permanent visibility.
1261                        (and gnus-permanently-visible-groups
1262                             (string-match gnus-permanently-visible-groups
1263                                           group))
1264                        (memq 'visible params)
1265                        (cdr (assq 'visible params)))))))
1266           (gnus-group-insert-group-line
1267            group (gnus-info-level info)
1268            (gnus-info-marks info) unread (gnus-info-method info)))))
1269
1270     ;; List dead groups.
1271     (when (or gnus-group-listed-groups
1272               (and (>= level gnus-level-zombie)
1273                    (<= lowest gnus-level-zombie)))
1274       (gnus-group-prepare-flat-list-dead
1275        (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
1276        gnus-level-zombie ?Z
1277        regexp))
1278     (when not-in-list
1279       (dolist (group gnus-zombie-list)
1280         (setq not-in-list (delete group not-in-list))))
1281     (when (or gnus-group-listed-groups
1282               (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)))
1283       (gnus-group-prepare-flat-list-dead
1284        (gnus-union
1285         not-in-list
1286         (setq gnus-killed-list (sort gnus-killed-list 'string<)))
1287        gnus-level-killed ?K regexp))
1288
1289     (gnus-group-set-mode-line)
1290     (setq gnus-group-list-mode (cons level predicate))
1291     (gnus-run-hooks 'gnus-group-prepare-hook)
1292     t))
1293
1294 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
1295   ;; List zombies and killed lists somewhat faster, which was
1296   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
1297   ;; this by ignoring the group format specification altogether.
1298   (let (group)
1299     (if (> (length groups) gnus-group-listing-limit)
1300         (while groups
1301           (setq group (pop groups))
1302           (when (gnus-group-prepare-logic
1303                  group
1304                  (or (not regexp)
1305                      (and (stringp regexp) (string-match regexp group))
1306                      (and (functionp regexp) (funcall regexp group))))
1307             (gnus-add-text-properties
1308              (point) (prog1 (1+ (point))
1309                        (insert " " mark "     *: "
1310                                (gnus-group-decoded-name group)
1311                                "\n"))
1312              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
1313                    'gnus-unread t
1314                    'gnus-level level))))
1315       (while groups
1316         (setq group (pop groups))
1317         (when (gnus-group-prepare-logic
1318                group
1319                (or (not regexp)
1320                    (and (stringp regexp) (string-match regexp group))
1321                    (and (functionp regexp) (funcall regexp group))))
1322           (gnus-group-insert-group-line
1323            group level nil
1324            (let ((active (gnus-active group)))
1325              (if active
1326                  (if (zerop (cdr active))
1327                      0
1328                    (- (1+ (cdr active)) (car active)))
1329                nil))
1330            (gnus-method-simplify (gnus-find-method-for-group group))))))))
1331
1332 (defun gnus-group-update-group-line ()
1333   "Update the current line in the group buffer."
1334   (let* ((buffer-read-only nil)
1335          (group (gnus-group-group-name))
1336          (entry (and group (gnus-group-entry group)))
1337          gnus-group-indentation)
1338     (when group
1339       (and entry
1340            (not (gnus-ephemeral-group-p group))
1341            (gnus-dribble-enter
1342             (concat "(gnus-group-set-info '"
1343                     (gnus-prin1-to-string (nth 2 entry))
1344                     ")")))
1345       (setq gnus-group-indentation (gnus-group-group-indentation))
1346       (gnus-delete-line)
1347       (gnus-group-insert-group-line-info group)
1348       (forward-line -1)
1349       (gnus-group-position-point))))
1350
1351 (defun gnus-group-insert-group-line-info (group)
1352   "Insert GROUP on the current line."
1353   (let ((entry (gnus-group-entry group))
1354         (gnus-group-indentation (gnus-group-group-indentation))
1355         active info)
1356     (if entry
1357         (progn
1358           ;; (Un)subscribed group.
1359           (setq info (nth 2 entry))
1360           (gnus-group-insert-group-line
1361            group (gnus-info-level info) (gnus-info-marks info)
1362            (or (car entry) t) (gnus-info-method info)))
1363       ;; This group is dead.
1364       (gnus-group-insert-group-line
1365        group
1366        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
1367        nil
1368        (if (setq active (gnus-active group))
1369            (if (zerop (cdr active))
1370                0
1371              (- (1+ (cdr active)) (car active)))
1372          nil)
1373        (gnus-method-simplify (gnus-find-method-for-group group))))))
1374
1375 (defun gnus-number-of-unseen-articles-in-group (group)
1376   (let* ((info (nth 2 (gnus-group-entry group)))
1377          (marked (gnus-info-marks info))
1378          (seen (cdr (assq 'seen marked)))
1379          (active (gnus-active group)))
1380     (if (not active)
1381         0
1382       (length (gnus-uncompress-range
1383                (gnus-range-difference
1384                 (gnus-range-difference (list active) (gnus-info-read info))
1385                 seen))))))
1386
1387 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level
1388                                                     gnus-tmp-marked number
1389                                                     gnus-tmp-method)
1390   "Insert a group line in the group buffer."
1391   (let* ((gnus-tmp-method
1392           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
1393          (group-name-charset (gnus-group-name-charset gnus-tmp-method
1394                                                       gnus-tmp-group))
1395          (gnus-tmp-active (gnus-active gnus-tmp-group))
1396          (gnus-tmp-number-total
1397           (if gnus-tmp-active
1398               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
1399             0))
1400          (gnus-tmp-number-of-unread
1401           (if (numberp number) (int-to-string (max 0 number))
1402             "*"))
1403          (gnus-tmp-number-of-read
1404           (if (numberp number)
1405               (int-to-string (max 0 (- gnus-tmp-number-total number)))
1406             "*"))
1407          (gnus-tmp-subscribed
1408           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
1409                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
1410                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
1411                 (t ?K)))
1412          (gnus-tmp-qualified-group
1413           (gnus-group-name-decode (gnus-group-real-name gnus-tmp-group)
1414                                   group-name-charset))
1415          (gnus-tmp-comment
1416           (or (gnus-group-get-parameter gnus-tmp-group 'comment t)
1417               gnus-tmp-group))
1418          (gnus-tmp-newsgroup-description
1419           (if gnus-description-hashtb
1420               (or (gnus-group-name-decode
1421                    (gnus-gethash gnus-tmp-group gnus-description-hashtb)
1422                    group-name-charset) "")
1423             ""))
1424          (gnus-tmp-moderated
1425           (if (and gnus-moderated-hashtb
1426                    (gnus-gethash gnus-tmp-group gnus-moderated-hashtb))
1427               ?m ? ))
1428          (gnus-tmp-moderated-string
1429           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
1430          (gnus-tmp-group-icon "==&&==")
1431          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
1432          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
1433          (gnus-tmp-news-method-string
1434           (if gnus-tmp-method
1435               (format "(%s:%s)" (car gnus-tmp-method)
1436                       (cadr gnus-tmp-method)) ""))
1437          (gnus-tmp-marked-mark
1438           (if (and (numberp number)
1439                    (zerop number)
1440                    (cdr (assq 'tick gnus-tmp-marked)))
1441               ?* ? ))
1442          (gnus-tmp-summary-live
1443           (if (and (not gnus-group-is-exiting-p)
1444                    (gnus-buffer-live-p (gnus-summary-buffer-name
1445                                         gnus-tmp-group)))
1446               ?* ? ))
1447          (gnus-tmp-process-marked
1448           (if (member gnus-tmp-group gnus-group-marked)
1449               gnus-process-mark ? ))
1450          (buffer-read-only nil)
1451          header gnus-tmp-header)        ; passed as parameter to user-funcs.
1452     (beginning-of-line)
1453     (gnus-add-text-properties
1454      (point)
1455      (prog1 (1+ (point))
1456        ;; Insert the text.
1457        (let ((gnus-tmp-group (gnus-group-name-decode
1458                               gnus-tmp-group group-name-charset)))
1459          (eval gnus-group-line-format-spec)))
1460      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
1461                   gnus-unread ,(if (numberp number)
1462                                    (string-to-int gnus-tmp-number-of-unread)
1463                                  t)
1464                   gnus-marked ,gnus-tmp-marked-mark
1465                   gnus-indentation ,gnus-group-indentation
1466                   gnus-level ,gnus-tmp-level))
1467     (forward-line -1)
1468     (when (inline (gnus-visual-p 'group-highlight 'highlight))
1469       (gnus-run-hooks 'gnus-group-update-hook))
1470     (forward-line)
1471     ;; Allow XEmacs to remove front-sticky text properties.
1472     (gnus-group-remove-excess-properties)))
1473
1474 (defun gnus-group-highlight-line ()
1475   "Highlight the current line according to `gnus-group-highlight'."
1476   (let* ((list gnus-group-highlight)
1477          (p (point))
1478          (end (point-at-eol))
1479          ;; now find out where the line starts and leave point there.
1480          (beg (progn (beginning-of-line) (point)))
1481          (group (gnus-group-group-name))
1482          (entry (gnus-group-entry group))
1483          (unread (if (numberp (car entry)) (car entry) 0))
1484          (active (gnus-active group))
1485          (total (if active (1+ (- (cdr active) (car active))) 0))
1486          (info (nth 2 entry))
1487          (method (inline (gnus-server-get-method group (gnus-info-method info))))
1488          (marked (gnus-info-marks info))
1489          (mailp (apply 'append
1490                        (mapcar
1491                         (lambda (x)
1492                           (memq x (assoc (symbol-name
1493                                           (car (or method gnus-select-method)))
1494                                          gnus-valid-select-methods)))
1495                         '(mail post-mail))))
1496          (level (or (gnus-info-level info) gnus-level-killed))
1497          (score (or (gnus-info-score info) 0))
1498          (ticked (gnus-range-length (cdr (assq 'tick marked))))
1499          (group-age (gnus-group-timestamp-delta group))
1500          (inhibit-read-only t))
1501     ;; Eval the cars of the lists until we find a match.
1502     (while (and list
1503                 (not (eval (caar list))))
1504       (setq list (cdr list)))
1505     (let ((face (cdar list)))
1506       (unless (eq face (get-text-property beg 'face))
1507         (gnus-put-text-property-excluding-characters-with-faces
1508          beg end 'face
1509          (setq face (if (boundp face) (symbol-value face) face)))
1510         (gnus-extent-start-open beg)))
1511     (goto-char p)))
1512
1513 (defun gnus-group-update-group (group &optional visible-only)
1514   "Update all lines where GROUP appear.
1515 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
1516 already."
1517   ;; Can't use `save-excursion' here, so we do it manually.
1518   (let ((buf (current-buffer))
1519         mark)
1520     (set-buffer gnus-group-buffer)
1521     (setq mark (point-marker))
1522     ;; The buffer may be narrowed.
1523     (save-restriction
1524       (widen)
1525       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
1526             (loc (point-min))
1527             found buffer-read-only)
1528         ;; Enter the current status into the dribble buffer.
1529         (let ((entry (gnus-group-entry group)))
1530           (when (and entry
1531                      (not (gnus-ephemeral-group-p group)))
1532             (gnus-dribble-enter
1533              (concat "(gnus-group-set-info '"
1534                      (gnus-prin1-to-string (nth 2 entry))
1535                      ")"))))
1536         ;; Find all group instances.  If topics are in use, each group
1537         ;; may be listed in more than once.
1538         (while (setq loc (text-property-any
1539                           loc (point-max) 'gnus-group ident))
1540           (setq found t)
1541           (goto-char loc)
1542           (let ((gnus-group-indentation (gnus-group-group-indentation)))
1543             (gnus-delete-line)
1544             (gnus-group-insert-group-line-info group)
1545             (save-excursion
1546               (forward-line -1)
1547               (gnus-run-hooks 'gnus-group-update-group-hook)))
1548           (setq loc (1+ loc)))
1549         (unless (or found visible-only)
1550           ;; No such line in the buffer, find out where it's supposed to
1551           ;; go, and insert it there (or at the end of the buffer).
1552           (if gnus-goto-missing-group-function
1553               (funcall gnus-goto-missing-group-function group)
1554             (let ((entry (cddr (gnus-group-entry group))))
1555               (while (and entry (car entry)
1556                           (not
1557                            (gnus-goto-char
1558                             (text-property-any
1559                              (point-min) (point-max)
1560                              'gnus-group (gnus-intern-safe
1561                                           (caar entry) gnus-active-hashtb)))))
1562                 (setq entry (cdr entry)))
1563               (or entry (goto-char (point-max)))))
1564           ;; Finally insert the line.
1565           (let ((gnus-group-indentation (gnus-group-group-indentation)))
1566             (gnus-group-insert-group-line-info group)
1567             (save-excursion
1568               (forward-line -1)
1569               (gnus-run-hooks 'gnus-group-update-group-hook))))
1570         (when gnus-group-update-group-function
1571           (funcall gnus-group-update-group-function group))
1572         (gnus-group-set-mode-line)))
1573     (goto-char mark)
1574     (set-marker mark nil)
1575     (set-buffer buf)))
1576
1577 (defun gnus-group-set-mode-line ()
1578   "Update the mode line in the group buffer."
1579   (when (memq 'group gnus-updated-mode-lines)
1580     ;; Yes, we want to keep this mode line updated.
1581     (save-excursion
1582       (set-buffer gnus-group-buffer)
1583       (let* ((gformat (or gnus-group-mode-line-format-spec
1584                           (gnus-set-format 'group-mode)))
1585              (gnus-tmp-news-server (cadr gnus-select-method))
1586              (gnus-tmp-news-method (car gnus-select-method))
1587              (gnus-tmp-colon (if (equal gnus-tmp-news-server "") "" ":"))
1588              (max-len 60)
1589              gnus-tmp-header            ;Dummy binding for user-defined formats
1590              ;; Get the resulting string.
1591              (modified
1592               (and gnus-dribble-buffer
1593                    (buffer-name gnus-dribble-buffer)
1594                    (buffer-modified-p gnus-dribble-buffer)
1595                    (save-excursion
1596                      (set-buffer gnus-dribble-buffer)
1597                      (not (zerop (buffer-size))))))
1598              (mode-string (eval gformat)))
1599         ;; Say whether the dribble buffer has been modified.
1600         (setq mode-line-modified
1601               (if modified (car gnus-mode-line-modified)
1602                 (cdr gnus-mode-line-modified)))
1603         ;; If the line is too long, we chop it off.
1604         (when (> (length mode-string) max-len)
1605           (setq mode-string (substring mode-string 0 (- max-len 4))))
1606         (prog1
1607             (setq mode-line-buffer-identification
1608                   (gnus-mode-line-buffer-identification
1609                    (list mode-string)))
1610           (set-buffer-modified-p modified))))))
1611
1612 (defun gnus-group-group-name ()
1613   "Get the name of the newsgroup on the current line."
1614   (let ((group (get-text-property (point-at-bol) 'gnus-group)))
1615     (when group
1616       (symbol-name group))))
1617
1618 (defun gnus-group-group-level ()
1619   "Get the level of the newsgroup on the current line."
1620   (get-text-property (point-at-bol) 'gnus-level))
1621
1622 (defun gnus-group-group-indentation ()
1623   "Get the indentation of the newsgroup on the current line."
1624   (or (get-text-property (point-at-bol) 'gnus-indentation)
1625       (and gnus-group-indentation-function
1626            (funcall gnus-group-indentation-function))
1627       ""))
1628
1629 (defun gnus-group-group-unread ()
1630   "Get the number of unread articles of the newsgroup on the current line."
1631   (get-text-property (point-at-bol) 'gnus-unread))
1632
1633 (defun gnus-group-new-mail (group)
1634   (if (nnmail-new-mail-p (gnus-group-real-name group))
1635       gnus-new-mail-mark
1636     ? ))
1637
1638 (defun gnus-group-level (group)
1639   "Return the estimated level of GROUP."
1640   (or (gnus-info-level (gnus-get-info group))
1641       (and (member group gnus-zombie-list) gnus-level-zombie)
1642       gnus-level-killed))
1643
1644 (defun gnus-group-search-forward (&optional backward all level first-too)
1645   "Find the next newsgroup with unread articles.
1646 If BACKWARD is non-nil, find the previous newsgroup instead.
1647 If ALL is non-nil, just find any newsgroup.
1648 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
1649 group exists.
1650 If FIRST-TOO, the current line is also eligible as a target."
1651   (let ((way (if backward -1 1))
1652         (low gnus-level-killed)
1653         (beg (point))
1654         pos found lev)
1655     (if (and backward (progn (beginning-of-line)) (bobp))
1656         nil
1657       (unless first-too
1658         (forward-line way))
1659       (while (and
1660               (not (eobp))
1661               (not (setq
1662                     found
1663                     (and
1664                      (get-text-property (point) 'gnus-group)
1665                      (or all
1666                          (and
1667                           (let ((unread
1668                                  (get-text-property (point) 'gnus-unread)))
1669                             (and (numberp unread) (> unread 0)))
1670                           (setq lev (get-text-property (point)
1671                                                        'gnus-level))
1672                           (<= lev gnus-level-subscribed)))
1673                      (or (not level)
1674                          (and (setq lev (get-text-property (point)
1675                                                            'gnus-level))
1676                               (or (= lev level)
1677                                   (and (< lev low)
1678                                        (< level lev)
1679                                        (progn
1680                                          (setq low lev)
1681                                          (setq pos (point))
1682                                          nil))))))))
1683               (zerop (forward-line way)))))
1684     (if found
1685         (progn (gnus-group-position-point) t)
1686       (goto-char (or pos beg))
1687       (and pos t))))
1688
1689 (defun gnus-total-fetched-for (group)
1690   (let* ((size-in-cache (or (gnus-cache-total-fetched-for group) 0))
1691          (size-in-agent (or (gnus-agent-total-fetched-for group) 0))
1692          (size (+ size-in-cache size-in-agent))
1693          (suffix '("B" "K" "M" "G"))
1694          (scale 1024.0)
1695          (cutoff (* 10 scale)))
1696     (while (> size cutoff)
1697       (setq size (/ size scale)
1698             suffix (cdr suffix)))
1699     (format "%5.1f%s" size (car suffix))))
1700
1701 ;;; Gnus group mode commands
1702
1703 ;; Group marking.
1704
1705 (defun gnus-group-mark-line-p ()
1706   (save-excursion
1707     (beginning-of-line)
1708     (forward-char (or (cdr (assq 'process gnus-group-mark-positions)) 2))
1709     (eq (char-after) gnus-process-mark)))
1710
1711 (defun gnus-group-mark-group (n &optional unmark no-advance)
1712   "Mark the current group."
1713   (interactive "p")
1714   (let ((buffer-read-only nil)
1715         group)
1716     (while (and (> n 0)
1717                 (not (eobp)))
1718       (when (setq group (gnus-group-group-name))
1719         ;; Go to the mark position.
1720         (beginning-of-line)
1721         (forward-char (or (cdr (assq 'process gnus-group-mark-positions)) 2))
1722         (delete-char 1)
1723         (if unmark
1724             (progn
1725               (setq gnus-group-marked (delete group gnus-group-marked))
1726               (insert-char ? 1 t))
1727            (setq gnus-group-marked
1728                  (cons group (delete group gnus-group-marked)))
1729            (insert-char gnus-process-mark 1 t)))
1730       (unless no-advance
1731         (gnus-group-next-group 1))
1732       (decf n))
1733     (gnus-summary-position-point)
1734     n))
1735
1736 (defun gnus-group-unmark-group (n)
1737   "Remove the mark from the current group."
1738   (interactive "p")
1739   (gnus-group-mark-group n 'unmark)
1740   (gnus-group-position-point))
1741
1742 (defun gnus-group-unmark-all-groups ()
1743   "Unmark all groups."
1744   (interactive)
1745   (save-excursion
1746     (mapc 'gnus-group-remove-mark gnus-group-marked))
1747   (gnus-group-position-point))
1748
1749 (defun gnus-group-mark-region (unmark beg end)
1750   "Mark all groups between point and mark.
1751 If UNMARK, remove the mark instead."
1752   (interactive "P\nr")
1753   (let ((num (count-lines beg end)))
1754     (save-excursion
1755       (goto-char beg)
1756       (- num (gnus-group-mark-group num unmark)))))
1757
1758 (defun gnus-group-mark-buffer (&optional unmark)
1759   "Mark all groups in the buffer.
1760 If UNMARK, remove the mark instead."
1761   (interactive "P")
1762   (gnus-group-mark-region unmark (point-min) (point-max)))
1763
1764 (defun gnus-group-mark-regexp (regexp)
1765   "Mark all groups that match some regexp."
1766   (interactive "sMark (regexp): ")
1767   (let ((alist (cdr gnus-newsrc-alist))
1768         group)
1769     (save-excursion
1770       (while alist
1771         (when (string-match regexp (setq group (gnus-info-group (pop alist))))
1772           (gnus-group-jump-to-group group)
1773           (gnus-group-set-mark group)))))
1774   (gnus-group-position-point))
1775
1776 (defun gnus-group-remove-mark (group &optional test-marked)
1777   "Remove the process mark from GROUP and move point there.
1778 Return nil if the group isn't displayed."
1779   (if (gnus-group-goto-group group nil test-marked)
1780       (save-excursion
1781         (gnus-group-mark-group 1 'unmark t)
1782         t)
1783     (setq gnus-group-marked
1784           (delete group gnus-group-marked))
1785     nil))
1786
1787 (defun gnus-group-set-mark (group)
1788   "Set the process mark on GROUP."
1789   (if (gnus-group-goto-group group)
1790       (save-excursion
1791         (gnus-group-mark-group 1 nil t))
1792     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
1793
1794 (defun gnus-group-universal-argument (arg &optional groups func)
1795   "Perform any command on all groups according to the process/prefix convention."
1796   (interactive "P")
1797   (if (eq (setq func (or func
1798                          (key-binding
1799                           (read-key-sequence
1800                            (substitute-command-keys
1801                             "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
1802           'undefined)
1803       (gnus-error 1 "Undefined key")
1804     (gnus-group-iterate arg
1805       (lambda (group)
1806         (command-execute func))))
1807   (gnus-group-position-point))
1808
1809 (defun gnus-group-process-prefix (n)
1810   "Return a list of groups to work on.
1811 Take into consideration N (the prefix) and the list of marked groups."
1812   (cond
1813    (n
1814     (setq n (prefix-numeric-value n))
1815     ;; There is a prefix, so we return a list of the N next
1816     ;; groups.
1817     (let ((way (if (< n 0) -1 1))
1818           (n (abs n))
1819           group groups)
1820       (save-excursion
1821         (while (> n 0)
1822           (if (setq group (gnus-group-group-name))
1823               (push group groups))
1824           (setq n (1- n))
1825           (gnus-group-next-group way)))
1826       (nreverse groups)))
1827    ((and (gnus-region-active-p) (mark))
1828     ;; Work on the region between point and mark.
1829     (let ((max (max (point) (mark)))
1830           groups)
1831       (save-excursion
1832         (goto-char (min (point) (mark)))
1833         (while
1834             (and
1835              (push (gnus-group-group-name) groups)
1836              (zerop (gnus-group-next-group 1))
1837              (< (point) max)))
1838         (nreverse groups))))
1839    (gnus-group-marked
1840     ;; No prefix, but a list of marked articles.
1841     (reverse gnus-group-marked))
1842    (t
1843     ;; Neither marked articles or a prefix, so we return the
1844     ;; current group.
1845     (let ((group (gnus-group-group-name)))
1846       (and group (list group))))))
1847
1848 ;;; !!!Surely gnus-group-iterate should be a macro instead?  I can't
1849 ;;; imagine why I went through these contortions...
1850 (eval-and-compile
1851   (let ((function (make-symbol "gnus-group-iterate-function"))
1852         (window (make-symbol "gnus-group-iterate-window"))
1853         (groups (make-symbol "gnus-group-iterate-groups"))
1854         (group (make-symbol "gnus-group-iterate-group")))
1855     (eval
1856      `(defun gnus-group-iterate (arg ,function)
1857         "Iterate FUNCTION over all process/prefixed groups.
1858 FUNCTION will be called with the group name as the parameter
1859 and with point over the group in question."
1860         (let ((,groups (gnus-group-process-prefix arg))
1861               (,window (selected-window))
1862               ,group)
1863           (while ,groups
1864             (setq ,group (car ,groups)
1865                   ,groups (cdr ,groups))
1866             (select-window ,window)
1867             (gnus-group-remove-mark ,group)
1868             (save-selected-window
1869               (save-excursion
1870                 (funcall ,function ,group)))))))))
1871
1872 (put 'gnus-group-iterate 'lisp-indent-function 1)
1873
1874 ;; Selecting groups.
1875
1876 (defun gnus-group-read-group (&optional all no-article group select-articles)
1877   "Read news in this newsgroup.
1878 If the prefix argument ALL is non-nil, already read articles become
1879 readable.  IF ALL is a number, fetch this number of articles.  If the
1880 optional argument NO-ARTICLE is non-nil, no article will be
1881 auto-selected upon group entry.  If GROUP is non-nil, fetch that
1882 group."
1883   (interactive "P")
1884   (let ((no-display (eq all 0))
1885         (group (or group (gnus-group-group-name)))
1886         number active marked entry)
1887     (when (eq all 0)
1888       (setq all nil))
1889     (unless group
1890       (error "No group on current line"))
1891     (setq marked (gnus-info-marks
1892                   (nth 2 (setq entry (gnus-group-entry group)))))
1893     ;; This group might be a dead group.  In that case we have to get
1894     ;; the number of unread articles from `gnus-active-hashtb'.
1895     (setq number
1896           (cond ((numberp all) all)
1897                 (entry (car entry))
1898                 ((setq active (gnus-active group))
1899                  (- (1+ (cdr active)) (car active)))))
1900     (gnus-summary-read-group
1901      group (or all (and (numberp number)
1902                         (zerop (+ number (gnus-range-length
1903                                           (cdr (assq 'tick marked)))
1904                                   (gnus-range-length
1905                                    (cdr (assq 'dormant marked)))))))
1906      no-article nil no-display nil select-articles)))
1907
1908 (defun gnus-group-select-group (&optional all)
1909   "Select this newsgroup.
1910 No article is selected automatically.
1911 If the group is opened, just switch the summary buffer.
1912 If ALL is non-nil, already read articles become readable.
1913 If ALL is a positive number, fetch this number of the latest
1914 articles in the group.
1915 If ALL is a negative number, fetch this number of the earliest
1916 articles in the group."
1917   (interactive "P")
1918   (when (and (eobp) (not (gnus-group-group-name)))
1919     (forward-line -1))
1920   (gnus-group-read-group all t))
1921
1922 (defun gnus-group-quick-select-group (&optional all)
1923   "Select the current group \"quickly\".
1924 This means that no highlighting or scoring will be performed.
1925 If ALL (the prefix argument) is 0, don't even generate the summary
1926 buffer.
1927
1928 This might be useful if you want to toggle threading
1929 before entering the group."
1930   (interactive "P")
1931   (require 'gnus-score)
1932   (let (gnus-visual
1933         gnus-score-find-score-files-function
1934         gnus-home-score-file
1935         gnus-apply-kill-hook
1936         gnus-summary-expunge-below)
1937     (gnus-group-read-group all t)))
1938
1939 (defun gnus-group-visible-select-group (&optional all)
1940   "Select the current group without hiding any articles."
1941   (interactive "P")
1942   (let ((gnus-inhibit-limiting t))
1943     (gnus-group-read-group all t)))
1944
1945 (defun gnus-group-select-group-ephemerally ()
1946   "Select the current group without doing any processing whatsoever.
1947 You will actually be entered into a group that's a copy of
1948 the current group; no changes you make while in this group will
1949 be permanent."
1950   (interactive)
1951   (require 'gnus-score)
1952   (let* (gnus-visual
1953          gnus-score-find-score-files-function gnus-apply-kill-hook
1954          gnus-summary-expunge-below gnus-show-threads gnus-suppress-duplicates
1955          gnus-summary-mode-hook gnus-select-group-hook
1956          (group (gnus-group-group-name))
1957          (method (gnus-find-method-for-group group)))
1958     (gnus-group-read-ephemeral-group
1959      (gnus-group-prefixed-name group method) method)))
1960
1961 ;;;###autoload
1962 (defun gnus-fetch-group (group &optional articles)
1963   "Start Gnus if necessary and enter GROUP.
1964 Returns whether the fetching was successful or not."
1965   (interactive (list (completing-read "Group name: " gnus-active-hashtb)))
1966   (unless (get-buffer gnus-group-buffer)
1967     (gnus-no-server))
1968   (gnus-group-read-group articles nil group))
1969
1970 ;;;###autoload
1971 (defun gnus-fetch-group-other-frame (group)
1972   "Pop up a frame and enter GROUP."
1973   (interactive "P")
1974   (let ((window (get-buffer-window gnus-group-buffer)))
1975     (cond (window
1976            (select-frame (window-frame window)))
1977           ((= (length (frame-list)) 1)
1978            (select-frame (make-frame)))
1979           (t
1980            (other-frame 1))))
1981   (gnus-fetch-group group))
1982
1983 (defvar gnus-ephemeral-group-server 0)
1984
1985 (defcustom gnus-large-ephemeral-newsgroup 200
1986   "The number of articles which indicates a large ephemeral newsgroup.
1987 Same as `gnus-large-newsgroup', but only used for ephemeral newsgroups.
1988
1989 If the number of articles in a newsgroup is greater than this value,
1990 confirmation is required for selecting the newsgroup.  If it is nil, no
1991 confirmation is required."
1992   :version "21.4"
1993   :group 'gnus-group-select
1994   :type '(choice (const :tag "No limit" nil)
1995                  integer))
1996
1997 (defcustom gnus-fetch-old-ephemeral-headers nil
1998   "Same as `gnus-fetch-old-headers', but only used for ephemeral newsgroups."
1999   :version "21.4"
2000   :group 'gnus-thread
2001   :type '(choice (const :tag "off" nil)
2002                  (const some)
2003                  number
2004                  (sexp :menu-tag "other" t)))
2005
2006 ;; Enter a group that is not in the group buffer.  Non-nil is returned
2007 ;; if selection was successful.
2008 (defun gnus-group-read-ephemeral-group (group method &optional activate
2009                                               quit-config request-only
2010                                               select-articles
2011                                               parameters)
2012   "Read GROUP from METHOD as an ephemeral group.
2013 If ACTIVATE, request the group first.
2014 If QUIT-CONFIG, use that window configuration when exiting from the
2015 ephemeral group.
2016 If REQUEST-ONLY, don't actually read the group; just request it.
2017 If SELECT-ARTICLES, only select those articles.
2018 If PARAMETERS, use those as the group parameters.
2019
2020 Return the name of the group if selection was successful."
2021   (interactive
2022    (list
2023     ;; (gnus-read-group "Group name: ")
2024     (completing-read
2025      "Group: " gnus-active-hashtb
2026      nil nil nil
2027      'gnus-group-history)
2028     (gnus-read-method "From method: ")))
2029   ;; Transform the select method into a unique server.
2030   (when (stringp method)
2031     (setq method (gnus-server-to-method method)))
2032   (setq method
2033         `(,(car method) ,(concat (cadr method) "-ephemeral")
2034           (,(intern (format "%s-address" (car method))) ,(cadr method))
2035           ,@(cddr method)))
2036   (let ((group (if (gnus-group-foreign-p group) group
2037                  (gnus-group-prefixed-name (gnus-group-real-name group)
2038                                            method))))
2039     (gnus-sethash
2040      group
2041      `(-1 nil (,group
2042                ,gnus-level-default-subscribed nil nil ,method
2043                ,(cons
2044                  (if quit-config
2045                      (cons 'quit-config quit-config)
2046                    (cons 'quit-config
2047                          (cons gnus-summary-buffer
2048                                gnus-current-window-configuration)))
2049                  parameters)))
2050      gnus-newsrc-hashtb)
2051     (push method gnus-ephemeral-servers)
2052     (set-buffer gnus-group-buffer)
2053     (unless (gnus-check-server method)
2054       (error "Unable to contact server: %s" (gnus-status-message method)))
2055     (when activate
2056       (gnus-activate-group group 'scan)
2057       (unless (gnus-request-group group)
2058         (error "Couldn't request group: %s"
2059                (nnheader-get-report (car method)))))
2060     (if request-only
2061         group
2062       (condition-case ()
2063           (when (let ((gnus-large-newsgroup gnus-large-ephemeral-newsgroup)
2064                       (gnus-fetch-old-headers
2065                        gnus-fetch-old-ephemeral-headers))
2066                   (gnus-group-read-group t t group select-articles))
2067             group)
2068         ;;(error nil)
2069         (quit
2070          (message "Quit reading the ephemeral group")
2071          nil)))))
2072
2073 (defun gnus-group-jump-to-group (group &optional prompt)
2074   "Jump to newsgroup GROUP.
2075
2076 If PROMPT (the prefix) is a number, use the prompt specified in
2077 `gnus-group-jump-to-group-prompt'."
2078   (interactive
2079    (list (mm-string-make-unibyte
2080           (completing-read
2081            "Group: " gnus-active-hashtb nil
2082            (gnus-read-active-file-p)
2083            (if current-prefix-arg
2084                (cdr (assq current-prefix-arg gnus-group-jump-to-group-prompt))
2085              (or (and (stringp gnus-group-jump-to-group-prompt)
2086                       gnus-group-jump-to-group-prompt)
2087                  (let ((p (cdr (assq 0 gnus-group-jump-to-group-prompt))))
2088                    (and (stringp p) p))))
2089            'gnus-group-history))))
2090
2091   (when (equal group "")
2092     (error "Empty group name"))
2093
2094   (unless (gnus-ephemeral-group-p group)
2095     ;; Either go to the line in the group buffer...
2096     (unless (gnus-group-goto-group group)
2097       ;; ... or insert the line.
2098       (gnus-group-update-group group)
2099       (gnus-group-goto-group group)))
2100   ;; Adjust cursor point.
2101   (gnus-group-position-point))
2102
2103 (defun gnus-group-goto-group (group &optional far test-marked)
2104   "Goto to newsgroup GROUP.
2105 If FAR, it is likely that the group is not on the current line.
2106 If TEST-MARKED, the line must be marked."
2107   (when group
2108     (beginning-of-line)
2109     (cond
2110      ;; It's quite likely that we are on the right line, so
2111      ;; we check the current line first.
2112      ((and (not far)
2113            (eq (get-text-property (point) 'gnus-group)
2114                (gnus-intern-safe group gnus-active-hashtb))
2115            (or (not test-marked) (gnus-group-mark-line-p)))
2116       (point))
2117      ;; Previous and next line are also likely, so we check them as well.
2118      ((and (not far)
2119            (save-excursion
2120              (forward-line -1)
2121              (and (eq (get-text-property (point) 'gnus-group)
2122                       (gnus-intern-safe group gnus-active-hashtb))
2123                   (or (not test-marked) (gnus-group-mark-line-p)))))
2124       (forward-line -1)
2125       (point))
2126      ((and (not far)
2127            (save-excursion
2128              (forward-line 1)
2129              (and (eq (get-text-property (point) 'gnus-group)
2130                       (gnus-intern-safe group gnus-active-hashtb))
2131                   (or (not test-marked) (gnus-group-mark-line-p)))))
2132       (forward-line 1)
2133       (point))
2134      (test-marked
2135       (goto-char (point-min))
2136       (let (found)
2137         (while (and (not found)
2138                     (gnus-goto-char
2139                      (text-property-any
2140                       (point) (point-max)
2141                       'gnus-group
2142                       (gnus-intern-safe group gnus-active-hashtb))))
2143           (if (gnus-group-mark-line-p)
2144               (setq found t)
2145             (forward-line 1)))
2146         found))
2147      (t
2148       ;; Search through the entire buffer.
2149       (gnus-goto-char
2150        (text-property-any
2151         (point-min) (point-max)
2152         'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))))
2153
2154 (defun gnus-group-next-group (n &optional silent)
2155   "Go to next N'th newsgroup.
2156 If N is negative, search backward instead.
2157 Returns the difference between N and the number of skips actually
2158 done."
2159   (interactive "p")
2160   (gnus-group-next-unread-group n t nil silent))
2161
2162 (defun gnus-group-next-unread-group (n &optional all level silent)
2163   "Go to next N'th unread newsgroup.
2164 If N is negative, search backward instead.
2165 If ALL is non-nil, choose any newsgroup, unread or not.
2166 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
2167 such group can be found, the next group with a level higher than
2168 LEVEL.
2169 Returns the difference between N and the number of skips actually
2170 made."
2171   (interactive "p")
2172   (let ((backward (< n 0))
2173         (n (abs n)))
2174     (while (and (> n 0)
2175                 (gnus-group-search-forward
2176                  backward (or (not gnus-group-goto-unread) all) level))
2177       (setq n (1- n)))
2178     (when (and (/= 0 n)
2179                (not silent))
2180       (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
2181                     (if level " on this level or higher" "")))
2182     n))
2183
2184 (defun gnus-group-prev-group (n)
2185   "Go to previous N'th newsgroup.
2186 Returns the difference between N and the number of skips actually
2187 done."
2188   (interactive "p")
2189   (gnus-group-next-unread-group (- n) t))
2190
2191 (defun gnus-group-prev-unread-group (n)
2192   "Go to previous N'th unread newsgroup.
2193 Returns the difference between N and the number of skips actually
2194 done."
2195   (interactive "p")
2196   (gnus-group-next-unread-group (- n)))
2197
2198 (defun gnus-group-next-unread-group-same-level (n)
2199   "Go to next N'th unread newsgroup on the same level.
2200 If N is negative, search backward instead.
2201 Returns the difference between N and the number of skips actually
2202 done."
2203   (interactive "p")
2204   (gnus-group-next-unread-group n t (gnus-group-group-level))
2205   (gnus-group-position-point))
2206
2207 (defun gnus-group-prev-unread-group-same-level (n)
2208   "Go to next N'th unread newsgroup on the same level.
2209 Returns the difference between N and the number of skips actually
2210 done."
2211   (interactive "p")
2212   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
2213   (gnus-group-position-point))
2214
2215 (defun gnus-group-best-unread-group (&optional exclude-group)
2216   "Go to the group with the highest level.
2217 If EXCLUDE-GROUP, do not go to that group."
2218   (interactive)
2219   (goto-char (point-min))
2220   (let ((best 100000)
2221         unread best-point)
2222     (while (not (eobp))
2223       (setq unread (get-text-property (point) 'gnus-unread))
2224       (when (and (numberp unread) (> unread 0))
2225         (when (and (get-text-property (point) 'gnus-level)
2226                    (< (get-text-property (point) 'gnus-level) best)
2227                    (or (not exclude-group)
2228                        (not (equal exclude-group (gnus-group-group-name)))))
2229           (setq best (get-text-property (point) 'gnus-level))
2230           (setq best-point (point))))
2231       (forward-line 1))
2232     (when best-point
2233       (goto-char best-point))
2234     (gnus-group-position-point)
2235     (and best-point (gnus-group-group-name))))
2236
2237 (defun gnus-group-first-unread-group ()
2238   "Go to the first group with unread articles."
2239   (interactive)
2240   (prog1
2241       (let ((opoint (point))
2242             unread)
2243         (goto-char (point-min))
2244         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
2245                 (and (numberp unread)   ; Not a topic.
2246                      (not (zerop unread))) ; Has unread articles.
2247                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
2248             (point)                     ; Success.
2249           (goto-char opoint)
2250           nil))                         ; Not success.
2251     (gnus-group-position-point)))
2252
2253 (defun gnus-group-enter-server-mode ()
2254   "Jump to the server buffer."
2255   (interactive)
2256   (gnus-enter-server-buffer))
2257
2258 (defun gnus-group-make-group-simple (&optional group)
2259   "Add a new newsgroup.
2260 The user will be prompted for GROUP."
2261   (interactive
2262    (list (completing-read "Group: " gnus-active-hashtb
2263                           nil nil nil 'gnus-group-history)))
2264   (gnus-group-make-group
2265    (gnus-group-real-name group)
2266    (gnus-group-server group)))
2267
2268 (defun gnus-group-make-group (name &optional method address args)
2269   "Add a new newsgroup.
2270 The user will be prompted for a NAME, for a select METHOD, and an
2271 ADDRESS."
2272   (interactive
2273    (list
2274     (gnus-read-group "Group name: ")
2275     (gnus-read-method "From method: ")))
2276
2277   (when (stringp method)
2278     (setq method (or (gnus-server-to-method method) method)))
2279   (let* ((meth (gnus-method-simplify
2280                 (when (and method
2281                            (not (gnus-server-equal method gnus-select-method)))
2282                   (if address (list (intern method) address)
2283                     method))))
2284          (nname (if method (gnus-group-prefixed-name name meth) name))
2285          backend info)
2286     (when (gnus-group-entry nname)
2287       (error "Group %s already exists" nname))
2288     ;; Subscribe to the new group.
2289     (gnus-group-change-level
2290      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
2291      gnus-level-default-subscribed gnus-level-killed
2292      (and (gnus-group-group-name)
2293           (gnus-group-entry (gnus-group-group-name)))
2294      t)
2295     ;; Make it active.
2296     (gnus-set-active nname (cons 1 0))
2297     (unless (gnus-ephemeral-group-p name)
2298       (gnus-dribble-enter
2299        (concat "(gnus-group-set-info '"
2300                (gnus-prin1-to-string (cdr info)) ")")))
2301     ;; Insert the line.
2302     (gnus-group-insert-group-line-info nname)
2303     (forward-line -1)
2304     (gnus-group-position-point)
2305
2306     ;; Load the back end and try to make the back end create
2307     ;; the group as well.
2308     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
2309                                                   nil meth))))
2310                  gnus-valid-select-methods)
2311       (require backend))
2312     (gnus-check-server meth)
2313     (when (gnus-check-backend-function 'request-create-group nname)
2314       (unless (gnus-request-create-group nname nil args)
2315         (error "Could not create group on server: %s"
2316                (nnheader-get-report backend))))
2317     t))
2318
2319 (defun gnus-group-delete-groups (&optional arg)
2320   "Delete the current group.  Only meaningful with editable groups."
2321   (interactive "P")
2322   (let ((n (length (gnus-group-process-prefix arg))))
2323     (when (gnus-yes-or-no-p
2324            (if (= n 1)
2325                "Delete this 1 group? "
2326              (format "Delete these %d groups? " n)))
2327       (gnus-group-iterate arg
2328         (lambda (group)
2329           (gnus-group-delete-group group nil t))))))
2330
2331 (defun gnus-group-delete-group (group &optional force no-prompt)
2332   "Delete the current group.  Only meaningful with editable groups.
2333 If FORCE (the prefix) is non-nil, all the articles in the group will
2334 be deleted.  This is \"deleted\" as in \"removed forever from the face
2335 of the Earth\".  There is no undo.  The user will be prompted before
2336 doing the deletion.
2337 Note that you also have to specify FORCE if you want the group to
2338 be removed from the server, even when it's empty."
2339   (interactive
2340    (list (gnus-group-group-name)
2341          current-prefix-arg))
2342   (unless group
2343     (error "No group to delete"))
2344   (unless (gnus-check-backend-function 'request-delete-group group)
2345     (error "This back end does not support group deletion"))
2346   (prog1
2347       (if (and (not no-prompt)
2348                (not (gnus-yes-or-no-p
2349                      (format
2350                       "Do you really want to delete %s%s? "
2351                       group (if force " and all its contents" "")))))
2352           ()                            ; Whew!
2353         (gnus-message 6 "Deleting group %s..." group)
2354         (if (not (gnus-request-delete-group group force))
2355             (gnus-error 3 "Couldn't delete group %s" group)
2356           (gnus-message 6 "Deleting group %s...done" group)
2357           (gnus-group-goto-group group)
2358           (gnus-group-kill-group 1 t)
2359           (gnus-set-active group nil)
2360           t))
2361     (gnus-group-position-point)))
2362
2363 (defun gnus-group-rename-group (group new-name)
2364   "Rename group from GROUP to NEW-NAME.
2365 When used interactively, GROUP is the group under point
2366 and NEW-NAME will be prompted for."
2367   (interactive
2368    (list
2369     (gnus-group-group-name)
2370     (progn
2371       (unless (gnus-check-backend-function
2372                'request-rename-group (gnus-group-group-name))
2373         (error "This back end does not support renaming groups"))
2374       (gnus-read-group "Rename group to: "
2375                        (gnus-group-real-name (gnus-group-group-name))))))
2376
2377   (unless (gnus-check-backend-function 'request-rename-group group)
2378     (error "This back end does not support renaming groups"))
2379   (unless group
2380     (error "No group to rename"))
2381   (when (equal (gnus-group-real-name group) new-name)
2382     (error "Can't rename to the same name"))
2383
2384   ;; We find the proper prefixed name.
2385   (setq new-name
2386         (if (gnus-group-native-p group)
2387             ;; Native group.
2388             new-name
2389           ;; Foreign group.
2390           (gnus-group-prefixed-name
2391            (gnus-group-real-name new-name)
2392            (gnus-info-method (gnus-get-info group)))))
2393
2394   (when (gnus-active new-name)
2395     (error "The group %s already exists" new-name))
2396
2397   (gnus-message 6 "Renaming group %s to %s..." group new-name)
2398   (prog1
2399       (if (progn
2400             (gnus-group-goto-group group)
2401             (not (when (< (gnus-group-group-level) gnus-level-zombie)
2402                    (gnus-request-rename-group group new-name))))
2403           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
2404         ;; We rename the group internally by killing it...
2405         (gnus-group-kill-group)
2406         ;; ... changing its name ...
2407         (setcar (cdar gnus-list-of-killed-groups) new-name)
2408         ;; ... and then yanking it.  Magic!
2409         (gnus-group-yank-group)
2410         (gnus-set-active new-name (gnus-active group))
2411         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
2412         new-name)
2413     (setq gnus-killed-list (delete group gnus-killed-list))
2414     (gnus-set-active group nil)
2415     (gnus-dribble-touch)
2416     (gnus-group-position-point)))
2417
2418 (defun gnus-group-edit-group (group &optional part)
2419   "Edit the group on the current line."
2420   (interactive (list (gnus-group-group-name)))
2421   (let ((part (or part 'info))
2422         info)
2423     (unless group
2424       (error "No group on current line"))
2425     (unless (setq info (gnus-get-info group))
2426       (error "Killed group; can't be edited"))
2427     (ignore-errors
2428       (gnus-close-group group))
2429     (gnus-edit-form
2430      ;; Find the proper form to edit.
2431      (cond ((eq part 'method)
2432             (or (gnus-info-method info) "native"))
2433            ((eq part 'params)
2434             (gnus-info-params info))
2435            (t info))
2436      ;; The proper documentation.
2437      (format
2438       "Editing the %s for `%s'."
2439       (cond
2440        ((eq part 'method) "select method")
2441        ((eq part 'params) "group parameters")
2442        (t "group info"))
2443       (gnus-group-decoded-name group))
2444      `(lambda (form)
2445         (gnus-group-edit-group-done ',part ,group form)))
2446     (local-set-key
2447      "\C-c\C-i"
2448      (gnus-create-info-command
2449       (cond
2450        ((eq part 'method)
2451         "(gnus)Select Methods")
2452        ((eq part 'params)
2453         "(gnus)Group Parameters")
2454        (t
2455         "(gnus)Group Info"))))))
2456
2457 (defun gnus-group-edit-group-method (group)
2458   "Edit the select method of GROUP."
2459   (interactive (list (gnus-group-group-name)))
2460   (gnus-group-edit-group group 'method))
2461
2462 (defun gnus-group-edit-group-parameters (group)
2463   "Edit the group parameters of GROUP."
2464   (interactive (list (gnus-group-group-name)))
2465   (gnus-group-edit-group group 'params))
2466
2467 (defun gnus-group-edit-group-done (part group form)
2468   "Update variables."
2469   (let* ((method (cond ((eq part 'info) (nth 4 form))
2470                        ((eq part 'method) form)
2471                        (t nil)))
2472          (info (cond ((eq part 'info) form)
2473                      ((eq part 'method) (gnus-get-info group))
2474                      (t nil)))
2475          (new-group (if info
2476                         (if (or (not method)
2477                                 (gnus-server-equal
2478                                  gnus-select-method method))
2479                             (gnus-group-real-name (car info))
2480                           (gnus-group-prefixed-name
2481                            (gnus-group-real-name (car info)) method))
2482                       nil)))
2483     (when (and new-group
2484                (not (equal new-group group)))
2485       (when (gnus-group-goto-group group)
2486         (gnus-group-kill-group 1))
2487       (gnus-activate-group new-group))
2488     ;; Set the info.
2489     (if (not (and info new-group))
2490         (gnus-group-set-info form (or new-group group) part)
2491       (setq info (gnus-copy-sequence info))
2492       (setcar info new-group)
2493       (unless (gnus-server-equal method "native")
2494         (unless (nthcdr 3 info)
2495           (nconc info (list nil nil)))
2496         (unless (nthcdr 4 info)
2497           (nconc info (list nil)))
2498         (gnus-info-set-method info method))
2499       (gnus-group-set-info info))
2500     (gnus-group-update-group (or new-group group))
2501     (gnus-group-position-point)))
2502
2503 (defun gnus-group-make-useful-group (group method)
2504   "Create one of the groups described in `gnus-useful-groups'."
2505   (interactive
2506    (let ((entry (assoc (completing-read "Create group: " gnus-useful-groups
2507                                         nil t)
2508                        gnus-useful-groups)))
2509      (list (cadr entry) (caddr entry))))
2510   (setq method (gnus-copy-sequence method))
2511   (let (entry)
2512     (while (setq entry (memq (assq 'eval method) method))
2513       (setcar entry (eval (cadar entry)))))
2514   (gnus-group-make-group group method))
2515
2516 (defun gnus-group-make-help-group (&optional noerror)
2517   "Create the Gnus documentation group.
2518 Optional argument NOERROR modifies the behavior of this function when the
2519 group already exists:
2520 - if not given, and error is signaled,
2521 - if t, stay silent,
2522 - if anything else, just print a message."
2523   (interactive)
2524   (let ((name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
2525         (file (nnheader-find-etc-directory "gnus-tut.txt" t)))
2526     (if (gnus-group-entry name)
2527         (cond ((eq noerror nil)
2528                (error "Documentation group already exists"))
2529               ((eq noerror t)
2530                ;; stay silent
2531                )
2532               (t
2533                (gnus-message 1 "Documentation group already exists")))
2534       ;; else:
2535       (if (not file)
2536           (gnus-message 1 "Couldn't find doc group")
2537         (gnus-group-make-group
2538          (gnus-group-real-name name)
2539          (list 'nndoc "gnus-help"
2540                (list 'nndoc-address file)
2541                (list 'nndoc-article-type 'mbox))))
2542       ))
2543   (gnus-group-position-point))
2544
2545 (defun gnus-group-make-doc-group (file type)
2546   "Create a group that uses a single file as the source."
2547   (interactive
2548    (list (read-file-name "File name: ")
2549          (and current-prefix-arg 'ask)))
2550   (when (eq type 'ask)
2551     (let ((err "")
2552           char found)
2553       (while (not found)
2554         (message
2555          "%sFile type (mbox, babyl, digest, forward, mmdf, guess) [mbdfag]: "
2556          err)
2557         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
2558                           ((= char ?b) 'babyl)
2559                           ((= char ?d) 'digest)
2560                           ((= char ?f) 'forward)
2561                           ((= char ?a) 'mmfd)
2562                           ((= char ?g) 'guess)
2563                           (t (setq err (format "%c unknown. " char))
2564                              nil))))
2565       (setq type found)))
2566   (let* ((file (expand-file-name file))
2567          (name (gnus-generate-new-group-name
2568                 (gnus-group-prefixed-name
2569                  (file-name-nondirectory file) '(nndoc "")))))
2570     (gnus-group-make-group
2571      (gnus-group-real-name name)
2572      (list 'nndoc file
2573            (list 'nndoc-address file)
2574            (list 'nndoc-article-type (or type 'guess))))))
2575
2576 (defvar nnweb-type-definition)
2577 (defvar gnus-group-web-type-history nil)
2578 (defvar gnus-group-web-search-history nil)
2579 (defun gnus-group-make-web-group (&optional solid)
2580   "Create an ephemeral nnweb group.
2581 If SOLID (the prefix), create a solid group."
2582   (interactive "P")
2583   (require 'nnweb)
2584   (let* ((group
2585           (if solid (gnus-read-group "Group name: ")
2586             (message-unique-id)))
2587          (default-type (or (car gnus-group-web-type-history)
2588                            (symbol-name (caar nnweb-type-definition))))
2589          (type
2590           (gnus-string-or
2591            (completing-read
2592             (format "Search engine type (default %s): " default-type)
2593             (mapcar (lambda (elem) (list (symbol-name (car elem))))
2594                     nnweb-type-definition)
2595             nil t nil 'gnus-group-web-type-history)
2596            default-type))
2597          (search
2598           (read-string
2599            "Search string: "
2600            (cons (or (car gnus-group-web-search-history) "") 0)
2601            'gnus-group-web-search-history))
2602          (method
2603           `(nnweb ,group (nnweb-search ,search)
2604                   (nnweb-type ,(intern type))
2605                   (nnweb-ephemeral-p t))))
2606     (if solid
2607         (progn
2608           (gnus-pull 'nnweb-ephemeral-p method)
2609           (gnus-group-make-group group method))
2610       (gnus-group-read-ephemeral-group
2611        group method t
2612        (cons (current-buffer)
2613              (if (eq major-mode 'gnus-summary-mode) 'summary 'group))))))
2614
2615 (eval-when-compile
2616   (defvar nnrss-group-alist)
2617   (defun nnrss-discover-feed (arg))
2618   (defun nnrss-save-server-data (arg)))
2619 (defun gnus-group-make-rss-group (&optional url)
2620   "Given a URL, discover if there is an RSS feed.
2621 If there is, use Gnus to create an nnrss group"
2622   (interactive)
2623   (require 'nnrss)
2624   (if (not url)
2625       (setq url (read-from-minibuffer "URL to Search for RSS: ")))
2626   (let ((feedinfo (nnrss-discover-feed url)))
2627     (if feedinfo
2628         (let ((title (read-from-minibuffer "Title: "
2629                                            (cdr (assoc 'title
2630                                                        feedinfo))))
2631               (desc  (read-from-minibuffer "Description: "
2632                                            (cdr (assoc 'description
2633                                                        feedinfo))))
2634               (href (cdr (assoc 'href feedinfo))))
2635           (push (list title href desc)
2636                 nnrss-group-alist)
2637           (gnus-group-make-group title '(nnrss ""))
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-check-reasonable-setup)
3631     (gnus-run-hooks 'gnus-after-getting-new-news-hook)
3632     (gnus-group-list-groups (and (numberp arg)
3633                                  (max (car gnus-group-list-mode) arg)))))
3634
3635 (defun gnus-group-get-new-news-this-group (&optional n dont-scan)
3636   "Check for newly arrived news in the current group (and the N-1 next groups).
3637 The difference between N and the number of newsgroup checked is returned.
3638 If N is negative, this group and the N-1 previous groups will be checked.
3639 If DONT-SCAN is non-nil, scan non-activated groups as well."
3640   (interactive "P")
3641   (let* ((groups (gnus-group-process-prefix n))
3642          (ret (if (numberp n) (- n (length groups)) 0))
3643          (beg (unless n
3644                 (point)))
3645          group method
3646          (gnus-inhibit-demon t)
3647          ;; Binding this variable will inhibit multiple fetchings
3648          ;; of the same mail source.
3649          (nnmail-fetched-sources (list t)))
3650     (gnus-run-hooks 'gnus-get-new-news-hook)
3651     (while (setq group (pop groups))
3652       (gnus-group-remove-mark group)
3653       ;; Bypass any previous denials from the server.
3654       (gnus-remove-denial (setq method (gnus-find-method-for-group group)))
3655       (if (gnus-activate-group group (if dont-scan nil 'scan) nil method)
3656           (let ((info (gnus-get-info group))
3657                 (active (gnus-active group)))
3658             (when info
3659               (gnus-request-update-info info method))
3660             (gnus-get-unread-articles-in-group info active)
3661             (unless (gnus-virtual-group-p group)
3662               (gnus-close-group group))
3663             (when gnus-agent
3664               (gnus-agent-save-group-info
3665                method (gnus-group-real-name group) active))
3666             (gnus-group-update-group group))
3667         (if (eq (gnus-server-status (gnus-find-method-for-group group))
3668                 'denied)
3669             (gnus-error 3 "Server denied access")
3670           (gnus-error 3 "%s error: %s" group (gnus-status-message group)))))
3671     (when beg
3672       (goto-char beg))
3673     (when gnus-goto-next-group-when-activating
3674       (gnus-group-next-unread-group 1 t))
3675     (gnus-summary-position-point)
3676     ret))
3677
3678 (defun gnus-group-fetch-faq (group &optional faq-dir)
3679   "Fetch the FAQ for the current group.
3680 If given a prefix argument, prompt for the FAQ dir
3681 to use."
3682   (interactive
3683    (list
3684     (gnus-group-group-name)
3685     (when current-prefix-arg
3686       (completing-read
3687        "FAQ dir: " (and (listp gnus-group-faq-directory)
3688                         (mapcar #'list
3689                                 gnus-group-faq-directory))))))
3690   (unless group
3691     (error "No group name given"))
3692   (let ((dirs (or faq-dir gnus-group-faq-directory))
3693         dir found file)
3694     (unless (listp dirs)
3695       (setq dirs (list dirs)))
3696     (while (and (not found)
3697                 (setq dir (pop dirs)))
3698       (let ((name (gnus-group-real-name group)))
3699         (setq file (expand-file-name name dir)))
3700       (if (not (file-exists-p file))
3701           (gnus-message 1 "No such file: %s" file)
3702         (let ((enable-local-variables nil))
3703           (find-file file)
3704           (setq found t))))))
3705
3706 (defun gnus-group-fetch-charter (group)
3707   "Fetch the charter for the current group.
3708 If given a prefix argument, prompt for a group."
3709   (interactive
3710    (list (or (when current-prefix-arg
3711                (completing-read "Group: " gnus-active-hashtb))
3712              (gnus-group-group-name)
3713              gnus-newsgroup-name)))
3714   (unless group
3715     (error "No group name given"))
3716   (require 'mm-url)
3717   (condition-case nil (require 'url-http) (error nil))
3718   (let ((name (mm-url-form-encode-xwfu (gnus-group-real-name group)))
3719         url hierarchy)
3720     (when (string-match "\\(^[^\\.]+\\)\\..*" name)
3721       (setq hierarchy (match-string 1 name))
3722       (if (and (setq url (cdr (assoc hierarchy gnus-group-charter-alist)))
3723                (if (fboundp 'url-http-file-exists-p)
3724                    (url-http-file-exists-p (eval url))
3725                  t))
3726           (browse-url (eval url))
3727         (setq url (concat "http://" hierarchy
3728                           ".news-admin.org/charters/" name))
3729         (if (and (fboundp 'url-http-file-exists-p)
3730                  (url-http-file-exists-p url))
3731             (browse-url url)
3732           (gnus-group-fetch-control group))))))
3733
3734 (defun gnus-group-fetch-control (group)
3735   "Fetch the archived control messages for the current group.
3736 If given a prefix argument, prompt for a group."
3737   (interactive
3738    (list (or (when current-prefix-arg
3739                (completing-read "Group: " gnus-active-hashtb))
3740              (gnus-group-group-name)
3741              gnus-newsgroup-name)))
3742   (unless group
3743     (error "No group name given"))
3744   (let ((name (gnus-group-real-name group))
3745         hierarchy)
3746     (when (string-match "\\(^[^\\.]+\\)\\..*" name)
3747       (setq hierarchy (match-string 1 name))
3748       (if gnus-group-fetch-control-use-browse-url
3749           (browse-url (concat "ftp://ftp.isc.org/usenet/control/"
3750                               hierarchy "/" name ".gz"))
3751         (let ((enable-local-variables nil))
3752           (gnus-group-read-ephemeral-group
3753            group
3754            `(nndoc ,group (nndoc-address
3755                            ,(find-file-noselect
3756                              (concat "/ftp@ftp.isc.org:/usenet/control/"
3757                                      hierarchy "/" name ".gz")))
3758                    (nndoc-article-type mbox)) t nil nil))))))
3759
3760 (defun gnus-group-describe-group (force &optional group)
3761   "Display a description of the current newsgroup."
3762   (interactive (list current-prefix-arg (gnus-group-group-name)))
3763   (let* ((method (gnus-find-method-for-group group))
3764          (mname (gnus-group-prefixed-name "" method))
3765          desc)
3766     (when (and force
3767                gnus-description-hashtb)
3768       (gnus-sethash mname nil gnus-description-hashtb))
3769     (unless group
3770       (error "No group name given"))
3771     (when (or (and gnus-description-hashtb
3772                    ;; We check whether this group's method has been
3773                    ;; queried for a description file.
3774                    (gnus-gethash mname gnus-description-hashtb))
3775               (setq desc (gnus-group-get-description group))
3776               (gnus-read-descriptions-file method))
3777       (gnus-message 1
3778                     (or desc (gnus-gethash group gnus-description-hashtb)
3779                         "No description available")))))
3780
3781 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
3782 (defun gnus-group-describe-all-groups (&optional force)
3783   "Pop up a buffer with descriptions of all newsgroups."
3784   (interactive "P")
3785   (when force
3786     (setq gnus-description-hashtb nil))
3787   (when (not (or gnus-description-hashtb
3788                  (gnus-read-all-descriptions-files)))
3789     (error "Couldn't request descriptions file"))
3790   (let ((buffer-read-only nil)
3791         b)
3792     (erase-buffer)
3793     (mapatoms
3794      (lambda (group)
3795        (setq b (point))
3796        (let ((charset (gnus-group-name-charset nil (symbol-name group))))
3797          (insert (format "      *: %-20s %s\n"
3798                          (gnus-group-name-decode
3799                           (symbol-name group) charset)
3800                          (gnus-group-name-decode
3801                           (symbol-value group) charset))))
3802        (gnus-add-text-properties
3803         b (1+ b) (list 'gnus-group group
3804                        'gnus-unread t 'gnus-marked nil
3805                        'gnus-level (1+ gnus-level-subscribed))))
3806      gnus-description-hashtb)
3807     (goto-char (point-min))
3808     (gnus-group-position-point)))
3809
3810 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
3811 (defun gnus-group-apropos (regexp &optional search-description)
3812   "List all newsgroups that have names that match a regexp."
3813   (interactive "sGnus apropos (regexp): ")
3814   (let ((prev "")
3815         (obuf (current-buffer))
3816         groups des)
3817     ;; Go through all newsgroups that are known to Gnus.
3818     (mapatoms
3819      (lambda (group)
3820        (and (symbol-name group)
3821             (string-match regexp (symbol-name group))
3822             (symbol-value group)
3823             (push (symbol-name group) groups)))
3824      gnus-active-hashtb)
3825     ;; Also go through all descriptions that are known to Gnus.
3826     (when search-description
3827       (mapatoms
3828        (lambda (group)
3829          (and (string-match regexp (symbol-value group))
3830               (push (symbol-name group) groups)))
3831        gnus-description-hashtb))
3832     (if (not groups)
3833         (gnus-message 3 "No groups matched \"%s\"." regexp)
3834       ;; Print out all the groups.
3835       (save-excursion
3836         (pop-to-buffer "*Gnus Help*")
3837         (buffer-disable-undo)
3838         (erase-buffer)
3839         (setq groups (sort groups 'string<))
3840         (while groups
3841           ;; Groups may be entered twice into the list of groups.
3842           (when (not (string= (car groups) prev))
3843             (setq prev (car groups))
3844             (let ((charset (gnus-group-name-charset nil prev)))
3845               (insert (gnus-group-name-decode prev charset) "\n")
3846               (when (and gnus-description-hashtb
3847                          (setq des (gnus-gethash (car groups)
3848                                                  gnus-description-hashtb)))
3849                 (insert "  " (gnus-group-name-decode des charset) "\n"))))
3850           (setq groups (cdr groups)))
3851         (goto-char (point-min))))
3852     (pop-to-buffer obuf)))
3853
3854 (defun gnus-group-description-apropos (regexp)
3855   "List all newsgroups that have names or descriptions that match REGEXP."
3856   (interactive "sGnus description apropos (regexp): ")
3857   (when (not (or gnus-description-hashtb
3858                  (gnus-read-all-descriptions-files)))
3859     (error "Couldn't request descriptions file"))
3860   (gnus-group-apropos regexp t))
3861
3862 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
3863 (defun gnus-group-list-matching (level regexp &optional all lowest)
3864   "List all groups with unread articles that match REGEXP.
3865 If the prefix LEVEL is non-nil, it should be a number that says which
3866 level to cut off listing groups.
3867 If ALL, also list groups with no unread articles.
3868 If LOWEST, don't list groups with level lower than LOWEST.
3869
3870 This command may read the active file."
3871   (interactive "P\nsList newsgroups matching: ")
3872   ;; First make sure active file has been read.
3873   (when (and level
3874              (> (prefix-numeric-value level) gnus-level-killed))
3875     (gnus-get-killed-groups))
3876   (funcall gnus-group-prepare-function
3877    (or level gnus-level-subscribed) (and all t) (or lowest 1) regexp)
3878   (goto-char (point-min))
3879   (gnus-group-position-point))
3880
3881 (defun gnus-group-list-all-matching (level regexp &optional lowest)
3882   "List all groups that match REGEXP.
3883 If the prefix LEVEL is non-nil, it should be a number that says which
3884 level to cut off listing groups.
3885 If LOWEST, don't list groups with level lower than LOWEST."
3886   (interactive "P\nsList newsgroups matching: ")
3887   (when level
3888     (setq level (prefix-numeric-value level)))
3889   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
3890
3891 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
3892 (defun gnus-group-save-newsrc (&optional force)
3893   "Save the Gnus startup files.
3894 If FORCE, force saving whether it is necessary or not."
3895   (interactive "P")
3896   (gnus-save-newsrc-file force))
3897
3898 (defun gnus-group-restart (&optional arg)
3899   "Force Gnus to read the .newsrc file."
3900   (interactive "P")
3901   (when (gnus-yes-or-no-p
3902          (format "Are you sure you want to restart Gnus? "))
3903     (gnus-save-newsrc-file)
3904     (gnus-clear-system)
3905     (gnus)))
3906
3907 (defun gnus-group-read-init-file ()
3908   "Read the Gnus elisp init file."
3909   (interactive)
3910   (gnus-read-init-file)
3911   (gnus-message 5 "Read %s" gnus-init-file))
3912
3913 (defun gnus-group-check-bogus-groups (&optional silent)
3914   "Check bogus newsgroups.
3915 If given a prefix, don't ask for confirmation before removing a bogus
3916 group."
3917   (interactive "P")
3918   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
3919   (gnus-group-list-groups))
3920
3921 (defun gnus-group-find-new-groups (&optional arg)
3922   "Search for new groups and add them.
3923 Each new group will be treated with `gnus-subscribe-newsgroup-method'.
3924 With 1 C-u, use the `ask-server' method to query the server for new
3925 groups.
3926 With 2 C-u's, use most complete method possible to query the server
3927 for new groups, and subscribe the new groups as zombies."
3928   (interactive "p")
3929   (gnus-find-new-newsgroups (or arg 1))
3930   (gnus-group-list-groups))
3931
3932 (defun gnus-group-edit-global-kill (&optional article group)
3933   "Edit the global kill file.
3934 If GROUP, edit that local kill file instead."
3935   (interactive "P")
3936   (setq gnus-current-kill-article article)
3937   (gnus-kill-file-edit-file group)
3938   (gnus-message
3939    6
3940    (substitute-command-keys
3941     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
3942             (if group "local" "global")))))
3943
3944 (defun gnus-group-edit-local-kill (article group)
3945   "Edit a local kill file."
3946   (interactive (list nil (gnus-group-group-name)))
3947   (gnus-group-edit-global-kill article group))
3948
3949 (defun gnus-group-force-update ()
3950   "Update `.newsrc' file."
3951   (interactive)
3952   (gnus-save-newsrc-file))
3953
3954 (defvar gnus-backlog-articles)
3955
3956 (defun gnus-group-suspend ()
3957   "Suspend the current Gnus session.
3958 In fact, cleanup buffers except for group mode buffer.
3959 The hook `gnus-suspend-gnus-hook' is called before actually suspending."
3960   (interactive)
3961   (gnus-run-hooks 'gnus-suspend-gnus-hook)
3962   (gnus-offer-save-summaries)
3963   ;; Kill Gnus buffers except for group mode buffer.
3964   (let ((group-buf (get-buffer gnus-group-buffer)))
3965     (mapcar (lambda (buf)
3966               (unless (or (member buf (list group-buf gnus-dribble-buffer))
3967                           (with-current-buffer buf
3968                             (eq major-mode 'message-mode)))
3969                 (gnus-kill-buffer buf)))
3970             (gnus-buffers))
3971     (setq gnus-backlog-articles nil)
3972     (gnus-kill-gnus-frames)
3973     (when group-buf
3974       (bury-buffer group-buf)
3975       (delete-windows-on group-buf t))))
3976
3977 (defun gnus-group-clear-dribble ()
3978   "Clear all information from the dribble buffer."
3979   (interactive)
3980   (gnus-dribble-clear)
3981   (gnus-message 7 "Cleared dribble buffer"))
3982
3983 (defun gnus-group-exit ()
3984   "Quit reading news after updating .newsrc.eld and .newsrc.
3985 The hook `gnus-exit-gnus-hook' is called before actually exiting."
3986   (interactive)
3987   (when
3988       (or noninteractive                ;For gnus-batch-kill
3989           (not gnus-interactive-exit)   ;Without confirmation
3990           gnus-expert-user
3991           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
3992     (gnus-run-hooks 'gnus-exit-gnus-hook)
3993     ;; Offer to save data from non-quitted summary buffers.
3994     (gnus-offer-save-summaries)
3995     ;; Save the newsrc file(s).
3996     (gnus-save-newsrc-file)
3997     ;; Kill-em-all.
3998     (gnus-close-backends)
3999     ;; Reset everything.
4000     (gnus-clear-system)
4001     ;; Allow the user to do things after cleaning up.
4002     (gnus-run-hooks 'gnus-after-exiting-gnus-hook)))
4003
4004 (defun gnus-group-quit ()
4005   "Quit reading news without updating .newsrc.eld or .newsrc.
4006 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4007   (interactive)
4008   (when (or noninteractive              ;For gnus-batch-kill
4009             (zerop (buffer-size))
4010             (not (gnus-server-opened gnus-select-method))
4011             gnus-expert-user
4012             (not gnus-current-startup-file)
4013             (gnus-yes-or-no-p
4014              (format "Quit reading news without saving %s? "
4015                      (file-name-nondirectory gnus-current-startup-file))))
4016     (gnus-run-hooks 'gnus-exit-gnus-hook)
4017     (gnus-configure-windows 'group t)
4018     (when (and (gnus-buffer-live-p gnus-dribble-buffer)
4019                (not (zerop (save-excursion
4020                             (set-buffer gnus-dribble-buffer)
4021                             (buffer-size)))))
4022       (gnus-dribble-enter
4023        ";;; Gnus was exited on purpose without saving the .newsrc files."))
4024     (gnus-dribble-save)
4025     (gnus-close-backends)
4026     (gnus-clear-system)
4027     (gnus-kill-buffer gnus-group-buffer)
4028     ;; Allow the user to do things after cleaning up.
4029     (gnus-run-hooks 'gnus-after-exiting-gnus-hook)))
4030
4031 (defun gnus-group-describe-briefly ()
4032   "Give a one line description of the group mode commands."
4033   (interactive)
4034   (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")))
4035
4036 (defun gnus-group-browse-foreign-server (method)
4037   "Browse a foreign news server.
4038 If called interactively, this function will ask for a select method
4039  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
4040 If not, METHOD should be a list where the first element is the method
4041 and the second element is the address."
4042   (interactive
4043    (list (let ((how (completing-read
4044                      "Which back end: "
4045                      (append gnus-valid-select-methods gnus-server-alist)
4046                      nil t (cons "nntp" 0) 'gnus-method-history)))
4047            ;; We either got a back end name or a virtual server name.
4048            ;; If the first, we also need an address.
4049            (if (assoc how gnus-valid-select-methods)
4050                (list (intern how)
4051                      ;; Suggested by mapjph@bath.ac.uk.
4052                      (completing-read
4053                       "Address: "
4054                       (mapcar 'list gnus-secondary-servers)))
4055              ;; We got a server name.
4056              how))))
4057   (gnus-browse-foreign-server method))
4058
4059 (defun gnus-group-set-info (info &optional method-only-group part)
4060   (when (or info part)
4061     (let* ((entry (gnus-group-entry
4062                    (or method-only-group (gnus-info-group info))))
4063            (part-info info)
4064            (info (if method-only-group (nth 2 entry) info))
4065            method)
4066       (when method-only-group
4067         (unless entry
4068           (error "Trying to change non-existent group %s" method-only-group))
4069         ;; We have received parts of the actual group info - either the
4070         ;; select method or the group parameters.        We first check
4071         ;; whether we have to extend the info, and if so, do that.
4072         (let ((len (length info))
4073               (total (if (eq part 'method) 5 6)))
4074           (when (< len total)
4075             (setcdr (nthcdr (1- len) info)
4076                     (make-list (- total len) nil)))
4077           ;; Then we enter the new info.
4078           (setcar (nthcdr (1- total) info) part-info)))
4079       (unless entry
4080         ;; This is a new group, so we just create it.
4081         (save-excursion
4082           (set-buffer gnus-group-buffer)
4083           (setq method (gnus-info-method info))
4084           (when (gnus-server-equal method "native")
4085             (setq method nil))
4086           (save-excursion
4087             (set-buffer gnus-group-buffer)
4088             (if method
4089                 ;; It's a foreign group...
4090                 (gnus-group-make-group
4091                  (gnus-group-real-name (gnus-info-group info))
4092                  (if (stringp method) method
4093                    (prin1-to-string (car method)))
4094                  (and (consp method)
4095                       (nth 1 (gnus-info-method info))))
4096               ;; It's a native group.
4097               (gnus-group-make-group (gnus-info-group info))))
4098           (gnus-message 6 "Note: New group created")
4099           (setq entry
4100                 (gnus-group-entry (gnus-group-prefixed-name
4101                                    (gnus-group-real-name (gnus-info-group info))
4102                                    (or (gnus-info-method info) gnus-select-method))))))
4103       ;; Whether it was a new group or not, we now have the entry, so we
4104       ;; can do the update.
4105       (if entry
4106           (progn
4107             (setcar (nthcdr 2 entry) info)
4108             (when (and (not (eq (car entry) t))
4109                        (gnus-active (gnus-info-group info)))
4110               (setcar entry (length
4111                              (gnus-list-of-unread-articles (car info))))))
4112         (error "No such group: %s" (gnus-info-group info))))))
4113
4114 (defun gnus-group-set-method-info (group select-method)
4115   (gnus-group-set-info select-method group 'method))
4116
4117 (defun gnus-group-set-params-info (group params)
4118   (gnus-group-set-info params group 'params))
4119
4120 (defun gnus-add-marked-articles (group type articles &optional info force)
4121   ;; Add ARTICLES of TYPE to the info of GROUP.
4122   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
4123   ;; add, but replace marked articles of TYPE with ARTICLES.
4124   (let ((info (or info (gnus-get-info group)))
4125         marked m)
4126     (or (not info)
4127         (and (not (setq marked (nthcdr 3 info)))
4128              (or (null articles)
4129                  (setcdr (nthcdr 2 info)
4130                          (list (list (cons type (gnus-compress-sequence
4131                                                  articles t)))))))
4132         (and (not (setq m (assq type (car marked))))
4133              (or (null articles)
4134                  (setcar marked
4135                          (cons (cons type (gnus-compress-sequence articles t) )
4136                                (car marked)))))
4137         (if force
4138             (if (null articles)
4139                 (setcar (nthcdr 3 info)
4140                         (gnus-delete-alist type (car marked)))
4141               (setcdr m (gnus-compress-sequence articles t)))
4142           (setcdr m (gnus-compress-sequence
4143                      (sort (nconc (gnus-uncompress-range (cdr m))
4144                                   (copy-sequence articles)) '<) t))))))
4145
4146 (defun gnus-add-mark (group mark article)
4147   "Mark ARTICLE in GROUP with MARK, whether the group is displayed or not."
4148   (let ((buffer (gnus-summary-buffer-name group)))
4149     (if (gnus-buffer-live-p buffer)
4150         (save-excursion
4151           (set-buffer (get-buffer buffer))
4152           (gnus-summary-add-mark article mark))
4153       (gnus-add-marked-articles group (cdr (assq mark gnus-article-mark-lists))
4154                                 (list article)))))
4155
4156 ;;;
4157 ;;; Group timestamps
4158 ;;;
4159
4160 (defun gnus-group-set-timestamp ()
4161   "Change the timestamp of the current group to the current time.
4162 This function can be used in hooks like `gnus-select-group-hook'
4163 or `gnus-group-catchup-group-hook'."
4164   (when gnus-newsgroup-name
4165     (let ((time (current-time)))
4166       (setcdr (cdr time) nil)
4167       (gnus-group-set-parameter gnus-newsgroup-name 'timestamp time))))
4168
4169 (defsubst gnus-group-timestamp (group)
4170   "Return the timestamp for GROUP."
4171   (gnus-group-get-parameter group 'timestamp t))
4172
4173 (defun gnus-group-timestamp-delta (group)
4174   "Return the offset in seconds from the timestamp for GROUP to the current time, as a floating point number."
4175   (let* ((time (or (gnus-group-timestamp group)
4176                    (list 0 0)))
4177          (delta (subtract-time (current-time) time)))
4178     (+ (* (nth 0 delta) 65536.0)
4179        (nth 1 delta))))
4180
4181 (defun gnus-group-timestamp-string (group)
4182   "Return a string of the timestamp for GROUP."
4183   (let ((time (gnus-group-timestamp group)))
4184     (if (not time)
4185         ""
4186       (gnus-time-iso8601 time))))
4187
4188 (defun gnus-group-list-cached (level &optional lowest)
4189   "List all groups with cached articles.
4190 If the prefix LEVEL is non-nil, it should be a number that says which
4191 level to cut off listing groups.
4192 If LOWEST, don't list groups with level lower than LOWEST.
4193
4194 This command may read the active file."
4195   (interactive "P")
4196   (when level
4197     (setq level (prefix-numeric-value level)))
4198   (when (or (not level) (>= level gnus-level-zombie))
4199     (gnus-cache-open))
4200   (funcall gnus-group-prepare-function
4201            (or level gnus-level-subscribed)
4202            #'(lambda (info)
4203                (let ((marks (gnus-info-marks info)))
4204                  (assq 'cache marks)))
4205            lowest
4206            #'(lambda (group)
4207                (or (gnus-gethash group
4208                                  gnus-cache-active-hashtb)
4209                    ;; Cache active file might use "."
4210                    ;; instead of ":".
4211                    (gnus-gethash
4212                     (mapconcat 'identity
4213                                (split-string group ":")
4214                                ".")
4215                     gnus-cache-active-hashtb))))
4216   (goto-char (point-min))
4217   (gnus-group-position-point))
4218
4219 (defun gnus-group-list-dormant (level &optional lowest)
4220   "List all groups with dormant articles.
4221 If the prefix LEVEL is non-nil, it should be a number that says which
4222 level to cut off listing groups.
4223 If LOWEST, don't list groups with level lower than LOWEST.
4224
4225 This command may read the active file."
4226   (interactive "P")
4227   (when level
4228     (setq level (prefix-numeric-value level)))
4229   (when (or (not level) (>= level gnus-level-zombie))
4230     (gnus-cache-open))
4231   (funcall gnus-group-prepare-function
4232            (or level gnus-level-subscribed)
4233            #'(lambda (info)
4234                (let ((marks (gnus-info-marks info)))
4235                  (assq 'dormant marks)))
4236            lowest
4237            'ignore)
4238   (goto-char (point-min))
4239   (gnus-group-position-point))
4240
4241 (defun gnus-group-listed-groups ()
4242   "Return a list of listed groups."
4243   (let (point groups)
4244     (goto-char (point-min))
4245     (while (setq point (text-property-not-all (point) (point-max)
4246                                               'gnus-group nil))
4247       (goto-char point)
4248       (push (symbol-name (get-text-property point 'gnus-group)) groups)
4249       (forward-char 1))
4250     groups))
4251
4252 (defun gnus-group-list-plus (&optional args)
4253   "List groups plus the current selection."
4254   (interactive "P")
4255   (let ((gnus-group-listed-groups (gnus-group-listed-groups))
4256         (gnus-group-list-mode gnus-group-list-mode) ;; Save it.
4257         func)
4258     (push last-command-event unread-command-events)
4259     (if (featurep 'xemacs)
4260         (push (make-event 'key-press '(key ?A)) unread-command-events)
4261       (push ?A unread-command-events))
4262     (let (gnus-pick-mode keys)
4263       (setq keys (if (featurep 'xemacs)
4264                      (events-to-keys (read-key-sequence nil))
4265                    (read-key-sequence nil)))
4266       (setq func (lookup-key (current-local-map) keys)))
4267     (if (or (not func)
4268             (numberp func))
4269         (ding)
4270       (call-interactively func))))
4271
4272 (defun gnus-group-list-flush (&optional args)
4273   "Flush groups from the current selection."
4274   (interactive "P")
4275   (let ((gnus-group-list-option 'flush))
4276     (gnus-group-list-plus args)))
4277
4278 (defun gnus-group-list-limit (&optional args)
4279   "List groups limited within the current selection."
4280   (interactive "P")
4281   (let ((gnus-group-list-option 'limit))
4282     (gnus-group-list-plus args)))
4283
4284 (defun gnus-group-mark-article-read (group article)
4285   "Mark ARTICLE read."
4286   (let ((buffer (gnus-summary-buffer-name group))
4287         (mark gnus-read-mark)
4288         active n)
4289     (if (get-buffer buffer)
4290         (with-current-buffer buffer
4291           (setq active gnus-newsgroup-active)
4292           (gnus-activate-group group)
4293           (when gnus-newsgroup-prepared
4294             (when (and gnus-newsgroup-auto-expire
4295                        (memq mark gnus-auto-expirable-marks))
4296               (setq mark gnus-expirable-mark))
4297             (setq mark (gnus-request-update-mark
4298                         group article mark))
4299             (gnus-mark-article-as-read article mark)
4300             (setq gnus-newsgroup-active (gnus-active group))
4301             (when active
4302               (setq n (1+ (cdr active)))
4303               (while (<= n (cdr gnus-newsgroup-active))
4304                 (unless (eq n article)
4305                   (push n gnus-newsgroup-unselected))
4306                 (setq n (1+ n)))
4307               (setq gnus-newsgroup-unselected
4308                     (nreverse gnus-newsgroup-unselected)))))
4309       (gnus-activate-group group)
4310       (gnus-group-make-articles-read group (list article))
4311       (when (gnus-group-auto-expirable-p group)
4312         (gnus-add-marked-articles
4313          group 'expire (list article))))))
4314
4315 (provide 'gnus-group)
4316
4317 ;;; arch-tag: 2eb5440f-0bca-4091-814c-e37817536af6
4318 ;;; gnus-group.el ends here