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