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