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