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