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