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