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