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