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