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