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