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