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