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