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