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