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