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