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