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