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