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