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