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