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