*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95,96 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
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 '(run-hooks 'gnus-load-hook))
30
31 (require 'mail-utils)
32 (require 'timezone)
33 (require 'nnheader)
34 (require 'message)
35
36 (eval-when-compile (require 'cl))
37
38 ;; Site dependent variables.  These variables should be defined in
39 ;; paths.el.
40
41 (defvar gnus-default-nntp-server nil
42   "Specify a default NNTP server.
43 This variable should be defined in paths.el, and should never be set
44 by the user.
45 If you want to change servers, you should use `gnus-select-method'.
46 See the documentation to that variable.")
47
48 (defvar gnus-backup-default-subscribed-newsgroups
49   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
50   "Default default new newsgroups the first time Gnus is run.
51 Should be set in paths.el, and shouldn't be touched by the user.")
52
53 (defvar gnus-local-domain nil
54   "Local domain name without a host name.
55 The DOMAINNAME environment variable is used instead if it is defined.
56 If the `system-name' function returns the full Internet name, there is
57 no need to set this variable.")
58
59 (defvar gnus-local-organization nil
60   "String with a description of what organization (if any) the user belongs to.
61 The ORGANIZATION environment variable is used instead if it is defined.
62 If this variable contains a function, this function will be called
63 with the current newsgroup name as the argument.  The function should
64 return a string.
65
66 In any case, if the string (either in the variable, in the environment
67 variable, or returned by the function) is a file name, the contents of
68 this file will be used as the organization.")
69
70 ;; Customization variables
71
72 ;; Don't touch this variable.
73 (defvar gnus-nntp-service "nntp"
74   "*NNTP service name (\"nntp\" or 119).
75 This is an obsolete variable, which is scarcely used.  If you use an
76 nntp server for your newsgroup and want to change the port number
77 used to 899, you would say something along these lines:
78
79  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
80
81 (defvar gnus-nntpserver-file "/etc/nntpserver"
82   "*A file with only the name of the nntp server in it.")
83
84 ;; This function is used to check both the environment variable
85 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
86 ;; an nntp server name default.
87 (defun gnus-getenv-nntpserver ()
88   (or (getenv "NNTPSERVER")
89       (and (file-readable-p gnus-nntpserver-file)
90            (save-excursion
91              (set-buffer (get-buffer-create " *gnus nntp*"))
92              (buffer-disable-undo (current-buffer))
93              (insert-file-contents gnus-nntpserver-file)
94              (let ((name (buffer-string)))
95                (prog1
96                    (if (string-match "^[ \t\n]*$" name)
97                        nil
98                      name)
99                  (kill-buffer (current-buffer))))))))
100
101 (defvar gnus-select-method
102   (nconc
103    (list 'nntp (or (condition-case ()
104                        (gnus-getenv-nntpserver)
105                      (error nil))
106                    (if (and gnus-default-nntp-server
107                             (not (string= gnus-default-nntp-server "")))
108                        gnus-default-nntp-server)
109                    (system-name)))
110    (if (or (null gnus-nntp-service)
111            (equal gnus-nntp-service "nntp"))
112        nil
113      (list gnus-nntp-service)))
114   "*Default method for selecting a newsgroup.
115 This variable should be a list, where the first element is how the
116 news is to be fetched, the second is the address.
117
118 For instance, if you want to get your news via NNTP from
119 \"flab.flab.edu\", you could say:
120
121 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
122
123 If you want to use your local spool, say:
124
125 (setq gnus-select-method (list 'nnspool (system-name)))
126
127 If you use this variable, you must set `gnus-nntp-server' to nil.
128
129 There is a lot more to know about select methods and virtual servers -
130 see the manual for details.")
131
132 (defvar gnus-message-archive-method 
133   '(nnfolder "archive" (nnfolder-directory "~/Mail/archive/")
134              (nnfolder-active-file "~/Mail/archive/active")
135              (nnfolder-get-new-mail nil)
136              (nnfolder-inhibit-expiry t))
137   "*Method used for archiving messages you've sent.
138 This should be a mail method.")
139
140 (defvar gnus-refer-article-method nil
141   "*Preferred method for fetching an article by Message-ID.
142 If you are reading news from the local spool (with nnspool), fetching
143 articles by Message-ID is painfully slow.  By setting this method to an
144 nntp method, you might get acceptable results.
145
146 The value of this variable must be a valid select method as discussed
147 in the documentation of `gnus-select-method'.")
148
149 (defvar gnus-secondary-select-methods nil
150   "*A list of secondary methods that will be used for reading news.
151 This is a list where each element is a complete select method (see
152 `gnus-select-method').
153
154 If, for instance, you want to read your mail with the nnml backend,
155 you could set this variable:
156
157 (setq gnus-secondary-select-methods '((nnml \"\")))")
158
159 (defvar gnus-secondary-servers nil
160   "*List of NNTP servers that the user can choose between interactively.
161 To make Gnus query you for a server, you have to give `gnus' a
162 non-numeric prefix - `C-u M-x gnus', in short.")
163
164 (defvar gnus-nntp-server nil
165   "*The name of the host running the NNTP server.
166 This variable is semi-obsolete.  Use the `gnus-select-method'
167 variable instead.")
168
169 (defvar gnus-startup-file "~/.newsrc"
170   "*Your `.newsrc' file.
171 `.newsrc-SERVER' will be used instead if that exists.")
172
173 (defvar gnus-init-file "~/.gnus"
174   "*Your Gnus elisp startup file.
175 If a file with the .el or .elc suffixes exist, it will be read
176 instead.")
177
178 (defvar gnus-group-faq-directory
179   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
180     "/ftp@sunsite.auc.dk:/pub/usenet/"
181     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
182     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
183     "/ftp@rtfm.mit.edu:/pub/usenet/"
184     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
185     "/ftp@ftp.sunet.se:/pub/usenet/"
186     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
187     "/ftp@hwarang.postech.ac.kr:/pub/usenet/"
188     "/ftp@ftp.hk.super.net:/mirror/faqs/")
189   "*Directory where the group FAQs are stored.
190 This will most commonly be on a remote machine, and the file will be
191 fetched by ange-ftp.
192
193 This variable can also be a list of directories.  In that case, the
194 first element in the list will be used by default, and the others will
195 be used as backup sites.
196
197 Note that Gnus uses an aol machine as the default directory.  If this
198 feels fundamentally unclean, just think of it as a way to finally get
199 something of value back from them.
200
201 If the default site is too slow, try one of these:
202
203    North America: mirrors.aol.com                /pub/rtfm/usenet
204                   ftp.seas.gwu.edu               /pub/rtfm
205                   rtfm.mit.edu                   /pub/usenet
206    Europe:        ftp.uni-paderborn.de           /pub/FAQ
207                   src.doc.ic.ac.uk               /usenet/news-FAQS
208                   ftp.sunet.se                   /pub/usenet
209                   sunsite.auc.dk                 /pub/usenet
210    Asia:          nctuccca.edu.tw                /USENET/FAQ
211                   hwarang.postech.ac.kr          /pub/usenet
212                   ftp.hk.super.net               /mirror/faqs")
213
214 (defvar gnus-group-archive-directory
215   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
216   "*The address of the (ding) archives.")
217
218 (defvar gnus-group-recent-archive-directory
219   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
220   "*The address of the most recent (ding) articles.")
221
222 (defvar gnus-default-subscribed-newsgroups nil
223   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
224 It should be a list of strings.
225 If it is `t', Gnus will not do anything special the first time it is
226 started; it'll just use the normal newsgroups subscription methods.")
227
228 (defvar gnus-use-cross-reference t
229   "*Non-nil means that cross referenced articles will be marked as read.
230 If nil, ignore cross references.  If t, mark articles as read in
231 subscribed newsgroups.  If neither t nor nil, mark as read in all
232 newsgroups.")
233
234 (defvar gnus-single-article-buffer t
235   "*If non-nil, display all articles in the same buffer.
236 If nil, each group will get its own article buffer.")
237
238 (defvar gnus-use-dribble-file t
239   "*Non-nil means that Gnus will use a dribble file to store user updates.
240 If Emacs should crash without saving the .newsrc files, complete
241 information can be restored from the dribble file.")
242
243 (defvar gnus-dribble-directory nil
244   "*The directory where dribble files will be saved.
245 If this variable is nil, the directory where the .newsrc files are
246 saved will be used.")
247
248 (defvar gnus-asynchronous nil
249   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
250
251 (defvar gnus-kill-summary-on-exit t
252   "*If non-nil, kill the summary buffer when you exit from it.
253 If nil, the summary will become a \"*Dead Summary*\" buffer, and
254 it will be killed sometime later.")
255
256 (defvar gnus-large-newsgroup 200
257   "*The number of articles which indicates a large newsgroup.
258 If the number of articles in a newsgroup is greater than this value,
259 confirmation is required for selecting the newsgroup.")
260
261 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
262 (defvar gnus-no-groups-message "No news is horrible news"
263   "*Message displayed by Gnus when no groups are available.")
264
265 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
266   "*Non-nil means that the default name of a file to save articles in is the group name.
267 If it's nil, the directory form of the group name is used instead.
268
269 If this variable is a list, and the list contains the element
270 `not-score', long file names will not be used for score files; if it
271 contains the element `not-save', long file names will not be used for
272 saving; and if it contains the element `not-kill', long file names
273 will not be used for kill files.")
274
275 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
276   "*Name of the directory articles will be saved in (default \"~/News\").
277 Initialized from the SAVEDIR environment variable.")
278
279 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
280   "*Name of the directory where kill files will be stored (default \"~/News\").
281 Initialized from the SAVEDIR environment variable.")
282
283 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
284   "*A function to save articles in your favorite format.
285 The function must be interactively callable (in other words, it must
286 be an Emacs command).
287
288 Gnus provides the following functions:
289
290 * gnus-summary-save-in-rmail (Rmail format)
291 * gnus-summary-save-in-mail (Unix mail format)
292 * gnus-summary-save-in-folder (MH folder)
293 * gnus-summary-save-in-file (article format).
294 * gnus-summary-save-in-vm (use VM's folder format).")
295
296 (defvar gnus-prompt-before-saving 'always
297   "*This variable says how much prompting is to be done when saving articles.
298 If it is nil, no prompting will be done, and the articles will be
299 saved to the default files.  If this variable is `always', each and
300 every article that is saved will be preceded by a prompt, even when
301 saving large batches of articles.  If this variable is neither nil not
302 `always', there the user will be prompted once for a file name for
303 each invocation of the saving commands.")
304
305 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
306   "*A function generating a file name to save articles in Rmail format.
307 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
308
309 (defvar gnus-mail-save-name (function gnus-plain-save-name)
310   "*A function generating a file name to save articles in Unix mail format.
311 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
312
313 (defvar gnus-folder-save-name (function gnus-folder-save-name)
314   "*A function generating a file name to save articles in MH folder.
315 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
316
317 (defvar gnus-file-save-name (function gnus-numeric-save-name)
318   "*A function generating a file name to save articles in article format.
319 The function is called with NEWSGROUP, HEADERS, and optional
320 LAST-FILE.")
321
322 (defvar gnus-split-methods
323   '((gnus-article-archive-name))
324   "*Variable used to suggest where articles are to be saved.
325 For instance, if you would like to save articles related to Gnus in
326 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
327 you could set this variable to something like:
328
329  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
330    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
331
332 This variable is an alist where the where the key is the match and the
333 value is a list of possible files to save in if the match is non-nil.
334
335 If the match is a string, it is used as a regexp match on the
336 article.  If the match is a symbol, that symbol will be funcalled
337 from the buffer of the article to be saved with the newsgroup as the
338 parameter.  If it is a list, it will be evaled in the same buffer.
339
340 If this form or function returns a string, this string will be used as
341 a possible file name; and if it returns a non-nil list, that list will
342 be used as possible file names.")
343
344 (defvar gnus-move-split-methods nil
345   "*Variable used to suggest where articles are to be moved to.
346 It uses the same syntax as the `gnus-split-methods' variable.")
347
348 (defvar gnus-save-score nil
349   "*If non-nil, save group scoring info.")
350
351 (defvar gnus-use-adaptive-scoring nil
352   "*If non-nil, use some adaptive scoring scheme.")
353
354 (defvar gnus-use-cache nil
355   "*If nil, Gnus will ignore the article cache.
356 If `passive', it will allow entering (and reading) articles
357 explicitly entered into the cache.  If anything else, use the
358 cache to the full extent of the law.")
359
360 (defvar gnus-use-trees nil
361   "*If non-nil, display a thread tree buffer.")
362
363 (defvar gnus-use-grouplens nil
364   "*If non-nil, use GroupLens ratings.")
365
366 (defvar gnus-keep-backlog nil
367   "*If non-nil, Gnus will keep read articles for later re-retrieval.
368 If it is a number N, then Gnus will only keep the last N articles
369 read.  If it is neither nil nor a number, Gnus will keep all read
370 articles.  This is not a good idea.")
371
372 (defvar gnus-use-nocem nil
373   "*If non-nil, Gnus will read NoCeM cancel messages.")
374
375 (defvar gnus-use-demon nil
376   "If non-nil, Gnus might use some demons.")
377
378 (defvar gnus-use-scoring t
379   "*If non-nil, enable scoring.")
380
381 (defvar gnus-use-picons nil
382   "*If non-nil, display picons.")
383
384 (defvar gnus-fetch-old-headers nil
385   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
386 If an unread article in the group refers to an older, already read (or
387 just marked as read) article, the old article will not normally be
388 displayed in the Summary buffer.  If this variable is non-nil, Gnus
389 will attempt to grab the headers to the old articles, and thereby
390 build complete threads.  If it has the value `some', only enough
391 headers to connect otherwise loose threads will be displayed.
392 This variable can also be a number.  In that case, no more than that
393 number of old headers will be fetched.
394
395 The server has to support NOV for any of this to work.")
396
397 ;see gnus-cus.el
398 ;(defvar gnus-visual t
399 ;  "*If non-nil, will do various highlighting.
400 ;If nil, no mouse highlights (or any other highlights) will be
401 ;performed.  This might speed up Gnus some when generating large group
402 ;and summary buffers.")
403
404 (defvar gnus-novice-user t
405   "*Non-nil means that you are a usenet novice.
406 If non-nil, verbose messages may be displayed and confirmations may be
407 required.")
408
409 (defvar gnus-expert-user nil
410   "*Non-nil means that you will never be asked for confirmation about anything.
411 And that means *anything*.")
412
413 (defvar gnus-verbose 7
414   "*Integer that says how verbose Gnus should be.
415 The higher the number, the more messages Gnus will flash to say what
416 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
417 display most important messages; and at ten, Gnus will keep on
418 jabbering all the time.")
419
420 (defvar gnus-keep-same-level nil
421   "*Non-nil means that the next newsgroup after the current will be on the same level.
422 When you type, for instance, `n' after reading the last article in the
423 current newsgroup, you will go to the next newsgroup.  If this variable
424 is nil, the next newsgroup will be the next from the group
425 buffer.
426 If this variable is non-nil, Gnus will either put you in the
427 next newsgroup with the same level, or, if no such newsgroup is
428 available, the next newsgroup with the lowest possible level higher
429 than the current level.
430 If this variable is `best', Gnus will make the next newsgroup the one
431 with the best level.")
432
433 (defvar gnus-summary-make-false-root 'adopt
434   "*nil means that Gnus won't gather loose threads.
435 If the root of a thread has expired or been read in a previous
436 session, the information necessary to build a complete thread has been
437 lost.  Instead of having many small sub-threads from this original thread
438 scattered all over the summary buffer, Gnus can gather them.
439
440 If non-nil, Gnus will try to gather all loose sub-threads from an
441 original thread into one large thread.
442
443 If this variable is non-nil, it should be one of `none', `adopt',
444 `dummy' or `empty'.
445
446 If this variable is `none', Gnus will not make a false root, but just
447 present the sub-threads after another.
448 If this variable is `dummy', Gnus will create a dummy root that will
449 have all the sub-threads as children.
450 If this variable is `adopt', Gnus will make one of the \"children\"
451 the parent and mark all the step-children as such.
452 If this variable is `empty', the \"children\" are printed with empty
453 subject fields.  (Or rather, they will be printed with a string
454 given by the `gnus-summary-same-subject' variable.)")
455
456 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
457   "*A regexp to match subjects to be excluded from loose thread gathering.
458 As loose thread gathering is done on subjects only, that means that
459 there can be many false gatherings performed.  By rooting out certain
460 common subjects, gathering might become saner.")
461
462 (defvar gnus-summary-gather-subject-limit nil
463   "*Maximum length of subject comparisons when gathering loose threads.
464 Use nil to compare full subjects.  Setting this variable to a low
465 number will help gather threads that have been corrupted by
466 newsreaders chopping off subject lines, but it might also mean that
467 unrelated articles that have subject that happen to begin with the
468 same few characters will be incorrectly gathered.
469
470 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
471 comparing subjects.")
472
473 (defvar gnus-simplify-ignored-prefixes nil
474   "*Regexp, matches for which are removed from subject lines when simplifying.")
475
476 (defvar gnus-build-sparse-threads nil
477   "*If non-nil, fill in the gaps in threads.
478 If `some', only fill in the gaps that are needed to tie loose threads
479 together.  If `more', fill in all leaf nodes that Gnus can find.  If
480 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
481
482 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
483   "Function used for gathering loose threads.
484 There are two pre-defined functions: `gnus-gather-threads-by-subject',
485 which only takes Subjects into consideration; and
486 `gnus-gather-threads-by-references', which compared the References
487 headers of the articles to find matches.")
488
489 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
490 (defvar gnus-summary-same-subject ""
491   "*String indicating that the current article has the same subject as the previous.
492 This variable will only be used if the value of
493 `gnus-summary-make-false-root' is `empty'.")
494
495 (defvar gnus-summary-goto-unread t
496   "*If non-nil, marking commands will go to the next unread article.
497 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
498 whether it is read or not.")
499
500 (defvar gnus-group-goto-unread t
501   "*If non-nil, movement commands will go to the next unread and subscribed group.")
502
503 (defvar gnus-goto-next-group-when-activating t
504   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
505
506 (defvar gnus-check-new-newsgroups t
507   "*Non-nil means that Gnus will add new newsgroups at startup.
508 If this variable is `ask-server', Gnus will ask the server for new
509 groups since the last time it checked.  This means that the killed list
510 is no longer necessary, so you could set `gnus-save-killed-list' to
511 nil.
512
513 A variant is to have this variable be a list of select methods.  Gnus
514 will then use the `ask-server' method on all these select methods to
515 query for new groups from all those servers.
516
517 Eg.
518   (setq gnus-check-new-newsgroups
519         '((nntp \"some.server\") (nntp \"other.server\")))
520
521 If this variable is nil, then you have to tell Gnus explicitly to
522 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
523
524 (defvar gnus-check-bogus-newsgroups nil
525   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
526 If this variable is nil, then you have to tell Gnus explicitly to
527 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
528
529 (defvar gnus-read-active-file t
530   "*Non-nil means that Gnus will read the entire active file at startup.
531 If this variable is nil, Gnus will only know about the groups in your
532 `.newsrc' file.
533
534 If this variable is `some', Gnus will try to only read the relevant
535 parts of the active file from the server.  Not all servers support
536 this, and it might be quite slow with other servers, but this should
537 generally be faster than both the t and nil value.
538
539 If you set this variable to nil or `some', you probably still want to
540 be told about new newsgroups that arrive.  To do that, set
541 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
542 properly with all servers.")
543
544 (defvar gnus-level-subscribed 5
545   "*Groups with levels less than or equal to this variable are subscribed.")
546
547 (defvar gnus-level-unsubscribed 7
548   "*Groups with levels less than or equal to this variable are unsubscribed.
549 Groups with levels less than `gnus-level-subscribed', which should be
550 less than this variable, are subscribed.")
551
552 (defvar gnus-level-zombie 8
553   "*Groups with this level are zombie groups.")
554
555 (defvar gnus-level-killed 9
556   "*Groups with this level are killed.")
557
558 (defvar gnus-level-default-subscribed 3
559   "*New subscribed groups will be subscribed at this level.")
560
561 (defvar gnus-level-default-unsubscribed 6
562   "*New unsubscribed groups will be unsubscribed at this level.")
563
564 (defvar gnus-activate-level (1+ gnus-level-subscribed)
565   "*Groups higher than this level won't be activated on startup.
566 Setting this variable to something log might save lots of time when
567 you have many groups that you aren't interested in.")
568
569 (defvar gnus-activate-foreign-newsgroups 4
570   "*If nil, Gnus will not check foreign newsgroups at startup.
571 If it is non-nil, it should be a number between one and nine.  Foreign
572 newsgroups that have a level lower or equal to this number will be
573 activated on startup.  For instance, if you want to active all
574 subscribed newsgroups, but not the rest, you'd set this variable to
575 `gnus-level-subscribed'.
576
577 If you subscribe to lots of newsgroups from different servers, startup
578 might take a while.  By setting this variable to nil, you'll save time,
579 but you won't be told how many unread articles there are in the
580 groups.")
581
582 (defvar gnus-save-newsrc-file t
583   "*Non-nil means that Gnus will save the `.newsrc' file.
584 Gnus always saves its own startup file, which is called
585 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
586 be readily understood by other newsreaders.  If you don't plan on
587 using other newsreaders, set this variable to nil to save some time on
588 exit.")
589
590 (defvar gnus-save-killed-list t
591   "*If non-nil, save the list of killed groups to the startup file.
592 If you set this variable to nil, you'll save both time (when starting
593 and quitting) and space (both memory and disk), but it will also mean
594 that Gnus has no record of which groups are new and which are old, so
595 the automatic new newsgroups subscription methods become meaningless.
596
597 You should always set `gnus-check-new-newsgroups' to `ask-server' or
598 nil if you set this variable to nil.")
599
600 (defvar gnus-interactive-catchup t
601   "*If non-nil, require your confirmation when catching up a group.")
602
603 (defvar gnus-interactive-post t
604   "*If non-nil, group name will be asked for when posting.")
605
606 (defvar gnus-interactive-exit t
607   "*If non-nil, require your confirmation when exiting Gnus.")
608
609 (defvar gnus-kill-killed t
610   "*If non-nil, Gnus will apply kill files to already killed articles.
611 If it is nil, Gnus will never apply kill files to articles that have
612 already been through the scoring process, which might very well save lots
613 of time.")
614
615 (defvar gnus-extract-address-components 'gnus-extract-address-components
616   "*Function for extracting address components from a From header.
617 Two pre-defined function exist: `gnus-extract-address-components',
618 which is the default, quite fast, and too simplistic solution, and
619 `mail-extract-address-components', which works much better, but is
620 slower.")
621
622 (defvar gnus-summary-default-score 0
623   "*Default article score level.
624 If this variable is nil, scoring will be disabled.")
625
626 (defvar gnus-summary-zcore-fuzz 0
627   "*Fuzziness factor for the zcore in the summary buffer.
628 Articles with scores closer than this to `gnus-summary-default-score'
629 will not be marked.")
630
631 (defvar gnus-simplify-subject-fuzzy-regexp nil
632   "*Strings to be removed when doing fuzzy matches.
633 This can either be a regular expression or list of regular expressions
634 that will be removed from subject strings if fuzzy subject
635 simplification is selected.")
636
637 (defvar gnus-permanently-visible-groups nil
638   "*Regexp to match groups that should always be listed in the group buffer.
639 This means that they will still be listed when there are no unread
640 articles in the groups.")
641
642 (defvar gnus-list-groups-with-ticked-articles t
643   "*If non-nil, list groups that have only ticked articles.
644 If nil, only list groups that have unread articles.")
645
646 (defvar gnus-group-default-list-level gnus-level-subscribed
647   "*Default listing level.
648 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
649
650 (defvar gnus-group-use-permanent-levels nil
651   "*If non-nil, once you set a level, Gnus will use this level.")
652
653 (defvar gnus-group-list-inactive-groups t
654   "*If non-nil, inactive groups will be listed.")
655
656 (defvar gnus-show-mime nil
657   "*If non-nil, do mime processing of articles.
658 The articles will simply be fed to the function given by
659 `gnus-show-mime-method'.")
660
661 (defvar gnus-strict-mime t
662   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
663
664 (defvar gnus-show-mime-method 'metamail-buffer
665   "*Function to process a MIME message.
666 The function is called from the article buffer.")
667
668 (defvar gnus-decode-encoded-word-method (lambda ())
669   "*Function to decode a MIME encoded-words.
670 The function is called from the article buffer.")
671
672 (defvar gnus-show-threads t
673   "*If non-nil, display threads in summary mode.")
674
675 (defvar gnus-thread-hide-subtree nil
676   "*If non-nil, hide all threads initially.
677 If threads are hidden, you have to run the command
678 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
679 to expose hidden threads.")
680
681 (defvar gnus-thread-hide-killed t
682   "*If non-nil, hide killed threads automatically.")
683
684 (defvar gnus-thread-ignore-subject nil
685   "*If non-nil, ignore subjects and do all threading based on the Reference header.
686 If nil, which is the default, articles that have different subjects
687 from their parents will start separate threads.")
688
689 (defvar gnus-thread-operation-ignore-subject t
690   "*If non-nil, subjects will be ignored when doing thread commands.
691 This affects commands like `gnus-summary-kill-thread' and
692 `gnus-summary-lower-thread'.
693
694 If this variable is nil, articles in the same thread with different
695 subjects will not be included in the operation in question.  If this
696 variable is `fuzzy', only articles that have subjects that are fuzzily
697 equal will be included.")
698
699 (defvar gnus-thread-indent-level 4
700   "*Number that says how much each sub-thread should be indented.")
701
702 (defvar gnus-ignored-newsgroups
703   (purecopy (mapconcat 'identity
704                        '("^to\\."       ; not "real" groups
705                          "^[0-9. \t]+ " ; all digits in name
706                          "[][\"#'()]"   ; bogus characters
707                          )
708                        "\\|"))
709   "*A regexp to match uninteresting newsgroups in the active file.
710 Any lines in the active file matching this regular expression are
711 removed from the newsgroup list before anything else is done to it,
712 thus making them effectively non-existent.")
713
714 (defvar gnus-ignored-headers
715   "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:\\|^Received:\\|^Mail-from:"
716   "*All headers that match this regexp will be hidden.
717 This variable can also be a list of regexps of headers to be ignored.
718 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
719
720 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-"
721   "*All headers that do not match this regexp will be hidden.
722 This variable can also be a list of regexp of headers to remain visible.
723 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
724
725 (defvar gnus-sorted-header-list
726   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
727     "^Cc:" "^Date:" "^Organization:")
728   "*This variable is a list of regular expressions.
729 If it is non-nil, headers that match the regular expressions will
730 be placed first in the article buffer in the sequence specified by
731 this list.")
732
733 (defvar gnus-boring-article-headers
734   '(empty followup-to reply-to)
735   "*Headers that are only to be displayed if they have interesting data.
736 Possible values in this list are `empty', `newsgroups', `followup-to',
737 `reply-to', and `date'.")
738
739 (defvar gnus-show-all-headers nil
740   "*If non-nil, don't hide any headers.")
741
742 (defvar gnus-save-all-headers t
743   "*If non-nil, don't remove any headers before saving.")
744
745 (defvar gnus-saved-headers gnus-visible-headers
746   "*Headers to keep if `gnus-save-all-headers' is nil.
747 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
748 If that variable is nil, however, all headers that match this regexp
749 will be kept while the rest will be deleted before saving.")
750
751 (defvar gnus-inhibit-startup-message nil
752   "*If non-nil, the startup message will not be displayed.")
753
754 (defvar gnus-signature-separator "^-- *$"
755   "Regexp matching signature separator.")
756
757 (defvar gnus-signature-limit nil
758   "Provide a limit to what is considered a signature.
759 If it is a number, no signature may not be longer (in characters) than
760 that number.  If it is a function, the function will be called without
761 any parameters, and if it returns nil, there is no signature in the
762 buffer.  If it is a string, it will be used as a regexp.  If it
763 matches, the text in question is not a signature.")
764
765 (defvar gnus-auto-extend-newsgroup t
766   "*If non-nil, extend newsgroup forward and backward when requested.")
767
768 (defvar gnus-auto-select-first t
769   "*If nil, don't select the first unread article when entering a group.
770 If this variable is `best', select the highest-scored unread article
771 in the group.  If neither nil nor `best', select the first unread
772 article.
773
774 If you want to prevent automatic selection of the first unread article
775 in some newsgroups, set the variable to nil in
776 `gnus-select-group-hook'.")
777
778 (defvar gnus-auto-select-next t
779   "*If non-nil, offer to go to the next group from the end of the previous.
780 If the value is t and the next newsgroup is empty, Gnus will exit
781 summary mode and go back to group mode.  If the value is neither nil
782 nor t, Gnus will select the following unread newsgroup.  In
783 particular, if the value is the symbol `quietly', the next unread
784 newsgroup will be selected without any confirmation, and if it is
785 `almost-quietly', the next group will be selected without any
786 confirmation if you are located on the last article in the group.
787 Finally, if this variable is `slightly-quietly', the `Z n' command
788 will go to the next group without confirmation.")
789
790 (defvar gnus-auto-select-same nil
791   "*If non-nil, select the next article with the same subject.")
792
793 (defvar gnus-summary-check-current nil
794   "*If non-nil, consider the current article when moving.
795 The \"unread\" movement commands will stay on the same line if the
796 current article is unread.")
797
798 (defvar gnus-auto-center-summary t
799   "*If non-nil, always center the current summary buffer.
800 In particular, if `vertical' do only vertical recentering.  If non-nil
801 and non-`vertical', do both horizontal and vertical recentering.")
802
803 (defvar gnus-break-pages t
804   "*If non-nil, do page breaking on articles.
805 The page delimiter is specified by the `gnus-page-delimiter'
806 variable.")
807
808 (defvar gnus-page-delimiter "^\^L"
809   "*Regexp describing what to use as article page delimiters.
810 The default value is \"^\^L\", which is a form linefeed at the
811 beginning of a line.")
812
813 (defvar gnus-use-full-window t
814   "*If non-nil, use the entire Emacs screen.")
815
816 (defvar gnus-window-configuration nil
817   "Obsolete variable.  See `gnus-buffer-configuration'.")
818
819 (defvar gnus-window-min-width 2
820   "*Minimum width of Gnus buffers.")
821
822 (defvar gnus-window-min-height 1
823   "*Minimum height of Gnus buffers.")
824
825 (defvar gnus-buffer-configuration
826   '((group
827      (vertical 1.0
828                (group 1.0 point)
829                (if gnus-carpal '(group-carpal 4))))
830     (summary
831      (vertical 1.0
832                (summary 1.0 point)
833                (if gnus-carpal '(summary-carpal 4))))
834     (article
835      (cond 
836       (gnus-use-picons
837        '(frame 1.0
838                (vertical 1.0
839                          (summary 0.25 point)
840                          (if gnus-carpal '(summary-carpal 4))
841                          (article 1.0))
842                (vertical ((height . 5) (width . 15)
843                           (user-position . t)
844                           (left . -1) (top . 1))
845                          (picons 1.0))))
846       (gnus-use-trees
847        '(vertical 1.0
848                   (summary 0.25 point)
849                   (tree 0.25)
850                   (article 1.0)))
851       (t
852        '(vertical 1.0
853                  (summary 0.25 point)
854                  (if gnus-carpal '(summary-carpal 4))
855                  (if gnus-use-trees '(tree 0.25))
856                  (article 1.0)))))
857     (server
858      (vertical 1.0
859                (server 1.0 point)
860                (if gnus-carpal '(server-carpal 2))))
861     (browse
862      (vertical 1.0
863                (browse 1.0 point)
864                (if gnus-carpal '(browse-carpal 2))))
865     (message
866      (vertical 1.0
867                (message 1.0 point)))
868     (pick
869      (vertical 1.0
870                (article 1.0 point)))
871     (info
872      (vertical 1.0
873                (info 1.0 point)))
874     (summary-faq
875      (vertical 1.0
876                (summary 0.25)
877                (faq 1.0 point)))
878     (edit-group
879      (vertical 1.0
880                (group 0.5)
881                (edit-group 1.0 point)))
882     (edit-server
883      (vertical 1.0
884                (server 0.5)
885                (edit-server 1.0 point)))
886     (edit-score
887      (vertical 1.0
888                (summary 0.25)
889                (edit-score 1.0 point)))
890     (post
891      (vertical 1.0
892                (post 1.0 point)))
893     (reply
894      (vertical 1.0
895                (article-copy 0.5)
896                (message 1.0 point)))
897     (forward
898      (vertical 1.0
899                (message 1.0 point)))
900     (reply-yank
901      (vertical 1.0
902                (message 1.0 point)))
903     (mail-bounce
904      (vertical 1.0
905                (article 0.5)
906                (message 1.0 point)))
907     (draft
908      (vertical 1.0
909                (draft 1.0 point)))
910     (pipe
911      (vertical 1.0
912                (summary 0.25 point)
913                (if gnus-carpal '(summary-carpal 4))
914                ("*Shell Command Output*" 1.0)))
915     (bug
916      (vertical 1.0
917                ("*Gnus Help Bug*" 0.5)
918                ("*Gnus Bug*" 1.0 point)))
919     (compose-bounce
920      (vertical 1.0
921                (article 0.5)
922                (message 1.0 point))))
923   "Window configuration for all possible Gnus buffers.
924 This variable is a list of lists.  Each of these lists has a NAME and
925 a RULE.  The NAMEs are commonsense names like `group', which names a
926 rule used when displaying the group buffer; `summary', which names a
927 rule for what happens when you enter a group and do not display an
928 article buffer; and so on.  See the value of this variable for a
929 complete list of NAMEs.
930
931 Each RULE is a list of vectors.  The first element in this vector is
932 the name of the buffer to be displayed; the second element is the
933 percentage of the screen this buffer is to occupy (a number in the
934 0.0-0.99 range); the optional third element is `point', which should
935 be present to denote which buffer point is to go to after making this
936 buffer configuration.")
937
938 (defvar gnus-window-to-buffer
939   '((group . gnus-group-buffer)
940     (summary . gnus-summary-buffer)
941     (article . gnus-article-buffer)
942     (server . gnus-server-buffer)
943     (browse . "*Gnus Browse Server*")
944     (edit-group . gnus-group-edit-buffer)
945     (edit-server . gnus-server-edit-buffer)
946     (group-carpal . gnus-carpal-group-buffer)
947     (summary-carpal . gnus-carpal-summary-buffer)
948     (server-carpal . gnus-carpal-server-buffer)
949     (browse-carpal . gnus-carpal-browse-buffer)
950     (edit-score . gnus-score-edit-buffer)
951     (message . gnus-message-buffer)
952     (mail . gnus-message-buffer)
953     (post-news . gnus-message-buffer)
954     (faq . gnus-faq-buffer)
955     (picons . "*Picons*")
956     (tree . gnus-tree-buffer)
957     (info . gnus-info-buffer)
958     (article-copy . gnus-article-copy)
959     (draft . gnus-draft-buffer))
960   "Mapping from short symbols to buffer names or buffer variables.")
961
962 (defvar gnus-carpal nil
963   "*If non-nil, display clickable icons.")
964
965 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
966   "*Function called with a group name when new group is detected.
967 A few pre-made functions are supplied: `gnus-subscribe-randomly'
968 inserts new groups at the beginning of the list of groups;
969 `gnus-subscribe-alphabetically' inserts new groups in strict
970 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
971 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
972 for your decision; `gnus-subscribe-killed' kills all new groups.")
973
974 ;; Suggested by a bug report by Hallvard B Furuseth.
975 ;; <h.b.furuseth@usit.uio.no>.
976 (defvar gnus-subscribe-options-newsgroup-method
977   (function gnus-subscribe-alphabetically)
978   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
979 If, for instance, you want to subscribe to all newsgroups in the
980 \"no\" and \"alt\" hierarchies, you'd put the following in your
981 .newsrc file:
982
983 options -n no.all alt.all
984
985 Gnus will the subscribe all new newsgroups in these hierarchies with
986 the subscription method in this variable.")
987
988 (defvar gnus-subscribe-hierarchical-interactive nil
989   "*If non-nil, Gnus will offer to subscribe hierarchically.
990 When a new hierarchy appears, Gnus will ask the user:
991
992 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
993
994 If the user pressed `d', Gnus will descend the hierarchy, `y' will
995 subscribe to all newsgroups in the hierarchy and `s' will skip this
996 hierarchy in its entirety.")
997
998 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
999   "*Function used for sorting the group buffer.
1000 This function will be called with group info entries as the arguments
1001 for the groups to be sorted.  Pre-made functions include
1002 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1003 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1004 `gnus-group-sort-by-rank'.
1005
1006 This variable can also be a list of sorting functions.  In that case,
1007 the most significant sort function should be the last function in the
1008 list.")
1009
1010 ;; Mark variables suggested by Thomas Michanek
1011 ;; <Thomas.Michanek@telelogic.se>.
1012 (defvar gnus-unread-mark ? 
1013   "*Mark used for unread articles.")
1014 (defvar gnus-ticked-mark ?!
1015   "*Mark used for ticked articles.")
1016 (defvar gnus-dormant-mark ??
1017   "*Mark used for dormant articles.")
1018 (defvar gnus-del-mark ?r
1019   "*Mark used for del'd articles.")
1020 (defvar gnus-read-mark ?R
1021   "*Mark used for read articles.")
1022 (defvar gnus-expirable-mark ?E
1023   "*Mark used for expirable articles.")
1024 (defvar gnus-killed-mark ?K
1025   "*Mark used for killed articles.")
1026 (defvar gnus-souped-mark ?F
1027   "*Mark used for killed articles.")
1028 (defvar gnus-kill-file-mark ?X
1029   "*Mark used for articles killed by kill files.")
1030 (defvar gnus-low-score-mark ?Y
1031   "*Mark used for articles with a low score.")
1032 (defvar gnus-catchup-mark ?C
1033   "*Mark used for articles that are caught up.")
1034 (defvar gnus-replied-mark ?A
1035   "*Mark used for articles that have been replied to.")
1036 (defvar gnus-cached-mark ?*
1037   "*Mark used for articles that are in the cache.")
1038 (defvar gnus-saved-mark ?S
1039   "*Mark used for articles that have been saved to.")
1040 (defvar gnus-process-mark ?#
1041   "*Process mark.")
1042 (defvar gnus-ancient-mark ?O
1043   "*Mark used for ancient articles.")
1044 (defvar gnus-sparse-mark ?Q
1045   "*Mark used for sparsely reffed articles.")
1046 (defvar gnus-canceled-mark ?G
1047   "*Mark used for canceled articles.")
1048 (defvar gnus-score-over-mark ?+
1049   "*Score mark used for articles with high scores.")
1050 (defvar gnus-score-below-mark ?-
1051   "*Score mark used for articles with low scores.")
1052 (defvar gnus-empty-thread-mark ? 
1053   "*There is no thread under the article.")
1054 (defvar gnus-not-empty-thread-mark ?=
1055   "*There is a thread under the article.")
1056
1057 (defvar gnus-view-pseudo-asynchronously nil
1058   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1059
1060 (defvar gnus-view-pseudos nil
1061   "*If `automatic', pseudo-articles will be viewed automatically.
1062 If `not-confirm', pseudos will be viewed automatically, and the user
1063 will not be asked to confirm the command.")
1064
1065 (defvar gnus-view-pseudos-separately t
1066   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1067 If nil, all files that use the same viewing command will be given as a
1068 list of parameters to that command.")
1069
1070 (defvar gnus-insert-pseudo-articles t
1071   "*If non-nil, insert pseudo-articles when decoding articles.")
1072
1073 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1074   "*Format of group lines.
1075 It works along the same lines as a normal formatting string,
1076 with some simple extensions.
1077
1078 %M    Only marked articles (character, \"*\" or \" \")
1079 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1080 %L    Level of subscribedness (integer)
1081 %N    Number of unread articles (integer)
1082 %I    Number of dormant articles (integer)
1083 %i    Number of ticked and dormant (integer)
1084 %T    Number of ticked articles (integer)
1085 %R    Number of read articles (integer)
1086 %t    Total number of articles (integer)
1087 %y    Number of unread, unticked articles (integer)
1088 %G    Group name (string)
1089 %g    Qualified group name (string)
1090 %D    Group description (string)
1091 %s    Select method (string)
1092 %o    Moderated group (char, \"m\")
1093 %p    Process mark (char)
1094 %O    Moderated group (string, \"(m)\" or \"\")
1095 %P    Topic indentation (string)
1096 %l    Whether there are GroupLens predictions for this group (string)
1097 %n    Select from where (string)
1098 %z    A string that look like `<%s:%n>' if a foreign select method is used
1099 %u    User defined specifier.  The next character in the format string should
1100       be a letter.  Gnus will call the function gnus-user-format-function-X,
1101       where X is the letter following %u.  The function will be passed the
1102       current header as argument.  The function should return a string, which
1103       will be inserted into the buffer just like information from any other
1104       group specifier.
1105
1106 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1107 the mouse point move inside the area.  There can only be one such area.
1108
1109 Note that this format specification is not always respected.  For
1110 reasons of efficiency, when listing killed groups, this specification
1111 is ignored altogether.  If the spec is changed considerably, your
1112 output may end up looking strange when listing both alive and killed
1113 groups.
1114
1115 If you use %o or %O, reading the active file will be slower and quite
1116 a bit of extra memory will be used. %D will also worsen performance.
1117 Also note that if you change the format specification to include any
1118 of these specs, you must probably re-start Gnus to see them go into
1119 effect.")
1120
1121 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1122   "*The format specification of the lines in the summary buffer.
1123
1124 It works along the same lines as a normal formatting string,
1125 with some simple extensions.
1126
1127 %N   Article number, left padded with spaces (string)
1128 %S   Subject (string)
1129 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1130 %n   Name of the poster (string)
1131 %a   Extracted name of the poster (string)
1132 %A   Extracted address of the poster (string)
1133 %F   Contents of the From: header (string)
1134 %x   Contents of the Xref: header (string)
1135 %D   Date of the article (string)
1136 %d   Date of the article (string) in DD-MMM format
1137 %M   Message-id of the article (string)
1138 %r   References of the article (string)
1139 %c   Number of characters in the article (integer)
1140 %L   Number of lines in the article (integer)
1141 %I   Indentation based on thread level (a string of spaces)
1142 %T   A string with two possible values: 80 spaces if the article
1143      is on thread level two or larger and 0 spaces on level one
1144 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1145 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1146 %[   Opening bracket (character, \"[\" or \"<\")
1147 %]   Closing bracket (character, \"]\" or \">\")
1148 %>   Spaces of length thread-level (string)
1149 %<   Spaces of length (- 20 thread-level) (string)
1150 %i   Article score (number)
1151 %z   Article zcore (character)
1152 %t   Number of articles under the current thread (number).
1153 %e   Whether the thread is empty or not (character).
1154 %l   GroupLens score (string).
1155 %u   User defined specifier.  The next character in the format string should
1156      be a letter.  Gnus will call the function gnus-user-format-function-X,
1157      where X is the letter following %u.  The function will be passed the
1158      current header as argument.  The function should return a string, which
1159      will be inserted into the summary just like information from any other
1160      summary specifier.
1161
1162 Text between %( and %) will be highlighted with `gnus-mouse-face'
1163 when the mouse point is placed inside the area.  There can only be one
1164 such area.
1165
1166 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1167 with care.  For reasons of efficiency, Gnus will compute what column
1168 these characters will end up in, and \"hard-code\" that.  This means that
1169 it is illegal to have these specs after a variable-length spec.  Well,
1170 you might not be arrested, but your summary buffer will look strange,
1171 which is bad enough.
1172
1173 The smart choice is to have these specs as for to the left as
1174 possible.
1175
1176 This restriction may disappear in later versions of Gnus.")
1177
1178 (defvar gnus-summary-dummy-line-format
1179   "*  %(:                          :%) %S\n"
1180   "*The format specification for the dummy roots in the summary buffer.
1181 It works along the same lines as a normal formatting string,
1182 with some simple extensions.
1183
1184 %S  The subject")
1185
1186 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1187   "*The format specification for the summary mode line.
1188 It works along the same lines as a normal formatting string,
1189 with some simple extensions:
1190
1191 %G  Group name
1192 %p  Unprefixed group name
1193 %A  Current article number
1194 %V  Gnus version
1195 %U  Number of unread articles in the group
1196 %e  Number of unselected articles in the group
1197 %Z  A string with unread/unselected article counts
1198 %g  Shortish group name
1199 %S  Subject of the current article
1200 %u  User-defined spec
1201 %s  Current score file name
1202 %d  Number of dormant articles
1203 %r  Number of articles that have been marked as read in this session
1204 %E  Number of articles expunged by the score files")
1205
1206 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1207   "*The format specification for the article mode line.
1208 See `gnus-summary-mode-line-format' for a closer description.")
1209
1210 (defvar gnus-group-mode-line-format "Gnus: %%b {%M:%S}"
1211   "*The format specification for the group mode line.
1212 It works along the same lines as a normal formatting string,
1213 with some simple extensions:
1214
1215 %S   The native news server.
1216 %M   The native select method.")
1217
1218 (defvar gnus-valid-select-methods
1219   '(("nntp" post address prompt-address)
1220     ("nnspool" post address)
1221     ("nnvirtual" post-mail virtual prompt-address)
1222     ("nnmbox" mail respool address)
1223     ("nnml" mail respool address)
1224     ("nnmh" mail respool address)
1225     ("nndir" post-mail prompt-address address)
1226     ("nneething" none address prompt-address)
1227     ("nndoc" none address prompt-address)
1228     ("nnbabyl" mail address respool)
1229     ("nnkiboze" post address virtual)
1230     ("nnsoup" post-mail address)
1231     ("nndraft" post-mail)
1232     ("nnfolder" mail respool address))
1233   "An alist of valid select methods.
1234 The first element of each list lists should be a string with the name
1235 of the select method.  The other elements may be be the category of
1236 this method (ie. `post', `mail', `none' or whatever) or other
1237 properties that this method has (like being respoolable).
1238 If you implement a new select method, all you should have to change is
1239 this variable.  I think.")
1240
1241 (defvar gnus-updated-mode-lines '(group article summary tree)
1242   "*List of buffers that should update their mode lines.
1243 The list may contain the symbols `group', `article' and `summary'.  If
1244 the corresponding symbol is present, Gnus will keep that mode line
1245 updated with information that may be pertinent.
1246 If this variable is nil, screen refresh may be quicker.")
1247
1248 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1249 (defvar gnus-mode-non-string-length nil
1250   "*Max length of mode-line non-string contents.
1251 If this is nil, Gnus will take space as is needed, leaving the rest
1252 of the modeline intact.")
1253
1254 ;see gnus-cus.el
1255 ;(defvar gnus-mouse-face 'highlight
1256 ;  "*Face used for mouse highlighting in Gnus.
1257 ;No mouse highlights will be done if `gnus-visual' is nil.")
1258
1259 (defvar gnus-summary-mark-below nil
1260   "*Mark all articles with a score below this variable as read.
1261 This variable is local to each summary buffer and usually set by the
1262 score file.")
1263
1264 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1265   "*List of functions used for sorting articles in the summary buffer.
1266 This variable is only used when not using a threaded display.")
1267
1268 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1269   "*List of functions used for sorting threads in the summary buffer.
1270 By default, threads are sorted by article number.
1271
1272 Each function takes two threads and return non-nil if the first thread
1273 should be sorted before the other.  If you use more than one function,
1274 the primary sort function should be the last.  You should probably
1275 always include `gnus-thread-sort-by-number' in the list of sorting
1276 functions -- preferably first.
1277
1278 Ready-mady functions include `gnus-thread-sort-by-number',
1279 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1280 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1281 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1282
1283 (defvar gnus-thread-score-function '+
1284   "*Function used for calculating the total score of a thread.
1285
1286 The function is called with the scores of the article and each
1287 subthread and should then return the score of the thread.
1288
1289 Some functions you can use are `+', `max', or `min'.")
1290
1291 (defvar gnus-summary-expunge-below nil
1292   "All articles that have a score less than this variable will be expunged.")
1293
1294 (defvar gnus-thread-expunge-below nil
1295   "All threads that have a total score less than this variable will be expunged.
1296 See `gnus-thread-score-function' for en explanation of what a
1297 \"thread score\" is.")
1298
1299 (defvar gnus-auto-subscribed-groups
1300   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1301   "*All new groups that match this regexp will be subscribed automatically.
1302 Note that this variable only deals with new groups.  It has no effect
1303 whatsoever on old groups.")
1304
1305 (defvar gnus-options-subscribe nil
1306   "*All new groups matching this regexp will be subscribed unconditionally.
1307 Note that this variable deals only with new newsgroups.  This variable
1308 does not affect old newsgroups.")
1309
1310 (defvar gnus-options-not-subscribe nil
1311   "*All new groups matching this regexp will be ignored.
1312 Note that this variable deals only with new newsgroups.  This variable
1313 does not affect old (already subscribed) newsgroups.")
1314
1315 (defvar gnus-auto-expirable-newsgroups nil
1316   "*Groups in which to automatically mark read articles as expirable.
1317 If non-nil, this should be a regexp that should match all groups in
1318 which to perform auto-expiry.  This only makes sense for mail groups.")
1319
1320 (defvar gnus-total-expirable-newsgroups nil
1321   "*Groups in which to perform expiry of all read articles.
1322 Use with extreme caution.  All groups that match this regexp will be
1323 expiring - which means that all read articles will be deleted after
1324 (say) one week.  (This only goes for mail groups and the like, of
1325 course.)")
1326
1327 (defvar gnus-group-uncollapsed-levels 1
1328   "Number of group name elements to leave alone when making a short group name.")
1329
1330 (defvar gnus-hidden-properties '(invisible t intangible t)
1331   "Property list to use for hiding text.")
1332
1333 (defvar gnus-modtime-botch nil
1334   "*Non-nil means .newsrc should be deleted prior to save.  
1335 Its use is due to the bogus appearance that .newsrc was modified on
1336 disc.")
1337
1338 ;; Hooks.
1339
1340 (defvar gnus-group-mode-hook nil
1341   "*A hook for Gnus group mode.")
1342
1343 (defvar gnus-summary-mode-hook nil
1344   "*A hook for Gnus summary mode.
1345 This hook is run before any variables are set in the summary buffer.")
1346
1347 (defvar gnus-article-mode-hook nil
1348   "*A hook for Gnus article mode.")
1349
1350 (defvar gnus-summary-prepare-exit-hook nil
1351   "*A hook called when preparing to exit from the summary buffer.
1352 It calls `gnus-summary-expire-articles' by default.")
1353 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1354
1355 (defvar gnus-summary-exit-hook nil
1356   "*A hook called on exit from the summary buffer.")
1357
1358 (defvar gnus-group-catchup-group-hook nil
1359   "*A hook run when catching up a group from the group buffer.")
1360
1361 (defvar gnus-open-server-hook nil
1362   "*A hook called just before opening connection to the news server.")
1363
1364 (defvar gnus-load-hook nil
1365   "*A hook run while Gnus is loaded.")
1366
1367 (defvar gnus-startup-hook nil
1368   "*A hook called at startup.
1369 This hook is called after Gnus is connected to the NNTP server.")
1370
1371 (defvar gnus-get-new-news-hook nil
1372   "*A hook run just before Gnus checks for new news.")
1373
1374 (defvar gnus-after-getting-new-news-hook nil
1375   "*A hook run after Gnus checks for new news.")
1376
1377 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1378   "*A function that is called to generate the group buffer.
1379 The function is called with three arguments: The first is a number;
1380 all group with a level less or equal to that number should be listed,
1381 if the second is non-nil, empty groups should also be displayed.  If
1382 the third is non-nil, it is a number.  No groups with a level lower
1383 than this number should be displayed.
1384
1385 The only current function implemented is `gnus-group-prepare-flat'.")
1386
1387 (defvar gnus-group-prepare-hook nil
1388   "*A hook called after the group buffer has been generated.
1389 If you want to modify the group buffer, you can use this hook.")
1390
1391 (defvar gnus-summary-prepare-hook nil
1392   "*A hook called after the summary buffer has been generated.
1393 If you want to modify the summary buffer, you can use this hook.")
1394
1395 (defvar gnus-summary-generate-hook nil
1396   "*A hook run just before generating the summary buffer.
1397 This hook is commonly used to customize threading variables and the
1398 like.")
1399
1400 (defvar gnus-article-prepare-hook nil
1401   "*A hook called after an article has been prepared in the article buffer.
1402 If you want to run a special decoding program like nkf, use this hook.")
1403
1404 ;(defvar gnus-article-display-hook nil
1405 ;  "*A hook called after the article is displayed in the article buffer.
1406 ;The hook is designed to change the contents of the article
1407 ;buffer.  Typical functions that this hook may contain are
1408 ;`gnus-article-hide-headers' (hide selected headers),
1409 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1410 ;`gnus-article-hide-signature' (hide signature) and
1411 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1412 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1413 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1414 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1415
1416 (defvar gnus-article-x-face-command
1417   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1418   "String or function to be executed to display an X-Face header.
1419 If it is a string, the command will be executed in a sub-shell
1420 asynchronously.  The compressed face will be piped to this command.")
1421
1422 (defvar gnus-article-x-face-too-ugly nil
1423   "Regexp matching posters whose face shouldn't be shown automatically.")
1424
1425 (defvar gnus-select-group-hook nil
1426   "*A hook called when a newsgroup is selected.
1427
1428 If you'd like to simplify subjects like the
1429 `gnus-summary-next-same-subject' command does, you can use the
1430 following hook:
1431
1432  (setq gnus-select-group-hook
1433       (list
1434         (lambda ()
1435           (mapcar (lambda (header)
1436                      (mail-header-set-subject
1437                       header
1438                       (gnus-simplify-subject
1439                        (mail-header-subject header) 're-only)))
1440                   gnus-newsgroup-headers))))")
1441
1442 (defvar gnus-select-article-hook nil
1443   "*A hook called when an article is selected.")
1444
1445 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1446   "*A hook called to apply kill files to a group.
1447 This hook is intended to apply a kill file to the selected newsgroup.
1448 The function `gnus-apply-kill-file' is called by default.
1449
1450 Since a general kill file is too heavy to use only for a few
1451 newsgroups, I recommend you to use a lighter hook function.  For
1452 example, if you'd like to apply a kill file to articles which contains
1453 a string `rmgroup' in subject in newsgroup `control', you can use the
1454 following hook:
1455
1456  (setq gnus-apply-kill-hook
1457       (list
1458         (lambda ()
1459           (cond ((string-match \"control\" gnus-newsgroup-name)
1460                  (gnus-kill \"Subject\" \"rmgroup\")
1461                  (gnus-expunge \"X\"))))))")
1462
1463 (defvar gnus-visual-mark-article-hook
1464   (list 'gnus-highlight-selected-summary)
1465   "*Hook run after selecting an article in the summary buffer.
1466 It is meant to be used for highlighting the article in some way.  It
1467 is not run if `gnus-visual' is nil.")
1468
1469 (defvar gnus-parse-headers-hook nil
1470   "*A hook called before parsing the headers.")
1471 (add-hook 'gnus-parse-headers-hook 'gnus-headers-decode-quoted-printable)
1472
1473 (defvar gnus-exit-group-hook nil
1474   "*A hook called when exiting (not quitting) summary mode.")
1475
1476 (defvar gnus-suspend-gnus-hook nil
1477   "*A hook called when suspending (not exiting) Gnus.")
1478
1479 (defvar gnus-exit-gnus-hook nil
1480   "*A hook called when exiting Gnus.")
1481
1482 (defvar gnus-after-exiting-gnus-hook nil
1483   "*A hook called after exiting Gnus.")
1484
1485 (defvar gnus-save-newsrc-hook nil
1486   "*A hook called before saving any of the newsrc files.")
1487
1488 (defvar gnus-save-quick-newsrc-hook nil
1489   "*A hook called just before saving the quick newsrc file.
1490 Can be used to turn version control on or off.")
1491
1492 (defvar gnus-save-standard-newsrc-hook nil
1493   "*A hook called just before saving the standard newsrc file.
1494 Can be used to turn version control on or off.")
1495
1496 (defvar gnus-summary-update-hook
1497   (list 'gnus-summary-highlight-line)
1498   "*A hook called when a summary line is changed.
1499 The hook will not be called if `gnus-visual' is nil.
1500
1501 The default function `gnus-summary-highlight-line' will
1502 highlight the line according to the `gnus-summary-highlight'
1503 variable.")
1504
1505 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1506   "*A hook called when a group line is changed.
1507 The hook will not be called if `gnus-visual' is nil.
1508
1509 The default function `gnus-group-highlight-line' will
1510 highlight the line according to the `gnus-group-highlight'
1511 variable.")
1512
1513 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1514   "*A hook called when an article is selected for the first time.
1515 The hook is intended to mark an article as read (or unread)
1516 automatically when it is selected.")
1517
1518 (defvar gnus-group-change-level-function nil
1519   "Function run when a group level is changed.
1520 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1521
1522 ;; Remove any hilit infestation.
1523 (add-hook 'gnus-startup-hook
1524           (lambda ()
1525             (remove-hook 'gnus-summary-prepare-hook
1526                          'hilit-rehighlight-buffer-quietly)
1527             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1528             (setq gnus-mark-article-hook
1529                   '(gnus-summary-mark-read-and-unread-as-read))
1530             (remove-hook 'gnus-article-prepare-hook
1531                          'hilit-rehighlight-buffer-quietly)))
1532
1533 \f
1534 ;; Internal variables
1535
1536 (defvar gnus-method-history nil)
1537 ;; Variable holding the user answers to all method prompts.
1538
1539 (defvar gnus-server-alist nil
1540   "List of available servers.")
1541
1542 (defvar gnus-group-indentation-function nil)
1543
1544 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1545
1546 (defvar gnus-goto-missing-group-function nil)
1547
1548 (defvar gnus-override-subscribe-method nil)
1549
1550 (defvar gnus-group-goto-next-group-function nil
1551   "Function to override finding the next group after listing groups.")
1552
1553 (defconst gnus-article-mark-lists
1554   '((marked . tick) (replied . reply)
1555     (expirable . expire) (killed . killed)
1556     (bookmarks . bookmark) (dormant . dormant)
1557     (scored . score) (saved . save)
1558     (cached . cache)
1559     ))
1560
1561 ;; Avoid highlighting in kill files.
1562 (defvar gnus-summary-inhibit-highlight nil)
1563 (defvar gnus-newsgroup-selected-overlay nil)
1564
1565 (defvar gnus-inhibit-hiding nil)
1566 (defvar gnus-group-indentation "")
1567 (defvar gnus-inhibit-limiting nil)
1568 (defvar gnus-created-frames nil)
1569
1570 (defvar gnus-article-mode-map nil)
1571 (defvar gnus-dribble-buffer nil)
1572 (defvar gnus-headers-retrieved-by nil)
1573 (defvar gnus-article-reply nil)
1574 (defvar gnus-override-method nil)
1575 (defvar gnus-article-check-size nil)
1576
1577 (defvar gnus-current-score-file nil)
1578 (defvar gnus-newsgroup-adaptive-score-file nil)
1579 (defvar gnus-scores-exclude-files nil)
1580
1581 (defvar gnus-opened-servers nil)
1582
1583 (defvar gnus-current-move-group nil)
1584
1585 (defvar gnus-newsgroup-dependencies nil)
1586 (defvar gnus-newsgroup-async nil)
1587 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1588
1589 (defvar gnus-newsgroup-adaptive nil)
1590
1591 (defvar gnus-summary-display-table nil)
1592 (defvar gnus-summary-display-article-function nil)
1593
1594 (defvar gnus-summary-highlight-line-function nil
1595   "Function called after highlighting a summary line.")
1596
1597 (defvar gnus-group-line-format-alist
1598   `((?M gnus-tmp-marked-mark ?c)
1599     (?S gnus-tmp-subscribed ?c)
1600     (?L gnus-tmp-level ?d)
1601     (?N (cond ((eq number t) "*" )
1602               ((numberp number) 
1603                (int-to-string
1604                 (+ number
1605                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1606                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1607               (t number)) ?s)
1608     (?R gnus-tmp-number-of-read ?s)
1609     (?t gnus-tmp-number-total ?d)
1610     (?y gnus-tmp-number-of-unread ?s)
1611     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1612     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1613     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1614            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1615     (?g gnus-tmp-group ?s)
1616     (?G gnus-tmp-qualified-group ?s)
1617     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1618     (?D gnus-tmp-newsgroup-description ?s)
1619     (?o gnus-tmp-moderated ?c)
1620     (?O gnus-tmp-moderated-string ?s)
1621     (?p gnus-tmp-process-marked ?c)
1622     (?s gnus-tmp-news-server ?s)
1623     (?n gnus-tmp-news-method ?s)
1624     (?P gnus-group-indentation ?s)
1625     (?l gnus-tmp-grouplens ?s)
1626     (?z gnus-tmp-news-method-string ?s)
1627     (?u gnus-tmp-user-defined ?s)))
1628
1629 (defvar gnus-summary-line-format-alist
1630   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1631     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1632     (?s gnus-tmp-subject-or-nil ?s)
1633     (?n gnus-tmp-name ?s)
1634     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1635         ?s)
1636     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1637             gnus-tmp-from) ?s)
1638     (?F gnus-tmp-from ?s)
1639     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1640     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1641     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1642     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1643     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1644     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1645     (?L gnus-tmp-lines ?d)
1646     (?I gnus-tmp-indentation ?s)
1647     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1648     (?R gnus-tmp-replied ?c)
1649     (?\[ gnus-tmp-opening-bracket ?c)
1650     (?\] gnus-tmp-closing-bracket ?c)
1651     (?\> (make-string gnus-tmp-level ? ) ?s)
1652     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1653     (?i gnus-tmp-score ?d)
1654     (?z gnus-tmp-score-char ?c)
1655     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1656     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1657     (?U gnus-tmp-unread ?c)
1658     (?t (gnus-summary-number-of-articles-in-thread
1659          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1660         ?d)
1661     (?e (gnus-summary-number-of-articles-in-thread
1662          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1663         ?c)
1664     (?u gnus-tmp-user-defined ?s))
1665   "An alist of format specifications that can appear in summary lines,
1666 and what variables they correspond with, along with the type of the
1667 variable (string, integer, character, etc).")
1668
1669 (defvar gnus-summary-dummy-line-format-alist
1670   `((?S gnus-tmp-subject ?s)
1671     (?N gnus-tmp-number ?d)
1672     (?u gnus-tmp-user-defined ?s)))
1673
1674 (defvar gnus-summary-mode-line-format-alist
1675   `((?G gnus-tmp-group-name ?s)
1676     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1677     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1678     (?A gnus-tmp-article-number ?d)
1679     (?Z gnus-tmp-unread-and-unselected ?s)
1680     (?V gnus-version ?s)
1681     (?U gnus-tmp-unread ?d)
1682     (?S gnus-tmp-subject ?s)
1683     (?e gnus-tmp-unselected ?d)
1684     (?u gnus-tmp-user-defined ?s)
1685     (?d (length gnus-newsgroup-dormant) ?d)
1686     (?t (length gnus-newsgroup-marked) ?d)
1687     (?r (length gnus-newsgroup-reads) ?d)
1688     (?E gnus-newsgroup-expunged-tally ?d)
1689     (?s (gnus-current-score-file-nondirectory) ?s)))
1690
1691 (defvar gnus-article-mode-line-format-alist
1692   gnus-summary-mode-line-format-alist)
1693
1694 (defvar gnus-group-mode-line-format-alist
1695   `((?S gnus-tmp-news-server ?s)
1696     (?M gnus-tmp-news-method ?s)
1697     (?u gnus-tmp-user-defined ?s)))
1698
1699 (defvar gnus-have-read-active-file nil)
1700
1701 (defconst gnus-maintainer
1702   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1703   "The mail address of the Gnus maintainers.")
1704
1705 (defconst gnus-version "September Gnus v0.77"
1706   "Version number for this version of Gnus.")
1707
1708 (defvar gnus-info-nodes
1709   '((gnus-group-mode "(gnus)The Group Buffer")
1710     (gnus-summary-mode "(gnus)The Summary Buffer")
1711     (gnus-article-mode "(gnus)The Article Buffer"))
1712   "Assoc list of major modes and related Info nodes.")
1713
1714 (defvar gnus-group-buffer "*Group*")
1715 (defvar gnus-summary-buffer "*Summary*")
1716 (defvar gnus-article-buffer "*Article*")
1717 (defvar gnus-server-buffer "*Server*")
1718
1719 (defvar gnus-work-buffer " *gnus work*")
1720
1721 (defvar gnus-original-article-buffer " *Original Article*")
1722 (defvar gnus-original-article nil)
1723
1724 (defvar gnus-buffer-list nil
1725   "Gnus buffers that should be killed on exit.")
1726
1727 (defvar gnus-slave nil
1728   "Whether this Gnus is a slave or not.")
1729
1730 (defvar gnus-variable-list
1731   '(gnus-newsrc-options gnus-newsrc-options-n
1732     gnus-newsrc-last-checked-date
1733     gnus-newsrc-alist gnus-server-alist
1734     gnus-killed-list gnus-zombie-list
1735     gnus-topic-topology gnus-topic-alist
1736     gnus-format-specs)
1737   "Gnus variables saved in the quick startup file.")
1738
1739 (defvar gnus-newsrc-options nil
1740   "Options line in the .newsrc file.")
1741
1742 (defvar gnus-newsrc-options-n nil
1743   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1744
1745 (defvar gnus-newsrc-last-checked-date nil
1746   "Date Gnus last asked server for new newsgroups.")
1747
1748 (defvar gnus-topic-topology nil
1749   "The complete topic hierarchy.")
1750
1751 (defvar gnus-topic-alist nil
1752   "The complete topic-group alist.")
1753
1754 (defvar gnus-newsrc-alist nil
1755   "Assoc list of read articles.
1756 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1757
1758 (defvar gnus-newsrc-hashtb nil
1759   "Hashtable of gnus-newsrc-alist.")
1760
1761 (defvar gnus-killed-list nil
1762   "List of killed newsgroups.")
1763
1764 (defvar gnus-killed-hashtb nil
1765   "Hash table equivalent of gnus-killed-list.")
1766
1767 (defvar gnus-zombie-list nil
1768   "List of almost dead newsgroups.")
1769
1770 (defvar gnus-description-hashtb nil
1771   "Descriptions of newsgroups.")
1772
1773 (defvar gnus-list-of-killed-groups nil
1774   "List of newsgroups that have recently been killed by the user.")
1775
1776 (defvar gnus-active-hashtb nil
1777   "Hashtable of active articles.")
1778
1779 (defvar gnus-moderated-list nil
1780   "List of moderated newsgroups.")
1781
1782 (defvar gnus-group-marked nil)
1783
1784 (defvar gnus-current-startup-file nil
1785   "Startup file for the current host.")
1786
1787 (defvar gnus-last-search-regexp nil
1788   "Default regexp for article search command.")
1789
1790 (defvar gnus-last-shell-command nil
1791   "Default shell command on article.")
1792
1793 (defvar gnus-current-select-method nil
1794   "The current method for selecting a newsgroup.")
1795
1796 (defvar gnus-group-list-mode nil)
1797
1798 (defvar gnus-article-internal-prepare-hook nil)
1799
1800 (defvar gnus-newsgroup-name nil)
1801 (defvar gnus-newsgroup-begin nil)
1802 (defvar gnus-newsgroup-end nil)
1803 (defvar gnus-newsgroup-last-rmail nil)
1804 (defvar gnus-newsgroup-last-mail nil)
1805 (defvar gnus-newsgroup-last-folder nil)
1806 (defvar gnus-newsgroup-last-file nil)
1807 (defvar gnus-newsgroup-auto-expire nil)
1808 (defvar gnus-newsgroup-active nil)
1809
1810 (defvar gnus-newsgroup-data nil)
1811 (defvar gnus-newsgroup-data-reverse nil)
1812 (defvar gnus-newsgroup-limit nil)
1813 (defvar gnus-newsgroup-limits nil)
1814
1815 (defvar gnus-newsgroup-unreads nil
1816   "List of unread articles in the current newsgroup.")
1817
1818 (defvar gnus-newsgroup-unselected nil
1819   "List of unselected unread articles in the current newsgroup.")
1820
1821 (defvar gnus-newsgroup-reads nil
1822   "Alist of read articles and article marks in the current newsgroup.")
1823
1824 (defvar gnus-newsgroup-expunged-tally nil)
1825
1826 (defvar gnus-newsgroup-marked nil
1827   "List of ticked articles in the current newsgroup (a subset of unread art).")
1828
1829 (defvar gnus-newsgroup-killed nil
1830   "List of ranges of articles that have been through the scoring process.")
1831
1832 (defvar gnus-newsgroup-cached nil
1833   "List of articles that come from the article cache.")
1834
1835 (defvar gnus-newsgroup-saved nil
1836   "List of articles that have been saved.")
1837
1838 (defvar gnus-newsgroup-kill-headers nil)
1839
1840 (defvar gnus-newsgroup-replied nil
1841   "List of articles that have been replied to in the current newsgroup.")
1842
1843 (defvar gnus-newsgroup-expirable nil
1844   "List of articles in the current newsgroup that can be expired.")
1845
1846 (defvar gnus-newsgroup-processable nil
1847   "List of articles in the current newsgroup that can be processed.")
1848
1849 (defvar gnus-newsgroup-bookmarks nil
1850   "List of articles in the current newsgroup that have bookmarks.")
1851
1852 (defvar gnus-newsgroup-dormant nil
1853   "List of dormant articles in the current newsgroup.")
1854
1855 (defvar gnus-newsgroup-scored nil
1856   "List of scored articles in the current newsgroup.")
1857
1858 (defvar gnus-newsgroup-headers nil
1859   "List of article headers in the current newsgroup.")
1860
1861 (defvar gnus-newsgroup-threads nil)
1862
1863 (defvar gnus-newsgroup-prepared nil
1864   "Whether the current group has been prepared properly.")
1865
1866 (defvar gnus-newsgroup-ancient nil
1867   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1868
1869 (defvar gnus-newsgroup-sparse nil)
1870
1871 (defvar gnus-current-article nil)
1872 (defvar gnus-article-current nil)
1873 (defvar gnus-current-headers nil)
1874 (defvar gnus-have-all-headers nil)
1875 (defvar gnus-last-article nil)
1876 (defvar gnus-newsgroup-history nil)
1877 (defvar gnus-current-kill-article nil)
1878
1879 ;; Save window configuration.
1880 (defvar gnus-prev-winconf nil)
1881
1882 (defvar gnus-summary-mark-positions nil)
1883 (defvar gnus-group-mark-positions nil)
1884
1885 (defvar gnus-reffed-article-number nil)
1886
1887 ;;; Let the byte-compiler know that we know about this variable.
1888 (defvar rmail-default-rmail-file)
1889
1890 (defvar gnus-cache-removable-articles nil)
1891
1892 (defvar gnus-dead-summary nil)
1893
1894 (defconst gnus-summary-local-variables
1895   '(gnus-newsgroup-name
1896     gnus-newsgroup-begin gnus-newsgroup-end
1897     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1898     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1899     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1900     gnus-newsgroup-unselected gnus-newsgroup-marked
1901     gnus-newsgroup-reads gnus-newsgroup-saved
1902     gnus-newsgroup-replied gnus-newsgroup-expirable
1903     gnus-newsgroup-processable gnus-newsgroup-killed
1904     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1905     gnus-newsgroup-headers gnus-newsgroup-threads
1906     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1907     gnus-current-article gnus-current-headers gnus-have-all-headers
1908     gnus-last-article gnus-article-internal-prepare-hook
1909     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1910     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1911     gnus-newsgroup-async gnus-thread-expunge-below
1912     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1913     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1914     gnus-newsgroup-history gnus-newsgroup-ancient
1915     gnus-newsgroup-sparse
1916     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1917     gnus-newsgroup-adaptive-score-file
1918     (gnus-newsgroup-expunged-tally . 0)
1919     gnus-cache-removable-articles gnus-newsgroup-cached
1920     gnus-newsgroup-data gnus-newsgroup-data-reverse
1921     gnus-newsgroup-limit gnus-newsgroup-limits)
1922   "Variables that are buffer-local to the summary buffers.")
1923
1924 (defconst gnus-bug-message
1925   "Sending a bug report to the Gnus Towers.
1926 ========================================
1927
1928 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1929 be sent to the Gnus Bug Exterminators.
1930
1931 At the bottom of the buffer you'll see lots of variable settings.
1932 Please do not delete those.  They will tell the Bug People what your
1933 environment is, so that it will be easier to locate the bugs.
1934
1935 If you have found a bug that makes Emacs go \"beep\", set
1936 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1937 and include the backtrace in your bug report.
1938
1939 Please describe the bug in annoying, painstaking detail.
1940
1941 Thank you for your help in stamping out bugs.
1942 ")
1943
1944 ;;; End of variables.
1945
1946 ;; Define some autoload functions Gnus might use.
1947 (eval-and-compile
1948
1949   ;; This little mapcar goes through the list below and marks the
1950   ;; symbols in question as autoloaded functions.
1951   (mapcar
1952    (lambda (package)
1953      (let ((interactive (nth 1 (memq ':interactive package))))
1954        (mapcar
1955         (lambda (function)
1956           (let (keymap)
1957             (when (consp function)
1958               (setq keymap (car (memq 'keymap function)))
1959               (setq function (car function)))
1960             (autoload function (car package) nil interactive keymap)))
1961         (if (eq (nth 1 package) ':interactive)
1962             (cdddr package)
1963           (cdr package)))))
1964    '(("metamail" metamail-buffer)
1965      ("info" Info-goto-node)
1966      ("hexl" hexl-hex-string-to-integer)
1967      ("pp" pp pp-to-string pp-eval-expression)
1968      ("mail-extr" mail-extract-address-components)
1969      ("nnmail" nnmail-split-fancy nnmail-article-group)
1970      ("nnvirtual" nnvirtual-catchup-group)
1971      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1972       timezone-make-sortable-date timezone-make-time-string)
1973      ("sendmail" mail-position-on-field mail-setup)
1974      ("rmailout" rmail-output)
1975      ("rnewspost" news-mail-other-window news-reply-yank-original
1976       news-caesar-buffer-body)
1977      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1978       rmail-show-message)
1979      ("gnus-soup" :interactive t
1980       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1981       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1982      ("nnsoup" nnsoup-pack-replies)
1983      ("gnus-scomo" :interactive t gnus-score-mode)
1984      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
1985       gnus-Folder-save-name gnus-folder-save-name)
1986      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
1987      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
1988       gnus-server-make-menu-bar gnus-article-make-menu-bar
1989       gnus-browse-make-menu-bar gnus-highlight-selected-summary
1990       gnus-summary-highlight-line gnus-carpal-setup-buffer
1991       gnus-group-highlight-line
1992       gnus-article-add-button gnus-insert-next-page-button
1993       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
1994      ("gnus-vis" :interactive t
1995       gnus-article-push-button gnus-article-press-button
1996       gnus-article-highlight gnus-article-highlight-some
1997       gnus-article-highlight-headers gnus-article-highlight-signature
1998       gnus-article-add-buttons gnus-article-add-buttons-to-head
1999       gnus-article-next-button gnus-article-prev-button)
2000      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2001       gnus-demon-add-disconnection gnus-demon-add-handler
2002       gnus-demon-remove-handler)
2003      ("gnus-demon" :interactive t
2004       gnus-demon-init gnus-demon-cancel)
2005      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2006       gnus-tree-open gnus-tree-close)
2007      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2008       gnus-nocem-unwanted-article-p)
2009      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2010      ("gnus-srvr" gnus-browse-foreign-server)
2011      ("gnus-cite" :interactive t
2012       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2013       gnus-article-hide-citation gnus-article-fill-cited-article)
2014      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2015       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2016       gnus-execute gnus-expunge)
2017      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2018       gnus-cache-possibly-remove-articles gnus-cache-request-article
2019       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2020       gnus-cache-enter-remove-article gnus-cached-article-p
2021       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2022      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2023       gnus-cache-remove-article)
2024      ("gnus-score" :interactive t
2025       gnus-summary-increase-score gnus-summary-lower-score
2026       gnus-score-flush-cache gnus-score-close
2027       gnus-score-raise-same-subject-and-select
2028       gnus-score-raise-same-subject gnus-score-default
2029       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2030       gnus-score-lower-same-subject gnus-score-lower-thread
2031       gnus-possibly-score-headers)
2032      ("gnus-score"
2033       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2034       gnus-current-score-file-nondirectory gnus-score-adaptive
2035       gnus-score-find-trace gnus-score-file-name)
2036      ("gnus-edit" :interactive t gnus-score-customize)
2037      ("gnus-topic" :interactive t gnus-topic-mode)
2038      ("gnus-topic" gnus-topic-remove-group)
2039      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2040      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2041      ("gnus-uu" :interactive t
2042       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2043       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2044       gnus-uu-mark-by-regexp gnus-uu-mark-all
2045       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2046       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2047       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2048       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2049       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2050       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2051       gnus-uu-decode-binhex-view)
2052      ("gnus-msg" (gnus-summary-send-map keymap)
2053       gnus-mail-yank-original gnus-mail-send-and-exit
2054       gnus-sendmail-setup-mail gnus-article-mail
2055       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2056      ("gnus-msg" :interactive t
2057       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2058       gnus-summary-followup gnus-summary-followup-with-original
2059       gnus-summary-followup-and-reply
2060       gnus-summary-followup-and-reply-with-original
2061       gnus-summary-cancel-article gnus-summary-supersede-article
2062       gnus-post-news gnus-inews-news gnus-cancel-news
2063       gnus-summary-reply gnus-summary-reply-with-original
2064       gnus-summary-mail-forward gnus-summary-mail-other-window
2065       gnus-bug)
2066      ("gnus-picon" :interactive t gnus-article-display-picons
2067       gnus-group-display-picons gnus-picons-article-display-x-face)
2068      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2069       gnus-grouplens-mode)
2070      ("gnus-vm" gnus-vm-mail-setup)
2071      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2072       gnus-summary-save-article-vm gnus-yank-article))))
2073
2074 \f
2075
2076 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2077 ;; If you want the cursor to go somewhere else, set these two
2078 ;; functions in some startup hook to whatever you want.
2079 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2080 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2081
2082 ;;; Various macros and substs.
2083
2084 (defun gnus-header-from (header)
2085   (mail-header-from header))
2086
2087 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2088   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2089   (let ((tempvar (make-symbol "GnusStartBufferWindow")))
2090     `(let ((,tempvar (selected-window)))
2091        (unwind-protect
2092            (progn
2093              (pop-to-buffer ,buffer)
2094              ,@forms)
2095          (select-window ,tempvar)))))
2096
2097 (defmacro gnus-gethash (string hashtable)
2098   "Get hash value of STRING in HASHTABLE."
2099   `(symbol-value (intern-soft ,string ,hashtable)))
2100
2101 (defmacro gnus-sethash (string value hashtable)
2102   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2103   `(set (intern ,string ,hashtable) ,value))
2104
2105 (defmacro gnus-intern-safe (string hashtable)
2106   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2107   `(let ((symbol (intern ,string ,hashtable)))
2108      (or (boundp symbol)
2109          (set symbol nil))
2110      symbol))
2111
2112 (defmacro gnus-group-unread (group)
2113   "Get the currently computed number of unread articles in GROUP."
2114   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2115
2116 (defmacro gnus-group-entry (group)
2117   "Get the newsrc entry for GROUP."
2118   `(gnus-gethash ,group gnus-newsrc-hashtb))
2119
2120 (defmacro gnus-active (group)
2121   "Get active info on GROUP."
2122   `(gnus-gethash ,group gnus-active-hashtb))
2123
2124 (defmacro gnus-set-active (group active)
2125   "Set GROUP's active info."
2126   `(gnus-sethash ,group ,active gnus-active-hashtb))
2127
2128 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2129 ;;   function `substring' might cut on a middle of multi-octet
2130 ;;   character.
2131 (defun gnus-truncate-string (str width)
2132   (substring str 0 width))
2133
2134 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2135 ;; to limit the length of a string.  This function is necessary since
2136 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2137 (defsubst gnus-limit-string (str width)
2138   (if (> (length str) width)
2139       (substring str 0 width)
2140     str))
2141
2142 (defsubst gnus-simplify-subject-re (subject)
2143   "Remove \"Re:\" from subject lines."
2144   (if (string-match "^[Rr][Ee]: *" subject)
2145       (substring subject (match-end 0))
2146     subject))
2147
2148 (defsubst gnus-functionp (form)
2149   "Return non-nil if FORM is funcallable."
2150   (or (and (symbolp form) (fboundp form))
2151       (and (listp form) (eq (car form) 'lambda))))
2152
2153 (defsubst gnus-goto-char (point)
2154   (and point (goto-char point)))
2155
2156 (defmacro gnus-buffer-exists-p (buffer)
2157   `(let ((buffer ,buffer))
2158      (and buffer
2159           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2160                    buffer))))
2161
2162 (defmacro gnus-kill-buffer (buffer)
2163   `(let ((buf ,buffer))
2164      (if (gnus-buffer-exists-p buf)
2165          (kill-buffer buf))))
2166
2167 (defsubst gnus-point-at-bol ()
2168   "Return point at the beginning of the line."
2169   (let ((p (point)))
2170     (beginning-of-line)
2171     (prog1
2172         (point)
2173       (goto-char p))))
2174
2175 (defsubst gnus-point-at-eol ()
2176   "Return point at the end of the line."
2177   (let ((p (point)))
2178     (end-of-line)
2179     (prog1
2180         (point)
2181       (goto-char p))))
2182
2183 (defun gnus-alive-p ()
2184   "Say whether Gnus is running or not."
2185   (and gnus-group-buffer
2186        (get-buffer gnus-group-buffer)))
2187
2188 ;; Delete the current line (and the next N lines.);
2189 (defmacro gnus-delete-line (&optional n)
2190   `(delete-region (progn (beginning-of-line) (point))
2191                   (progn (forward-line ,(or n 1)) (point))))
2192
2193 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2194 (defvar gnus-init-inhibit nil)
2195 (defun gnus-read-init-file (&optional inhibit-next)
2196   (if gnus-init-inhibit
2197       (setq gnus-init-inhibit nil)
2198     (setq gnus-init-inhibit inhibit-next)
2199     (and gnus-init-file
2200          (or (and (file-exists-p gnus-init-file)
2201                   ;; Don't try to load a directory.
2202                   (not (file-directory-p gnus-init-file)))
2203              (file-exists-p (concat gnus-init-file ".el"))
2204              (file-exists-p (concat gnus-init-file ".elc")))
2205          (condition-case var
2206              (load gnus-init-file nil t)
2207            (error
2208             (error "Error in %s: %s" gnus-init-file var))))))
2209
2210 ;; Info access macros.
2211
2212 (defmacro gnus-info-group (info)
2213   `(nth 0 ,info))
2214 (defmacro gnus-info-rank (info)
2215   `(nth 1 ,info))
2216 (defmacro gnus-info-read (info)
2217   `(nth 2 ,info))
2218 (defmacro gnus-info-marks (info)
2219   `(nth 3 ,info))
2220 (defmacro gnus-info-method (info)
2221   `(nth 4 ,info))
2222 (defmacro gnus-info-params (info)
2223   `(nth 5 ,info))
2224
2225 (defmacro gnus-info-level (info)
2226   `(let ((rank (gnus-info-rank ,info)))
2227      (if (consp rank)
2228          (car rank)
2229        rank)))
2230 (defmacro gnus-info-score (info)
2231   `(let ((rank (gnus-info-rank ,info)))
2232      (or (and (consp rank) (cdr rank)) 0)))
2233
2234 (defmacro gnus-info-set-group (info group)
2235   `(setcar ,info ,group))
2236 (defmacro gnus-info-set-rank (info rank)
2237   `(setcar (nthcdr 1 ,info) ,rank))
2238 (defmacro gnus-info-set-read (info read)
2239   `(setcar (nthcdr 2 ,info) ,read))
2240 (defmacro gnus-info-set-marks (info marks)
2241   `(setcar (nthcdr 3 ,info) ,marks))
2242 (defmacro gnus-info-set-method (info method)
2243   `(setcar (nthcdr 4 ,info) ,method))
2244 (defmacro gnus-info-set-params (info params)
2245   `(setcar (nthcdr 5 ,info) ,params))
2246
2247 (defmacro gnus-info-set-level (info level)
2248   `(let ((rank (cdr ,info)))
2249      (if (consp (car rank))
2250          (setcar (car rank) ,level)
2251        (setcar rank ,level))))
2252 (defmacro gnus-info-set-score (info score)
2253   `(let ((rank (cdr ,info)))
2254      (if (consp (car rank))
2255          (setcdr (car rank) ,score)
2256        (setcar rank (cons (car rank) ,score)))))
2257
2258 (defmacro gnus-get-info (group)
2259   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2260
2261 (defun gnus-byte-code (func)
2262   "Return a form that can be `eval'ed based on FUNC."
2263   (let ((fval (symbol-function func)))
2264     (if (byte-code-function-p fval)
2265         (let ((flist (append fval nil)))
2266           (setcar flist 'byte-code)
2267           flist)
2268       (cons 'progn (cddr fval)))))
2269
2270 ;;; Load the compatability functions.
2271
2272 (require 'gnus-cus)
2273 (require 'gnus-ems)
2274
2275 \f
2276 ;;;
2277 ;;; Shutdown
2278 ;;;
2279
2280 (defvar gnus-shutdown-alist nil)
2281
2282 (defun gnus-add-shutdown (function &rest symbols)
2283   "Run FUNCTION whenever one of SYMBOLS is shut down."
2284   (push (cons function symbols) gnus-shutdown-alist))
2285
2286 (defun gnus-shutdown (symbol)
2287   "Shut down everything that waits for SYMBOL."
2288   (let ((alist gnus-shutdown-alist)
2289         entry)
2290     (while (setq entry (pop alist))
2291       (when (memq symbol (cdr entry))
2292         (funcall (car entry))))))
2293
2294 \f
2295
2296 ;; Format specs.  The chunks below are the machine-generated forms
2297 ;; that are to be evaled as the result of the default format strings.
2298 ;; We write them in here to get them byte-compiled.  That way the
2299 ;; default actions will be quite fast, while still retaining the full
2300 ;; flexibility of the user-defined format specs.
2301
2302 ;; First we have lots of dummy defvars to let the compiler know these
2303 ;; are really dynamic variables.
2304
2305 (defvar gnus-tmp-unread)
2306 (defvar gnus-tmp-replied)
2307 (defvar gnus-tmp-score-char)
2308 (defvar gnus-tmp-indentation)
2309 (defvar gnus-tmp-opening-bracket)
2310 (defvar gnus-tmp-lines)
2311 (defvar gnus-tmp-name)
2312 (defvar gnus-tmp-closing-bracket)
2313 (defvar gnus-tmp-subject-or-nil)
2314 (defvar gnus-tmp-subject)
2315 (defvar gnus-tmp-marked)
2316 (defvar gnus-tmp-marked-mark)
2317 (defvar gnus-tmp-subscribed)
2318 (defvar gnus-tmp-process-marked)
2319 (defvar gnus-tmp-number-of-unread)
2320 (defvar gnus-tmp-group-name)
2321 (defvar gnus-tmp-group)
2322 (defvar gnus-tmp-article-number)
2323 (defvar gnus-tmp-unread-and-unselected)
2324 (defvar gnus-tmp-news-method)
2325 (defvar gnus-tmp-news-server)
2326 (defvar gnus-tmp-article-number)
2327 (defvar gnus-mouse-face)
2328 (defvar gnus-mouse-face-prop)
2329
2330 (defun gnus-summary-line-format-spec ()
2331   (insert gnus-tmp-unread gnus-tmp-replied
2332           gnus-tmp-score-char gnus-tmp-indentation)
2333   (put-text-property
2334    (point)
2335    (progn
2336      (insert
2337       gnus-tmp-opening-bracket
2338       (format "%4d: %-20s"
2339               gnus-tmp-lines
2340               (if (> (length gnus-tmp-name) 20)
2341                   (substring gnus-tmp-name 0 20)
2342                 gnus-tmp-name))
2343       gnus-tmp-closing-bracket)
2344      (point))
2345    gnus-mouse-face-prop gnus-mouse-face)
2346   (insert " " gnus-tmp-subject-or-nil "\n"))
2347
2348 (defvar gnus-summary-line-format-spec
2349   (gnus-byte-code 'gnus-summary-line-format-spec))
2350
2351 (defun gnus-summary-dummy-line-format-spec ()
2352   (insert "*  ")
2353   (put-text-property
2354    (point)
2355    (progn
2356      (insert ":                          :")
2357      (point))
2358    gnus-mouse-face-prop gnus-mouse-face)
2359   (insert " " gnus-tmp-subject "\n"))
2360
2361 (defvar gnus-summary-dummy-line-format-spec
2362   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2363
2364 (defun gnus-group-line-format-spec ()
2365   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2366           gnus-tmp-process-marked
2367           gnus-group-indentation
2368           (format "%5s: " gnus-tmp-number-of-unread))
2369   (put-text-property
2370    (point)
2371    (progn
2372      (insert gnus-tmp-group "\n")
2373      (1- (point)))
2374    gnus-mouse-face-prop gnus-mouse-face))
2375 (defvar gnus-group-line-format-spec
2376   (gnus-byte-code 'gnus-group-line-format-spec))
2377
2378 (defvar gnus-format-specs
2379   `((version . ,emacs-version)
2380     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2381     (summary-dummy ,gnus-summary-dummy-line-format
2382                    ,gnus-summary-dummy-line-format-spec)
2383     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2384
2385 (defvar gnus-article-mode-line-format-spec nil)
2386 (defvar gnus-summary-mode-line-format-spec nil)
2387 (defvar gnus-group-mode-line-format-spec nil)
2388
2389 ;;; Phew.  All that gruft is over, fortunately.
2390
2391 \f
2392 ;;;
2393 ;;; Gnus Utility Functions
2394 ;;;
2395
2396 (defun gnus-extract-address-components (from)
2397   (let (name address)
2398     ;; First find the address - the thing with the @ in it.  This may
2399     ;; not be accurate in mail addresses, but does the trick most of
2400     ;; the time in news messages.
2401     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2402         (setq address (substring from (match-beginning 0) (match-end 0))))
2403     ;; Then we check whether the "name <address>" format is used.
2404     (and address
2405          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2406          ;; Linear white space is not required.
2407          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2408          (and (setq name (substring from 0 (match-beginning 0)))
2409               ;; Strip any quotes from the name.
2410               (string-match "\".*\"" name)
2411               (setq name (substring name 1 (1- (match-end 0))))))
2412     ;; If not, then "address (name)" is used.
2413     (or name
2414         (and (string-match "(.+)" from)
2415              (setq name (substring from (1+ (match-beginning 0))
2416                                    (1- (match-end 0)))))
2417         (and (string-match "()" from)
2418              (setq name address))
2419         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2420         ;; XOVER might not support folded From headers.
2421         (and (string-match "(.*" from)
2422              (setq name (substring from (1+ (match-beginning 0))
2423                                    (match-end 0)))))
2424     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2425     (list (or name from) (or address from))))
2426
2427 (defun gnus-fetch-field (field)
2428   "Return the value of the header FIELD of current article."
2429   (save-excursion
2430     (save-restriction
2431       (let ((case-fold-search t)
2432             (inhibit-point-motion-hooks t))
2433         (nnheader-narrow-to-headers)
2434         (mail-fetch-field field)))))
2435
2436 (defun gnus-goto-colon ()
2437   (beginning-of-line)
2438   (search-forward ":" (gnus-point-at-eol) t))
2439
2440 ;;;###autoload
2441 (defun gnus-update-format (var)
2442   "Update the format specification near point."
2443   (interactive
2444    (list
2445     (save-excursion
2446       (eval-defun nil)
2447       ;; Find the end of the current word.
2448       (re-search-forward "[ \t\n]" nil t)
2449       ;; Search backward.
2450       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2451         (match-string 1)))))
2452   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2453                               (match-string 1 var))))
2454          (entry (assq type gnus-format-specs))
2455          value spec)
2456     (when entry
2457       (setq gnus-format-specs (delq entry gnus-format-specs)))
2458     (set
2459      (intern (format "%s-spec" var))
2460      (gnus-parse-format (setq value (symbol-value (intern var)))
2461                         (symbol-value (intern (format "%s-alist" var)))
2462                         (not (string-match "mode" var))))
2463     (setq spec (symbol-value (intern (format "%s-spec" var))))
2464     (push (list type value spec) gnus-format-specs)
2465
2466     (pop-to-buffer "*Gnus Format*")
2467     (erase-buffer)
2468     (lisp-interaction-mode)
2469     (insert (pp-to-string spec))))
2470
2471 (defun gnus-update-format-specifications (&optional force)
2472   "Update all (necessary) format specifications."
2473   ;; Make the indentation array.
2474   (gnus-make-thread-indent-array)
2475
2476   ;; See whether all the stored info needs to be flushed.
2477   (when (or force
2478             (not (equal emacs-version
2479                         (cdr (assq 'version gnus-format-specs)))))
2480     (setq gnus-format-specs nil))
2481
2482   ;; Go through all the formats and see whether they need updating.
2483   (let ((types '(summary summary-dummy group
2484                          summary-mode group-mode article-mode))
2485         new-format entry type val)
2486     (while (setq type (pop types))
2487       ;; Jump to the proper buffer to find out the value of
2488       ;; the variable, if possible.  (It may be buffer-local.)
2489       (save-excursion
2490         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2491               val)
2492           (when (and (boundp buffer)
2493                      (setq val (symbol-value buffer))
2494                      (get-buffer val)
2495                      (buffer-name (get-buffer val)))
2496             (set-buffer (get-buffer val)))
2497           (setq new-format (symbol-value
2498                             (intern (format "gnus-%s-line-format" type))))))
2499       (setq entry (cdr (assq type gnus-format-specs)))
2500       (if (and entry
2501                (equal (car entry) new-format))
2502           ;; Use the old format.
2503           (set (intern (format "gnus-%s-line-format-spec" type))
2504                (cadr entry))
2505         ;; This is a new format.
2506         (setq val
2507               (if (not (stringp new-format))
2508                   ;; This is a function call or something.
2509                   new-format
2510                 ;; This is a "real" format.
2511                 (gnus-parse-format
2512                  new-format
2513                  (symbol-value
2514                   (intern (format "gnus-%s-line-format-alist"
2515                                   (if (eq type 'article-mode)
2516                                       'summary-mode type))))
2517                  (not (string-match "mode$" (symbol-name type))))))
2518         ;; Enter the new format spec into the list.
2519         (if entry
2520             (progn
2521               (setcar (cdr entry) val)
2522               (setcar entry new-format))
2523           (push (list type new-format val) gnus-format-specs))
2524         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2525
2526   (unless (assq 'version gnus-format-specs)
2527     (push (cons 'version emacs-version) gnus-format-specs))
2528
2529   (gnus-update-group-mark-positions)
2530   (gnus-update-summary-mark-positions))
2531
2532 (defun gnus-update-summary-mark-positions ()
2533   "Compute where the summary marks are to go."
2534   (save-excursion
2535     (let ((gnus-replied-mark 129)
2536           (gnus-score-below-mark 130)
2537           (gnus-score-over-mark 130)
2538           (thread nil)
2539           (gnus-visual nil)
2540           pos)
2541       (gnus-set-work-buffer)
2542       (gnus-summary-insert-line
2543        [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2544       (goto-char (point-min))
2545       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2546                                          (- (point) 2)))))
2547       (goto-char (point-min))
2548       (push (cons 'replied (and (search-forward "\201" nil t) (- (point) 2)))
2549             pos)
2550       (goto-char (point-min))
2551       (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2552             pos)
2553       (setq gnus-summary-mark-positions pos))))
2554
2555 (defun gnus-update-group-mark-positions ()
2556   (save-excursion
2557     (let ((gnus-process-mark 128)
2558           (gnus-group-marked '("dummy.group")))
2559       (gnus-set-active "dummy.group" '(0 . 0))
2560       (gnus-set-work-buffer)
2561       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2562       (goto-char (point-min))
2563       (setq gnus-group-mark-positions
2564             (list (cons 'process (and (search-forward "\200" nil t)
2565                                       (- (point) 2))))))))
2566
2567 (defvar gnus-mouse-face-0 'highlight)
2568 (defvar gnus-mouse-face-1 'highlight)
2569 (defvar gnus-mouse-face-2 'highlight)
2570 (defvar gnus-mouse-face-3 'highlight)
2571 (defvar gnus-mouse-face-4 'highlight)
2572
2573 (defun gnus-mouse-face-function (form type)
2574   `(put-text-property
2575     (point) (progn ,@form (point))
2576     gnus-mouse-face-prop
2577     ,(if (equal type 0)
2578          'gnus-mouse-face
2579        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2580
2581 (defvar gnus-face-0 'bold)
2582 (defvar gnus-face-1 'italic)
2583 (defvar gnus-face-2 'bold-italic)
2584 (defvar gnus-face-3 'bold)
2585 (defvar gnus-face-4 'bold)
2586
2587 (defun gnus-face-face-function (form type)
2588   `(put-text-property
2589     (point) (progn ,@form (point))
2590     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2591
2592 (defun gnus-max-width-function (el max-width)
2593   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2594   (if (symbolp el)
2595       `(if (> (length ,el) ,max-width)
2596            (substring ,el 0 ,max-width)
2597          ,el)
2598     `(let ((val (eval ,el)))
2599        (if (numberp val)
2600            (setq val (int-to-string val)))
2601        (if (> (length val) ,max-width)
2602            (substring val 0 ,max-width)
2603          val))))
2604
2605 (defun gnus-parse-format (format spec-alist &optional insert)
2606   ;; This function parses the FORMAT string with the help of the
2607   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2608   ;; string.  If the FORMAT string contains the specifiers %( and %)
2609   ;; the text between them will have the mouse-face text property.
2610   (if (string-match
2611        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2612        format)
2613       (gnus-parse-complex-format format spec-alist)
2614     ;; This is a simple format.
2615     (gnus-parse-simple-format format spec-alist insert)))
2616
2617 (defun gnus-parse-complex-format (format spec-alist)
2618   (save-excursion
2619     (gnus-set-work-buffer)
2620     (insert format)
2621     (goto-char (point-min))
2622     (while (re-search-forward "\"" nil t)
2623       (replace-match "\\\"" nil t))
2624     (goto-char (point-min))
2625     (insert "(\"")
2626     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2627       (let ((number (if (match-beginning 1)
2628                         (match-string 1) "0"))
2629             (delim (aref (match-string 2) 0)))
2630         (if (or (= delim ?\() (= delim ?\{))
2631             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2632                                    " " number " \""))
2633           (replace-match "\")\""))))
2634     (goto-char (point-max))
2635     (insert "\")")
2636     (goto-char (point-min))
2637     (let ((form (read (current-buffer))))
2638       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2639
2640 (defun gnus-complex-form-to-spec (form spec-alist)
2641   (delq nil
2642         (mapcar
2643          (lambda (sform)
2644            (if (stringp sform)
2645                (gnus-parse-simple-format sform spec-alist t)
2646              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2647                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2648                       (nth 1 sform))))
2649          form)))
2650
2651 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2652   ;; This function parses the FORMAT string with the help of the
2653   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2654   ;; string.
2655   (let ((max-width 0)
2656         spec flist fstring newspec elem beg result dontinsert)
2657     (save-excursion
2658       (gnus-set-work-buffer)
2659       (insert format)
2660       (goto-char (point-min))
2661       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2662                                 nil t)
2663         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2664               (setq newspec "%"
2665                     beg (1+ (match-beginning 0)))
2666           ;; First check if there are any specs that look anything like
2667           ;; "%12,12A", ie. with a "max width specification".  These have
2668           ;; to be treated specially.
2669           (if (setq beg (match-beginning 1))
2670               (setq max-width
2671                     (string-to-int
2672                      (buffer-substring
2673                       (1+ (match-beginning 1)) (match-end 1))))
2674             (setq max-width 0)
2675             (setq beg (match-beginning 2)))
2676           ;; Find the specification from `spec-alist'.
2677           (unless (setq elem (cdr (assq spec spec-alist)))
2678             (setq elem '("*" ?s)))
2679           ;; Treat user defined format specifiers specially.
2680           (when (eq (car elem) 'gnus-tmp-user-defined)
2681             (setq elem
2682                   (list
2683                    (list (intern (concat "gnus-user-format-function-"
2684                                          (match-string 3)))
2685                          'gnus-tmp-header) ?s))
2686             (delete-region (match-beginning 3) (match-end 3)))
2687           (if (not (zerop max-width))
2688               (let ((el (car elem)))
2689                 (cond ((= (cadr elem) ?c)
2690                        (setq el (list 'char-to-string el)))
2691                       ((= (cadr elem) ?d)
2692                        (setq el (list 'int-to-string el))))
2693                 (setq flist (cons (gnus-max-width-function el max-width)
2694                                   flist))
2695                 (setq newspec ?s))
2696             (progn
2697               (setq flist (cons (car elem) flist))
2698               (setq newspec (cadr elem)))))
2699         ;; Remove the old specification (and possibly a ",12" string).
2700         (delete-region beg (match-end 2))
2701         ;; Insert the new specification.
2702         (goto-char beg)
2703         (insert newspec))
2704       (setq fstring (buffer-substring 1 (point-max))))
2705     ;; Do some postprocessing to increase efficiency.
2706     (setq
2707      result
2708      (cond
2709       ;; Emptyness.
2710       ((string= fstring "")
2711        nil)
2712       ;; Not a format string.
2713       ((not (string-match "%" fstring))
2714        (list fstring))
2715       ;; A format string with just a single string spec.
2716       ((string= fstring "%s")
2717        (list (car flist)))
2718       ;; A single character.
2719       ((string= fstring "%c")
2720        (list (car flist)))
2721       ;; A single number.
2722       ((string= fstring "%d")
2723        (setq dontinsert)
2724        (if insert
2725            (list `(princ ,(car flist)))
2726          (list `(int-to-string ,(car flist)))))
2727       ;; Just lots of chars and strings.
2728       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2729        (nreverse flist))
2730       ;; A single string spec at the beginning of the spec.
2731       ((string-match "\\`%[sc][^%]+\\'" fstring)
2732        (list (car flist) (substring fstring 2)))
2733       ;; A single string spec in the middle of the spec.
2734       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2735        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2736       ;; A single string spec in the end of the spec.
2737       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2738        (list (match-string 1 fstring) (car flist)))
2739       ;; A more complex spec.
2740       (t
2741        (list (cons 'format (cons fstring (nreverse flist)))))))
2742
2743     (if insert
2744         (when result
2745           (if dontinsert
2746               result
2747             (cons 'insert result)))
2748       (cond ((stringp result)
2749              result)
2750             ((consp result)
2751              (cons 'concat result))
2752             (t "")))))
2753
2754 (defun gnus-eval-format (format &optional alist props)
2755   "Eval the format variable FORMAT, using ALIST.
2756 If PROPS, insert the result."
2757   (let ((form (gnus-parse-format format alist props)))
2758     (if props
2759         (add-text-properties (point) (progn (eval form) (point)) props)
2760       (eval form))))
2761
2762 (defun gnus-remove-text-with-property (prop)
2763   "Delete all text in the current buffer with text property PROP."
2764   (save-excursion
2765     (goto-char (point-min))
2766     (while (not (eobp))
2767       (while (get-text-property (point) prop)
2768         (delete-char 1))
2769       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2770
2771 (defun gnus-set-work-buffer ()
2772   (if (get-buffer gnus-work-buffer)
2773       (progn
2774         (set-buffer gnus-work-buffer)
2775         (erase-buffer))
2776     (set-buffer (get-buffer-create gnus-work-buffer))
2777     (kill-all-local-variables)
2778     (buffer-disable-undo (current-buffer))
2779     (gnus-add-current-to-buffer-list)))
2780
2781 ;; Article file names when saving.
2782
2783 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2784   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2785 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2786 Otherwise, it is like ~/News/news/group/num."
2787   (let ((default
2788           (expand-file-name
2789            (concat (if (gnus-use-long-file-name 'not-save)
2790                        (gnus-capitalize-newsgroup newsgroup)
2791                      (gnus-newsgroup-directory-form newsgroup))
2792                    "/" (int-to-string (mail-header-number headers)))
2793            (or gnus-article-save-directory "~/News"))))
2794     (if (and last-file
2795              (string-equal (file-name-directory default)
2796                            (file-name-directory last-file))
2797              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2798         default
2799       (or last-file default))))
2800
2801 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2802   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2803 If variable `gnus-use-long-file-name' is non-nil, it is
2804 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2805   (let ((default
2806           (expand-file-name
2807            (concat (if (gnus-use-long-file-name 'not-save)
2808                        newsgroup
2809                      (gnus-newsgroup-directory-form newsgroup))
2810                    "/" (int-to-string (mail-header-number headers)))
2811            (or gnus-article-save-directory "~/News"))))
2812     (if (and last-file
2813              (string-equal (file-name-directory default)
2814                            (file-name-directory last-file))
2815              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2816         default
2817       (or last-file default))))
2818
2819 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2820   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2821 If variable `gnus-use-long-file-name' is non-nil, it is
2822 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2823   (or last-file
2824       (expand-file-name
2825        (if (gnus-use-long-file-name 'not-save)
2826            (gnus-capitalize-newsgroup newsgroup)
2827          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2828        (or gnus-article-save-directory "~/News"))))
2829
2830 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2831   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2832 If variable `gnus-use-long-file-name' is non-nil, it is
2833 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2834   (or last-file
2835       (expand-file-name
2836        (if (gnus-use-long-file-name 'not-save)
2837            newsgroup
2838          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2839        (or gnus-article-save-directory "~/News"))))
2840
2841 ;; For subscribing new newsgroup
2842
2843 (defun gnus-subscribe-hierarchical-interactive (groups)
2844   (let ((groups (sort groups 'string<))
2845         prefixes prefix start ans group starts)
2846     (while groups
2847       (setq prefixes (list "^"))
2848       (while (and groups prefixes)
2849         (while (not (string-match (car prefixes) (car groups)))
2850           (setq prefixes (cdr prefixes)))
2851         (setq prefix (car prefixes))
2852         (setq start (1- (length prefix)))
2853         (if (and (string-match "[^\\.]\\." (car groups) start)
2854                  (cdr groups)
2855                  (setq prefix
2856                        (concat "^" (substring (car groups) 0 (match-end 0))))
2857                  (string-match prefix (cadr groups)))
2858             (progn
2859               (setq prefixes (cons prefix prefixes))
2860               (message "Descend hierarchy %s? ([y]nsq): "
2861                        (substring prefix 1 (1- (length prefix))))
2862               (setq ans (read-char))
2863               (cond ((= ans ?n)
2864                      (while (and groups
2865                                  (string-match prefix
2866                                                (setq group (car groups))))
2867                        (setq gnus-killed-list
2868                              (cons group gnus-killed-list))
2869                        (gnus-sethash group group gnus-killed-hashtb)
2870                        (setq groups (cdr groups)))
2871                      (setq starts (cdr starts)))
2872                     ((= ans ?s)
2873                      (while (and groups
2874                                  (string-match prefix
2875                                                (setq group (car groups))))
2876                        (gnus-sethash group group gnus-killed-hashtb)
2877                        (gnus-subscribe-alphabetically (car groups))
2878                        (setq groups (cdr groups)))
2879                      (setq starts (cdr starts)))
2880                     ((= ans ?q)
2881                      (while groups
2882                        (setq group (car groups))
2883                        (setq gnus-killed-list (cons group gnus-killed-list))
2884                        (gnus-sethash group group gnus-killed-hashtb)
2885                        (setq groups (cdr groups))))
2886                     (t nil)))
2887           (message "Subscribe %s? ([n]yq)" (car groups))
2888           (setq ans (read-char))
2889           (setq group (car groups))
2890           (cond ((= ans ?y)
2891                  (gnus-subscribe-alphabetically (car groups))
2892                  (gnus-sethash group group gnus-killed-hashtb))
2893                 ((= ans ?q)
2894                  (while groups
2895                    (setq group (car groups))
2896                    (setq gnus-killed-list (cons group gnus-killed-list))
2897                    (gnus-sethash group group gnus-killed-hashtb)
2898                    (setq groups (cdr groups))))
2899                 (t
2900                  (setq gnus-killed-list (cons group gnus-killed-list))
2901                  (gnus-sethash group group gnus-killed-hashtb)))
2902           (setq groups (cdr groups)))))))
2903
2904 (defun gnus-subscribe-randomly (newsgroup)
2905   "Subscribe new NEWSGROUP by making it the first newsgroup."
2906   (gnus-subscribe-newsgroup newsgroup))
2907
2908 (defun gnus-subscribe-alphabetically (newgroup)
2909   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2910   (let ((groups (cdr gnus-newsrc-alist))
2911         before)
2912     (while (and (not before) groups)
2913       (if (string< newgroup (caar groups))
2914           (setq before (caar groups))
2915         (setq groups (cdr groups))))
2916     (gnus-subscribe-newsgroup newgroup before)))
2917
2918 (defun gnus-subscribe-hierarchically (newgroup)
2919   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2920   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2921   (save-excursion
2922     (set-buffer (find-file-noselect gnus-current-startup-file))
2923     (let ((groupkey newgroup)
2924           before)
2925       (while (and (not before) groupkey)
2926         (goto-char (point-min))
2927         (let ((groupkey-re
2928                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2929           (while (and (re-search-forward groupkey-re nil t)
2930                       (progn
2931                         (setq before (match-string 1))
2932                         (string< before newgroup)))))
2933         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2934         (setq groupkey
2935               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2936                   (substring groupkey (match-beginning 1) (match-end 1)))))
2937       (gnus-subscribe-newsgroup newgroup before))))
2938
2939 (defun gnus-subscribe-interactively (group)
2940   "Subscribe the new GROUP interactively.
2941 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2942 it is killed."
2943   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2944       (gnus-subscribe-hierarchically group)
2945     (push group gnus-killed-list)))
2946
2947 (defun gnus-subscribe-zombies (group)
2948   "Make the new GROUP into a zombie group."
2949   (push group gnus-zombie-list))
2950
2951 (defun gnus-subscribe-killed (group)
2952   "Make the new GROUP a killed group."
2953   (push group gnus-killed-list))
2954
2955 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2956   "Subscribe new NEWSGROUP.
2957 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2958 the first newsgroup."
2959   ;; We subscribe the group by changing its level to `subscribed'.
2960   (gnus-group-change-level
2961    newsgroup gnus-level-default-subscribed
2962    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2963   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2964
2965 ;; For directories
2966
2967 (defun gnus-newsgroup-directory-form (newsgroup)
2968   "Make hierarchical directory name from NEWSGROUP name."
2969   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
2970         (len (length newsgroup))
2971         idx)
2972     ;; If this is a foreign group, we don't want to translate the
2973     ;; entire name.
2974     (if (setq idx (string-match ":" newsgroup))
2975         (aset newsgroup idx ?/)
2976       (setq idx 0))
2977     ;; Replace all occurrences of `.' with `/'.
2978     (while (< idx len)
2979       (if (= (aref newsgroup idx) ?.)
2980           (aset newsgroup idx ?/))
2981       (setq idx (1+ idx)))
2982     newsgroup))
2983
2984 (defun gnus-newsgroup-savable-name (group)
2985   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2986   ;; with dots.
2987   (nnheader-replace-chars-in-string group ?/ ?.))
2988
2989 (defun gnus-make-directory (dir)
2990   "Make DIRECTORY recursively."
2991   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
2992   ;; of the many mysteries of the universe.
2993   (let* ((dir (expand-file-name dir default-directory))
2994          dirs err)
2995     (if (string-match "/$" dir)
2996         (setq dir (substring dir 0 (match-beginning 0))))
2997     ;; First go down the path until we find a directory that exists.
2998     (while (not (file-exists-p dir))
2999       (setq dirs (cons dir dirs))
3000       (string-match "/[^/]+$" dir)
3001       (setq dir (substring dir 0 (match-beginning 0))))
3002     ;; Then create all the subdirs.
3003     (while (and dirs (not err))
3004       (condition-case ()
3005           (make-directory (car dirs))
3006         (error (setq err t)))
3007       (setq dirs (cdr dirs)))
3008     ;; We return whether we were successful or not.
3009     (not dirs)))
3010
3011 (defun gnus-capitalize-newsgroup (newsgroup)
3012   "Capitalize NEWSGROUP name."
3013   (and (not (zerop (length newsgroup)))
3014        (concat (char-to-string (upcase (aref newsgroup 0)))
3015                (substring newsgroup 1))))
3016
3017 ;; Various... things.
3018
3019 (defun gnus-simplify-subject (subject &optional re-only)
3020   "Remove `Re:' and words in parentheses.
3021 If RE-ONLY is non-nil, strip leading `Re:'s only."
3022   (let ((case-fold-search t))           ;Ignore case.
3023     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3024     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3025       (setq subject (substring subject (match-end 0))))
3026     ;; Remove uninteresting prefixes.
3027     (if (and (not re-only)
3028              gnus-simplify-ignored-prefixes
3029              (string-match gnus-simplify-ignored-prefixes subject))
3030         (setq subject (substring subject (match-end 0))))
3031     ;; Remove words in parentheses from end.
3032     (unless re-only
3033       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3034         (setq subject (substring subject 0 (match-beginning 0)))))
3035     ;; Return subject string.
3036     subject))
3037
3038 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3039 ;; all whitespace.
3040 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3041 (defun gnus-simplify-buffer-fuzzy ()
3042   (goto-char (point-min))
3043   (while (search-forward "\t" nil t)
3044     (replace-match " " t t))
3045   (goto-char (point-min))
3046   (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3047   (goto-char (match-beginning 0))
3048   (while (or
3049           (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3050           (looking-at "^[[].*: .*[]]$"))
3051     (goto-char (point-min))
3052     (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3053                               nil t)
3054       (replace-match "" t t))
3055     (goto-char (point-min))
3056     (while (re-search-forward "^[[].*: .*[]]$" nil t)
3057       (goto-char (match-end 0))
3058       (delete-char -1)
3059       (delete-region
3060        (progn (goto-char (match-beginning 0)))
3061        (re-search-forward ":"))))
3062   (goto-char (point-min))
3063   (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3064     (replace-match "" t t))
3065   (goto-char (point-min))
3066   (while (re-search-forward "  +" nil t)
3067     (replace-match " " t t))
3068   (goto-char (point-min))
3069   (while (re-search-forward " $" nil t)
3070     (replace-match "" t t))
3071   (goto-char (point-min))
3072   (while (re-search-forward "^ +" nil t)
3073     (replace-match "" t t))
3074   (goto-char (point-min))
3075   (when gnus-simplify-subject-fuzzy-regexp
3076     (if (listp gnus-simplify-subject-fuzzy-regexp)
3077         (let ((list gnus-simplify-subject-fuzzy-regexp))
3078           (while list
3079             (goto-char (point-min))
3080             (while (re-search-forward (car list) nil t)
3081               (replace-match "" t t))
3082             (setq list (cdr list))))
3083       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3084         (replace-match "" t t)))))
3085
3086 (defun gnus-simplify-subject-fuzzy (subject)
3087   "Siplify a subject string fuzzily."
3088   (save-excursion
3089     (gnus-set-work-buffer)
3090     (let ((case-fold-search t))
3091       (insert subject)
3092       (inline (gnus-simplify-buffer-fuzzy))
3093       (buffer-string))))
3094
3095 ;; Add the current buffer to the list of buffers to be killed on exit.
3096 (defun gnus-add-current-to-buffer-list ()
3097   (or (memq (current-buffer) gnus-buffer-list)
3098       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3099
3100 (defun gnus-string> (s1 s2)
3101   (not (or (string< s1 s2)
3102            (string= s1 s2))))
3103
3104 ;;; General various misc type functions.
3105
3106 (defun gnus-clear-system ()
3107   "Clear all variables and buffers."
3108   ;; Clear Gnus variables.
3109   (let ((variables gnus-variable-list))
3110     (while variables
3111       (set (car variables) nil)
3112       (setq variables (cdr variables))))
3113   ;; Clear other internal variables.
3114   (setq gnus-list-of-killed-groups nil
3115         gnus-have-read-active-file nil
3116         gnus-newsrc-alist nil
3117         gnus-newsrc-hashtb nil
3118         gnus-killed-list nil
3119         gnus-zombie-list nil
3120         gnus-killed-hashtb nil
3121         gnus-active-hashtb nil
3122         gnus-moderated-list nil
3123         gnus-description-hashtb nil
3124         gnus-current-headers nil
3125         gnus-thread-indent-array nil
3126         gnus-newsgroup-headers nil
3127         gnus-newsgroup-name nil
3128         gnus-server-alist nil
3129         gnus-group-list-mode nil
3130         gnus-opened-servers nil
3131         gnus-current-select-method nil)
3132   (gnus-shutdown 'gnus)
3133   ;; Kill the startup file.
3134   (and gnus-current-startup-file
3135        (get-file-buffer gnus-current-startup-file)
3136        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3137   ;; Clear the dribble buffer.
3138   (gnus-dribble-clear)
3139   ;; Kill global KILL file buffer.
3140   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3141     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3142   (gnus-kill-buffer nntp-server-buffer)
3143   ;; Kill Gnus buffers.
3144   (while gnus-buffer-list
3145     (gnus-kill-buffer (pop gnus-buffer-list)))
3146   ;; Remove Gnus frames.
3147   (while gnus-created-frames
3148     (when (frame-live-p (car gnus-created-frames))
3149       ;; We slap a condition-case around this `delete-frame' to ensure 
3150       ;; agains errors if we try do delete the single frame that's left.
3151       (condition-case ()
3152           (delete-frame (car gnus-created-frames))
3153         (error nil)))
3154     (pop gnus-created-frames)))
3155
3156 (defun gnus-windows-old-to-new (setting)
3157   ;; First we take care of the really, really old Gnus 3 actions.
3158   (when (symbolp setting)
3159     (setq setting
3160           ;; Take care of ooold GNUS 3.x values.
3161           (cond ((eq setting 'SelectArticle) 'article)
3162                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3163                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3164                 (t setting))))
3165   (if (or (listp setting)
3166           (not (and gnus-window-configuration
3167                     (memq setting '(group summary article)))))
3168       setting
3169     (let* ((setting (if (eq setting 'group)
3170                         (if (assq 'newsgroup gnus-window-configuration)
3171                             'newsgroup
3172                           'newsgroups) setting))
3173            (elem (cadr (assq setting gnus-window-configuration)))
3174            (total (apply '+ elem))
3175            (types '(group summary article))
3176            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3177            (i 0)
3178            perc
3179            out)
3180       (while (< i 3)
3181         (or (not (numberp (nth i elem)))
3182             (zerop (nth i elem))
3183             (progn
3184               (setq perc  (/ (float (nth 0 elem)) total))
3185               (setq out (cons (if (eq pbuf (nth i types))
3186                                   (vector (nth i types) perc 'point)
3187                                 (vector (nth i types) perc))
3188                               out))))
3189         (setq i (1+ i)))
3190       (list (nreverse out)))))
3191
3192 ;;;###autoload
3193 (defun gnus-add-configuration (conf)
3194   "Add the window configuration CONF to `gnus-buffer-configuration'."
3195   (setq gnus-buffer-configuration
3196         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3197                          gnus-buffer-configuration))))
3198
3199 (defvar gnus-frame-list nil)
3200
3201 (defun gnus-configure-frame (split &optional window)
3202   "Split WINDOW according to SPLIT."
3203   (unless window
3204     (setq window (get-buffer-window (current-buffer))))
3205   (select-window window)
3206   ;; This might be an old-stylee buffer config.
3207   (when (vectorp split)
3208     (setq split (append split nil)))
3209   (when (or (consp (car split))
3210             (vectorp (car split)))
3211     (push 1.0 split)
3212     (push 'vertical split))
3213   ;; The SPLIT might be something that is to be evaled to
3214   ;; return a new SPLIT.
3215   (while (and (not (assq (car split) gnus-window-to-buffer))
3216               (gnus-functionp (car split)))
3217     (setq split (eval split)))
3218   (let* ((type (car split))
3219          (subs (cddr split))
3220          (len (if (eq type 'horizontal) (window-width) (window-height)))
3221          (total 0)
3222          (window-min-width (or gnus-window-min-width window-min-width))
3223          (window-min-height (or gnus-window-min-height window-min-height))
3224          s result new-win rest comp-subs size sub)
3225     (cond
3226      ;; Nothing to do here.
3227      ((null split))
3228      ;; Don't switch buffers.
3229      ((null type)
3230       (and (memq 'point split) window))
3231      ;; This is a buffer to be selected.
3232      ((not (memq type '(frame horizontal vertical)))
3233       (let ((buffer (cond ((stringp type) type)
3234                           (t (cdr (assq type gnus-window-to-buffer)))))
3235             buf)
3236         (unless buffer
3237           (error "Illegal buffer type: %s" type))
3238         (unless (setq buf (get-buffer (if (symbolp buffer)
3239                                           (symbol-value buffer) buffer)))
3240           (setq buf (get-buffer-create (if (symbolp buffer)
3241                                            (symbol-value buffer) buffer))))
3242         (switch-to-buffer buf)
3243         ;; We return the window if it has the `point' spec.
3244         (and (memq 'point split) window)))
3245      ;; This is a frame split.
3246      ((eq type 'frame)
3247       (unless gnus-frame-list
3248         (setq gnus-frame-list (list (window-frame
3249                                      (get-buffer-window (current-buffer))))))
3250       (let ((i 0)
3251             params frame fresult)
3252         (while (< i (length subs))
3253           ;; Frame parameter is gotten from the sub-split.
3254           (setq params (cadr (elt subs i)))
3255           ;; It should be a list.
3256           (unless (listp params)
3257             (setq params nil))
3258           ;; Create a new frame?
3259           (unless (setq frame (elt gnus-frame-list i))
3260             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3261             (push frame gnus-created-frames))
3262           ;; Is the old frame still alive?
3263           (unless (frame-live-p frame)
3264             (setcar (nthcdr i gnus-frame-list)
3265                     (setq frame (make-frame params))))
3266           ;; Select the frame in question and do more splits there.
3267           (select-frame frame)
3268           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3269           (incf i))
3270         ;; Select the frame that has the selected buffer.
3271         (when fresult
3272           (select-frame (window-frame fresult)))))
3273      ;; This is a normal split.
3274      (t
3275       (when (> (length subs) 0)
3276         ;; First we have to compute the sizes of all new windows.
3277         (while subs
3278           (setq sub (append (pop subs) nil))
3279           (while (and (not (assq (car sub) gnus-window-to-buffer))
3280                       (gnus-functionp (car sub)))
3281             (setq sub (eval sub)))
3282           (when sub
3283             (push sub comp-subs)
3284             (setq size (cadar comp-subs))
3285             (cond ((equal size 1.0)
3286                    (setq rest (car comp-subs))
3287                    (setq s 0))
3288                   ((floatp size)
3289                    (setq s (floor (* size len))))
3290                   ((integerp size)
3291                    (setq s size))
3292                   (t
3293                    (error "Illegal size: %s" size)))
3294             ;; Try to make sure that we are inside the safe limits.
3295             (cond ((zerop s))
3296                   ((eq type 'horizontal)
3297                    (setq s (max s window-min-width)))
3298                   ((eq type 'vertical)
3299                    (setq s (max s window-min-height))))
3300             (setcar (cdar comp-subs) s)
3301             (incf total s)))
3302         ;; Take care of the "1.0" spec.
3303         (if rest
3304             (setcar (cdr rest) (- len total))
3305           (error "No 1.0 specs in %s" split))
3306         ;; The we do the actual splitting in a nice recursive
3307         ;; fashion.
3308         (setq comp-subs (nreverse comp-subs))
3309         (while comp-subs
3310           (if (null (cdr comp-subs))
3311               (setq new-win window)
3312             (setq new-win
3313                   (split-window window (cadar comp-subs)
3314                                 (eq type 'horizontal))))
3315           (setq result (or (gnus-configure-frame
3316                             (car comp-subs) window) result))
3317           (select-window new-win)
3318           (setq window new-win)
3319           (setq comp-subs (cdr comp-subs))))
3320       ;; Return the proper window, if any.
3321       (when result
3322         (select-window result))))))
3323
3324 (defvar gnus-frame-split-p nil)
3325
3326 (defun gnus-configure-windows (setting &optional force)
3327   (setq setting (gnus-windows-old-to-new setting))
3328   (let ((split (if (symbolp setting)
3329                    (cadr (assq setting gnus-buffer-configuration))
3330                  setting))
3331         all-visible)
3332
3333     (setq gnus-frame-split-p nil)
3334
3335     (unless split
3336       (error "No such setting: %s" setting))
3337
3338     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3339              (not force))
3340         ;; All the windows mentioned are already visible, so we just
3341         ;; put point in the assigned buffer, and do not touch the
3342         ;; winconf.
3343         (select-window all-visible)
3344
3345       ;; Either remove all windows or just remove all Gnus windows.
3346       (let ((frame (selected-frame)))
3347         (unwind-protect
3348             (if gnus-use-full-window
3349                 ;; We want to remove all other windows.
3350                 (if (not gnus-frame-split-p)
3351                     ;; This is not a `frame' split, so we ignore the
3352                     ;; other frames.  
3353                     (delete-other-windows)
3354                   ;; This is a `frame' split, so we delete all windows
3355                   ;; on all frames.
3356                   (mapcar 
3357                    (lambda (frame)
3358                      (unless (eq (cdr (assq 'minibuffer
3359                                             (frame-parameters frame)))
3360                                  'only)
3361                        (select-frame frame)
3362                        (delete-other-windows)))
3363                    (frame-list)))
3364               ;; Just remove some windows.
3365               (gnus-remove-some-windows)
3366               (switch-to-buffer nntp-server-buffer))
3367           (select-frame frame)))
3368
3369       (switch-to-buffer nntp-server-buffer)
3370       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3371
3372 (defun gnus-all-windows-visible-p (split)
3373   "Say whether all buffers in SPLIT are currently visible.
3374 In particular, the value returned will be the window that
3375 should have point."
3376   (let ((stack (list split))
3377         (all-visible t)
3378         type buffer win buf)
3379     (while (and (setq split (pop stack))
3380                 all-visible)
3381       ;; Be backwards compatible.
3382       (when (vectorp split)
3383         (setq split (append split nil)))
3384       (when (or (consp (car split))
3385                 (vectorp (car split)))
3386         (push 1.0 split)
3387         (push 'vertical split))
3388       ;; The SPLIT might be something that is to be evaled to
3389       ;; return a new SPLIT.
3390       (while (and (not (assq (car split) gnus-window-to-buffer))
3391                   (gnus-functionp (car split)))
3392         (setq split (eval split)))
3393
3394       (setq type (elt split 0))
3395       (cond
3396        ;; Nothing here.
3397        ((null split) t)
3398        ;; A buffer.
3399        ((not (memq type '(horizontal vertical frame)))
3400         (setq buffer (cond ((stringp type) type)
3401                            (t (cdr (assq type gnus-window-to-buffer)))))
3402         (unless buffer
3403           (error "Illegal buffer type: %s" type))
3404         (when (setq buf (get-buffer (if (symbolp buffer)
3405                                         (symbol-value buffer)
3406                                       buffer)))
3407           (setq win (get-buffer-window buf t)))
3408         (if win
3409             (when (memq 'point split)
3410                 (setq all-visible win))
3411           (setq all-visible nil)))
3412        (t
3413         (when (eq type 'frame)
3414           (setq gnus-frame-split-p t))
3415         (setq stack (append (cddr split) stack)))))
3416     (unless (eq all-visible t)
3417       all-visible)))
3418
3419 (defun gnus-window-top-edge (&optional window)
3420   (nth 1 (window-edges window)))
3421
3422 (defun gnus-remove-some-windows ()
3423   (let ((buffers gnus-window-to-buffer)
3424         buf bufs lowest-buf lowest)
3425     (save-excursion
3426       ;; Remove windows on all known Gnus buffers.
3427       (while buffers
3428         (setq buf (cdar buffers))
3429         (if (symbolp buf)
3430             (setq buf (and (boundp buf) (symbol-value buf))))
3431         (and buf
3432              (get-buffer-window buf)
3433              (progn
3434                (setq bufs (cons buf bufs))
3435                (pop-to-buffer buf)
3436                (if (or (not lowest)
3437                        (< (gnus-window-top-edge) lowest))
3438                    (progn
3439                      (setq lowest (gnus-window-top-edge))
3440                      (setq lowest-buf buf)))))
3441         (setq buffers (cdr buffers)))
3442       ;; Remove windows on *all* summary buffers.
3443       (walk-windows
3444        (lambda (win)
3445          (let ((buf (window-buffer win)))
3446            (if (string-match    "^\\*Summary" (buffer-name buf))
3447                (progn
3448                  (setq bufs (cons buf bufs))
3449                  (pop-to-buffer buf)
3450                  (if (or (not lowest)
3451                          (< (gnus-window-top-edge) lowest))
3452                      (progn
3453                        (setq lowest-buf buf)
3454                        (setq lowest (gnus-window-top-edge)))))))))
3455       (and lowest-buf
3456            (progn
3457              (pop-to-buffer lowest-buf)
3458              (switch-to-buffer nntp-server-buffer)))
3459       (while bufs
3460         (and (not (eq (car bufs) lowest-buf))
3461              (delete-windows-on (car bufs)))
3462         (setq bufs (cdr bufs))))))
3463
3464 (defun gnus-version ()
3465   "Version numbers of this version of Gnus."
3466   (interactive)
3467   (let ((methods gnus-valid-select-methods)
3468         (mess gnus-version)
3469         meth)
3470     ;; Go through all the legal select methods and add their version
3471     ;; numbers to the total version string.  Only the backends that are
3472     ;; currently in use will have their message numbers taken into
3473     ;; consideration.
3474     (while methods
3475       (setq meth (intern (concat (caar methods) "-version")))
3476       (and (boundp meth)
3477            (stringp (symbol-value meth))
3478            (setq mess (concat mess "; " (symbol-value meth))))
3479       (setq methods (cdr methods)))
3480     (gnus-message 2 mess)))
3481
3482 (defun gnus-info-find-node ()
3483   "Find Info documentation of Gnus."
3484   (interactive)
3485   ;; Enlarge info window if needed.
3486   (let ((mode major-mode)
3487         gnus-info-buffer)
3488     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3489     (setq gnus-info-buffer (current-buffer))
3490     (gnus-configure-windows 'info)))
3491
3492 (defun gnus-days-between (date1 date2)
3493   ;; Return the number of days between date1 and date2.
3494   (- (gnus-day-number date1) (gnus-day-number date2)))
3495
3496 (defun gnus-day-number (date)
3497   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3498                      (timezone-parse-date date))))
3499     (timezone-absolute-from-gregorian
3500      (nth 1 dat) (nth 2 dat) (car dat))))
3501
3502 (defun gnus-encode-date (date)
3503   "Convert DATE to internal time."
3504   (let* ((parse (timezone-parse-date date))
3505          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3506          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3507     (encode-time (caddr time) (cadr time) (car time)
3508                  (caddr date) (cadr date) (car date) (nth 4 date))))
3509
3510 (defun gnus-time-minus (t1 t2)
3511   "Subtract two internal times."
3512   (let ((borrow (< (cadr t1) (cadr t2))))
3513     (list (- (car t1) (car t2) (if borrow 1 0))
3514           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3515
3516 (defun gnus-file-newer-than (file date)
3517   (let ((fdate (nth 5 (file-attributes file))))
3518     (or (> (car fdate) (car date))
3519         (and (= (car fdate) (car date))
3520              (> (nth 1 fdate) (nth 1 date))))))
3521
3522 (defmacro gnus-local-set-keys (&rest plist)
3523   "Set the keys in PLIST in the current keymap."
3524   `(gnus-define-keys-1 (current-local-map) ',plist))
3525
3526 (defmacro gnus-define-keys (keymap &rest plist)
3527   "Define all keys in PLIST in KEYMAP."
3528   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3529
3530 (put 'gnus-define-keys 'lisp-indent-function 1)
3531 (put 'gnus-define-keys 'lisp-indent-hook 1)
3532 (put 'gnus-define-keymap 'lisp-indent-function 1)
3533 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3534
3535 (defmacro gnus-define-keymap (keymap &rest plist)
3536   "Define all keys in PLIST in KEYMAP."
3537   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3538
3539 (defun gnus-define-keys-1 (keymap plist)
3540   (when (null keymap)
3541     (error "Can't set keys in a null keymap"))
3542   (cond ((symbolp keymap)
3543          (setq keymap (symbol-value keymap)))
3544         ((keymapp keymap))
3545         ((listp keymap)
3546          (set (car keymap) nil)
3547          (define-prefix-command (car keymap))
3548          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3549          (setq keymap (symbol-value (car keymap)))))
3550   (let (key)
3551     (while plist
3552       (when (symbolp (setq key (pop plist)))
3553         (setq key (symbol-value key)))
3554       (define-key keymap key (pop plist)))))
3555
3556 (defun gnus-group-read-only-p (&optional group)
3557   "Check whether GROUP supports editing or not.
3558 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3559 that that variable is buffer-local to the summary buffers."
3560   (let ((group (or group gnus-newsgroup-name)))
3561     (not (gnus-check-backend-function 'request-replace-article group))))
3562
3563 (defun gnus-group-total-expirable-p (group)
3564   "Check whether GROUP is total-expirable or not."
3565   (let ((params (gnus-info-params (gnus-get-info group))))
3566     (or (memq 'total-expire params)
3567         (cdr (assq 'total-expire params)) ; (total-expire . t)
3568         (and gnus-total-expirable-newsgroups ; Check var.
3569              (string-match gnus-total-expirable-newsgroups group)))))
3570
3571 (defun gnus-group-auto-expirable-p (group)
3572   "Check whether GROUP is total-expirable or not."
3573   (let ((params (gnus-info-params (gnus-get-info group))))
3574     (or (memq 'auto-expire params)
3575         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3576         (and gnus-auto-expirable-newsgroups ; Check var.
3577              (string-match gnus-auto-expirable-newsgroups group)))))
3578
3579 (defun gnus-virtual-group-p (group)
3580   "Say whether GROUP is virtual or not."
3581   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3582                         gnus-valid-select-methods)))
3583
3584 (defun gnus-news-group-p (group &optional article)
3585   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3586   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3587       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3588            (eq (gnus-request-type group article) 'news))))
3589
3590 (defsubst gnus-simplify-subject-fully (subject)
3591   "Simplify a subject string according to the user's wishes."
3592   (cond
3593    ((null gnus-summary-gather-subject-limit)
3594     (gnus-simplify-subject-re subject))
3595    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3596     (gnus-simplify-subject-fuzzy subject))
3597    ((numberp gnus-summary-gather-subject-limit)
3598     (gnus-limit-string (gnus-simplify-subject-re subject)
3599                        gnus-summary-gather-subject-limit))
3600    (t
3601     subject)))
3602
3603 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3604   "Check whether two subjects are equal.  If optional argument
3605 simple-first is t, first argument is already simplified."
3606   (cond
3607    ((null simple-first)
3608     (equal (gnus-simplify-subject-fully s1)
3609            (gnus-simplify-subject-fully s2)))
3610    (t
3611     (equal s1
3612            (gnus-simplify-subject-fully s2)))))
3613
3614 ;; Returns a list of writable groups.
3615 (defun gnus-writable-groups ()
3616   (let ((alist gnus-newsrc-alist)
3617         groups group)
3618     (while (setq group (car (pop alist)))
3619       (unless (gnus-group-read-only-p group)
3620         (push group groups)))
3621     (nreverse groups)))
3622
3623 (defun gnus-completing-read (default prompt &rest args)
3624   ;; Like `completing-read', except that DEFAULT is the default argument.
3625   (let* ((prompt (concat prompt " (default " default ") "))
3626          (answer (apply 'completing-read prompt args)))
3627     (if (or (null answer) (zerop (length answer)))
3628         default
3629       answer)))
3630
3631 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3632 ;; the echo area.
3633 (defun gnus-y-or-n-p (prompt)
3634   (prog1
3635       (y-or-n-p prompt)
3636     (message "")))
3637
3638 (defun gnus-yes-or-no-p (prompt)
3639   (prog1
3640       (yes-or-no-p prompt)
3641     (message "")))
3642
3643 ;; Check whether to use long file names.
3644 (defun gnus-use-long-file-name (symbol)
3645   ;; The variable has to be set...
3646   (and gnus-use-long-file-name
3647        ;; If it isn't a list, then we return t.
3648        (or (not (listp gnus-use-long-file-name))
3649            ;; If it is a list, and the list contains `symbol', we
3650            ;; return nil.
3651            (not (memq symbol gnus-use-long-file-name)))))
3652
3653 ;; I suspect there's a better way, but I haven't taken the time to do
3654 ;; it yet. -erik selberg@cs.washington.edu
3655 (defun gnus-dd-mmm (messy-date)
3656   "Return a string like DD-MMM from a big messy string"
3657   (let ((datevec (timezone-parse-date messy-date)))
3658     (format "%2s-%s"
3659             (condition-case ()
3660                 ;; Make sure leading zeroes are stripped.
3661                 (number-to-string (string-to-number (aref datevec 2)))
3662               (error "??"))
3663             (capitalize
3664              (or (car
3665                   (nth (1- (string-to-number (aref datevec 1)))
3666                        timezone-months-assoc))
3667                  "???")))))
3668
3669 ;; Make a hash table (default and minimum size is 255).
3670 ;; Optional argument HASHSIZE specifies the table size.
3671 (defun gnus-make-hashtable (&optional hashsize)
3672   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3673
3674 ;; Make a number that is suitable for hashing; bigger than MIN and one
3675 ;; less than 2^x.
3676 (defun gnus-create-hash-size (min)
3677   (let ((i 1))
3678     (while (< i min)
3679       (setq i (* 2 i)))
3680     (1- i)))
3681
3682 ;; Show message if message has a lower level than `gnus-verbose'.
3683 ;; Guideline for numbers:
3684 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3685 ;; for things that take a long time, 7 - not very important messages
3686 ;; on stuff, 9 - messages inside loops.
3687 (defun gnus-message (level &rest args)
3688   (if (<= level gnus-verbose)
3689       (apply 'message args)
3690     ;; We have to do this format thingy here even if the result isn't
3691     ;; shown - the return value has to be the same as the return value
3692     ;; from `message'.
3693     (apply 'format args)))
3694
3695 (defun gnus-error (level &rest args)
3696   "Beep an error if `gnus-verbose' is on LEVEL or less."
3697   (when (<= (floor level) gnus-verbose)
3698     (apply 'message args)
3699     (ding)
3700     (let (duration)
3701       (when (and (floatp level)
3702                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3703         (sit-for duration))))
3704   nil)
3705
3706 ;; Generate a unique new group name.
3707 (defun gnus-generate-new-group-name (leaf)
3708   (let ((name leaf)
3709         (num 0))
3710     (while (gnus-gethash name gnus-newsrc-hashtb)
3711       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3712     name))
3713
3714 (defsubst gnus-hide-text (b e props)
3715   "Set text PROPS on the B to E region, extending `intangble' 1 past B."
3716   (add-text-properties b e props)
3717   (when (memq 'intangible props)
3718     (put-text-property (max (1- b) (point-min))
3719                        b 'intangible (cddr (memq 'intangible props)))))
3720
3721 (defsubst gnus-unhide-text (b e)
3722   "Remove hidden text properties from region between B and E."
3723   (remove-text-properties b e gnus-hidden-properties)
3724   (when (memq 'intangible gnus-hidden-properties)
3725     (put-text-property (max (1- b) (point-min))
3726                        b 'intangible nil)))
3727
3728 (defun gnus-hide-text-type (b e type)
3729   "Hide text of TYPE between B and E."
3730   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3731
3732 ;; Find out whether the gnus-visual TYPE is wanted.
3733 (defun gnus-visual-p (&optional type class)
3734   (and gnus-visual                      ; Has to be non-nil, at least.
3735        (if (not type)                   ; We don't care about type.
3736            gnus-visual
3737          (if (listp gnus-visual)        ; It's a list, so we check it.
3738              (or (memq type gnus-visual)
3739                  (memq class gnus-visual))
3740            t))))
3741
3742 (defun gnus-parent-id (references)
3743   "Return the last Message-ID in REFERENCES."
3744   (when (and references
3745              (string-match "\\(<[^<>]+>\\)[ \t\n]*\\'" references))
3746     (substring references (match-beginning 1) (match-end 1))))
3747
3748 (defun gnus-split-references (references)
3749   "Return a list of Message-IDs in REFERENCES."
3750   (let ((beg 0)
3751         ids)
3752     (while (string-match "<[^>]+>" references beg)
3753       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3754             ids))
3755     (nreverse ids)))
3756
3757 (defun gnus-buffer-live-p (buffer)
3758   "Say whether BUFFER is alive or not."
3759   (and buffer
3760        (get-buffer buffer)
3761        (buffer-name (get-buffer buffer))))
3762
3763 (defun gnus-ephemeral-group-p (group)
3764   "Say whether GROUP is ephemeral or not."
3765   (gnus-group-get-parameter group 'quit-config))
3766
3767 (defun gnus-group-quit-config (group)
3768   "Return the quit-config of GROUP."
3769   (gnus-group-get-parameter group 'quit-config))
3770
3771 (defun gnus-simplify-mode-line ()
3772   "Make mode lines a bit simpler."
3773   (setq mode-line-modified "-- ")
3774   (when (listp mode-line-format)
3775     (make-local-variable 'mode-line-format)
3776     (setq mode-line-format (copy-sequence mode-line-format))
3777     (when (equal (nth 3 mode-line-format) "   ")
3778       (setcar (nthcdr 3 mode-line-format) " "))))
3779
3780 ;;; List and range functions
3781
3782 (defun gnus-last-element (list)
3783   "Return last element of LIST."
3784   (while (cdr list)
3785     (setq list (cdr list)))
3786   (car list))
3787
3788 (defun gnus-copy-sequence (list)
3789   "Do a complete, total copy of a list."
3790   (if (and (consp list) (not (consp (cdr list))))
3791       (cons (car list) (cdr list))
3792     (mapcar (lambda (elem) (if (consp elem)
3793                                (if (consp (cdr elem))
3794                                    (gnus-copy-sequence elem)
3795                                  (cons (car elem) (cdr elem)))
3796                              elem))
3797             list)))
3798
3799 (defun gnus-set-difference (list1 list2)
3800   "Return a list of elements of LIST1 that do not appear in LIST2."
3801   (let ((list1 (copy-sequence list1)))
3802     (while list2
3803       (setq list1 (delq (car list2) list1))
3804       (setq list2 (cdr list2)))
3805     list1))
3806
3807 (defun gnus-sorted-complement (list1 list2)
3808   "Return a list of elements of LIST1 that do not appear in LIST2.
3809 Both lists have to be sorted over <."
3810   (let (out)
3811     (if (or (null list1) (null list2))
3812         (or list1 list2)
3813       (while (and list1 list2)
3814         (cond ((= (car list1) (car list2))
3815                (setq list1 (cdr list1)
3816                      list2 (cdr list2)))
3817               ((< (car list1) (car list2))
3818                (setq out (cons (car list1) out))
3819                (setq list1 (cdr list1)))
3820               (t
3821                (setq out (cons (car list2) out))
3822                (setq list2 (cdr list2)))))
3823       (nconc (nreverse out) (or list1 list2)))))
3824
3825 (defun gnus-intersection (list1 list2)
3826   (let ((result nil))
3827     (while list2
3828       (if (memq (car list2) list1)
3829           (setq result (cons (car list2) result)))
3830       (setq list2 (cdr list2)))
3831     result))
3832
3833 (defun gnus-sorted-intersection (list1 list2)
3834   ;; LIST1 and LIST2 have to be sorted over <.
3835   (let (out)
3836     (while (and list1 list2)
3837       (cond ((= (car list1) (car list2))
3838              (setq out (cons (car list1) out)
3839                    list1 (cdr list1)
3840                    list2 (cdr list2)))
3841             ((< (car list1) (car list2))
3842              (setq list1 (cdr list1)))
3843             (t
3844              (setq list2 (cdr list2)))))
3845     (nreverse out)))
3846
3847 (defun gnus-set-sorted-intersection (list1 list2)
3848   ;; LIST1 and LIST2 have to be sorted over <.
3849   ;; This function modifies LIST1.
3850   (let* ((top (cons nil list1))
3851          (prev top))
3852     (while (and list1 list2)
3853       (cond ((= (car list1) (car list2))
3854              (setq prev list1
3855                    list1 (cdr list1)
3856                    list2 (cdr list2)))
3857             ((< (car list1) (car list2))
3858              (setcdr prev (cdr list1))
3859              (setq list1 (cdr list1)))
3860             (t
3861              (setq list2 (cdr list2)))))
3862     (setcdr prev nil)
3863     (cdr top)))
3864
3865 (defun gnus-compress-sequence (numbers &optional always-list)
3866   "Convert list of numbers to a list of ranges or a single range.
3867 If ALWAYS-LIST is non-nil, this function will always release a list of
3868 ranges."
3869   (let* ((first (car numbers))
3870          (last (car numbers))
3871          result)
3872     (if (null numbers)
3873         nil
3874       (if (not (listp (cdr numbers)))
3875           numbers
3876         (while numbers
3877           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3878                 ((= (1+ last) (car numbers)) ;Still in sequence
3879                  (setq last (car numbers)))
3880                 (t                      ;End of one sequence
3881                  (setq result
3882                        (cons (if (= first last) first
3883                                (cons first last)) result))
3884                  (setq first (car numbers))
3885                  (setq last  (car numbers))))
3886           (setq numbers (cdr numbers)))
3887         (if (and (not always-list) (null result))
3888             (if (= first last) (list first) (cons first last))
3889           (nreverse (cons (if (= first last) first (cons first last))
3890                           result)))))))
3891
3892 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3893 (defun gnus-uncompress-range (ranges)
3894   "Expand a list of ranges into a list of numbers.
3895 RANGES is either a single range on the form `(num . num)' or a list of
3896 these ranges."
3897   (let (first last result)
3898     (cond
3899      ((null ranges)
3900       nil)
3901      ((not (listp (cdr ranges)))
3902       (setq first (car ranges))
3903       (setq last (cdr ranges))
3904       (while (<= first last)
3905         (setq result (cons first result))
3906         (setq first (1+ first)))
3907       (nreverse result))
3908      (t
3909       (while ranges
3910         (if (atom (car ranges))
3911             (if (numberp (car ranges))
3912                 (setq result (cons (car ranges) result)))
3913           (setq first (caar ranges))
3914           (setq last  (cdar ranges))
3915           (while (<= first last)
3916             (setq result (cons first result))
3917             (setq first (1+ first))))
3918         (setq ranges (cdr ranges)))
3919       (nreverse result)))))
3920
3921 (defun gnus-add-to-range (ranges list)
3922   "Return a list of ranges that has all articles from both RANGES and LIST.
3923 Note: LIST has to be sorted over `<'."
3924   (if (not ranges)
3925       (gnus-compress-sequence list t)
3926     (setq list (copy-sequence list))
3927     (or (listp (cdr ranges))
3928         (setq ranges (list ranges)))
3929     (let ((out ranges)
3930           ilist lowest highest temp)
3931       (while (and ranges list)
3932         (setq ilist list)
3933         (setq lowest (or (and (atom (car ranges)) (car ranges))
3934                          (caar ranges)))
3935         (while (and list (cdr list) (< (cadr list) lowest))
3936           (setq list (cdr list)))
3937         (if (< (car ilist) lowest)
3938             (progn
3939               (setq temp list)
3940               (setq list (cdr list))
3941               (setcdr temp nil)
3942               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3943         (setq highest (or (and (atom (car ranges)) (car ranges))
3944                           (cdar ranges)))
3945         (while (and list (<= (car list) highest))
3946           (setq list (cdr list)))
3947         (setq ranges (cdr ranges)))
3948       (if list
3949           (setq out (nconc (gnus-compress-sequence list t) out)))
3950       (setq out (sort out (lambda (r1 r2)
3951                             (< (or (and (atom r1) r1) (car r1))
3952                                (or (and (atom r2) r2) (car r2))))))
3953       (setq ranges out)
3954       (while ranges
3955         (if (atom (car ranges))
3956             (if (cdr ranges)
3957                 (if (atom (cadr ranges))
3958                     (if (= (1+ (car ranges)) (cadr ranges))
3959                         (progn
3960                           (setcar ranges (cons (car ranges)
3961                                                (cadr ranges)))
3962                           (setcdr ranges (cddr ranges))))
3963                   (if (= (1+ (car ranges)) (caadr ranges))
3964                       (progn
3965                         (setcar (cadr ranges) (car ranges))
3966                         (setcar ranges (cadr ranges))
3967                         (setcdr ranges (cddr ranges))))))
3968           (if (cdr ranges)
3969               (if (atom (cadr ranges))
3970                   (if (= (1+ (cdar ranges)) (cadr ranges))
3971                       (progn
3972                         (setcdr (car ranges) (cadr ranges))
3973                         (setcdr ranges (cddr ranges))))
3974                 (if (= (1+ (cdar ranges)) (caadr ranges))
3975                     (progn
3976                       (setcdr (car ranges) (cdadr ranges))
3977                       (setcdr ranges (cddr ranges)))))))
3978         (setq ranges (cdr ranges)))
3979       out)))
3980
3981 (defun gnus-remove-from-range (ranges list)
3982   "Return a list of ranges that has all articles from LIST removed from RANGES.
3983 Note: LIST has to be sorted over `<'."
3984   ;; !!! This function shouldn't look like this, but I've got a headache.
3985   (gnus-compress-sequence
3986    (gnus-sorted-complement
3987     (gnus-uncompress-range ranges) list)))
3988
3989 (defun gnus-member-of-range (number ranges)
3990   (if (not (listp (cdr ranges)))
3991       (and (>= number (car ranges))
3992            (<= number (cdr ranges)))
3993     (let ((not-stop t))
3994       (while (and ranges
3995                   (if (numberp (car ranges))
3996                       (>= number (car ranges))
3997                     (>= number (caar ranges)))
3998                   not-stop)
3999         (if (if (numberp (car ranges))
4000                 (= number (car ranges))
4001               (and (>= number (caar ranges))
4002                    (<= number (cdar ranges))))
4003             (setq not-stop nil))
4004         (setq ranges (cdr ranges)))
4005       (not not-stop))))
4006
4007 (defun gnus-range-length (range)
4008   "Return the length RANGE would have if uncompressed."
4009   (length (gnus-uncompress-range range)))
4010
4011 (defun gnus-sublist-p (list sublist)
4012   "Test whether all elements in SUBLIST are members of LIST."
4013   (let ((sublistp t))
4014     (while sublist
4015       (unless (memq (pop sublist) list)
4016         (setq sublistp nil
4017               sublist nil)))
4018     sublistp))
4019
4020 \f
4021 ;;;
4022 ;;; Gnus group mode
4023 ;;;
4024
4025 (defvar gnus-group-mode-map nil)
4026 (put 'gnus-group-mode 'mode-class 'special)
4027
4028 (unless gnus-group-mode-map
4029   (setq gnus-group-mode-map (make-keymap))
4030   (suppress-keymap gnus-group-mode-map)
4031
4032   (gnus-define-keys gnus-group-mode-map
4033     " " gnus-group-read-group
4034     "=" gnus-group-select-group
4035     "\r" gnus-group-select-group
4036     "\M-\r" gnus-group-quick-select-group
4037     "j" gnus-group-jump-to-group
4038     "n" gnus-group-next-unread-group
4039     "p" gnus-group-prev-unread-group
4040     "\177" gnus-group-prev-unread-group
4041     [delete] gnus-group-prev-unread-group
4042     "N" gnus-group-next-group
4043     "P" gnus-group-prev-group
4044     "\M-n" gnus-group-next-unread-group-same-level
4045     "\M-p" gnus-group-prev-unread-group-same-level
4046     "," gnus-group-best-unread-group
4047     "." gnus-group-first-unread-group
4048     "u" gnus-group-unsubscribe-current-group
4049     "U" gnus-group-unsubscribe-group
4050     "c" gnus-group-catchup-current
4051     "C" gnus-group-catchup-current-all
4052     "l" gnus-group-list-groups
4053     "L" gnus-group-list-all-groups
4054     "m" gnus-group-mail
4055     "g" gnus-group-get-new-news
4056     "\M-g" gnus-group-get-new-news-this-group
4057     "R" gnus-group-restart
4058     "r" gnus-group-read-init-file
4059     "B" gnus-group-browse-foreign-server
4060     "b" gnus-group-check-bogus-groups
4061     "F" gnus-find-new-newsgroups
4062     "\C-c\C-d" gnus-group-describe-group
4063     "\M-d" gnus-group-describe-all-groups
4064     "\C-c\C-a" gnus-group-apropos
4065     "\C-c\M-\C-a" gnus-group-description-apropos
4066     "a" gnus-group-post-news
4067     "\ek" gnus-group-edit-local-kill
4068     "\eK" gnus-group-edit-global-kill
4069     "\C-k" gnus-group-kill-group
4070     "\C-y" gnus-group-yank-group
4071     "\C-w" gnus-group-kill-region
4072     "\C-x\C-t" gnus-group-transpose-groups
4073     "\C-c\C-l" gnus-group-list-killed
4074     "\C-c\C-x" gnus-group-expire-articles
4075     "\C-c\M-\C-x" gnus-group-expire-all-groups
4076     "V" gnus-version
4077     "s" gnus-group-save-newsrc
4078     "z" gnus-group-suspend
4079     "Z" gnus-group-clear-dribble
4080     "q" gnus-group-exit
4081     "Q" gnus-group-quit
4082     "?" gnus-group-describe-briefly
4083     "\C-c\C-i" gnus-info-find-node
4084     "\M-e" gnus-group-edit-group-method
4085     "^" gnus-group-enter-server-mode
4086     gnus-mouse-2 gnus-mouse-pick-group
4087     "<" beginning-of-buffer
4088     ">" end-of-buffer
4089     "\C-c\C-b" gnus-bug
4090     "\C-c\C-s" gnus-group-sort-groups
4091     "t" gnus-topic-mode
4092     "\C-c\M-g" gnus-activate-all-groups
4093     "\M-&" gnus-group-universal-argument
4094     "#" gnus-group-mark-group
4095     "\M-#" gnus-group-unmark-group)
4096
4097   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4098     "m" gnus-group-mark-group
4099     "u" gnus-group-unmark-group
4100     "w" gnus-group-mark-region
4101     "m" gnus-group-mark-buffer
4102     "r" gnus-group-mark-regexp
4103     "U" gnus-group-unmark-all-groups)
4104
4105   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4106     "d" gnus-group-make-directory-group
4107     "h" gnus-group-make-help-group
4108     "a" gnus-group-make-archive-group
4109     "k" gnus-group-make-kiboze-group
4110     "m" gnus-group-make-group
4111     "E" gnus-group-edit-group
4112     "e" gnus-group-edit-group-method
4113     "p" gnus-group-edit-group-parameters
4114     "v" gnus-group-add-to-virtual
4115     "V" gnus-group-make-empty-virtual
4116     "D" gnus-group-enter-directory
4117     "f" gnus-group-make-doc-group
4118     "r" gnus-group-rename-group
4119     "\177" gnus-group-delete-group
4120     [delete] gnus-group-delete-group)
4121
4122    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4123      "b" gnus-group-brew-soup
4124      "w" gnus-soup-save-areas
4125      "s" gnus-soup-send-replies
4126      "p" gnus-soup-pack-packet
4127      "r" nnsoup-pack-replies)
4128
4129    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4130      "s" gnus-group-sort-groups
4131      "a" gnus-group-sort-groups-by-alphabet
4132      "u" gnus-group-sort-groups-by-unread
4133      "l" gnus-group-sort-groups-by-level
4134      "v" gnus-group-sort-groups-by-score
4135      "r" gnus-group-sort-groups-by-rank
4136      "m" gnus-group-sort-groups-by-method)
4137
4138    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4139      "k" gnus-group-list-killed
4140      "z" gnus-group-list-zombies
4141      "s" gnus-group-list-groups
4142      "u" gnus-group-list-all-groups
4143      "A" gnus-group-list-active
4144      "a" gnus-group-apropos
4145      "d" gnus-group-description-apropos
4146      "m" gnus-group-list-matching
4147      "M" gnus-group-list-all-matching
4148      "l" gnus-group-list-level)
4149
4150    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4151      "f" gnus-score-flush-cache)
4152
4153    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4154      "f" gnus-group-fetch-faq)
4155
4156    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4157      "l" gnus-group-set-current-level
4158      "t" gnus-group-unsubscribe-current-group
4159      "s" gnus-group-unsubscribe-group
4160      "k" gnus-group-kill-group
4161      "y" gnus-group-yank-group
4162      "w" gnus-group-kill-region
4163      "\C-k" gnus-group-kill-level
4164      "z" gnus-group-kill-all-zombies))
4165
4166 (defun gnus-group-mode ()
4167   "Major mode for reading news.
4168
4169 All normal editing commands are switched off.
4170 \\<gnus-group-mode-map>
4171 The group buffer lists (some of) the groups available.  For instance,
4172 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4173 lists all zombie groups.
4174
4175 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4176 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4177
4178 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4179
4180 The following commands are available:
4181
4182 \\{gnus-group-mode-map}"
4183   (interactive)
4184   (when (and menu-bar-mode
4185              (gnus-visual-p 'group-menu 'menu))
4186     (gnus-group-make-menu-bar))
4187   (kill-all-local-variables)
4188   (gnus-simplify-mode-line)
4189   (setq major-mode 'gnus-group-mode)
4190   (setq mode-name "Group")
4191   (gnus-group-set-mode-line)
4192   (setq mode-line-process nil)
4193   (use-local-map gnus-group-mode-map)
4194   (buffer-disable-undo (current-buffer))
4195   (setq truncate-lines t)
4196   (setq buffer-read-only t)
4197   (run-hooks 'gnus-group-mode-hook))
4198
4199 (defun gnus-mouse-pick-group (e)
4200   "Enter the group under the mouse pointer."
4201   (interactive "e")
4202   (mouse-set-point e)
4203   (gnus-group-read-group nil))
4204
4205 ;; Look at LEVEL and find out what the level is really supposed to be.
4206 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4207 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4208 (defun gnus-group-default-level (&optional level number-or-nil)
4209   (cond
4210    (gnus-group-use-permanent-levels
4211 ;    (setq gnus-group-default-list-level
4212 ;         (or level gnus-group-default-list-level))
4213     (or level gnus-group-default-list-level gnus-level-subscribed))
4214    (number-or-nil
4215     level)
4216    (t
4217     (or level gnus-group-default-list-level gnus-level-subscribed))))
4218
4219 ;;;###autoload
4220 (defun gnus-slave-no-server (&optional arg)
4221   "Read network news as a slave, without connecting to local server"
4222   (interactive "P")
4223   (gnus-no-server arg t))
4224
4225 ;;;###autoload
4226 (defun gnus-no-server (&optional arg slave)
4227   "Read network news.
4228 If ARG is a positive number, Gnus will use that as the
4229 startup level.  If ARG is nil, Gnus will be started at level 2.
4230 If ARG is non-nil and not a positive number, Gnus will
4231 prompt the user for the name of an NNTP server to use.
4232 As opposed to `gnus', this command will not connect to the local server."
4233   (interactive "P")
4234   (let ((gnus-group-use-permanent-levels t))
4235     (gnus (or arg (1- gnus-level-default-subscribed)) t slave))
4236   (make-local-variable 'gnus-group-use-permanent-levels)
4237   (setq gnus-group-use-permanent-levels t))
4238
4239 ;;;###autoload
4240 (defun gnus-slave (&optional arg)
4241   "Read news as a slave."
4242   (interactive "P")
4243   (gnus arg nil 'slave))
4244
4245 ;;;###autoload
4246 (defun gnus-other-frame (&optional arg)
4247   "Pop up a frame to read news."
4248   (interactive "P")
4249   (if (get-buffer gnus-group-buffer)
4250       (let ((pop-up-frames t))
4251         (gnus arg))
4252     (select-frame (make-frame))
4253     (gnus arg)))
4254
4255 ;;;###autoload
4256 (defun gnus (&optional arg dont-connect slave)
4257   "Read network news.
4258 If ARG is non-nil and a positive number, Gnus will use that as the
4259 startup level.  If ARG is non-nil and not a positive number, Gnus will
4260 prompt the user for the name of an NNTP server to use."
4261   (interactive "P")
4262
4263   (if (get-buffer gnus-group-buffer)
4264       (progn
4265         (switch-to-buffer gnus-group-buffer)
4266         (gnus-group-get-new-news))
4267
4268     (gnus-clear-system)
4269     (nnheader-init-server-buffer)
4270     (gnus-read-init-file)
4271     (setq gnus-slave slave)
4272
4273     (gnus-group-setup-buffer)
4274     (let ((buffer-read-only nil))
4275       (erase-buffer)
4276       (if (not gnus-inhibit-startup-message)
4277           (progn
4278             (gnus-group-startup-message)
4279             (sit-for 0))))
4280
4281     (let ((level (and (numberp arg) (> arg 0) arg))
4282           did-connect)
4283       (unwind-protect
4284           (progn
4285             (or dont-connect
4286                 (setq did-connect
4287                       (gnus-start-news-server (and arg (not level))))))
4288         (if (and (not dont-connect)
4289                  (not did-connect))
4290             (gnus-group-quit)
4291           (run-hooks 'gnus-startup-hook)
4292           ;; NNTP server is successfully open.
4293
4294           ;; Find the current startup file name.
4295           (setq gnus-current-startup-file
4296                 (gnus-make-newsrc-file gnus-startup-file))
4297
4298           ;; Read the dribble file.
4299           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4300
4301           ;; Allow using GroupLens predictions.
4302           (when gnus-use-grouplens
4303             (bbb-login)
4304             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4305
4306           (gnus-summary-make-display-table)
4307           ;; Do the actual startup.
4308           (gnus-setup-news nil level dont-connect)
4309           ;; Generate the group buffer.
4310           (gnus-group-list-groups level)
4311           (gnus-group-first-unread-group)
4312           (gnus-configure-windows 'group)
4313           (gnus-group-set-mode-line))))))
4314
4315 (defun gnus-unload ()
4316   "Unload all Gnus features."
4317   (interactive)
4318   (or (boundp 'load-history)
4319       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4320   (let ((history load-history)
4321         feature)
4322     (while history
4323       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4324            (setq feature (cdr (assq 'provide (car history))))
4325            (unload-feature feature 'force))
4326       (setq history (cdr history)))))
4327
4328 (defun gnus-compile ()
4329   "Byte-compile the user-defined format specs."
4330   (interactive)
4331   (let ((entries gnus-format-specs)
4332         entry gnus-tmp-func)
4333     (save-excursion
4334       (gnus-message 7 "Compiling format specs...")
4335
4336       (while entries
4337         (setq entry (pop entries))
4338         (if (eq (car entry) 'version)
4339             (setq gnus-format-specs (delq entry gnus-format-specs))
4340           (when (and (listp (caddr entry))
4341                      (not (eq 'byte-code (caaddr entry))))
4342             (fset 'gnus-tmp-func
4343                   `(lambda () ,(caddr entry)))
4344             (byte-compile 'gnus-tmp-func)
4345             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4346
4347       (push (cons 'version emacs-version) gnus-format-specs)
4348
4349       (gnus-message 7 "Compiling user specs...done"))))
4350
4351 (defun gnus-indent-rigidly (start end arg)
4352   "Indent rigidly using only spaces and no tabs."
4353   (save-excursion
4354     (save-restriction
4355       (narrow-to-region start end)
4356       (indent-rigidly start end arg)
4357       (goto-char (point-min))
4358       (while (search-forward "\t" nil t)
4359         (replace-match "        " t t)))))
4360
4361 (defun gnus-group-startup-message (&optional x y)
4362   "Insert startup message in current buffer."
4363   ;; Insert the message.
4364   (erase-buffer)
4365   (insert
4366    (format "              %s
4367           _    ___ _             _
4368           _ ___ __ ___  __    _ ___
4369           __   _     ___    __  ___
4370               _           ___     _
4371              _  _ __             _
4372              ___   __            _
4373                    __           _
4374                     _      _   _
4375                    _      _    _
4376                       _  _    _
4377                   __  ___
4378                  _   _ _     _
4379                 _   _
4380               _    _
4381              _    _
4382             _
4383           __
4384
4385 "
4386            ""))
4387   ;; And then hack it.
4388   (gnus-indent-rigidly (point-min) (point-max)
4389                        (/ (max (- (window-width) (or x 46)) 0) 2))
4390   (goto-char (point-min))
4391   (forward-line 1)
4392   (let* ((pheight (count-lines (point-min) (point-max)))
4393          (wheight (window-height))
4394          (rest (- wheight pheight)))
4395     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4396   ;; Fontify some.
4397   (goto-char (point-min))
4398   (and (search-forward "Praxis" nil t)
4399        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4400   (goto-char (point-min))
4401   (let* ((mode-string (gnus-group-set-mode-line)))
4402     (setq mode-line-buffer-identification
4403           (list (concat gnus-version (substring (car mode-string) 4))))
4404     (set-buffer-modified-p t)))
4405
4406 (defun gnus-group-setup-buffer ()
4407   (or (get-buffer gnus-group-buffer)
4408       (progn
4409         (switch-to-buffer gnus-group-buffer)
4410         (gnus-add-current-to-buffer-list)
4411         (gnus-group-mode)
4412         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4413
4414 (defun gnus-group-list-groups (&optional level unread lowest)
4415   "List newsgroups with level LEVEL or lower that have unread articles.
4416 Default is all subscribed groups.
4417 If argument UNREAD is non-nil, groups with no unread articles are also
4418 listed."
4419   (interactive (list (if current-prefix-arg
4420                          (prefix-numeric-value current-prefix-arg)
4421                        (or
4422                         (gnus-group-default-level nil t)
4423                         gnus-group-default-list-level
4424                         gnus-level-subscribed))))
4425   (or level
4426       (setq level (car gnus-group-list-mode)
4427             unread (cdr gnus-group-list-mode)))
4428   (setq level (gnus-group-default-level level))
4429   (gnus-group-setup-buffer)             ;May call from out of group buffer
4430   (gnus-update-format-specifications)
4431   (let ((case-fold-search nil)
4432         (props (text-properties-at (gnus-point-at-bol)))
4433         (group (gnus-group-group-name)))
4434     (set-buffer gnus-group-buffer)
4435     (funcall gnus-group-prepare-function level unread lowest)
4436     (if (zerop (buffer-size))
4437         (gnus-message 5 gnus-no-groups-message)
4438       (goto-char (point-max))
4439       (when (or (not gnus-group-goto-next-group-function)
4440                 (not (funcall gnus-group-goto-next-group-function 
4441                               group props)))
4442         (if (not group)
4443             ;; Go to the first group with unread articles.
4444             (gnus-group-search-forward t)
4445           ;; Find the right group to put point on.  If the current group
4446           ;; has disappeared in the new listing, try to find the next
4447           ;; one.        If no next one can be found, just leave point at the
4448           ;; first newsgroup in the buffer.
4449           (if (not (gnus-goto-char
4450                     (text-property-any
4451                      (point-min) (point-max)
4452                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4453               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4454                 (while (and newsrc
4455                             (not (gnus-goto-char
4456                                   (text-property-any
4457                                    (point-min) (point-max) 'gnus-group
4458                                    (gnus-intern-safe
4459                                     (caar newsrc) gnus-active-hashtb)))))
4460                   (setq newsrc (cdr newsrc)))
4461                 (or newsrc (progn (goto-char (point-max))
4462                                   (forward-line -1)))))))
4463       ;; Adjust cursor point.
4464       (gnus-group-position-point))))
4465
4466 (defun gnus-group-list-level (level &optional all)
4467   "List groups on LEVEL.
4468 If ALL (the prefix), also list groups that have no unread articles."
4469   (interactive "nList groups on level: \nP")
4470   (gnus-group-list-groups level all level))
4471
4472 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4473   "List all newsgroups with unread articles of level LEVEL or lower.
4474 If ALL is non-nil, list groups that have no unread articles.
4475 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4476 If REGEXP, only list groups matching REGEXP."
4477   (set-buffer gnus-group-buffer)
4478   (let ((buffer-read-only nil)
4479         (newsrc (cdr gnus-newsrc-alist))
4480         (lowest (or lowest 1))
4481         info clevel unread group params)
4482     (erase-buffer)
4483     (if (< lowest gnus-level-zombie)
4484         ;; List living groups.
4485         (while newsrc
4486           (setq info (car newsrc)
4487                 group (gnus-info-group info)
4488                 params (gnus-info-params info)
4489                 newsrc (cdr newsrc)
4490                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4491           (and unread                   ; This group might be bogus
4492                (or (not regexp)
4493                    (string-match regexp group))
4494                (<= (setq clevel (gnus-info-level info)) level)
4495                (>= clevel lowest)
4496                (or all                  ; We list all groups?
4497                    (if (eq unread t)    ; Unactivated?
4498                        gnus-group-list-inactive-groups ; We list unactivated 
4499                      (> unread 0))      ; We list groups with unread articles
4500                    (and gnus-list-groups-with-ticked-articles
4501                         (cdr (assq 'tick (gnus-info-marks info))))
4502                                         ; And groups with tickeds
4503                    ;; Check for permanent visibility.
4504                    (and gnus-permanently-visible-groups
4505                         (string-match gnus-permanently-visible-groups
4506                                       group))
4507                    (memq 'visible params)
4508                    (cdr (assq 'visible params)))
4509                (gnus-group-insert-group-line
4510                 group (gnus-info-level info)
4511                 (gnus-info-marks info) unread (gnus-info-method info)))))
4512
4513     ;; List dead groups.
4514     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4515          (gnus-group-prepare-flat-list-dead
4516           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4517           gnus-level-zombie ?Z
4518           regexp))
4519     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4520          (gnus-group-prepare-flat-list-dead
4521           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4522           gnus-level-killed ?K regexp))
4523
4524     (gnus-group-set-mode-line)
4525     (setq gnus-group-list-mode (cons level all))
4526     (run-hooks 'gnus-group-prepare-hook)))
4527
4528 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4529   ;; List zombies and killed lists somewhat faster, which was
4530   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4531   ;; this by ignoring the group format specification altogether.
4532   (let (group)
4533     (if regexp
4534         ;; This loop is used when listing groups that match some
4535         ;; regexp.
4536         (while groups
4537           (setq group (pop groups))
4538           (when (string-match regexp group)
4539             (add-text-properties
4540              (point) (prog1 (1+ (point))
4541                        (insert " " mark "     *: " group "\n"))
4542              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4543                    'gnus-unread t
4544                    'gnus-level level))))
4545       ;; This loop is used when listing all groups.
4546       (while groups
4547         (add-text-properties
4548          (point) (prog1 (1+ (point))
4549                    (insert " " mark "     *: "
4550                            (setq group (pop groups)) "\n"))
4551          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4552                'gnus-unread t
4553                'gnus-level level))))))
4554
4555 (defmacro gnus-group-real-name (group)
4556   "Find the real name of a foreign newsgroup."
4557   `(let ((gname ,group))
4558      (if (string-match ":[^:]+$" gname)
4559          (substring gname (1+ (match-beginning 0)))
4560        gname)))
4561
4562 (defsubst gnus-server-add-address (method)
4563   (let ((method-name (symbol-name (car method))))
4564     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4565              (not (assq (intern (concat method-name "-address")) method)))
4566         (append method (list (list (intern (concat method-name "-address"))
4567                                    (nth 1 method))))
4568       method)))
4569
4570 (defsubst gnus-server-get-method (group method)
4571   ;; Input either a server name, and extended server name, or a
4572   ;; select method, and return a select method.
4573   (cond ((stringp method)
4574          (gnus-server-to-method method))
4575         ((equal method gnus-select-method)
4576          gnus-select-method)
4577         ((and (stringp (car method)) group)
4578          (gnus-server-extend-method group method))
4579         ((and method (not group)
4580               (equal (cadr method) ""))
4581          method)
4582         (t
4583          (gnus-server-add-address method))))
4584
4585 (defun gnus-server-to-method (server)
4586   "Map virtual server names to select methods."
4587   (or 
4588    ;; Perhaps this is the native server?
4589    (and (equal server "native") gnus-select-method)
4590    ;; It should be in the server alist.
4591    (cdr (assoc server gnus-server-alist))
4592    ;; If not, we look through all the opened server
4593    ;; to see whether we can find it there.
4594    (let ((opened gnus-opened-servers))
4595      (while (and opened
4596                  (not (equal server (format "%s:%s" (caaar opened)
4597                                             (cadaar opened)))))
4598        (pop opened))
4599      (caar opened))))
4600
4601 (defmacro gnus-method-equal (ss1 ss2)
4602   "Say whether two servers are equal."
4603   `(let ((s1 ,ss1)
4604          (s2 ,ss2))
4605      (or (equal s1 s2)
4606          (and (= (length s1) (length s2))
4607               (progn
4608                 (while (and s1 (member (car s1) s2))
4609                   (setq s1 (cdr s1)))
4610                 (null s1))))))
4611
4612 (defun gnus-server-equal (m1 m2)
4613   "Say whether two methods are equal."
4614   (let ((m1 (cond ((null m1) gnus-select-method)
4615                   ((stringp m1) (gnus-server-to-method m1))
4616                   (t m1)))
4617         (m2 (cond ((null m2) gnus-select-method)
4618                   ((stringp m2) (gnus-server-to-method m2))
4619                   (t m2))))
4620     (gnus-method-equal m1 m2)))
4621
4622 (defun gnus-servers-using-backend (backend)
4623   "Return a list of known servers using BACKEND."
4624   (let ((opened gnus-opened-servers)
4625         out)
4626     (while opened
4627       (when (eq backend (caaar opened))
4628         (push (caar opened) out))
4629       (pop opened))
4630     out))
4631
4632 (defun gnus-group-prefixed-name (group method)
4633   "Return the whole name from GROUP and METHOD."
4634   (and (stringp method) (setq method (gnus-server-to-method method)))
4635   (concat (format "%s" (car method))
4636           (if (and
4637                (or (assoc (format "%s" (car method)) 
4638                           (gnus-methods-using 'address))
4639                    (gnus-server-equal method gnus-message-archive-method))
4640                (nth 1 method)
4641                (not (string= (nth 1 method) "")))
4642               (concat "+" (nth 1 method)))
4643           ":" group))
4644
4645 (defun gnus-group-real-prefix (group)
4646   "Return the prefix of the current group name."
4647   (if (string-match "^[^:]+:" group)
4648       (substring group 0 (match-end 0))
4649     ""))
4650
4651 (defun gnus-group-method (group)
4652   "Return the server or method used for selecting GROUP."
4653   (let ((prefix (gnus-group-real-prefix group)))
4654     (if (equal prefix "")
4655         gnus-select-method
4656       (let ((servers gnus-opened-servers)
4657             (server "")
4658             backend possible found)
4659         (if (string-match "^[^\\+]+\\+" prefix)
4660             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4661                   server (substring prefix (match-end 0) (1- (length prefix))))
4662           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4663         (while servers
4664           (when (eq (caaar servers) backend)
4665             (setq possible (caar servers))
4666             (when (equal (cadaar servers) server)
4667               (setq found (caar servers))))
4668           (pop servers))
4669         (or (car (rassoc found gnus-server-alist))
4670             found
4671             (car (rassoc possible gnus-server-alist))
4672             possible
4673             (list backend server))))))
4674
4675 (defsubst gnus-secondary-method-p (method)
4676   "Return whether METHOD is a secondary select method."
4677   (let ((methods gnus-secondary-select-methods)
4678         (gmethod (gnus-server-get-method nil method)))
4679     (while (and methods
4680                 (not (equal (gnus-server-get-method nil (car methods))
4681                             gmethod)))
4682       (setq methods (cdr methods)))
4683     methods))
4684
4685 (defun gnus-group-foreign-p (group)
4686   "Say whether a group is foreign or not."
4687   (and (not (gnus-group-native-p group))
4688        (not (gnus-group-secondary-p group))))
4689
4690 (defun gnus-group-native-p (group)
4691   "Say whether the group is native or not."
4692   (not (string-match ":" group)))
4693
4694 (defun gnus-group-secondary-p (group)
4695   "Say whether the group is secondary or not."
4696   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4697
4698 (defun gnus-group-get-parameter (group &optional symbol)
4699   "Returns the group parameters for GROUP.
4700 If SYMBOL, return the value of that symbol in the group parameters."
4701   (let ((params (gnus-info-params (gnus-get-info group))))
4702     (if symbol
4703         (gnus-group-parameter-value params symbol)
4704       params)))
4705
4706 (defun gnus-group-parameter-value (params symbol)
4707   "Return the value of SYMBOL in group PARAMS."
4708   (or (car (memq symbol params))        ; It's either a simple symbol
4709       (cdr (assq symbol params))))      ; or a cons.
4710
4711 (defun gnus-group-add-parameter (group param)
4712   "Add parameter PARAM to GROUP."
4713   (let ((info (gnus-get-info group)))
4714     (if (not info)
4715         () ; This is a dead group.  We just ignore it.
4716       ;; Cons the new param to the old one and update.
4717       (gnus-group-set-info (cons param (gnus-info-params info))
4718                            group 'params))))
4719
4720 (defun gnus-group-set-parameter (group name value)
4721   "Set parameter NAME to VALUE in GROUP."
4722   (let ((info (gnus-get-info group)))
4723     (if (not info)
4724         () ; This is a dead group.  We just ignore it.
4725       (let ((old-params (gnus-info-params info))
4726             (new-params (list (cons name value))))
4727         (while old-params
4728           (if (or (not (listp (car old-params)))
4729                   (not (eq (caar old-params) name)))
4730               (setq new-params (append new-params (list (car old-params)))))
4731           (setq old-params (cdr old-params)))
4732         (gnus-group-set-info new-params group 'params)))))
4733
4734 (defun gnus-group-add-score (group &optional score)
4735   "Add SCORE to the GROUP score.
4736 If SCORE is nil, add 1 to the score of GROUP."
4737   (let ((info (gnus-get-info group)))
4738     (when info
4739       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4740
4741 (defun gnus-summary-bubble-group ()
4742   "Increase the score of the current group.
4743 This is a handy function to add to `gnus-summary-exit-hook' to
4744 increase the score of each group you read."
4745   (gnus-group-add-score gnus-newsgroup-name))
4746
4747 (defun gnus-group-set-info (info &optional method-only-group part)
4748   (let* ((entry (gnus-gethash
4749                  (or method-only-group (gnus-info-group info))
4750                  gnus-newsrc-hashtb))
4751          (part-info info)
4752          (info (if method-only-group (nth 2 entry) info))
4753          method)
4754     (when method-only-group
4755       (unless entry
4756         (error "Trying to change non-existent group %s" method-only-group))
4757       ;; We have received parts of the actual group info - either the
4758       ;; select method or the group parameters.  We first check
4759       ;; whether we have to extend the info, and if so, do that.
4760       (let ((len (length info))
4761             (total (if (eq part 'method) 5 6)))
4762         (when (< len total)
4763           (setcdr (nthcdr (1- len) info)
4764                   (make-list (- total len) nil)))
4765         ;; Then we enter the new info.
4766         (setcar (nthcdr (1- total) info) part-info)))
4767     (unless entry
4768       ;; This is a new group, so we just create it.
4769       (save-excursion
4770         (set-buffer gnus-group-buffer)
4771         (setq method (gnus-info-method info))
4772         (when (gnus-server-equal method "native")
4773           (setq method nil))
4774         (if method
4775             ;; It's a foreign group...
4776             (gnus-group-make-group
4777              (gnus-group-real-name (gnus-info-group info))
4778              (if (stringp method) method
4779                (prin1-to-string (car method)))
4780              (and (consp method)
4781                   (nth 1 (gnus-info-method info))))
4782           ;; It's a native group.
4783           (gnus-group-make-group (gnus-info-group info)))
4784         (gnus-message 6 "Note: New group created")
4785         (setq entry
4786               (gnus-gethash (gnus-group-prefixed-name
4787                              (gnus-group-real-name (gnus-info-group info))
4788                              (or (gnus-info-method info) gnus-select-method))
4789                             gnus-newsrc-hashtb))))
4790     ;; Whether it was a new group or not, we now have the entry, so we
4791     ;; can do the update.
4792     (if entry
4793         (progn
4794           (setcar (nthcdr 2 entry) info)
4795           (when (and (not (eq (car entry) t))
4796                      (gnus-active (gnus-info-group info)))
4797             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4798       (error "No such group: %s" (gnus-info-group info)))))
4799
4800 (defun gnus-group-set-method-info (group select-method)
4801   (gnus-group-set-info select-method group 'method))
4802
4803 (defun gnus-group-set-params-info (group params)
4804   (gnus-group-set-info params group 'params))
4805
4806 (defun gnus-group-update-group-line ()
4807   "Update the current line in the group buffer."
4808   (let* ((buffer-read-only nil)
4809          (group (gnus-group-group-name))
4810          (gnus-group-indentation (gnus-group-group-indentation))
4811          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4812     (and entry
4813          (not (gnus-ephemeral-group-p group))
4814          (gnus-dribble-enter
4815           (concat "(gnus-group-set-info '"
4816                   (prin1-to-string (nth 2 entry)) ")")))
4817     (gnus-delete-line)
4818     (gnus-group-insert-group-line-info group)
4819     (forward-line -1)
4820     (gnus-group-position-point)))
4821
4822 (defun gnus-group-insert-group-line-info (group)
4823   "Insert GROUP on the current line."
4824   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4825         active info)
4826     (if entry
4827         (progn
4828           ;; (Un)subscribed group.
4829           (setq info (nth 2 entry))
4830           (gnus-group-insert-group-line
4831            group (gnus-info-level info) (gnus-info-marks info)
4832            (or (car entry) t) (gnus-info-method info)))
4833       ;; This group is dead.
4834       (gnus-group-insert-group-line
4835        group
4836        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4837        nil
4838        (if (setq active (gnus-active group))
4839            (- (1+ (cdr active)) (car active)) 0)
4840        nil))))
4841
4842 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4843                                                     gnus-tmp-marked number
4844                                                     gnus-tmp-method)
4845   "Insert a group line in the group buffer."
4846   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4847          (gnus-tmp-number-total
4848           (if gnus-tmp-active
4849               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4850             0))
4851          (gnus-tmp-number-of-unread
4852           (if (numberp number) (int-to-string (max 0 number))
4853             "*"))
4854          (gnus-tmp-number-of-read
4855           (if (numberp number)
4856               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4857             "*"))
4858          (gnus-tmp-subscribed
4859           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4860                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4861                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4862                 (t ?K)))
4863          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4864          (gnus-tmp-newsgroup-description
4865           (if gnus-description-hashtb
4866               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4867             ""))
4868          (gnus-tmp-moderated
4869           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4870          (gnus-tmp-moderated-string
4871           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4872          (gnus-tmp-method
4873           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4874          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4875          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4876          (gnus-tmp-news-method-string
4877           (if gnus-tmp-method
4878               (format "(%s:%s)" (car gnus-tmp-method)
4879                       (cadr gnus-tmp-method)) ""))
4880          (gnus-tmp-marked-mark
4881           (if (and (numberp number)
4882                    (zerop number)
4883                    (cdr (assq 'tick gnus-tmp-marked)))
4884               ?* ? ))
4885          (gnus-tmp-process-marked
4886           (if (member gnus-tmp-group gnus-group-marked)
4887               gnus-process-mark ? ))
4888          (gnus-tmp-grouplens
4889           (or (and gnus-use-grouplens
4890                    (bbb-grouplens-group-p gnus-tmp-group))
4891               ""))
4892          (buffer-read-only nil)
4893          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4894     (beginning-of-line)
4895     (add-text-properties
4896      (point)
4897      (prog1 (1+ (point))
4898        ;; Insert the text.
4899        (eval gnus-group-line-format-spec))
4900      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4901        gnus-unread ,(if (numberp number)
4902                         (string-to-int gnus-tmp-number-of-unread)
4903                       t)
4904        gnus-marked ,gnus-tmp-marked-mark
4905        gnus-indentation ,gnus-group-indentation
4906        gnus-level ,gnus-tmp-level))
4907     (when (inline (gnus-visual-p 'group-highlight 'highlight))
4908       (forward-line -1)
4909       (run-hooks 'gnus-group-update-hook)
4910       (forward-line))
4911     ;; Allow XEmacs to remove front-sticky text properties.
4912     (gnus-group-remove-excess-properties)))
4913
4914 (defun gnus-group-update-group (group &optional visible-only)
4915   "Update all lines where GROUP appear.
4916 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4917 already."
4918   (save-excursion
4919     (set-buffer gnus-group-buffer)
4920     ;; The buffer may be narrowed.
4921     (save-restriction
4922       (widen)
4923       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4924             (loc (point-min))
4925             found buffer-read-only)
4926         ;; Enter the current status into the dribble buffer.
4927         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4928           (if (and entry (not (gnus-ephemeral-group-p group)))
4929               (gnus-dribble-enter
4930                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4931                        ")"))))
4932         ;; Find all group instances.  If topics are in use, each group
4933         ;; may be listed in more than once.
4934         (while (setq loc (text-property-any
4935                           loc (point-max) 'gnus-group ident))
4936           (setq found t)
4937           (goto-char loc)
4938           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4939             (gnus-delete-line)
4940             (gnus-group-insert-group-line-info group))
4941           (setq loc (1+ loc)))
4942         (unless (or found visible-only)
4943           ;; No such line in the buffer, find out where it's supposed to
4944           ;; go, and insert it there (or at the end of the buffer).
4945           (if gnus-goto-missing-group-function
4946               (funcall gnus-goto-missing-group-function group)
4947             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
4948               (while (and entry (car entry)
4949                           (not
4950                            (gnus-goto-char
4951                             (text-property-any
4952                              (point-min) (point-max)
4953                              'gnus-group (gnus-intern-safe
4954                                           (caar entry) gnus-active-hashtb)))))
4955                 (setq entry (cdr entry)))
4956               (or entry (goto-char (point-max)))))
4957           ;; Finally insert the line.
4958           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4959             (gnus-group-insert-group-line-info group)))
4960         (gnus-group-set-mode-line)))))
4961
4962 (defun gnus-group-set-mode-line ()
4963   (when (memq 'group gnus-updated-mode-lines)
4964     (let* ((gformat (or gnus-group-mode-line-format-spec
4965                         (setq gnus-group-mode-line-format-spec
4966                               (gnus-parse-format
4967                                gnus-group-mode-line-format
4968                                gnus-group-mode-line-format-alist))))
4969            (gnus-tmp-news-server (cadr gnus-select-method))
4970            (gnus-tmp-news-method (car gnus-select-method))
4971            (max-len 60)
4972            gnus-tmp-header                      ;Dummy binding for user-defined formats
4973            ;; Get the resulting string.
4974            (mode-string (eval gformat)))
4975       ;; If the line is too long, we chop it off.
4976       (when (> (length mode-string) max-len)
4977         (setq mode-string (substring mode-string 0 (- max-len 4))))
4978       (prog1
4979           (setq mode-line-buffer-identification (list mode-string))
4980         (set-buffer-modified-p t)))))
4981
4982 (defun gnus-group-group-name ()
4983   "Get the name of the newsgroup on the current line."
4984   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
4985     (and group (symbol-name group))))
4986
4987 (defun gnus-group-group-level ()
4988   "Get the level of the newsgroup on the current line."
4989   (get-text-property (gnus-point-at-bol) 'gnus-level))
4990
4991 (defun gnus-group-group-indentation ()
4992   "Get the indentation of the newsgroup on the current line."
4993   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
4994       (and gnus-group-indentation-function
4995            (funcall gnus-group-indentation-function))
4996       ""))
4997
4998 (defun gnus-group-group-unread ()
4999   "Get the number of unread articles of the newsgroup on the current line."
5000   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5001
5002 (defun gnus-group-search-forward (&optional backward all level first-too)
5003   "Find the next newsgroup with unread articles.
5004 If BACKWARD is non-nil, find the previous newsgroup instead.
5005 If ALL is non-nil, just find any newsgroup.
5006 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5007 group exists.
5008 If FIRST-TOO, the current line is also eligible as a target."
5009   (let ((way (if backward -1 1))
5010         (low gnus-level-killed)
5011         (beg (point))
5012         pos found lev)
5013     (if (and backward (progn (beginning-of-line)) (bobp))
5014         nil
5015       (or first-too (forward-line way))
5016       (while (and
5017               (not (eobp))
5018               (not (setq
5019                     found
5020                     (and (or all
5021                              (and
5022                               (let ((unread
5023                                      (get-text-property (point) 'gnus-unread)))
5024                                 (and (numberp unread) (> unread 0)))
5025                               (setq lev (get-text-property (point)
5026                                                            'gnus-level))
5027                               (<= lev gnus-level-subscribed)))
5028                          (or (not level)
5029                              (and (setq lev (get-text-property (point)
5030                                                                'gnus-level))
5031                                   (or (= lev level)
5032                                       (and (< lev low)
5033                                            (< level lev)
5034                                            (progn
5035                                              (setq low lev)
5036                                              (setq pos (point))
5037                                              nil))))))))
5038               (zerop (forward-line way)))))
5039     (if found
5040         (progn (gnus-group-position-point) t)
5041       (goto-char (or pos beg))
5042       (and pos t))))
5043
5044 ;;; Gnus group mode commands
5045
5046 ;; Group marking.
5047
5048 (defun gnus-group-mark-group (n &optional unmark no-advance)
5049   "Mark the current group."
5050   (interactive "p")
5051   (let ((buffer-read-only nil)
5052         group)
5053     (while
5054         (and (> n 0)
5055              (setq group (gnus-group-group-name))
5056              (progn
5057                (beginning-of-line)
5058                (forward-char
5059                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5060                (delete-char 1)
5061                (if unmark
5062                    (progn
5063                      (insert " ")
5064                      (setq gnus-group-marked (delete group gnus-group-marked)))
5065                  (insert "#")
5066                  (setq gnus-group-marked
5067                        (cons group (delete group gnus-group-marked))))
5068                t)
5069              (or no-advance (zerop (gnus-group-next-group 1))))
5070       (setq n (1- n)))
5071     (gnus-summary-position-point)
5072     n))
5073
5074 (defun gnus-group-unmark-group (n)
5075   "Remove the mark from the current group."
5076   (interactive "p")
5077   (gnus-group-mark-group n 'unmark)
5078   (gnus-group-position-point))
5079
5080 (defun gnus-group-unmark-all-groups ()
5081   "Unmark all groups."
5082   (interactive)
5083   (let ((groups gnus-group-marked))
5084     (save-excursion
5085       (while groups
5086         (gnus-group-remove-mark (pop groups)))))
5087   (gnus-group-position-point))
5088
5089 (defun gnus-group-mark-region (unmark beg end)
5090   "Mark all groups between point and mark.
5091 If UNMARK, remove the mark instead."
5092   (interactive "P\nr")
5093   (let ((num (count-lines beg end)))
5094     (save-excursion
5095       (goto-char beg)
5096       (- num (gnus-group-mark-group num unmark)))))
5097
5098 (defun gnus-group-mark-buffer (&optional unmark)
5099   "Mark all groups in the buffer.
5100 If UNMARK, remove the mark instead."
5101   (interactive "P")
5102   (gnus-group-mark-region unmark (point-min) (point-max)))
5103
5104 (defun gnus-group-mark-regexp (regexp)
5105   "Mark all groups that match some regexp."
5106   (interactive "sMark (regexp): ")
5107   (let ((alist (cdr gnus-newsrc-alist))
5108         group)
5109     (while alist
5110       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5111         (gnus-group-set-mark group))))
5112   (gnus-group-position-point))
5113
5114 (defun gnus-group-remove-mark (group)
5115   "Remove the process mark from GROUP and move point there.
5116 Return nil if the group isn't displayed."
5117   (if (gnus-group-goto-group group)
5118       (save-excursion
5119         (gnus-group-mark-group 1 'unmark t)
5120         t)
5121     (setq gnus-group-marked
5122           (delete group gnus-group-marked))
5123     nil))
5124
5125 (defun gnus-group-set-mark (group)
5126   "Set the process mark on GROUP."
5127   (if (gnus-group-goto-group group) 
5128       (save-excursion
5129         (gnus-group-mark-group 1 nil t))
5130     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5131
5132 (defun gnus-group-universal-argument (arg &optional groups func)
5133   "Perform any command on all groups accoring to the process/prefix convention."
5134   (interactive "P")
5135   (let ((groups (or groups (gnus-group-process-prefix arg)))
5136         group func)
5137     (if (eq (setq func (or func
5138                            (key-binding
5139                             (read-key-sequence
5140                              (substitute-command-keys
5141                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5142             'undefined)
5143         (gnus-error 1 "Undefined key")
5144       (while groups
5145         (gnus-group-remove-mark (setq group (pop groups)))
5146         (command-execute func))))
5147   (gnus-group-position-point))
5148
5149 (defun gnus-group-process-prefix (n)
5150   "Return a list of groups to work on.
5151 Take into consideration N (the prefix) and the list of marked groups."
5152   (cond
5153    (n
5154     (setq n (prefix-numeric-value n))
5155     ;; There is a prefix, so we return a list of the N next
5156     ;; groups.
5157     (let ((way (if (< n 0) -1 1))
5158           (n (abs n))
5159           group groups)
5160       (save-excursion
5161         (while (and (> n 0)
5162                     (setq group (gnus-group-group-name)))
5163           (setq groups (cons group groups))
5164           (setq n (1- n))
5165           (gnus-group-next-group way)))
5166       (nreverse groups)))
5167    ((and (boundp 'transient-mark-mode)
5168          transient-mark-mode
5169          mark-active)
5170     ;; Work on the region between point and mark.
5171     (let ((max (max (point) (mark)))
5172           groups)
5173       (save-excursion
5174         (goto-char (min (point) (mark)))
5175         (while
5176             (and
5177              (push (gnus-group-group-name) groups)
5178              (zerop (gnus-group-next-group 1))
5179              (< (point) max)))
5180         (nreverse groups))))
5181    (gnus-group-marked
5182     ;; No prefix, but a list of marked articles.
5183     (reverse gnus-group-marked))
5184    (t
5185     ;; Neither marked articles or a prefix, so we return the
5186     ;; current group.
5187     (let ((group (gnus-group-group-name)))
5188       (and group (list group))))))
5189
5190 ;; Selecting groups.
5191
5192 (defun gnus-group-read-group (&optional all no-article group)
5193   "Read news in this newsgroup.
5194 If the prefix argument ALL is non-nil, already read articles become
5195 readable.  IF ALL is a number, fetch this number of articles.  If the
5196 optional argument NO-ARTICLE is non-nil, no article will be
5197 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5198 group."
5199   (interactive "P")
5200   (let ((group (or group (gnus-group-group-name)))
5201         number active marked entry)
5202     (or group (error "No group on current line"))
5203     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5204                                             group gnus-newsrc-hashtb)))))
5205     ;; This group might be a dead group.  In that case we have to get
5206     ;; the number of unread articles from `gnus-active-hashtb'.
5207     (setq number
5208           (cond ((numberp all) all)
5209                 (entry (car entry))
5210                 ((setq active (gnus-active group))
5211                  (- (1+ (cdr active)) (car active)))))
5212     (gnus-summary-read-group
5213      group (or all (and (numberp number)
5214                         (zerop (+ number (length (cdr (assq 'tick marked)))
5215                                   (length (cdr (assq 'dormant marked)))))))
5216      no-article)))
5217
5218 (defun gnus-group-select-group (&optional all)
5219   "Select this newsgroup.
5220 No article is selected automatically.
5221 If ALL is non-nil, already read articles become readable.
5222 If ALL is a number, fetch this number of articles."
5223   (interactive "P")
5224   (gnus-group-read-group all t))
5225
5226 (defun gnus-group-quick-select-group (&optional all)
5227   "Select the current group \"quickly\".
5228 This means that no highlighting or scoring will be performed."
5229   (interactive "P")
5230   (let (gnus-visual
5231         gnus-score-find-score-files-function
5232         gnus-apply-kill-hook
5233         gnus-summary-expunge-below)
5234     (gnus-group-read-group all t)))
5235
5236 (defun gnus-group-visible-select-group (&optional all)
5237   "Select the current group without hiding any articles."
5238   (interactive "P")
5239   (let ((gnus-inhibit-limiting t))
5240     (gnus-group-read-group all t)))
5241
5242 ;;;###autoload
5243 (defun gnus-fetch-group (group)
5244   "Start Gnus if necessary and enter GROUP.
5245 Returns whether the fetching was successful or not."
5246   (interactive "sGroup name: ")
5247   (or (get-buffer gnus-group-buffer)
5248       (gnus))
5249   (gnus-group-read-group nil nil group))
5250
5251 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5252 ;; if selection was successful.
5253 (defun gnus-group-read-ephemeral-group
5254   (group method &optional activate quit-config)
5255   (let ((group (if (gnus-group-foreign-p group) group
5256                  (gnus-group-prefixed-name group method))))
5257     (gnus-sethash
5258      group
5259      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5260                      ((quit-config . ,(if quit-config quit-config
5261                                         (cons (current-buffer) 'summary))))))
5262      gnus-newsrc-hashtb)
5263     (set-buffer gnus-group-buffer)
5264     (or (gnus-check-server method)
5265         (error "Unable to contact server: %s" (gnus-status-message method)))
5266     (if activate (or (gnus-request-group group)
5267                      (error "Couldn't request group")))
5268     (condition-case ()
5269         (gnus-group-read-group t t group)
5270       (error nil)
5271       (quit nil))))
5272
5273 (defun gnus-group-jump-to-group (group)
5274   "Jump to newsgroup GROUP."
5275   (interactive
5276    (list (completing-read
5277           "Group: " gnus-active-hashtb nil
5278           (memq gnus-select-method gnus-have-read-active-file))))
5279
5280   (when (equal group "")
5281     (error "Empty group name"))
5282
5283   (when (string-match "[\000-\032]" group)
5284     (error "Control characters in group: %s" group))
5285
5286   (let ((b (text-property-any
5287             (point-min) (point-max)
5288             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5289     (unless (gnus-ephemeral-group-p group)
5290       (if b
5291           ;; Either go to the line in the group buffer...
5292           (goto-char b)
5293         ;; ... or insert the line.
5294         (or
5295          (gnus-active group)
5296          (gnus-activate-group group)
5297          (error "%s error: %s" group (gnus-status-message group)))
5298
5299         (gnus-group-update-group group)
5300         (goto-char (text-property-any
5301                     (point-min) (point-max)
5302                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5303     ;; Adjust cursor point.
5304     (gnus-group-position-point)))
5305
5306 (defun gnus-group-goto-group (group)
5307   "Goto to newsgroup GROUP."
5308   (when group
5309     (let ((b (text-property-any (point-min) (point-max)
5310                                 'gnus-group (gnus-intern-safe
5311                                              group gnus-active-hashtb))))
5312       (and b (goto-char b)))))
5313
5314 (defun gnus-group-next-group (n)
5315   "Go to next N'th newsgroup.
5316 If N is negative, search backward instead.
5317 Returns the difference between N and the number of skips actually
5318 done."
5319   (interactive "p")
5320   (gnus-group-next-unread-group n t))
5321
5322 (defun gnus-group-next-unread-group (n &optional all level)
5323   "Go to next N'th unread newsgroup.
5324 If N is negative, search backward instead.
5325 If ALL is non-nil, choose any newsgroup, unread or not.
5326 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5327 such group can be found, the next group with a level higher than
5328 LEVEL.
5329 Returns the difference between N and the number of skips actually
5330 made."
5331   (interactive "p")
5332   (let ((backward (< n 0))
5333         (n (abs n)))
5334     (while (and (> n 0)
5335                 (gnus-group-search-forward
5336                  backward (or (not gnus-group-goto-unread) all) level))
5337       (setq n (1- n)))
5338     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5339                                (if level " on this level or higher" "")))
5340     n))
5341
5342 (defun gnus-group-prev-group (n)
5343   "Go to previous N'th newsgroup.
5344 Returns the difference between N and the number of skips actually
5345 done."
5346   (interactive "p")
5347   (gnus-group-next-unread-group (- n) t))
5348
5349 (defun gnus-group-prev-unread-group (n)
5350   "Go to previous N'th unread newsgroup.
5351 Returns the difference between N and the number of skips actually
5352 done."
5353   (interactive "p")
5354   (gnus-group-next-unread-group (- n)))
5355
5356 (defun gnus-group-next-unread-group-same-level (n)
5357   "Go to next N'th unread newsgroup on the same level.
5358 If N is negative, search backward instead.
5359 Returns the difference between N and the number of skips actually
5360 done."
5361   (interactive "p")
5362   (gnus-group-next-unread-group n t (gnus-group-group-level))
5363   (gnus-group-position-point))
5364
5365 (defun gnus-group-prev-unread-group-same-level (n)
5366   "Go to next N'th unread newsgroup on the same level.
5367 Returns the difference between N and the number of skips actually
5368 done."
5369   (interactive "p")
5370   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5371   (gnus-group-position-point))
5372
5373 (defun gnus-group-best-unread-group (&optional exclude-group)
5374   "Go to the group with the highest level.
5375 If EXCLUDE-GROUP, do not go to that group."
5376   (interactive)
5377   (goto-char (point-min))
5378   (let ((best 100000)
5379         unread best-point)
5380     (while (not (eobp))
5381       (setq unread (get-text-property (point) 'gnus-unread))
5382       (if (and (numberp unread) (> unread 0))
5383           (progn
5384             (if (and (get-text-property (point) 'gnus-level)
5385                      (< (get-text-property (point) 'gnus-level) best)
5386                      (or (not exclude-group)
5387                          (not (equal exclude-group (gnus-group-group-name)))))
5388                 (progn
5389                   (setq best (get-text-property (point) 'gnus-level))
5390                   (setq best-point (point))))))
5391       (forward-line 1))
5392     (if best-point (goto-char best-point))
5393     (gnus-summary-position-point)
5394     (and best-point (gnus-group-group-name))))
5395
5396 (defun gnus-group-first-unread-group ()
5397   "Go to the first group with unread articles."
5398   (interactive)
5399   (prog1
5400       (let ((opoint (point))
5401             unread)
5402         (goto-char (point-min))
5403         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5404                 (and (numberp unread)   ; Not a topic.
5405                      (not (zerop unread))) ; Has unread articles.
5406                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5407             (point)                     ; Success.
5408           (goto-char opoint)
5409           nil))                         ; Not success.
5410     (gnus-group-position-point)))
5411
5412 (defun gnus-group-enter-server-mode ()
5413   "Jump to the server buffer."
5414   (interactive)
5415   (gnus-enter-server-buffer))
5416
5417 (defun gnus-group-make-group (name &optional method address)
5418   "Add a new newsgroup.
5419 The user will be prompted for a NAME, for a select METHOD, and an
5420 ADDRESS."
5421   (interactive
5422    (cons
5423     (read-string "Group name: ")
5424     (let ((method
5425            (completing-read
5426             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5427             nil t nil 'gnus-method-history)))
5428       (cond ((assoc method gnus-valid-select-methods)
5429              (list method
5430                    (if (memq 'prompt-address
5431                              (assoc method gnus-valid-select-methods))
5432                        (read-string "Address: ")
5433                      "")))
5434             ((assoc method gnus-server-alist)
5435              (list method))
5436             (t
5437              (list method ""))))))
5438
5439   (save-excursion
5440     (set-buffer gnus-group-buffer)
5441     (let* ((meth (and method (if address (list (intern method) address)
5442                                method)))
5443            (nname (if method (gnus-group-prefixed-name name meth) name))
5444            backend info)
5445       (and (gnus-gethash nname gnus-newsrc-hashtb)
5446            (error "Group %s already exists" nname))
5447       (gnus-group-change-level
5448        (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5449        gnus-level-default-subscribed gnus-level-killed
5450        (and (gnus-group-group-name)
5451             (gnus-gethash (gnus-group-group-name)
5452                           gnus-newsrc-hashtb))
5453        t)
5454       (gnus-set-active nname (cons 1 0))
5455       (or (gnus-ephemeral-group-p name)
5456           (gnus-dribble-enter
5457            (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5458       (gnus-group-insert-group-line-info nname)
5459
5460       (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5461                                                     nil meth))))
5462                    gnus-valid-select-methods)
5463         (require backend))
5464       (gnus-check-server meth)
5465       (and (gnus-check-backend-function 'request-create-group nname)
5466            (gnus-request-create-group nname))
5467       t)))
5468
5469 (defun gnus-group-delete-group (group &optional force)
5470   "Delete the current group.
5471 If FORCE (the prefix) is non-nil, all the articles in the group will
5472 be deleted.  This is \"deleted\" as in \"removed forever from the face
5473 of the Earth\".  There is no undo."
5474   (interactive
5475    (list (gnus-group-group-name)
5476          current-prefix-arg))
5477   (or group (error "No group to rename"))
5478   (or (gnus-check-backend-function 'request-delete-group group)
5479       (error "This backend does not support group deletion"))
5480   (prog1
5481       (if (not (gnus-yes-or-no-p
5482                 (format
5483                  "Do you really want to delete %s%s? "
5484                  group (if force " and all its contents" ""))))
5485           () ; Whew!
5486         (gnus-message 6 "Deleting group %s..." group)
5487         (if (not (gnus-request-delete-group group force))
5488             (gnus-error 3 "Couldn't delete group %s" group)
5489           (gnus-message 6 "Deleting group %s...done" group)
5490           (gnus-group-goto-group group)
5491           (gnus-group-kill-group 1 t)
5492           (gnus-sethash group nil gnus-active-hashtb)
5493           t))
5494     (gnus-group-position-point)))
5495
5496 (defun gnus-group-rename-group (group new-name)
5497   (interactive
5498    (list
5499     (gnus-group-group-name)
5500     (progn
5501       (or (gnus-check-backend-function
5502            'request-rename-group (gnus-group-group-name))
5503           (error "This backend does not support renaming groups"))
5504       (read-string "New group name: "))))
5505
5506   (or (gnus-check-backend-function 'request-rename-group group)
5507       (error "This backend does not support renaming groups"))
5508
5509   (or group (error "No group to rename"))
5510   (and (string-match "^[ \t]*$" new-name)
5511        (error "Not a valid group name"))
5512
5513   ;; We find the proper prefixed name.
5514   (setq new-name
5515         (gnus-group-prefixed-name
5516          (gnus-group-real-name new-name)
5517          (gnus-info-method (gnus-get-info group))))
5518
5519   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5520   (prog1
5521       (if (not (gnus-request-rename-group group new-name))
5522           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5523         ;; We rename the group internally by killing it...
5524         (gnus-group-goto-group group)
5525         (gnus-group-kill-group)
5526         ;; ... changing its name ...
5527         (setcar (cdar gnus-list-of-killed-groups) new-name)
5528         ;; ... and then yanking it.  Magic!
5529         (gnus-group-yank-group)
5530         (gnus-set-active new-name (gnus-active group))
5531         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5532         new-name)
5533     (gnus-group-position-point)))
5534
5535 (defun gnus-group-edit-group (group &optional part)
5536   "Edit the group on the current line."
5537   (interactive (list (gnus-group-group-name)))
5538   (let* ((part (or part 'info))
5539          (done-func `(lambda ()
5540                        "Exit editing mode and update the information."
5541                        (interactive)
5542                        (gnus-group-edit-group-done ',part ,group)))
5543          (winconf (current-window-configuration))
5544          info)
5545     (or group (error "No group on current line"))
5546     (or (setq info (gnus-get-info group))
5547         (error "Killed group; can't be edited"))
5548     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5549     (gnus-configure-windows 'edit-group)
5550     (gnus-add-current-to-buffer-list)
5551     (emacs-lisp-mode)
5552     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5553     (use-local-map (copy-keymap emacs-lisp-mode-map))
5554     (local-set-key "\C-c\C-c" done-func)
5555     (make-local-variable 'gnus-prev-winconf)
5556     (setq gnus-prev-winconf winconf)
5557     (erase-buffer)
5558     (insert
5559      (cond
5560       ((eq part 'method)
5561        ";; Type `C-c C-c' after editing the select method.\n\n")
5562       ((eq part 'params)
5563        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5564       ((eq part 'info)
5565        ";; Type `C-c C-c' after editing the group info.\n\n")))
5566     (insert
5567      (pp-to-string
5568       (cond ((eq part 'method)
5569              (or (gnus-info-method info) "native"))
5570             ((eq part 'params)
5571              (gnus-info-params info))
5572             (t info)))
5573      "\n")))
5574
5575 (defun gnus-group-edit-group-method (group)
5576   "Edit the select method of GROUP."
5577   (interactive (list (gnus-group-group-name)))
5578   (gnus-group-edit-group group 'method))
5579
5580 (defun gnus-group-edit-group-parameters (group)
5581   "Edit the group parameters of GROUP."
5582   (interactive (list (gnus-group-group-name)))
5583   (gnus-group-edit-group group 'params))
5584
5585 (defun gnus-group-edit-group-done (part group)
5586   "Get info from buffer, update variables and jump to the group buffer."
5587   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5588   (goto-char (point-min))
5589   (let* ((form (read (current-buffer)))
5590          (winconf gnus-prev-winconf)
5591          (method (cond ((eq part 'info) (nth 4 form))
5592                        ((eq part 'method) form)
5593                        (t nil)))
5594          (info (cond ((eq part 'info) form)
5595                      ((eq part 'method) (gnus-get-info group))
5596                      (t nil)))
5597          (new-group (if info
5598                       (if (or (not method)
5599                               (gnus-server-equal
5600                                gnus-select-method method))
5601                           (gnus-group-real-name (car info))
5602                         (gnus-group-prefixed-name
5603                          (gnus-group-real-name (car info)) method))
5604                       nil)))
5605     (when (and new-group
5606                (not (equal new-group group)))
5607       (when (gnus-group-goto-group group)
5608         (gnus-group-kill-group 1))
5609       (gnus-activate-group new-group))
5610     ;; Set the info.
5611     (if (and info new-group)
5612         (progn
5613           (setq info (gnus-copy-sequence info))
5614           (setcar info new-group)
5615           (unless (gnus-server-equal method "native")
5616             (unless (nthcdr 3 info)
5617               (nconc info (list nil nil)))
5618             (unless (nthcdr 4 info)
5619               (nconc info (list nil)))
5620             (gnus-info-set-method info method))
5621           (gnus-group-set-info info))
5622       (gnus-group-set-info form (or new-group group) part))
5623     (kill-buffer (current-buffer))
5624     (and winconf (set-window-configuration winconf))
5625     (set-buffer gnus-group-buffer)
5626     (gnus-group-update-group (or new-group group))
5627     (gnus-group-position-point)))
5628
5629 (defun gnus-group-make-help-group ()
5630   "Create the Gnus documentation group."
5631   (interactive)
5632   (let ((path load-path)
5633         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5634         file dir)
5635     (and (gnus-gethash name gnus-newsrc-hashtb)
5636          (error "Documentation group already exists"))
5637     (while path
5638       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5639             file nil)
5640       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5641                 (file-exists-p
5642                  (setq file (concat (file-name-directory
5643                                      (directory-file-name dir))
5644                                     "etc/gnus-tut.txt"))))
5645         (setq path nil)))
5646     (if (not file)
5647         (gnus-message 1 "Couldn't find doc group")
5648       (gnus-group-make-group
5649        (gnus-group-real-name name)
5650        (list 'nndoc "gnus-help"
5651              (list 'nndoc-address file)
5652              (list 'nndoc-article-type 'mbox)))))
5653   (gnus-group-position-point))
5654
5655 (defun gnus-group-make-doc-group (file type)
5656   "Create a group that uses a single file as the source."
5657   (interactive
5658    (list (read-file-name "File name: ")
5659          (and current-prefix-arg 'ask)))
5660   (when (eq type 'ask)
5661     (let ((err "")
5662           char found)
5663       (while (not found)
5664         (message
5665          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5666          err)
5667         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5668                           ((= char ?b) 'babyl)
5669                           ((= char ?d) 'digest)
5670                           ((= char ?f) 'forward)
5671                           ((= char ?a) 'mmfd)
5672                           (t (setq err (format "%c unknown. " char))
5673                              nil))))
5674       (setq type found)))
5675   (let* ((file (expand-file-name file))
5676          (name (gnus-generate-new-group-name
5677                 (gnus-group-prefixed-name
5678                  (file-name-nondirectory file) '(nndoc "")))))
5679     (gnus-group-make-group
5680      (gnus-group-real-name name)
5681      (list 'nndoc (file-name-nondirectory file)
5682            (list 'nndoc-address file)
5683            (list 'nndoc-article-type (or type 'guess))))
5684     (forward-line -1)
5685     (gnus-group-position-point)))
5686
5687 (defun gnus-group-make-archive-group (&optional all)
5688   "Create the (ding) Gnus archive group of the most recent articles.
5689 Given a prefix, create a full group."
5690   (interactive "P")
5691   (let ((group (gnus-group-prefixed-name
5692                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5693     (and (gnus-gethash group gnus-newsrc-hashtb)
5694          (error "Archive group already exists"))
5695     (gnus-group-make-group
5696      (gnus-group-real-name group)
5697      (list 'nndir (if all "hpc" "edu")
5698            (list 'nndir-directory
5699                  (if all gnus-group-archive-directory
5700                    gnus-group-recent-archive-directory)))))
5701   (forward-line -1)
5702   (gnus-group-position-point))
5703
5704 (defun gnus-group-make-directory-group (dir)
5705   "Create an nndir group.
5706 The user will be prompted for a directory.  The contents of this
5707 directory will be used as a newsgroup.  The directory should contain
5708 mail messages or news articles in files that have numeric names."
5709   (interactive
5710    (list (read-file-name "Create group from directory: ")))
5711   (or (file-exists-p dir) (error "No such directory"))
5712   (or (file-directory-p dir) (error "Not a directory"))
5713   (let ((ext "")
5714         (i 0)
5715         group)
5716     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5717       (setq group
5718             (gnus-group-prefixed-name
5719              (concat (file-name-as-directory (directory-file-name dir))
5720                      ext)
5721              '(nndir "")))
5722       (setq ext (format "<%d>" (setq i (1+ i)))))
5723     (gnus-group-make-group
5724      (gnus-group-real-name group)
5725      (list 'nndir group (list 'nndir-directory dir))))
5726   (forward-line -1)
5727   (gnus-group-position-point))
5728
5729 (defun gnus-group-make-kiboze-group (group address scores)
5730   "Create an nnkiboze group.
5731 The user will be prompted for a name, a regexp to match groups, and
5732 score file entries for articles to include in the group."
5733   (interactive
5734    (list
5735     (read-string "nnkiboze group name: ")
5736     (read-string "Source groups (regexp): ")
5737     (let ((headers (mapcar (lambda (group) (list group))
5738                            '("subject" "from" "number" "date" "message-id"
5739                              "references" "chars" "lines" "xref"
5740                              "followup" "all" "body" "head")))
5741           scores header regexp regexps)
5742       (while (not (equal "" (setq header (completing-read
5743                                           "Match on header: " headers nil t))))
5744         (setq regexps nil)
5745         (while (not (equal "" (setq regexp (read-string
5746                                             (format "Match on %s (string): "
5747                                                     header)))))
5748           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5749         (setq scores (cons (cons header regexps) scores)))
5750       scores)))
5751   (gnus-group-make-group group "nnkiboze" address)
5752   (save-excursion
5753     (gnus-set-work-buffer)
5754     (let (emacs-lisp-mode-hook)
5755       (pp scores (current-buffer)))
5756     (write-region (point-min) (point-max)
5757                   (gnus-score-file-name (concat "nnkiboze:" group))))
5758   (forward-line -1)
5759   (gnus-group-position-point))
5760
5761 (defun gnus-group-add-to-virtual (n vgroup)
5762   "Add the current group to a virtual group."
5763   (interactive
5764    (list current-prefix-arg
5765          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5766                           "nnvirtual:")))
5767   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5768       (error "%s is not an nnvirtual group" vgroup))
5769   (let* ((groups (gnus-group-process-prefix n))
5770          (method (gnus-info-method (gnus-get-info vgroup))))
5771     (setcar (cdr method)
5772             (concat
5773              (nth 1 method) "\\|"
5774              (mapconcat
5775               (lambda (s)
5776                 (gnus-group-remove-mark s)
5777                 (concat "\\(^" (regexp-quote s) "$\\)"))
5778               groups "\\|"))))
5779   (gnus-group-position-point))
5780
5781 (defun gnus-group-make-empty-virtual (group)
5782   "Create a new, fresh, empty virtual group."
5783   (interactive "sCreate new, empty virtual group: ")
5784   (let* ((method (list 'nnvirtual "^$"))
5785          (pgroup (gnus-group-prefixed-name group method)))
5786     ;; Check whether it exists already.
5787     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5788          (error "Group %s already exists." pgroup))
5789     ;; Subscribe the new group after the group on the current line.
5790     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5791     (gnus-group-update-group pgroup)
5792     (forward-line -1)
5793     (gnus-group-position-point)))
5794
5795 (defun gnus-group-enter-directory (dir)
5796   "Enter an ephemeral nneething group."
5797   (interactive "DDirectory to read: ")
5798   (let* ((method (list 'nneething dir))
5799          (leaf (gnus-group-prefixed-name
5800                 (file-name-nondirectory (directory-file-name dir))
5801                 method))
5802          (name (gnus-generate-new-group-name leaf)))
5803     (let ((nneething-read-only t))
5804       (or (gnus-group-read-ephemeral-group
5805            name method t
5806            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5807                                       'summary 'group)))
5808           (error "Couldn't enter %s" dir)))))
5809
5810 ;; Group sorting commands
5811 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5812
5813 (defun gnus-group-sort-groups (func &optional reverse)
5814   "Sort the group buffer according to FUNC.
5815 If REVERSE, reverse the sorting order."
5816   (interactive (list gnus-group-sort-function
5817                      current-prefix-arg))
5818   (let ((func (cond 
5819                ((not (listp func)) func)
5820                ((null func) func)
5821                ((= 1 (length func)) (car func))
5822                (t `(lambda (t1 t2)
5823                      ,(gnus-make-sort-function 
5824                        (reverse func)))))))
5825     ;; We peel off the dummy group from the alist.
5826     (when func
5827       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5828         (pop gnus-newsrc-alist))
5829       ;; Do the sorting.
5830       (setq gnus-newsrc-alist
5831             (sort gnus-newsrc-alist func))
5832       (when reverse
5833         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5834       ;; Regenerate the hash table.
5835       (gnus-make-hashtable-from-newsrc-alist)
5836       (gnus-group-list-groups))))
5837
5838 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5839   "Sort the group buffer alphabetically by group name.
5840 If REVERSE, sort in reverse order."
5841   (interactive "P")
5842   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5843
5844 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5845   "Sort the group buffer by number of unread articles.
5846 If REVERSE, sort in reverse order."
5847   (interactive "P")
5848   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5849
5850 (defun gnus-group-sort-groups-by-level (&optional reverse)
5851   "Sort the group buffer by group level.
5852 If REVERSE, sort in reverse order."
5853   (interactive "P")
5854   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5855
5856 (defun gnus-group-sort-groups-by-score (&optional reverse)
5857   "Sort the group buffer by group score.
5858 If REVERSE, sort in reverse order."
5859   (interactive "P")
5860   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5861
5862 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5863   "Sort the group buffer by group rank.
5864 If REVERSE, sort in reverse order."
5865   (interactive "P")
5866   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5867
5868 (defun gnus-group-sort-groups-by-method (&optional reverse)
5869   "Sort the group buffer alphabetically by backend name.
5870 If REVERSE, sort in reverse order."
5871   (interactive "P")
5872   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5873
5874 (defun gnus-group-sort-by-alphabet (info1 info2)
5875   "Sort alphabetically."
5876   (string< (gnus-info-group info1) (gnus-info-group info2)))
5877
5878 (defun gnus-group-sort-by-unread (info1 info2)
5879   "Sort by number of unread articles."
5880   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5881         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5882     (< (or (and (numberp n1) n1) 0)
5883        (or (and (numberp n2) n2) 0))))
5884
5885 (defun gnus-group-sort-by-level (info1 info2)
5886   "Sort by level."
5887   (< (gnus-info-level info1) (gnus-info-level info2)))
5888
5889 (defun gnus-group-sort-by-method (info1 info2)
5890   "Sort alphabetically by backend name."
5891   (string< (symbol-name (car (gnus-find-method-for-group
5892                               (gnus-info-group info1) info1)))
5893            (symbol-name (car (gnus-find-method-for-group
5894                               (gnus-info-group info2) info2)))))
5895
5896 (defun gnus-group-sort-by-score (info1 info2)
5897   "Sort by group score."
5898   (< (gnus-info-score info1) (gnus-info-score info2)))
5899
5900 (defun gnus-group-sort-by-rank (info1 info2)
5901   "Sort by level and score."
5902   (let ((level1 (gnus-info-level info1))
5903         (level2 (gnus-info-level info2)))
5904     (or (< level1 level2)
5905         (and (= level1 level2)
5906              (< (gnus-info-score info1) (gnus-info-score info2))))))
5907
5908 ;; Group catching up.
5909
5910 (defun gnus-group-clear-data (n)
5911   "Clear all marks and read ranges from the current group."
5912   (interactive "P")
5913   (let ((groups (gnus-group-process-prefix n))
5914         group info)
5915     (while (setq group (pop groups))
5916       (setq info (gnus-get-info group))
5917       (gnus-info-set-read info nil)
5918       (when (gnus-info-marks info)
5919         (gnus-info-set-marks info nil))
5920       (gnus-get-unread-articles-in-group info (gnus-active group) t)
5921       (when (gnus-group-goto-group group)
5922         (gnus-group-remove-mark group)
5923         (gnus-group-update-group-line)))))
5924
5925 (defun gnus-group-catchup-current (&optional n all)
5926   "Mark all articles not marked as unread in current newsgroup as read.
5927 If prefix argument N is numeric, the ARG next newsgroups will be
5928 caught up.  If ALL is non-nil, marked articles will also be marked as
5929 read.  Cross references (Xref: header) of articles are ignored.
5930 The difference between N and actual number of newsgroups that were
5931 caught up is returned."
5932   (interactive "P")
5933   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5934                gnus-expert-user
5935                (gnus-y-or-n-p
5936                 (if all
5937                     "Do you really want to mark all articles as read? "
5938                   "Mark all unread articles as read? "))))
5939       n
5940     (let ((groups (gnus-group-process-prefix n))
5941           (ret 0))
5942       (while groups
5943         ;; Virtual groups have to be given special treatment.
5944         (let ((method (gnus-find-method-for-group (car groups))))
5945           (if (eq 'nnvirtual (car method))
5946               (nnvirtual-catchup-group
5947                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5948         (gnus-group-remove-mark (car groups))
5949         (if (prog1
5950                 (gnus-group-goto-group (car groups))
5951               (gnus-group-catchup (car groups) all))
5952             (gnus-group-update-group-line)
5953           (setq ret (1+ ret)))
5954         (setq groups (cdr groups)))
5955       (gnus-group-next-unread-group 1)
5956       ret)))
5957
5958 (defun gnus-group-catchup-current-all (&optional n)
5959   "Mark all articles in current newsgroup as read.
5960 Cross references (Xref: header) of articles are ignored."
5961   (interactive "P")
5962   (gnus-group-catchup-current n 'all))
5963
5964 (defun gnus-group-catchup (group &optional all)
5965   "Mark all articles in GROUP as read.
5966 If ALL is non-nil, all articles are marked as read.
5967 The return value is the number of articles that were marked as read,
5968 or nil if no action could be taken."
5969   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5970          (num (car entry)))
5971     ;; Do the updating only if the newsgroup isn't killed.
5972     (if (not (numberp (car entry)))
5973         (gnus-message 1 "Can't catch up; non-active group")
5974       ;; Do auto-expirable marks if that's required.
5975       (when (gnus-group-auto-expirable-p group)
5976         (gnus-add-marked-articles
5977          group 'expire (gnus-list-of-unread-articles group))
5978         (when all
5979           (let ((marks (nth 3 (nth 2 entry))))
5980             (gnus-add-marked-articles
5981              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
5982             (gnus-add-marked-articles
5983              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
5984       (when entry
5985         (gnus-update-read-articles group nil)
5986         ;; Also nix out the lists of marks and dormants.
5987         (when all
5988           (gnus-add-marked-articles group 'tick nil nil 'force)
5989           (gnus-add-marked-articles group 'dormant nil nil 'force))
5990         (run-hooks 'gnus-group-catchup-group-hook)
5991         num))))
5992
5993 (defun gnus-group-expire-articles (&optional n)
5994   "Expire all expirable articles in the current newsgroup."
5995   (interactive "P")
5996   (let ((groups (gnus-group-process-prefix n))
5997         group)
5998     (unless groups
5999       (error "No groups to expire"))
6000     (while (setq group (pop groups))
6001       (gnus-group-remove-mark group)
6002       (when (gnus-check-backend-function 'request-expire-articles group)
6003         (gnus-message 6 "Expiring articles in %s..." group)
6004         (let* ((info (gnus-get-info group))
6005                (expirable (if (gnus-group-total-expirable-p group)
6006                               (cons nil (gnus-list-of-read-articles group))
6007                             (assq 'expire (gnus-info-marks info))))
6008                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6009           (when expirable
6010             (setcdr
6011              expirable
6012              (gnus-compress-sequence
6013               (if expiry-wait
6014                   ;; We set the expiry variables to the groupp
6015                   ;; parameter. 
6016                   (let ((nnmail-expiry-wait-function nil)
6017                         (nnmail-expiry-wait expiry-wait))
6018                     (gnus-request-expire-articles
6019                      (gnus-uncompress-sequence (cdr expirable)) group))
6020                 ;; Just expire using the normal expiry values.
6021                 (gnus-request-expire-articles
6022                  (gnus-uncompress-sequence (cdr expirable)) group)))))
6023           (gnus-message 6 "Expiring articles in %s...done" group)))
6024       (gnus-group-position-point))))
6025
6026 (defun gnus-group-expire-all-groups ()
6027   "Expire all expirable articles in all newsgroups."
6028   (interactive)
6029   (save-excursion
6030     (gnus-message 5 "Expiring...")
6031     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6032                                      (cdr gnus-newsrc-alist))))
6033       (gnus-group-expire-articles nil)))
6034   (gnus-group-position-point)
6035   (gnus-message 5 "Expiring...done"))
6036
6037 (defun gnus-group-set-current-level (n level)
6038   "Set the level of the next N groups to LEVEL."
6039   (interactive
6040    (list
6041     current-prefix-arg
6042     (string-to-int
6043      (let ((s (read-string
6044                (format "Level (default %s): "
6045                        (or (gnus-group-group-level) 
6046                            gnus-level-default-subscribed)))))
6047        (if (string-match "^\\s-*$" s)
6048            (int-to-string (or (gnus-group-group-level) 
6049                               gnus-level-default-subscribed))
6050          s)))))
6051   (or (and (>= level 1) (<= level gnus-level-killed))
6052       (error "Illegal level: %d" level))
6053   (let ((groups (gnus-group-process-prefix n))
6054         group)
6055     (while (setq group (pop groups))
6056       (gnus-group-remove-mark group)
6057       (gnus-message 6 "Changed level of %s from %d to %d"
6058                     group (or (gnus-group-group-level) gnus-level-killed)
6059                     level)
6060       (gnus-group-change-level
6061        group level (or (gnus-group-group-level) gnus-level-killed))
6062       (gnus-group-update-group-line)))
6063   (gnus-group-position-point))
6064
6065 (defun gnus-group-unsubscribe-current-group (&optional n)
6066   "Toggle subscription of the current group.
6067 If given numerical prefix, toggle the N next groups."
6068   (interactive "P")
6069   (let ((groups (gnus-group-process-prefix n))
6070         group)
6071     (while groups
6072       (setq group (car groups)
6073             groups (cdr groups))
6074       (gnus-group-remove-mark group)
6075       (gnus-group-unsubscribe-group
6076        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6077                  gnus-level-default-unsubscribed
6078                gnus-level-default-subscribed) t)
6079       (gnus-group-update-group-line))
6080     (gnus-group-next-group 1)))
6081
6082 (defun gnus-group-unsubscribe-group (group &optional level silent)
6083   "Toggle subscription to GROUP.
6084 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6085 group line."
6086   (interactive
6087    (list (completing-read
6088           "Group: " gnus-active-hashtb nil
6089           (memq gnus-select-method gnus-have-read-active-file))))
6090   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6091     (cond
6092      ((string-match "^[ \t]$" group)
6093       (error "Empty group name"))
6094      (newsrc
6095       ;; Toggle subscription flag.
6096       (gnus-group-change-level
6097        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6098                                       gnus-level-subscribed)
6099                                   (1+ gnus-level-subscribed)
6100                                 gnus-level-default-subscribed)))
6101       (unless silent
6102         (gnus-group-update-group group)))
6103      ((and (stringp group)
6104            (or (not (memq gnus-select-method gnus-have-read-active-file))
6105                (gnus-active group)))
6106       ;; Add new newsgroup.
6107       (gnus-group-change-level
6108        group
6109        (if level level gnus-level-default-subscribed)
6110        (or (and (member group gnus-zombie-list)
6111                 gnus-level-zombie)
6112            gnus-level-killed)
6113        (and (gnus-group-group-name)
6114             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6115       (unless silent
6116         (gnus-group-update-group group)))
6117      (t (error "No such newsgroup: %s" group)))
6118     (gnus-group-position-point)))
6119
6120 (defun gnus-group-transpose-groups (n)
6121   "Move the current newsgroup up N places.
6122 If given a negative prefix, move down instead.  The difference between
6123 N and the number of steps taken is returned."
6124   (interactive "p")
6125   (or (gnus-group-group-name)
6126       (error "No group on current line"))
6127   (gnus-group-kill-group 1)
6128   (prog1
6129       (forward-line (- n))
6130     (gnus-group-yank-group)
6131     (gnus-group-position-point)))
6132
6133 (defun gnus-group-kill-all-zombies ()
6134   "Kill all zombie newsgroups."
6135   (interactive)
6136   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6137   (setq gnus-zombie-list nil)
6138   (gnus-group-list-groups))
6139
6140 (defun gnus-group-kill-region (begin end)
6141   "Kill newsgroups in current region (excluding current point).
6142 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6143   (interactive "r")
6144   (let ((lines
6145          ;; Count lines.
6146          (save-excursion
6147            (count-lines
6148             (progn
6149               (goto-char begin)
6150               (beginning-of-line)
6151               (point))
6152             (progn
6153               (goto-char end)
6154               (beginning-of-line)
6155               (point))))))
6156     (goto-char begin)
6157     (beginning-of-line)                 ;Important when LINES < 1
6158     (gnus-group-kill-group lines)))
6159
6160 (defun gnus-group-kill-group (&optional n discard)
6161   "Kill the next N groups.
6162 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6163 However, only groups that were alive can be yanked; already killed
6164 groups or zombie groups can't be yanked.
6165 The return value is the name of the group that was killed, or a list
6166 of groups killed."
6167   (interactive "P")
6168   (let ((buffer-read-only nil)
6169         (groups (gnus-group-process-prefix n))
6170         group entry level out)
6171     (if (< (length groups) 10)
6172         ;; This is faster when there are few groups.
6173         (while groups
6174           (push (setq group (pop groups)) out)
6175           (gnus-group-remove-mark group)
6176           (setq level (gnus-group-group-level))
6177           (gnus-delete-line)
6178           (when (and (not discard)
6179                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6180             (push (cons (car entry) (nth 2 entry))
6181                   gnus-list-of-killed-groups))
6182           (gnus-group-change-level
6183            (if entry entry group) gnus-level-killed (if entry nil level)))
6184       ;; If there are lots and lots of groups to be killed, we use
6185       ;; this thing instead.
6186       (let (entry)
6187         (setq groups (nreverse groups))
6188         (while groups
6189           (gnus-group-remove-mark (setq group (pop groups)))
6190           (gnus-delete-line)
6191           (cond
6192            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6193             (push (cons (car entry) (nth 2 entry))
6194                   gnus-list-of-killed-groups)
6195             (setcdr (cdr entry) (cdddr entry)))
6196            ((member group gnus-zombie-list)
6197             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6198         (gnus-make-hashtable-from-newsrc-alist)))
6199
6200     (gnus-group-position-point)
6201     (if (< (length out) 2) (car out) (nreverse out))))
6202
6203 (defun gnus-group-yank-group (&optional arg)
6204   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6205 inserting it before the current newsgroup.  The numeric ARG specifies
6206 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6207 is returned, or (if several groups are yanked) a list of yanked groups
6208 is returned."
6209   (interactive "p")
6210   (setq arg (or arg 1))
6211   (let (info group prev out)
6212     (while (>= (decf arg) 0)
6213       (if (not (setq info (pop gnus-list-of-killed-groups)))
6214           (error "No more newsgroups to yank"))
6215       (push (setq group (nth 1 info)) out)
6216       ;; Find which newsgroup to insert this one before - search
6217       ;; backward until something suitable is found.  If there are no
6218       ;; other newsgroups in this buffer, just make this newsgroup the
6219       ;; first newsgroup.
6220       (setq prev (gnus-group-group-name))
6221       (gnus-group-change-level
6222        info (gnus-info-level (cdr info)) gnus-level-killed
6223        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6224        t)
6225       (gnus-group-insert-group-line-info group))
6226     (forward-line -1)
6227     (gnus-group-position-point)
6228     (if (< (length out) 2) (car out) (nreverse out))))
6229
6230 (defun gnus-group-kill-level (level)
6231   "Kill all groups that is on a certain LEVEL."
6232   (interactive "nKill all groups on level: ")
6233   (cond
6234    ((= level gnus-level-zombie)
6235     (setq gnus-killed-list
6236           (nconc gnus-zombie-list gnus-killed-list))
6237     (setq gnus-zombie-list nil))
6238    ((and (< level gnus-level-zombie)
6239          (> level 0)
6240          (or gnus-expert-user
6241              (gnus-yes-or-no-p
6242               (format
6243                "Do you really want to kill all groups on level %d? "
6244                level))))
6245     (let* ((prev gnus-newsrc-alist)
6246            (alist (cdr prev)))
6247       (while alist
6248         (if (= (gnus-info-level level) level)
6249             (setcdr prev (cdr alist))
6250           (setq prev alist))
6251         (setq alist (cdr alist)))
6252       (gnus-make-hashtable-from-newsrc-alist)
6253       (gnus-group-list-groups)))
6254    (t
6255     (error "Can't kill; illegal level: %d" level))))
6256
6257 (defun gnus-group-list-all-groups (&optional arg)
6258   "List all newsgroups with level ARG or lower.
6259 Default is gnus-level-unsubscribed, which lists all subscribed and most
6260 unsubscribed groups."
6261   (interactive "P")
6262   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6263
6264 ;; Redefine this to list ALL killed groups if prefix arg used.
6265 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6266 (defun gnus-group-list-killed (&optional arg)
6267   "List all killed newsgroups in the group buffer.
6268 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6269 entail asking the server for the groups."
6270   (interactive "P")
6271   ;; Find all possible killed newsgroups if arg.
6272   (when arg
6273     ;; First make sure active file has been read.
6274     (unless gnus-have-read-active-file
6275       (let ((gnus-read-active-file t))
6276         (gnus-read-active-file)))
6277     (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
6278     ;; Go through all newsgroups that are known to Gnus - enlarge kill list
6279     (mapatoms
6280      (lambda (sym)
6281        (let ((groups 0)
6282              (group (symbol-name sym)))
6283          (if (or (null group)
6284                  (gnus-gethash group gnus-killed-hashtb)
6285                  (gnus-gethash group gnus-newsrc-hashtb))
6286              ()
6287            (let ((do-sub (gnus-matches-options-n group)))
6288              (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
6289                  ()
6290                (setq groups (1+ groups))
6291                (setq gnus-killed-list
6292                      (cons group gnus-killed-list))
6293                (gnus-sethash group group gnus-killed-hashtb))))))
6294      gnus-active-hashtb))
6295   (if (not gnus-killed-list)
6296       (gnus-message 6 "No killed groups")
6297     (let (gnus-group-list-mode)
6298       (funcall gnus-group-prepare-function
6299                gnus-level-killed t gnus-level-killed))
6300     (goto-char (point-min)))
6301   (gnus-group-position-point))
6302
6303 (defun gnus-group-list-zombies ()
6304   "List all zombie newsgroups in the group buffer."
6305   (interactive)
6306   (if (not gnus-zombie-list)
6307       (gnus-message 6 "No zombie groups")
6308     (let (gnus-group-list-mode)
6309       (funcall gnus-group-prepare-function
6310                gnus-level-zombie t gnus-level-zombie))
6311     (goto-char (point-min)))
6312   (gnus-group-position-point))
6313
6314 (defun gnus-group-list-active ()
6315   "List all groups that are available from the server(s)."
6316   (interactive)
6317   ;; First we make sure that we have really read the active file.
6318   (unless gnus-have-read-active-file
6319     (let ((gnus-read-active-file t))
6320       (gnus-read-active-file)))
6321   ;; Find all groups and sort them.
6322   (let ((groups
6323          (sort
6324           (let (list)
6325             (mapatoms
6326              (lambda (sym)
6327                (and (symbol-value sym)
6328                     (setq list (cons (symbol-name sym) list))))
6329              gnus-active-hashtb)
6330             list)
6331           'string<))
6332         (buffer-read-only nil))
6333     (erase-buffer)
6334     (while groups
6335       (gnus-group-insert-group-line-info (pop groups)))
6336     (goto-char (point-min))))
6337
6338 (defun gnus-activate-all-groups (level)
6339   "Activate absolutely all groups."
6340   (interactive (list 7))
6341   (let ((gnus-activate-level level)
6342         (gnus-activate-foreign-newsgroups level))
6343     (gnus-group-get-new-news)))
6344
6345 (defun gnus-group-get-new-news (&optional arg)
6346   "Get newly arrived articles.
6347 If ARG is a number, it specifies which levels you are interested in
6348 re-scanning.  If ARG is non-nil and not a number, this will force
6349 \"hard\" re-reading of the active files from all servers."
6350   (interactive "P")
6351   (run-hooks 'gnus-get-new-news-hook)
6352   ;; We might read in new NoCeM messages here.
6353   (when (and gnus-use-nocem 
6354              (null arg))
6355     (gnus-nocem-scan-groups))
6356   ;; If ARG is not a number, then we read the active file.
6357   (when (and arg (not (numberp arg)))
6358     (let ((gnus-read-active-file t))
6359       (gnus-read-active-file))
6360     (setq arg nil))
6361
6362   (setq arg (gnus-group-default-level arg t))
6363   (if (and gnus-read-active-file (not arg))
6364       (progn
6365         (gnus-read-active-file)
6366         (gnus-get-unread-articles arg))
6367     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6368       (gnus-get-unread-articles arg)))
6369   (run-hooks 'gnus-after-getting-new-news-hook)
6370   (gnus-group-list-groups))
6371
6372 (defun gnus-group-get-new-news-this-group (&optional n)
6373   "Check for newly arrived news in the current group (and the N-1 next groups).
6374 The difference between N and the number of newsgroup checked is returned.
6375 If N is negative, this group and the N-1 previous groups will be checked."
6376   (interactive "P")
6377   (let* ((groups (gnus-group-process-prefix n))
6378          (ret (if (numberp n) (- n (length groups)) 0))
6379          (beg (unless n (point)))
6380          group)
6381     (while (setq group (pop groups))
6382       (gnus-group-remove-mark group)
6383       (if (gnus-activate-group group 'scan)
6384           (progn
6385             (gnus-get-unread-articles-in-group
6386              (gnus-get-info group) (gnus-active group) t)
6387             (unless (gnus-virtual-group-p group)
6388               (gnus-close-group group))
6389             (gnus-group-update-group group))
6390         (gnus-error 3 "%s error: %s" group (gnus-status-message group))))
6391     (when beg (goto-char beg))
6392     (when gnus-goto-next-group-when-activating
6393       (gnus-group-next-unread-group 1 t))
6394     (gnus-summary-position-point)
6395     ret))
6396
6397 (defun gnus-group-fetch-faq (group &optional faq-dir)
6398   "Fetch the FAQ for the current group."
6399   (interactive
6400    (list
6401     (gnus-group-real-name (gnus-group-group-name))
6402     (cond (current-prefix-arg
6403            (completing-read
6404             "Faq dir: " (and (listp gnus-group-faq-directory)
6405                              gnus-group-faq-directory))))))
6406   (or faq-dir
6407       (setq faq-dir (if (listp gnus-group-faq-directory)
6408                         (car gnus-group-faq-directory)
6409                       gnus-group-faq-directory)))
6410   (or group (error "No group name given"))
6411   (let ((file (concat (file-name-as-directory faq-dir)
6412                       (gnus-group-real-name group))))
6413     (if (not (file-exists-p file))
6414         (error "No such file: %s" file)
6415       (find-file file))))
6416
6417 (defun gnus-group-describe-group (force &optional group)
6418   "Display a description of the current newsgroup."
6419   (interactive (list current-prefix-arg (gnus-group-group-name)))
6420   (and force (setq gnus-description-hashtb nil))
6421   (let ((method (gnus-find-method-for-group group))
6422         desc)
6423     (or group (error "No group name given"))
6424     (and (or (and gnus-description-hashtb
6425                   ;; We check whether this group's method has been
6426                   ;; queried for a description file.
6427                   (gnus-gethash
6428                    (gnus-group-prefixed-name "" method)
6429                    gnus-description-hashtb))
6430              (setq desc (gnus-group-get-description group))
6431              (gnus-read-descriptions-file method))
6432          (gnus-message 1
6433           (or desc (gnus-gethash group gnus-description-hashtb)
6434               "No description available")))))
6435
6436 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6437 (defun gnus-group-describe-all-groups (&optional force)
6438   "Pop up a buffer with descriptions of all newsgroups."
6439   (interactive "P")
6440   (and force (setq gnus-description-hashtb nil))
6441   (if (not (or gnus-description-hashtb
6442                (gnus-read-all-descriptions-files)))
6443       (error "Couldn't request descriptions file"))
6444   (let ((buffer-read-only nil)
6445         b)
6446     (erase-buffer)
6447     (mapatoms
6448      (lambda (group)
6449        (setq b (point))
6450        (insert (format "      *: %-20s %s\n" (symbol-name group)
6451                        (symbol-value group)))
6452        (add-text-properties
6453         b (1+ b) (list 'gnus-group group
6454                        'gnus-unread t 'gnus-marked nil
6455                        'gnus-level (1+ gnus-level-subscribed))))
6456      gnus-description-hashtb)
6457     (goto-char (point-min))
6458     (gnus-group-position-point)))
6459
6460 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6461 (defun gnus-group-apropos (regexp &optional search-description)
6462   "List all newsgroups that have names that match a regexp."
6463   (interactive "sGnus apropos (regexp): ")
6464   (let ((prev "")
6465         (obuf (current-buffer))
6466         groups des)
6467     ;; Go through all newsgroups that are known to Gnus.
6468     (mapatoms
6469      (lambda (group)
6470        (and (symbol-name group)
6471             (string-match regexp (symbol-name group))
6472             (setq groups (cons (symbol-name group) groups))))
6473      gnus-active-hashtb)
6474     ;; Also go through all descriptions that are known to Gnus.
6475     (when search-description
6476       (mapatoms
6477        (lambda (group)
6478          (and (string-match regexp (symbol-value group))
6479               (gnus-active (symbol-name group))
6480               (setq groups (cons (symbol-name group) groups))))
6481        gnus-description-hashtb))
6482     (if (not groups)
6483         (gnus-message 3 "No groups matched \"%s\"." regexp)
6484       ;; Print out all the groups.
6485       (save-excursion
6486         (pop-to-buffer "*Gnus Help*")
6487         (buffer-disable-undo (current-buffer))
6488         (erase-buffer)
6489         (setq groups (sort groups 'string<))
6490         (while groups
6491           ;; Groups may be entered twice into the list of groups.
6492           (if (not (string= (car groups) prev))
6493               (progn
6494                 (insert (setq prev (car groups)) "\n")
6495                 (if (and gnus-description-hashtb
6496                          (setq des (gnus-gethash (car groups)
6497                                                  gnus-description-hashtb)))
6498                     (insert "  " des "\n"))))
6499           (setq groups (cdr groups)))
6500         (goto-char (point-min))))
6501     (pop-to-buffer obuf)))
6502
6503 (defun gnus-group-description-apropos (regexp)
6504   "List all newsgroups that have names or descriptions that match a regexp."
6505   (interactive "sGnus description apropos (regexp): ")
6506   (if (not (or gnus-description-hashtb
6507                (gnus-read-all-descriptions-files)))
6508       (error "Couldn't request descriptions file"))
6509   (gnus-group-apropos regexp t))
6510
6511 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6512 (defun gnus-group-list-matching (level regexp &optional all lowest)
6513   "List all groups with unread articles that match REGEXP.
6514 If the prefix LEVEL is non-nil, it should be a number that says which
6515 level to cut off listing groups.
6516 If ALL, also list groups with no unread articles.
6517 If LOWEST, don't list groups with level lower than LOWEST."
6518   (interactive "P\nsList newsgroups matching: ")
6519   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6520                            all (or lowest 1) regexp)
6521   (goto-char (point-min))
6522   (gnus-group-position-point))
6523
6524 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6525   "List all groups that match REGEXP.
6526 If the prefix LEVEL is non-nil, it should be a number that says which
6527 level to cut off listing groups.
6528 If LOWEST, don't list groups with level lower than LOWEST."
6529   (interactive "P\nsList newsgroups matching: ")
6530   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6531
6532 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6533 (defun gnus-group-save-newsrc (&optional force)
6534   "Save the Gnus startup files.
6535 If FORCE, force saving whether it is necessary or not."
6536   (interactive "P")
6537   (gnus-save-newsrc-file force))
6538
6539 (defun gnus-group-restart (&optional arg)
6540   "Force Gnus to read the .newsrc file."
6541   (interactive "P")
6542   (when (gnus-yes-or-no-p
6543          (format "Are you sure you want to read %s? "
6544                  gnus-current-startup-file))
6545     (gnus-save-newsrc-file)
6546     (gnus-setup-news 'force)
6547     (gnus-group-list-groups arg)))
6548
6549 (defun gnus-group-read-init-file ()
6550   "Read the Gnus elisp init file."
6551   (interactive)
6552   (gnus-read-init-file))
6553
6554 (defun gnus-group-check-bogus-groups (&optional silent)
6555   "Check bogus newsgroups.
6556 If given a prefix, don't ask for confirmation before removing a bogus
6557 group."
6558   (interactive "P")
6559   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6560   (gnus-group-list-groups))
6561
6562 (defun gnus-group-edit-global-kill (&optional article group)
6563   "Edit the global kill file.
6564 If GROUP, edit that local kill file instead."
6565   (interactive "P")
6566   (setq gnus-current-kill-article article)
6567   (gnus-kill-file-edit-file group)
6568   (gnus-message
6569    6
6570    (substitute-command-keys
6571     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6572             (if group "local" "global")))))
6573
6574 (defun gnus-group-edit-local-kill (article group)
6575   "Edit a local kill file."
6576   (interactive (list nil (gnus-group-group-name)))
6577   (gnus-group-edit-global-kill article group))
6578
6579 (defun gnus-group-force-update ()
6580   "Update `.newsrc' file."
6581   (interactive)
6582   (gnus-save-newsrc-file))
6583
6584 (defun gnus-group-suspend ()
6585   "Suspend the current Gnus session.
6586 In fact, cleanup buffers except for group mode buffer.
6587 The hook gnus-suspend-gnus-hook is called before actually suspending."
6588   (interactive)
6589   (run-hooks 'gnus-suspend-gnus-hook)
6590   ;; Kill Gnus buffers except for group mode buffer.
6591   (let ((group-buf (get-buffer gnus-group-buffer)))
6592     ;; Do this on a separate list in case the user does a ^G before we finish
6593     (let ((gnus-buffer-list
6594            (delq group-buf (delq gnus-dribble-buffer
6595                                  (append gnus-buffer-list nil)))))
6596       (while gnus-buffer-list
6597         (gnus-kill-buffer (car gnus-buffer-list))
6598         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6599     (if group-buf
6600         (progn
6601           (setq gnus-buffer-list (list group-buf))
6602           (bury-buffer group-buf)
6603           (delete-windows-on group-buf t)))))
6604
6605 (defun gnus-group-clear-dribble ()
6606   "Clear all information from the dribble buffer."
6607   (interactive)
6608   (gnus-dribble-clear)
6609   (gnus-message 7 "Cleared dribble buffer"))
6610
6611 (defun gnus-group-exit ()
6612   "Quit reading news after updating .newsrc.eld and .newsrc.
6613 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6614   (interactive)
6615   (when 
6616       (or noninteractive                ;For gnus-batch-kill
6617           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6618           (not gnus-interactive-exit)   ;Without confirmation
6619           gnus-expert-user
6620           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6621     (run-hooks 'gnus-exit-gnus-hook)
6622     ;; Offer to save data from non-quitted summary buffers.
6623     (gnus-offer-save-summaries)
6624     ;; Save the newsrc file(s).
6625     (gnus-save-newsrc-file)
6626     ;; Kill-em-all.
6627     (gnus-close-backends)
6628     ;; Reset everything.
6629     (gnus-clear-system)
6630     ;; Allow the user to do things after cleaning up.
6631     (run-hooks 'gnus-after-exiting-gnus-hook)))
6632
6633 (defun gnus-close-backends ()
6634   ;; Send a close request to all backends that support such a request.
6635   (let ((methods gnus-valid-select-methods)
6636         func)
6637     (while methods
6638       (if (fboundp (setq func (intern (concat (caar methods)
6639                                               "-request-close"))))
6640           (funcall func))
6641       (setq methods (cdr methods)))))
6642
6643 (defun gnus-group-quit ()
6644   "Quit reading news without updating .newsrc.eld or .newsrc.
6645 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6646   (interactive)
6647   (when (or noninteractive              ;For gnus-batch-kill
6648             (zerop (buffer-size))
6649             (not (gnus-server-opened gnus-select-method))
6650             gnus-expert-user
6651             (not gnus-current-startup-file)
6652             (gnus-yes-or-no-p
6653              (format "Quit reading news without saving %s? "
6654                      (file-name-nondirectory gnus-current-startup-file))))
6655     (run-hooks 'gnus-exit-gnus-hook)
6656     (if gnus-use-full-window
6657         (delete-other-windows)
6658       (gnus-remove-some-windows))
6659     (gnus-dribble-save)
6660     (gnus-close-backends)
6661     (gnus-clear-system)
6662     ;; Allow the user to do things after cleaning up.
6663     (run-hooks 'gnus-after-exiting-gnus-hook)))
6664
6665 (defun gnus-offer-save-summaries ()
6666   "Offer to save all active summary buffers."
6667   (save-excursion
6668     (let ((buflist (buffer-list))
6669           buffers bufname)
6670       ;; Go through all buffers and find all summaries.
6671       (while buflist
6672         (and (setq bufname (buffer-name (car buflist)))
6673              (string-match "Summary" bufname)
6674              (save-excursion
6675                (set-buffer bufname)
6676                ;; We check that this is, indeed, a summary buffer.
6677                (and (eq major-mode 'gnus-summary-mode)
6678                     ;; Also make sure this isn't bogus.
6679                     gnus-newsgroup-prepared))
6680              (push bufname buffers))
6681         (setq buflist (cdr buflist)))
6682       ;; Go through all these summary buffers and offer to save them.
6683       (when buffers
6684         (map-y-or-n-p
6685          "Update summary buffer %s? "
6686          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6687          buffers)))))
6688
6689 (defun gnus-group-describe-briefly ()
6690   "Give a one line description of the group mode commands."
6691   (interactive)
6692   (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")))
6693
6694 (defun gnus-group-browse-foreign-server (method)
6695   "Browse a foreign news server.
6696 If called interactively, this function will ask for a select method
6697  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6698 If not, METHOD should be a list where the first element is the method
6699 and the second element is the address."
6700   (interactive
6701    (list (let ((how (completing-read
6702                      "Which backend: "
6703                      (append gnus-valid-select-methods gnus-server-alist)
6704                      nil t (cons "nntp" 0) 'gnus-method-history)))
6705            ;; We either got a backend name or a virtual server name.
6706            ;; If the first, we also need an address.
6707            (if (assoc how gnus-valid-select-methods)
6708                (list (intern how)
6709                      ;; Suggested by mapjph@bath.ac.uk.
6710                      (completing-read
6711                       "Address: "
6712                       (mapcar (lambda (server) (list server))
6713                               gnus-secondary-servers)))
6714              ;; We got a server name, so we find the method.
6715              (gnus-server-to-method how)))))
6716   (gnus-browse-foreign-server method))
6717
6718 \f
6719 ;;;
6720 ;;; Gnus summary mode
6721 ;;;
6722
6723 (defvar gnus-summary-mode-map nil)
6724
6725 (put 'gnus-summary-mode 'mode-class 'special)
6726
6727 (unless gnus-summary-mode-map
6728   (setq gnus-summary-mode-map (make-keymap))
6729   (suppress-keymap gnus-summary-mode-map)
6730
6731   ;; Non-orthogonal keys
6732
6733   (gnus-define-keys gnus-summary-mode-map
6734     " " gnus-summary-next-page
6735     "\177" gnus-summary-prev-page
6736     [delete] gnus-summary-prev-page
6737     "\r" gnus-summary-scroll-up
6738     "n" gnus-summary-next-unread-article
6739     "p" gnus-summary-prev-unread-article
6740     "N" gnus-summary-next-article
6741     "P" gnus-summary-prev-article
6742     "\M-\C-n" gnus-summary-next-same-subject
6743     "\M-\C-p" gnus-summary-prev-same-subject
6744     "\M-n" gnus-summary-next-unread-subject
6745     "\M-p" gnus-summary-prev-unread-subject
6746     "." gnus-summary-first-unread-article
6747     "," gnus-summary-best-unread-article
6748     "\M-s" gnus-summary-search-article-forward
6749     "\M-r" gnus-summary-search-article-backward
6750     "<" gnus-summary-beginning-of-article
6751     ">" gnus-summary-end-of-article
6752     "j" gnus-summary-goto-article
6753     "^" gnus-summary-refer-parent-article
6754     "\M-^" gnus-summary-refer-article
6755     "u" gnus-summary-tick-article-forward
6756     "!" gnus-summary-tick-article-forward
6757     "U" gnus-summary-tick-article-backward
6758     "d" gnus-summary-mark-as-read-forward
6759     "D" gnus-summary-mark-as-read-backward
6760     "E" gnus-summary-mark-as-expirable
6761     "\M-u" gnus-summary-clear-mark-forward
6762     "\M-U" gnus-summary-clear-mark-backward
6763     "k" gnus-summary-kill-same-subject-and-select
6764     "\C-k" gnus-summary-kill-same-subject
6765     "\M-\C-k" gnus-summary-kill-thread
6766     "\M-\C-l" gnus-summary-lower-thread
6767     "e" gnus-summary-edit-article
6768     "#" gnus-summary-mark-as-processable
6769     "\M-#" gnus-summary-unmark-as-processable
6770     "\M-\C-t" gnus-summary-toggle-threads
6771     "\M-\C-s" gnus-summary-show-thread
6772     "\M-\C-h" gnus-summary-hide-thread
6773     "\M-\C-f" gnus-summary-next-thread
6774     "\M-\C-b" gnus-summary-prev-thread
6775     "\M-\C-u" gnus-summary-up-thread
6776     "\M-\C-d" gnus-summary-down-thread
6777     "&" gnus-summary-execute-command
6778     "c" gnus-summary-catchup-and-exit
6779     "\C-w" gnus-summary-mark-region-as-read
6780     "\C-t" gnus-summary-toggle-truncation
6781     "?" gnus-summary-mark-as-dormant
6782     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6783     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6784     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6785     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6786     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6787     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6788     "=" gnus-summary-expand-window
6789     "\C-x\C-s" gnus-summary-reselect-current-group
6790     "\M-g" gnus-summary-rescan-group
6791     "w" gnus-summary-stop-page-breaking
6792     "\C-c\C-r" gnus-summary-caesar-message
6793     "\M-t" gnus-summary-toggle-mime
6794     "f" gnus-summary-followup
6795     "F" gnus-summary-followup-with-original
6796     "C" gnus-summary-cancel-article
6797     "r" gnus-summary-reply
6798     "R" gnus-summary-reply-with-original
6799     "\C-c\C-f" gnus-summary-mail-forward
6800     "o" gnus-summary-save-article
6801     "\C-o" gnus-summary-save-article-mail
6802     "|" gnus-summary-pipe-output
6803     "\M-k" gnus-summary-edit-local-kill
6804     "\M-K" gnus-summary-edit-global-kill
6805     "V" gnus-version
6806     "\C-c\C-d" gnus-summary-describe-group
6807     "q" gnus-summary-exit
6808     "Q" gnus-summary-exit-no-update
6809     "\C-c\C-i" gnus-info-find-node
6810     gnus-mouse-2 gnus-mouse-pick-article
6811     "m" gnus-summary-mail-other-window
6812     "a" gnus-summary-post-news
6813     "x" gnus-summary-limit-to-unread
6814     "s" gnus-summary-isearch-article
6815     "t" gnus-article-hide-headers
6816     "g" gnus-summary-show-article
6817     "l" gnus-summary-goto-last-article
6818     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6819     "\C-d" gnus-summary-enter-digest-group
6820     "\C-c\C-b" gnus-bug
6821     "*" gnus-cache-enter-article
6822     "\M-*" gnus-cache-remove-article
6823     "\M-&" gnus-summary-universal-argument
6824     "\C-l" gnus-recenter
6825     "I" gnus-summary-increase-score
6826     "L" gnus-summary-lower-score
6827
6828     "V" gnus-summary-score-map
6829     "X" gnus-uu-extract-map
6830     "S" gnus-summary-send-map)
6831
6832   ;; Sort of orthogonal keymap
6833   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6834     "t" gnus-summary-tick-article-forward
6835     "!" gnus-summary-tick-article-forward
6836     "d" gnus-summary-mark-as-read-forward
6837     "r" gnus-summary-mark-as-read-forward
6838     "c" gnus-summary-clear-mark-forward
6839     " " gnus-summary-clear-mark-forward
6840     "e" gnus-summary-mark-as-expirable
6841     "x" gnus-summary-mark-as-expirable
6842     "?" gnus-summary-mark-as-dormant
6843     "b" gnus-summary-set-bookmark
6844     "B" gnus-summary-remove-bookmark
6845     "#" gnus-summary-mark-as-processable
6846     "\M-#" gnus-summary-unmark-as-processable
6847     "S" gnus-summary-limit-include-expunged
6848     "C" gnus-summary-catchup
6849     "H" gnus-summary-catchup-to-here
6850     "\C-c" gnus-summary-catchup-all
6851     "k" gnus-summary-kill-same-subject-and-select
6852     "K" gnus-summary-kill-same-subject
6853     "P" gnus-uu-mark-map)
6854
6855   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6856     "c" gnus-summary-clear-above
6857     "u" gnus-summary-tick-above
6858     "m" gnus-summary-mark-above
6859     "k" gnus-summary-kill-below)
6860
6861   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6862     "/" gnus-summary-limit-to-subject
6863     "n" gnus-summary-limit-to-articles
6864     "w" gnus-summary-pop-limit
6865     "s" gnus-summary-limit-to-subject
6866     "a" gnus-summary-limit-to-author
6867     "u" gnus-summary-limit-to-unread
6868     "m" gnus-summary-limit-to-marks
6869     "v" gnus-summary-limit-to-score
6870     "D" gnus-summary-limit-include-dormant
6871     "d" gnus-summary-limit-exclude-dormant
6872     ;;  "t" gnus-summary-limit-exclude-thread
6873     "E" gnus-summary-limit-include-expunged
6874     "c" gnus-summary-limit-exclude-childless-dormant
6875     "C" gnus-summary-limit-mark-excluded-as-read)
6876
6877   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6878     "n" gnus-summary-next-unread-article
6879     "p" gnus-summary-prev-unread-article
6880     "N" gnus-summary-next-article
6881     "P" gnus-summary-prev-article
6882     "\C-n" gnus-summary-next-same-subject
6883     "\C-p" gnus-summary-prev-same-subject
6884     "\M-n" gnus-summary-next-unread-subject
6885     "\M-p" gnus-summary-prev-unread-subject
6886     "f" gnus-summary-first-unread-article
6887     "b" gnus-summary-best-unread-article
6888     "j" gnus-summary-goto-article
6889     "g" gnus-summary-goto-subject
6890     "l" gnus-summary-goto-last-article
6891     "p" gnus-summary-pop-article)
6892
6893   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6894     "k" gnus-summary-kill-thread
6895     "l" gnus-summary-lower-thread
6896     "i" gnus-summary-raise-thread
6897     "T" gnus-summary-toggle-threads
6898     "t" gnus-summary-rethread-current
6899     "^" gnus-summary-reparent-thread
6900     "s" gnus-summary-show-thread
6901     "S" gnus-summary-show-all-threads
6902     "h" gnus-summary-hide-thread
6903     "H" gnus-summary-hide-all-threads
6904     "n" gnus-summary-next-thread
6905     "p" gnus-summary-prev-thread
6906     "u" gnus-summary-up-thread
6907     "o" gnus-summary-top-thread
6908     "d" gnus-summary-down-thread
6909     "#" gnus-uu-mark-thread
6910     "\M-#" gnus-uu-unmark-thread)
6911
6912   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6913     "c" gnus-summary-catchup-and-exit
6914     "C" gnus-summary-catchup-all-and-exit
6915     "E" gnus-summary-exit-no-update
6916     "Q" gnus-summary-exit
6917     "Z" gnus-summary-exit
6918     "n" gnus-summary-catchup-and-goto-next-group
6919     "R" gnus-summary-reselect-current-group
6920     "G" gnus-summary-rescan-group
6921     "N" gnus-summary-next-group
6922     "P" gnus-summary-prev-group)
6923
6924   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
6925     " " gnus-summary-next-page
6926     "n" gnus-summary-next-page
6927     "\177" gnus-summary-prev-page
6928     [delete] gnus-summary-prev-page
6929     "p" gnus-summary-prev-page
6930     "\r" gnus-summary-scroll-up
6931     "<" gnus-summary-beginning-of-article
6932     ">" gnus-summary-end-of-article
6933     "b" gnus-summary-beginning-of-article
6934     "e" gnus-summary-end-of-article
6935     "^" gnus-summary-refer-parent-article
6936     "r" gnus-summary-refer-parent-article
6937     "R" gnus-summary-refer-references
6938     "g" gnus-summary-show-article
6939     "s" gnus-summary-isearch-article)
6940
6941   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
6942     "b" gnus-article-add-buttons
6943     "B" gnus-article-add-buttons-to-head
6944     "o" gnus-article-treat-overstrike
6945     ;;  "w" gnus-article-word-wrap
6946     "w" gnus-article-fill-cited-article
6947     "c" gnus-article-remove-cr
6948     "L" gnus-article-remove-trailing-blank-lines
6949     "q" gnus-article-de-quoted-unreadable
6950     "f" gnus-article-display-x-face
6951     "l" gnus-summary-stop-page-breaking
6952     "r" gnus-summary-caesar-message
6953     "t" gnus-article-hide-headers
6954     "v" gnus-summary-verbose-headers
6955     "m" gnus-summary-toggle-mime)
6956
6957   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6958     "a" gnus-article-hide
6959     "h" gnus-article-hide-headers
6960     "b" gnus-article-hide-boring-headers
6961     "s" gnus-article-hide-signature
6962     "c" gnus-article-hide-citation
6963     "p" gnus-article-hide-pgp
6964     "\C-c" gnus-article-hide-citation-maybe)
6965
6966   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
6967     "a" gnus-article-highlight
6968     "h" gnus-article-highlight-headers
6969     "c" gnus-article-highlight-citation
6970     "s" gnus-article-highlight-signature)
6971
6972   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
6973     "z" gnus-article-date-ut
6974     "u" gnus-article-date-ut
6975     "l" gnus-article-date-local
6976     "e" gnus-article-date-lapsed
6977     "o" gnus-article-date-original)
6978
6979   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
6980     "v" gnus-version
6981     "f" gnus-summary-fetch-faq
6982     "d" gnus-summary-describe-group
6983     "h" gnus-summary-describe-briefly
6984     "i" gnus-info-find-node)
6985
6986   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
6987     "e" gnus-summary-expire-articles
6988     "\M-\C-e" gnus-summary-expire-articles-now
6989     "\177" gnus-summary-delete-article
6990     [delete] gnus-summary-delete-article
6991     "m" gnus-summary-move-article
6992     "r" gnus-summary-respool-article
6993     "w" gnus-summary-edit-article
6994     "c" gnus-summary-copy-article
6995     "B" gnus-summary-crosspost-article
6996     "q" gnus-summary-respool-query
6997     "i" gnus-summary-import-article)
6998
6999   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7000     "o" gnus-summary-save-article
7001     "m" gnus-summary-save-article-mail
7002     "r" gnus-summary-save-article-rmail
7003     "f" gnus-summary-save-article-file
7004     "b" gnus-summary-save-article-body-file
7005     "h" gnus-summary-save-article-folder
7006     "v" gnus-summary-save-article-vm
7007     "p" gnus-summary-pipe-output
7008     "s" gnus-soup-add-article)
7009   )
7010
7011 \f
7012
7013 (defun gnus-summary-mode (&optional group)
7014   "Major mode for reading articles.
7015
7016 All normal editing commands are switched off.
7017 \\<gnus-summary-mode-map>
7018 Each line in this buffer represents one article.  To read an
7019 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7020 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7021 respectively.
7022
7023 You can also post articles and send mail from this buffer.  To
7024 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7025 of an article, type `\\[gnus-summary-reply]'.
7026
7027 There are approx. one gazillion commands you can execute in this
7028 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7029
7030 The following commands are available:
7031
7032 \\{gnus-summary-mode-map}"
7033   (interactive)
7034   (when (and menu-bar-mode
7035              (gnus-visual-p 'summary-menu 'menu))
7036     (gnus-summary-make-menu-bar))
7037   (kill-all-local-variables)
7038   (let ((locals gnus-summary-local-variables))
7039     (while locals
7040       (if (consp (car locals))
7041           (progn
7042             (make-local-variable (caar locals))
7043             (set (caar locals) (eval (cdar locals))))
7044         (make-local-variable (car locals))
7045         (set (car locals) nil))
7046       (setq locals (cdr locals))))
7047   (gnus-make-thread-indent-array)
7048   (gnus-simplify-mode-line)
7049   (setq major-mode 'gnus-summary-mode)
7050   (setq mode-name "Summary")
7051   (make-local-variable 'minor-mode-alist)
7052   (use-local-map gnus-summary-mode-map)
7053   (buffer-disable-undo (current-buffer))
7054   (setq buffer-read-only t)             ;Disable modification
7055   (setq truncate-lines t)
7056   (setq selective-display t)
7057   (setq selective-display-ellipses t)   ;Display `...'
7058   (setq buffer-display-table gnus-summary-display-table)
7059   (setq gnus-newsgroup-name group)
7060   (make-local-variable 'gnus-summary-line-format)
7061   (make-local-variable 'gnus-summary-line-format-spec)
7062   (run-hooks 'gnus-summary-mode-hook))
7063
7064 (defun gnus-summary-make-display-table ()
7065   ;; Change the display table.  Odd characters have a tendency to mess
7066   ;; up nicely formatted displays - we make all possible glyphs
7067   ;; display only a single character.
7068
7069   ;; We start from the standard display table, if any.
7070   (setq gnus-summary-display-table
7071         (or (copy-sequence standard-display-table)
7072             (make-display-table)))
7073   ;; Nix out all the control chars...
7074   (let ((i 32))
7075     (while (>= (setq i (1- i)) 0)
7076       (aset gnus-summary-display-table i [??])))
7077   ;; ... but not newline and cr, of course. (cr is necessary for the
7078   ;; selective display).
7079   (aset gnus-summary-display-table ?\n nil)
7080   (aset gnus-summary-display-table ?\r nil)
7081   ;; We nix out any glyphs over 126 that are not set already.
7082   (let ((i 256))
7083     (while (>= (setq i (1- i)) 127)
7084       ;; Only modify if the entry is nil.
7085       (or (aref gnus-summary-display-table i)
7086           (aset gnus-summary-display-table i [??])))))
7087
7088 (defun gnus-summary-clear-local-variables ()
7089   (let ((locals gnus-summary-local-variables))
7090     (while locals
7091       (if (consp (car locals))
7092           (and (vectorp (caar locals))
7093                (set (caar locals) nil))
7094         (and (vectorp (car locals))
7095              (set (car locals) nil)))
7096       (setq locals (cdr locals)))))
7097
7098 ;; Summary data functions.
7099
7100 (defmacro gnus-data-number (data)
7101   `(car ,data))
7102
7103 (defmacro gnus-data-set-number (data number)
7104   `(setcar ,data ,number))
7105
7106 (defmacro gnus-data-mark (data)
7107   `(nth 1 ,data))
7108
7109 (defmacro gnus-data-set-mark (data mark)
7110   `(setcar (nthcdr 1 ,data) ,mark))
7111
7112 (defmacro gnus-data-pos (data)
7113   `(nth 2 ,data))
7114
7115 (defmacro gnus-data-set-pos (data pos)
7116   `(setcar (nthcdr 2 ,data) ,pos))
7117
7118 (defmacro gnus-data-header (data)
7119   `(nth 3 ,data))
7120
7121 (defmacro gnus-data-level (data)
7122   `(nth 4 ,data))
7123
7124 (defmacro gnus-data-unread-p (data)
7125   `(= (nth 1 ,data) gnus-unread-mark))
7126
7127 (defmacro gnus-data-pseudo-p (data)
7128   `(consp (nth 3 ,data)))
7129
7130 (defmacro gnus-data-find (number)
7131   `(assq ,number gnus-newsgroup-data))
7132
7133 (defmacro gnus-data-find-list (number &optional data)
7134   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7135      (memq (assq ,number bdata)
7136            bdata)))
7137
7138 (defmacro gnus-data-make (number mark pos header level)
7139   `(list ,number ,mark ,pos ,header ,level))
7140
7141 (defun gnus-data-enter (after-article number mark pos header level offset)
7142   (let ((data (gnus-data-find-list after-article)))
7143     (or data (error "No such article: %d" after-article))
7144     (setcdr data (cons (gnus-data-make number mark pos header level)
7145                        (cdr data)))
7146     (setq gnus-newsgroup-data-reverse nil)
7147     (gnus-data-update-list (cddr data) offset)))
7148
7149 (defun gnus-data-enter-list (after-article list &optional offset)
7150   (when list
7151     (let ((data (and after-article (gnus-data-find-list after-article)))
7152           (ilist list))
7153       (or data (not after-article) (error "No such article: %d" after-article))
7154       ;; Find the last element in the list to be spliced into the main
7155       ;; list.
7156       (while (cdr list)
7157         (setq list (cdr list)))
7158       (if (not data)
7159           (progn
7160             (setcdr list gnus-newsgroup-data)
7161             (setq gnus-newsgroup-data ilist)
7162             (and offset (gnus-data-update-list (cdr list) offset)))
7163         (setcdr list (cdr data))
7164         (setcdr data ilist)
7165         (and offset (gnus-data-update-list (cdr data) offset)))
7166       (setq gnus-newsgroup-data-reverse nil))))
7167
7168 (defun gnus-data-remove (article &optional offset)
7169   (let ((data gnus-newsgroup-data))
7170     (if (= (gnus-data-number (car data)) article)
7171         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7172               gnus-newsgroup-data-reverse nil)
7173       (while (cdr data)
7174         (and (= (gnus-data-number (cadr data)) article)
7175              (progn
7176                (setcdr data (cddr data))
7177                (and offset (gnus-data-update-list (cdr data) offset))
7178                (setq data nil
7179                      gnus-newsgroup-data-reverse nil)))
7180         (setq data (cdr data))))))
7181
7182 (defmacro gnus-data-list (backward)
7183   `(if ,backward
7184        (or gnus-newsgroup-data-reverse
7185            (setq gnus-newsgroup-data-reverse
7186                  (reverse gnus-newsgroup-data)))
7187      gnus-newsgroup-data))
7188
7189 (defun gnus-data-update-list (data offset)
7190   "Add OFFSET to the POS of all data entries in DATA."
7191   (while data
7192     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7193     (setq data (cdr data))))
7194
7195 (defun gnus-data-compute-positions ()
7196   "Compute the positions of all articles."
7197   (let ((data gnus-newsgroup-data)
7198         pos)
7199     (while data
7200       (when (setq pos (text-property-any
7201                        (point-min) (point-max)
7202                        'gnus-number (gnus-data-number (car data))))
7203         (gnus-data-set-pos (car data) (+ pos 3)))
7204       (setq data (cdr data)))))
7205
7206 (defun gnus-summary-article-pseudo-p (article)
7207   "Say whether this article is a pseudo article or not."
7208   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7209
7210 (defun gnus-article-parent-p (number)
7211   "Say whether this article is a parent or not."
7212   (let ((data (gnus-data-find-list number)))
7213     (and (cdr data)                     ; There has to be an article after...
7214          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7215             (gnus-data-level (nth 1 data))))))
7216
7217 (defun gnus-article-children (number)
7218   "Return a list of all children to NUMBER."
7219   (let* ((data (gnus-data-find-list number))
7220          (level (gnus-data-level (car data)))
7221          children)
7222     (setq data (cdr data))
7223     (while (and data            
7224                 (= (gnus-data-level (car data)) (1+ level)))
7225       (push (gnus-data-number (car data)) children)
7226       (setq data (cdr data)))
7227     children))
7228
7229 (defmacro gnus-summary-skip-intangible ()
7230   "If the current article is intangible, then jump to a different article."
7231   '(let ((to (get-text-property (point) 'gnus-intangible)))
7232     (and to (gnus-summary-goto-subject to))))
7233
7234 (defmacro gnus-summary-article-intangible-p ()
7235   "Say whether this article is intangible or not."
7236   '(get-text-property (point) 'gnus-intangible))
7237
7238 ;; Some summary mode macros.
7239
7240 (defmacro gnus-summary-article-number ()
7241   "The article number of the article on the current line.
7242 If there isn's an article number here, then we return the current
7243 article number."
7244   '(progn
7245      (gnus-summary-skip-intangible)
7246      (or (get-text-property (point) 'gnus-number)
7247          (gnus-summary-last-subject))))
7248
7249 (defmacro gnus-summary-article-header (&optional number)
7250   `(gnus-data-header (gnus-data-find
7251                       ,(or number '(gnus-summary-article-number)))))
7252
7253 (defmacro gnus-summary-thread-level (&optional number)
7254   `(if (and (eq gnus-summary-make-false-root 'dummy)
7255             (get-text-property (point) 'gnus-intangible))
7256        0
7257      (gnus-data-level (gnus-data-find
7258                        ,(or number '(gnus-summary-article-number))))))
7259
7260 (defmacro gnus-summary-article-mark (&optional number)
7261   `(gnus-data-mark (gnus-data-find
7262                     ,(or number '(gnus-summary-article-number)))))
7263
7264 (defmacro gnus-summary-article-pos (&optional number)
7265   `(gnus-data-pos (gnus-data-find
7266                    ,(or number '(gnus-summary-article-number)))))
7267
7268 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7269 (defmacro gnus-summary-article-subject (&optional number)
7270   "Return current subject string or nil if nothing."
7271   `(let ((headers
7272           ,(if number
7273                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7274              '(gnus-data-header (assq (gnus-summary-article-number)
7275                                       gnus-newsgroup-data)))))
7276      (and headers
7277           (vectorp headers)
7278           (mail-header-subject headers))))
7279
7280 (defmacro gnus-summary-article-score (&optional number)
7281   "Return current article score."
7282   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7283                   gnus-newsgroup-scored))
7284        gnus-summary-default-score 0))
7285
7286 (defun gnus-summary-article-children (&optional number)
7287   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7288          (level (gnus-data-level (car data)))
7289          l children)
7290     (while (and (setq data (cdr data))
7291                 (> (setq l (gnus-data-level (car data))) level))
7292       (and (= (1+ level) l)
7293            (setq children (cons (gnus-data-number (car data))
7294                                 children))))
7295     (nreverse children)))
7296
7297 (defun gnus-summary-article-parent (&optional number)
7298   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7299                                     (gnus-data-list t)))
7300          (level (gnus-data-level (car data))))
7301     (if (zerop level)
7302         () ; This is a root.
7303       ;; We search until we find an article with a level less than
7304       ;; this one.  That function has to be the parent.
7305       (while (and (setq data (cdr data))
7306                   (not (< (gnus-data-level (car data)) level))))
7307       (and data (gnus-data-number (car data))))))
7308
7309 (defun gnus-unread-mark-p (mark)
7310   "Say whether MARK is the unread mark."
7311   (= mark gnus-unread-mark))
7312
7313 (defun gnus-read-mark-p (mark)
7314   "Say whether MARK is one of the marks that mark as read.
7315 This is all marks except unread, ticked, dormant, and expirable."
7316   (not (or (= mark gnus-unread-mark)
7317            (= mark gnus-ticked-mark)
7318            (= mark gnus-dormant-mark)
7319            (= mark gnus-expirable-mark))))
7320
7321 ;; Various summary mode internalish functions.
7322
7323 (defun gnus-mouse-pick-article (e)
7324   (interactive "e")
7325   (mouse-set-point e)
7326   (gnus-summary-next-page nil t))
7327
7328 (defun gnus-summary-setup-buffer (group)
7329   "Initialize summary buffer."
7330   (let ((buffer (concat "*Summary " group "*")))
7331     (if (get-buffer buffer)
7332         (progn
7333           (set-buffer buffer)
7334           (setq gnus-summary-buffer (current-buffer))
7335           (not gnus-newsgroup-prepared))
7336       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7337       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7338       (gnus-add-current-to-buffer-list)
7339       (gnus-summary-mode group)
7340       (when gnus-carpal
7341         (gnus-carpal-setup-buffer 'summary))
7342       (unless gnus-single-article-buffer
7343         (make-local-variable 'gnus-article-buffer)
7344         (make-local-variable 'gnus-article-current)
7345         (make-local-variable 'gnus-original-article-buffer))
7346       (setq gnus-newsgroup-name group)
7347       t)))
7348
7349 (defun gnus-set-global-variables ()
7350   ;; Set the global equivalents of the summary buffer-local variables
7351   ;; to the latest values they had.  These reflect the summary buffer
7352   ;; that was in action when the last article was fetched.
7353   (when (eq major-mode 'gnus-summary-mode)
7354     (setq gnus-summary-buffer (current-buffer))
7355     (let ((name gnus-newsgroup-name)
7356           (marked gnus-newsgroup-marked)
7357           (unread gnus-newsgroup-unreads)
7358           (headers gnus-current-headers)
7359           (data gnus-newsgroup-data)
7360           (summary gnus-summary-buffer)
7361           (article-buffer gnus-article-buffer)
7362           (original gnus-original-article-buffer)
7363           (gac gnus-article-current)
7364           (score-file gnus-current-score-file))
7365       (save-excursion
7366         (set-buffer gnus-group-buffer)
7367         (setq gnus-newsgroup-name name)
7368         (setq gnus-newsgroup-marked marked)
7369         (setq gnus-newsgroup-unreads unread)
7370         (setq gnus-current-headers headers)
7371         (setq gnus-newsgroup-data data)
7372         (setq gnus-article-current gac)
7373         (setq gnus-summary-buffer summary)
7374         (setq gnus-article-buffer article-buffer)
7375         (setq gnus-original-article-buffer original)
7376         (setq gnus-current-score-file score-file)))))
7377
7378 (defun gnus-summary-last-article-p (&optional article)
7379   "Return whether ARTICLE is the last article in the buffer."
7380   (if (not (setq article (or article (gnus-summary-article-number))))
7381       t ; All non-existant numbers are the last article. :-)
7382     (not (cdr (gnus-data-find-list article)))))
7383
7384 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7385   "Insert a dummy root in the summary buffer."
7386   (beginning-of-line)
7387   (add-text-properties
7388    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7389    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7390
7391 (defvar gnus-thread-indent-array nil)
7392 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
7393 (defun gnus-make-thread-indent-array ()
7394   (let ((n 200))
7395     (unless (and gnus-thread-indent-array
7396                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7397       (setq gnus-thread-indent-array (make-vector 201 "")
7398             gnus-thread-indent-array-level gnus-thread-indent-level)
7399       (while (>= n 0)
7400         (aset gnus-thread-indent-array n
7401               (make-string (* n gnus-thread-indent-level) ? ))
7402         (setq n (1- n))))))
7403
7404 (defun gnus-summary-insert-line
7405   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7406                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7407                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7408   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7409          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7410          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7411          (gnus-tmp-score-char
7412           (if (or (null gnus-summary-default-score)
7413                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7414                       gnus-summary-zcore-fuzz)) ? 
7415             (if (< gnus-tmp-score gnus-summary-default-score)
7416                 gnus-score-below-mark gnus-score-over-mark)))
7417          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7418                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7419                                   gnus-cached-mark)
7420                                  (gnus-tmp-replied gnus-replied-mark)
7421                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7422                                   gnus-saved-mark)
7423                                  (t gnus-unread-mark)))
7424          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7425          (gnus-tmp-name
7426           (cond
7427            ((string-match "(.+)" gnus-tmp-from)
7428             (substring gnus-tmp-from
7429                        (1+ (match-beginning 0)) (1- (match-end 0))))
7430            ((string-match "<[^>]+> *$" gnus-tmp-from)
7431             (let ((beg (match-beginning 0)))
7432               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7433                        (substring gnus-tmp-from (1+ (match-beginning 0))
7434                                   (1- (match-end 0))))
7435                   (substring gnus-tmp-from 0 beg))))
7436            (t gnus-tmp-from)))
7437          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7438          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7439          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7440          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7441          (buffer-read-only nil))
7442     (when (string= gnus-tmp-name "")
7443       (setq gnus-tmp-name gnus-tmp-from))
7444     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7445     (put-text-property
7446      (point)
7447      (progn (eval gnus-summary-line-format-spec) (point))
7448      'gnus-number gnus-tmp-number)
7449     (when (gnus-visual-p 'summary-highlight 'highlight)
7450       (forward-line -1)
7451       (run-hooks 'gnus-summary-update-hook)
7452       (forward-line 1))))
7453
7454 (defun gnus-summary-update-line (&optional dont-update)
7455   ;; Update summary line after change.
7456   (when (and gnus-summary-default-score
7457              (not gnus-summary-inhibit-highlight))
7458     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7459            (article (gnus-summary-article-number))
7460            (score (gnus-summary-article-score article)))
7461       (unless dont-update
7462         (if (and gnus-summary-mark-below
7463                  (< (gnus-summary-article-score)
7464                     gnus-summary-mark-below))
7465             ;; This article has a low score, so we mark it as read.
7466             (when (memq article gnus-newsgroup-unreads)
7467               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7468           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7469             ;; This article was previously marked as read on account
7470             ;; of a low score, but now it has risen, so we mark it as
7471             ;; unread.
7472             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7473         (gnus-summary-update-mark
7474          (if (or (null gnus-summary-default-score)
7475                  (<= (abs (- score gnus-summary-default-score))
7476                      gnus-summary-zcore-fuzz)) ? 
7477            (if (< score gnus-summary-default-score)
7478                gnus-score-below-mark gnus-score-over-mark)) 'score))
7479       ;; Do visual highlighting.
7480       (when (gnus-visual-p 'summary-highlight 'highlight)
7481         (run-hooks 'gnus-summary-update-hook)))))
7482
7483 (defvar gnus-tmp-new-adopts nil)
7484
7485 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7486   ;; Sum up all elements (and sub-elements) in a list.
7487   (let* ((number
7488           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7489           (cond
7490            ((and (consp thread) (cdr thread))
7491             (apply
7492              '+ 1 (mapcar
7493                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7494            ((null thread)
7495             1)
7496            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7497             1)
7498            (t 1))))
7499     (when (and level (zerop level) gnus-tmp-new-adopts)
7500       (incf number
7501             (apply '+ (mapcar
7502                        'gnus-summary-number-of-articles-in-thread
7503                        gnus-tmp-new-adopts))))
7504     (if char
7505         (if (> number 1) gnus-not-empty-thread-mark
7506           gnus-empty-thread-mark)
7507       number)))
7508
7509 (defun gnus-summary-set-local-parameters (group)
7510  "Go through the local params of GROUP and set all variable specs in that list."
7511   (let ((params (gnus-info-params (gnus-get-info group)))
7512         elem)
7513     (while params
7514       (setq elem (car params)
7515             params (cdr params))
7516       (and (consp elem)                 ; Has to be a cons.
7517            (consp (cdr elem))           ; The cdr has to be a list.
7518            (symbolp (car elem))         ; Has to be a symbol in there.
7519            (not (memq (car elem) 
7520                       '(quit-config to-address to-list to-group)))
7521            (progn                       ; So we set it.
7522              (make-local-variable (car elem))
7523              (set (car elem) (eval (nth 1 elem))))))))
7524
7525 (defun gnus-summary-read-group (group &optional show-all no-article
7526                                       kill-buffer no-display)
7527   "Start reading news in newsgroup GROUP.
7528 If SHOW-ALL is non-nil, already read articles are also listed.
7529 If NO-ARTICLE is non-nil, no article is selected initially.
7530 If NO-DISPLAY, don't generate a summary buffer."
7531   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7532   (let* ((new-group (gnus-summary-setup-buffer group))
7533          (quit-config (gnus-group-quit-config group))
7534          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7535     (cond
7536      ;; This summary buffer exists already, so we just select it.
7537      ((not new-group)
7538       (gnus-set-global-variables)
7539       (when kill-buffer
7540         (gnus-kill-or-deaden-summary kill-buffer))
7541       (gnus-configure-windows 'summary 'force)
7542       (gnus-set-mode-line 'summary)
7543       (gnus-summary-position-point)
7544       (message "")
7545       t)
7546      ;; We couldn't select this group.
7547      ((null did-select)
7548       (when (and (eq major-mode 'gnus-summary-mode)
7549                  (not (equal (current-buffer) kill-buffer)))
7550         (kill-buffer (current-buffer))
7551         (if (not quit-config)
7552             (progn
7553               (set-buffer gnus-group-buffer)
7554               (gnus-group-jump-to-group group)
7555               (gnus-group-next-unread-group 1))
7556           (if (not (buffer-name (car quit-config)))
7557               (gnus-configure-windows 'group 'force)
7558             (set-buffer (car quit-config))
7559             (and (eq major-mode 'gnus-summary-mode)
7560                  (gnus-set-global-variables))
7561             (gnus-configure-windows (cdr quit-config)))))
7562       (gnus-message 3 "Can't select group")
7563       nil)
7564      ;; The user did a `C-g' while prompting for number of articles,
7565      ;; so we exit this group.
7566      ((eq did-select 'quit)
7567       (and (eq major-mode 'gnus-summary-mode)
7568            (not (equal (current-buffer) kill-buffer))
7569            (kill-buffer (current-buffer)))
7570       (when kill-buffer
7571         (gnus-kill-or-deaden-summary kill-buffer))
7572       (if (not quit-config)
7573           (progn
7574             (set-buffer gnus-group-buffer)
7575             (gnus-group-jump-to-group group)
7576             (gnus-group-next-unread-group 1)
7577             (gnus-configure-windows 'group 'force))
7578         (if (not (buffer-name (car quit-config)))
7579             (gnus-configure-windows 'group 'force)
7580           (set-buffer (car quit-config))
7581           (and (eq major-mode 'gnus-summary-mode)
7582                (gnus-set-global-variables))
7583           (gnus-configure-windows (cdr quit-config))))
7584       ;; Finally signal the quit.
7585       (signal 'quit nil))
7586      ;; The group was successfully selected.
7587      (t
7588       (gnus-set-global-variables)
7589       ;; Save the active value in effect when the group was entered.
7590       (setq gnus-newsgroup-active
7591             (gnus-copy-sequence
7592              (gnus-active gnus-newsgroup-name)))
7593       ;; You can change the summary buffer in some way with this hook.
7594       (run-hooks 'gnus-select-group-hook)
7595       ;; Set any local variables in the group parameters.
7596       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7597       (gnus-update-format-specifications)
7598       ;; Do score processing.
7599       (when gnus-use-scoring
7600         (gnus-possibly-score-headers))
7601       ;; Check whether to fill in the gaps in the threads.
7602       (when gnus-build-sparse-threads
7603         (gnus-build-sparse-threads))
7604       ;; Find the initial limit.
7605       (if gnus-show-threads
7606           (if show-all
7607               (let ((gnus-newsgroup-dormant nil))
7608                 (gnus-summary-initial-limit show-all))
7609             (gnus-summary-initial-limit show-all))
7610         (setq gnus-newsgroup-limit 
7611               (mapcar 
7612                (lambda (header) (mail-header-number header))
7613                gnus-newsgroup-headers)))
7614       ;; Generate the summary buffer.
7615       (unless no-display
7616         (gnus-summary-prepare))
7617       (when gnus-use-trees
7618         (gnus-tree-open group)
7619         (setq gnus-summary-highlight-line-function
7620               'gnus-tree-highlight-article))
7621       ;; If the summary buffer is empty, but there are some low-scored
7622       ;; articles or some excluded dormants, we include these in the
7623       ;; buffer.
7624       (when (and (zerop (buffer-size))
7625                  (not no-display))
7626         (cond (gnus-newsgroup-dormant
7627                (gnus-summary-limit-include-dormant))
7628               ((and gnus-newsgroup-scored show-all)
7629                (gnus-summary-limit-include-expunged))))
7630       ;; Function `gnus-apply-kill-file' must be called in this hook.
7631       (run-hooks 'gnus-apply-kill-hook)
7632       (if (and (zerop (buffer-size))
7633                (not no-display))
7634           (progn
7635             ;; This newsgroup is empty.
7636             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7637             (gnus-message 6 "No unread news")
7638             (when kill-buffer
7639               (gnus-kill-or-deaden-summary kill-buffer))
7640             ;; Return nil from this function.
7641             nil)
7642         ;; Hide conversation thread subtrees.  We cannot do this in
7643         ;; gnus-summary-prepare-hook since kill processing may not
7644         ;; work with hidden articles.
7645         (and gnus-show-threads
7646              gnus-thread-hide-subtree
7647              (gnus-summary-hide-all-threads))
7648         ;; Show first unread article if requested.
7649         (if (and (not no-article)
7650                  (not no-display)
7651                  gnus-newsgroup-unreads
7652                  gnus-auto-select-first)
7653             (unless (if (eq gnus-auto-select-first 'best)
7654                         (gnus-summary-best-unread-article)
7655                       (gnus-summary-first-unread-article))
7656               (gnus-configure-windows 'summary))
7657           ;; Don't select any articles, just move point to the first
7658           ;; article in the group.
7659           (goto-char (point-min))
7660           (gnus-summary-position-point)
7661           (gnus-set-mode-line 'summary)
7662           (gnus-configure-windows 'summary 'force))
7663         ;; If we are in async mode, we send some info to the backend.
7664         (when gnus-newsgroup-async
7665           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7666         (when kill-buffer
7667           (gnus-kill-or-deaden-summary kill-buffer))
7668         (when (get-buffer-window gnus-group-buffer t)
7669           ;; Gotta use windows, because recenter does wierd stuff if
7670           ;; the current buffer ain't the displayed window.
7671           (let ((owin (selected-window)))
7672             (select-window (get-buffer-window gnus-group-buffer t))
7673             (when (gnus-group-goto-group group)
7674               (recenter))
7675             (select-window owin))))
7676       ;; Mark this buffer as "prepared".
7677       (setq gnus-newsgroup-prepared t)
7678       t))))
7679
7680 (defun gnus-summary-prepare ()
7681   "Generate the summary buffer."
7682   (let ((buffer-read-only nil))
7683     (erase-buffer)
7684     (setq gnus-newsgroup-data nil
7685           gnus-newsgroup-data-reverse nil)
7686     (run-hooks 'gnus-summary-generate-hook)
7687     ;; Generate the buffer, either with threads or without.
7688     (when gnus-newsgroup-headers
7689       (gnus-summary-prepare-threads
7690        (if gnus-show-threads
7691            (gnus-sort-gathered-threads
7692             (funcall gnus-summary-thread-gathering-function
7693                      (gnus-sort-threads
7694                       (gnus-cut-threads (gnus-make-threads)))))
7695          ;; Unthreaded display.
7696          (gnus-sort-articles gnus-newsgroup-headers))))
7697     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7698     ;; Call hooks for modifying summary buffer.
7699     (goto-char (point-min))
7700     (run-hooks 'gnus-summary-prepare-hook)))
7701
7702 (defun gnus-gather-threads-by-subject (threads)
7703   "Gather threads by looking at Subject headers."
7704   (if (not gnus-summary-make-false-root)
7705       threads
7706     (let ((hashtb (gnus-make-hashtable 1023))
7707           (prev threads)
7708           (result threads)
7709           subject hthread whole-subject)
7710       (while threads
7711         (setq whole-subject (mail-header-subject (caar threads)))
7712         (setq subject
7713               (cond
7714                ;; Truncate the subject.
7715                ((numberp gnus-summary-gather-subject-limit)
7716                 (setq subject (gnus-simplify-subject-re whole-subject))
7717                 (if (> (length subject) gnus-summary-gather-subject-limit)
7718                     (substring subject 0 gnus-summary-gather-subject-limit)
7719                   subject))
7720                ;; Fuzzily simplify it.
7721                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7722                 (gnus-simplify-subject-fuzzy whole-subject))
7723                ;; Just remove the leading "Re:".
7724                (t
7725                 (gnus-simplify-subject-re whole-subject))))
7726
7727         (if (and gnus-summary-gather-exclude-subject
7728                  (string-match gnus-summary-gather-exclude-subject
7729                                subject))
7730             ()          ; We don't want to do anything with this article.
7731           ;; We simplify the subject before looking it up in the
7732           ;; hash table.
7733
7734           (if (setq hthread (gnus-gethash subject hashtb))
7735               (progn
7736                 ;; We enter a dummy root into the thread, if we
7737                 ;; haven't done that already.
7738                 (unless (stringp (caar hthread))
7739                   (setcar hthread (list whole-subject (car hthread))))
7740                 ;; We add this new gathered thread to this gathered
7741                 ;; thread.
7742                 (setcdr (car hthread)
7743                         (nconc (cdar hthread) (list (car threads))))
7744                 ;; Remove it from the list of threads.
7745                 (setcdr prev (cdr threads))
7746                 (setq threads prev))
7747             ;; Enter this thread into the hash table.
7748             (gnus-sethash subject threads hashtb)))
7749         (setq prev threads)
7750         (setq threads (cdr threads)))
7751       result)))
7752
7753 (defun gnus-gather-threads-by-references (threads)
7754   "Gather threads by looking at References headers."
7755   (let ((idhashtb (gnus-make-hashtable 1023))
7756         (thhashtb (gnus-make-hashtable 1023))
7757         (prev threads)
7758         (result threads)
7759         ids references id gthread gid entered)
7760     (while threads
7761       (when (setq references (mail-header-references (caar threads)))
7762         (setq id (mail-header-id (caar threads)))
7763         (setq ids (gnus-split-references references))
7764         (setq entered nil)
7765         (while ids
7766           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7767               (progn
7768                 (gnus-sethash (car ids) id idhashtb)
7769                 (gnus-sethash id threads thhashtb))
7770             (setq gthread (gnus-gethash gid thhashtb))
7771             (unless entered
7772               ;; We enter a dummy root into the thread, if we
7773               ;; haven't done that already.
7774               (unless (stringp (caar gthread))
7775                 (setcar gthread (list (mail-header-subject (caar gthread))
7776                                       (car gthread))))
7777               ;; We add this new gathered thread to this gathered
7778               ;; thread.
7779               (setcdr (car gthread)
7780                       (nconc (cdar gthread) (list (car threads)))))
7781             ;; Add it into the thread hash table.
7782             (gnus-sethash id gthread thhashtb)
7783             (setq entered t)
7784             ;; Remove it from the list of threads.
7785             (setcdr prev (cdr threads))
7786             (setq threads prev))
7787           (setq ids (cdr ids))))
7788       (setq prev threads)
7789       (setq threads (cdr threads)))
7790     result))
7791
7792 (defun gnus-sort-gathered-threads (threads)
7793   "Sort subtreads inside each gathered thread by article number."
7794   (let ((result threads))
7795     (while threads
7796       (when (stringp (caar threads))
7797         (setcdr (car threads)
7798                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7799       (setq threads (cdr threads)))
7800     result))
7801
7802 (defun gnus-make-threads ()
7803   "Go through the dependency hashtb and find the roots.  Return all threads."
7804   (let (threads)
7805     (mapatoms
7806      (lambda (refs)
7807        (unless (car (symbol-value refs))
7808          ;; These threads do not refer back to any other articles,
7809          ;; so they're roots.
7810          (setq threads (append (cdr (symbol-value refs)) threads))))
7811      gnus-newsgroup-dependencies)
7812     threads))
7813
7814 (defun gnus-build-sparse-threads ()
7815   (let ((headers gnus-newsgroup-headers)
7816         (deps gnus-newsgroup-dependencies)
7817         header references generation relations 
7818         cthread subject child end pthread relation)
7819     ;; First we create an alist of generations/relations, where 
7820     ;; generations is how much we trust the ralation, and the relation
7821     ;; is parent/child.
7822     (gnus-message 7 "Making sparse threads...")
7823     (save-excursion
7824       (nnheader-set-temp-buffer " *gnus sparse threads*")
7825       (while (setq header (pop headers))
7826         (when (and (setq references (mail-header-references header))
7827                    (not (string= references "")))
7828           (insert references)
7829           (setq child (mail-header-id header)
7830                 subject (mail-header-subject header))
7831           (setq generation 0)
7832           (while (search-backward ">" nil t)
7833             (setq end (1+ (point)))
7834             (when (search-backward "<" nil t)
7835               (push (list (incf generation) 
7836                           child (setq child (buffer-substring (point) end))
7837                           subject)
7838                     relations)))
7839           (push (list (1+ generation) child nil subject) relations)
7840           (erase-buffer)))
7841       (kill-buffer (current-buffer)))
7842     ;; Sort over trustworthiness.
7843     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7844     (while (setq relation (pop relations))
7845       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7846                 (unless (car (symbol-value cthread))
7847                   ;; Make this article the parent of these threads.
7848                   (setcar (symbol-value cthread)
7849                           (vector gnus-reffed-article-number 
7850                                   (cadddr relation) 
7851                                   "" ""
7852                                   (cadr relation) 
7853                                   (or (caddr relation) "") 0 0 "")))
7854               (set cthread (list (vector gnus-reffed-article-number
7855                                          (cadddr relation) 
7856                                          "" "" (cadr relation) 
7857                                          (or (caddr relation) "") 0 0 ""))))
7858         (push gnus-reffed-article-number gnus-newsgroup-limit)
7859         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7860         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7861               gnus-newsgroup-reads)
7862         (decf gnus-reffed-article-number)
7863         ;; Make this new thread the child of its parent.
7864         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7865             (setcdr (symbol-value pthread)
7866                     (nconc (cdr (symbol-value pthread))
7867                            (list (symbol-value cthread))))
7868           (set pthread (list nil (symbol-value cthread))))))
7869     (gnus-message 7 "Making sparse threads...done")))
7870
7871 (defun gnus-build-old-threads ()
7872   ;; Look at all the articles that refer back to old articles, and
7873   ;; fetch the headers for the articles that aren't there.  This will
7874   ;; build complete threads - if the roots haven't been expired by the
7875   ;; server, that is.
7876   (let (id heads)
7877     (mapatoms
7878      (lambda (refs)
7879        (when (not (car (symbol-value refs)))
7880          (setq heads (cdr (symbol-value refs)))
7881          (while heads
7882            (if (memq (mail-header-number (caar heads))
7883                      gnus-newsgroup-dormant)
7884                (setq heads (cdr heads))
7885              (setq id (symbol-name refs))
7886              (while (and (setq id (gnus-build-get-header id))
7887                          (not (car (gnus-gethash
7888                                     id gnus-newsgroup-dependencies)))))
7889              (setq heads nil)))))
7890      gnus-newsgroup-dependencies)))
7891
7892 (defun gnus-build-get-header (id)
7893   ;; Look through the buffer of NOV lines and find the header to
7894   ;; ID.  Enter this line into the dependencies hash table, and return
7895   ;; the id of the parent article (if any).
7896   (let ((deps gnus-newsgroup-dependencies)
7897         found header)
7898     (prog1
7899         (save-excursion
7900           (set-buffer nntp-server-buffer)
7901           (goto-char (point-min))
7902           (while (and (not found) (search-forward id nil t))
7903             (beginning-of-line)
7904             (setq found (looking-at
7905                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7906                                  (regexp-quote id))))
7907             (or found (beginning-of-line 2)))
7908           (when found
7909             (beginning-of-line)
7910             (and
7911              (setq header (gnus-nov-parse-line
7912                            (read (current-buffer)) deps))
7913              (gnus-parent-id (mail-header-references header)))))
7914       (when header
7915         (let ((number (mail-header-number header)))
7916           (push number gnus-newsgroup-limit)
7917           (push header gnus-newsgroup-headers)
7918           (if (memq number gnus-newsgroup-unselected)
7919               (progn
7920                 (push number gnus-newsgroup-unreads)
7921                 (setq gnus-newsgroup-unselected
7922                       (delq number gnus-newsgroup-unselected)))
7923             (push number gnus-newsgroup-ancient)))))))
7924
7925 (defun gnus-summary-update-article (article &optional header)
7926   "Update ARTICLE in the summary buffer."
7927   (set-buffer gnus-summary-buffer)
7928   (let* ((header (or header (gnus-summary-article-header article)))
7929          (id (mail-header-id header))
7930          (data (gnus-data-find article))
7931          (thread (gnus-id-to-thread id))
7932          (parent
7933           (gnus-id-to-thread (or (gnus-parent-id 
7934                                   (mail-header-references header))
7935                                  "tull")))
7936          (buffer-read-only nil)
7937          (old (car thread))
7938          (number (mail-header-number header))
7939          pos)
7940     (when thread
7941       (setcar thread nil)
7942       (when parent
7943         (delq thread parent))
7944       (if (gnus-summary-insert-subject id header)
7945           ;; Set the (possibly) new article number in the data structure.
7946           (gnus-data-set-number data (gnus-id-to-article id))
7947         (setcar thread old)
7948         nil))))
7949
7950 (defun gnus-rebuild-thread (id)
7951   "Rebuild the thread containing ID."
7952   (let ((buffer-read-only nil)
7953         current thread data)
7954     (if (not gnus-show-threads)
7955         (setq thread (list (car (gnus-id-to-thread id))))
7956       ;; Get the thread this article is part of.
7957       (setq thread (gnus-remove-thread id)))
7958     (setq current (save-excursion
7959                     (and (zerop (forward-line -1))
7960                          (gnus-summary-article-number))))
7961     ;; If this is a gathered thread, we have to go some re-gathering.
7962     (when (stringp (car thread))
7963       (let ((subject (car thread))
7964             roots thr)
7965         (setq thread (cdr thread))
7966         (while thread
7967           (unless (memq (setq thr (gnus-id-to-thread
7968                                       (gnus-root-id
7969                                        (mail-header-id (caar thread)))))
7970                         roots)
7971             (push thr roots))
7972           (setq thread (cdr thread)))
7973         ;; We now have all (unique) roots.
7974         (if (= (length roots) 1)
7975             ;; All the loose roots are now one solid root.
7976             (setq thread (car roots))
7977           (setq thread (cons subject (gnus-sort-threads roots))))))
7978     (let (threads)
7979       ;; We then insert this thread into the summary buffer.
7980       (let (gnus-newsgroup-data gnus-newsgroup-threads)
7981         (gnus-summary-prepare-threads (list thread))
7982         (setq data (nreverse gnus-newsgroup-data))
7983         (setq threads gnus-newsgroup-threads))
7984       ;; We splice the new data into the data structure.
7985       (gnus-data-enter-list current data)
7986       (gnus-data-compute-positions)
7987       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
7988
7989 (defun gnus-id-to-thread (id)
7990   "Return the (sub-)thread where ID appears."
7991   (gnus-gethash id gnus-newsgroup-dependencies))
7992
7993 (defun gnus-id-to-article (id)
7994   "Return the article number of ID."
7995   (let ((thread (gnus-id-to-thread id)))
7996     (when thread
7997       (mail-header-number (car thread)))))
7998
7999 (defun gnus-id-to-header (id)
8000   "Return the article headers of ID."
8001   (car (gnus-id-to-thread id)))
8002
8003 (defun gnus-article-displayed-root-p (article)
8004   "Say whether ARTICLE is a root(ish) article."
8005   (let ((level (gnus-summary-thread-level article))
8006         particle)
8007     (cond 
8008      ((null level) nil)
8009      ((zerop level) t)
8010      ((and (= 1 level)
8011            (null (setq particle (gnus-id-to-article
8012                                  (gnus-parent-id 
8013                                   (mail-header-references 
8014                                    (gnus-summary-article-header article))))))
8015            (null (gnus-summary-thread-level particle)))))))
8016
8017 (defun gnus-root-id (id)
8018   "Return the id of the root of the thread where ID appears."
8019   (let (last-id prev)
8020     (while (and id (setq prev (car (gnus-gethash 
8021                                     id gnus-newsgroup-dependencies))))
8022       (setq last-id id
8023             id (gnus-parent-id (mail-header-references prev))))
8024     last-id))
8025
8026 (defun gnus-remove-thread (id &optional dont-remove)
8027   "Remove the thread that has ID in it."
8028   (let ((dep gnus-newsgroup-dependencies)
8029         headers thread last-id)
8030     ;; First go up in this thread until we find the root.
8031     (setq last-id (gnus-root-id id))
8032     (setq headers (list (car (gnus-id-to-thread last-id))
8033                         (caadr (gnus-id-to-thread last-id))))
8034     ;; We have now found the real root of this thread.  It might have
8035     ;; been gathered into some loose thread, so we have to search
8036     ;; through the threads to find the thread we wanted.
8037     (let ((threads gnus-newsgroup-threads)
8038           sub)
8039       (while threads
8040         (setq sub (car threads))
8041         (if (stringp (car sub))
8042             ;; This is a gathered threads, so we look at the roots
8043             ;; below it to find whether this article in in this
8044             ;; gathered root.
8045             (progn
8046               (setq sub (cdr sub))
8047               (while sub
8048                 (when (member (caar sub) headers)
8049                   (setq thread (car threads)
8050                         threads nil
8051                         sub nil))
8052                 (setq sub (cdr sub))))
8053           ;; It's an ordinary thread, so we check it.
8054           (when (eq (car sub) (car headers))
8055             (setq thread sub
8056                   threads nil)))
8057         (setq threads (cdr threads)))
8058       ;; If this article is in no thread, then it's a root.
8059       (if thread
8060           (unless dont-remove
8061             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8062         (setq thread (gnus-gethash last-id dep)))
8063       (when thread
8064         (prog1
8065             thread ; We return this thread.
8066           (unless dont-remove
8067             (if (stringp (car thread))
8068                 (progn
8069                   ;; If we use dummy roots, then we have to remove the
8070                   ;; dummy root as well.
8071                   (when (eq gnus-summary-make-false-root 'dummy)
8072                     ;; Uhm.
8073                     )
8074                   (setq thread (cdr thread))
8075                   (while thread
8076                     (gnus-remove-thread-1 (car thread))
8077                     (setq thread (cdr thread))))
8078               (gnus-remove-thread-1 thread))))))))
8079
8080 (defun gnus-remove-thread-1 (thread)
8081   "Remove the thread THREAD recursively."
8082   (let ((number (mail-header-number (car thread)))
8083         pos)
8084     (when (setq pos (text-property-any
8085                      (point-min) (point-max) 'gnus-number number))
8086       (goto-char pos)
8087       (gnus-delete-line)
8088       (gnus-data-remove number))
8089     (setq thread (cdr thread))
8090     (while thread
8091       (gnus-remove-thread-1 (pop thread)))))
8092
8093 (defun gnus-sort-threads (threads)
8094   "Sort THREADS."
8095   (if (not gnus-thread-sort-functions)
8096       threads
8097     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8098                     (car gnus-thread-sort-functions)
8099                   `(lambda (t1 t2)
8100                      ,(gnus-make-sort-function 
8101                        (reverse gnus-thread-sort-functions))))))
8102       (gnus-message 7 "Sorting threads...")
8103       (prog1
8104           (sort threads func)
8105         (gnus-message 7 "Sorting threads...done")))))
8106
8107 (defun gnus-sort-articles (articles)
8108   "Sort ARTICLES."
8109   (when gnus-article-sort-functions
8110     (let ((func (if (= 1 (length gnus-article-sort-functions))
8111                     (car gnus-article-sort-functions)
8112                   `(lambda (t1 t2)
8113                      ,(gnus-make-sort-function 
8114                        (reverse gnus-article-sort-functions))))))
8115       (gnus-message 7 "Sorting articles...")
8116       (prog1
8117           (setq gnus-newsgroup-headers (sort articles func))
8118         (gnus-message 7 "Sorting articles...done")))))
8119
8120 (defun gnus-make-sort-function (funs)
8121   "Return a composite sort condition based on the functions in FUNC."
8122   (if (cdr funs)
8123       `(or (,(car funs) t1 t2)
8124            (and (not (,(car funs) t2 t1))
8125                 ,(gnus-make-sort-function (cdr funs))))
8126     `(,(car funs) t1 t2)))
8127                  
8128 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8129 (defmacro gnus-thread-header (thread)
8130   ;; Return header of first article in THREAD.
8131   ;; Note that THREAD must never, ever be anything else than a variable -
8132   ;; using some other form will lead to serious barfage.
8133   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8134   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8135   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8136         (vector thread) 2))
8137
8138 (defsubst gnus-article-sort-by-number (h1 h2)
8139   "Sort articles by article number."
8140   (< (mail-header-number h1)
8141      (mail-header-number h2)))
8142
8143 (defun gnus-thread-sort-by-number (h1 h2)
8144   "Sort threads by root article number."
8145   (gnus-article-sort-by-number
8146    (gnus-thread-header h1) (gnus-thread-header h2)))
8147
8148 (defsubst gnus-article-sort-by-author (h1 h2)
8149   "Sort articles by root author."
8150   (string-lessp
8151    (let ((extract (funcall
8152                    gnus-extract-address-components
8153                    (mail-header-from h1))))
8154      (or (car extract) (cdr extract)))
8155    (let ((extract (funcall
8156                    gnus-extract-address-components
8157                    (mail-header-from h2))))
8158      (or (car extract) (cdr extract)))))
8159
8160 (defun gnus-thread-sort-by-author (h1 h2)
8161   "Sort threads by root author."
8162   (gnus-article-sort-by-author
8163    (gnus-thread-header h1)  (gnus-thread-header h2)))
8164
8165 (defsubst gnus-article-sort-by-subject (h1 h2)
8166   "Sort articles by root subject."
8167   (string-lessp
8168    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8169    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8170
8171 (defun gnus-thread-sort-by-subject (h1 h2)
8172   "Sort threads by root subject."
8173   (gnus-article-sort-by-subject
8174    (gnus-thread-header h1) (gnus-thread-header h2)))
8175
8176 (defsubst gnus-article-sort-by-date (h1 h2)
8177   "Sort articles by root article date."
8178   (string-lessp
8179    (gnus-sortable-date (mail-header-date h1))
8180    (gnus-sortable-date (mail-header-date h2))))
8181
8182 (defun gnus-thread-sort-by-date (h1 h2)
8183   "Sort threads by root article date."
8184   (gnus-article-sort-by-date
8185    (gnus-thread-header h1) (gnus-thread-header h2)))
8186
8187 (defsubst gnus-article-sort-by-score (h1 h2)
8188   "Sort articles by root article score.
8189 Unscored articles will be counted as having a score of zero."
8190   (> (or (cdr (assq (mail-header-number h1)
8191                     gnus-newsgroup-scored))
8192          gnus-summary-default-score 0)
8193      (or (cdr (assq (mail-header-number h2)
8194                     gnus-newsgroup-scored))
8195          gnus-summary-default-score 0)))
8196
8197 (defun gnus-thread-sort-by-score (h1 h2)
8198   "Sort threads by root article score."
8199   (gnus-article-sort-by-score
8200    (gnus-thread-header h1) (gnus-thread-header h2)))
8201
8202 (defun gnus-thread-sort-by-total-score (h1 h2)
8203   "Sort threads by the sum of all scores in the thread.
8204 Unscored articles will be counted as having a score of zero."
8205   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8206
8207 (defun gnus-thread-total-score (thread)
8208   ;;  This function find the total score of THREAD.
8209   (if (consp thread)
8210       (if (stringp (car thread))
8211           (apply gnus-thread-score-function 0
8212                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8213         (gnus-thread-total-score-1 thread))
8214     (gnus-thread-total-score-1 (list thread))))
8215
8216 (defun gnus-thread-total-score-1 (root)
8217   ;; This function find the total score of the thread below ROOT.
8218   (setq root (car root))
8219   (apply gnus-thread-score-function
8220          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8221              gnus-summary-default-score 0)
8222          (mapcar 'gnus-thread-total-score
8223                  (cdr (gnus-gethash (mail-header-id root)
8224                                     gnus-newsgroup-dependencies)))))
8225
8226 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8227 (defvar gnus-tmp-prev-subject nil)
8228 (defvar gnus-tmp-false-parent nil)
8229 (defvar gnus-tmp-root-expunged nil)
8230 (defvar gnus-tmp-dummy-line nil)
8231
8232 (defun gnus-summary-prepare-threads (threads)
8233   "Prepare summary buffer from THREADS and indentation LEVEL.
8234 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8235 or a straight list of headers."
8236   (gnus-message 7 "Generating summary...")
8237
8238   (setq gnus-newsgroup-threads threads)
8239   (beginning-of-line)
8240
8241   (let ((gnus-tmp-level 0)
8242         (default-score (or gnus-summary-default-score 0))
8243         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8244         thread number subject stack state gnus-tmp-gathered beg-match
8245         new-roots gnus-tmp-new-adopts thread-end
8246         gnus-tmp-header gnus-tmp-unread
8247         gnus-tmp-replied gnus-tmp-subject-or-nil
8248         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8249         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8250         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8251
8252     (setq gnus-tmp-prev-subject nil)
8253
8254     (if (vectorp (car threads))
8255         ;; If this is a straight (sic) list of headers, then a
8256         ;; threaded summary display isn't required, so we just create
8257         ;; an unthreaded one.
8258         (gnus-summary-prepare-unthreaded threads)
8259
8260       ;; Do the threaded display.
8261
8262       (while (or threads stack gnus-tmp-new-adopts new-roots)
8263
8264         (if (and (= gnus-tmp-level 0)
8265                  (not (setq gnus-tmp-dummy-line nil))
8266                  (or (not stack)
8267                      (= (caar stack) 0))
8268                  (not gnus-tmp-false-parent)
8269                  (or gnus-tmp-new-adopts new-roots))
8270             (if gnus-tmp-new-adopts
8271                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8272                       thread (list (car gnus-tmp-new-adopts))
8273                       gnus-tmp-header (caar thread)
8274                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8275               (if new-roots
8276                   (setq thread (list (car new-roots))
8277                         gnus-tmp-header (caar thread)
8278                         new-roots (cdr new-roots))))
8279
8280           (if threads
8281               ;; If there are some threads, we do them before the
8282               ;; threads on the stack.
8283               (setq thread threads
8284                     gnus-tmp-header (caar thread))
8285             ;; There were no current threads, so we pop something off
8286             ;; the stack.
8287             (setq state (car stack)
8288                   gnus-tmp-level (car state)
8289                   thread (cdr state)
8290                   stack (cdr stack)
8291                   gnus-tmp-header (caar thread))))
8292
8293         (setq gnus-tmp-false-parent nil)
8294         (setq gnus-tmp-root-expunged nil)
8295         (setq thread-end nil)
8296
8297         (if (stringp gnus-tmp-header)
8298             ;; The header is a dummy root.
8299             (cond
8300              ((eq gnus-summary-make-false-root 'adopt)
8301               ;; We let the first article adopt the rest.
8302               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8303                                                (cddar thread)))
8304               (setq gnus-tmp-gathered
8305                     (nconc (mapcar
8306                             (lambda (h) (mail-header-number (car h)))
8307                             (cddar thread))
8308                            gnus-tmp-gathered))
8309               (setq thread (cons (list (caar thread)
8310                                        (cadar thread))
8311                                  (cdr thread)))
8312               (setq gnus-tmp-level -1
8313                     gnus-tmp-false-parent t))
8314              ((eq gnus-summary-make-false-root 'empty)
8315               ;; We print adopted articles with empty subject fields.
8316               (setq gnus-tmp-gathered
8317                     (nconc (mapcar
8318                             (lambda (h) (mail-header-number (car h)))
8319                             (cddar thread))
8320                            gnus-tmp-gathered))
8321               (setq gnus-tmp-level -1))
8322              ((eq gnus-summary-make-false-root 'dummy)
8323               ;; We remember that we probably want to output a dummy
8324               ;; root.
8325               (setq gnus-tmp-dummy-line gnus-tmp-header)
8326               (setq gnus-tmp-prev-subject gnus-tmp-header))
8327              (t
8328               ;; We do not make a root for the gathered
8329               ;; sub-threads at all.
8330               (setq gnus-tmp-level -1)))
8331
8332           (setq number (mail-header-number gnus-tmp-header)
8333                 subject (mail-header-subject gnus-tmp-header))
8334
8335           (cond
8336            ;; If the thread has changed subject, we might want to make
8337            ;; this subthread into a root.
8338            ((and (null gnus-thread-ignore-subject)
8339                  (not (zerop gnus-tmp-level))
8340                  gnus-tmp-prev-subject
8341                  (not (inline
8342                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8343             (setq new-roots (nconc new-roots (list (car thread)))
8344                   thread-end t
8345                   gnus-tmp-header nil))
8346            ;; If the article lies outside the current limit,
8347            ;; then we do not display it.
8348            ((and (not (memq number gnus-newsgroup-limit))
8349                  (not gnus-tmp-dummy-line))
8350             (setq gnus-tmp-gathered
8351                   (nconc (mapcar
8352                           (lambda (h) (mail-header-number (car h)))
8353                           (cdar thread))
8354                          gnus-tmp-gathered))
8355             (setq gnus-tmp-new-adopts (if (cdar thread)
8356                                           (append gnus-tmp-new-adopts
8357                                                   (cdar thread))
8358                                         gnus-tmp-new-adopts)
8359                   thread-end t
8360                   gnus-tmp-header nil)
8361             (when (zerop gnus-tmp-level)
8362               (setq gnus-tmp-root-expunged t)))
8363            ;; Perhaps this article is to be marked as read?
8364            ((and gnus-summary-mark-below
8365                  (< (or (cdr (assq number gnus-newsgroup-scored))
8366                         default-score)
8367                     gnus-summary-mark-below)
8368                  ;; Don't touch sparse articles.
8369                  (not (memq number gnus-newsgroup-sparse)))
8370             (setq gnus-newsgroup-unreads
8371                   (delq number gnus-newsgroup-unreads))
8372             (if gnus-newsgroup-auto-expire
8373                 (push number gnus-newsgroup-expirable)
8374               (push (cons number gnus-low-score-mark)
8375                     gnus-newsgroup-reads))))
8376
8377           (when gnus-tmp-header
8378             ;; We may have an old dummy line to output before this
8379             ;; article.
8380             (when gnus-tmp-dummy-line
8381               (gnus-summary-insert-dummy-line
8382                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8383               (setq gnus-tmp-dummy-line nil))
8384
8385             ;; Compute the mark.
8386             (setq
8387              gnus-tmp-unread
8388              (cond
8389               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8390               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8391               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8392               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8393               (t (or (cdr (assq number gnus-newsgroup-reads))
8394                      gnus-ancient-mark))))
8395
8396             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8397                                   gnus-tmp-header gnus-tmp-level)
8398                   gnus-newsgroup-data)
8399
8400             ;; Actually insert the line.
8401             (setq
8402              gnus-tmp-subject-or-nil
8403              (cond
8404               ((and gnus-thread-ignore-subject
8405                     gnus-tmp-prev-subject
8406                     (not (inline (gnus-subject-equal
8407                                   gnus-tmp-prev-subject subject))))
8408                subject)
8409               ((zerop gnus-tmp-level)
8410                (if (and (eq gnus-summary-make-false-root 'empty)
8411                         (memq number gnus-tmp-gathered)
8412                         gnus-tmp-prev-subject
8413                         (inline (gnus-subject-equal
8414                                  gnus-tmp-prev-subject subject)))
8415                    gnus-summary-same-subject
8416                  subject))
8417               (t gnus-summary-same-subject)))
8418             (if (and (eq gnus-summary-make-false-root 'adopt)
8419                      (= gnus-tmp-level 1)
8420                      (memq number gnus-tmp-gathered))
8421                 (setq gnus-tmp-opening-bracket ?\<
8422                       gnus-tmp-closing-bracket ?\>)
8423               (setq gnus-tmp-opening-bracket ?\[
8424                     gnus-tmp-closing-bracket ?\]))
8425             (setq
8426              gnus-tmp-indentation
8427              (aref gnus-thread-indent-array gnus-tmp-level)
8428              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8429              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8430                                 gnus-summary-default-score 0)
8431              gnus-tmp-score-char
8432              (if (or (null gnus-summary-default-score)
8433                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8434                          gnus-summary-zcore-fuzz)) ? 
8435                (if (< gnus-tmp-score gnus-summary-default-score)
8436                    gnus-score-below-mark gnus-score-over-mark))
8437              gnus-tmp-replied
8438              (cond ((memq number gnus-newsgroup-processable)
8439                     gnus-process-mark)
8440                    ((memq number gnus-newsgroup-cached)
8441                     gnus-cached-mark)
8442                    ((memq number gnus-newsgroup-replied)
8443                     gnus-replied-mark)
8444                    (t gnus-unread-mark))
8445              gnus-tmp-from (mail-header-from gnus-tmp-header)
8446              gnus-tmp-name
8447              (cond
8448               ((string-match "(.+)" gnus-tmp-from)
8449                (substring gnus-tmp-from
8450                           (1+ (match-beginning 0)) (1- (match-end 0))))
8451               ((string-match "<[^>]+> *$" gnus-tmp-from)
8452                (setq beg-match (match-beginning 0))
8453                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8454                         (substring gnus-tmp-from (1+ (match-beginning 0))
8455                                    (1- (match-end 0))))
8456                    (substring gnus-tmp-from 0 beg-match)))
8457               (t gnus-tmp-from)))
8458             (when (string= gnus-tmp-name "")
8459               (setq gnus-tmp-name gnus-tmp-from))
8460             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8461             (put-text-property
8462              (point)
8463              (progn (eval gnus-summary-line-format-spec) (point))
8464              'gnus-number number)
8465             (when gnus-visual-p
8466               (forward-line -1)
8467               (run-hooks 'gnus-summary-update-hook)
8468               (forward-line 1))
8469
8470             (setq gnus-tmp-prev-subject subject)))
8471
8472         (when (nth 1 thread)
8473           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8474         (incf gnus-tmp-level)
8475         (setq threads (if thread-end nil (cdar thread)))
8476         (unless threads
8477           (setq gnus-tmp-level 0)))))
8478   (gnus-message 7 "Generating summary...done"))
8479
8480 (defun gnus-summary-prepare-unthreaded (headers)
8481   "Generate an unthreaded summary buffer based on HEADERS."
8482   (let (header number mark)
8483
8484     (while headers
8485       ;; We may have to root out some bad articles...
8486       (when (memq (setq number (mail-header-number
8487                                 (setq header (pop headers))))
8488                   gnus-newsgroup-limit)
8489         ;; Mark article as read when it has a low score.
8490         (when (and gnus-summary-mark-below
8491                    (< (or (cdr (assq number gnus-newsgroup-scored))
8492                           gnus-summary-default-score 0)
8493                       gnus-summary-mark-below))
8494           (setq gnus-newsgroup-unreads
8495                 (delq number gnus-newsgroup-unreads))
8496           (if gnus-newsgroup-auto-expire
8497               (push number gnus-newsgroup-expirable)
8498             (push (cons number gnus-low-score-mark)
8499                   gnus-newsgroup-reads)))
8500
8501         (setq mark
8502               (cond
8503                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8504                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8505                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8506                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8507                (t (or (cdr (assq number gnus-newsgroup-reads))
8508                       gnus-ancient-mark))))
8509         (setq gnus-newsgroup-data
8510               (cons (gnus-data-make number mark (1+ (point)) header 0)
8511                     gnus-newsgroup-data))
8512         (gnus-summary-insert-line
8513          header 0 nil mark (memq number gnus-newsgroup-replied)
8514          (memq number gnus-newsgroup-expirable)
8515          (mail-header-subject header) nil
8516          (cdr (assq number gnus-newsgroup-scored))
8517          (memq number gnus-newsgroup-processable))))))
8518
8519 (defun gnus-select-newsgroup (group &optional read-all)
8520   "Select newsgroup GROUP.
8521 If READ-ALL is non-nil, all articles in the group are selected."
8522   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8523          (info (nth 2 entry))
8524          articles fetched-articles cached)
8525
8526     (or (gnus-check-server
8527          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8528         (error "Couldn't open server"))
8529
8530     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8531         (gnus-activate-group group) ; Or we can activate it...
8532         (progn ; Or we bug out.
8533           (when (equal major-mode 'gnus-summary-mode)
8534             (kill-buffer (current-buffer)))
8535           (error "Couldn't request group %s: %s"
8536                  group (gnus-status-message group))))
8537
8538     (setq gnus-newsgroup-name group)
8539     (setq gnus-newsgroup-unselected nil)
8540     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8541
8542     (and gnus-asynchronous
8543          (gnus-check-backend-function
8544           'request-asynchronous gnus-newsgroup-name)
8545          (setq gnus-newsgroup-async
8546                (gnus-request-asynchronous gnus-newsgroup-name)))
8547
8548     ;; Adjust and set lists of article marks.
8549     (when info
8550       (gnus-adjust-marked-articles info))
8551
8552     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8553     (when (gnus-virtual-group-p group)
8554       (setq cached gnus-newsgroup-cached))
8555
8556     (setq gnus-newsgroup-unreads
8557           (gnus-set-difference
8558            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8559            gnus-newsgroup-dormant))
8560
8561     (setq gnus-newsgroup-processable nil)
8562
8563     (setq articles (gnus-articles-to-read group read-all))
8564
8565     (cond
8566      ((null articles)
8567       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8568       'quit)
8569      ((eq articles 0) nil)
8570      (t
8571       ;; Init the dependencies hash table.
8572       (setq gnus-newsgroup-dependencies
8573             (gnus-make-hashtable (length articles)))
8574       ;; Retrieve the headers and read them in.
8575       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8576       (setq gnus-newsgroup-headers
8577             (if (eq 'nov
8578                     (setq gnus-headers-retrieved-by
8579                           (gnus-retrieve-headers
8580                            articles gnus-newsgroup-name
8581                            ;; We might want to fetch old headers, but
8582                            ;; not if there is only 1 article.
8583                            (and gnus-fetch-old-headers
8584                                 (or (and
8585                                      (not (eq gnus-fetch-old-headers 'some))
8586                                      (not (numberp gnus-fetch-old-headers)))
8587                                     (> (length articles) 1))))))
8588                 (gnus-get-newsgroup-headers-xover articles)
8589               (gnus-get-newsgroup-headers)))
8590       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8591
8592       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8593       (when cached
8594         (setq gnus-newsgroup-cached cached))
8595
8596       ;; Set the initial limit.
8597       (setq gnus-newsgroup-limit (copy-sequence articles))
8598       ;; Remove canceled articles from the list of unread articles.
8599       (setq gnus-newsgroup-unreads
8600             (gnus-set-sorted-intersection
8601              gnus-newsgroup-unreads
8602              (setq fetched-articles
8603                    (mapcar (lambda (headers) (mail-header-number headers))
8604                            gnus-newsgroup-headers))))
8605       ;; Removed marked articles that do not exist.
8606       (gnus-update-missing-marks
8607        (gnus-sorted-complement fetched-articles articles))
8608       ;; We might want to build some more threads first.
8609       (and gnus-fetch-old-headers
8610            (eq gnus-headers-retrieved-by 'nov)
8611            (gnus-build-old-threads))
8612       ;; Check whether auto-expire is to be done in this group.
8613       (setq gnus-newsgroup-auto-expire
8614             (gnus-group-auto-expirable-p group))
8615       ;; Set up the article buffer now, if necessary.
8616       (unless gnus-single-article-buffer
8617         (gnus-article-setup-buffer))
8618       ;; First and last article in this newsgroup.
8619       (and gnus-newsgroup-headers
8620            (setq gnus-newsgroup-begin
8621                  (mail-header-number (car gnus-newsgroup-headers)))
8622            (setq gnus-newsgroup-end
8623                  (mail-header-number
8624                   (gnus-last-element gnus-newsgroup-headers))))
8625       (setq gnus-reffed-article-number -1)
8626       ;; GROUP is successfully selected.
8627       (or gnus-newsgroup-headers t)))))
8628
8629 (defun gnus-articles-to-read (group read-all)
8630   ;; Find out what articles the user wants to read.
8631   (let* ((articles
8632           ;; Select all articles if `read-all' is non-nil, or if there
8633           ;; are no unread articles.
8634           (if (or read-all
8635                   (and (zerop (length gnus-newsgroup-marked))
8636                        (zerop (length gnus-newsgroup-unreads))))
8637               (gnus-uncompress-range (gnus-active group))
8638             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8639                           (copy-sequence gnus-newsgroup-unreads))
8640                   '<)))
8641          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8642          (scored (length scored-list))
8643          (number (length articles))
8644          (marked (+ (length gnus-newsgroup-marked)
8645                     (length gnus-newsgroup-dormant)))
8646          (select
8647           (cond
8648            ((numberp read-all)
8649             read-all)
8650            (t
8651             (condition-case ()
8652                 (cond
8653                  ((and (or (<= scored marked) (= scored number))
8654                        (numberp gnus-large-newsgroup)
8655                        (> number gnus-large-newsgroup))
8656                   (let ((input
8657                          (read-string
8658                           (format
8659                            "How many articles from %s (default %d): "
8660                            gnus-newsgroup-name number))))
8661                     (if (string-match "^[ \t]*$" input) number input)))
8662                  ((and (> scored marked) (< scored number))
8663                   (let ((input
8664                          (read-string
8665                           (format "%s %s (%d scored, %d total): "
8666                                   "How many articles from"
8667                                   group scored number))))
8668                     (if (string-match "^[ \t]*$" input)
8669                         number input)))
8670                  (t number))
8671               (quit nil))))))
8672     (setq select (if (stringp select) (string-to-number select) select))
8673     (if (or (null select) (zerop select))
8674         select
8675       (if (and (not (zerop scored)) (<= (abs select) scored))
8676           (progn
8677             (setq articles (sort scored-list '<))
8678             (setq number (length articles)))
8679         (setq articles (copy-sequence articles)))
8680
8681       (if (< (abs select) number)
8682           (if (< select 0)
8683               ;; Select the N oldest articles.
8684               (setcdr (nthcdr (1- (abs select)) articles) nil)
8685             ;; Select the N most recent articles.
8686             (setq articles (nthcdr (- number select) articles))))
8687       (setq gnus-newsgroup-unselected
8688             (gnus-sorted-intersection
8689              gnus-newsgroup-unreads
8690              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8691       articles)))
8692
8693 (defun gnus-killed-articles (killed articles)
8694   (let (out)
8695     (while articles
8696       (if (inline (gnus-member-of-range (car articles) killed))
8697           (setq out (cons (car articles) out)))
8698       (setq articles (cdr articles)))
8699     out))
8700
8701 (defun gnus-uncompress-marks (marks)
8702   "Uncompress the mark ranges in MARKS."
8703   (let ((uncompressed '(score bookmark))
8704         out)
8705     (while marks
8706       (if (memq (caar marks) uncompressed)
8707           (push (car marks) out)
8708         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8709       (setq marks (cdr marks)))
8710     out))
8711
8712 (defun gnus-adjust-marked-articles (info)
8713   "Set all article lists and remove all marks that are no longer legal."
8714   (let* ((marked-lists (gnus-info-marks info))
8715          (active (gnus-active (gnus-info-group info)))
8716          (min (car active))
8717          (max (cdr active))
8718          (types gnus-article-mark-lists)
8719          (uncompressed '(score bookmark))
8720          marks var articles article mark)
8721
8722     (while marked-lists
8723       (setq marks (pop marked-lists))
8724       (set (setq var (intern (format "gnus-newsgroup-%s"
8725                                      (car (rassq (setq mark (car marks))
8726                                                  types)))))
8727            (if (memq (car marks) uncompressed) (cdr marks)
8728              (gnus-uncompress-range (cdr marks))))
8729
8730       (setq articles (symbol-value var))
8731
8732       ;; All articles have to be subsets of the active articles.
8733       (cond
8734        ;; Adjust "simple" lists.
8735        ((memq mark '(tick dormant expirable reply killed save))
8736         (while articles
8737           (when (or (< (setq article (pop articles)) min) (> article max))
8738             (set var (delq article (symbol-value var))))))
8739        ;; Adjust assocs.
8740        ((memq mark '(score bookmark))
8741         (while articles
8742           (when (or (< (car (setq article (pop articles))) min)
8743                     (> (car article) max))
8744             (set var (delq article (symbol-value var))))))))))
8745
8746 (defun gnus-update-missing-marks (missing)
8747   "Go through the list of MISSING articles and remove them mark lists."
8748   (when missing
8749     (let ((types gnus-article-mark-lists)
8750           var m)
8751       ;; Go through all types.
8752       (while types
8753         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8754         (when (symbol-value var)
8755           ;; This list has articles.  So we delete all missing articles
8756           ;; from it.
8757           (setq m missing)
8758           (while m
8759             (set var (delq (pop m) (symbol-value var)))))))))
8760
8761 (defun gnus-update-marks ()
8762   "Enter the various lists of marked articles into the newsgroup info list."
8763   (let ((types gnus-article-mark-lists)
8764         (info (gnus-get-info gnus-newsgroup-name))
8765         (uncompressed '(score bookmark killed))
8766         type list newmarked symbol)
8767     (when info
8768       ;; Add all marks lists that are non-nil to the list of marks lists.
8769       (while types
8770         (setq type (pop types))
8771         (when (setq list (symbol-value
8772                           (setq symbol
8773                                 (intern (format "gnus-newsgroup-%s"
8774                                                 (car type))))))
8775           (push (cons (cdr type)
8776                       (if (memq (cdr type) uncompressed) list
8777                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8778                 newmarked)))
8779
8780       ;; Enter these new marks into the info of the group.
8781       (if (nthcdr 3 info)
8782           (setcar (nthcdr 3 info) newmarked)
8783         ;; Add the marks lists to the end of the info.
8784         (when newmarked
8785           (setcdr (nthcdr 2 info) (list newmarked))))
8786
8787       ;; Cut off the end of the info if there's nothing else there.
8788       (let ((i 5))
8789         (while (and (> i 2)
8790                     (not (nth i info)))
8791           (when (nthcdr (decf i) info)
8792             (setcdr (nthcdr i info) nil)))))))
8793
8794 (defun gnus-add-marked-articles (group type articles &optional info force)
8795   ;; Add ARTICLES of TYPE to the info of GROUP.
8796   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8797   ;; add, but replace marked articles of TYPE with ARTICLES.
8798   (let ((info (or info (gnus-get-info group)))
8799         (uncompressed '(score bookmark killed))
8800         marked m)
8801     (or (not info)
8802         (and (not (setq marked (nthcdr 3 info)))
8803              (or (null articles)
8804                  (setcdr (nthcdr 2 info)
8805                          (list (list (cons type (gnus-compress-sequence
8806                                                  articles t)))))))
8807         (and (not (setq m (assq type (car marked))))
8808              (or (null articles)
8809                  (setcar marked
8810                          (cons (cons type (gnus-compress-sequence articles t) )
8811                                (car marked)))))
8812         (if force
8813             (if (null articles)
8814                 (setcar (nthcdr 3 info)
8815                         (delq (assq type (car marked)) (car marked)))
8816               (setcdr m (gnus-compress-sequence articles t)))
8817           (setcdr m (gnus-compress-sequence
8818                      (sort (nconc (gnus-uncompress-range (cdr m))
8819                                   (copy-sequence articles)) '<) t))))))
8820
8821 (defun gnus-set-mode-line (where)
8822   "This function sets the mode line of the article or summary buffers.
8823 If WHERE is `summary', the summary mode line format will be used."
8824   ;; Is this mode line one we keep updated?
8825   (when (memq where gnus-updated-mode-lines)
8826     (let (mode-string)
8827       (save-excursion
8828         ;; We evaluate this in the summary buffer since these
8829         ;; variables are buffer-local to that buffer.
8830         (set-buffer gnus-summary-buffer)
8831         ;; We bind all these variables that are used in the `eval' form
8832         ;; below.
8833         (let* ((mformat (symbol-value
8834                          (intern
8835                           (format "gnus-%s-mode-line-format-spec" where))))
8836                (gnus-tmp-group-name gnus-newsgroup-name)
8837                (gnus-tmp-article-number (or gnus-current-article 0))
8838                (gnus-tmp-unread gnus-newsgroup-unreads)
8839                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8840                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8841                (gnus-tmp-unread-and-unselected
8842                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8843                             (zerop gnus-tmp-unselected)) "")
8844                       ((zerop gnus-tmp-unselected)
8845                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8846                       (t (format "{%d(+%d) more}"
8847                                  gnus-tmp-unread-and-unticked
8848                                  gnus-tmp-unselected))))
8849                (gnus-tmp-subject
8850                 (if (and gnus-current-headers
8851                          (vectorp gnus-current-headers))
8852                     (mail-header-subject gnus-current-headers) ""))
8853                max-len
8854                gnus-tmp-header);; passed as argument to any user-format-funcs
8855           (setq mode-string (eval mformat))
8856           (setq max-len (max 4 (if gnus-mode-non-string-length
8857                                    (- (frame-width)
8858                                       gnus-mode-non-string-length)
8859                                  (length mode-string))))
8860           ;; We might have to chop a bit of the string off...
8861           (when (> (length mode-string) max-len)
8862             (setq mode-string
8863                   (concat (gnus-truncate-string mode-string (- max-len 3))
8864                           "...")))
8865           ;; Pad the mode string a bit.
8866           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8867       ;; Update the mode line.
8868       (setq mode-line-buffer-identification (list mode-string))
8869       (set-buffer-modified-p t))))
8870
8871 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8872   "Go through the HEADERS list and add all Xrefs to a hash table.
8873 The resulting hash table is returned, or nil if no Xrefs were found."
8874   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
8875          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8876          (xref-hashtb (make-vector 63 0))
8877          start group entry number xrefs header)
8878     (while headers
8879       (setq header (pop headers))
8880       (when (and (setq xrefs (mail-header-xref header))
8881                  (not (memq (setq number (mail-header-number header))
8882                             unreads)))
8883         (setq start 0)
8884         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8885           (setq start (match-end 0))
8886           (setq group (if prefix
8887                           (concat prefix (substring xrefs (match-beginning 1)
8888                                                     (match-end 1)))
8889                         (substring xrefs (match-beginning 1) (match-end 1))))
8890           (setq number
8891                 (string-to-int (substring xrefs (match-beginning 2)
8892                                           (match-end 2))))
8893           (if (setq entry (gnus-gethash group xref-hashtb))
8894               (setcdr entry (cons number (cdr entry)))
8895             (gnus-sethash group (cons number nil) xref-hashtb)))))
8896     (and start xref-hashtb)))
8897
8898 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8899   "Look through all the headers and mark the Xrefs as read."
8900   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8901         name entry info xref-hashtb idlist method nth4)
8902     (save-excursion
8903       (set-buffer gnus-group-buffer)
8904       (when (setq xref-hashtb
8905                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8906         (mapatoms
8907          (lambda (group)
8908            (unless (string= from-newsgroup (setq name (symbol-name group)))
8909              (setq idlist (symbol-value group))
8910              ;; Dead groups are not updated.
8911              (and (prog1
8912                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8913                             info (nth 2 entry))
8914                     (if (stringp (setq nth4 (gnus-info-method info)))
8915                         (setq nth4 (gnus-server-to-method nth4))))
8916                   ;; Only do the xrefs if the group has the same
8917                   ;; select method as the group we have just read.
8918                   (or (gnus-methods-equal-p
8919                        nth4 (gnus-find-method-for-group from-newsgroup))
8920                       virtual
8921                       (equal nth4 (setq method (gnus-find-method-for-group
8922                                                 from-newsgroup)))
8923                       (and (equal (car nth4) (car method))
8924                            (equal (nth 1 nth4) (nth 1 method))))
8925                   gnus-use-cross-reference
8926                   (or (not (eq gnus-use-cross-reference t))
8927                       virtual
8928                       ;; Only do cross-references on subscribed
8929                       ;; groups, if that is what is wanted.
8930                       (<= (gnus-info-level info) gnus-level-subscribed))
8931                   (gnus-group-make-articles-read name idlist))))
8932          xref-hashtb)))))
8933
8934 (defun gnus-group-make-articles-read (group articles)
8935   (let* ((num 0)
8936          (entry (gnus-gethash group gnus-newsrc-hashtb))
8937          (info (nth 2 entry))
8938          (active (gnus-active group))
8939          range)
8940     ;; First peel off all illegal article numbers.
8941     (if active
8942         (let ((ids articles)
8943               id first)
8944           (while ids
8945             (setq id (car ids))
8946             (if (and first (> id (cdr active)))
8947                 (progn
8948                   ;; We'll end up in this situation in one particular
8949                   ;; obscure situation.  If you re-scan a group and get
8950                   ;; a new article that is cross-posted to a different
8951                   ;; group that has not been re-scanned, you might get
8952                   ;; crossposted article that has a higher number than
8953                   ;; Gnus believes possible.  So we re-activate this
8954                   ;; group as well.  This might mean doing the
8955                   ;; crossposting thingy will *increase* the number
8956                   ;; of articles in some groups.  Tsk, tsk.
8957                   (setq active (or (gnus-activate-group group) active))))
8958             (if (or (> id (cdr active))
8959                     (< id (car active)))
8960                 (setq articles (delq id articles)))
8961             (setq ids (cdr ids)))))
8962     ;; If the read list is nil, we init it.
8963     (and active
8964          (null (gnus-info-read info))
8965          (> (car active) 1)
8966          (gnus-info-set-read info (cons 1 (1- (car active)))))
8967     ;; Then we add the read articles to the range.
8968     (gnus-info-set-read
8969      info
8970      (setq range
8971            (gnus-add-to-range
8972             (gnus-info-read info) (setq articles (sort articles '<)))))
8973     ;; Then we have to re-compute how many unread
8974     ;; articles there are in this group.
8975     (if active
8976         (progn
8977           (cond
8978            ((not range)
8979             (setq num (- (1+ (cdr active)) (car active))))
8980            ((not (listp (cdr range)))
8981             (setq num (- (cdr active) (- (1+ (cdr range))
8982                                          (car range)))))
8983            (t
8984             (while range
8985               (if (numberp (car range))
8986                   (setq num (1+ num))
8987                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
8988               (setq range (cdr range)))
8989             (setq num (- (cdr active) num))))
8990           ;; Update the number of unread articles.
8991           (setcar entry num)
8992           ;; Update the group buffer.
8993           (gnus-group-update-group group t)))))
8994
8995 (defun gnus-methods-equal-p (m1 m2)
8996   (let ((m1 (or m1 gnus-select-method))
8997         (m2 (or m2 gnus-select-method)))
8998     (or (equal m1 m2)
8999         (and (eq (car m1) (car m2))
9000              (or (not (memq 'address (assoc (symbol-name (car m1))
9001                                             gnus-valid-select-methods)))
9002                  (equal (nth 1 m1) (nth 1 m2)))))))
9003
9004 (defsubst gnus-header-value ()
9005   (buffer-substring (match-end 0) (gnus-point-at-eol)))
9006
9007 (defvar gnus-newsgroup-none-id 0)
9008
9009 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9010   (let ((cur nntp-server-buffer)
9011         (dependencies
9012          (or dependencies
9013              (save-excursion (set-buffer gnus-summary-buffer)
9014                              gnus-newsgroup-dependencies)))
9015         headers id id-dep ref-dep end ref)
9016     (save-excursion
9017       (set-buffer nntp-server-buffer)
9018       (let ((case-fold-search t)
9019             in-reply-to header p lines)
9020         (goto-char (point-min))
9021         ;; Search to the beginning of the next header.  Error messages
9022         ;; do not begin with 2 or 3.
9023         (while (re-search-forward "^[23][0-9]+ " nil t)
9024           (setq id nil
9025                 ref nil)
9026           ;; This implementation of this function, with nine
9027           ;; search-forwards instead of the one re-search-forward and
9028           ;; a case (which basically was the old function) is actually
9029           ;; about twice as fast, even though it looks messier.  You
9030           ;; can't have everything, I guess.  Speed and elegance
9031           ;; doesn't always go hand in hand.
9032           (setq
9033            header
9034            (vector
9035             ;; Number.
9036             (prog1
9037                 (read cur)
9038               (end-of-line)
9039               (setq p (point))
9040               (narrow-to-region (point)
9041                                 (or (and (search-forward "\n.\n" nil t)
9042                                          (- (point) 2))
9043                                     (point))))
9044             ;; Subject.
9045             (progn
9046               (goto-char p)
9047               (if (search-forward "\nsubject: " nil t)
9048                   (gnus-header-value) "(none)"))
9049             ;; From.
9050             (progn
9051               (goto-char p)
9052               (if (search-forward "\nfrom: " nil t)
9053                   (gnus-header-value) "(nobody)"))
9054             ;; Date.
9055             (progn
9056               (goto-char p)
9057               (if (search-forward "\ndate: " nil t)
9058                   (gnus-header-value) ""))
9059             ;; Message-ID.
9060             (progn
9061               (goto-char p)
9062               (if (search-forward "\nmessage-id: " nil t)
9063                   (setq id (gnus-header-value))
9064                 ;; If there was no message-id, we just fake one to make
9065                 ;; subsequent routines simpler.
9066                 (setq id (concat "none+"
9067                                  (int-to-string
9068                                   (setq gnus-newsgroup-none-id
9069                                         (1+ gnus-newsgroup-none-id)))))))
9070             ;; References.
9071             (progn
9072               (goto-char p)
9073               (if (search-forward "\nreferences: " nil t)
9074                   (prog1
9075                       (gnus-header-value)
9076                     (setq end (match-end 0))
9077                     (save-excursion
9078                       (setq ref
9079                             (buffer-substring
9080                              (progn
9081                                (end-of-line)
9082                                (search-backward ">" end t)
9083                                (1+ (point)))
9084                              (progn
9085                                (search-backward "<" end t)
9086                                (point))))))
9087                 ;; Get the references from the in-reply-to header if there
9088                 ;; were no references and the in-reply-to header looks
9089                 ;; promising.
9090                 (if (and (search-forward "\nin-reply-to: " nil t)
9091                          (setq in-reply-to (gnus-header-value))
9092                          (string-match "<[^>]+>" in-reply-to))
9093                     (setq ref (substring in-reply-to (match-beginning 0)
9094                                          (match-end 0)))
9095                   (setq ref ""))))
9096             ;; Chars.
9097             0
9098             ;; Lines.
9099             (progn
9100               (goto-char p)
9101               (if (search-forward "\nlines: " nil t)
9102                   (if (numberp (setq lines (read cur)))
9103                       lines 0)
9104                 0))
9105             ;; Xref.
9106             (progn
9107               (goto-char p)
9108               (and (search-forward "\nxref: " nil t)
9109                    (gnus-header-value)))))
9110           ;; We do the threading while we read the headers.  The
9111           ;; message-id and the last reference are both entered into
9112           ;; the same hash table.  Some tippy-toeing around has to be
9113           ;; done in case an article has arrived before the article
9114           ;; which it refers to.
9115           (if (boundp (setq id-dep (intern id dependencies)))
9116               (if (and (car (symbol-value id-dep))
9117                        (not force-new))
9118                   ;; An article with this Message-ID has already
9119                   ;; been seen, so we ignore this one, except we add
9120                   ;; any additional Xrefs (in case the two articles
9121                   ;; came from different servers).
9122                   (progn
9123                     (mail-header-set-xref
9124                      (car (symbol-value id-dep))
9125                      (concat (or (mail-header-xref
9126                                   (car (symbol-value id-dep))) "")
9127                              (or (mail-header-xref header) "")))
9128                     (setq header nil))
9129                 (setcar (symbol-value id-dep) header))
9130             (set id-dep (list header)))
9131           (when header
9132             (if (boundp (setq ref-dep (intern ref dependencies)))
9133                 (setcdr (symbol-value ref-dep)
9134                         (nconc (cdr (symbol-value ref-dep))
9135                                (list (symbol-value id-dep))))
9136               (set ref-dep (list nil (symbol-value id-dep))))
9137             (setq headers (cons header headers)))
9138           (goto-char (point-max))
9139           (widen))
9140         (nreverse headers)))))
9141
9142 ;; The following macros and functions were written by Felix Lee
9143 ;; <flee@cse.psu.edu>.
9144
9145 (defmacro gnus-nov-read-integer ()
9146   '(prog1
9147        (if (= (following-char) ?\t)
9148            0
9149          (let ((num (condition-case nil (read buffer) (error nil))))
9150            (if (numberp num) num 0)))
9151      (or (eobp) (forward-char 1))))
9152
9153 (defmacro gnus-nov-skip-field ()
9154   '(search-forward "\t" eol 'move))
9155
9156 (defmacro gnus-nov-field ()
9157   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9158
9159 ;; Goes through the xover lines and returns a list of vectors
9160 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9161                                                   force-new dependencies)
9162   "Parse the news overview data in the server buffer, and return a
9163 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9164   ;; Get the Xref when the users reads the articles since most/some
9165   ;; NNTP servers do not include Xrefs when using XOVER.
9166   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9167   (let ((cur nntp-server-buffer)
9168         (dependencies (or dependencies gnus-newsgroup-dependencies))
9169         number headers header)
9170     (save-excursion
9171       (set-buffer nntp-server-buffer)
9172       ;; Allow the user to mangle the headers before parsing them.
9173       (run-hooks 'gnus-parse-headers-hook)
9174       (goto-char (point-min))
9175       (while (and sequence (not (eobp)))
9176         (setq number (read cur))
9177         (while (and sequence (< (car sequence) number))
9178           (setq sequence (cdr sequence)))
9179         (and sequence
9180              (eq number (car sequence))
9181              (progn
9182                (setq sequence (cdr sequence))
9183                (if (setq header
9184                          (inline (gnus-nov-parse-line
9185                                   number dependencies force-new)))
9186                    (setq headers (cons header headers)))))
9187         (forward-line 1))
9188       (setq headers (nreverse headers)))
9189     headers))
9190
9191 ;; This function has to be called with point after the article number
9192 ;; on the beginning of the line.
9193 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9194   (let ((none 0)
9195         (eol (gnus-point-at-eol))
9196         (buffer (current-buffer))
9197         header ref id id-dep ref-dep)
9198
9199     ;; overview: [num subject from date id refs chars lines misc]
9200     (narrow-to-region (point) eol)
9201     (or (eobp) (forward-char))
9202
9203     (condition-case nil
9204         (setq header
9205               (vector
9206                number                   ; number
9207                (gnus-nov-field)         ; subject
9208                (gnus-nov-field)         ; from
9209                (gnus-nov-field)         ; date
9210                (setq id (or (gnus-nov-field)
9211                             (concat "none+"
9212                                     (int-to-string
9213                                      (setq none (1+ none)))))) ; id
9214                (progn
9215                  (save-excursion
9216                    (let ((beg (point)))
9217                      (search-forward "\t" eol)
9218                      (if (search-backward ">" beg t)
9219                          (setq ref
9220                                (buffer-substring
9221                                 (1+ (point))
9222                                 (search-backward "<" beg t)))
9223                        (setq ref nil))))
9224                  (gnus-nov-field))      ; refs
9225                (gnus-nov-read-integer)  ; chars
9226                (gnus-nov-read-integer)  ; lines
9227                (if (= (following-char) ?\n)
9228                    nil
9229                  (gnus-nov-field))      ; misc
9230                ))
9231       (error (progn
9232                (gnus-error 4 "Strange nov line")
9233                (setq header nil)
9234                (goto-char eol))))
9235
9236     (widen)
9237
9238     ;; We build the thread tree.
9239     (when header
9240       (if (boundp (setq id-dep (intern id dependencies)))
9241           (if (and (car (symbol-value id-dep))
9242                    (not force-new))
9243               ;; An article with this Message-ID has already been seen,
9244               ;; so we ignore this one, except we add any additional
9245               ;; Xrefs (in case the two articles came from different
9246               ;; servers.
9247               (progn
9248                 (mail-header-set-xref
9249                  (car (symbol-value id-dep))
9250                  (concat (or (mail-header-xref
9251                               (car (symbol-value id-dep))) "")
9252                          (or (mail-header-xref header) "")))
9253                 (setq header nil))
9254             (setcar (symbol-value id-dep) header))
9255         (set id-dep (list header))))
9256     (if header
9257         (progn
9258           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9259               (setcdr (symbol-value ref-dep)
9260                       (nconc (cdr (symbol-value ref-dep))
9261                              (list (symbol-value id-dep))))
9262             (set ref-dep (list nil (symbol-value id-dep))))))
9263     header))
9264
9265 (defun gnus-article-get-xrefs ()
9266   "Fill in the Xref value in `gnus-current-headers', if necessary.
9267 This is meant to be called in `gnus-article-internal-prepare-hook'."
9268   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9269                                  gnus-current-headers)))
9270     (or (not gnus-use-cross-reference)
9271         (not headers)
9272         (and (mail-header-xref headers)
9273              (not (string= (mail-header-xref headers) "")))
9274         (let ((case-fold-search t)
9275               xref)
9276           (save-restriction
9277             (nnheader-narrow-to-headers)
9278             (goto-char (point-min))
9279             (if (or (and (eq (downcase (following-char)) ?x)
9280                          (looking-at "Xref:"))
9281                     (search-forward "\nXref:" nil t))
9282                 (progn
9283                   (goto-char (1+ (match-end 0)))
9284                   (setq xref (buffer-substring (point)
9285                                                (progn (end-of-line) (point))))
9286                   (mail-header-set-xref headers xref))))))))
9287
9288 (defun gnus-summary-insert-subject (id &optional old-header)
9289   "Find article ID and insert the summary line for that article."
9290   (let ((header (gnus-read-header id))
9291         (number (and (numberp id) id))
9292         pos)
9293     (when header
9294       ;; Rebuild the thread that this article is part of and go to the
9295       ;; article we have fetched.
9296       (when old-header
9297         (when (setq pos (text-property-any
9298                          (point-min) (point-max) 'gnus-number 
9299                          (mail-header-number old-header)))
9300           (goto-char pos)
9301           (gnus-delete-line)
9302           (gnus-data-remove (mail-header-number old-header))))
9303       (gnus-rebuild-thread (mail-header-id header))
9304       (gnus-summary-goto-subject (setq number (mail-header-number header))))
9305     (when (and (numberp number)
9306                (> number 0))
9307       ;; We have to update the boundaries even if we can't fetch the
9308       ;; article if ID is a number -- so that the next `P' or `N'
9309       ;; command will fetch the previous (or next) article even
9310       ;; if the one we tried to fetch this time has been canceled.
9311       (and (> number gnus-newsgroup-end)
9312            (setq gnus-newsgroup-end number))
9313       (and (< number gnus-newsgroup-begin)
9314            (setq gnus-newsgroup-begin number))
9315       (setq gnus-newsgroup-unselected
9316             (delq number gnus-newsgroup-unselected)))
9317     ;; Report back a success?
9318     (and header (mail-header-number header))))
9319
9320 (defun gnus-summary-work-articles (n)
9321   "Return a list of articles to be worked upon.  The prefix argument,
9322 the list of process marked articles, and the current article will be
9323 taken into consideration."
9324   (cond
9325    ((and n (numberp n))
9326     ;; A numerical prefix has been given.
9327     (let ((backward (< n 0))
9328           (n (abs n))
9329           articles article)
9330       (save-excursion
9331         (while
9332             (and (> n 0)
9333                  (push (setq article (gnus-summary-article-number))
9334                        articles)
9335                  (if backward
9336                      (gnus-summary-find-prev nil article)
9337                    (gnus-summary-find-next nil article)))
9338           (decf n)))
9339       (nreverse articles)))
9340    ((and (boundp 'transient-mark-mode)
9341          transient-mark-mode
9342          mark-active)
9343     ;; Work on the region between point and mark.
9344     (let ((max (max (point) (mark)))
9345           articles article)
9346       (save-excursion
9347         (goto-char (min (point) (mark)))
9348         (while
9349             (and
9350              (push (setq article (gnus-summary-article-number)) articles)
9351              (gnus-summary-find-next nil article)
9352              (< (point) max)))
9353         (nreverse articles))))
9354    (gnus-newsgroup-processable
9355     ;; There are process-marked articles present.
9356     (reverse gnus-newsgroup-processable))
9357    (t
9358     ;; Just return the current article.
9359     (list (gnus-summary-article-number)))))
9360
9361 (defun gnus-summary-search-group (&optional backward use-level)
9362   "Search for next unread newsgroup.
9363 If optional argument BACKWARD is non-nil, search backward instead."
9364   (save-excursion
9365     (set-buffer gnus-group-buffer)
9366     (if (gnus-group-search-forward
9367          backward nil (if use-level (gnus-group-group-level) nil))
9368         (gnus-group-group-name))))
9369
9370 (defun gnus-summary-best-group (&optional exclude-group)
9371   "Find the name of the best unread group.
9372 If EXCLUDE-GROUP, do not go to this group."
9373   (save-excursion
9374     (set-buffer gnus-group-buffer)
9375     (save-excursion
9376       (gnus-group-best-unread-group exclude-group))))
9377
9378 (defun gnus-summary-find-next (&optional unread article backward)
9379   (if backward (gnus-summary-find-prev)
9380     (let* ((dummy (gnus-summary-article-intangible-p))
9381            (article (or article (gnus-summary-article-number)))
9382            (arts (gnus-data-find-list article))
9383            result)
9384       (when (and (not dummy)
9385                  (or (not gnus-summary-check-current)
9386                      (not unread)
9387                      (not (gnus-data-unread-p (car arts)))))
9388         (setq arts (cdr arts)))
9389       (when (setq result
9390                   (if unread
9391                       (progn
9392                         (while arts
9393                           (when (gnus-data-unread-p (car arts))
9394                             (setq result (car arts)
9395                                   arts nil))
9396                           (setq arts (cdr arts)))
9397                         result)
9398                     (car arts)))
9399         (goto-char (gnus-data-pos result))
9400         (gnus-data-number result)))))
9401
9402 (defun gnus-summary-find-prev (&optional unread article)
9403   (let* ((article (or article (gnus-summary-article-number)))
9404          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9405          result)
9406     (when (or (not gnus-summary-check-current)
9407               (not unread)
9408               (not (gnus-data-unread-p (car arts))))
9409       (setq arts (cdr arts)))
9410     (if (setq result
9411               (if unread
9412                   (progn
9413                     (while arts
9414                       (and (gnus-data-unread-p (car arts))
9415                            (setq result (car arts)
9416                                  arts nil))
9417                       (setq arts (cdr arts)))
9418                     result)
9419                 (car arts)))
9420         (progn
9421           (goto-char (gnus-data-pos result))
9422           (gnus-data-number result)))))
9423
9424 (defun gnus-summary-find-subject (subject &optional unread backward article)
9425   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9426          (article (or article (gnus-summary-article-number)))
9427          (articles (gnus-data-list backward))
9428          (arts (gnus-data-find-list article articles))
9429          result)
9430     (when (or (not gnus-summary-check-current)
9431               (not unread)
9432               (not (gnus-data-unread-p (car arts))))
9433       (setq arts (cdr arts)))
9434     (while arts
9435       (and (or (not unread)
9436                (gnus-data-unread-p (car arts)))
9437            (vectorp (gnus-data-header (car arts)))
9438            (gnus-subject-equal
9439             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9440            (setq result (car arts)
9441                  arts nil))
9442       (setq arts (cdr arts)))
9443     (and result
9444          (goto-char (gnus-data-pos result))
9445          (gnus-data-number result))))
9446
9447 (defun gnus-summary-search-forward (&optional unread subject backward)
9448   "Search forward for an article.
9449 If UNREAD, look for unread articles.  If SUBJECT, look for
9450 articles with that subject.  If BACKWARD, search backward instead."
9451   (cond (subject (gnus-summary-find-subject subject unread backward))
9452         (backward (gnus-summary-find-prev unread))
9453         (t (gnus-summary-find-next unread))))
9454
9455 (defun gnus-recenter (&optional n)
9456   "Center point in window and redisplay frame.
9457 Also do horizontal recentering."
9458   (interactive "P")
9459   (when (and gnus-auto-center-summary
9460              (not (eq gnus-auto-center-summary 'vertical)))
9461     (gnus-horizontal-recenter))
9462   (recenter n))
9463
9464 (defun gnus-summary-recenter ()
9465   "Center point in the summary window.
9466 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9467 displayed, no centering will be performed."
9468   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9469   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9470   (let* ((top (cond ((< (window-height) 4) 0)
9471                     ((< (window-height) 7) 1)
9472                     (t 2)))
9473          (height (1- (window-height)))
9474          (bottom (save-excursion (goto-char (point-max))
9475                                  (forward-line (- height))
9476                                  (point)))
9477          (window (get-buffer-window (current-buffer))))
9478     ;; The user has to want it.
9479     (when gnus-auto-center-summary
9480       (when (get-buffer-window gnus-article-buffer)
9481        ;; Only do recentering when the article buffer is displayed,
9482        ;; Set the window start to either `bottom', which is the biggest
9483        ;; possible valid number, or the second line from the top,
9484        ;; whichever is the least.
9485        (set-window-start
9486         window (min bottom (save-excursion 
9487                              (forward-line (- top)) (point)))))
9488       ;; Do horizontal recentering while we're at it.
9489       (when (and (get-buffer-window (current-buffer) t)
9490                  (not (eq gnus-auto-center-summary 'vertical)))
9491         (let ((selected (selected-window)))
9492           (select-window (get-buffer-window (current-buffer) t))
9493           (gnus-summary-position-point)
9494           (gnus-horizontal-recenter)
9495           (select-window selected))))))
9496
9497 (defun gnus-horizontal-recenter ()
9498   "Recenter the current buffer horizontally."
9499   (if (< (current-column) (/ (window-width) 2))
9500       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9501     (let* ((orig (point))
9502            (end (window-end (get-buffer-window (current-buffer) t)))
9503            (max 0))
9504       ;; Find the longest line currently displayed in the window.
9505       (goto-char (window-start))
9506       (while (and (not (eobp)) 
9507                   (< (point) end))
9508         (end-of-line)
9509         (setq max (max max (current-column)))
9510         (forward-line 1))
9511       (goto-char orig)
9512       ;; Scroll horizontally to center (sort of) the point.
9513       (if (> max (window-width))
9514           (set-window-hscroll 
9515            (get-buffer-window (current-buffer) t)
9516            (min (- (current-column) (/ (window-width) 3))
9517                 (+ 2 (- max (window-width)))))
9518         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9519       max)))
9520
9521 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9522 (defun gnus-short-group-name (group &optional levels)
9523   "Collapse GROUP name LEVELS."
9524   (let* ((name "") 
9525          (foreign "")
9526          (depth 0) 
9527          (skip 1)
9528          (levels (or levels
9529                      (progn
9530                        (while (string-match "\\." group skip)
9531                          (setq skip (match-end 0)
9532                                depth (+ depth 1)))
9533                        depth))))
9534     (if (string-match ":" group)
9535         (setq foreign (substring group 0 (match-end 0))
9536               group (substring group (match-end 0))))
9537     (while group
9538       (if (and (string-match "\\." group)
9539                (> levels (- gnus-group-uncollapsed-levels 1)))
9540           (setq name (concat name (substring group 0 1))
9541                 group (substring group (match-end 0))
9542                 levels (- levels 1)
9543                 name (concat name "."))
9544         (setq name (concat foreign name group)
9545               group nil)))
9546     name))
9547
9548 (defun gnus-summary-jump-to-group (newsgroup)
9549   "Move point to NEWSGROUP in group mode buffer."
9550   ;; Keep update point of group mode buffer if visible.
9551   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9552       (save-window-excursion
9553         ;; Take care of tree window mode.
9554         (if (get-buffer-window gnus-group-buffer)
9555             (pop-to-buffer gnus-group-buffer))
9556         (gnus-group-jump-to-group newsgroup))
9557     (save-excursion
9558       ;; Take care of tree window mode.
9559       (if (get-buffer-window gnus-group-buffer)
9560           (pop-to-buffer gnus-group-buffer)
9561         (set-buffer gnus-group-buffer))
9562       (gnus-group-jump-to-group newsgroup))))
9563
9564 ;; This function returns a list of article numbers based on the
9565 ;; difference between the ranges of read articles in this group and
9566 ;; the range of active articles.
9567 (defun gnus-list-of-unread-articles (group)
9568   (let* ((read (gnus-info-read (gnus-get-info group)))
9569          (active (gnus-active group))
9570          (last (cdr active))
9571          first nlast unread)
9572     ;; If none are read, then all are unread.
9573     (if (not read)
9574         (setq first (car active))
9575       ;; If the range of read articles is a single range, then the
9576       ;; first unread article is the article after the last read
9577       ;; article.  Sounds logical, doesn't it?
9578       (if (not (listp (cdr read)))
9579           (setq first (1+ (cdr read)))
9580         ;; `read' is a list of ranges.
9581         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9582                                 (caar read))) 1)
9583             (setq first 1))
9584         (while read
9585           (if first
9586               (while (< first nlast)
9587                 (setq unread (cons first unread))
9588                 (setq first (1+ first))))
9589           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9590           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9591           (setq read (cdr read)))))
9592     ;; And add the last unread articles.
9593     (while (<= first last)
9594       (setq unread (cons first unread))
9595       (setq first (1+ first)))
9596     ;; Return the list of unread articles.
9597     (nreverse unread)))
9598
9599 (defun gnus-list-of-read-articles (group)
9600   "Return a list of unread, unticked and non-dormant articles."
9601   (let* ((info (gnus-get-info group))
9602          (marked (gnus-info-marks info))
9603          (active (gnus-active group)))
9604     (and info active
9605          (gnus-set-difference
9606           (gnus-sorted-complement
9607            (gnus-uncompress-range active)
9608            (gnus-list-of-unread-articles group))
9609           (append
9610            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9611            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9612
9613 ;; Various summary commands
9614
9615 (defun gnus-summary-universal-argument (arg)
9616   "Perform any operation on all articles that are process/prefixed."
9617   (interactive "P")
9618   (gnus-set-global-variables)
9619   (let ((articles (gnus-summary-work-articles arg))
9620         func article)
9621     (if (eq
9622          (setq
9623           func
9624           (key-binding
9625            (read-key-sequence
9626             (substitute-command-keys
9627              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9628              ))))
9629          'undefined)
9630         (gnus-error 1 "Undefined key")
9631       (save-excursion
9632         (while articles
9633           (gnus-summary-goto-subject (setq article (pop articles)))
9634           (command-execute func)
9635           (gnus-summary-remove-process-mark article)))))
9636   (gnus-summary-position-point))
9637
9638 (defun gnus-summary-toggle-truncation (&optional arg)
9639   "Toggle truncation of summary lines.
9640 With arg, turn line truncation on iff arg is positive."
9641   (interactive "P")
9642   (setq truncate-lines
9643         (if (null arg) (not truncate-lines)
9644           (> (prefix-numeric-value arg) 0)))
9645   (redraw-display))
9646
9647 (defun gnus-summary-reselect-current-group (&optional all rescan)
9648   "Exit and then reselect the current newsgroup.
9649 The prefix argument ALL means to select all articles."
9650   (interactive "P")
9651   (gnus-set-global-variables)
9652   (let ((current-subject (gnus-summary-article-number))
9653         (group gnus-newsgroup-name))
9654     (setq gnus-newsgroup-begin nil)
9655     (gnus-summary-exit)
9656     ;; We have to adjust the point of group mode buffer because the
9657     ;; current point was moved to the next unread newsgroup by
9658     ;; exiting.
9659     (gnus-summary-jump-to-group group)
9660     (when rescan
9661       (save-excursion
9662         (gnus-group-get-new-news-this-group 1)))
9663     (gnus-group-read-group all t)
9664     (gnus-summary-goto-subject current-subject)))
9665
9666 (defun gnus-summary-rescan-group (&optional all)
9667   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9668   (interactive "P")
9669   (gnus-summary-reselect-current-group all t))
9670
9671 (defun gnus-summary-update-info ()
9672   (let* ((group gnus-newsgroup-name))
9673     (when gnus-newsgroup-kill-headers
9674       (setq gnus-newsgroup-killed
9675             (gnus-compress-sequence
9676              (nconc
9677               (gnus-set-sorted-intersection
9678                (gnus-uncompress-range gnus-newsgroup-killed)
9679                (setq gnus-newsgroup-unselected
9680                      (sort gnus-newsgroup-unselected '<)))
9681               (setq gnus-newsgroup-unreads
9682                     (sort gnus-newsgroup-unreads '<))) t)))
9683     (unless (listp (cdr gnus-newsgroup-killed))
9684       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9685     (let ((headers gnus-newsgroup-headers))
9686       (run-hooks 'gnus-exit-group-hook)
9687       (unless gnus-save-score
9688         (setq gnus-newsgroup-scored nil))
9689       ;; Set the new ranges of read articles.
9690       (gnus-update-read-articles
9691        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9692       ;; Set the current article marks.
9693       (gnus-update-marks)
9694       ;; Do the cross-ref thing.
9695       (when gnus-use-cross-reference
9696         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9697       ;; Do adaptive scoring, and possibly save score files.
9698       (when gnus-newsgroup-adaptive
9699         (gnus-score-adaptive))
9700       (when gnus-use-scoring
9701         (gnus-score-save))
9702       ;; Do not switch windows but change the buffer to work.
9703       (set-buffer gnus-group-buffer)
9704       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9705           (gnus-group-update-group group)))))
9706
9707 (defun gnus-summary-exit (&optional temporary)
9708   "Exit reading current newsgroup, and then return to group selection mode.
9709 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9710   (interactive)
9711   (gnus-set-global-variables)
9712   (gnus-kill-save-kill-buffer)
9713   (let* ((group gnus-newsgroup-name)
9714          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9715          (mode major-mode)
9716          (buf (current-buffer)))
9717     (run-hooks 'gnus-summary-prepare-exit-hook)
9718     ;; If we have several article buffers, we kill them at exit.
9719     (unless gnus-single-article-buffer
9720       (gnus-kill-buffer gnus-article-buffer)
9721       (gnus-kill-buffer gnus-original-article-buffer)
9722       (setq gnus-article-current nil))
9723     (when gnus-use-cache
9724       (gnus-cache-possibly-remove-articles)
9725       (gnus-cache-save-buffers))
9726     (when gnus-use-trees
9727       (gnus-tree-close group))
9728     ;; Make all changes in this group permanent.
9729     (unless quit-config
9730       (gnus-summary-update-info))
9731     (gnus-close-group group)
9732     ;; Make sure where I was, and go to next newsgroup.
9733     (set-buffer gnus-group-buffer)
9734     (unless quit-config
9735       (gnus-group-jump-to-group group)
9736       (gnus-group-next-unread-group 1))
9737     (run-hooks 'gnus-summary-exit-hook)
9738     (unless gnus-single-article-buffer
9739       (setq gnus-article-current nil))
9740     (if temporary
9741         nil                             ;Nothing to do.
9742       ;; If we have several article buffers, we kill them at exit.
9743       (unless gnus-single-article-buffer
9744         (gnus-kill-buffer gnus-article-buffer)
9745         (gnus-kill-buffer gnus-original-article-buffer)
9746         (setq gnus-article-current nil))
9747       (set-buffer buf)
9748       (if (not gnus-kill-summary-on-exit)
9749           (gnus-deaden-summary)
9750         ;; We set all buffer-local variables to nil.  It is unclear why
9751         ;; this is needed, but if we don't, buffer-local variables are
9752         ;; not garbage-collected, it seems.  This would the lead to en
9753         ;; ever-growing Emacs.
9754         (gnus-summary-clear-local-variables)
9755         (when (get-buffer gnus-article-buffer)
9756           (bury-buffer gnus-article-buffer))
9757         ;; We clear the global counterparts of the buffer-local
9758         ;; variables as well, just to be on the safe side.
9759         (gnus-configure-windows 'group 'force)
9760         (gnus-summary-clear-local-variables)
9761         ;; Return to group mode buffer.
9762         (if (eq mode 'gnus-summary-mode)
9763             (gnus-kill-buffer buf)))
9764       (setq gnus-current-select-method gnus-select-method)
9765       (pop-to-buffer gnus-group-buffer)
9766       ;; Clear the current group name.
9767       (if (not quit-config)
9768           (progn
9769             (gnus-group-jump-to-group group)
9770             (gnus-group-next-unread-group 1)
9771             (gnus-configure-windows 'group 'force))
9772         (if (not (buffer-name (car quit-config)))
9773             (gnus-configure-windows 'group 'force)
9774           (set-buffer (car quit-config))
9775           (and (eq major-mode 'gnus-summary-mode)
9776                (gnus-set-global-variables))
9777           (gnus-configure-windows (cdr quit-config))))
9778       (unless quit-config
9779         (setq gnus-newsgroup-name nil)))))
9780
9781 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9782 (defun gnus-summary-exit-no-update (&optional no-questions)
9783   "Quit reading current newsgroup without updating read article info."
9784   (interactive)
9785   (gnus-set-global-variables)
9786   (let* ((group gnus-newsgroup-name)
9787          (quit-config (gnus-group-quit-config group)))
9788     (when (or no-questions
9789               gnus-expert-user
9790               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9791       ;; If we have several article buffers, we kill them at exit.
9792       (unless gnus-single-article-buffer
9793         (gnus-kill-buffer gnus-article-buffer)
9794         (gnus-kill-buffer gnus-original-article-buffer)
9795         (setq gnus-article-current nil))
9796       (if (not gnus-kill-summary-on-exit)
9797           (gnus-deaden-summary)
9798         (gnus-close-group group)
9799         (gnus-summary-clear-local-variables)
9800         (set-buffer gnus-group-buffer)
9801         (gnus-summary-clear-local-variables)
9802         (when (get-buffer gnus-summary-buffer)
9803           (kill-buffer gnus-summary-buffer)))
9804       (unless gnus-single-article-buffer
9805         (setq gnus-article-current nil))
9806       (when gnus-use-trees
9807         (gnus-tree-close group))
9808       (when (get-buffer gnus-article-buffer)
9809         (bury-buffer gnus-article-buffer))
9810       ;; Return to the group buffer.
9811       (gnus-configure-windows 'group 'force)
9812       ;; Clear the current group name.
9813       (setq gnus-newsgroup-name nil)
9814       (when (equal (gnus-group-group-name) group)
9815         (gnus-group-next-unread-group 1))
9816       (when quit-config
9817         (if (not (buffer-name (car quit-config)))
9818             (gnus-configure-windows 'group 'force)
9819           (set-buffer (car quit-config))
9820           (when (eq major-mode 'gnus-summary-mode)
9821             (gnus-set-global-variables))
9822           (gnus-configure-windows (cdr quit-config)))))))
9823
9824 ;;; Dead summaries.
9825
9826 (defvar gnus-dead-summary-mode-map nil)
9827
9828 (if gnus-dead-summary-mode-map
9829     nil
9830   (setq gnus-dead-summary-mode-map (make-keymap))
9831   (suppress-keymap gnus-dead-summary-mode-map)
9832   (substitute-key-definition
9833    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9834   (let ((keys '("\C-d" "\r" "\177")))
9835     (while keys
9836       (define-key gnus-dead-summary-mode-map
9837         (pop keys) 'gnus-summary-wake-up-the-dead))))
9838
9839 (defvar gnus-dead-summary-mode nil
9840   "Minor mode for Gnus summary buffers.")
9841
9842 (defun gnus-dead-summary-mode (&optional arg)
9843   "Minor mode for Gnus summary buffers."
9844   (interactive "P")
9845   (when (eq major-mode 'gnus-summary-mode)
9846     (make-local-variable 'gnus-dead-summary-mode)
9847     (setq gnus-dead-summary-mode
9848           (if (null arg) (not gnus-dead-summary-mode)
9849             (> (prefix-numeric-value arg) 0)))
9850     (when gnus-dead-summary-mode
9851       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9852         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9853       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9854         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9855               minor-mode-map-alist)))))
9856
9857 (defun gnus-deaden-summary ()
9858   "Make the current summary buffer into a dead summary buffer."
9859   ;; Kill any previous dead summary buffer.
9860   (when (and gnus-dead-summary
9861              (buffer-name gnus-dead-summary))
9862     (save-excursion
9863       (set-buffer gnus-dead-summary)
9864       (when gnus-dead-summary-mode
9865         (kill-buffer (current-buffer)))))
9866   ;; Make this the current dead summary.
9867   (setq gnus-dead-summary (current-buffer))
9868   (gnus-dead-summary-mode 1)
9869   (let ((name (buffer-name)))
9870     (when (string-match "Summary" name)
9871       (rename-buffer
9872        (concat (substring name 0 (match-beginning 0)) "Dead "
9873                (substring name (match-beginning 0))) t))))
9874
9875 (defun gnus-kill-or-deaden-summary (buffer)
9876   "Kill or deaden the summary BUFFER."
9877   (cond (gnus-kill-summary-on-exit
9878          (when (and gnus-use-trees
9879                     (and (get-buffer buffer)
9880                          (buffer-name (get-buffer buffer))))
9881            (save-excursion
9882              (set-buffer (get-buffer buffer))
9883              (gnus-tree-close gnus-newsgroup-name)))
9884          (gnus-kill-buffer buffer))
9885         ((and (get-buffer buffer)
9886               (buffer-name (get-buffer buffer)))
9887          (save-excursion
9888            (set-buffer buffer)
9889            (gnus-deaden-summary)))))
9890
9891 (defun gnus-summary-wake-up-the-dead (&rest args)
9892   "Wake up the dead summary buffer."
9893   (interactive)
9894   (gnus-dead-summary-mode -1)
9895   (let ((name (buffer-name)))
9896     (when (string-match "Dead " name)
9897       (rename-buffer
9898        (concat (substring name 0 (match-beginning 0))
9899                (substring name (match-end 0))) t)))
9900   (gnus-message 3 "This dead summary is now alive again"))
9901
9902 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9903 (defun gnus-summary-fetch-faq (&optional faq-dir)
9904   "Fetch the FAQ for the current group.
9905 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9906 in."
9907   (interactive
9908    (list
9909     (if current-prefix-arg
9910         (completing-read
9911          "Faq dir: " (and (listp gnus-group-faq-directory)
9912                           gnus-group-faq-directory)))))
9913   (let (gnus-faq-buffer)
9914     (and (setq gnus-faq-buffer
9915                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9916          (gnus-configure-windows 'summary-faq))))
9917
9918 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9919 (defun gnus-summary-describe-group (&optional force)
9920   "Describe the current newsgroup."
9921   (interactive "P")
9922   (gnus-group-describe-group force gnus-newsgroup-name))
9923
9924 (defun gnus-summary-describe-briefly ()
9925   "Describe summary mode commands briefly."
9926   (interactive)
9927   (gnus-message 6
9928                 (substitute-command-keys "\\<gnus-summary-mode-map>\\[gnus-summary-next-page]:Select  \\[gnus-summary-next-unread-article]:Forward  \\[gnus-summary-prev-unread-article]:Backward  \\[gnus-summary-exit]:Exit  \\[gnus-info-find-node]:Run Info  \\[gnus-summary-describe-briefly]:This help")))
9929
9930 ;; Walking around group mode buffer from summary mode.
9931
9932 (defun gnus-summary-next-group (&optional no-article target-group backward)
9933   "Exit current newsgroup and then select next unread newsgroup.
9934 If prefix argument NO-ARTICLE is non-nil, no article is selected
9935 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9936 previous group instead."
9937   (interactive "P")
9938   (gnus-set-global-variables)
9939   (let ((current-group gnus-newsgroup-name)
9940         (current-buffer (current-buffer))
9941         entered)
9942     ;; First we semi-exit this group to update Xrefs and all variables.
9943     ;; We can't do a real exit, because the window conf must remain
9944     ;; the same in case the user is prompted for info, and we don't
9945     ;; want the window conf to change before that...
9946     (gnus-summary-exit t)
9947     (while (not entered)
9948       ;; Then we find what group we are supposed to enter.
9949       (set-buffer gnus-group-buffer)
9950       (gnus-group-jump-to-group current-group)
9951       (setq target-group
9952             (or target-group
9953                 (if (eq gnus-keep-same-level 'best)
9954                     (gnus-summary-best-group gnus-newsgroup-name)
9955                   (gnus-summary-search-group backward gnus-keep-same-level))))
9956       (if (not target-group)
9957           ;; There are no further groups, so we return to the group
9958           ;; buffer.
9959           (progn
9960             (gnus-message 5 "Returning to the group buffer")
9961             (setq entered t)
9962             (set-buffer current-buffer)
9963             (gnus-summary-exit))
9964         ;; We try to enter the target group.
9965         (gnus-group-jump-to-group target-group)
9966         (let ((unreads (gnus-group-group-unread)))
9967           (if (and (or (eq t unreads)
9968                        (and unreads (not (zerop unreads))))
9969                    (gnus-summary-read-group
9970                     target-group nil no-article current-buffer))
9971               (setq entered t)
9972             (setq current-group target-group
9973                   target-group nil)))))))
9974
9975 (defun gnus-summary-prev-group (&optional no-article)
9976   "Exit current newsgroup and then select previous unread newsgroup.
9977 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
9978   (interactive "P")
9979   (gnus-summary-next-group no-article nil t))
9980
9981 ;; Walking around summary lines.
9982
9983 (defun gnus-summary-first-subject (&optional unread)
9984   "Go to the first unread subject.
9985 If UNREAD is non-nil, go to the first unread article.
9986 Returns the article selected or nil if there are no unread articles."
9987   (interactive "P")
9988   (prog1
9989       (cond
9990        ;; Empty summary.
9991        ((null gnus-newsgroup-data)
9992         (gnus-message 3 "No articles in the group")
9993         nil)
9994        ;; Pick the first article.
9995        ((not unread)
9996         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
9997         (gnus-data-number (car gnus-newsgroup-data)))
9998        ;; No unread articles.
9999        ((null gnus-newsgroup-unreads)
10000         (gnus-message 3 "No more unread articles")
10001         nil)
10002        ;; Find the first unread article.
10003        (t
10004         (let ((data gnus-newsgroup-data))
10005           (while (and data
10006                       (not (gnus-data-unread-p (car data))))
10007             (setq data (cdr data)))
10008           (if data
10009               (progn
10010                 (goto-char (gnus-data-pos (car data)))
10011                 (gnus-data-number (car data)))))))
10012     (gnus-summary-position-point)))
10013
10014 (defun gnus-summary-next-subject (n &optional unread dont-display)
10015   "Go to next N'th summary line.
10016 If N is negative, go to the previous N'th subject line.
10017 If UNREAD is non-nil, only unread articles are selected.
10018 The difference between N and the actual number of steps taken is
10019 returned."
10020   (interactive "p")
10021   (let ((backward (< n 0))
10022         (n (abs n)))
10023     (while (and (> n 0)
10024                 (if backward
10025                     (gnus-summary-find-prev unread)
10026                   (gnus-summary-find-next unread)))
10027       (setq n (1- n)))
10028     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10029                                (if unread " unread" "")))
10030     (unless dont-display
10031       (gnus-summary-recenter)
10032       (gnus-summary-position-point))
10033     n))
10034
10035 (defun gnus-summary-next-unread-subject (n)
10036   "Go to next N'th unread summary line."
10037   (interactive "p")
10038   (gnus-summary-next-subject n t))
10039
10040 (defun gnus-summary-prev-subject (n &optional unread)
10041   "Go to previous N'th summary line.
10042 If optional argument UNREAD is non-nil, only unread article is selected."
10043   (interactive "p")
10044   (gnus-summary-next-subject (- n) unread))
10045
10046 (defun gnus-summary-prev-unread-subject (n)
10047   "Go to previous N'th unread summary line."
10048   (interactive "p")
10049   (gnus-summary-next-subject (- n) t))
10050
10051 (defun gnus-summary-goto-subject (article &optional force silent)
10052   "Go the subject line of ARTICLE.
10053 If FORCE, also allow jumping to articles not currently shown."
10054   (let ((b (point))
10055         (data (gnus-data-find article)))
10056     ;; We read in the article if we have to.
10057     (and (not data)
10058          force
10059          (gnus-summary-insert-subject article)
10060          (setq data (gnus-data-find article)))
10061     (goto-char b)
10062     (if (not data)
10063         (progn
10064           (unless silent
10065             (gnus-message 3 "Can't find article %d" article))
10066           nil)
10067       (goto-char (gnus-data-pos data))
10068       article)))
10069
10070 ;; Walking around summary lines with displaying articles.
10071
10072 (defun gnus-summary-expand-window (&optional arg)
10073   "Make the summary buffer take up the entire Emacs frame.
10074 Given a prefix, will force an `article' buffer configuration."
10075   (interactive "P")
10076   (gnus-set-global-variables)
10077   (if arg
10078       (gnus-configure-windows 'article 'force)
10079     (gnus-configure-windows 'summary 'force)))
10080
10081 (defun gnus-summary-display-article (article &optional all-header)
10082   "Display ARTICLE in article buffer."
10083   (gnus-set-global-variables)
10084   (if (null article)
10085       nil
10086     (prog1
10087         (if gnus-summary-display-article-function
10088             (funcall gnus-summary-display-article-function article all-header)
10089           (gnus-article-prepare article all-header))
10090       (run-hooks 'gnus-select-article-hook)
10091       (gnus-summary-recenter)
10092       (gnus-summary-goto-subject article)
10093       (when gnus-use-trees
10094         (gnus-possibly-generate-tree article)
10095         (gnus-highlight-selected-tree article))
10096       ;; Successfully display article.
10097       (gnus-article-set-window-start
10098        (cdr (assq article gnus-newsgroup-bookmarks)))
10099       t)))
10100
10101 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10102   "Select the current article.
10103 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10104 non-nil, the article will be re-fetched even if it already present in
10105 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10106 be displayed."
10107   (let ((article (or article (gnus-summary-article-number)))
10108         (all-headers (not (not all-headers))) ;Must be T or NIL.
10109         gnus-summary-display-article-function
10110         did)
10111     (and (not pseudo)
10112          (gnus-summary-article-pseudo-p article)
10113          (error "This is a pseudo-article."))
10114     (prog1
10115         (save-excursion
10116           (set-buffer gnus-summary-buffer)
10117           (if (or (and gnus-single-article-buffer
10118                        (or (null gnus-current-article)
10119                            (null gnus-article-current)
10120                            (null (get-buffer gnus-article-buffer))
10121                            (not (eq article (cdr gnus-article-current)))
10122                            (not (equal (car gnus-article-current)
10123                                        gnus-newsgroup-name))))
10124                   (and (not gnus-single-article-buffer)
10125                        (or (null gnus-current-article)
10126                            (not (eq gnus-current-article article))))
10127                   force)
10128               ;; The requested article is different from the current article.
10129               (prog1
10130                   (gnus-summary-display-article article all-headers)
10131                 (setq did article))
10132             (if (or all-headers gnus-show-all-headers)
10133                 (gnus-article-show-all-headers))
10134             'old))
10135       (if did
10136           (gnus-article-set-window-start
10137            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10138
10139 (defun gnus-summary-set-current-mark (&optional current-mark)
10140   "Obsolete function."
10141   nil)
10142
10143 (defun gnus-summary-next-article (&optional unread subject backward push)
10144   "Select the next article.
10145 If UNREAD, only unread articles are selected.
10146 If SUBJECT, only articles with SUBJECT are selected.
10147 If BACKWARD, the previous article is selected instead of the next."
10148   (interactive "P")
10149   (gnus-set-global-variables)
10150   (cond
10151    ;; Is there such an article?
10152    ((and (gnus-summary-search-forward unread subject backward)
10153          (or (gnus-summary-display-article (gnus-summary-article-number))
10154              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10155     (gnus-summary-position-point))
10156    ;; If not, we try the first unread, if that is wanted.
10157    ((and subject
10158          gnus-auto-select-same
10159          (or (gnus-summary-first-unread-article)
10160              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10161     (gnus-summary-position-point)
10162     (gnus-message 6 "Wrapped"))
10163    ;; Try to get next/previous article not displayed in this group.
10164    ((and gnus-auto-extend-newsgroup
10165          (not unread) (not subject))
10166     (gnus-summary-goto-article
10167      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10168      nil t))
10169    ;; Go to next/previous group.
10170    (t
10171     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10172         (gnus-summary-jump-to-group gnus-newsgroup-name))
10173     (let ((cmd last-command-char)
10174           (group
10175            (if (eq gnus-keep-same-level 'best)
10176                (gnus-summary-best-group gnus-newsgroup-name)
10177              (gnus-summary-search-group backward gnus-keep-same-level))))
10178       ;; For some reason, the group window gets selected.  We change
10179       ;; it back.
10180       (select-window (get-buffer-window (current-buffer)))
10181       ;; Select next unread newsgroup automagically.
10182       (cond
10183        ((not gnus-auto-select-next)
10184         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10185        ((or (eq gnus-auto-select-next 'quietly)
10186             (and (eq gnus-auto-select-next 'slightly-quietly)
10187                  push)
10188             (and (eq gnus-auto-select-next 'almost-quietly)
10189                  (gnus-summary-last-article-p)))
10190         ;; Select quietly.
10191         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10192             (gnus-summary-exit)
10193           (gnus-message 7 "No more%s articles (%s)..."
10194                         (if unread " unread" "")
10195                         (if group (concat "selecting " group)
10196                           "exiting"))
10197           (gnus-summary-next-group nil group backward)))
10198        (t
10199         (gnus-summary-walk-group-buffer
10200          gnus-newsgroup-name cmd unread backward)))))))
10201
10202 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10203   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10204                       (?\C-p (gnus-group-prev-unread-group 1))))
10205         keve key group ended)
10206     (save-excursion
10207       (set-buffer gnus-group-buffer)
10208       (gnus-summary-jump-to-group from-group)
10209       (setq group
10210             (if (eq gnus-keep-same-level 'best)
10211                 (gnus-summary-best-group gnus-newsgroup-name)
10212               (gnus-summary-search-group backward gnus-keep-same-level))))
10213     (while (not ended)
10214       (gnus-message
10215        5 "No more%s articles%s" (if unread " unread" "")
10216        (if (and group
10217                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10218            (format " (Type %s for %s [%s])"
10219                    (single-key-description cmd) group
10220                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10221          (format " (Type %s to exit %s)"
10222                  (single-key-description cmd)
10223                  gnus-newsgroup-name)))
10224       ;; Confirm auto selection.
10225       (setq key (car (setq keve (gnus-read-event-char))))
10226       (setq ended t)
10227       (cond
10228        ((assq key keystrokes)
10229         (let ((obuf (current-buffer)))
10230           (switch-to-buffer gnus-group-buffer)
10231           (and group
10232                (gnus-group-jump-to-group group))
10233           (eval (cadr (assq key keystrokes)))
10234           (setq group (gnus-group-group-name))
10235           (switch-to-buffer obuf))
10236         (setq ended nil))
10237        ((equal key cmd)
10238         (if (or (not group)
10239                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10240             (gnus-summary-exit)
10241           (gnus-summary-next-group nil group backward)))
10242        (t
10243         (push (cdr keve) unread-command-events))))))
10244
10245 (defun gnus-read-event-char ()
10246   "Get the next event."
10247   (let ((event (read-event)))
10248     (cons (and (numberp event) event) event)))
10249
10250 (defun gnus-summary-next-unread-article ()
10251   "Select unread article after current one."
10252   (interactive)
10253   (gnus-summary-next-article t (and gnus-auto-select-same
10254                                     (gnus-summary-article-subject))))
10255
10256 (defun gnus-summary-prev-article (&optional unread subject)
10257   "Select the article after the current one.
10258 If UNREAD is non-nil, only unread articles are selected."
10259   (interactive "P")
10260   (gnus-summary-next-article unread subject t))
10261
10262 (defun gnus-summary-prev-unread-article ()
10263   "Select unred article before current one."
10264   (interactive)
10265   (gnus-summary-prev-article t (and gnus-auto-select-same
10266                                     (gnus-summary-article-subject))))
10267
10268 (defun gnus-summary-next-page (&optional lines circular)
10269   "Show next page of the selected article.
10270 If at the end of the current article, select the next article.
10271 LINES says how many lines should be scrolled up.
10272
10273 If CIRCULAR is non-nil, go to the start of the article instead of
10274 selecting the next article when reaching the end of the current
10275 article."
10276   (interactive "P")
10277   (setq gnus-summary-buffer (current-buffer))
10278   (gnus-set-global-variables)
10279   (let ((article (gnus-summary-article-number))
10280         (endp nil))
10281     (gnus-configure-windows 'article)
10282     (if (or (null gnus-current-article)
10283             (null gnus-article-current)
10284             (/= article (cdr gnus-article-current))
10285             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10286         ;; Selected subject is different from current article's.
10287         (gnus-summary-display-article article)
10288       (gnus-eval-in-buffer-window
10289        gnus-article-buffer
10290        (setq endp (gnus-article-next-page lines)))
10291       (if endp
10292           (cond (circular
10293                  (gnus-summary-beginning-of-article))
10294                 (lines
10295                  (gnus-message 3 "End of message"))
10296                 ((null lines)
10297                  (if (and (eq gnus-summary-goto-unread 'never)
10298                           (not (gnus-summary-last-article-p article)))
10299                      (gnus-summary-next-article)
10300                    (gnus-summary-next-unread-article))))))
10301     (gnus-summary-recenter)
10302     (gnus-summary-position-point)))
10303
10304 (defun gnus-summary-prev-page (&optional lines)
10305   "Show previous page of selected article.
10306 Argument LINES specifies lines to be scrolled down."
10307   (interactive "P")
10308   (gnus-set-global-variables)
10309   (let ((article (gnus-summary-article-number)))
10310     (gnus-configure-windows 'article)
10311     (if (or (null gnus-current-article)
10312             (null gnus-article-current)
10313             (/= article (cdr gnus-article-current))
10314             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10315         ;; Selected subject is different from current article's.
10316         (gnus-summary-display-article article)
10317       (gnus-summary-recenter)
10318       (gnus-eval-in-buffer-window gnus-article-buffer
10319                                   (gnus-article-prev-page lines))))
10320   (gnus-summary-position-point))
10321
10322 (defun gnus-summary-scroll-up (lines)
10323   "Scroll up (or down) one line current article.
10324 Argument LINES specifies lines to be scrolled up (or down if negative)."
10325   (interactive "p")
10326   (gnus-set-global-variables)
10327   (gnus-configure-windows 'article)
10328   (gnus-summary-show-thread)
10329   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10330     (gnus-eval-in-buffer-window
10331      gnus-article-buffer
10332      (cond ((> lines 0)
10333             (if (gnus-article-next-page lines)
10334                 (gnus-message 3 "End of message")))
10335            ((< lines 0)
10336             (gnus-article-prev-page (- lines))))))
10337   (gnus-summary-recenter)
10338   (gnus-summary-position-point))
10339
10340 (defun gnus-summary-next-same-subject ()
10341   "Select next article which has the same subject as current one."
10342   (interactive)
10343   (gnus-set-global-variables)
10344   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10345
10346 (defun gnus-summary-prev-same-subject ()
10347   "Select previous article which has the same subject as current one."
10348   (interactive)
10349   (gnus-set-global-variables)
10350   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10351
10352 (defun gnus-summary-next-unread-same-subject ()
10353   "Select next unread article which has the same subject as current one."
10354   (interactive)
10355   (gnus-set-global-variables)
10356   (gnus-summary-next-article t (gnus-summary-article-subject)))
10357
10358 (defun gnus-summary-prev-unread-same-subject ()
10359   "Select previous unread article which has the same subject as current one."
10360   (interactive)
10361   (gnus-set-global-variables)
10362   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10363
10364 (defun gnus-summary-first-unread-article ()
10365   "Select the first unread article.
10366 Return nil if there are no unread articles."
10367   (interactive)
10368   (gnus-set-global-variables)
10369   (prog1
10370       (if (gnus-summary-first-subject t)
10371           (progn
10372             (gnus-summary-show-thread)
10373             (gnus-summary-first-subject t)
10374             (gnus-summary-display-article (gnus-summary-article-number))))
10375     (gnus-summary-position-point)))
10376
10377 (defun gnus-summary-best-unread-article ()
10378   "Select the unread article with the highest score."
10379   (interactive)
10380   (gnus-set-global-variables)
10381   (let ((best -1000000)
10382         (data gnus-newsgroup-data)
10383         article score)
10384     (while data
10385       (and (gnus-data-unread-p (car data))
10386            (> (setq score
10387                     (gnus-summary-article-score (gnus-data-number (car data))))
10388               best)
10389            (setq best score
10390                  article (gnus-data-number (car data))))
10391       (setq data (cdr data)))
10392     (prog1
10393         (if article
10394             (gnus-summary-goto-article article)
10395           (error "No unread articles"))
10396       (gnus-summary-position-point))))
10397
10398 (defun gnus-summary-last-subject ()
10399   "Go to the last displayed subject line in the group."
10400   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10401     (when article
10402       (gnus-summary-goto-subject article))))
10403
10404 (defun gnus-summary-goto-article (article &optional all-headers force)
10405   "Fetch ARTICLE and display it if it exists.
10406 If ALL-HEADERS is non-nil, no header lines are hidden."
10407   (interactive
10408    (list
10409     (string-to-int
10410      (completing-read
10411       "Article number: "
10412       (mapcar (lambda (number) (list (int-to-string number)))
10413               gnus-newsgroup-limit)))
10414     current-prefix-arg
10415     t))
10416   (prog1
10417       (if (gnus-summary-goto-subject article force)
10418           (gnus-summary-display-article article all-headers)
10419         (gnus-message 4 "Couldn't go to article %s" article) nil)
10420     (gnus-summary-position-point)))
10421
10422 (defun gnus-summary-goto-last-article ()
10423   "Go to the previously read article."
10424   (interactive)
10425   (prog1
10426       (and gnus-last-article
10427            (gnus-summary-goto-article gnus-last-article))
10428     (gnus-summary-position-point)))
10429
10430 (defun gnus-summary-pop-article (number)
10431   "Pop one article off the history and go to the previous.
10432 NUMBER articles will be popped off."
10433   (interactive "p")
10434   (let (to)
10435     (setq gnus-newsgroup-history
10436           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10437     (if to
10438         (gnus-summary-goto-article (car to))
10439       (error "Article history empty")))
10440   (gnus-summary-position-point))
10441
10442 ;; Summary commands and functions for limiting the summary buffer.
10443
10444 (defun gnus-summary-limit-to-articles (n)
10445   "Limit the summary buffer to the next N articles.
10446 If not given a prefix, use the process marked articles instead."
10447   (interactive "P")
10448   (gnus-set-global-variables)
10449   (prog1
10450       (let ((articles (gnus-summary-work-articles n)))
10451         (setq gnus-newsgroup-processable nil)
10452         (gnus-summary-limit articles))
10453     (gnus-summary-position-point)))
10454
10455 (defun gnus-summary-pop-limit (&optional total)
10456   "Restore the previous limit.
10457 If given a prefix, remove all limits."
10458   (interactive "P")
10459   (gnus-set-global-variables)
10460   (when total 
10461     (setq gnus-newsgroup-limits
10462           (list (mapcar (lambda (h) (mail-header-number h))
10463                         gnus-newsgroup-headers))))
10464   (unless gnus-newsgroup-limits
10465     (error "No limit to pop"))
10466   (prog1
10467       (gnus-summary-limit nil 'pop)
10468     (gnus-summary-position-point)))
10469
10470 (defun gnus-summary-limit-to-subject (subject &optional header)
10471   "Limit the summary buffer to articles that have subjects that match a regexp."
10472   (interactive "sRegexp: ")
10473   (unless header
10474     (setq header "subject"))
10475   (when (not (equal "" subject))
10476     (prog1
10477         (let ((articles (gnus-summary-find-matching
10478                          (or header "subject") subject 'all)))
10479           (or articles (error "Found no matches for \"%s\"" subject))
10480           (gnus-summary-limit articles))
10481       (gnus-summary-position-point))))
10482
10483 (defun gnus-summary-limit-to-author (from)
10484   "Limit the summary buffer to articles that have authors that match a regexp."
10485   (interactive "sRegexp: ")
10486   (gnus-summary-limit-to-subject from "from"))
10487
10488 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10489 (make-obsolete
10490  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10491
10492 (defun gnus-summary-limit-to-unread (&optional all)
10493   "Limit the summary buffer to articles that are not marked as read.
10494 If ALL is non-nil, limit strictly to unread articles."
10495   (interactive "P")
10496   (if all
10497       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10498     (gnus-summary-limit-to-marks
10499      ;; Concat all the marks that say that an article is read and have
10500      ;; those removed.
10501      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10502            gnus-killed-mark gnus-kill-file-mark
10503            gnus-low-score-mark gnus-expirable-mark
10504            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10505      'reverse)))
10506
10507 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10508 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10509
10510 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10511   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10512 If REVERSE, limit the summary buffer to articles that are not marked
10513 with MARKS.  MARKS can either be a string of marks or a list of marks.
10514 Returns how many articles were removed."
10515   (interactive "sMarks: ")
10516   (gnus-set-global-variables)
10517   (prog1
10518       (let ((data gnus-newsgroup-data)
10519             (marks (if (listp marks) marks
10520                      (append marks nil))) ; Transform to list.
10521             articles)
10522         (while data
10523           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10524                  (memq (gnus-data-mark (car data)) marks))
10525                (setq articles (cons (gnus-data-number (car data)) articles)))
10526           (setq data (cdr data)))
10527         (gnus-summary-limit articles))
10528     (gnus-summary-position-point)))
10529
10530 (defun gnus-summary-limit-to-score (&optional score)
10531   "Limit to articles with score at or above SCORE."
10532   (interactive "P")
10533   (gnus-set-global-variables)
10534   (setq score (if score
10535                   (prefix-numeric-value score)
10536                 (or gnus-summary-default-score 0)))
10537   (let ((data gnus-newsgroup-data)
10538         articles)
10539     (while data
10540       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10541                 score)
10542         (push (gnus-data-number (car data)) articles))
10543       (setq data (cdr data)))
10544     (prog1
10545         (gnus-summary-limit articles)
10546       (gnus-summary-position-point))))
10547
10548 (defun gnus-summary-limit-include-dormant ()
10549   "Display all the hidden articles that are marked as dormant."
10550   (interactive)
10551   (gnus-set-global-variables)
10552   (or gnus-newsgroup-dormant
10553       (error "There are no dormant articles in this group"))
10554   (prog1
10555       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10556     (gnus-summary-position-point)))
10557
10558 (defun gnus-summary-limit-exclude-dormant ()
10559   "Hide all dormant articles."
10560   (interactive)
10561   (gnus-set-global-variables)
10562   (prog1
10563       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10564     (gnus-summary-position-point)))
10565
10566 (defun gnus-summary-limit-exclude-childless-dormant ()
10567   "Hide all dormant articles that have no children."
10568   (interactive)
10569   (gnus-set-global-variables)
10570   (let ((data (gnus-data-list t))
10571         articles d children)
10572     ;; Find all articles that are either not dormant or have
10573     ;; children.
10574     (while (setq d (pop data))
10575       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10576                 (and (setq children 
10577                            (gnus-article-children (gnus-data-number d)))
10578                      (let (found)
10579                        (while children
10580                          (when (memq (car children) articles)
10581                            (setq children nil
10582                                  found t))
10583                          (pop children))
10584                        found)))
10585         (push (gnus-data-number d) articles)))
10586     ;; Do the limiting.
10587     (prog1
10588         (gnus-summary-limit articles)
10589       (gnus-summary-position-point))))
10590
10591 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10592   "Mark all unread excluded articles as read.
10593 If ALL, mark even excluded ticked and dormants as read."
10594   (interactive "P")
10595   (let ((articles (gnus-sorted-complement
10596                    (sort
10597                     (mapcar (lambda (h) (mail-header-number h))
10598                             gnus-newsgroup-headers)
10599                     '<)
10600                    (sort gnus-newsgroup-limit '<)))
10601         article)
10602     (setq gnus-newsgroup-unreads nil)
10603     (if all
10604         (setq gnus-newsgroup-dormant nil
10605               gnus-newsgroup-marked nil
10606               gnus-newsgroup-reads
10607               (nconc
10608                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10609                gnus-newsgroup-reads))
10610       (while (setq article (pop articles))
10611         (unless (or (memq article gnus-newsgroup-dormant)
10612                     (memq article gnus-newsgroup-marked))
10613           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10614
10615 (defun gnus-summary-limit (articles &optional pop)
10616   (if pop
10617       ;; We pop the previous limit off the stack and use that.
10618       (setq articles (car gnus-newsgroup-limits)
10619             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10620     ;; We use the new limit, so we push the old limit on the stack.
10621     (setq gnus-newsgroup-limits
10622           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10623   ;; Set the limit.
10624   (setq gnus-newsgroup-limit articles)
10625   (let ((total (length gnus-newsgroup-data))
10626         (data (gnus-data-find-list (gnus-summary-article-number)))
10627         found)
10628     ;; This will do all the work of generating the new summary buffer
10629     ;; according to the new limit.
10630     (gnus-summary-prepare)
10631     ;; Hide any threads, possibly.
10632     (and gnus-show-threads
10633          gnus-thread-hide-subtree
10634          (gnus-summary-hide-all-threads))
10635     ;; Try to return to the article you were at, or one in the
10636     ;; neighborhood.
10637     (if data
10638         ;; We try to find some article after the current one.
10639         (while data
10640           (and (gnus-summary-goto-subject
10641                 (gnus-data-number (car data)) nil t)
10642                (setq data nil
10643                      found t))
10644           (setq data (cdr data))))
10645     (or found
10646         ;; If there is no data, that means that we were after the last
10647         ;; article.  The same goes when we can't find any articles
10648         ;; after the current one.
10649         (progn
10650           (goto-char (point-max))
10651           (gnus-summary-find-prev)))
10652     ;; We return how many articles were removed from the summary
10653     ;; buffer as a result of the new limit.
10654     (- total (length gnus-newsgroup-data))))
10655
10656 (defsubst gnus-cut-thread (thread)
10657   "Go forwards in the thread until we find an article that we want to display."
10658   (when (eq gnus-fetch-old-headers 'some)
10659     ;; Deal with old-fetched headers.
10660     (while (and thread
10661                 (memq (mail-header-number (car thread)) 
10662                       gnus-newsgroup-ancient)
10663                 (<= (length (cdr thread)) 1))
10664       (setq thread (cadr thread))))
10665   ;; Deal with sparse threads.
10666   (when (or (eq gnus-build-sparse-threads 'some)
10667             (eq gnus-build-sparse-threads 'more))
10668     (while (and thread
10669                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10670                 (= (length (cdr thread)) 1))
10671       (setq thread (cadr thread))))
10672   thread)
10673
10674 (defun gnus-cut-threads (threads)
10675   "Cut off all uninteresting articles from the beginning of threads."
10676   (when (or (eq gnus-fetch-old-headers 'some)
10677             (eq gnus-build-sparse-threads 'some)
10678             (eq gnus-build-sparse-threads 'more))
10679     (let ((th threads))
10680       (while th
10681         (setcar th (gnus-cut-thread (car th)))
10682         (setq th (cdr th)))))
10683   ;; Remove nixed out threads.
10684   (delq nil threads))
10685
10686 (defun gnus-summary-initial-limit (&optional show-if-empty)
10687   "Figure out what the initial limit is supposed to be on group entry.
10688 This entails weeding out unwanted dormants, low-scored articles,
10689 fetch-old-headers verbiage, and so on."
10690   ;; Most groups have nothing to remove.
10691   (if (or gnus-inhibit-limiting
10692           (and (null gnus-newsgroup-dormant)
10693                (not (eq gnus-fetch-old-headers 'some))
10694                (null gnus-summary-expunge-below)
10695                (not (eq gnus-build-sparse-threads 'some))
10696                (not (eq gnus-build-sparse-threads 'more))
10697                (null gnus-thread-expunge-below)
10698                (not gnus-use-nocem)))
10699       () ; Do nothing.
10700     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10701     (setq gnus-newsgroup-limit nil)
10702     (mapatoms
10703      (lambda (node)
10704        (unless (car (symbol-value node))
10705          ;; These threads have no parents -- they are roots.
10706          (let ((nodes (cdr (symbol-value node)))
10707                thread)
10708            (while nodes
10709              (if (and gnus-thread-expunge-below
10710                       (< (gnus-thread-total-score (car nodes))
10711                          gnus-thread-expunge-below))
10712                  (gnus-expunge-thread (pop nodes))
10713                (setq thread (pop nodes))
10714                (gnus-summary-limit-children thread))))))
10715      gnus-newsgroup-dependencies)
10716     ;; If this limitation resulted in an empty group, we might
10717     ;; pop the previous limit and use it instead.
10718     (when (and (not gnus-newsgroup-limit)
10719                show-if-empty)
10720       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10721     gnus-newsgroup-limit))
10722
10723 (defun gnus-summary-limit-children (thread)
10724   "Return 1 if this subthread is visible and 0 if it is not."
10725   ;; First we get the number of visible children to this thread.  This
10726   ;; is done by recursing down the thread using this function, so this
10727   ;; will really go down to a leaf article first, before slowly
10728   ;; working its way up towards the root.
10729   (when thread
10730     (let ((children
10731            (if (cdr thread)
10732                (apply '+ (mapcar 'gnus-summary-limit-children
10733                                  (cdr thread)))
10734              0))
10735           (number (mail-header-number (car thread)))
10736           score)
10737       (if (or
10738            ;; If this article is dormant and has absolutely no visible
10739            ;; children, then this article isn't visible.
10740            (and (memq number gnus-newsgroup-dormant)
10741                 (= children 0))
10742            ;; If this is a "fetch-old-headered" and there is only one
10743            ;; visible child (or less), then we don't want this article.
10744            (and (eq gnus-fetch-old-headers 'some)
10745                 (memq number gnus-newsgroup-ancient)
10746                 (zerop children))
10747            ;; If this is a sparsely inserted article with no children,
10748            ;; we don't want it.
10749            (and (eq gnus-build-sparse-threads 'some)
10750                 (memq number gnus-newsgroup-sparse)
10751                 (zerop children))
10752            ;; If we use expunging, and this article is really
10753            ;; low-scored, then we don't want this article.
10754            (when (and gnus-summary-expunge-below
10755                       (< (setq score
10756                                (or (cdr (assq number gnus-newsgroup-scored))
10757                                    gnus-summary-default-score))
10758                          gnus-summary-expunge-below))
10759              ;; We increase the expunge-tally here, but that has
10760              ;; nothing to do with the limits, really.
10761              (incf gnus-newsgroup-expunged-tally)
10762              ;; We also mark as read here, if that's wanted.
10763              (when (and gnus-summary-mark-below
10764                         (< score gnus-summary-mark-below))
10765                (setq gnus-newsgroup-unreads
10766                      (delq number gnus-newsgroup-unreads))
10767                (if gnus-newsgroup-auto-expire
10768                    (push number gnus-newsgroup-expirable)
10769                  (push (cons number gnus-low-score-mark)
10770                        gnus-newsgroup-reads)))
10771              t)
10772            (and gnus-use-nocem
10773                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10774           ;; Nope, invisible article.
10775           0
10776         ;; Ok, this article is to be visible, so we add it to the limit
10777         ;; and return 1.
10778         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10779         1))))
10780
10781 (defun gnus-expunge-thread (thread)
10782   "Mark all articles in THREAD as read."
10783   (let* ((number (mail-header-number (car thread))))
10784     (incf gnus-newsgroup-expunged-tally)
10785     ;; We also mark as read here, if that's wanted.
10786     (setq gnus-newsgroup-unreads
10787           (delq number gnus-newsgroup-unreads))
10788     (if gnus-newsgroup-auto-expire
10789         (push number gnus-newsgroup-expirable)
10790       (push (cons number gnus-low-score-mark)
10791             gnus-newsgroup-reads)))
10792   ;; Go recursively through all subthreads.
10793   (mapcar 'gnus-expunge-thread (cdr thread)))
10794
10795 ;; Summary article oriented commands
10796
10797 (defun gnus-summary-refer-parent-article (n)
10798   "Refer parent article N times.
10799 The difference between N and the number of articles fetched is returned."
10800   (interactive "p")
10801   (gnus-set-global-variables)
10802   (while
10803       (and
10804        (> n 0)
10805        (let* ((header (gnus-summary-article-header))
10806               (ref
10807                ;; If we try to find the parent of the currently
10808                ;; displayed article, then we take a look at the actual
10809                ;; References header, since this is slightly more
10810                ;; reliable than the References field we got from the
10811                ;; server.
10812                (if (and (eq (mail-header-number header)
10813                             (cdr gnus-article-current))
10814                         (equal gnus-newsgroup-name
10815                                (car gnus-article-current)))
10816                    (save-excursion
10817                      (set-buffer gnus-original-article-buffer)
10818                      (nnheader-narrow-to-headers)
10819                      (prog1
10820                          (mail-fetch-field "references")
10821                        (widen)))
10822                  ;; It's not the current article, so we take a bet on
10823                  ;; the value we got from the server.
10824                  (mail-header-references header))))
10825          (if (setq ref (or ref (mail-header-references header)))
10826              (or (gnus-summary-refer-article (gnus-parent-id ref))
10827                  (gnus-message 1 "Couldn't find parent"))
10828            (gnus-message 1 "No references in article %d"
10829                          (gnus-summary-article-number))
10830            nil)))
10831     (setq n (1- n)))
10832   (gnus-summary-position-point)
10833   n)
10834
10835 (defun gnus-summary-refer-references ()
10836   "Fetch all articles mentioned in the References header.
10837 Return how many articles were fetched."
10838   (interactive)
10839   (gnus-set-global-variables)
10840   (let ((ref (mail-header-references (gnus-summary-article-header)))
10841         (current (gnus-summary-article-number))
10842         (n 0))
10843     ;; For each Message-ID in the References header...
10844     (while (string-match "<[^>]*>" ref)
10845       (incf n)
10846       ;; ... fetch that article.
10847       (gnus-summary-refer-article
10848        (prog1 (match-string 0 ref)
10849          (setq ref (substring ref (match-end 0))))))
10850     (gnus-summary-goto-subject current)
10851     (gnus-summary-position-point)
10852     n))
10853
10854 (defun gnus-summary-refer-article (message-id)
10855   "Fetch an article specified by MESSAGE-ID."
10856   (interactive "sMessage-ID: ")
10857   (when (and (stringp message-id)
10858              (not (zerop (length message-id))))
10859     ;; Construct the correct Message-ID if necessary.
10860     ;; Suggested by tale@pawl.rpi.edu.
10861     (unless (string-match "^<" message-id)
10862       (setq message-id (concat "<" message-id)))
10863     (unless (string-match ">$" message-id)
10864       (setq message-id (concat message-id ">")))
10865     (let ((header (car (gnus-gethash message-id
10866                                      gnus-newsgroup-dependencies))))
10867       (if header
10868           ;; The article is present in the buffer, to we just go to it.
10869           (gnus-summary-goto-article (mail-header-number header) nil t)
10870         ;; We fetch the article
10871         (let ((gnus-override-method 
10872                (and (gnus-news-group-p gnus-newsgroup-name)
10873                     gnus-refer-article-method))
10874               number)
10875           ;; Start the special refer-article method, if necessary.
10876           (when gnus-refer-article-method
10877             (gnus-check-server gnus-refer-article-method))
10878           ;; Fetch the header, and display the article.
10879           (if (setq number (gnus-summary-insert-subject message-id))
10880               (gnus-summary-select-article nil nil nil number)
10881             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10882
10883 (defun gnus-summary-enter-digest-group (&optional force)
10884   "Enter a digest group based on the current article."
10885   (interactive "P")
10886   (gnus-set-global-variables)
10887   (gnus-summary-select-article)
10888   (let ((name (format "%s-%d"
10889                       (gnus-group-prefixed-name
10890                        gnus-newsgroup-name (list 'nndoc ""))
10891                       gnus-current-article))
10892         (ogroup gnus-newsgroup-name)
10893         (case-fold-search t)
10894         (buf (current-buffer))
10895         dig)
10896     (save-excursion
10897       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10898       (insert-buffer-substring gnus-original-article-buffer)
10899       (narrow-to-region
10900        (goto-char (point-min))
10901        (or (search-forward "\n\n" nil t) (point)))
10902       (goto-char (point-min))
10903       (delete-matching-lines "^\\(Path\\):\\|^From ")
10904       (widen))
10905     (unwind-protect
10906         (if (gnus-group-read-ephemeral-group
10907              name `(nndoc ,name (nndoc-address
10908                                  ,(get-buffer dig))
10909                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10910             ;; Make all postings to this group go to the parent group.
10911             (nconc (gnus-info-params (gnus-get-info name))
10912                    (list (cons 'to-group ogroup)))
10913           ;; Couldn't select this doc group.
10914           (switch-to-buffer buf)
10915           (gnus-set-global-variables)
10916           (gnus-configure-windows 'summary)
10917           (gnus-message 3 "Article couldn't be entered?"))
10918       (kill-buffer dig))))
10919
10920 (defun gnus-summary-isearch-article (&optional regexp-p)
10921   "Do incremental search forward on the current article.
10922 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10923   (interactive "P")
10924   (gnus-set-global-variables)
10925   (gnus-summary-select-article)
10926   (gnus-configure-windows 'article)
10927   (gnus-eval-in-buffer-window
10928    gnus-article-buffer
10929    (goto-char (point-min))
10930    (isearch-forward regexp-p)))
10931
10932 (defun gnus-summary-search-article-forward (regexp &optional backward)
10933   "Search for an article containing REGEXP forward.
10934 If BACKWARD, search backward instead."
10935   (interactive
10936    (list (read-string
10937           (format "Search article %s (regexp%s): "
10938                   (if current-prefix-arg "backward" "forward")
10939                   (if gnus-last-search-regexp
10940                       (concat ", default " gnus-last-search-regexp)
10941                     "")))
10942          current-prefix-arg))
10943   (gnus-set-global-variables)
10944   (if (string-equal regexp "")
10945       (setq regexp (or gnus-last-search-regexp ""))
10946     (setq gnus-last-search-regexp regexp))
10947   (if (gnus-summary-search-article regexp backward)
10948       (gnus-article-set-window-start
10949        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10950     (error "Search failed: \"%s\"" regexp)))
10951
10952 (defun gnus-summary-search-article-backward (regexp)
10953   "Search for an article containing REGEXP backward."
10954   (interactive
10955    (list (read-string
10956           (format "Search article backward (regexp%s): "
10957                   (if gnus-last-search-regexp
10958                       (concat ", default " gnus-last-search-regexp)
10959                     "")))))
10960   (gnus-summary-search-article-forward regexp 'backward))
10961
10962 (defun gnus-summary-search-article (regexp &optional backward)
10963   "Search for an article containing REGEXP.
10964 Optional argument BACKWARD means do search for backward.
10965 gnus-select-article-hook is not called during the search."
10966   (let ((gnus-select-article-hook nil)  ;Disable hook.
10967         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10968         (re-search
10969          (if backward
10970              (function re-search-backward) (function re-search-forward)))
10971         (found nil)
10972         (last nil))
10973     ;; Hidden thread subtrees must be searched for ,too.
10974     (gnus-summary-show-all-threads)
10975     ;; First of all, search current article.
10976     ;; We don't want to read article again from NNTP server nor reset
10977     ;; current point.
10978     (gnus-summary-select-article)
10979     (gnus-message 9 "Searching article: %d..." gnus-current-article)
10980     (setq last gnus-current-article)
10981     (gnus-eval-in-buffer-window
10982      gnus-article-buffer
10983      (save-restriction
10984        (widen)
10985        ;; Begin search from current point.
10986        (setq found (funcall re-search regexp nil t))))
10987     ;; Then search next articles.
10988     (while (and (not found)
10989                 (gnus-summary-display-article
10990                  (if backward (gnus-summary-find-prev)
10991                    (gnus-summary-find-next))))
10992       (gnus-message 9 "Searching article: %d..." gnus-current-article)
10993       (gnus-eval-in-buffer-window
10994        gnus-article-buffer
10995        (save-restriction
10996          (widen)
10997          (goto-char (if backward (point-max) (point-min)))
10998          (setq found (funcall re-search regexp nil t)))))
10999     (message "")
11000     ;; Adjust article pointer.
11001     (or (eq last gnus-current-article)
11002         (setq gnus-last-article last))
11003     ;; Return T if found such article.
11004     found))
11005
11006 (defun gnus-summary-find-matching (header regexp &optional backward unread
11007                                           not-case-fold)
11008   "Return a list of all articles that match REGEXP on HEADER.
11009 The search stars on the current article and goes forwards unless
11010 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11011 If UNREAD is non-nil, only unread articles will
11012 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11013 in the comparisons."
11014   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11015                 (gnus-data-find-list
11016                  (gnus-summary-article-number) (gnus-data-list backward))))
11017         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11018         (case-fold-search (not not-case-fold))
11019         articles d)
11020     (or (fboundp (intern (concat "mail-header-" header)))
11021         (error "%s is not a valid header" header))
11022     (while data
11023       (setq d (car data))
11024       (and (or (not unread)             ; We want all articles...
11025                (gnus-data-unread-p d))  ; Or just unreads.
11026            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11027            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11028            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11029       (setq data (cdr data)))
11030     (nreverse articles)))
11031
11032 (defun gnus-summary-execute-command (header regexp command &optional backward)
11033   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11034 If HEADER is an empty string (or nil), the match is done on the entire
11035 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11036   (interactive
11037    (list (let ((completion-ignore-case t))
11038            (completing-read
11039             "Header name: "
11040             (mapcar (lambda (string) (list string))
11041                     '("Number" "Subject" "From" "Lines" "Date"
11042                       "Message-ID" "Xref" "References" "Body"))
11043             nil 'require-match))
11044          (read-string "Regexp: ")
11045          (read-key-sequence "Command: ")
11046          current-prefix-arg))
11047   (when (equal header "Body")
11048     (setq header ""))
11049   (gnus-set-global-variables)
11050   ;; Hidden thread subtrees must be searched as well.
11051   (gnus-summary-show-all-threads)
11052   ;; We don't want to change current point nor window configuration.
11053   (save-excursion
11054     (save-window-excursion
11055       (gnus-message 6 "Executing %s..." (key-description command))
11056       ;; We'd like to execute COMMAND interactively so as to give arguments.
11057       (gnus-execute header regexp
11058                     `(lambda () (call-interactively ',(key-binding command)))
11059                     backward)
11060       (gnus-message 6 "Executing %s...done" (key-description command)))))
11061
11062 (defun gnus-summary-beginning-of-article ()
11063   "Scroll the article back to the beginning."
11064   (interactive)
11065   (gnus-set-global-variables)
11066   (gnus-summary-select-article)
11067   (gnus-configure-windows 'article)
11068   (gnus-eval-in-buffer-window
11069    gnus-article-buffer
11070    (widen)
11071    (goto-char (point-min))
11072    (and gnus-break-pages (gnus-narrow-to-page))))
11073
11074 (defun gnus-summary-end-of-article ()
11075   "Scroll to the end of the article."
11076   (interactive)
11077   (gnus-set-global-variables)
11078   (gnus-summary-select-article)
11079   (gnus-configure-windows 'article)
11080   (gnus-eval-in-buffer-window
11081    gnus-article-buffer
11082    (widen)
11083    (goto-char (point-max))
11084    (recenter -3)
11085    (and gnus-break-pages (gnus-narrow-to-page))))
11086
11087 (defun gnus-summary-show-article (&optional arg)
11088   "Force re-fetching of the current article.
11089 If ARG (the prefix) is non-nil, show the raw article without any
11090 article massaging functions being run."
11091   (interactive "P")
11092   (gnus-set-global-variables)
11093   (if (not arg)
11094       ;; Select the article the normal way.
11095       (gnus-summary-select-article nil 'force)
11096     ;; Bind the article treatment functions to nil.
11097     (let ((gnus-have-all-headers t)
11098           gnus-article-display-hook
11099           gnus-article-prepare-hook
11100           gnus-break-pages
11101           gnus-visual)
11102       (gnus-summary-select-article nil 'force)))
11103 ;  (gnus-configure-windows 'article)
11104   (gnus-summary-position-point))
11105
11106 (defun gnus-summary-verbose-headers (&optional arg)
11107   "Toggle permanent full header display.
11108 If ARG is a positive number, turn header display on.
11109 If ARG is a negative number, turn header display off."
11110   (interactive "P")
11111   (gnus-set-global-variables)
11112   (gnus-summary-toggle-header arg)
11113   (setq gnus-show-all-headers
11114         (cond ((or (not (numberp arg))
11115                    (zerop arg))
11116                (not gnus-show-all-headers))
11117               ((natnump arg)
11118                t))))
11119
11120 (defun gnus-summary-toggle-header (&optional arg)
11121   "Show the headers if they are hidden, or hide them if they are shown.
11122 If ARG is a positive number, show the entire header.
11123 If ARG is a negative number, hide the unwanted header lines."
11124   (interactive "P")
11125   (gnus-set-global-variables)
11126   (save-excursion
11127     (set-buffer gnus-article-buffer)
11128     (let* ((buffer-read-only nil)
11129            (inhibit-point-motion-hooks t)
11130            (hidden (text-property-any
11131                     (goto-char (point-min)) (search-forward "\n\n")
11132                     'invisible t))
11133            e)
11134       (goto-char (point-min))
11135       (when (search-forward "\n\n" nil t)
11136         (delete-region (point-min) (1- (point))))
11137       (goto-char (point-min))
11138       (save-excursion
11139         (set-buffer gnus-original-article-buffer)
11140         (goto-char (point-min))
11141         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11142       (insert-buffer-substring gnus-original-article-buffer 1 e)
11143       (let ((gnus-inhibit-hiding t))
11144         (run-hooks 'gnus-article-display-hook))
11145       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11146           (gnus-article-hide-headers)))))
11147
11148 (defun gnus-summary-show-all-headers ()
11149   "Make all header lines visible."
11150   (interactive)
11151   (gnus-set-global-variables)
11152   (gnus-article-show-all-headers))
11153
11154 (defun gnus-summary-toggle-mime (&optional arg)
11155   "Toggle MIME processing.
11156 If ARG is a positive number, turn MIME processing on."
11157   (interactive "P")
11158   (gnus-set-global-variables)
11159   (setq gnus-show-mime
11160         (if (null arg) (not gnus-show-mime)
11161           (> (prefix-numeric-value arg) 0)))
11162   (gnus-summary-select-article t 'force))
11163
11164 (defun gnus-summary-caesar-message (&optional arg)
11165   "Caesar rotate the current article by 13.
11166 The numerical prefix specifies how manu places to rotate each letter
11167 forward."
11168   (interactive "P")
11169   (gnus-set-global-variables)
11170   (gnus-summary-select-article)
11171   (let ((mail-header-separator ""))
11172     (gnus-eval-in-buffer-window
11173      gnus-article-buffer
11174      (save-restriction
11175        (widen)
11176        (let ((start (window-start)))
11177          (news-caesar-buffer-body arg)
11178          (set-window-start (get-buffer-window (current-buffer)) start))))))
11179
11180 (defun gnus-summary-stop-page-breaking ()
11181   "Stop page breaking in the current article."
11182   (interactive)
11183   (gnus-set-global-variables)
11184   (gnus-summary-select-article)
11185   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11186
11187 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11188   "Move the current article to a different newsgroup.
11189 If N is a positive number, move the N next articles.
11190 If N is a negative number, move the N previous articles.
11191 If N is nil and any articles have been marked with the process mark,
11192 move those articles instead.
11193 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11194 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11195 re-spool using this method.
11196
11197 For this function to work, both the current newsgroup and the
11198 newsgroup that you want to move to have to support the `request-move'
11199 and `request-accept' functions."
11200   (interactive "P")
11201   (unless action (setq action 'move))
11202   (gnus-set-global-variables)
11203   ;; Check whether the source group supports the required functions.
11204   (cond ((and (eq action 'move)
11205               (not (gnus-check-backend-function
11206                     'request-move-article gnus-newsgroup-name)))
11207          (error "The current group does not support article moving"))
11208         ((and (eq action 'crosspost)
11209               (not (gnus-check-backend-function
11210                     'request-replace-article gnus-newsgroup-name)))
11211          (error "The current group does not support article editing")))
11212   (let ((articles (gnus-summary-work-articles n))
11213         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11214         (names '((move "move" "Moving")
11215                  (copy "copy" "Copying")
11216                  (crosspost "crosspost" "Crossposting")))
11217         (copy-buf (save-excursion
11218                     (nnheader-set-temp-buffer " *copy article*")))
11219         art-group to-method new-xref article to-groups)
11220     (unless (assq action names)
11221       (error "Unknown action %s" action))
11222     ;; Read the newsgroup name.
11223     (when (and (not to-newsgroup)
11224                (not select-method))
11225       (setq to-newsgroup
11226             (gnus-read-move-group-name
11227              (cadr (assq action names))
11228              gnus-current-move-group articles prefix))
11229       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11230     (setq to-method (or select-method 
11231                         (gnus-find-method-for-group to-newsgroup)))
11232     ;; Check the method we are to move this article to...
11233     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11234         (error "%s does not support article copying" (car to-method)))
11235     (or (gnus-check-server to-method)
11236         (error "Can't open server %s" (car to-method)))
11237     (gnus-message 6 "%s to %s: %s..."
11238                   (caddr (assq action names))
11239                   (or (car select-method) to-newsgroup) articles)
11240     (while articles
11241       (setq article (pop articles))
11242       (setq
11243        art-group
11244        (cond
11245         ;; Move the article.
11246         ((eq action 'move)
11247          (gnus-request-move-article
11248           article                       ; Article to move
11249           gnus-newsgroup-name           ; From newsgrouo
11250           (nth 1 (gnus-find-method-for-group
11251                   gnus-newsgroup-name)) ; Server
11252           (list 'gnus-request-accept-article
11253                 to-newsgroup (list 'quote select-method)
11254                 (not articles))         ; Accept form
11255           (not articles)))              ; Only save nov last time
11256         ;; Copy the article.
11257         ((eq action 'copy)
11258          (save-excursion
11259            (set-buffer copy-buf)
11260            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11261            (gnus-request-accept-article
11262             to-newsgroup select-method (not articles))))
11263         ;; Crosspost the article.
11264         ((eq action 'crosspost)
11265          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11266            (setq new-xref (concat gnus-newsgroup-name ":" article))
11267            (if (and xref (not (string= xref "")))
11268                (progn
11269                  (when (string-match "^Xref: " xref)
11270                    (setq xref (substring xref (match-end 0))))
11271                  (setq new-xref (concat xref " " new-xref)))
11272              (setq new-xref (concat (system-name) " " new-xref)))
11273            (save-excursion
11274              (set-buffer copy-buf)
11275              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11276              (nnheader-replace-header "xref" new-xref)
11277              (gnus-request-accept-article
11278               to-newsgroup select-method (not articles)))))))
11279       (if (not art-group)
11280           (gnus-message 1 "Couldn't %s article %s"
11281                         (cadr (assq action names)) article)
11282         (let* ((entry
11283                 (or
11284                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11285                  (gnus-gethash
11286                   (gnus-group-prefixed-name
11287                    (car art-group)
11288                    (or select-method 
11289                        (gnus-find-method-for-group to-newsgroup)))
11290                   gnus-newsrc-hashtb)))
11291                (info (nth 2 entry))
11292                (to-group (gnus-info-group info)))
11293           ;; Update the group that has been moved to.
11294           (when (and info
11295                      (memq action '(move copy)))
11296             (unless (member to-group to-groups)
11297               (push to-group to-groups))
11298
11299             (unless (memq article gnus-newsgroup-unreads)
11300               (gnus-info-set-read
11301                info (gnus-add-to-range (gnus-info-read info)
11302                                        (list (cdr art-group)))))
11303
11304             ;; Copy any marks over to the new group.
11305             (let ((marks gnus-article-mark-lists)
11306                   (to-article (cdr art-group)))
11307
11308               ;; See whether the article is to be put in the cache.
11309               (when gnus-use-cache
11310                 (gnus-cache-possibly-enter-article
11311                  to-group to-article
11312                  (let ((header (copy-sequence
11313                                 (gnus-summary-article-header article))))
11314                    (mail-header-set-number header to-article)
11315                    header)
11316                  (memq article gnus-newsgroup-marked)
11317                  (memq article gnus-newsgroup-dormant)
11318                  (memq article gnus-newsgroup-unreads)))
11319
11320               (while marks
11321                 (when (memq article (symbol-value
11322                                      (intern (format "gnus-newsgroup-%s"
11323                                                      (caar marks)))))
11324                   ;; If the other group is the same as this group,
11325                   ;; then we have to add the mark to the list.
11326                   (when (equal to-group gnus-newsgroup-name)
11327                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11328                          (cons to-article
11329                                (symbol-value
11330                                 (intern (format "gnus-newsgroup-%s"
11331                                                 (caar marks)))))))
11332                   ;; Copy mark to other group.
11333                   (gnus-add-marked-articles
11334                    to-group (cdar marks) (list to-article) info))
11335                 (setq marks (cdr marks)))))
11336
11337           ;; Update the Xref header in this article to point to
11338           ;; the new crossposted article we have just created.
11339           (when (eq action 'crosspost)
11340             (save-excursion
11341               (set-buffer copy-buf)
11342               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11343               (nnheader-replace-header
11344                "xref" (concat new-xref " " (gnus-group-prefixed-name
11345                                             (car art-group) to-method)
11346                               ":" (cdr art-group)))
11347               (gnus-request-replace-article
11348                article gnus-newsgroup-name (current-buffer)))))
11349
11350         (gnus-summary-goto-subject article)
11351         (when (eq action 'move)
11352           (gnus-summary-mark-article article gnus-canceled-mark)))
11353       (gnus-summary-remove-process-mark article))
11354     ;; Re-activate all groups that have been moved to.
11355     (while to-groups
11356       (gnus-activate-group (pop to-groups)))
11357     
11358     (gnus-kill-buffer copy-buf)
11359     (gnus-summary-position-point)
11360     (gnus-set-mode-line 'summary)))
11361
11362 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11363   "Move the current article to a different newsgroup.
11364 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11365 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11366 re-spool using this method."
11367   (interactive "P")
11368   (gnus-summary-move-article n nil select-method 'copy))
11369
11370 (defun gnus-summary-crosspost-article (&optional n)
11371   "Crosspost the current article to some other group."
11372   (interactive "P")
11373   (gnus-summary-move-article n nil nil 'crosspost))
11374
11375 (defvar gnus-summary-respool-default-method nil
11376   "Default method for respooling an article.  
11377 If nil, use to the current newsgroup method.")
11378
11379 (defun gnus-summary-respool-article (&optional n method)
11380   "Respool the current article.
11381 The article will be squeezed through the mail spooling process again,
11382 which means that it will be put in some mail newsgroup or other
11383 depending on `nnmail-split-methods'.
11384 If N is a positive number, respool the N next articles.
11385 If N is a negative number, respool the N previous articles.
11386 If N is nil and any articles have been marked with the process mark,
11387 respool those articles instead.
11388
11389 Respooling can be done both from mail groups and \"real\" newsgroups.
11390 In the former case, the articles in question will be moved from the
11391 current group into whatever groups they are destined to.  In the
11392 latter case, they will be copied into the relevant groups."
11393   (interactive 
11394    (list current-prefix-arg
11395          (let* ((methods (gnus-methods-using 'respool))
11396                 (methname
11397                  (symbol-name (or gnus-summary-respool-default-method
11398                                   (car (gnus-find-method-for-group
11399                                         gnus-newsgroup-name)))))
11400                 (method
11401                  (gnus-completing-read 
11402                   methname "What backend do you want to use when? "
11403                   methods nil t nil 'gnus-method-history))
11404                 ms)
11405            (cond
11406             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11407              (list (intern method) ""))
11408             ((= 1 (length ms))
11409              (car ms))
11410             (t
11411              (cdr (completing-read 
11412                    "Server name: "
11413                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11414   (gnus-set-global-variables)
11415   (unless method
11416     (error "No method given for respooling"))
11417   (if (assoc (symbol-name
11418               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11419              (gnus-methods-using 'respool))
11420       (gnus-summary-move-article n nil method)
11421     (gnus-summary-copy-article n nil method)))
11422
11423 (defun gnus-summary-import-article (file)
11424   "Import a random file into a mail newsgroup."
11425   (interactive "fImport file: ")
11426   (gnus-set-global-variables)
11427   (let ((group gnus-newsgroup-name)
11428         (now (current-time))
11429         atts lines)
11430     (or (gnus-check-backend-function 'request-accept-article group)
11431         (error "%s does not support article importing" group))
11432     (or (file-readable-p file)
11433         (not (file-regular-p file))
11434         (error "Can't read %s" file))
11435     (save-excursion
11436       (set-buffer (get-buffer-create " *import file*"))
11437       (buffer-disable-undo (current-buffer))
11438       (erase-buffer)
11439       (insert-file-contents file)
11440       (goto-char (point-min))
11441       (unless (nnheader-article-p)
11442         ;; This doesn't look like an article, so we fudge some headers.
11443         (setq atts (file-attributes file)
11444               lines (count-lines (point-min) (point-max)))
11445         (insert "From: " (read-string "From: ") "\n"
11446                 "Subject: " (read-string "Subject: ") "\n"
11447                 "Date: " (timezone-make-date-arpa-standard
11448                           (current-time-string (nth 5 atts))
11449                           (current-time-zone now)
11450                           (current-time-zone now)) "\n"
11451                 "Message-ID: " (gnus-inews-message-id) "\n"
11452                 "Lines: " (int-to-string lines) "\n"
11453                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11454       (gnus-request-accept-article group nil t)
11455       (kill-buffer (current-buffer)))))
11456
11457 (defun gnus-summary-expire-articles ()
11458   "Expire all articles that are marked as expirable in the current group."
11459   (interactive)
11460   (gnus-set-global-variables)
11461   (when (gnus-check-backend-function
11462          'request-expire-articles gnus-newsgroup-name)
11463     ;; This backend supports expiry.
11464     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11465            (expirable (if total
11466                           (gnus-list-of-read-articles gnus-newsgroup-name)
11467                         (setq gnus-newsgroup-expirable
11468                               (sort gnus-newsgroup-expirable '<))))
11469            (expiry-wait (gnus-group-get-parameter
11470                          gnus-newsgroup-name 'expiry-wait))
11471            es)
11472       (when expirable
11473         ;; There are expirable articles in this group, so we run them
11474         ;; through the expiry process.
11475         (gnus-message 6 "Expiring articles...")
11476         ;; The list of articles that weren't expired is returned.
11477         (if expiry-wait
11478             (let ((nnmail-expiry-wait-function nil)
11479                   (nnmail-expiry-wait expiry-wait))
11480               (setq es (gnus-request-expire-articles
11481                         expirable gnus-newsgroup-name)))
11482           (setq es (gnus-request-expire-articles
11483                     expirable gnus-newsgroup-name)))
11484         (or total (setq gnus-newsgroup-expirable es))
11485         ;; We go through the old list of expirable, and mark all
11486         ;; really expired articles as nonexistent.
11487         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11488           (let ((gnus-use-cache nil))
11489             (while expirable
11490               (unless (memq (car expirable) es)
11491                 (when (gnus-data-find (car expirable))
11492                   (gnus-summary-mark-article
11493                    (car expirable) gnus-canceled-mark)))
11494               (setq expirable (cdr expirable)))))
11495         (gnus-message 6 "Expiring articles...done")))))
11496
11497 (defun gnus-summary-expire-articles-now ()
11498   "Expunge all expirable articles in the current group.
11499 This means that *all* articles that are marked as expirable will be
11500 deleted forever, right now."
11501   (interactive)
11502   (gnus-set-global-variables)
11503   (or gnus-expert-user
11504       (gnus-y-or-n-p
11505        "Are you really, really, really sure you want to expunge? ")
11506       (error "Phew!"))
11507   (let ((nnmail-expiry-wait 'immediate)
11508         (nnmail-expiry-wait-function nil))
11509     (gnus-summary-expire-articles)))
11510
11511 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11512 (defun gnus-summary-delete-article (&optional n)
11513   "Delete the N next (mail) articles.
11514 This command actually deletes articles.  This is not a marking
11515 command.  The article will disappear forever from your life, never to
11516 return.
11517 If N is negative, delete backwards.
11518 If N is nil and articles have been marked with the process mark,
11519 delete these instead."
11520   (interactive "P")
11521   (gnus-set-global-variables)
11522   (or (gnus-check-backend-function 'request-expire-articles
11523                                    gnus-newsgroup-name)
11524       (error "The current newsgroup does not support article deletion."))
11525   ;; Compute the list of articles to delete.
11526   (let ((articles (gnus-summary-work-articles n))
11527         not-deleted)
11528     (if (and gnus-novice-user
11529              (not (gnus-y-or-n-p
11530                    (format "Do you really want to delete %s forever? "
11531                            (if (> (length articles) 1) "these articles"
11532                              "this article")))))
11533         ()
11534       ;; Delete the articles.
11535       (setq not-deleted (gnus-request-expire-articles
11536                          articles gnus-newsgroup-name 'force))
11537       (while articles
11538         (gnus-summary-remove-process-mark (car articles))
11539         ;; The backend might not have been able to delete the article
11540         ;; after all.
11541         (or (memq (car articles) not-deleted)
11542             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11543         (setq articles (cdr articles))))
11544     (gnus-summary-position-point)
11545     (gnus-set-mode-line 'summary)
11546     not-deleted))
11547
11548 (defun gnus-summary-edit-article (&optional force)
11549   "Enter into a buffer and edit the current article.
11550 This will have permanent effect only in mail groups.
11551 If FORCE is non-nil, allow editing of articles even in read-only
11552 groups."
11553   (interactive "P")
11554   (save-excursion
11555     (set-buffer gnus-summary-buffer)
11556     (gnus-set-global-variables)
11557     (when (and (not force)
11558                (gnus-group-read-only-p))
11559       (error "The current newsgroup does not support article editing."))
11560     (gnus-summary-select-article t nil t)
11561     (gnus-configure-windows 'article)
11562     (select-window (get-buffer-window gnus-article-buffer))
11563     (gnus-message 6 "C-c C-c to end edits")
11564     (setq buffer-read-only nil)
11565     (text-mode)
11566     (use-local-map (copy-keymap (current-local-map)))
11567     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11568     (buffer-enable-undo)
11569     (widen)
11570     (goto-char (point-min))
11571     (search-forward "\n\n" nil t)))
11572
11573 (defun gnus-summary-edit-article-done ()
11574   "Make edits to the current article permanent."
11575   (interactive)
11576   (if (gnus-group-read-only-p)
11577       (progn
11578         (gnus-summary-edit-article-postpone)
11579         (gnus-error
11580          1 "The current newsgroup does not support article editing."))
11581     (let ((buf (format "%s" (buffer-string))))
11582       (erase-buffer)
11583       (insert buf)
11584       (if (not (gnus-request-replace-article
11585                 (cdr gnus-article-current) (car gnus-article-current)
11586                 (current-buffer)))
11587           (error "Couldn't replace article.")
11588         (gnus-article-mode)
11589         (use-local-map gnus-article-mode-map)
11590         (setq buffer-read-only t)
11591         (buffer-disable-undo (current-buffer))
11592         (gnus-configure-windows 'summary)
11593         (gnus-summary-update-article (cdr gnus-article-current))
11594         (when gnus-use-cache
11595           (gnus-cache-update-article 
11596            (cdr gnus-article-current) (car gnus-article-current))))
11597       (run-hooks 'gnus-article-display-hook)
11598       (and (gnus-visual-p 'summary-highlight 'highlight)
11599            (run-hooks 'gnus-visual-mark-article-hook)))))
11600
11601 (defun gnus-summary-edit-article-postpone ()
11602   "Postpone changes to the current article."
11603   (interactive)
11604   (gnus-article-mode)
11605   (use-local-map gnus-article-mode-map)
11606   (setq buffer-read-only t)
11607   (buffer-disable-undo (current-buffer))
11608   (gnus-configure-windows 'summary)
11609   (and (gnus-visual-p 'summary-highlight 'highlight)
11610        (run-hooks 'gnus-visual-mark-article-hook)))
11611
11612 (defun gnus-summary-respool-query ()
11613   "Query where the respool algorithm would put this article."
11614   (interactive)
11615   (gnus-set-global-variables)
11616   (gnus-summary-select-article)
11617   (save-excursion
11618     (set-buffer gnus-article-buffer)
11619     (save-restriction
11620       (goto-char (point-min))
11621       (search-forward "\n\n")
11622       (narrow-to-region (point-min) (point))
11623       (pp-eval-expression
11624        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11625
11626 ;; Summary score commands.
11627
11628 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11629
11630 (defun gnus-summary-raise-score (n)
11631   "Raise the score of the current article by N."
11632   (interactive "p")
11633   (gnus-set-global-variables)
11634   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11635
11636 (defun gnus-summary-set-score (n)
11637   "Set the score of the current article to N."
11638   (interactive "p")
11639   (gnus-set-global-variables)
11640   (save-excursion
11641     (gnus-summary-show-thread)
11642     (let ((buffer-read-only nil))
11643       ;; Set score.
11644       (gnus-summary-update-mark
11645        (if (= n (or gnus-summary-default-score 0)) ? 
11646          (if (< n (or gnus-summary-default-score 0))
11647              gnus-score-below-mark gnus-score-over-mark)) 'score))
11648     (let* ((article (gnus-summary-article-number))
11649            (score (assq article gnus-newsgroup-scored)))
11650       (if score (setcdr score n)
11651         (setq gnus-newsgroup-scored
11652               (cons (cons article n) gnus-newsgroup-scored))))
11653     (gnus-summary-update-line)))
11654
11655 (defun gnus-summary-current-score ()
11656   "Return the score of the current article."
11657   (interactive)
11658   (gnus-set-global-variables)
11659   (gnus-message 1 "%s" (gnus-summary-article-score)))
11660
11661 ;; Summary marking commands.
11662
11663 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11664   "Mark articles which has the same subject as read, and then select the next.
11665 If UNMARK is positive, remove any kind of mark.
11666 If UNMARK is negative, tick articles."
11667   (interactive "P")
11668   (gnus-set-global-variables)
11669   (if unmark
11670       (setq unmark (prefix-numeric-value unmark)))
11671   (let ((count
11672          (gnus-summary-mark-same-subject
11673           (gnus-summary-article-subject) unmark)))
11674     ;; Select next unread article.  If auto-select-same mode, should
11675     ;; select the first unread article.
11676     (gnus-summary-next-article t (and gnus-auto-select-same
11677                                       (gnus-summary-article-subject)))
11678     (gnus-message 7 "%d article%s marked as %s"
11679                   count (if (= count 1) " is" "s are")
11680                   (if unmark "unread" "read"))))
11681
11682 (defun gnus-summary-kill-same-subject (&optional unmark)
11683   "Mark articles which has the same subject as read.
11684 If UNMARK is positive, remove any kind of mark.
11685 If UNMARK is negative, tick articles."
11686   (interactive "P")
11687   (gnus-set-global-variables)
11688   (if unmark
11689       (setq unmark (prefix-numeric-value unmark)))
11690   (let ((count
11691          (gnus-summary-mark-same-subject
11692           (gnus-summary-article-subject) unmark)))
11693     ;; If marked as read, go to next unread subject.
11694     (if (null unmark)
11695         ;; Go to next unread subject.
11696         (gnus-summary-next-subject 1 t))
11697     (gnus-message 7 "%d articles are marked as %s"
11698                   count (if unmark "unread" "read"))))
11699
11700 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11701   "Mark articles with same SUBJECT as read, and return marked number.
11702 If optional argument UNMARK is positive, remove any kinds of marks.
11703 If optional argument UNMARK is negative, mark articles as unread instead."
11704   (let ((count 1))
11705     (save-excursion
11706       (cond
11707        ((null unmark)                   ; Mark as read.
11708         (while (and
11709                 (progn
11710                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11711                   (gnus-summary-show-thread) t)
11712                 (gnus-summary-find-subject subject))
11713           (setq count (1+ count))))
11714        ((> unmark 0)                    ; Tick.
11715         (while (and
11716                 (progn
11717                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11718                   (gnus-summary-show-thread) t)
11719                 (gnus-summary-find-subject subject))
11720           (setq count (1+ count))))
11721        (t                               ; Mark as unread.
11722         (while (and
11723                 (progn
11724                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11725                   (gnus-summary-show-thread) t)
11726                 (gnus-summary-find-subject subject))
11727           (setq count (1+ count)))))
11728       (gnus-set-mode-line 'summary)
11729       ;; Return the number of marked articles.
11730       count)))
11731
11732 (defun gnus-summary-mark-as-processable (n &optional unmark)
11733   "Set the process mark on the next N articles.
11734 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11735 the process mark instead.  The difference between N and the actual
11736 number of articles marked is returned."
11737   (interactive "p")
11738   (gnus-set-global-variables)
11739   (let ((backward (< n 0))
11740         (n (abs n)))
11741     (while (and
11742             (> n 0)
11743             (if unmark
11744                 (gnus-summary-remove-process-mark
11745                  (gnus-summary-article-number))
11746               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11747             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11748       (setq n (1- n)))
11749     (if (/= 0 n) (gnus-message 7 "No more articles"))
11750     (gnus-summary-recenter)
11751     (gnus-summary-position-point)
11752     n))
11753
11754 (defun gnus-summary-unmark-as-processable (n)
11755   "Remove the process mark from the next N articles.
11756 If N is negative, mark backward instead.  The difference between N and
11757 the actual number of articles marked is returned."
11758   (interactive "p")
11759   (gnus-set-global-variables)
11760   (gnus-summary-mark-as-processable n t))
11761
11762 (defun gnus-summary-unmark-all-processable ()
11763   "Remove the process mark from all articles."
11764   (interactive)
11765   (gnus-set-global-variables)
11766   (save-excursion
11767     (while gnus-newsgroup-processable
11768       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11769   (gnus-summary-position-point))
11770
11771 (defun gnus-summary-mark-as-expirable (n)
11772   "Mark N articles forward as expirable.
11773 If N is negative, mark backward instead.  The difference between N and
11774 the actual number of articles marked is returned."
11775   (interactive "p")
11776   (gnus-set-global-variables)
11777   (gnus-summary-mark-forward n gnus-expirable-mark))
11778
11779 (defun gnus-summary-mark-article-as-replied (article)
11780   "Mark ARTICLE replied and update the summary line."
11781   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11782   (let ((buffer-read-only nil))
11783     (when (gnus-summary-goto-subject article)
11784       (gnus-summary-update-secondary-mark article))))
11785
11786 (defun gnus-summary-set-bookmark (article)
11787   "Set a bookmark in current article."
11788   (interactive (list (gnus-summary-article-number)))
11789   (gnus-set-global-variables)
11790   (if (or (not (get-buffer gnus-article-buffer))
11791           (not gnus-current-article)
11792           (not gnus-article-current)
11793           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11794       (error "No current article selected"))
11795   ;; Remove old bookmark, if one exists.
11796   (let ((old (assq article gnus-newsgroup-bookmarks)))
11797     (if old (setq gnus-newsgroup-bookmarks
11798                   (delq old gnus-newsgroup-bookmarks))))
11799   ;; Set the new bookmark, which is on the form
11800   ;; (article-number . line-number-in-body).
11801   (setq gnus-newsgroup-bookmarks
11802         (cons
11803          (cons article
11804                (save-excursion
11805                  (set-buffer gnus-article-buffer)
11806                  (count-lines
11807                   (min (point)
11808                        (save-excursion
11809                          (goto-char (point-min))
11810                          (search-forward "\n\n" nil t)
11811                          (point)))
11812                   (point))))
11813          gnus-newsgroup-bookmarks))
11814   (gnus-message 6 "A bookmark has been added to the current article."))
11815
11816 (defun gnus-summary-remove-bookmark (article)
11817   "Remove the bookmark from the current article."
11818   (interactive (list (gnus-summary-article-number)))
11819   (gnus-set-global-variables)
11820   ;; Remove old bookmark, if one exists.
11821   (let ((old (assq article gnus-newsgroup-bookmarks)))
11822     (if old
11823         (progn
11824           (setq gnus-newsgroup-bookmarks
11825                 (delq old gnus-newsgroup-bookmarks))
11826           (gnus-message 6 "Removed bookmark."))
11827       (gnus-message 6 "No bookmark in current article."))))
11828
11829 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11830 (defun gnus-summary-mark-as-dormant (n)
11831   "Mark N articles forward as dormant.
11832 If N is negative, mark backward instead.  The difference between N and
11833 the actual number of articles marked is returned."
11834   (interactive "p")
11835   (gnus-set-global-variables)
11836   (gnus-summary-mark-forward n gnus-dormant-mark))
11837
11838 (defun gnus-summary-set-process-mark (article)
11839   "Set the process mark on ARTICLE and update the summary line."
11840   (setq gnus-newsgroup-processable
11841         (cons article
11842               (delq article gnus-newsgroup-processable)))
11843   (when (gnus-summary-goto-subject article)
11844     (gnus-summary-show-thread)
11845     (gnus-summary-update-secondary-mark article)))
11846
11847 (defun gnus-summary-remove-process-mark (article)
11848   "Remove the process mark from ARTICLE and update the summary line."
11849   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11850   (when (gnus-summary-goto-subject article)
11851     (gnus-summary-show-thread)
11852     (gnus-summary-update-secondary-mark article)))
11853
11854 (defun gnus-summary-set-saved-mark (article)
11855   "Set the process mark on ARTICLE and update the summary line."
11856   (push article gnus-newsgroup-saved)
11857   (when (gnus-summary-goto-subject article)
11858     (gnus-summary-update-secondary-mark article)))
11859
11860 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11861   "Mark N articles as read forwards.
11862 If N is negative, mark backwards instead.
11863 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11864 marked as unread.
11865 The difference between N and the actual number of articles marked is
11866 returned."
11867   (interactive "p")
11868   (gnus-set-global-variables)
11869   (let ((backward (< n 0))
11870         (gnus-summary-goto-unread
11871          (and gnus-summary-goto-unread
11872               (not (eq gnus-summary-goto-unread 'never))
11873               (not (memq mark (list gnus-unread-mark
11874                                     gnus-ticked-mark gnus-dormant-mark)))))
11875         (n (abs n))
11876         (mark (or mark gnus-del-mark)))
11877     (while (and (> n 0)
11878                 (gnus-summary-mark-article nil mark no-expire)
11879                 (zerop (gnus-summary-next-subject
11880                         (if backward -1 1)
11881                         (and gnus-summary-goto-unread
11882                              (not (eq gnus-summary-goto-unread 'never)))
11883                         t)))
11884       (setq n (1- n)))
11885     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11886     (gnus-summary-recenter)
11887     (gnus-summary-position-point)
11888     (gnus-set-mode-line 'summary)
11889     n))
11890
11891 (defun gnus-summary-mark-article-as-read (mark)
11892   "Mark the current article quickly as read with MARK."
11893   (let ((article (gnus-summary-article-number)))
11894     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11895     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11896     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11897     (setq gnus-newsgroup-reads
11898           (cons (cons article mark) gnus-newsgroup-reads))
11899     ;; Possibly remove from cache, if that is used.
11900     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11901     ;; Allow the backend to change the mark.
11902     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11903     ;; Check for auto-expiry.
11904     (when (and gnus-newsgroup-auto-expire
11905                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11906                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11907                    (= mark gnus-ancient-mark)
11908                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11909       (setq mark gnus-expirable-mark)
11910       (push article gnus-newsgroup-expirable))
11911     ;; Set the mark in the buffer.
11912     (gnus-summary-update-mark mark 'unread)
11913     t))
11914
11915 (defun gnus-summary-mark-article-as-unread (mark)
11916   "Mark the current article quickly as unread with MARK."
11917   (let ((article (gnus-summary-article-number)))
11918     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11919     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11920     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11921     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11922     (cond ((= mark gnus-ticked-mark)
11923            (push article gnus-newsgroup-marked))
11924           ((= mark gnus-dormant-mark)
11925            (push article gnus-newsgroup-dormant))
11926           (t
11927            (push article gnus-newsgroup-unreads)))
11928     (setq gnus-newsgroup-reads
11929           (delq (assq article gnus-newsgroup-reads)
11930                 gnus-newsgroup-reads))
11931
11932     ;; See whether the article is to be put in the cache.
11933     (and gnus-use-cache
11934          (vectorp (gnus-summary-article-header article))
11935          (save-excursion
11936            (gnus-cache-possibly-enter-article
11937             gnus-newsgroup-name article
11938             (gnus-summary-article-header article)
11939             (= mark gnus-ticked-mark)
11940             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11941
11942     ;; Fix the mark.
11943     (gnus-summary-update-mark mark 'unread)
11944     t))
11945
11946 (defun gnus-summary-mark-article (&optional article mark no-expire)
11947   "Mark ARTICLE with MARK.  MARK can be any character.
11948 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11949 `??' (dormant) and `?E' (expirable).
11950 If MARK is nil, then the default character `?D' is used.
11951 If ARTICLE is nil, then the article on the current line will be
11952 marked."
11953   ;; The mark might be a string.
11954   (and (stringp mark)
11955        (setq mark (aref mark 0)))
11956   ;; If no mark is given, then we check auto-expiring.
11957   (and (not no-expire)
11958        gnus-newsgroup-auto-expire
11959        (or (not mark)
11960            (and (numberp mark)
11961                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11962                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11963                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11964        (setq mark gnus-expirable-mark))
11965   (let* ((mark (or mark gnus-del-mark))
11966          (article (or article (gnus-summary-article-number))))
11967     (or article (error "No article on current line"))
11968     (if (or (= mark gnus-unread-mark)
11969             (= mark gnus-ticked-mark)
11970             (= mark gnus-dormant-mark))
11971         (gnus-mark-article-as-unread article mark)
11972       (gnus-mark-article-as-read article mark))
11973
11974     ;; See whether the article is to be put in the cache.
11975     (and gnus-use-cache
11976          (not (= mark gnus-canceled-mark))
11977          (vectorp (gnus-summary-article-header article))
11978          (save-excursion
11979            (gnus-cache-possibly-enter-article
11980             gnus-newsgroup-name article
11981             (gnus-summary-article-header article)
11982             (= mark gnus-ticked-mark)
11983             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11984
11985     (if (gnus-summary-goto-subject article nil t)
11986         (let ((buffer-read-only nil))
11987           (gnus-summary-show-thread)
11988           ;; Fix the mark.
11989           (gnus-summary-update-mark mark 'unread)
11990           t))))
11991
11992 (defun gnus-summary-update-secondary-mark (article)
11993   "Update the secondary (read, process, cache) mark."
11994   (gnus-summary-update-mark
11995    (cond ((memq article gnus-newsgroup-processable)
11996           gnus-process-mark)
11997          ((memq article gnus-newsgroup-cached)
11998           gnus-cached-mark)
11999          ((memq article gnus-newsgroup-replied)
12000           gnus-replied-mark)
12001          ((memq article gnus-newsgroup-saved)
12002           gnus-saved-mark)
12003          (t gnus-unread-mark))
12004    'replied)
12005   (when (gnus-visual-p 'summary-highlight 'highlight)
12006     (run-hooks 'gnus-summary-update-hook))
12007   t)
12008
12009 (defun gnus-summary-update-mark (mark type)
12010   (beginning-of-line)
12011   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12012         (buffer-read-only nil))
12013     (when (and forward
12014                (<= (+ forward (point)) (point-max)))
12015       ;; Go to the right position on the line.
12016       (goto-char (+ forward (point)))
12017       ;; Replace the old mark with the new mark.
12018       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12019       ;; Optionally update the marks by some user rule.
12020       (when (eq type 'unread)
12021         (gnus-data-set-mark
12022          (gnus-data-find (gnus-summary-article-number)) mark)
12023         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12024
12025 (defun gnus-mark-article-as-read (article &optional mark)
12026   "Enter ARTICLE in the pertinent lists and remove it from others."
12027   ;; Make the article expirable.
12028   (let ((mark (or mark gnus-del-mark)))
12029     (if (= mark gnus-expirable-mark)
12030         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12031       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12032     ;; Remove from unread and marked lists.
12033     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12034     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12035     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12036     (push (cons article mark) gnus-newsgroup-reads)
12037     ;; Possibly remove from cache, if that is used.
12038     (when gnus-use-cache
12039       (gnus-cache-enter-remove-article article))))
12040
12041 (defun gnus-mark-article-as-unread (article &optional mark)
12042   "Enter ARTICLE in the pertinent lists and remove it from others."
12043   (let ((mark (or mark gnus-ticked-mark)))
12044     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12045     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12046     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12047     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12048     (cond ((= mark gnus-ticked-mark)
12049            (push article gnus-newsgroup-marked))
12050           ((= mark gnus-dormant-mark)
12051            (push article gnus-newsgroup-dormant))
12052           (t
12053            (push article gnus-newsgroup-unreads)))
12054     (setq gnus-newsgroup-reads
12055           (delq (assq article gnus-newsgroup-reads)
12056                 gnus-newsgroup-reads))))
12057
12058 (defalias 'gnus-summary-mark-as-unread-forward
12059   'gnus-summary-tick-article-forward)
12060 (make-obsolete 'gnus-summary-mark-as-unread-forward
12061                'gnus-summary-tick-article-forward)
12062 (defun gnus-summary-tick-article-forward (n)
12063   "Tick N articles forwards.
12064 If N is negative, tick backwards instead.
12065 The difference between N and the number of articles ticked is returned."
12066   (interactive "p")
12067   (gnus-summary-mark-forward n gnus-ticked-mark))
12068
12069 (defalias 'gnus-summary-mark-as-unread-backward
12070   'gnus-summary-tick-article-backward)
12071 (make-obsolete 'gnus-summary-mark-as-unread-backward
12072                'gnus-summary-tick-article-backward)
12073 (defun gnus-summary-tick-article-backward (n)
12074   "Tick N articles backwards.
12075 The difference between N and the number of articles ticked is returned."
12076   (interactive "p")
12077   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12078
12079 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12080 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12081 (defun gnus-summary-tick-article (&optional article clear-mark)
12082   "Mark current article as unread.
12083 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12084 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12085   (interactive)
12086   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12087                                        gnus-ticked-mark)))
12088
12089 (defun gnus-summary-mark-as-read-forward (n)
12090   "Mark N articles as read forwards.
12091 If N is negative, mark backwards instead.
12092 The difference between N and the actual number of articles marked is
12093 returned."
12094   (interactive "p")
12095   (gnus-summary-mark-forward n gnus-del-mark t))
12096
12097 (defun gnus-summary-mark-as-read-backward (n)
12098   "Mark the N articles as read backwards.
12099 The difference between N and the actual number of articles marked is
12100 returned."
12101   (interactive "p")
12102   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12103
12104 (defun gnus-summary-mark-as-read (&optional article mark)
12105   "Mark current article as read.
12106 ARTICLE specifies the article to be marked as read.
12107 MARK specifies a string to be inserted at the beginning of the line."
12108   (gnus-summary-mark-article article mark))
12109
12110 (defun gnus-summary-clear-mark-forward (n)
12111   "Clear marks from N articles forward.
12112 If N is negative, clear backward instead.
12113 The difference between N and the number of marks cleared is returned."
12114   (interactive "p")
12115   (gnus-summary-mark-forward n gnus-unread-mark))
12116
12117 (defun gnus-summary-clear-mark-backward (n)
12118   "Clear marks from N articles backward.
12119 The difference between N and the number of marks cleared is returned."
12120   (interactive "p")
12121   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12122
12123 (defun gnus-summary-mark-unread-as-read ()
12124   "Intended to be used by `gnus-summary-mark-article-hook'."
12125   (when (memq gnus-current-article gnus-newsgroup-unreads)
12126     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12127
12128 (defun gnus-summary-mark-read-and-unread-as-read ()
12129   "Intended to be used by `gnus-summary-mark-article-hook'."
12130   (let ((mark (gnus-summary-article-mark)))
12131     (when (or (gnus-unread-mark-p mark)
12132               (gnus-read-mark-p mark))
12133       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12134
12135 (defun gnus-summary-mark-region-as-read (point mark all)
12136   "Mark all unread articles between point and mark as read.
12137 If given a prefix, mark all articles between point and mark as read,
12138 even ticked and dormant ones."
12139   (interactive "r\nP")
12140   (save-excursion
12141     (let (article)
12142       (goto-char point)
12143       (beginning-of-line)
12144       (while (and
12145               (< (point) mark)
12146               (progn
12147                 (when (or all
12148                           (memq (setq article (gnus-summary-article-number))
12149                                 gnus-newsgroup-unreads))
12150                   (gnus-summary-mark-article article gnus-del-mark))
12151                 t)
12152               (gnus-summary-find-next))))))
12153
12154 (defun gnus-summary-mark-below (score mark)
12155   "Mark articles with score less than SCORE with MARK."
12156   (interactive "P\ncMark: ")
12157   (gnus-set-global-variables)
12158   (setq score (if score
12159                   (prefix-numeric-value score)
12160                 (or gnus-summary-default-score 0)))
12161   (save-excursion
12162     (set-buffer gnus-summary-buffer)
12163     (goto-char (point-min))
12164     (while 
12165         (progn
12166           (and (< (gnus-summary-article-score) score)
12167                (gnus-summary-mark-article nil mark))
12168           (gnus-summary-find-next)))))
12169
12170 (defun gnus-summary-kill-below (&optional score)
12171   "Mark articles with score below SCORE as read."
12172   (interactive "P")
12173   (gnus-set-global-variables)
12174   (gnus-summary-mark-below score gnus-killed-mark))
12175
12176 (defun gnus-summary-clear-above (&optional score)
12177   "Clear all marks from articles with score above SCORE."
12178   (interactive "P")
12179   (gnus-set-global-variables)
12180   (gnus-summary-mark-above score gnus-unread-mark))
12181
12182 (defun gnus-summary-tick-above (&optional score)
12183   "Tick all articles with score above SCORE."
12184   (interactive "P")
12185   (gnus-set-global-variables)
12186   (gnus-summary-mark-above score gnus-ticked-mark))
12187
12188 (defun gnus-summary-mark-above (score mark)
12189   "Mark articles with score over SCORE with MARK."
12190   (interactive "P\ncMark: ")
12191   (gnus-set-global-variables)
12192   (setq score (if score
12193                   (prefix-numeric-value score)
12194                 (or gnus-summary-default-score 0)))
12195   (save-excursion
12196     (set-buffer gnus-summary-buffer)
12197     (goto-char (point-min))
12198     (while (and (progn
12199                   (if (> (gnus-summary-article-score) score)
12200                       (gnus-summary-mark-article nil mark))
12201                   t)
12202                 (gnus-summary-find-next)))))
12203
12204 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12205 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12206 (defun gnus-summary-limit-include-expunged ()
12207   "Display all the hidden articles that were expunged for low scores."
12208   (interactive)
12209   (gnus-set-global-variables)
12210   (let ((buffer-read-only nil))
12211     (let ((scored gnus-newsgroup-scored)
12212           headers h)
12213       (while scored
12214         (or (gnus-summary-goto-subject (caar scored))
12215             (and (setq h (gnus-summary-article-header (caar scored)))
12216                  (< (cdar scored) gnus-summary-expunge-below)
12217                  (setq headers (cons h headers))))
12218         (setq scored (cdr scored)))
12219       (or headers (error "No expunged articles hidden."))
12220       (goto-char (point-min))
12221       (gnus-summary-prepare-unthreaded (nreverse headers)))
12222     (goto-char (point-min))
12223     (gnus-summary-position-point)))
12224
12225 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12226   "Mark all articles not marked as unread in this newsgroup as read.
12227 If prefix argument ALL is non-nil, all articles are marked as read.
12228 If QUIETLY is non-nil, no questions will be asked.
12229 If TO-HERE is non-nil, it should be a point in the buffer.  All
12230 articles before this point will be marked as read.
12231 The number of articles marked as read is returned."
12232   (interactive "P")
12233   (gnus-set-global-variables)
12234   (prog1
12235       (if (or quietly
12236               (not gnus-interactive-catchup) ;Without confirmation?
12237               gnus-expert-user
12238               (gnus-y-or-n-p
12239                (if all
12240                    "Mark absolutely all articles as read? "
12241                  "Mark all unread articles as read? ")))
12242           (if (and not-mark
12243                    (not gnus-newsgroup-adaptive)
12244                    (not gnus-newsgroup-auto-expire))
12245               (progn
12246                 (when all
12247                   (setq gnus-newsgroup-marked nil
12248                         gnus-newsgroup-dormant nil))
12249                 (setq gnus-newsgroup-unreads nil))
12250             ;; We actually mark all articles as canceled, which we
12251             ;; have to do when using auto-expiry or adaptive scoring.
12252             (gnus-summary-show-all-threads)
12253             (if (gnus-summary-first-subject (not all))
12254                 (while (and
12255                         (if to-here (< (point) to-here) t)
12256                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12257                         (gnus-summary-find-next (not all)))))
12258             (unless to-here
12259               (setq gnus-newsgroup-unreads nil))
12260             (gnus-set-mode-line 'summary)))
12261     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12262       (if (and (not to-here) (eq 'nnvirtual (car method)))
12263           (nnvirtual-catchup-group
12264            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12265     (gnus-summary-position-point)))
12266
12267 (defun gnus-summary-catchup-to-here (&optional all)
12268   "Mark all unticked articles before the current one as read.
12269 If ALL is non-nil, also mark ticked and dormant articles as read."
12270   (interactive "P")
12271   (gnus-set-global-variables)
12272   (save-excursion
12273     (let ((beg (point)))
12274       ;; We check that there are unread articles.
12275       (when (or all (gnus-summary-find-prev))
12276         (gnus-summary-catchup all t beg))))
12277   (gnus-summary-position-point))
12278
12279 (defun gnus-summary-catchup-all (&optional quietly)
12280   "Mark all articles in this newsgroup as read."
12281   (interactive "P")
12282   (gnus-set-global-variables)
12283   (gnus-summary-catchup t quietly))
12284
12285 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12286   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12287 If prefix argument ALL is non-nil, all articles are marked as read."
12288   (interactive "P")
12289   (gnus-set-global-variables)
12290   (gnus-summary-catchup all quietly nil 'fast)
12291   ;; Select next newsgroup or exit.
12292   (if (eq gnus-auto-select-next 'quietly)
12293       (gnus-summary-next-group nil)
12294     (gnus-summary-exit)))
12295
12296 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12297   "Mark all articles in this newsgroup as read, and then exit."
12298   (interactive "P")
12299   (gnus-set-global-variables)
12300   (gnus-summary-catchup-and-exit t quietly))
12301
12302 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12303 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12304   "Mark all articles in this group as read and select the next group.
12305 If given a prefix, mark all articles, unread as well as ticked, as
12306 read."
12307   (interactive "P")
12308   (gnus-set-global-variables)
12309   (save-excursion
12310     (gnus-summary-catchup all))
12311   (gnus-summary-next-article t nil nil t))
12312
12313 ;; Thread-based commands.
12314
12315 (defun gnus-summary-articles-in-thread (&optional article)
12316   "Return a list of all articles in the current thread.
12317 If ARTICLE is non-nil, return all articles in the thread that starts
12318 with that article."
12319   (let* ((article (or article (gnus-summary-article-number)))
12320          (data (gnus-data-find-list article))
12321          (top-level (gnus-data-level (car data)))
12322          (top-subject
12323           (cond ((null gnus-thread-operation-ignore-subject)
12324                  (gnus-simplify-subject-re
12325                   (mail-header-subject (gnus-data-header (car data)))))
12326                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12327                  (gnus-simplify-subject-fuzzy
12328                   (mail-header-subject (gnus-data-header (car data)))))
12329                 (t nil)))
12330          (end-point (save-excursion
12331                       (gnus-summary-go-to-next-thread) (point)))
12332          articles)
12333     (while (and data
12334                 (< (gnus-data-pos (car data)) end-point))
12335       (and (or (not top-subject)
12336                (string= top-subject
12337                         (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12338                             (gnus-simplify-subject-fuzzy
12339                              (mail-header-subject
12340                               (gnus-data-header (car data))))
12341                           (gnus-simplify-subject-re
12342                            (mail-header-subject
12343                             (gnus-data-header (car data)))))))
12344            (setq articles (cons (gnus-data-number (car data)) articles)))
12345       (unless (and (setq data (cdr data))
12346                    (> (gnus-data-level (car data)) top-level))
12347         (setq data nil)))
12348     ;; Return the list of articles.
12349     (nreverse articles)))
12350
12351 (defun gnus-summary-rethread-current ()
12352   "Rethread the thread the current article is part of."
12353   (interactive)
12354   (gnus-set-global-variables)
12355   (let* ((gnus-show-threads t)
12356          (article (gnus-summary-article-number))
12357          (id (mail-header-id (gnus-summary-article-header)))
12358          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12359     (unless id
12360       (error "No article on the current line"))
12361     (gnus-rebuild-thread id)
12362     (gnus-summary-goto-subject article)))
12363
12364 (defun gnus-summary-reparent-thread ()
12365   "Make current article child of the marked (or previous) article.
12366
12367 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12368 is non-nil or the Subject: of both articles are the same."
12369   (interactive)
12370   (or (not (gnus-group-read-only-p))
12371       (error "The current newsgroup does not support article editing."))
12372   (or (<= (length gnus-newsgroup-processable) 1)
12373       (error "No more than one article may be marked."))
12374   (save-window-excursion
12375     (let ((gnus-article-buffer " *reparent*")
12376           (current-article (gnus-summary-article-number))
12377           ; first grab the marked article, otherwise one line up.
12378           (parent-article (if (not (null gnus-newsgroup-processable))
12379                               (car gnus-newsgroup-processable)
12380                             (save-excursion
12381                               (if (eq (forward-line -1) 0)
12382                                   (gnus-summary-article-number)
12383                                 (error "Beginning of summary buffer."))))))
12384       (or (not (eq current-article parent-article))
12385           (error "An article may not be self-referential."))
12386       (let ((message-id (mail-header-id 
12387                          (gnus-summary-article-header parent-article))))
12388         (or (and message-id (not (equal message-id "")))
12389             (error "No message-id in desired parent."))
12390         (gnus-summary-select-article t t nil current-article)
12391         (set-buffer gnus-article-buffer)
12392         (setq buffer-read-only nil)
12393         (let ((buf (format "%s" (buffer-string))))
12394           (erase-buffer)
12395           (insert buf))
12396         (goto-char (point-min))
12397         (if (search-forward-regexp "^References: " nil t)
12398             (insert message-id " " )
12399           (insert "References: " message-id "\n"))
12400         (or (gnus-request-replace-article current-article
12401                                           (car gnus-article-current)
12402                                           gnus-article-buffer)
12403             (error "Couldn't replace article."))
12404         (set-buffer gnus-summary-buffer)
12405         (gnus-summary-unmark-all-processable)
12406         (gnus-summary-rethread-current)
12407         (gnus-message 3 "Article %d is now the child of article %d."
12408                       current-article parent-article)))))
12409
12410 (defun gnus-summary-toggle-threads (&optional arg)
12411   "Toggle showing conversation threads.
12412 If ARG is positive number, turn showing conversation threads on."
12413   (interactive "P")
12414   (gnus-set-global-variables)
12415   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12416     (setq gnus-show-threads
12417           (if (null arg) (not gnus-show-threads)
12418             (> (prefix-numeric-value arg) 0)))
12419     (gnus-summary-prepare)
12420     (gnus-summary-goto-subject current)
12421     (gnus-summary-position-point)))
12422
12423 (defun gnus-summary-show-all-threads ()
12424   "Show all threads."
12425   (interactive)
12426   (gnus-set-global-variables)
12427   (save-excursion
12428     (let ((buffer-read-only nil))
12429       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12430   (gnus-summary-position-point))
12431
12432 (defun gnus-summary-show-thread ()
12433   "Show thread subtrees.
12434 Returns nil if no thread was there to be shown."
12435   (interactive)
12436   (gnus-set-global-variables)
12437   (let ((buffer-read-only nil)
12438         (orig (point))
12439         ;; first goto end then to beg, to have point at beg after let
12440         (end (progn (end-of-line) (point)))
12441         (beg (progn (beginning-of-line) (point))))
12442     (prog1
12443         ;; Any hidden lines here?
12444         (search-forward "\r" end t)
12445       (subst-char-in-region beg end ?\^M ?\n t)
12446       (goto-char orig)
12447       (gnus-summary-position-point))))
12448
12449 (defun gnus-summary-hide-all-threads ()
12450   "Hide all thread subtrees."
12451   (interactive)
12452   (gnus-set-global-variables)
12453   (save-excursion
12454     (goto-char (point-min))
12455     (gnus-summary-hide-thread)
12456     (while (zerop (gnus-summary-next-thread 1 t))
12457       (gnus-summary-hide-thread)))
12458   (gnus-summary-position-point))
12459
12460 (defun gnus-summary-hide-thread ()
12461   "Hide thread subtrees.
12462 Returns nil if no threads were there to be hidden."
12463   (interactive)
12464   (gnus-set-global-variables)
12465   (let ((buffer-read-only nil)
12466         (start (point))
12467         (article (gnus-summary-article-number)))
12468     (goto-char start)
12469     ;; Go forward until either the buffer ends or the subthread
12470     ;; ends.
12471     (when (and (not (eobp))
12472                (or (zerop (gnus-summary-next-thread 1 t))
12473                    (goto-char (point-max))))
12474       (prog1
12475           (if (and (> (point) start)
12476                    (search-backward "\n" start t))
12477               (progn
12478                 (subst-char-in-region start (point) ?\n ?\^M)
12479                 (gnus-summary-goto-subject article))
12480             (goto-char start)
12481             nil)
12482         ;;(gnus-summary-position-point)
12483         ))))
12484
12485 (defun gnus-summary-go-to-next-thread (&optional previous)
12486   "Go to the same level (or less) next thread.
12487 If PREVIOUS is non-nil, go to previous thread instead.
12488 Return the article number moved to, or nil if moving was impossible."
12489   (let ((level (gnus-summary-thread-level))
12490         (way (if previous -1 1))
12491         (beg (point)))
12492     (forward-line way)
12493     (while (and (not (eobp))
12494                 (< level (gnus-summary-thread-level)))
12495       (forward-line way))
12496     (if (eobp)
12497         (progn
12498           (goto-char beg)
12499           nil)
12500       (setq beg (point))
12501       (prog1
12502           (gnus-summary-article-number)
12503         (goto-char beg)))))
12504
12505 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12506   "Go to the same level (or less) next thread.
12507 If PREVIOUS is non-nil, go to previous thread instead.
12508 Return the article number moved to, or nil if moving was impossible."
12509   (if (and (eq gnus-summary-make-false-root 'dummy)
12510            (gnus-summary-article-intangible-p))
12511       (let ((beg (point)))
12512         (while (and (zerop (forward-line 1))
12513                     (not (gnus-summary-article-intangible-p))
12514                     (not (zerop (save-excursion 
12515                                   (gnus-summary-thread-level))))))
12516         (if (eobp)
12517             (progn
12518               (goto-char beg)
12519               nil)
12520           (point)))
12521     (let* ((level (gnus-summary-thread-level))
12522            (article (gnus-summary-article-number))
12523            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12524            oart)
12525       (while data
12526         (if (<= (gnus-data-level (car data)) level)
12527             (setq oart (gnus-data-number (car data))
12528                   data nil)
12529           (setq data (cdr data))))
12530       (and oart
12531            (gnus-summary-goto-subject oart)))))
12532
12533 (defun gnus-summary-next-thread (n &optional silent)
12534   "Go to the same level next N'th thread.
12535 If N is negative, search backward instead.
12536 Returns the difference between N and the number of skips actually
12537 done.
12538
12539 If SILENT, don't output messages."
12540   (interactive "p")
12541   (gnus-set-global-variables)
12542   (let ((backward (< n 0))
12543         (n (abs n))
12544         old dum int)
12545     (while (and (> n 0)
12546                 (gnus-summary-go-to-next-thread backward))
12547       (decf n))
12548     (unless silent 
12549       (gnus-summary-position-point))
12550     (when (and (not silent) (/= 0 n))
12551       (gnus-message 7 "No more threads"))
12552     n))
12553
12554 (defun gnus-summary-prev-thread (n)
12555   "Go to the same level previous N'th thread.
12556 Returns the difference between N and the number of skips actually
12557 done."
12558   (interactive "p")
12559   (gnus-set-global-variables)
12560   (gnus-summary-next-thread (- n)))
12561
12562 (defun gnus-summary-go-down-thread ()
12563   "Go down one level in the current thread."
12564   (let ((children (gnus-summary-article-children)))
12565     (and children
12566          (gnus-summary-goto-subject (car children)))))
12567
12568 (defun gnus-summary-go-up-thread ()
12569   "Go up one level in the current thread."
12570   (let ((parent (gnus-summary-article-parent)))
12571     (and parent
12572          (gnus-summary-goto-subject parent))))
12573
12574 (defun gnus-summary-down-thread (n)
12575   "Go down thread N steps.
12576 If N is negative, go up instead.
12577 Returns the difference between N and how many steps down that were
12578 taken."
12579   (interactive "p")
12580   (gnus-set-global-variables)
12581   (let ((up (< n 0))
12582         (n (abs n)))
12583     (while (and (> n 0)
12584                 (if up (gnus-summary-go-up-thread)
12585                   (gnus-summary-go-down-thread)))
12586       (setq n (1- n)))
12587     (gnus-summary-position-point)
12588     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12589     n))
12590
12591 (defun gnus-summary-up-thread (n)
12592   "Go up thread N steps.
12593 If N is negative, go up instead.
12594 Returns the difference between N and how many steps down that were
12595 taken."
12596   (interactive "p")
12597   (gnus-set-global-variables)
12598   (gnus-summary-down-thread (- n)))
12599
12600 (defun gnus-summary-top-thread ()
12601   "Go to the top of the thread."
12602   (interactive)
12603   (gnus-set-global-variables)
12604   (while (gnus-summary-go-up-thread))
12605   (gnus-summary-article-number))
12606
12607 (defun gnus-summary-kill-thread (&optional unmark)
12608   "Mark articles under current thread as read.
12609 If the prefix argument is positive, remove any kinds of marks.
12610 If the prefix argument is negative, tick articles instead."
12611   (interactive "P")
12612   (gnus-set-global-variables)
12613   (if unmark
12614       (setq unmark (prefix-numeric-value unmark)))
12615   (let ((articles (gnus-summary-articles-in-thread)))
12616     (save-excursion
12617       ;; Expand the thread.
12618       (gnus-summary-show-thread)
12619       ;; Mark all the articles.
12620       (while articles
12621         (gnus-summary-goto-subject (car articles))
12622         (cond ((null unmark)
12623                (gnus-summary-mark-article-as-read gnus-killed-mark))
12624               ((> unmark 0)
12625                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12626               (t
12627                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12628         (setq articles (cdr articles))))
12629     ;; Hide killed subtrees.
12630     (and (null unmark)
12631          gnus-thread-hide-killed
12632          (gnus-summary-hide-thread))
12633     ;; If marked as read, go to next unread subject.
12634     (if (null unmark)
12635         ;; Go to next unread subject.
12636         (gnus-summary-next-subject 1 t)))
12637   (gnus-set-mode-line 'summary))
12638
12639 ;; Summary sorting commands
12640
12641 (defun gnus-summary-sort-by-number (&optional reverse)
12642   "Sort summary buffer by article number.
12643 Argument REVERSE means reverse order."
12644   (interactive "P")
12645   (gnus-summary-sort 'number reverse))
12646
12647 (defun gnus-summary-sort-by-author (&optional reverse)
12648   "Sort summary buffer by author name alphabetically.
12649 If case-fold-search is non-nil, case of letters is ignored.
12650 Argument REVERSE means reverse order."
12651   (interactive "P")
12652   (gnus-summary-sort 'author reverse))
12653
12654 (defun gnus-summary-sort-by-subject (&optional reverse)
12655   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12656 If case-fold-search is non-nil, case of letters is ignored.
12657 Argument REVERSE means reverse order."
12658   (interactive "P")
12659   (gnus-summary-sort 'subject reverse))
12660
12661 (defun gnus-summary-sort-by-date (&optional reverse)
12662   "Sort summary buffer by date.
12663 Argument REVERSE means reverse order."
12664   (interactive "P")
12665   (gnus-summary-sort 'date reverse))
12666
12667 (defun gnus-summary-sort-by-score (&optional reverse)
12668   "Sort summary buffer by score.
12669 Argument REVERSE means reverse order."
12670   (interactive "P")
12671   (gnus-summary-sort 'score reverse))
12672
12673 (defun gnus-summary-sort (predicate reverse)
12674   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12675   (gnus-set-global-variables)
12676   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12677          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12678          (gnus-thread-sort-functions
12679           (list
12680            (if (not reverse)
12681                thread
12682              `(lambda (t1 t2)
12683                 (,thread t2 t1)))))
12684          (gnus-article-sort-functions
12685           (list
12686            (if (not reverse)
12687                article
12688              `(lambda (t1 t2)
12689                 (,article t2 t1)))))
12690          (buffer-read-only)
12691          (gnus-summary-prepare-hook nil))
12692     ;; We do the sorting by regenerating the threads.
12693     (gnus-summary-prepare)
12694     ;; Hide subthreads if needed.
12695     (when (and gnus-show-threads gnus-thread-hide-subtree)
12696       (gnus-summary-hide-all-threads)))
12697   ;; If in async mode, we send some info to the backend.
12698   (when gnus-newsgroup-async
12699     (gnus-request-asynchronous
12700      gnus-newsgroup-name gnus-newsgroup-data)))
12701
12702 (defun gnus-sortable-date (date)
12703   "Make sortable string by string-lessp from DATE.
12704 Timezone package is used."
12705   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12706          (year (aref date 0))
12707          (month (aref date 1))
12708          (day (aref date 2)))
12709     (timezone-make-sortable-date
12710      year month day
12711      (timezone-make-time-string
12712       (aref date 3) (aref date 4) (aref date 5)))))
12713
12714 ;; Summary saving commands.
12715
12716 (defun gnus-summary-save-article (&optional n not-saved)
12717   "Save the current article using the default saver function.
12718 If N is a positive number, save the N next articles.
12719 If N is a negative number, save the N previous articles.
12720 If N is nil and any articles have been marked with the process mark,
12721 save those articles instead.
12722 The variable `gnus-default-article-saver' specifies the saver function."
12723   (interactive "P")
12724   (gnus-set-global-variables)
12725   (let ((articles (gnus-summary-work-articles n))
12726         file header article)
12727     (while articles
12728       (setq header (gnus-summary-article-header
12729                     (setq article (pop articles))))
12730       (if (not (vectorp header))
12731           ;; This is a pseudo-article.
12732           (if (assq 'name header)
12733               (gnus-copy-file (cdr (assq 'name header)))
12734             (gnus-message 1 "Article %d is unsaveable" article))
12735         ;; This is a real article.
12736         (save-window-excursion
12737           (gnus-summary-select-article t nil nil article))
12738         (unless gnus-save-all-headers
12739           ;; Remove headers accoring to `gnus-saved-headers'.
12740           (let ((gnus-visible-headers
12741                  (or gnus-saved-headers gnus-visible-headers)))
12742             (gnus-article-hide-headers nil t)))
12743         ;; Remove any X-Gnus lines.
12744         (save-excursion
12745           (set-buffer gnus-article-buffer)
12746           (save-restriction
12747             (let ((buffer-read-only nil))
12748               (nnheader-narrow-to-headers)
12749               (while (re-search-forward "^X-Gnus" nil t)
12750                 (gnus-delete-line)))))
12751         (save-window-excursion
12752           (if (not gnus-default-article-saver)
12753               (error "No default saver is defined.")
12754             (setq file (funcall
12755                         gnus-default-article-saver
12756                         (cond
12757                          ((not gnus-prompt-before-saving)
12758                           'default)
12759                          ((eq gnus-prompt-before-saving 'always)
12760                           nil)
12761                          (t file))))))
12762         (gnus-summary-remove-process-mark article)
12763         (unless not-saved
12764           (gnus-summary-set-saved-mark article))))
12765     (gnus-summary-position-point)
12766     n))
12767
12768 (defun gnus-summary-pipe-output (&optional arg)
12769   "Pipe the current article to a subprocess.
12770 If N is a positive number, pipe the N next articles.
12771 If N is a negative number, pipe the N previous articles.
12772 If N is nil and any articles have been marked with the process mark,
12773 pipe those articles instead."
12774   (interactive "P")
12775   (gnus-set-global-variables)
12776   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12777     (gnus-summary-save-article arg t))
12778   (gnus-configure-windows 'pipe))
12779
12780 (defun gnus-summary-save-article-mail (&optional arg)
12781   "Append the current article to an mail file.
12782 If N is a positive number, save the N next articles.
12783 If N is a negative number, save the N previous articles.
12784 If N is nil and any articles have been marked with the process mark,
12785 save those articles instead."
12786   (interactive "P")
12787   (gnus-set-global-variables)
12788   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12789     (gnus-summary-save-article arg)))
12790
12791 (defun gnus-summary-save-article-rmail (&optional arg)
12792   "Append the current article to an rmail file.
12793 If N is a positive number, save the N next articles.
12794 If N is a negative number, save the N previous articles.
12795 If N is nil and any articles have been marked with the process mark,
12796 save those articles instead."
12797   (interactive "P")
12798   (gnus-set-global-variables)
12799   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12800     (gnus-summary-save-article arg)))
12801
12802 (defun gnus-summary-save-article-file (&optional arg)
12803   "Append the current article to a file.
12804 If N is a positive number, save the N next articles.
12805 If N is a negative number, save the N previous articles.
12806 If N is nil and any articles have been marked with the process mark,
12807 save those articles instead."
12808   (interactive "P")
12809   (gnus-set-global-variables)
12810   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12811     (gnus-summary-save-article arg)))
12812
12813 (defun gnus-summary-save-article-body-file (&optional arg)
12814   "Append the current article body to a file.
12815 If N is a positive number, save the N next articles.
12816 If N is a negative number, save the N previous articles.
12817 If N is nil and any articles have been marked with the process mark,
12818 save those articles instead."
12819   (interactive "P")
12820   (gnus-set-global-variables)
12821   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12822     (gnus-summary-save-article arg)))
12823
12824 (defun gnus-get-split-value (methods)
12825   "Return a value based on the split METHODS."
12826   (let (split-name method result match)
12827     (when methods
12828       (save-excursion
12829         (set-buffer gnus-original-article-buffer)
12830         (save-restriction
12831           (nnheader-narrow-to-headers)
12832           (while methods
12833             (goto-char (point-min))
12834             (setq method (pop methods))
12835             (setq match (car method))
12836             (when (cond
12837                    ((stringp match)
12838                     ;; Regular expression.
12839                     (condition-case ()
12840                         (re-search-forward match nil t)
12841                       (error nil)))
12842                    ((gnus-functionp match)
12843                     ;; Function.
12844                     (save-restriction
12845                       (widen)
12846                       (setq result (funcall match gnus-newsgroup-name))))
12847                    ((consp match)
12848                     ;; Form.
12849                     (save-restriction
12850                       (widen)
12851                       (setq result (eval match)))))
12852               (setq split-name (append (cdr method) split-name))
12853               (cond ((stringp result)
12854                      (push result split-name))
12855                     ((consp result)
12856                      (setq split-name (append result split-name)))))))))
12857     split-name))
12858
12859 (defun gnus-read-move-group-name (prompt default articles prefix)
12860   "Read a group name."
12861   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12862          (prom
12863           (format "Where do you want to %s %s? "
12864                   prompt
12865                   (if (> (length articles) 1)
12866                       (format "these %d articles" (length articles))
12867                     "this article")))
12868          (to-newsgroup
12869           (cond
12870            ((null split-name)
12871             (completing-read
12872              (concat prom
12873                      (if default
12874                          (format "(default %s) " default)
12875                        ""))
12876              gnus-active-hashtb nil nil prefix))
12877            ((= 1 (length split-name))
12878             (completing-read prom gnus-active-hashtb
12879                              nil nil (cons (car split-name) 0)))
12880            (t
12881             (completing-read
12882              prom (mapcar (lambda (el) (list el)) (nreverse split-name)))))))
12883
12884     (when to-newsgroup
12885       (if (or (string= to-newsgroup "")
12886               (string= to-newsgroup prefix))
12887           (setq to-newsgroup (or default "")))
12888       (or (gnus-active to-newsgroup)
12889           (gnus-activate-group to-newsgroup)
12890           (error "No such group: %s" to-newsgroup)))
12891     to-newsgroup))
12892
12893 (defun gnus-read-save-file-name (prompt default-name)
12894   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12895          (file
12896           ;; Let the split methods have their say.
12897           (cond
12898            ;; No split name was found.
12899            ((null split-name)
12900             (read-file-name
12901              (concat prompt " (default "
12902                      (file-name-nondirectory default-name) ") ")
12903              (file-name-directory default-name)
12904              default-name))
12905            ;; A single split name was found
12906            ((= 1 (length split-name))
12907             (read-file-name
12908              (concat prompt " (default " (car split-name) ") ")
12909              gnus-article-save-directory
12910              (concat gnus-article-save-directory (car split-name))))
12911            ;; A list of splits was found.
12912            (t
12913             (setq split-name (mapcar (lambda (el) (list el))
12914                                      (nreverse split-name)))
12915             (let ((result (completing-read
12916                            (concat prompt " ") split-name nil nil)))
12917               (concat gnus-article-save-directory
12918                       (if (string= result "")
12919                           (caar split-name)
12920                         result)))))))
12921     ;; If we have read a directory, we append the default file name.
12922     (when (file-directory-p file)
12923       (setq file (concat (file-name-as-directory file)
12924                          (file-name-nondirectory default-name))))
12925     ;; Possibly translate some charaters.
12926     (nnheader-translate-file-chars file)))
12927
12928 (defun gnus-article-archive-name (group)
12929   "Return the first instance of an \"Archive-name\" in the current buffer."
12930   (let ((case-fold-search t))
12931     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12932       (match-string 1))))
12933
12934 (defun gnus-summary-save-in-rmail (&optional filename)
12935   "Append this article to Rmail file.
12936 Optional argument FILENAME specifies file name.
12937 Directory to save to is default to `gnus-article-save-directory' which
12938 is initialized from the SAVEDIR environment variable."
12939   (interactive)
12940   (gnus-set-global-variables)
12941   (let ((default-name
12942           (funcall gnus-rmail-save-name gnus-newsgroup-name
12943                    gnus-current-headers gnus-newsgroup-last-rmail)))
12944     (setq filename
12945           (cond ((eq filename 'default)
12946                  default-name)
12947                 (filename filename)
12948                 (t (gnus-read-save-file-name
12949                     "Save in rmail file:" default-name))))
12950     (gnus-make-directory (file-name-directory filename))
12951     (gnus-eval-in-buffer-window
12952      gnus-original-article-buffer
12953      (save-excursion
12954        (save-restriction
12955          (widen)
12956          (gnus-output-to-rmail filename))))
12957     ;; Remember the directory name to save articles
12958     (setq gnus-newsgroup-last-rmail filename)))
12959
12960 (defun gnus-summary-save-in-mail (&optional filename)
12961   "Append this article to Unix mail file.
12962 Optional argument FILENAME specifies file name.
12963 Directory to save to is default to `gnus-article-save-directory' which
12964 is initialized from the SAVEDIR environment variable."
12965   (interactive)
12966   (gnus-set-global-variables)
12967   (let ((default-name
12968           (funcall gnus-mail-save-name gnus-newsgroup-name
12969                    gnus-current-headers gnus-newsgroup-last-mail)))
12970     (setq filename
12971           (cond ((eq filename 'default)
12972                  default-name)
12973                 (filename filename)
12974                 (t (gnus-read-save-file-name
12975                     "Save in Unix mail file:" default-name))))
12976     (setq filename
12977           (expand-file-name filename
12978                             (and default-name
12979                                  (file-name-directory default-name))))
12980     (gnus-make-directory (file-name-directory filename))
12981     (gnus-eval-in-buffer-window
12982      gnus-original-article-buffer
12983      (save-excursion
12984        (save-restriction
12985          (widen)
12986          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
12987              (gnus-output-to-rmail filename)
12988            (let ((mail-use-rfc822 t))
12989              (rmail-output filename 1 t t))))))
12990     ;; Remember the directory name to save articles.
12991     (setq gnus-newsgroup-last-mail filename)))
12992
12993 (defun gnus-summary-save-in-file (&optional filename)
12994   "Append this article to file.
12995 Optional argument FILENAME specifies file name.
12996 Directory to save to is default to `gnus-article-save-directory' which
12997 is initialized from the SAVEDIR environment variable."
12998   (interactive)
12999   (gnus-set-global-variables)
13000   (let ((default-name
13001           (funcall gnus-file-save-name gnus-newsgroup-name
13002                    gnus-current-headers gnus-newsgroup-last-file)))
13003     (setq filename
13004           (cond ((eq filename 'default)
13005                  default-name)
13006                 (filename filename)
13007                 (t (gnus-read-save-file-name
13008                     "Save in file:" default-name))))
13009     (gnus-make-directory (file-name-directory filename))
13010     (gnus-eval-in-buffer-window
13011      gnus-original-article-buffer
13012      (save-excursion
13013        (save-restriction
13014          (widen)
13015          (gnus-output-to-file filename))))
13016     ;; Remember the directory name to save articles.
13017     (setq gnus-newsgroup-last-file filename)))
13018
13019 (defun gnus-summary-save-body-in-file (&optional filename)
13020   "Append this article body to a file.
13021 Optional argument FILENAME specifies file name.
13022 The directory to save in defaults to `gnus-article-save-directory' which
13023 is initialized from the SAVEDIR environment variable."
13024   (interactive)
13025   (gnus-set-global-variables)
13026   (let ((default-name
13027           (funcall gnus-file-save-name gnus-newsgroup-name
13028                    gnus-current-headers gnus-newsgroup-last-file)))
13029     (setq filename
13030           (cond ((eq filename 'default)
13031                  default-name)
13032                 (filename filename)
13033                 (t (gnus-read-save-file-name
13034                     "Save body in file:" default-name))))
13035     (gnus-make-directory (file-name-directory filename))
13036     (gnus-eval-in-buffer-window
13037      gnus-article-buffer
13038      (save-excursion
13039        (save-restriction
13040          (widen)
13041          (goto-char (point-min))
13042          (and (search-forward "\n\n" nil t)
13043               (narrow-to-region (point) (point-max)))
13044          (gnus-output-to-file filename))))
13045     ;; Remember the directory name to save articles.
13046     (setq gnus-newsgroup-last-file filename)))
13047
13048 (defun gnus-summary-save-in-pipe (&optional command)
13049   "Pipe this article to subprocess."
13050   (interactive)
13051   (gnus-set-global-variables)
13052   (setq command
13053         (cond ((eq command 'default)
13054                gnus-last-shell-command)
13055               (command command)
13056               (t (read-string "Shell command on article: "
13057                               gnus-last-shell-command))))
13058   (if (string-equal command "")
13059       (setq command gnus-last-shell-command))
13060   (gnus-eval-in-buffer-window
13061    gnus-article-buffer
13062    (save-restriction
13063      (widen)
13064      (shell-command-on-region (point-min) (point-max) command nil)))
13065   (setq gnus-last-shell-command command))
13066
13067 ;; Summary extract commands
13068
13069 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13070   (let ((buffer-read-only nil)
13071         (article (gnus-summary-article-number))
13072         after-article b e)
13073     (or (gnus-summary-goto-subject article)
13074         (error (format "No such article: %d" article)))
13075     (gnus-summary-position-point)
13076     ;; If all commands are to be bunched up on one line, we collect
13077     ;; them here.
13078     (if gnus-view-pseudos-separately
13079         ()
13080       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13081             files action)
13082         (while ps
13083           (setq action (cdr (assq 'action (car ps))))
13084           (setq files (list (cdr (assq 'name (car ps)))))
13085           (while (and ps (cdr ps)
13086                       (string= (or action "1")
13087                                (or (cdr (assq 'action (cadr ps))) "2")))
13088             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13089             (setcdr ps (cddr ps)))
13090           (if (not files)
13091               ()
13092             (if (not (string-match "%s" action))
13093                 (setq files (cons " " files)))
13094             (setq files (cons " " files))
13095             (and (assq 'execute (car ps))
13096                  (setcdr (assq 'execute (car ps))
13097                          (funcall (if (string-match "%s" action)
13098                                       'format 'concat)
13099                                   action
13100                                   (mapconcat (lambda (f) f) files " ")))))
13101           (setq ps (cdr ps)))))
13102     (if (and gnus-view-pseudos (not not-view))
13103         (while pslist
13104           (and (assq 'execute (car pslist))
13105                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13106                                      (eq gnus-view-pseudos 'not-confirm)))
13107           (setq pslist (cdr pslist)))
13108       (save-excursion
13109         (while pslist
13110           (setq after-article (or (cdr (assq 'article (car pslist)))
13111                                   (gnus-summary-article-number)))
13112           (gnus-summary-goto-subject after-article)
13113           (forward-line 1)
13114           (setq b (point))
13115           (insert "    " (file-name-nondirectory
13116                                 (cdr (assq 'name (car pslist))))
13117                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13118           (setq e (point))
13119           (forward-line -1)             ; back to `b'
13120           (add-text-properties
13121            b e (list 'gnus-number gnus-reffed-article-number
13122                      gnus-mouse-face-prop gnus-mouse-face))
13123           (gnus-data-enter
13124            after-article gnus-reffed-article-number
13125            gnus-unread-mark b (car pslist) 0 (- e b))
13126           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13127           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13128           (setq pslist (cdr pslist)))))))
13129
13130 (defun gnus-pseudos< (p1 p2)
13131   (let ((c1 (cdr (assq 'action p1)))
13132         (c2 (cdr (assq 'action p2))))
13133     (and c1 c2 (string< c1 c2))))
13134
13135 (defun gnus-request-pseudo-article (props)
13136   (cond ((assq 'execute props)
13137          (gnus-execute-command (cdr (assq 'execute props)))))
13138   (let ((gnus-current-article (gnus-summary-article-number)))
13139     (run-hooks 'gnus-mark-article-hook)))
13140
13141 (defun gnus-execute-command (command &optional automatic)
13142   (save-excursion
13143     (gnus-article-setup-buffer)
13144     (set-buffer gnus-article-buffer)
13145     (let ((command (if automatic command (read-string "Command: " command)))
13146           (buffer-read-only nil))
13147       (erase-buffer)
13148       (insert "$ " command "\n\n")
13149       (if gnus-view-pseudo-asynchronously
13150           (start-process "gnus-execute" nil "sh" "-c" command)
13151         (call-process "sh" nil t nil "-c" command)))))
13152
13153 (defun gnus-copy-file (file &optional to)
13154   "Copy FILE to TO."
13155   (interactive
13156    (list (read-file-name "Copy file: " default-directory)
13157          (read-file-name "Copy file to: " default-directory)))
13158   (gnus-set-global-variables)
13159   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13160   (and (file-directory-p to)
13161        (setq to (concat (file-name-as-directory to)
13162                         (file-name-nondirectory file))))
13163   (copy-file file to))
13164
13165 ;; Summary kill commands.
13166
13167 (defun gnus-summary-edit-global-kill (article)
13168   "Edit the \"global\" kill file."
13169   (interactive (list (gnus-summary-article-number)))
13170   (gnus-set-global-variables)
13171   (gnus-group-edit-global-kill article))
13172
13173 (defun gnus-summary-edit-local-kill ()
13174   "Edit a local kill file applied to the current newsgroup."
13175   (interactive)
13176   (gnus-set-global-variables)
13177   (setq gnus-current-headers (gnus-summary-article-header))
13178   (gnus-set-global-variables)
13179   (gnus-group-edit-local-kill
13180    (gnus-summary-article-number) gnus-newsgroup-name))
13181
13182 \f
13183 ;;;
13184 ;;; Gnus article mode
13185 ;;;
13186
13187 (put 'gnus-article-mode 'mode-class 'special)
13188
13189 (if gnus-article-mode-map
13190     nil
13191   (setq gnus-article-mode-map (make-keymap))
13192   (suppress-keymap gnus-article-mode-map)
13193
13194   (gnus-define-keys gnus-article-mode-map
13195     " " gnus-article-goto-next-page
13196     "\177" gnus-article-goto-prev-page
13197     [delete] gnus-article-goto-prev-page
13198     "\C-c^" gnus-article-refer-article
13199     "h" gnus-article-show-summary
13200     "s" gnus-article-show-summary
13201     "\C-c\C-m" gnus-article-mail
13202     "?" gnus-article-describe-briefly
13203     gnus-mouse-2 gnus-article-push-button
13204     "\r" gnus-article-press-button
13205     "\t" gnus-article-next-button
13206     "\M-\t" gnus-article-prev-button
13207     "\C-c\C-b" gnus-bug)
13208
13209   (substitute-key-definition
13210    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13211
13212 (defun gnus-article-mode ()
13213   "Major mode for displaying an article.
13214
13215 All normal editing commands are switched off.
13216
13217 The following commands are available:
13218
13219 \\<gnus-article-mode-map>
13220 \\[gnus-article-next-page]\t Scroll the article one page forwards
13221 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13222 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13223 \\[gnus-article-show-summary]\t Display the summary buffer
13224 \\[gnus-article-mail]\t Send a reply to the address near point
13225 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13226 \\[gnus-info-find-node]\t Go to the Gnus info node"
13227   (interactive)
13228   (when (and menu-bar-mode
13229              (gnus-visual-p 'article-menu 'menu))
13230     (gnus-article-make-menu-bar))
13231   (kill-all-local-variables)
13232   (gnus-simplify-mode-line)
13233   (setq mode-name "Article")
13234   (setq major-mode 'gnus-article-mode)
13235   (make-local-variable 'minor-mode-alist)
13236   (or (assq 'gnus-show-mime minor-mode-alist)
13237       (setq minor-mode-alist
13238             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13239   (use-local-map gnus-article-mode-map)
13240   (make-local-variable 'page-delimiter)
13241   (setq page-delimiter gnus-page-delimiter)
13242   (buffer-disable-undo (current-buffer))
13243   (setq buffer-read-only t)             ;Disable modification
13244   (run-hooks 'gnus-article-mode-hook))
13245
13246 (defun gnus-article-setup-buffer ()
13247   "Initialize the article buffer."
13248   (let* ((name (if gnus-single-article-buffer "*Article*"
13249                  (concat "*Article " gnus-newsgroup-name "*")))
13250          (original
13251           (progn (string-match "\\*Article" name)
13252                  (concat " *Original Article"
13253                          (substring name (match-end 0))))))
13254     (setq gnus-article-buffer name)
13255     (setq gnus-original-article-buffer original)
13256     ;; This might be a variable local to the summary buffer.
13257     (unless gnus-single-article-buffer
13258       (save-excursion
13259         (set-buffer gnus-summary-buffer)
13260         (setq gnus-article-buffer name)
13261         (setq gnus-original-article-buffer original)
13262         (gnus-set-global-variables))
13263       (make-local-variable 'gnus-summary-buffer))
13264     ;; Init original article buffer.
13265     (save-excursion
13266       (set-buffer (get-buffer-create gnus-original-article-buffer))
13267       (buffer-disable-undo (current-buffer))
13268       (setq major-mode 'gnus-original-article-mode)
13269       (make-local-variable 'gnus-original-article))
13270     (if (get-buffer name)
13271         (save-excursion
13272           (set-buffer name)
13273           (buffer-disable-undo (current-buffer))
13274           (setq buffer-read-only t)
13275           (gnus-add-current-to-buffer-list)
13276           (or (eq major-mode 'gnus-article-mode)
13277               (gnus-article-mode))
13278           (current-buffer))
13279       (save-excursion
13280         (set-buffer (get-buffer-create name))
13281         (gnus-add-current-to-buffer-list)
13282         (gnus-article-mode)
13283         (current-buffer)))))
13284
13285 ;; Set article window start at LINE, where LINE is the number of lines
13286 ;; from the head of the article.
13287 (defun gnus-article-set-window-start (&optional line)
13288   (set-window-start
13289    (get-buffer-window gnus-article-buffer)
13290    (save-excursion
13291      (set-buffer gnus-article-buffer)
13292      (goto-char (point-min))
13293      (if (not line)
13294          (point-min)
13295        (gnus-message 6 "Moved to bookmark")
13296        (search-forward "\n\n" nil t)
13297        (forward-line line)
13298        (point)))))
13299
13300 (defun gnus-kill-all-overlays ()
13301   "Delete all overlays in the current buffer."
13302   (when (fboundp 'overlay-lists)
13303     (let* ((overlayss (overlay-lists))
13304            (buffer-read-only nil)
13305            (overlays (nconc (car overlayss) (cdr overlayss))))
13306       (while overlays
13307         (delete-overlay (pop overlays))))))
13308
13309 (defun gnus-request-article-this-buffer (article group)
13310   "Get an article and insert it into this buffer."
13311   (let (do-update-line)
13312     (prog1
13313         (save-excursion
13314           (erase-buffer)
13315           (gnus-kill-all-overlays)
13316           (setq group (or group gnus-newsgroup-name))
13317
13318           ;; Open server if it has closed.
13319           (gnus-check-server (gnus-find-method-for-group group))
13320
13321           ;; Using `gnus-request-article' directly will insert the article into
13322           ;; `nntp-server-buffer' - so we'll save some time by not having to
13323           ;; copy it from the server buffer into the article buffer.
13324
13325           ;; We only request an article by message-id when we do not have the
13326           ;; headers for it, so we'll have to get those.
13327           (when (stringp article)
13328             (let ((gnus-override-method gnus-refer-article-method))
13329               (gnus-read-header article)))
13330
13331           ;; If the article number is negative, that means that this article
13332           ;; doesn't belong in this newsgroup (possibly), so we find its
13333           ;; message-id and request it by id instead of number.
13334           (when (and (numberp article)
13335                      gnus-summary-buffer
13336                      (get-buffer gnus-summary-buffer)
13337                      (buffer-name (get-buffer gnus-summary-buffer)))
13338             (save-excursion
13339               (set-buffer gnus-summary-buffer)
13340               (let ((header (gnus-summary-article-header article)))
13341                 (if (< article 0)
13342                     (cond 
13343                      ((memq article gnus-newsgroup-sparse)
13344                       ;; This is a sparse gap article.
13345                       (setq do-update-line article)
13346                       (setq article (mail-header-id header))
13347                       (let ((gnus-override-method gnus-refer-article-method))
13348                         (gnus-read-header article)))
13349                      ((vectorp header)
13350                       ;; It's a real article.
13351                       (setq article (mail-header-id header)))
13352                      (t
13353                       ;; It is an extracted pseudo-article.
13354                       (setq article 'pseudo)
13355                       (gnus-request-pseudo-article header))))
13356                 
13357                 (let ((method (gnus-find-method-for-group 
13358                                gnus-newsgroup-name)))
13359                   (if (not (eq (car method) 'nneething))
13360                       ()
13361                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13362                                        (mail-header-subject header))))
13363                       (if (file-directory-p dir)
13364                           (progn
13365                             (setq article 'nneething)
13366                             (gnus-group-enter-directory dir)))))))))
13367
13368           (cond
13369            ;; We first check `gnus-original-article-buffer'.
13370            ((and (get-buffer gnus-original-article-buffer)
13371                  (save-excursion
13372                    (set-buffer gnus-original-article-buffer)
13373                    (and (equal (car gnus-original-article) group)
13374                         (eq (cdr gnus-original-article) article))))
13375             (insert-buffer-substring gnus-original-article-buffer)
13376             'article)
13377            ;; Check the backlog.
13378            ((and gnus-keep-backlog
13379                  (gnus-backlog-request-article group article (current-buffer)))
13380             'article)
13381            ;; Check the cache.
13382            ((and gnus-use-cache
13383                  (numberp article)
13384                  (gnus-cache-request-article article group))
13385             'article)
13386            ;; Get the article and put into the article buffer.
13387            ((or (stringp article) (numberp article))
13388             (let ((gnus-override-method
13389                    (and (stringp article) gnus-refer-article-method))
13390                   (buffer-read-only nil))
13391               (erase-buffer)
13392               (gnus-kill-all-overlays)
13393               (if (gnus-request-article article group (current-buffer))
13394                   (progn
13395                     (and gnus-keep-backlog
13396                          (numberp article)
13397                          (gnus-backlog-enter-article
13398                           group article (current-buffer)))
13399                     'article))))
13400            ;; It was a pseudo.
13401            (t article)))
13402
13403       ;; Take the article from the original article buffer
13404       ;; and place it in the buffer it's supposed to be in.
13405       (when (and (get-buffer gnus-article-buffer)
13406                  (equal (buffer-name (current-buffer))
13407                         (buffer-name (get-buffer gnus-article-buffer))))
13408         (save-excursion
13409           (if (get-buffer gnus-original-article-buffer)
13410               (set-buffer (get-buffer gnus-original-article-buffer))
13411             (set-buffer (get-buffer-create gnus-original-article-buffer))
13412             (buffer-disable-undo (current-buffer))
13413             (setq major-mode 'gnus-original-article-mode)
13414             (setq buffer-read-only t)
13415             (gnus-add-current-to-buffer-list))
13416           (let (buffer-read-only)
13417             (erase-buffer)
13418             (insert-buffer-substring gnus-article-buffer))
13419           (setq gnus-original-article (cons group article))))
13420     
13421       ;; Update sparse articles.
13422       (when do-update-line
13423         (save-excursion
13424           (set-buffer gnus-summary-buffer)
13425           (gnus-summary-update-article do-update-line)
13426           (gnus-summary-goto-subject do-update-line)
13427           (set-window-point (get-buffer-window (current-buffer) t)
13428                             (point)))))))
13429
13430 (defun gnus-read-header (id &optional header)
13431   "Read the headers of article ID and enter them into the Gnus system."
13432   (let ((group gnus-newsgroup-name)
13433         where)
13434     ;; First we check to see whether the header in question is already
13435     ;; fetched.
13436     (if (stringp id)
13437         ;; This is a Message-ID.
13438         (setq header (or header (gnus-id-to-header id)))
13439       ;; This is an article number.
13440       (setq header (or header (gnus-summary-article-header id))))
13441     (if (and header
13442              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13443         ;; We have found the header.
13444         header
13445       ;; We have to really fetch the header to this article.
13446       (when (setq where
13447                   (if (gnus-check-backend-function 'request-head group)
13448                       (gnus-request-head id group)
13449                     (gnus-request-article id group)))
13450         (save-excursion
13451           (set-buffer nntp-server-buffer)
13452           (and (search-forward "\n\n" nil t)
13453                (delete-region (1- (point)) (point-max)))
13454           (goto-char (point-max))
13455           (insert ".\n")
13456           (goto-char (point-min))
13457           (insert "211 ")
13458           (princ (cond
13459                   ((numberp id) id)
13460                   ((cdr where) (cdr where))
13461                   (header (mail-header-number header))
13462                   (t gnus-reffed-article-number))
13463                  (current-buffer))
13464           (insert " Article retrieved.\n"))
13465         ;(when (and header
13466         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13467         ;  (setcar (gnus-id-to-thread id) nil))
13468         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13469             ()                          ; Malformed head.
13470           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13471             (if (and (stringp id)
13472                      (not (string= (gnus-group-real-name group)
13473                                    (car where))))
13474                 ;; If we fetched by Message-ID and the article came
13475                 ;; from a different group, we fudge some bogus article
13476                 ;; numbers for this article.
13477                 (mail-header-set-number header gnus-reffed-article-number))
13478             (decf gnus-reffed-article-number)
13479             (push header gnus-newsgroup-headers)
13480             (setq gnus-current-headers header)
13481             (push (mail-header-number header) gnus-newsgroup-limit))
13482           header)))))
13483
13484 (defun gnus-article-prepare (article &optional all-headers header)
13485   "Prepare ARTICLE in article mode buffer.
13486 ARTICLE should either be an article number or a Message-ID.
13487 If ARTICLE is an id, HEADER should be the article headers.
13488 If ALL-HEADERS is non-nil, no headers are hidden."
13489   (save-excursion
13490     ;; Make sure we start in a summary buffer.
13491     (unless (eq major-mode 'gnus-summary-mode)
13492       (set-buffer gnus-summary-buffer))
13493     (setq gnus-summary-buffer (current-buffer))
13494     ;; Make sure the connection to the server is alive.
13495     (unless (gnus-server-opened
13496              (gnus-find-method-for-group gnus-newsgroup-name))
13497       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13498       (gnus-request-group gnus-newsgroup-name t))
13499     (let* ((article (if header (mail-header-number header) article))
13500            (summary-buffer (current-buffer))
13501            (internal-hook gnus-article-internal-prepare-hook)
13502            (group gnus-newsgroup-name)
13503            result)
13504       (save-excursion
13505         (gnus-article-setup-buffer)
13506         (set-buffer gnus-article-buffer)
13507         ;; Deactivate active regions.
13508         (when (and (boundp 'transient-mark-mode)
13509                    transient-mark-mode)
13510           (setq mark-active nil))
13511         (if (not (setq result (let ((buffer-read-only nil))
13512                                 (gnus-request-article-this-buffer
13513                                  article group))))
13514             ;; There is no such article.
13515             (save-excursion
13516               (when (and (numberp article)
13517                          (not (memq article gnus-newsgroup-sparse)))
13518                 (setq gnus-article-current
13519                       (cons gnus-newsgroup-name article))
13520                 (set-buffer gnus-summary-buffer)
13521                 (setq gnus-current-article article)
13522                 (gnus-summary-mark-article article gnus-canceled-mark))
13523               (unless (memq article gnus-newsgroup-sparse)
13524                 (gnus-error
13525                  1 "No such article (may have expired or been canceled)")))
13526           (if (or (eq result 'pseudo) (eq result 'nneething))
13527               (progn
13528                 (save-excursion
13529                   (set-buffer summary-buffer)
13530                   (setq gnus-last-article gnus-current-article
13531                         gnus-newsgroup-history (cons gnus-current-article
13532                                                      gnus-newsgroup-history)
13533                         gnus-current-article 0
13534                         gnus-current-headers nil
13535                         gnus-article-current nil)
13536                   (if (eq result 'nneething)
13537                       (gnus-configure-windows 'summary)
13538                     (gnus-configure-windows 'article))
13539                   (gnus-set-global-variables))
13540                 (gnus-set-mode-line 'article))
13541             ;; The result from the `request' was an actual article -
13542             ;; or at least some text that is now displayed in the
13543             ;; article buffer.
13544             (if (and (numberp article)
13545                      (not (eq article gnus-current-article)))
13546                 ;; Seems like a new article has been selected.
13547                 ;; `gnus-current-article' must be an article number.
13548                 (save-excursion
13549                   (set-buffer summary-buffer)
13550                   (setq gnus-last-article gnus-current-article
13551                         gnus-newsgroup-history (cons gnus-current-article
13552                                                      gnus-newsgroup-history)
13553                         gnus-current-article article
13554                         gnus-current-headers
13555                         (gnus-summary-article-header gnus-current-article)
13556                         gnus-article-current
13557                         (cons gnus-newsgroup-name gnus-current-article))
13558                   (unless (vectorp gnus-current-headers)
13559                     (setq gnus-current-headers nil))
13560                   (gnus-summary-show-thread)
13561                   (run-hooks 'gnus-mark-article-hook)
13562                   (gnus-set-mode-line 'summary)
13563                   (and (gnus-visual-p 'article-highlight 'highlight)
13564                        (run-hooks 'gnus-visual-mark-article-hook))
13565                   ;; Set the global newsgroup variables here.
13566                   ;; Suggested by Jim Sisolak
13567                   ;; <sisolak@trans4.neep.wisc.edu>.
13568                   (gnus-set-global-variables)
13569                   (setq gnus-have-all-headers
13570                         (or all-headers gnus-show-all-headers))
13571                   (and gnus-use-cache
13572                        (vectorp (gnus-summary-article-header article))
13573                        (gnus-cache-possibly-enter-article
13574                         group article
13575                         (gnus-summary-article-header article)
13576                         (memq article gnus-newsgroup-marked)
13577                         (memq article gnus-newsgroup-dormant)
13578                         (memq article gnus-newsgroup-unreads)))))
13579             ;; Hooks for getting information from the article.
13580             ;; This hook must be called before being narrowed.
13581             (let (buffer-read-only)
13582               (run-hooks 'internal-hook)
13583               (run-hooks 'gnus-article-prepare-hook)
13584               ;; Decode MIME message.
13585               (if gnus-show-mime
13586                   (if (or (not gnus-strict-mime)
13587                           (gnus-fetch-field "Mime-Version"))
13588                       (funcall gnus-show-mime-method)
13589                     (funcall gnus-decode-encoded-word-method)))
13590               ;; Perform the article display hooks.
13591               (run-hooks 'gnus-article-display-hook))
13592             ;; Do page break.
13593             (goto-char (point-min))
13594             (and gnus-break-pages (gnus-narrow-to-page))
13595             (gnus-set-mode-line 'article)
13596             (gnus-configure-windows 'article)
13597             (goto-char (point-min))
13598             t))))))
13599
13600 (defun gnus-article-show-all-headers ()
13601   "Show all article headers in article mode buffer."
13602   (save-excursion
13603     (gnus-article-setup-buffer)
13604     (set-buffer gnus-article-buffer)
13605     (let ((buffer-read-only nil))
13606       (gnus-unhide-text (point-min) (point-max)))))
13607
13608 (defun gnus-article-hide-headers-if-wanted ()
13609   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13610 Provided for backwards compatibility."
13611   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13612       gnus-inhibit-hiding
13613       (gnus-article-hide-headers)))
13614
13615 (defsubst gnus-article-header-rank ()
13616   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13617   (let ((list gnus-sorted-header-list)
13618         (i 0))
13619     (while list
13620       (when (looking-at (car list))
13621         (setq list nil))
13622       (setq list (cdr list))
13623       (incf i))
13624     i))
13625
13626 (defun gnus-article-hide-headers (&optional arg delete)
13627   "Toggle whether to hide unwanted headers and possibly sort them as well.
13628 If given a negative prefix, always show; if given a positive prefix,
13629 always hide."
13630   (interactive "P")
13631   (unless (gnus-article-check-hidden-text 'headers arg)
13632     ;; This function might be inhibited.
13633     (unless gnus-inhibit-hiding
13634       (save-excursion
13635         (set-buffer gnus-article-buffer)
13636         (save-restriction
13637           (let ((buffer-read-only nil)
13638                 (props (nconc (list 'gnus-type 'headers)
13639                               gnus-hidden-properties))
13640                 (max (1+ (length gnus-sorted-header-list)))
13641                 (ignored (when (not (stringp gnus-visible-headers))
13642                            (cond ((stringp gnus-ignored-headers)
13643                                   gnus-ignored-headers)
13644                                  ((listp gnus-ignored-headers)
13645                                   (mapconcat 'identity gnus-ignored-headers
13646                                              "\\|")))))
13647                 (visible
13648                  (cond ((stringp gnus-visible-headers)
13649                         gnus-visible-headers)
13650                        ((and gnus-visible-headers
13651                              (listp gnus-visible-headers))
13652                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13653                 want-list beg)
13654             ;; First we narrow to just the headers.
13655             (widen)
13656             (goto-char (point-min))
13657             ;; Hide any "From " lines at the beginning of (mail) articles.
13658             (while (looking-at "From ")
13659               (forward-line 1))
13660             (unless (bobp)
13661               (gnus-hide-text (point-min) (point) props))
13662             ;; Then treat the rest of the header lines.
13663             (narrow-to-region
13664              (point)
13665              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13666             ;; Then we use the two regular expressions
13667             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13668             ;; select which header lines is to remain visible in the
13669             ;; article buffer.
13670             (goto-char (point-min))
13671             (while (re-search-forward "^[^ \t]*:" nil t)
13672               (beginning-of-line)
13673               ;; We add the headers we want to keep to a list and delete
13674               ;; them from the buffer.
13675               (put-text-property 
13676                (point) (1+ (point)) 'message-rank
13677                (if (or (and visible (looking-at visible))
13678                        (and ignored
13679                             (not (looking-at ignored))))
13680                    (gnus-article-header-rank) 
13681                  (+ 2 max)))
13682               (forward-line 1))
13683             (message-sort-headers-1)
13684             (when (setq beg (text-property-any 
13685                              (point-min) (point-max) 'message-rank (+ 2 max)))
13686               ;; We make the unwanted headers invisible.
13687               (if delete
13688                   (delete-region beg (point-max))
13689                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13690                 (gnus-hide-text-type beg (point-max) 'headers)))))))))
13691
13692 (defun gnus-article-hide-boring-headers (&optional arg)
13693   "Toggle hiding of headers that aren't very interesting.
13694 If given a negative prefix, always show; if given a positive prefix,
13695 always hide."
13696   (interactive "P")
13697   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13698     (save-excursion
13699       (set-buffer gnus-article-buffer)
13700       (save-restriction
13701         (let ((buffer-read-only nil)
13702               (list gnus-boring-article-headers)
13703               (inhibit-point-motion-hooks t)
13704               elem)
13705           (nnheader-narrow-to-headers)
13706           (while list
13707             (setq elem (pop list))
13708             (goto-char (point-min))
13709             (cond
13710              ;; Hide empty headers.
13711              ((eq elem 'empty)
13712               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13713                 (forward-line -1)
13714                 (gnus-hide-text-type
13715                  (progn (beginning-of-line) (point))
13716                  (progn 
13717                    (end-of-line)
13718                    (if (re-search-forward "^[^ \t]" nil t)
13719                        (match-beginning 0)
13720                      (point-max)))
13721                  'boring-headers)))
13722              ;; Hide boring Newsgroups header.
13723              ((eq elem 'newsgroups)
13724               (when (equal (mail-fetch-field "newsgroups")
13725                            (gnus-group-real-name gnus-newsgroup-name))
13726                 (gnus-article-hide-header "newsgroups")))
13727              ((eq elem 'followup-to)
13728               (when (equal (mail-fetch-field "followup-to")
13729                            (mail-fetch-field "newsgroups"))
13730                 (gnus-article-hide-header "followup-to")))
13731              ((eq elem 'reply-to)
13732               (let ((from (mail-fetch-field "from"))
13733                     (reply-to (mail-fetch-field "reply-to")))
13734                 (when (and
13735                        from reply-to
13736                        (equal 
13737                         (nth 1 (funcall gnus-extract-address-components from))
13738                         (nth 1 (funcall gnus-extract-address-components
13739                                         reply-to))))
13740                   (gnus-article-hide-header "reply-to"))))
13741              ((eq elem 'date)
13742               (let ((date (mail-fetch-field "date")))
13743                 (when (and date
13744                            (< (gnus-days-between date (current-time-string))
13745                               4))
13746                   (gnus-article-hide-header "date")))))))))))
13747
13748 (defun gnus-article-hide-header (header)
13749   (save-excursion
13750     (goto-char (point-min))
13751     (when (re-search-forward (concat "^" header ":") nil t)
13752       (gnus-hide-text-type
13753        (progn (beginning-of-line) (point))
13754        (progn 
13755          (end-of-line)
13756          (if (re-search-forward "^[^ \t]" nil t)
13757              (match-beginning 0)
13758            (point-max)))
13759        'boring-headers))))
13760
13761 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13762 (defun gnus-article-treat-overstrike ()
13763   "Translate overstrikes into bold text."
13764   (interactive)
13765   (save-excursion
13766     (set-buffer gnus-article-buffer)
13767     (let ((buffer-read-only nil))
13768       (while (search-forward "\b" nil t)
13769         (let ((next (following-char))
13770               (previous (char-after (- (point) 2))))
13771           (cond ((eq next previous)
13772                  (put-text-property (- (point) 2) (point) 'invisible t)
13773                  (put-text-property (point) (1+ (point)) 'face 'bold))
13774                 ((eq next ?_)
13775                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13776                  (put-text-property
13777                   (- (point) 2) (1- (point)) 'face 'underline))
13778                 ((eq previous ?_)
13779                  (put-text-property (- (point) 2) (point) 'invisible t)
13780                  (put-text-property
13781                   (point) (1+ (point))  'face 'underline))))))))
13782
13783 (defun gnus-article-word-wrap ()
13784   "Format too long lines."
13785   (interactive)
13786   (save-excursion
13787     (set-buffer gnus-article-buffer)
13788     (let ((buffer-read-only nil))
13789       (widen)
13790       (goto-char (point-min))
13791       (search-forward "\n\n" nil t)
13792       (end-of-line 1)
13793       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13794             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13795             (adaptive-fill-mode t))
13796         (while (not (eobp))
13797           (and (>= (current-column) (min fill-column (window-width)))
13798                (/= (preceding-char) ?:)
13799                (fill-paragraph nil))
13800           (end-of-line 2))))))
13801
13802 (defun gnus-article-remove-cr ()
13803   "Remove carriage returns from an article."
13804   (interactive)
13805   (save-excursion
13806     (set-buffer gnus-article-buffer)
13807     (let ((buffer-read-only nil))
13808       (goto-char (point-min))
13809       (while (search-forward "\r" nil t)
13810         (replace-match "" t t)))))
13811
13812 (defun gnus-article-remove-trailing-blank-lines ()
13813   "Remove all trailing blank lines from the article."
13814   (interactive)
13815   (save-excursion
13816     (set-buffer gnus-article-buffer)
13817     (let ((buffer-read-only nil))
13818       (goto-char (point-max))
13819       (delete-region
13820        (point)
13821        (progn
13822          (while (looking-at "^[ \t]*$")
13823            (forward-line -1))
13824          (forward-line 1)
13825          (point))))))
13826
13827 (defun gnus-article-display-x-face (&optional force)
13828   "Look for an X-Face header and display it if present."
13829   (interactive (list 'force))
13830   (save-excursion
13831     (set-buffer gnus-article-buffer)
13832     ;; Delete the old process, if any.
13833     (when (process-status "gnus-x-face")
13834       (delete-process "gnus-x-face"))
13835     (let ((inhibit-point-motion-hooks t)
13836           (case-fold-search nil)
13837           from)
13838       (save-restriction
13839         (nnheader-narrow-to-headers)
13840         (setq from (mail-fetch-field "from"))
13841         (goto-char (point-min))
13842         (when (and gnus-article-x-face-command
13843                    (or force
13844                        ;; Check whether this face is censored.
13845                        (not gnus-article-x-face-too-ugly)
13846                        (and gnus-article-x-face-too-ugly from
13847                             (not (string-match gnus-article-x-face-too-ugly
13848                                                from))))
13849                    ;; Has to be present.
13850                    (re-search-forward "^X-Face: " nil t))
13851           ;; We now have the area of the buffer where the X-Face is stored.
13852           (let ((beg (point))
13853                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13854             ;; We display the face.
13855             (if (symbolp gnus-article-x-face-command)
13856                 ;; The command is a lisp function, so we call it.
13857                 (if (gnus-functionp gnus-article-x-face-command)
13858                     (funcall gnus-article-x-face-command beg end)
13859                   (error "%s is not a function" gnus-article-x-face-command))
13860               ;; The command is a string, so we interpret the command
13861               ;; as a, well, command, and fork it off.
13862               (let ((process-connection-type nil))
13863                 (process-kill-without-query
13864                  (start-process
13865                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13866                 (process-send-region "gnus-x-face" beg end)
13867                 (process-send-eof "gnus-x-face")))))))))
13868
13869 (defun gnus-headers-decode-quoted-printable ()
13870   "Hack to remove QP encoding from headers."
13871   (let ((case-fold-search t)
13872         (inhibit-point-motion-hooks t)
13873         string)
13874     (goto-char (point-min))
13875     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13876       (setq string (match-string 1))
13877       (narrow-to-region (match-beginning 0) (match-end 0))
13878       (delete-region (point-min) (point-max))
13879       (insert string)
13880       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13881       (subst-char-in-region (point-min) (point-max) ?_ ? )
13882       (widen)
13883       (goto-char (point-min)))))
13884
13885 (defun gnus-article-de-quoted-unreadable (&optional force)
13886   "Do a naive translation of a quoted-printable-encoded article.
13887 This is in no way, shape or form meant as a replacement for real MIME
13888 processing, but is simply a stop-gap measure until MIME support is
13889 written.
13890 If FORCE, decode the article whether it is marked as quoted-printable
13891 or not."
13892   (interactive (list 'force))
13893   (save-excursion
13894     (set-buffer gnus-article-buffer)
13895     (let ((case-fold-search t)
13896           (buffer-read-only nil)
13897           (type (gnus-fetch-field "content-transfer-encoding")))
13898       (when (or force
13899                 (and type (string-match "quoted-printable" (downcase type))))
13900         (gnus-headers-decode-quoted-printable)
13901         (goto-char (point-min))
13902         (search-forward "\n\n" nil 'move)
13903         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13904
13905 (defun gnus-mime-decode-quoted-printable (from to)
13906   "Decode Quoted-Printable in the region between FROM and TO."
13907   (goto-char from)
13908   (while (search-forward "=" to t)
13909     (cond ((eq (following-char) ?\n)
13910            (delete-char -1)
13911            (delete-char 1))
13912           ((looking-at "[0-9A-F][0-9A-F]")
13913            (delete-char -1)
13914            (insert (hexl-hex-string-to-integer
13915                     (buffer-substring (point) (+ 2 (point)))))
13916            (delete-char 2))
13917           ((looking-at "=")
13918            (delete-char 1))
13919           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13920
13921 (defun gnus-article-hide-pgp (&optional arg)
13922   "Toggle hiding of any PGP headers and signatures in the current article.
13923 If given a negative prefix, always show; if given a positive prefix,
13924 always hide."
13925   (interactive "P")
13926   (unless (gnus-article-check-hidden-text 'pgp arg)
13927     (save-excursion
13928       (set-buffer gnus-article-buffer)
13929       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13930             buffer-read-only beg end)
13931         (widen)
13932         (goto-char (point-min))
13933         ;; Hide the "header".
13934         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13935              (gnus-hide-text (match-beginning 0) (match-end 0) props))
13936         (setq beg (point))
13937         ;; Hide the actual signature.
13938         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13939              (setq end (1+ (match-beginning 0)))
13940              (gnus-hide-text
13941               end
13942               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13943                   (match-end 0)
13944                 ;; Perhaps we shouldn't hide to the end of the buffer
13945                 ;; if there is no end to the signature?
13946                 (point-max))
13947               props))
13948         ;; Hide "- " PGP quotation markers.
13949         (when (and beg end)
13950           (narrow-to-region beg end)
13951           (goto-char (point-min))
13952           (while (re-search-forward "^- " nil t)
13953             (gnus-hide-text (match-beginning 0) (match-end 0) props))
13954           (widen))))))
13955
13956 (defun gnus-article-hide-signature (&optional arg)
13957   "Hide the signature in the current article.
13958 If given a negative prefix, always show; if given a positive prefix,
13959 always hide."
13960   (interactive "P")
13961   (unless (gnus-article-check-hidden-text 'signature arg)
13962     (save-excursion
13963       (set-buffer gnus-article-buffer)
13964       (save-restriction
13965         (let ((buffer-read-only nil))
13966           (when (gnus-narrow-to-signature)
13967             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
13968
13969 (defun gnus-article-strip-leading-blank-lines ()
13970   "Remove all blank lines from the beginning of the article."
13971   (interactive)
13972   (save-excursion
13973     (set-buffer gnus-article-buffer)
13974     (let (buffer-read-only)
13975       (goto-char (point-min))
13976       (when (search-forward "\n\n" nil t)
13977         (while (looking-at "[ \t]$")
13978           (gnus-delete-line))))))
13979
13980 (defun gnus-narrow-to-signature ()
13981   "Narrow to the signature."
13982   (widen)
13983   (goto-char (point-max))
13984   (when (re-search-backward gnus-signature-separator nil t)
13985     (forward-line 1)
13986     (when (or (null gnus-signature-limit)
13987               (and (numberp gnus-signature-limit)
13988                    (< (- (point-max) (point)) gnus-signature-limit))
13989               (and (gnus-functionp gnus-signature-limit)
13990                    (funcall gnus-signature-limit))
13991               (and (stringp gnus-signature-limit)
13992                    (not (re-search-forward gnus-signature-limit nil t))))
13993       (narrow-to-region (point) (point-max))
13994       t)))
13995
13996 (defun gnus-article-check-hidden-text (type arg)
13997   "Return nil if hiding is necessary."
13998   (save-excursion
13999     (set-buffer gnus-article-buffer)
14000     (let ((hide (gnus-article-hidden-text-p type)))
14001       (cond ((or (and (null arg) (eq hide 'hidden))
14002                  (and arg (< 0 (prefix-numeric-value arg))))
14003              (gnus-article-show-hidden-text type))
14004             ((eq hide 'shown)
14005              (gnus-article-show-hidden-text type t))
14006             (t nil)))))
14007
14008 (defun gnus-article-hidden-text-p (type)
14009   "Say whether the current buffer contains hidden text of type TYPE."
14010   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14011     (when pos
14012       (if (get-text-property pos 'invisible)
14013           'hidden
14014         'shown))))
14015
14016 (defun gnus-article-hide (&optional arg force)
14017   "Hide all the gruft in the current article.
14018 This means that PGP stuff, signatures, cited text and (some)
14019 headers will be hidden.
14020 If given a prefix, show the hidden text instead."
14021   (interactive (list current-prefix-arg 'force))
14022   (gnus-article-hide-headers arg)
14023   (gnus-article-hide-pgp arg)
14024   (gnus-article-hide-citation-maybe arg force)
14025   (gnus-article-hide-signature arg))
14026
14027 (defun gnus-article-show-hidden-text (type &optional hide)
14028   "Show all hidden text of type TYPE.
14029 If HIDE, hide the text instead."
14030   (save-excursion
14031     (set-buffer gnus-article-buffer)
14032     (let ((buffer-read-only nil)
14033           (inhibit-point-motion-hooks t)
14034           (beg (point-min)))
14035       (while (gnus-goto-char (text-property-any
14036                               beg (point-max) 'gnus-type type))
14037         (setq beg (point))
14038         (forward-char)
14039         (if hide
14040             (gnus-hide-text beg (point) gnus-hidden-properties)
14041           (gnus-unhide-text beg (point)))
14042         (setq beg (point)))
14043       t)))
14044
14045 (defvar gnus-article-time-units
14046   `((year . ,(* 365.25 24 60 60))
14047     (week . ,(* 7 24 60 60))
14048     (day . ,(* 24 60 60))
14049     (hour . ,(* 60 60))
14050     (minute . 60)
14051     (second . 1))
14052   "Mapping from time units to seconds.")
14053
14054 (defun gnus-article-date-ut (&optional type highlight)
14055   "Convert DATE date to universal time in the current article.
14056 If TYPE is `local', convert to local time; if it is `lapsed', output
14057 how much time has lapsed since DATE."
14058   (interactive (list 'ut t))
14059   (let* ((header (or gnus-current-headers
14060                      (gnus-summary-article-header) ""))
14061          (date (and (vectorp header) (mail-header-date header)))
14062          (date-regexp "^Date: \\|^X-Sent: ")
14063          (now (current-time))
14064          (inhibit-point-motion-hooks t)
14065          bface eface)
14066     (when (and date (not (string= date "")))
14067       (save-excursion
14068         (set-buffer gnus-article-buffer)
14069         (save-restriction
14070           (nnheader-narrow-to-headers)
14071           (let ((buffer-read-only nil))
14072             ;; Delete any old Date headers.
14073             (if (re-search-forward date-regexp nil t)
14074                 (progn
14075                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14076                         eface (get-text-property (gnus-point-at-eol) 'face))
14077                   (message-remove-header date-regexp t)
14078                   (beginning-of-line))
14079               (goto-char (point-max)))
14080             (insert
14081              (cond
14082               ;; Convert to the local timezone.  We have to slap a
14083               ;; `condition-case' round the calls to the timezone
14084               ;; functions since they aren't particularly resistant to
14085               ;; buggy dates.
14086               ((eq type 'local)
14087                (concat "Date: " (condition-case ()
14088                                     (timezone-make-date-arpa-standard date)
14089                                   (error date))
14090                        "\n"))
14091               ;; Convert to Universal Time.
14092               ((eq type 'ut)
14093                (concat "Date: "
14094                        (condition-case ()
14095                            (timezone-make-date-arpa-standard date nil "UT")
14096                          (error date))
14097                        "\n"))
14098               ;; Get the original date from the article.
14099               ((eq type 'original)
14100                (concat "Date: " date "\n"))
14101               ;; Do an X-Sent lapsed format.
14102               ((eq type 'lapsed)
14103                ;; If the date is seriously mangled, the timezone
14104                ;; functions are liable to bug out, so we condition-case
14105                ;; the entire thing.
14106                (let* ((real-time
14107                        (condition-case ()
14108                            (gnus-time-minus
14109                             (gnus-encode-date
14110                              (timezone-make-date-arpa-standard
14111                               (current-time-string now)
14112                               (current-time-zone now) "UT"))
14113                             (gnus-encode-date
14114                              (timezone-make-date-arpa-standard
14115                               date nil "UT")))
14116                          (error '(0 0))))
14117                       (real-sec (+ (* (float (car real-time)) 65536)
14118                                    (cadr real-time)))
14119                       (sec (abs real-sec))
14120                       num prev)
14121                  (if (zerop sec)
14122                      "X-Sent: Now\n"
14123                    (concat
14124                     "X-Sent: "
14125                     ;; This is a bit convoluted, but basically we go
14126                     ;; through the time units for years, weeks, etc,
14127                     ;; and divide things to see whether that results
14128                     ;; in positive answers.
14129                     (mapconcat
14130                      (lambda (unit)
14131                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14132                            ;; The (remaining) seconds are too few to
14133                            ;; be divided into this time unit.
14134                            ""
14135                          ;; It's big enough, so we output it.
14136                          (setq sec (- sec (* num (cdr unit))))
14137                          (prog1
14138                              (concat (if prev ", " "") (int-to-string
14139                                                         (floor num))
14140                                      " " (symbol-name (car unit))
14141                                      (if (> num 1) "s" ""))
14142                            (setq prev t))))
14143                      gnus-article-time-units "")
14144                     ;; If dates are odd, then it might appear like the
14145                     ;; article was sent in the future.
14146                     (if (> real-sec 0)
14147                         " ago\n"
14148                       " in the future\n")))))
14149               (t
14150                (error "Unknown conversion type: %s" type)))))
14151           ;; Do highlighting.
14152           (beginning-of-line)
14153           (when (and highlight (gnus-visual-p 'article-highlight 'highlight)
14154                      (looking-at "\\([^:]\\): *\\(.*\\)$"))
14155             (put-text-property (match-beginning 1) (match-end 1)
14156                                'face bface)
14157             (put-text-property (match-beginning 2) (match-end 2)
14158                                'face eface)))))))
14159
14160 (defun gnus-article-date-local (&optional highlight)
14161   "Convert the current article date to the local timezone."
14162   (interactive (list t))
14163   (gnus-article-date-ut 'local highlight))
14164
14165 (defun gnus-article-date-original (&optional highlight)
14166   "Convert the current article date to what it was originally.
14167 This is only useful if you have used some other date conversion
14168 function and want to see what the date was before converting."
14169   (interactive (list t))
14170   (gnus-article-date-ut 'original highlight))
14171
14172 (defun gnus-article-date-lapsed (&optional highlight)
14173   "Convert the current article date to time lapsed since it was sent."
14174   (interactive (list t))
14175   (gnus-article-date-ut 'lapsed highlight))
14176
14177 (defun gnus-article-maybe-highlight ()
14178   "Do some article highlighting if `gnus-visual' is non-nil."
14179   (if (gnus-visual-p 'article-highlight 'highlight)
14180       (gnus-article-highlight-some)))
14181
14182 ;; Article savers.
14183
14184 (defun gnus-output-to-rmail (file-name)
14185   "Append the current article to an Rmail file named FILE-NAME."
14186   (require 'rmail)
14187   ;; Most of these codes are borrowed from rmailout.el.
14188   (setq file-name (expand-file-name file-name))
14189   (setq rmail-default-rmail-file file-name)
14190   (let ((artbuf (current-buffer))
14191         (tmpbuf (get-buffer-create " *Gnus-output*")))
14192     (save-excursion
14193       (or (get-file-buffer file-name)
14194           (file-exists-p file-name)
14195           (if (gnus-yes-or-no-p
14196                (concat "\"" file-name "\" does not exist, create it? "))
14197               (let ((file-buffer (create-file-buffer file-name)))
14198                 (save-excursion
14199                   (set-buffer file-buffer)
14200                   (rmail-insert-rmail-file-header)
14201                   (let ((require-final-newline nil))
14202                     (write-region (point-min) (point-max) file-name t 1)))
14203                 (kill-buffer file-buffer))
14204             (error "Output file does not exist")))
14205       (set-buffer tmpbuf)
14206       (buffer-disable-undo (current-buffer))
14207       (erase-buffer)
14208       (insert-buffer-substring artbuf)
14209       (gnus-convert-article-to-rmail)
14210       ;; Decide whether to append to a file or to an Emacs buffer.
14211       (let ((outbuf (get-file-buffer file-name)))
14212         (if (not outbuf)
14213             (append-to-file (point-min) (point-max) file-name)
14214           ;; File has been visited, in buffer OUTBUF.
14215           (set-buffer outbuf)
14216           (let ((buffer-read-only nil)
14217                 (msg (and (boundp 'rmail-current-message)
14218                           (symbol-value 'rmail-current-message))))
14219             ;; If MSG is non-nil, buffer is in RMAIL mode.
14220             (if msg
14221                 (progn (widen)
14222                        (narrow-to-region (point-max) (point-max))))
14223             (insert-buffer-substring tmpbuf)
14224             (if msg
14225                 (progn
14226                   (goto-char (point-min))
14227                   (widen)
14228                   (search-backward "\^_")
14229                   (narrow-to-region (point) (point-max))
14230                   (goto-char (1+ (point-min)))
14231                   (rmail-count-new-messages t)
14232                   (rmail-show-message msg)))))))
14233     (kill-buffer tmpbuf)))
14234
14235 (defun gnus-output-to-file (file-name)
14236   "Append the current article to a file named FILE-NAME."
14237   (setq file-name (expand-file-name file-name))
14238   (let ((artbuf (current-buffer))
14239         (tmpbuf (get-buffer-create " *Gnus-output*")))
14240     (save-excursion
14241       (set-buffer tmpbuf)
14242       (buffer-disable-undo (current-buffer))
14243       (erase-buffer)
14244       (insert-buffer-substring artbuf)
14245       ;; Append newline at end of the buffer as separator, and then
14246       ;; save it to file.
14247       (goto-char (point-max))
14248       (insert "\n")
14249       (append-to-file (point-min) (point-max) file-name))
14250     (kill-buffer tmpbuf)))
14251
14252 (defun gnus-convert-article-to-rmail ()
14253   "Convert article in current buffer to Rmail message format."
14254   (let ((buffer-read-only nil))
14255     ;; Convert article directly into Babyl format.
14256     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14257     (goto-char (point-min))
14258     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14259     (while (search-forward "\n\^_" nil t) ;single char
14260       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14261     (goto-char (point-max))
14262     (insert "\^_")))
14263
14264 (defun gnus-narrow-to-page (&optional arg)
14265   "Narrow the article buffer to a page.
14266 If given a numerical ARG, move forward ARG pages."
14267   (interactive "P")
14268   (setq arg (if arg (prefix-numeric-value arg) 0))
14269   (save-excursion
14270     (set-buffer gnus-article-buffer)
14271     (goto-char (point-min))
14272     (widen)
14273     (when (gnus-visual-p 'page-marker)
14274       (let ((buffer-read-only nil))
14275         (gnus-remove-text-with-property 'gnus-prev)
14276         (gnus-remove-text-with-property 'gnus-next)))
14277     (when
14278         (cond ((< arg 0)
14279                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14280               ((> arg 0)
14281                (re-search-forward page-delimiter nil 'move arg)))
14282       (goto-char (match-end 0)))
14283     (narrow-to-region
14284      (point)
14285      (if (re-search-forward page-delimiter nil 'move)
14286          (match-beginning 0)
14287        (point)))
14288     (when (and (gnus-visual-p 'page-marker)
14289                (not (= (point-min) 1)))
14290       (save-excursion
14291         (goto-char (point-min))
14292         (gnus-insert-prev-page-button)))
14293     (when (and (gnus-visual-p 'page-marker)
14294                (not (= (1- (point-max)) (buffer-size))))
14295       (save-excursion
14296         (goto-char (point-max))
14297         (gnus-insert-next-page-button)))))
14298
14299 ;; Article mode commands
14300
14301 (defun gnus-article-goto-next-page ()
14302   "Show the next page of the article."
14303   (interactive)
14304   (when (gnus-article-next-page)
14305     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14306
14307 (defun gnus-article-goto-prev-page ()
14308   "Show the next page of the article."
14309   (interactive)
14310   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14311     (gnus-article-prev-page nil)))
14312
14313 (defun gnus-article-next-page (&optional lines)
14314   "Show the next page of the current article.
14315 If end of article, return non-nil.  Otherwise return nil.
14316 Argument LINES specifies lines to be scrolled up."
14317   (interactive "p")
14318   (move-to-window-line -1)
14319   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14320   (if (save-excursion
14321         (end-of-line)
14322         (and (pos-visible-in-window-p)  ;Not continuation line.
14323              (eobp)))
14324       ;; Nothing in this page.
14325       (if (or (not gnus-break-pages)
14326               (save-excursion
14327                 (save-restriction
14328                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14329           t                             ;Nothing more.
14330         (gnus-narrow-to-page 1)         ;Go to next page.
14331         nil)
14332     ;; More in this page.
14333     (condition-case ()
14334         (scroll-up lines)
14335       (end-of-buffer
14336        ;; Long lines may cause an end-of-buffer error.
14337        (goto-char (point-max))))
14338     (move-to-window-line 0)
14339     nil))
14340
14341 (defun gnus-article-prev-page (&optional lines)
14342   "Show previous page of current article.
14343 Argument LINES specifies lines to be scrolled down."
14344   (interactive "p")
14345   (move-to-window-line 0)
14346   (if (and gnus-break-pages
14347            (bobp)
14348            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14349       (progn
14350         (gnus-narrow-to-page -1)        ;Go to previous page.
14351         (goto-char (point-max))
14352         (recenter -1))
14353     (prog1
14354         (condition-case ()
14355             (scroll-down lines)
14356           (error nil))
14357       (move-to-window-line 0))))
14358
14359 (defun gnus-article-refer-article ()
14360   "Read article specified by message-id around point."
14361   (interactive)
14362   (let ((point (point)))
14363     (search-forward ">" nil t)          ;Move point to end of "<....>".
14364     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14365         (let ((message-id (match-string 1)))
14366           (goto-char point)
14367           (set-buffer gnus-summary-buffer)
14368           (gnus-summary-refer-article message-id))
14369       (goto-char (point))
14370       (error "No references around point"))))
14371
14372 (defun gnus-article-show-summary ()
14373   "Reconfigure windows to show summary buffer."
14374   (interactive)
14375   (gnus-configure-windows 'article)
14376   (gnus-summary-goto-subject gnus-current-article))
14377
14378 (defun gnus-article-describe-briefly ()
14379   "Describe article mode commands briefly."
14380   (interactive)
14381   (gnus-message 6
14382                 (substitute-command-keys "\\<gnus-article-mode-map>\\[gnus-article-next-page]:Next page  \\[gnus-article-prev-page]:Prev page  \\[gnus-article-show-summary]:Show summary  \\[gnus-info-find-node]:Run Info  \\[gnus-article-describe-briefly]:This help")))
14383
14384 (defun gnus-article-summary-command ()
14385   "Execute the last keystroke in the summary buffer."
14386   (interactive)
14387   (let ((obuf (current-buffer))
14388         (owin (current-window-configuration))
14389         func)
14390     (switch-to-buffer gnus-summary-buffer 'norecord)
14391     (setq func (lookup-key (current-local-map) (this-command-keys)))
14392     (call-interactively func)
14393     (set-buffer obuf)
14394     (set-window-configuration owin)
14395     (set-window-point (get-buffer-window (current-buffer)) (point))))
14396
14397 (defun gnus-article-summary-command-nosave ()
14398   "Execute the last keystroke in the summary buffer."
14399   (interactive)
14400   (let (func)
14401     (pop-to-buffer gnus-summary-buffer 'norecord)
14402     (setq func (lookup-key (current-local-map) (this-command-keys)))
14403     (call-interactively func)))
14404
14405 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14406   "Read a summary buffer key sequence and execute it from the article buffer."
14407   (interactive "P")
14408   (let ((nosaves
14409          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14410            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14411            "=" "^" "\M-^"))
14412         keys)
14413     (save-excursion
14414       (set-buffer gnus-summary-buffer)
14415       (push (or key last-command-event) unread-command-events)
14416       (setq keys (read-key-sequence nil)))
14417     (message "")
14418
14419     (if (member keys nosaves)
14420         (let (func)
14421           (pop-to-buffer gnus-summary-buffer 'norecord)
14422           (if (setq func (lookup-key (current-local-map) keys))
14423               (call-interactively func)
14424             (ding)))
14425       (let ((obuf (current-buffer))
14426             (owin (current-window-configuration))
14427             (opoint (point))
14428             func in-buffer)
14429         (if not-restore-window
14430             (pop-to-buffer gnus-summary-buffer 'norecord)
14431           (switch-to-buffer gnus-summary-buffer 'norecord))
14432         (setq in-buffer (current-buffer))
14433         (if (setq func (lookup-key (current-local-map) keys))
14434             (call-interactively func)
14435           (ding))
14436         (when (eq in-buffer (current-buffer))
14437           (set-buffer obuf)
14438           (unless not-restore-window
14439             (set-window-configuration owin))
14440           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14441
14442 \f
14443 ;;;
14444 ;;; Kill file handling.
14445 ;;;
14446
14447 ;;;###autoload
14448 (defalias 'gnus-batch-kill 'gnus-batch-score)
14449 ;;;###autoload
14450 (defun gnus-batch-score ()
14451   "Run batched scoring.
14452 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14453 Newsgroups is a list of strings in Bnews format.  If you want to score
14454 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14455 score the alt hierarchy, you'd say \"!alt.all\"."
14456   (interactive)
14457   (let* ((yes-and-no
14458           (gnus-newsrc-parse-options
14459            (apply (function concat)
14460                   (mapcar (lambda (g) (concat g " "))
14461                           command-line-args-left))))
14462          (gnus-expert-user t)
14463          (nnmail-spool-file nil)
14464          (gnus-use-dribble-file nil)
14465          (yes (car yes-and-no))
14466          (no (cdr yes-and-no))
14467          group newsrc entry
14468          ;; Disable verbose message.
14469          gnus-novice-user gnus-large-newsgroup)
14470     ;; Eat all arguments.
14471     (setq command-line-args-left nil)
14472     ;; Start Gnus.
14473     (gnus)
14474     ;; Apply kills to specified newsgroups in command line arguments.
14475     (setq newsrc (cdr gnus-newsrc-alist))
14476     (while newsrc
14477       (setq group (caar newsrc))
14478       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14479       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14480                (and (car entry)
14481                     (or (eq (car entry) t)
14482                         (not (zerop (car entry)))))
14483                (if yes (string-match yes group) t)
14484                (or (null no) (not (string-match no group))))
14485           (progn
14486             (gnus-summary-read-group group nil t nil t)
14487             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14488                  (gnus-summary-exit))))
14489       (setq newsrc (cdr newsrc)))
14490     ;; Exit Emacs.
14491     (switch-to-buffer gnus-group-buffer)
14492     (gnus-group-save-newsrc)))
14493
14494 (defun gnus-apply-kill-file ()
14495   "Apply a kill file to the current newsgroup.
14496 Returns the number of articles marked as read."
14497   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14498           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14499       (gnus-apply-kill-file-internal)
14500     0))
14501
14502 (defun gnus-kill-save-kill-buffer ()
14503   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14504     (when (get-file-buffer file)
14505       (save-excursion
14506         (set-buffer (get-file-buffer file))
14507         (and (buffer-modified-p) (save-buffer))
14508         (kill-buffer (current-buffer))))))
14509
14510 (defvar gnus-kill-file-name "KILL"
14511   "Suffix of the kill files.")
14512
14513 (defun gnus-newsgroup-kill-file (newsgroup)
14514   "Return the name of a kill file name for NEWSGROUP.
14515 If NEWSGROUP is nil, return the global kill file name instead."
14516   (cond 
14517    ;; The global KILL file is placed at top of the directory.
14518    ((or (null newsgroup)
14519         (string-equal newsgroup ""))
14520     (expand-file-name gnus-kill-file-name
14521                       (or gnus-kill-files-directory "~/News")))
14522    ;; Append ".KILL" to newsgroup name.
14523    ((gnus-use-long-file-name 'not-kill)
14524     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14525                               "." gnus-kill-file-name)
14526                       (or gnus-kill-files-directory "~/News")))
14527    ;; Place "KILL" under the hierarchical directory.
14528    (t
14529     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14530                               "/" gnus-kill-file-name)
14531                       (or gnus-kill-files-directory "~/News")))))
14532
14533 \f
14534 ;;;
14535 ;;; Dribble file
14536 ;;;
14537
14538 (defvar gnus-dribble-ignore nil)
14539 (defvar gnus-dribble-eval-file nil)
14540
14541 (defun gnus-dribble-file-name ()
14542   "Return the dribble file for the current .newsrc."
14543   (concat
14544    (if gnus-dribble-directory
14545        (concat (file-name-as-directory gnus-dribble-directory)
14546                (file-name-nondirectory gnus-current-startup-file))
14547      gnus-current-startup-file)
14548    "-dribble"))
14549
14550 (defun gnus-dribble-enter (string)
14551   "Enter STRING into the dribble buffer."
14552   (if (and (not gnus-dribble-ignore)
14553            gnus-dribble-buffer
14554            (buffer-name gnus-dribble-buffer))
14555       (let ((obuf (current-buffer)))
14556         (set-buffer gnus-dribble-buffer)
14557         (insert string "\n")
14558         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14559         (set-buffer obuf))))
14560
14561 (defun gnus-dribble-read-file ()
14562   "Read the dribble file from disk."
14563   (let ((dribble-file (gnus-dribble-file-name)))
14564     (save-excursion
14565       (set-buffer (setq gnus-dribble-buffer
14566                         (get-buffer-create
14567                          (file-name-nondirectory dribble-file))))
14568       (gnus-add-current-to-buffer-list)
14569       (erase-buffer)
14570       (setq buffer-file-name dribble-file)
14571       (auto-save-mode t)
14572       (buffer-disable-undo (current-buffer))
14573       (bury-buffer (current-buffer))
14574       (set-buffer-modified-p nil)
14575       (let ((auto (make-auto-save-file-name))
14576             (gnus-dribble-ignore t))
14577         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14578           ;; Load whichever file is newest -- the auto save file
14579           ;; or the "real" file.
14580           (if (file-newer-than-file-p auto dribble-file)
14581               (insert-file-contents auto)
14582             (insert-file-contents dribble-file))
14583           (unless (zerop (buffer-size))
14584             (set-buffer-modified-p t))
14585           ;; Set the file modes to reflect the .newsrc file modes.
14586           (save-buffer)
14587           (when (file-exists-p gnus-current-startup-file)
14588             (set-file-modes dribble-file
14589                             (file-modes gnus-current-startup-file)))
14590           ;; Possibly eval the file later.
14591           (when (gnus-y-or-n-p
14592                  "Auto-save file exists.  Do you want to read it? ")
14593             (setq gnus-dribble-eval-file t)))))))
14594
14595 (defun gnus-dribble-eval-file ()
14596   (if (not gnus-dribble-eval-file)
14597       ()
14598     (setq gnus-dribble-eval-file nil)
14599     (save-excursion
14600       (let ((gnus-dribble-ignore t))
14601         (set-buffer gnus-dribble-buffer)
14602         (eval-buffer (current-buffer))))))
14603
14604 (defun gnus-dribble-delete-file ()
14605   (if (file-exists-p (gnus-dribble-file-name))
14606       (delete-file (gnus-dribble-file-name)))
14607   (if gnus-dribble-buffer
14608       (save-excursion
14609         (set-buffer gnus-dribble-buffer)
14610         (let ((auto (make-auto-save-file-name)))
14611           (if (file-exists-p auto)
14612               (delete-file auto))
14613           (erase-buffer)
14614           (set-buffer-modified-p nil)))))
14615
14616 (defun gnus-dribble-save ()
14617   (if (and gnus-dribble-buffer
14618            (buffer-name gnus-dribble-buffer))
14619       (save-excursion
14620         (set-buffer gnus-dribble-buffer)
14621         (save-buffer))))
14622
14623 (defun gnus-dribble-clear ()
14624   (save-excursion
14625     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14626         (progn
14627           (set-buffer gnus-dribble-buffer)
14628           (erase-buffer)
14629           (set-buffer-modified-p nil)
14630           (setq buffer-saved-size (buffer-size))))))
14631
14632 \f
14633 ;;;
14634 ;;; Server Communication
14635 ;;;
14636
14637 (defun gnus-start-news-server (&optional confirm)
14638   "Open a method for getting news.
14639 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14640   (let (how)
14641     (if gnus-current-select-method
14642         ;; Stream is already opened.
14643         nil
14644       ;; Open NNTP server.
14645       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14646       (if confirm
14647           (progn
14648             ;; Read server name with completion.
14649             (setq gnus-nntp-server
14650                   (completing-read "NNTP server: "
14651                                    (mapcar (lambda (server) (list server))
14652                                            (cons (list gnus-nntp-server)
14653                                                  gnus-secondary-servers))
14654                                    nil nil gnus-nntp-server))))
14655
14656       (if (and gnus-nntp-server
14657                (stringp gnus-nntp-server)
14658                (not (string= gnus-nntp-server "")))
14659           (setq gnus-select-method
14660                 (cond ((or (string= gnus-nntp-server "")
14661                            (string= gnus-nntp-server "::"))
14662                        (list 'nnspool (system-name)))
14663                       ((string-match "^:" gnus-nntp-server)
14664                        (list 'nnmh gnus-nntp-server
14665                              (list 'nnmh-directory
14666                                    (file-name-as-directory
14667                                     (expand-file-name
14668                                      (concat "~/" (substring
14669                                                    gnus-nntp-server 1)))))
14670                              (list 'nnmh-get-new-mail nil)))
14671                       (t
14672                        (list 'nntp gnus-nntp-server)))))
14673
14674       (setq how (car gnus-select-method))
14675       (cond ((eq how 'nnspool)
14676              (require 'nnspool)
14677              (gnus-message 5 "Looking up local news spool..."))
14678             ((eq how 'nnmh)
14679              (require 'nnmh)
14680              (gnus-message 5 "Looking up mh spool..."))
14681             (t
14682              (require 'nntp)))
14683       (setq gnus-current-select-method gnus-select-method)
14684       (run-hooks 'gnus-open-server-hook)
14685       (or
14686        ;; gnus-open-server-hook might have opened it
14687        (gnus-server-opened gnus-select-method)
14688        (gnus-open-server gnus-select-method)
14689        (gnus-y-or-n-p
14690         (format
14691          "%s (%s) open error: '%s'.     Continue? "
14692          (car gnus-select-method) (cadr gnus-select-method)
14693          (gnus-status-message gnus-select-method)))
14694        (gnus-error 1 "Couldn't open server on %s"
14695                    (nth 1 gnus-select-method))))))
14696
14697 (defun gnus-check-group (group)
14698   "Try to make sure that the server where GROUP exists is alive."
14699   (let ((method (gnus-find-method-for-group group)))
14700     (or (gnus-server-opened method)
14701         (gnus-open-server method))))
14702
14703 (defun gnus-check-server (&optional method silent)
14704   "Check whether the connection to METHOD is down.
14705 If METHOD is nil, use `gnus-select-method'.
14706 If it is down, start it up (again)."
14707   (let ((method (or method gnus-select-method)))
14708     ;; Transform virtual server names into select methods.
14709     (when (stringp method)
14710       (setq method (gnus-server-to-method method)))
14711     (if (gnus-server-opened method)
14712         ;; The stream is already opened.
14713         t
14714       ;; Open the server.
14715       (unless silent
14716         (gnus-message 5 "Opening %s server%s..." (car method)
14717                       (if (equal (nth 1 method) "") ""
14718                         (format " on %s" (nth 1 method)))))
14719       (run-hooks 'gnus-open-server-hook)
14720       (prog1
14721           (gnus-open-server method)
14722         (unless silent
14723           (message ""))))))
14724
14725 (defun gnus-get-function (method function)
14726   "Return a function symbol based on METHOD and FUNCTION."
14727   ;; Translate server names into methods.
14728   (unless method
14729     (error "Attempted use of a nil select method"))
14730   (when (stringp method)
14731     (setq method (gnus-server-to-method method)))
14732   (let ((func (intern (format "%s-%s" (car method) function))))
14733     ;; If the functions isn't bound, we require the backend in
14734     ;; question.
14735     (unless (fboundp func)
14736       (require (car method))
14737       (unless (fboundp func)
14738         ;; This backend doesn't implement this function.
14739         (error "No such function: %s" func)))
14740     func))
14741
14742 \f
14743 ;;;
14744 ;;; Interface functions to the backends.
14745 ;;;
14746
14747 (defun gnus-open-server (method)
14748   "Open a connection to METHOD."
14749   (when (stringp method)
14750     (setq method (gnus-server-to-method method)))
14751   (let ((elem (assoc method gnus-opened-servers)))
14752     ;; If this method was previously denied, we just return nil.
14753     (if (eq (nth 1 elem) 'denied)
14754         (progn
14755           (gnus-message 1 "Denied server")
14756           nil)
14757       ;; Open the server.
14758       (let ((result
14759              (funcall (gnus-get-function method 'open-server)
14760                       (nth 1 method) (nthcdr 2 method))))
14761         ;; If this hasn't been opened before, we add it to the list.
14762         (unless elem
14763           (setq elem (list method nil)
14764                 gnus-opened-servers (cons elem gnus-opened-servers)))
14765         ;; Set the status of this server.
14766         (setcar (cdr elem) (if result 'ok 'denied))
14767         ;; Return the result from the "open" call.
14768         result))))
14769
14770 (defun gnus-close-server (method)
14771   "Close the connection to METHOD."
14772   (when (stringp method)
14773     (setq method (gnus-server-to-method method)))
14774   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14775
14776 (defun gnus-request-list (method)
14777   "Request the active file from METHOD."
14778   (when (stringp method)
14779     (setq method (gnus-server-to-method method)))
14780   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14781
14782 (defun gnus-request-list-newsgroups (method)
14783   "Request the newsgroups file from METHOD."
14784   (when (stringp method)
14785     (setq method (gnus-server-to-method method)))
14786   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14787
14788 (defun gnus-request-newgroups (date method)
14789   "Request all new groups since DATE from METHOD."
14790   (when (stringp method)
14791     (setq method (gnus-server-to-method method)))
14792   (funcall (gnus-get-function method 'request-newgroups)
14793            date (nth 1 method)))
14794
14795 (defun gnus-server-opened (method)
14796   "Check whether a connection to METHOD has been opened."
14797   (when (stringp method)
14798     (setq method (gnus-server-to-method method)))
14799   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14800
14801 (defun gnus-status-message (method)
14802   "Return the status message from METHOD.
14803 If METHOD is a string, it is interpreted as a group name.   The method
14804 this group uses will be queried."
14805   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14806                   method)))
14807     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14808
14809 (defun gnus-request-group (group &optional dont-check method)
14810   "Request GROUP.  If DONT-CHECK, no information is required."
14811   (let ((method (or method (gnus-find-method-for-group group))))
14812     (when (stringp method)
14813       (setq method (gnus-server-to-method method)))
14814     (funcall (gnus-get-function method 'request-group)
14815              (gnus-group-real-name group) (nth 1 method) dont-check)))
14816
14817 (defun gnus-request-asynchronous (group &optional articles)
14818   "Request that GROUP behave asynchronously.
14819 ARTICLES is the `data' of the group."
14820   (let ((method (gnus-find-method-for-group group)))
14821     (funcall (gnus-get-function method 'request-asynchronous)
14822              (gnus-group-real-name group) (nth 1 method) articles)))
14823
14824 (defun gnus-list-active-group (group)
14825   "Request active information on GROUP."
14826   (let ((method (gnus-find-method-for-group group))
14827         (func 'list-active-group))
14828     (when (gnus-check-backend-function func group)
14829       (funcall (gnus-get-function method func)
14830                (gnus-group-real-name group) (nth 1 method)))))
14831
14832 (defun gnus-request-group-description (group)
14833   "Request a description of GROUP."
14834   (let ((method (gnus-find-method-for-group group))
14835         (func 'request-group-description))
14836     (when (gnus-check-backend-function func group)
14837       (funcall (gnus-get-function method func)
14838                (gnus-group-real-name group) (nth 1 method)))))
14839
14840 (defun gnus-close-group (group)
14841   "Request the GROUP be closed."
14842   (let ((method (gnus-find-method-for-group group)))
14843     (funcall (gnus-get-function method 'close-group)
14844              (gnus-group-real-name group) (nth 1 method))))
14845
14846 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14847   "Request headers for ARTICLES in GROUP.
14848 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14849   (let ((method (gnus-find-method-for-group group)))
14850     (if (and gnus-use-cache (numberp (car articles)))
14851         (gnus-cache-retrieve-headers articles group fetch-old)
14852       (funcall (gnus-get-function method 'retrieve-headers)
14853                articles (gnus-group-real-name group) (nth 1 method)
14854                fetch-old))))
14855
14856 (defun gnus-retrieve-groups (groups method)
14857   "Request active information on GROUPS from METHOD."
14858   (when (stringp method)
14859     (setq method (gnus-server-to-method method)))
14860   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14861
14862 (defun gnus-request-type (group &optional article)
14863   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14864   (let ((method (gnus-find-method-for-group group)))
14865     (if (not (gnus-check-backend-function 'request-type (car method)))
14866         'unknown
14867       (funcall (gnus-get-function method 'request-type)
14868                (gnus-group-real-name group) article))))
14869
14870 (defun gnus-request-update-mark (group article mark)
14871   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14872   (let ((method (gnus-find-method-for-group group)))
14873     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14874         mark
14875       (funcall (gnus-get-function method 'request-update-mark)
14876                (gnus-group-real-name group) article mark))))
14877
14878 (defun gnus-request-article (article group &optional buffer)
14879   "Request the ARTICLE in GROUP.
14880 ARTICLE can either be an article number or an article Message-ID.
14881 If BUFFER, insert the article in that group."
14882   (let ((method (gnus-find-method-for-group group)))
14883     (funcall (gnus-get-function method 'request-article)
14884              article (gnus-group-real-name group) (nth 1 method) buffer)))
14885
14886 (defun gnus-request-head (article group)
14887   "Request the head of ARTICLE in GROUP."
14888   (let ((method (gnus-find-method-for-group group)))
14889     (funcall (gnus-get-function method 'request-head)
14890              article (gnus-group-real-name group) (nth 1 method))))
14891
14892 (defun gnus-request-body (article group)
14893   "Request the body of ARTICLE in GROUP."
14894   (let ((method (gnus-find-method-for-group group)))
14895     (funcall (gnus-get-function method 'request-body)
14896              article (gnus-group-real-name group) (nth 1 method))))
14897
14898 (defun gnus-request-post (method)
14899   "Post the current buffer using METHOD."
14900   (when (stringp method)
14901     (setq method (gnus-server-to-method method)))
14902   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14903
14904 (defun gnus-request-scan (group method)
14905   "Request a SCAN being performed in GROUP from METHOD.
14906 If GROUP is nil, all groups on METHOD are scanned."
14907   (let ((method (if group (gnus-find-method-for-group group) method)))
14908     (funcall (gnus-get-function method 'request-scan)
14909              (and group (gnus-group-real-name group)) (nth 1 method))))
14910
14911 (defsubst gnus-request-update-info (info method)
14912   "Request that METHOD update INFO."
14913   (when (stringp method)
14914     (setq method (gnus-server-to-method method)))
14915   (when (gnus-check-backend-function 'request-update-info (car method))
14916     (funcall (gnus-get-function method 'request-update-info)
14917              (gnus-group-real-name (gnus-info-group info))
14918              info (nth 1 method))))
14919
14920 (defun gnus-request-expire-articles (articles group &optional force)
14921   (let ((method (gnus-find-method-for-group group)))
14922     (funcall (gnus-get-function method 'request-expire-articles)
14923              articles (gnus-group-real-name group) (nth 1 method)
14924              force)))
14925
14926 (defun gnus-request-move-article
14927   (article group server accept-function &optional last)
14928   (let ((method (gnus-find-method-for-group group)))
14929     (funcall (gnus-get-function method 'request-move-article)
14930              article (gnus-group-real-name group)
14931              (nth 1 method) accept-function last)))
14932
14933 (defun gnus-request-accept-article (group method &optional last)
14934   ;; Make sure there's a newline at the end of the article.
14935   (when (stringp method)
14936     (setq method (gnus-server-to-method method)))
14937   (when (and (not method)
14938              (stringp group))
14939     (setq method (gnus-find-method-for-group group)))
14940   (goto-char (point-max))
14941   (unless (bolp)
14942     (insert "\n"))
14943   (let ((func (car (or method (gnus-find-method-for-group group)))))
14944     (funcall (intern (format "%s-request-accept-article" func))
14945              (if (stringp group) (gnus-group-real-name group) group)
14946              (cadr method)
14947              last)))
14948
14949 (defun gnus-request-replace-article (article group buffer)
14950   (let ((func (car (gnus-find-method-for-group group))))
14951     (funcall (intern (format "%s-request-replace-article" func))
14952              article (gnus-group-real-name group) buffer)))
14953
14954 (defun gnus-request-associate-buffer (group)
14955   (let ((method (gnus-find-method-for-group group)))
14956     (funcall (gnus-get-function method 'request-associate-buffer)
14957              (gnus-group-real-name group))))
14958
14959 (defun gnus-request-restore-buffer (article group)
14960   "Request a new buffer restored to the state of ARTICLE."
14961   (let ((method (gnus-find-method-for-group group)))
14962     (funcall (gnus-get-function method 'request-restore-buffer)
14963              article (gnus-group-real-name group) (nth 1 method))))
14964
14965 (defun gnus-request-create-group (group &optional method)
14966   (when (stringp method)
14967     (setq method (gnus-server-to-method method)))
14968   (let ((method (or method (gnus-find-method-for-group group))))
14969     (funcall (gnus-get-function method 'request-create-group)
14970              (gnus-group-real-name group) (nth 1 method))))
14971
14972 (defun gnus-request-delete-group (group &optional force)
14973   (let ((method (gnus-find-method-for-group group)))
14974     (funcall (gnus-get-function method 'request-delete-group)
14975              (gnus-group-real-name group) force (nth 1 method))))
14976
14977 (defun gnus-request-rename-group (group new-name)
14978   (let ((method (gnus-find-method-for-group group)))
14979     (funcall (gnus-get-function method 'request-rename-group)
14980              (gnus-group-real-name group)
14981              (gnus-group-real-name new-name) (nth 1 method))))
14982
14983 (defun gnus-member-of-valid (symbol group)
14984   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
14985   (memq symbol (assoc
14986                 (symbol-name (car (gnus-find-method-for-group group)))
14987                 gnus-valid-select-methods)))
14988
14989 (defun gnus-method-option-p (method option)
14990   "Return non-nil if select METHOD has OPTION as a parameter."
14991   (when (stringp method)
14992     (setq method (gnus-server-to-method method)))
14993   (memq option (assoc (format "%s" (car method))
14994                       gnus-valid-select-methods)))
14995
14996 (defun gnus-server-extend-method (group method)
14997   ;; This function "extends" a virtual server.  If the server is
14998   ;; "hello", and the select method is ("hello" (my-var "something"))
14999   ;; in the group "alt.alt", this will result in a new virtual server
15000   ;; called "hello+alt.alt".
15001   (let ((entry
15002          (gnus-copy-sequence
15003           (if (equal (car method) "native") gnus-select-method
15004             (cdr (assoc (car method) gnus-server-alist))))))
15005     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15006     (nconc entry (cdr method))))
15007
15008 (defun gnus-find-method-for-group (group &optional info)
15009   "Find the select method that GROUP uses."
15010   (or gnus-override-method
15011       (and (not group)
15012            gnus-select-method)
15013       (let ((info (or info (gnus-get-info group)))
15014             method)
15015         (if (or (not info)
15016                 (not (setq method (gnus-info-method info)))
15017                 (equal method "native"))
15018             gnus-select-method
15019           (setq method
15020                 (cond ((stringp method)
15021                        (gnus-server-to-method method))
15022                       ((stringp (car method))
15023                        (gnus-server-extend-method group method))
15024                       (t
15025                        method)))
15026           (cond ((equal (cadr method) "")
15027                  method)
15028                 ((null (cadr method))
15029                  (list (car method) ""))
15030                 (t
15031                  (gnus-server-add-address method)))))))
15032
15033 (defun gnus-check-backend-function (func group)
15034   "Check whether GROUP supports function FUNC."
15035   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15036                   group)))
15037     (fboundp (intern (format "%s-%s" method func)))))
15038
15039 (defun gnus-methods-using (feature)
15040   "Find all methods that have FEATURE."
15041   (let ((valids gnus-valid-select-methods)
15042         outs)
15043     (while valids
15044       (if (memq feature (car valids))
15045           (setq outs (cons (car valids) outs)))
15046       (setq valids (cdr valids)))
15047     outs))
15048
15049 \f
15050 ;;;
15051 ;;; Active & Newsrc File Handling
15052 ;;;
15053
15054 (defun gnus-setup-news (&optional rawfile level dont-connect)
15055   "Setup news information.
15056 If RAWFILE is non-nil, the .newsrc file will also be read.
15057 If LEVEL is non-nil, the news will be set up at level LEVEL."
15058   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15059
15060     (when init 
15061       ;; Clear some variables to re-initialize news information.
15062       (setq gnus-newsrc-alist nil
15063             gnus-active-hashtb nil)
15064       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15065       (gnus-read-newsrc-file rawfile))
15066
15067     (when (and (not (assoc "archive" gnus-server-alist))
15068                gnus-message-archive-method)
15069       (push (cons "archive" gnus-message-archive-method)
15070             gnus-server-alist))
15071
15072     ;; If we don't read the complete active file, we fill in the
15073     ;; hashtb here.
15074     (if (or (null gnus-read-active-file)
15075             (eq gnus-read-active-file 'some))
15076         (gnus-update-active-hashtb-from-killed))
15077
15078     ;; Read the active file and create `gnus-active-hashtb'.
15079     ;; If `gnus-read-active-file' is nil, then we just create an empty
15080     ;; hash table.  The partial filling out of the hash table will be
15081     ;; done in `gnus-get-unread-articles'.
15082     (and gnus-read-active-file
15083          (not level)
15084          (gnus-read-active-file))
15085
15086     (or gnus-active-hashtb
15087         (setq gnus-active-hashtb (make-vector 4095 0)))
15088
15089     ;; Initialize the cache.
15090     (when gnus-use-cache
15091       (gnus-cache-open))
15092
15093     ;; Possibly eval the dribble file.
15094     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15095
15096     (gnus-update-format-specifications)
15097
15098     ;; See whether we need to read the description file.
15099     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15100              (not gnus-description-hashtb)
15101              (not dont-connect)
15102              gnus-read-active-file)
15103         (gnus-read-all-descriptions-files))
15104
15105     ;; Find new newsgroups and treat them.
15106     (if (and init gnus-check-new-newsgroups (not level)
15107              (gnus-check-server gnus-select-method))
15108         (gnus-find-new-newsgroups))
15109
15110     ;; We might read in new NoCeM messages here.
15111     (when gnus-use-nocem 
15112       (gnus-nocem-scan-groups))
15113
15114     ;; Find the number of unread articles in each non-dead group.
15115     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15116       (gnus-get-unread-articles level))
15117
15118     (if (and init gnus-check-bogus-newsgroups
15119              gnus-read-active-file (not level)
15120              (gnus-server-opened gnus-select-method))
15121         (gnus-check-bogus-newsgroups))))
15122
15123 (defun gnus-find-new-newsgroups (&optional arg)
15124   "Search for new newsgroups and add them.
15125 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15126 The `-n' option line from .newsrc is respected.
15127 If ARG (the prefix), use the `ask-server' method to query
15128 the server for new groups."
15129   (interactive "P")
15130   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15131                        (null gnus-read-active-file)
15132                        (eq gnus-read-active-file 'some))
15133                    'ask-server gnus-check-new-newsgroups)))
15134     (unless (gnus-check-first-time-used)
15135       (if (or (consp check)
15136               (eq check 'ask-server))
15137           ;; Ask the server for new groups.
15138           (gnus-ask-server-for-new-groups)
15139         ;; Go through the active hashtb and look for new groups.
15140         (let ((groups 0)
15141               group new-newsgroups)
15142           (gnus-message 5 "Looking for new newsgroups...")
15143           (unless gnus-have-read-active-file
15144             (gnus-read-active-file))
15145           (setq gnus-newsrc-last-checked-date (current-time-string))
15146           (unless gnus-killed-hashtb
15147             (gnus-make-hashtable-from-killed))
15148           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15149           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15150           (mapatoms
15151            (lambda (sym)
15152              (if (or (null (setq group (symbol-name sym)))
15153                      (not (boundp sym))
15154                      (null (symbol-value sym))
15155                      (gnus-gethash group gnus-killed-hashtb)
15156                      (gnus-gethash group gnus-newsrc-hashtb))
15157                  ()
15158                (let ((do-sub (gnus-matches-options-n group)))
15159                  (cond
15160                   ((eq do-sub 'subscribe)
15161                    (setq groups (1+ groups))
15162                    (gnus-sethash group group gnus-killed-hashtb)
15163                    (funcall gnus-subscribe-options-newsgroup-method group))
15164                   ((eq do-sub 'ignore)
15165                    nil)
15166                   (t
15167                    (setq groups (1+ groups))
15168                    (gnus-sethash group group gnus-killed-hashtb)
15169                    (if gnus-subscribe-hierarchical-interactive
15170                        (setq new-newsgroups (cons group new-newsgroups))
15171                      (funcall gnus-subscribe-newsgroup-method group)))))))
15172            gnus-active-hashtb)
15173           (when new-newsgroups
15174             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15175           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15176           (if (> groups 0)
15177               (gnus-message 6 "%d new newsgroup%s arrived."
15178                             groups (if (> groups 1) "s have" " has"))
15179             (gnus-message 6 "No new newsgroups.")))))))
15180
15181 (defun gnus-matches-options-n (group)
15182   ;; Returns `subscribe' if the group is to be unconditionally
15183   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15184   ;; no match for the group.
15185
15186   ;; First we check the two user variables.
15187   (cond
15188    ((and gnus-options-subscribe
15189          (string-match gnus-options-subscribe group))
15190     'subscribe)
15191    ((and gnus-auto-subscribed-groups
15192          (string-match gnus-auto-subscribed-groups group))
15193     'subscribe)
15194    ((and gnus-options-not-subscribe
15195          (string-match gnus-options-not-subscribe group))
15196     'ignore)
15197    ;; Then we go through the list that was retrieved from the .newsrc
15198    ;; file.  This list has elements on the form
15199    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15200    ;; is in the reverse order of the options line) is returned.
15201    (t
15202     (let ((regs gnus-newsrc-options-n))
15203       (while (and regs
15204                   (not (string-match (caar regs) group)))
15205         (setq regs (cdr regs)))
15206       (and regs (cdar regs))))))
15207
15208 (defun gnus-ask-server-for-new-groups ()
15209   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15210          (methods (cons gnus-select-method
15211                         (nconc
15212                          (when gnus-message-archive-method
15213                            (list "archive"))
15214                          (append
15215                           (and (consp gnus-check-new-newsgroups)
15216                                gnus-check-new-newsgroups)
15217                           gnus-secondary-select-methods))))
15218          (groups 0)
15219          (new-date (current-time-string))
15220          group new-newsgroups got-new method hashtb
15221          gnus-override-subscribe-method)
15222     ;; Go through both primary and secondary select methods and
15223     ;; request new newsgroups.
15224     (while (setq method (gnus-server-get-method nil (pop methods)))
15225       (setq new-newsgroups nil)
15226       (setq gnus-override-subscribe-method method)
15227       (when (and (gnus-check-server method)
15228                  (gnus-request-newgroups date method))
15229         (save-excursion
15230           (setq got-new t)
15231           (setq hashtb (gnus-make-hashtable 100))
15232           (set-buffer nntp-server-buffer)
15233           ;; Enter all the new groups into a hashtable.
15234           (gnus-active-to-gnus-format method hashtb 'ignore))
15235         ;; Now all new groups from `method' are in `hashtb'.
15236         (mapatoms
15237          (lambda (group-sym)
15238            (if (or (null (setq group (symbol-name group-sym)))
15239                    (not (boundp group-sym))
15240                    (null (symbol-value group-sym))
15241                    (gnus-gethash group gnus-newsrc-hashtb)
15242                    (member group gnus-zombie-list)
15243                    (member group gnus-killed-list))
15244                ;; The group is already known.
15245                ()
15246              ;; Make this group active.
15247              (when (symbol-value group-sym)
15248                (gnus-set-active group (symbol-value group-sym)))
15249              ;; Check whether we want it or not.
15250              (let ((do-sub (gnus-matches-options-n group)))
15251                (cond
15252                 ((eq do-sub 'subscribe)
15253                  (incf groups)
15254                  (gnus-sethash group group gnus-killed-hashtb)
15255                  (funcall gnus-subscribe-options-newsgroup-method group))
15256                 ((eq do-sub 'ignore)
15257                  nil)
15258                 (t
15259                  (incf groups)
15260                  (gnus-sethash group group gnus-killed-hashtb)
15261                  (if gnus-subscribe-hierarchical-interactive
15262                      (push group new-newsgroups)
15263                    (funcall gnus-subscribe-newsgroup-method group)))))))
15264          hashtb))
15265       (when new-newsgroups
15266         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15267     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15268     (when (> groups 0)
15269       (gnus-message 6 "%d new newsgroup%s arrived."
15270                     groups (if (> groups 1) "s have" " has")))
15271     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15272     got-new))
15273
15274 (defun gnus-check-first-time-used ()
15275   (if (or (> (length gnus-newsrc-alist) 1)
15276           (file-exists-p gnus-startup-file)
15277           (file-exists-p (concat gnus-startup-file ".el"))
15278           (file-exists-p (concat gnus-startup-file ".eld")))
15279       nil
15280     (gnus-message 6 "First time user; subscribing you to default groups")
15281     (unless gnus-have-read-active-file
15282       (gnus-read-active-file))
15283     (setq gnus-newsrc-last-checked-date (current-time-string))
15284     (let ((groups gnus-default-subscribed-newsgroups)
15285           group)
15286       (if (eq groups t)
15287           nil
15288         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15289         (mapatoms
15290          (lambda (sym)
15291            (if (null (setq group (symbol-name sym)))
15292                ()
15293              (let ((do-sub (gnus-matches-options-n group)))
15294                (cond
15295                 ((eq do-sub 'subscribe)
15296                  (gnus-sethash group group gnus-killed-hashtb)
15297                  (funcall gnus-subscribe-options-newsgroup-method group))
15298                 ((eq do-sub 'ignore)
15299                  nil)
15300                 (t
15301                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15302          gnus-active-hashtb)
15303         (while groups
15304           (if (gnus-active (car groups))
15305               (gnus-group-change-level
15306                (car groups) gnus-level-default-subscribed gnus-level-killed))
15307           (setq groups (cdr groups)))
15308         (gnus-group-make-help-group)
15309         (and gnus-novice-user
15310              (gnus-message 7 "`A k' to list killed groups"))))))
15311
15312 (defun gnus-subscribe-group (group previous &optional method)
15313   (gnus-group-change-level
15314    (if method
15315        (list t group gnus-level-default-subscribed nil nil method)
15316      group)
15317    gnus-level-default-subscribed gnus-level-killed previous t))
15318
15319 ;; `gnus-group-change-level' is the fundamental function for changing
15320 ;; subscription levels of newsgroups.  This might mean just changing
15321 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15322 ;; again, which subscribes/unsubscribes a group, which is equally
15323 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15324 ;; from 8-9 to 1-7 means that you remove the group from the list of
15325 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15326 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15327 ;; which is trivial.
15328 ;; ENTRY can either be a string (newsgroup name) or a list (if
15329 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15330 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15331 ;; entries.
15332 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15333 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15334 ;; after.
15335 (defun gnus-group-change-level (entry level &optional oldlevel
15336                                       previous fromkilled)
15337   (let (group info active num)
15338     ;; Glean what info we can from the arguments
15339     (if (consp entry)
15340         (if fromkilled (setq group (nth 1 entry))
15341           (setq group (car (nth 2 entry))))
15342       (setq group entry))
15343     (if (and (stringp entry)
15344              oldlevel
15345              (< oldlevel gnus-level-zombie))
15346         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15347     (if (and (not oldlevel)
15348              (consp entry))
15349         (setq oldlevel (gnus-info-level (nth 2 entry)))
15350       (setq oldlevel (or oldlevel 9)))
15351     (if (stringp previous)
15352         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15353
15354     (if (and (>= oldlevel gnus-level-zombie)
15355              (gnus-gethash group gnus-newsrc-hashtb))
15356         ;; We are trying to subscribe a group that is already
15357         ;; subscribed.
15358         ()                              ; Do nothing.
15359
15360       (or (gnus-ephemeral-group-p group)
15361           (gnus-dribble-enter
15362            (format "(gnus-group-change-level %S %S %S %S %S)"
15363                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15364
15365       ;; Then we remove the newgroup from any old structures, if needed.
15366       ;; If the group was killed, we remove it from the killed or zombie
15367       ;; list.  If not, and it is in fact going to be killed, we remove
15368       ;; it from the newsrc hash table and assoc.
15369       (cond
15370        ((>= oldlevel gnus-level-zombie)
15371         (if (= oldlevel gnus-level-zombie)
15372             (setq gnus-zombie-list (delete group gnus-zombie-list))
15373           (setq gnus-killed-list (delete group gnus-killed-list))))
15374        (t
15375         (if (and (>= level gnus-level-zombie)
15376                  entry)
15377             (progn
15378               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15379               (if (nth 3 entry)
15380                   (setcdr (gnus-gethash (car (nth 3 entry))
15381                                         gnus-newsrc-hashtb)
15382                           (cdr entry)))
15383               (setcdr (cdr entry) (cdddr entry))))))
15384
15385       ;; Finally we enter (if needed) the list where it is supposed to
15386       ;; go, and change the subscription level.  If it is to be killed,
15387       ;; we enter it into the killed or zombie list.
15388       (cond 
15389        ((>= level gnus-level-zombie)
15390         ;; Remove from the hash table.
15391         (gnus-sethash group nil gnus-newsrc-hashtb)
15392         ;; We do not enter foreign groups into the list of dead
15393         ;; groups.
15394         (unless (gnus-group-foreign-p group)
15395           (if (= level gnus-level-zombie)
15396               (setq gnus-zombie-list (cons group gnus-zombie-list))
15397             (setq gnus-killed-list (cons group gnus-killed-list)))))
15398        (t
15399         ;; If the list is to be entered into the newsrc assoc, and
15400         ;; it was killed, we have to create an entry in the newsrc
15401         ;; hashtb format and fix the pointers in the newsrc assoc.
15402         (if (< oldlevel gnus-level-zombie)
15403             ;; It was alive, and it is going to stay alive, so we
15404             ;; just change the level and don't change any pointers or
15405             ;; hash table entries.
15406             (setcar (cdaddr entry) level)
15407           (if (listp entry)
15408               (setq info (cdr entry)
15409                     num (car entry))
15410             (setq active (gnus-active group))
15411             (setq num
15412                   (if active (- (1+ (cdr active)) (car active)) t))
15413             ;; Check whether the group is foreign.  If so, the
15414             ;; foreign select method has to be entered into the
15415             ;; info.
15416             (let ((method (or gnus-override-subscribe-method
15417                               (gnus-group-method group))))
15418               (if (eq method gnus-select-method)
15419                   (setq info (list group level nil))
15420                 (setq info (list group level nil nil method)))))
15421           (unless previous
15422             (setq previous
15423                   (let ((p gnus-newsrc-alist))
15424                     (while (cddr p)
15425                       (setq p (cdr p)))
15426                     p)))
15427           (setq entry (cons info (cddr previous)))
15428           (if (cdr previous)
15429               (progn
15430                 (setcdr (cdr previous) entry)
15431                 (gnus-sethash group (cons num (cdr previous))
15432                               gnus-newsrc-hashtb))
15433             (setcdr previous entry)
15434             (gnus-sethash group (cons num previous)
15435                           gnus-newsrc-hashtb))
15436           (when (cdr entry)
15437             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15438       (when gnus-group-change-level-function
15439         (funcall gnus-group-change-level-function group level oldlevel)))))
15440
15441 (defun gnus-kill-newsgroup (newsgroup)
15442   "Obsolete function.  Kills a newsgroup."
15443   (gnus-group-change-level
15444    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15445
15446 (defun gnus-check-bogus-newsgroups (&optional confirm)
15447   "Remove bogus newsgroups.
15448 If CONFIRM is non-nil, the user has to confirm the deletion of every
15449 newsgroup."
15450   (let ((newsrc (cdr gnus-newsrc-alist))
15451         bogus group entry info)
15452     (gnus-message 5 "Checking bogus newsgroups...")
15453     (unless gnus-have-read-active-file
15454       (gnus-read-active-file))
15455     (when (member gnus-select-method gnus-have-read-active-file)
15456       ;; Find all bogus newsgroup that are subscribed.
15457       (while newsrc
15458         (setq info (pop newsrc)
15459               group (gnus-info-group info))
15460         (unless (or (gnus-active group) ; Active
15461                     (gnus-info-method info) ; Foreign
15462                     (and confirm
15463                          (not (gnus-y-or-n-p
15464                                (format "Remove bogus newsgroup: %s " group)))))
15465           ;; Found a bogus newsgroup.
15466           (push group bogus)))
15467       ;; Remove all bogus subscribed groups by first killing them, and
15468       ;; then removing them from the list of killed groups.
15469       (while bogus
15470         (when (setq entry (gnus-gethash (setq group (pop bogus))
15471                                         gnus-newsrc-hashtb))
15472           (gnus-group-change-level entry gnus-level-killed)
15473           (setq gnus-killed-list (delete group gnus-killed-list))))
15474       ;; Then we remove all bogus groups from the list of killed and
15475       ;; zombie groups.  They are are removed without confirmation.
15476       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15477             killed)
15478         (while dead-lists
15479           (setq killed (symbol-value (car dead-lists)))
15480           (while killed
15481             (unless (gnus-active (setq group (pop killed)))
15482               ;; The group is bogus.
15483               ;; !!!Slow as hell.
15484               (set (car dead-lists)
15485                    (delete group (symbol-value (car dead-lists))))))
15486           (setq dead-lists (cdr dead-lists))))
15487       (gnus-message 5 "Checking bogus newsgroups...done"))))
15488
15489 (defun gnus-check-duplicate-killed-groups ()
15490   "Remove duplicates from the list of killed groups."
15491   (interactive)
15492   (let ((killed gnus-killed-list))
15493     (while killed
15494       (gnus-message 9 "%d" (length killed))
15495       (setcdr killed (delete (car killed) (cdr killed)))
15496       (setq killed (cdr killed)))))
15497
15498 ;; We want to inline a function from gnus-cache, so we cheat here:
15499 (eval-when-compile
15500   (provide 'gnus)
15501   (require 'gnus-cache))
15502
15503 (defun gnus-get-unread-articles-in-group (info active &optional update)
15504   (when active
15505     ;; Allow the backend to update the info in the group.
15506     (when (and update 
15507                (gnus-request-update-info
15508                 info (gnus-find-method-for-group (gnus-info-group info))))
15509       (gnus-activate-group (gnus-info-group info) nil t))
15510     (let* ((range (gnus-info-read info))
15511            (num 0))
15512       ;; If a cache is present, we may have to alter the active info.
15513       (when (and gnus-use-cache info)
15514         (inline (gnus-cache-possibly-alter-active 
15515                  (gnus-info-group info) active)))
15516       ;; Modify the list of read articles according to what articles
15517       ;; are available; then tally the unread articles and add the
15518       ;; number to the group hash table entry.
15519       (cond
15520        ((zerop (cdr active))
15521         (setq num 0))
15522        ((not range)
15523         (setq num (- (1+ (cdr active)) (car active))))
15524        ((not (listp (cdr range)))
15525         ;; Fix a single (num . num) range according to the
15526         ;; active hash table.
15527         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15528         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15529         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15530         ;; Compute number of unread articles.
15531         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15532        (t
15533         ;; The read list is a list of ranges.  Fix them according to
15534         ;; the active hash table.
15535         ;; First peel off any elements that are below the lower
15536         ;; active limit.
15537         (while (and (cdr range)
15538                     (>= (car active)
15539                         (or (and (atom (cadr range)) (cadr range))
15540                             (caadr range))))
15541           (if (numberp (car range))
15542               (setcar range
15543                       (cons (car range)
15544                             (or (and (numberp (cadr range))
15545                                      (cadr range))
15546                                 (cdadr range))))
15547             (setcdr (car range)
15548                     (or (and (numberp (nth 1 range)) (nth 1 range))
15549                         (cdadr range))))
15550           (setcdr range (cddr range)))
15551         ;; Adjust the first element to be the same as the lower limit.
15552         (if (and (not (atom (car range)))
15553                  (< (cdar range) (car active)))
15554             (setcdr (car range) (1- (car active))))
15555         ;; Then we want to peel off any elements that are higher
15556         ;; than the upper active limit.
15557         (let ((srange range))
15558           ;; Go past all legal elements.
15559           (while (and (cdr srange)
15560                       (<= (or (and (atom (cadr srange))
15561                                    (cadr srange))
15562                               (caadr srange)) (cdr active)))
15563             (setq srange (cdr srange)))
15564           (if (cdr srange)
15565               ;; Nuke all remaining illegal elements.
15566               (setcdr srange nil))
15567
15568           ;; Adjust the final element.
15569           (if (and (not (atom (car srange)))
15570                    (> (cdar srange) (cdr active)))
15571               (setcdr (car srange) (cdr active))))
15572         ;; Compute the number of unread articles.
15573         (while range
15574           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15575                                       (cdar range)))
15576                               (or (and (atom (car range)) (car range))
15577                                   (caar range)))))
15578           (setq range (cdr range)))
15579         (setq num (max 0 (- (cdr active) num)))))
15580       ;; Set the number of unread articles.
15581       (when info
15582         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15583       num)))
15584
15585 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15586 ;; and compute how many unread articles there are in each group.
15587 (defun gnus-get-unread-articles (&optional level)
15588   (let* ((newsrc (cdr gnus-newsrc-alist))
15589          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15590          (foreign-level
15591           (min
15592            (cond ((and gnus-activate-foreign-newsgroups
15593                        (not (numberp gnus-activate-foreign-newsgroups)))
15594                   (1+ gnus-level-subscribed))
15595                  ((numberp gnus-activate-foreign-newsgroups)
15596                   gnus-activate-foreign-newsgroups)
15597                  (t 0))
15598            level))
15599          info group active method)
15600     (gnus-message 5 "Checking new news...")
15601
15602     (while newsrc
15603       (setq active (gnus-active (setq group (gnus-info-group
15604                                              (setq info (pop newsrc))))))
15605
15606       ;; Check newsgroups.  If the user doesn't want to check them, or
15607       ;; they can't be checked (for instance, if the news server can't
15608       ;; be reached) we just set the number of unread articles in this
15609       ;; newsgroup to t.  This means that Gnus thinks that there are
15610       ;; unread articles, but it has no idea how many.
15611       (if (and (setq method (gnus-info-method info))
15612                (not (gnus-server-equal
15613                      gnus-select-method
15614                      (setq method (gnus-server-get-method nil method))))
15615                (not (gnus-secondary-method-p method)))
15616           ;; These groups are foreign.  Check the level.
15617           (when (<= (gnus-info-level info) foreign-level)
15618             (setq active (gnus-activate-group group 'scan))
15619             (unless (inline (gnus-virtual-group-p group))
15620               (inline (gnus-close-group group))))
15621
15622         ;; These groups are native or secondary.
15623         (when (and (<= (gnus-info-level info) level)
15624                    (not gnus-read-active-file))
15625           (setq active (gnus-activate-group group 'scan))
15626           (inline (gnus-close-group group))))
15627
15628       (if active
15629           (inline (gnus-get-unread-articles-in-group 
15630                    info active
15631                    (and method
15632                         (fboundp (intern (concat (symbol-name (car method))
15633                                                  "-request-update-info"))))))
15634         ;; The group couldn't be reached, so we nix out the number of
15635         ;; unread articles and stuff.
15636         (gnus-set-active group nil)
15637         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15638
15639     (gnus-message 5 "Checking new news...done")))
15640
15641 ;; Create a hash table out of the newsrc alist.  The `car's of the
15642 ;; alist elements are used as keys.
15643 (defun gnus-make-hashtable-from-newsrc-alist ()
15644   (let ((alist gnus-newsrc-alist)
15645         (ohashtb gnus-newsrc-hashtb)
15646         prev)
15647     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15648     (setq alist
15649           (setq prev (setq gnus-newsrc-alist
15650                            (if (equal (caar gnus-newsrc-alist)
15651                                       "dummy.group")
15652                                gnus-newsrc-alist
15653                              (cons (list "dummy.group" 0 nil) alist)))))
15654     (while alist
15655       (gnus-sethash
15656        (caar alist)
15657        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15658              prev)
15659        gnus-newsrc-hashtb)
15660       (setq prev alist
15661             alist (cdr alist)))))
15662
15663 (defun gnus-make-hashtable-from-killed ()
15664   "Create a hash table from the killed and zombie lists."
15665   (let ((lists '(gnus-killed-list gnus-zombie-list))
15666         list)
15667     (setq gnus-killed-hashtb
15668           (gnus-make-hashtable
15669            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15670     (while (setq list (pop lists))
15671       (setq list (symbol-value list))
15672       (while list
15673         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15674
15675 (defun gnus-activate-group (group &optional scan dont-check)
15676   ;; Check whether a group has been activated or not.
15677   ;; If SCAN, request a scan of that group as well.
15678   (let ((method (gnus-find-method-for-group group))
15679         active)
15680     (and (gnus-check-server method)
15681          ;; We escape all bugs and quit here to make it possible to
15682          ;; continue if a group is so out-there that it reports bugs
15683          ;; and stuff.
15684          (progn
15685            (and scan
15686                 (gnus-check-backend-function 'request-scan (car method))
15687                 (gnus-request-scan group method))
15688            t)
15689          (condition-case ()
15690              (gnus-request-group group dont-check)
15691         ;   (error nil)
15692            (quit nil))
15693          (save-excursion
15694            (set-buffer nntp-server-buffer)
15695            (goto-char (point-min))
15696            ;; Parse the result we got from `gnus-request-group'.
15697            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15698                 (progn
15699                   (goto-char (match-beginning 1))
15700                   (gnus-set-active
15701                    group (setq active (cons (read (current-buffer))
15702                                             (read (current-buffer)))))
15703                   ;; Return the new active info.
15704                   active))))))
15705
15706 (defun gnus-update-read-articles (group unread)
15707   "Update the list of read and ticked articles in GROUP using the
15708 UNREAD and TICKED lists.
15709 Note: UNSELECTED has to be sorted over `<'.
15710 Returns whether the updating was successful."
15711   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15712          (entry (gnus-gethash group gnus-newsrc-hashtb))
15713          (info (nth 2 entry))
15714          (prev 1)
15715          (unread (sort (copy-sequence unread) '<))
15716          read)
15717     (if (or (not info) (not active))
15718         ;; There is no info on this group if it was, in fact,
15719         ;; killed.  Gnus stores no information on killed groups, so
15720         ;; there's nothing to be done.
15721         ;; One could store the information somewhere temporarily,
15722         ;; perhaps...  Hmmm...
15723         ()
15724       ;; Remove any negative articles numbers.
15725       (while (and unread (< (car unread) 0))
15726         (setq unread (cdr unread)))
15727       ;; Remove any expired article numbers
15728       (while (and unread (< (car unread) (car active)))
15729         (setq unread (cdr unread)))
15730       ;; Compute the ranges of read articles by looking at the list of
15731       ;; unread articles.
15732       (while unread
15733         (if (/= (car unread) prev)
15734             (setq read (cons (if (= prev (1- (car unread))) prev
15735                                (cons prev (1- (car unread)))) read)))
15736         (setq prev (1+ (car unread)))
15737         (setq unread (cdr unread)))
15738       (when (<= prev (cdr active))
15739         (setq read (cons (cons prev (cdr active)) read)))
15740       ;; Enter this list into the group info.
15741       (gnus-info-set-read
15742        info (if (> (length read) 1) (nreverse read) read))
15743       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15744       (gnus-get-unread-articles-in-group info (gnus-active group))
15745       t)))
15746
15747 (defun gnus-make-articles-unread (group articles)
15748   "Mark ARTICLES in GROUP as unread."
15749   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15750                           (gnus-gethash (gnus-group-real-name group)
15751                                         gnus-newsrc-hashtb))))
15752          (ranges (gnus-info-read info))
15753          news article)
15754     (while articles
15755       (when (gnus-member-of-range
15756              (setq article (pop articles)) ranges)
15757         (setq news (cons article news))))
15758     (when news
15759       (gnus-info-set-read
15760        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15761       (gnus-group-update-group group t))))
15762
15763 ;; Enter all dead groups into the hashtb.
15764 (defun gnus-update-active-hashtb-from-killed ()
15765   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15766         (lists (list gnus-killed-list gnus-zombie-list))
15767         killed)
15768     (while lists
15769       (setq killed (car lists))
15770       (while killed
15771         (gnus-sethash (car killed) nil hashtb)
15772         (setq killed (cdr killed)))
15773       (setq lists (cdr lists)))))
15774
15775 ;; Get the active file(s) from the backend(s).
15776 (defun gnus-read-active-file ()
15777   (gnus-group-set-mode-line)
15778   (let ((methods 
15779          (append
15780           (if (gnus-check-server gnus-select-method)
15781               ;; The native server is available.
15782               (cons gnus-select-method gnus-secondary-select-methods)
15783             ;; The native server is down, so we just do the
15784             ;; secondary ones.
15785             gnus-secondary-select-methods)
15786           ;; Also read from the archive server.
15787           (when gnus-message-archive-method
15788             (list "archive"))))
15789         list-type)
15790     (setq gnus-have-read-active-file nil)
15791     (save-excursion
15792       (set-buffer nntp-server-buffer)
15793       (while methods
15794         (let* ((method (if (stringp (car methods))
15795                            (gnus-server-get-method nil (car methods))
15796                          (car methods)))
15797                (where (nth 1 method))
15798                (mesg (format "Reading active file%s via %s..."
15799                              (if (and where (not (zerop (length where))))
15800                                  (concat " from " where) "")
15801                              (car method))))
15802           (gnus-message 5 mesg)
15803           (when (gnus-check-server method)
15804             ;; Request that the backend scan its incoming messages.
15805             (and (gnus-check-backend-function 'request-scan (car method))
15806                  (gnus-request-scan nil method))
15807             (cond
15808              ((and (eq gnus-read-active-file 'some)
15809                    (gnus-check-backend-function 'retrieve-groups (car method)))
15810               (let ((newsrc (cdr gnus-newsrc-alist))
15811                     (gmethod (gnus-server-get-method nil method))
15812                     groups info)
15813                 (while (setq info (pop newsrc))
15814                   (when (gnus-server-equal
15815                          (gnus-find-method-for-group 
15816                           (gnus-info-group info) info)
15817                          gmethod)
15818                     (push (gnus-group-real-name (gnus-info-group info)) 
15819                           groups)))
15820                 (when groups
15821                   (gnus-check-server method)
15822                   (setq list-type (gnus-retrieve-groups groups method))
15823                   (cond
15824                    ((not list-type)
15825                     (gnus-error
15826                      1.2 "Cannot read partial active file from %s server."
15827                      (car method)))
15828                    ((eq list-type 'active)
15829                     (gnus-active-to-gnus-format method gnus-active-hashtb))
15830                    (t
15831                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
15832              (t
15833               (if (not (gnus-request-list method))
15834                   (unless (equal method gnus-message-archive-method)
15835                     (gnus-error 1 "Cannot read active file from %s server."
15836                                 (car method)))
15837                 (gnus-active-to-gnus-format method gnus-active-hashtb)
15838                 ;; We mark this active file as read.
15839                 (push method gnus-have-read-active-file)
15840                 (gnus-message 5 "%sdone" mesg))))))
15841         (setq methods (cdr methods))))))
15842
15843 ;; Read an active file and place the results in `gnus-active-hashtb'.
15844 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15845   (unless method
15846     (setq method gnus-select-method))
15847   (let ((cur (current-buffer))
15848         (hashtb (or hashtb
15849                     (if (and gnus-active-hashtb
15850                              (not (equal method gnus-select-method)))
15851                         gnus-active-hashtb
15852                       (setq gnus-active-hashtb
15853                             (if (equal method gnus-select-method)
15854                                 (gnus-make-hashtable
15855                                  (count-lines (point-min) (point-max)))
15856                               (gnus-make-hashtable 4096)))))))
15857     ;; Delete unnecessary lines.
15858     (goto-char (point-min))
15859     (while (search-forward "\nto." nil t)
15860       (delete-region (1+ (match-beginning 0))
15861                      (progn (forward-line 1) (point))))
15862     (or (string= gnus-ignored-newsgroups "")
15863         (progn
15864           (goto-char (point-min))
15865           (delete-matching-lines gnus-ignored-newsgroups)))
15866     ;; Make the group names readable as a lisp expression even if they
15867     ;; contain special characters.
15868     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15869     (goto-char (point-max))
15870     (while (re-search-backward "[][';?()#]" nil t)
15871       (insert ?\\))
15872     ;; If these are groups from a foreign select method, we insert the
15873     ;; group prefix in front of the group names.
15874     (and method (not (gnus-server-equal
15875                       (gnus-server-get-method nil method)
15876                       (gnus-server-get-method nil gnus-select-method)))
15877          (let ((prefix (gnus-group-prefixed-name "" method)))
15878            (goto-char (point-min))
15879            (while (and (not (eobp))
15880                        (progn (insert prefix)
15881                               (zerop (forward-line 1)))))))
15882     ;; Store the active file in a hash table.
15883     (goto-char (point-min))
15884     (if (string-match "%[oO]" gnus-group-line-format)
15885         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
15886         ;; If we want information on moderated groups, we use this
15887         ;; loop...
15888         (let* ((mod-hashtb (make-vector 7 0))
15889                (m (intern "m" mod-hashtb))
15890                group max min)
15891           (while (not (eobp))
15892             (condition-case nil
15893                 (progn
15894                   (narrow-to-region (point) (gnus-point-at-eol))
15895                   (setq group (let ((obarray hashtb)) (read cur)))
15896                   (if (and (numberp (setq max (read cur)))
15897                            (numberp (setq min (read cur)))
15898                            (progn
15899                              (skip-chars-forward " \t")
15900                              (not
15901                               (or (= (following-char) ?=)
15902                                   (= (following-char) ?x)
15903                                   (= (following-char) ?j)))))
15904                       (set group (cons min max))
15905                     (set group nil))
15906                   ;; Enter moderated groups into a list.
15907                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
15908                       (setq gnus-moderated-list
15909                             (cons (symbol-name group) gnus-moderated-list))))
15910               (error
15911                (and group
15912                     (symbolp group)
15913                     (set group nil))))
15914             (widen)
15915             (forward-line 1)))
15916       ;; And if we do not care about moderation, we use this loop,
15917       ;; which is faster.
15918       (let (group max min)
15919         (while (not (eobp))
15920           (condition-case ()
15921               (progn
15922                 (narrow-to-region (point) (gnus-point-at-eol))
15923                 ;; group gets set to a symbol interned in the hash table
15924                 ;; (what a hack!!) - jwz
15925                 (setq group (let ((obarray hashtb)) (read cur)))
15926                 (if (and (numberp (setq max (read cur)))
15927                          (numberp (setq min (read cur)))
15928                          (progn
15929                            (skip-chars-forward " \t")
15930                            (not
15931                             (or (= (following-char) ?=)
15932                                 (= (following-char) ?x)
15933                                 (= (following-char) ?j)))))
15934                     (set group (cons min max))
15935                   (set group nil)))
15936             (error
15937              (progn
15938                (and group
15939                     (symbolp group)
15940                     (set group nil))
15941                (or ignore-errors
15942                    (gnus-message 3 "Warning - illegal active: %s"
15943                                  (buffer-substring
15944                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
15945           (widen)
15946           (forward-line 1))))))
15947
15948 (defun gnus-groups-to-gnus-format (method &optional hashtb)
15949   ;; Parse a "groups" active file.
15950   (let ((cur (current-buffer))
15951         (hashtb (or hashtb
15952                     (if (and method gnus-active-hashtb)
15953                         gnus-active-hashtb
15954                       (setq gnus-active-hashtb
15955                             (gnus-make-hashtable
15956                              (count-lines (point-min) (point-max)))))))
15957         (prefix (and method
15958                      (not (gnus-server-equal
15959                            (gnus-server-get-method nil method)
15960                            (gnus-server-get-method nil gnus-select-method)))
15961                      (gnus-group-prefixed-name "" method))))
15962
15963     (goto-char (point-min))
15964     ;; We split this into to separate loops, one with the prefix
15965     ;; and one without to speed the reading up somewhat.
15966     (if prefix
15967         (let (min max opoint group)
15968           (while (not (eobp))
15969             (condition-case ()
15970                 (progn
15971                   (read cur) (read cur)
15972                   (setq min (read cur)
15973                         max (read cur)
15974                         opoint (point))
15975                   (skip-chars-forward " \t")
15976                   (insert prefix)
15977                   (goto-char opoint)
15978                   (set (let ((obarray hashtb)) (read cur))
15979                        (cons min max)))
15980               (error (and group (symbolp group) (set group nil))))
15981             (forward-line 1)))
15982       (let (min max group)
15983         (while (not (eobp))
15984           (condition-case ()
15985               (if (= (following-char) ?2)
15986                   (progn
15987                     (read cur) (read cur)
15988                     (setq min (read cur)
15989                           max (read cur))
15990                     (set (setq group (let ((obarray hashtb)) (read cur)))
15991                          (cons min max))))
15992             (error (and group (symbolp group) (set group nil))))
15993           (forward-line 1))))))
15994
15995 (defun gnus-read-newsrc-file (&optional force)
15996   "Read startup file.
15997 If FORCE is non-nil, the .newsrc file is read."
15998   ;; Reset variables that might be defined in the .newsrc.eld file.
15999   (let ((variables gnus-variable-list))
16000     (while variables
16001       (set (car variables) nil)
16002       (setq variables (cdr variables))))
16003   (let* ((newsrc-file gnus-current-startup-file)
16004          (quick-file (concat newsrc-file ".el")))
16005     (save-excursion
16006       ;; We always load the .newsrc.eld file.  If always contains
16007       ;; much information that can not be gotten from the .newsrc
16008       ;; file (ticked articles, killed groups, foreign methods, etc.)
16009       (gnus-read-newsrc-el-file quick-file)
16010
16011       (if (and (file-exists-p gnus-current-startup-file)
16012                (or force
16013                    (and (file-newer-than-file-p newsrc-file quick-file)
16014                         (file-newer-than-file-p newsrc-file
16015                                                 (concat quick-file "d")))
16016                    (not gnus-newsrc-alist)))
16017           ;; We read the .newsrc file.  Note that if there if a
16018           ;; .newsrc.eld file exists, it has already been read, and
16019           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16020           ;; the .newsrc file, Gnus will only use the information it
16021           ;; can find there for changing the data already read -
16022           ;; ie. reading the .newsrc file will not trash the data
16023           ;; already read (except for read articles).
16024           (save-excursion
16025             (gnus-message 5 "Reading %s..." newsrc-file)
16026             (set-buffer (find-file-noselect newsrc-file))
16027             (buffer-disable-undo (current-buffer))
16028             (gnus-newsrc-to-gnus-format)
16029             (kill-buffer (current-buffer))
16030             (gnus-message 5 "Reading %s...done" newsrc-file)))
16031
16032       ;; Read any slave files.
16033       (or gnus-slave
16034           (gnus-master-read-slave-newsrc)))))
16035
16036 (defun gnus-read-newsrc-el-file (file)
16037   (let ((ding-file (concat file "d")))
16038     ;; We always, always read the .eld file.
16039     (gnus-message 5 "Reading %s..." ding-file)
16040     (let (gnus-newsrc-assoc)
16041       (condition-case nil
16042           (load ding-file t t t)
16043         (error
16044          (gnus-error 1 "Error in %s" ding-file)))
16045       (when gnus-newsrc-assoc
16046         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16047     (gnus-make-hashtable-from-newsrc-alist)
16048     (when (file-newer-than-file-p file ding-file)
16049       ;; Old format quick file
16050       (gnus-message 5 "Reading %s..." file)
16051       ;; The .el file is newer than the .eld file, so we read that one
16052       ;; as well.
16053       (gnus-read-old-newsrc-el-file file))))
16054
16055 ;; Parse the old-style quick startup file
16056 (defun gnus-read-old-newsrc-el-file (file)
16057   (let (newsrc killed marked group m info)
16058     (prog1
16059         (let ((gnus-killed-assoc nil)
16060               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16061           (prog1
16062               (condition-case nil
16063                   (load file t t t)
16064                 (error nil))
16065             (setq newsrc gnus-newsrc-assoc
16066                   killed gnus-killed-assoc
16067                   marked gnus-marked-assoc)))
16068       (setq gnus-newsrc-alist nil)
16069       (while (setq info (gnus-get-info (setq group (pop newsrc))))
16070         (if info
16071             (progn
16072               (gnus-info-set-read info (cddr group))
16073               (gnus-info-set-level
16074                info (if (nth 1 group) gnus-level-default-subscribed
16075                       gnus-level-default-unsubscribed))
16076               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16077           (push (setq info
16078                       (list (car group)
16079                             (if (nth 1 group) gnus-level-default-subscribed
16080                               gnus-level-default-unsubscribed)
16081                             (cddr group)))
16082                 gnus-newsrc-alist))
16083         ;; Copy marks into info.
16084         (when (setq m (assoc (car group) marked))
16085           (unless (nthcdr 3 info)
16086             (nconc info (list nil)))
16087           (gnus-info-set-marks
16088            info (list (cons 'tick (gnus-compress-sequence 
16089                                    (sort (cdr m) '<) t))))))
16090       (setq newsrc killed)
16091       (while newsrc
16092         (setcar newsrc (caar newsrc))
16093         (setq newsrc (cdr newsrc)))
16094       (setq gnus-killed-list killed))
16095     ;; The .el file version of this variable does not begin with
16096     ;; "options", while the .eld version does, so we just add it if it
16097     ;; isn't there.
16098     (and
16099      gnus-newsrc-options
16100      (progn
16101        (and (not (string-match "^ *options" gnus-newsrc-options))
16102             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16103        (and (not (string-match "\n$" gnus-newsrc-options))
16104             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16105        ;; Finally, if we read some options lines, we parse them.
16106        (or (string= gnus-newsrc-options "")
16107            (gnus-newsrc-parse-options gnus-newsrc-options))))
16108
16109     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16110     (gnus-make-hashtable-from-newsrc-alist)))
16111
16112 (defun gnus-make-newsrc-file (file)
16113   "Make server dependent file name by catenating FILE and server host name."
16114   (let* ((file (expand-file-name file nil))
16115          (real-file (concat file "-" (nth 1 gnus-select-method))))
16116     (if (or (file-exists-p real-file)
16117             (file-exists-p (concat real-file ".el"))
16118             (file-exists-p (concat real-file ".eld")))
16119         real-file file)))
16120
16121 (defun gnus-newsrc-to-gnus-format ()
16122   (setq gnus-newsrc-options "")
16123   (setq gnus-newsrc-options-n nil)
16124
16125   (or gnus-active-hashtb
16126       (setq gnus-active-hashtb (make-vector 4095 0)))
16127   (let ((buf (current-buffer))
16128         (already-read (> (length gnus-newsrc-alist) 1))
16129         group subscribed options-symbol newsrc Options-symbol
16130         symbol reads num1)
16131     (goto-char (point-min))
16132     ;; We intern the symbol `options' in the active hashtb so that we
16133     ;; can `eq' against it later.
16134     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16135     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16136
16137     (while (not (eobp))
16138       ;; We first read the first word on the line by narrowing and
16139       ;; then reading into `gnus-active-hashtb'.  Most groups will
16140       ;; already exist in that hashtb, so this will save some string
16141       ;; space.
16142       (narrow-to-region
16143        (point)
16144        (progn (skip-chars-forward "^ \t!:\n") (point)))
16145       (goto-char (point-min))
16146       (setq symbol
16147             (and (/= (point-min) (point-max))
16148                  (let ((obarray gnus-active-hashtb)) (read buf))))
16149       (widen)
16150       ;; Now, the symbol we have read is either `options' or a group
16151       ;; name.  If it is an options line, we just add it to a string.
16152       (cond
16153        ((or (eq symbol options-symbol)
16154             (eq symbol Options-symbol))
16155         (setq gnus-newsrc-options
16156               ;; This concating is quite inefficient, but since our
16157               ;; thorough studies show that approx 99.37% of all
16158               ;; .newsrc files only contain a single options line, we
16159               ;; don't give a damn, frankly, my dear.
16160               (concat gnus-newsrc-options
16161                       (buffer-substring
16162                        (gnus-point-at-bol)
16163                        ;; Options may continue on the next line.
16164                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16165                                 (progn (beginning-of-line) (point)))
16166                            (point)))))
16167         (forward-line -1))
16168        (symbol
16169         ;; Group names can be just numbers.  
16170         (when (numberp symbol) 
16171           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16172         (or (boundp symbol) (set symbol nil))
16173         ;; It was a group name.
16174         (setq subscribed (= (following-char) ?:)
16175               group (symbol-name symbol)
16176               reads nil)
16177         (if (eolp)
16178             ;; If the line ends here, this is clearly a buggy line, so
16179             ;; we put point a the beginning of line and let the cond
16180             ;; below do the error handling.
16181             (beginning-of-line)
16182           ;; We skip to the beginning of the ranges.
16183           (skip-chars-forward "!: \t"))
16184         ;; We are now at the beginning of the list of read articles.
16185         ;; We read them range by range.
16186         (while
16187             (cond
16188              ((looking-at "[0-9]+")
16189               ;; We narrow and read a number instead of buffer-substring/
16190               ;; string-to-int because it's faster.  narrow/widen is
16191               ;; faster than save-restriction/narrow, and save-restriction
16192               ;; produces a garbage object.
16193               (setq num1 (progn
16194                            (narrow-to-region (match-beginning 0) (match-end 0))
16195                            (read buf)))
16196               (widen)
16197               ;; If the next character is a dash, then this is a range.
16198               (if (= (following-char) ?-)
16199                   (progn
16200                     ;; We read the upper bound of the range.
16201                     (forward-char 1)
16202                     (if (not (looking-at "[0-9]+"))
16203                         ;; This is a buggy line, by we pretend that
16204                         ;; it's kinda OK.  Perhaps the user should be
16205                         ;; dinged?
16206                         (setq reads (cons num1 reads))
16207                       (setq reads
16208                             (cons
16209                              (cons num1
16210                                    (progn
16211                                      (narrow-to-region (match-beginning 0)
16212                                                        (match-end 0))
16213                                      (read buf)))
16214                              reads))
16215                       (widen)))
16216                 ;; It was just a simple number, so we add it to the
16217                 ;; list of ranges.
16218                 (setq reads (cons num1 reads)))
16219               ;; If the next char in ?\n, then we have reached the end
16220               ;; of the line and return nil.
16221               (/= (following-char) ?\n))
16222              ((= (following-char) ?\n)
16223               ;; End of line, so we end.
16224               nil)
16225              (t
16226               ;; Not numbers and not eol, so this might be a buggy
16227               ;; line...
16228               (or (eobp)
16229                   ;; If it was eob instead of ?\n, we allow it.
16230                   (progn
16231                     ;; The line was buggy.
16232                     (setq group nil)
16233                     (gnus-error 3.1 "Mangled line: %s"
16234                                 (buffer-substring (gnus-point-at-bol)
16235                                                   (gnus-point-at-eol)))))
16236               nil))
16237           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16238           ;; we allow them, because it's a common mistake to put a
16239           ;; space after the comma.
16240           (skip-chars-forward ", "))
16241
16242         ;; We have already read .newsrc.eld, so we gently update the
16243         ;; data in the hash table with the information we have just
16244         ;; read.
16245         (when group
16246           (let ((info (gnus-get-info group))
16247                 level)
16248             (if info
16249                 ;; There is an entry for this file in the alist.
16250                 (progn
16251                   (gnus-info-set-read info (nreverse reads))
16252                   ;; We update the level very gently.  In fact, we
16253                   ;; only change it if there's been a status change
16254                   ;; from subscribed to unsubscribed, or vice versa.
16255                   (setq level (gnus-info-level info))
16256                   (cond ((and (<= level gnus-level-subscribed)
16257                               (not subscribed))
16258                          (setq level (if reads
16259                                          gnus-level-default-unsubscribed
16260                                        (1+ gnus-level-default-unsubscribed))))
16261                         ((and (> level gnus-level-subscribed) subscribed)
16262                          (setq level gnus-level-default-subscribed)))
16263                   (gnus-info-set-level info level))
16264               ;; This is a new group.
16265               (setq info (list group
16266                                (if subscribed
16267                                    gnus-level-default-subscribed
16268                                  (if reads
16269                                      (1+ gnus-level-subscribed)
16270                                    gnus-level-default-unsubscribed))
16271                                (nreverse reads))))
16272             (setq newsrc (cons info newsrc))))))
16273       (forward-line 1))
16274
16275     (setq newsrc (nreverse newsrc))
16276
16277     (if (not already-read)
16278         ()
16279       ;; We now have two newsrc lists - `newsrc', which is what we
16280       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16281       ;; what we've read from .newsrc.eld.  We have to merge these
16282       ;; lists.  We do this by "attaching" any (foreign) groups in the
16283       ;; gnus-newsrc-alist to the (native) group that precedes them.
16284       (let ((rc (cdr gnus-newsrc-alist))
16285             (prev gnus-newsrc-alist)
16286             entry mentry)
16287         (while rc
16288           (or (null (nth 4 (car rc)))   ; It's a native group.
16289               (assoc (caar rc) newsrc) ; It's already in the alist.
16290               (if (setq entry (assoc (caar prev) newsrc))
16291                   (setcdr (setq mentry (memq entry newsrc))
16292                           (cons (car rc) (cdr mentry)))
16293                 (setq newsrc (cons (car rc) newsrc))))
16294           (setq prev rc
16295                 rc (cdr rc)))))
16296
16297     (setq gnus-newsrc-alist newsrc)
16298     ;; We make the newsrc hashtb.
16299     (gnus-make-hashtable-from-newsrc-alist)
16300
16301     ;; Finally, if we read some options lines, we parse them.
16302     (or (string= gnus-newsrc-options "")
16303         (gnus-newsrc-parse-options gnus-newsrc-options))))
16304
16305 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16306 ;; The return value will be a list on the form
16307 ;; ((regexp1 . ignore)
16308 ;;  (regexp2 . subscribe)...)
16309 ;; When handling new newsgroups, groups that match a `ignore' regexp
16310 ;; will be ignored, and groups that match a `subscribe' regexp will be
16311 ;; subscribed.  A line like
16312 ;; options -n !all rec.all
16313 ;; will lead to a list that looks like
16314 ;; (("^rec\\..+" . subscribe)
16315 ;;  ("^.+" . ignore))
16316 ;; So all "rec.*" groups will be subscribed, while all the other
16317 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16318 ;; different from "options -n rec.all !all".
16319 (defun gnus-newsrc-parse-options (options)
16320   (let (out eol)
16321     (save-excursion
16322       (gnus-set-work-buffer)
16323       (insert (regexp-quote options))
16324       ;; First we treat all continuation lines.
16325       (goto-char (point-min))
16326       (while (re-search-forward "\n[ \t]+" nil t)
16327         (replace-match " " t t))
16328       ;; Then we transform all "all"s into ".+"s.
16329       (goto-char (point-min))
16330       (while (re-search-forward "\\ball\\b" nil t)
16331         (replace-match ".+" t t))
16332       (goto-char (point-min))
16333       ;; We remove all other options than the "-n" ones.
16334       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16335         (replace-match " ")
16336         (forward-char -1))
16337       (goto-char (point-min))
16338
16339       ;; We are only interested in "options -n" lines - we
16340       ;; ignore the other option lines.
16341       (while (re-search-forward "[ \t]-n" nil t)
16342         (setq eol
16343               (or (save-excursion
16344                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16345                          (- (point) 2)))
16346                   (gnus-point-at-eol)))
16347         ;; Search for all "words"...
16348         (while (re-search-forward "[^ \t,\n]+" eol t)
16349           (if (= (char-after (match-beginning 0)) ?!)
16350               ;; If the word begins with a bang (!), this is a "not"
16351               ;; spec.  We put this spec (minus the bang) and the
16352               ;; symbol `ignore' into the list.
16353               (setq out (cons (cons (concat
16354                                      "^" (buffer-substring
16355                                           (1+ (match-beginning 0))
16356                                           (match-end 0)))
16357                                     'ignore) out))
16358             ;; There was no bang, so this is a "yes" spec.
16359             (setq out (cons (cons (concat "^" (match-string 0))
16360                                   'subscribe) out)))))
16361
16362       (setq gnus-newsrc-options-n out))))
16363
16364 (defun gnus-save-newsrc-file (&optional force)
16365   "Save .newsrc file."
16366   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16367   ;; from the variable gnus-newsrc-alist.
16368   (when (and (or gnus-newsrc-alist gnus-killed-list)
16369              gnus-current-startup-file)
16370     (save-excursion
16371       (if (and (or gnus-use-dribble-file gnus-slave)
16372                (not force)
16373                (or (not gnus-dribble-buffer)
16374                    (not (buffer-name gnus-dribble-buffer))
16375                    (zerop (save-excursion
16376                             (set-buffer gnus-dribble-buffer)
16377                             (buffer-size)))))
16378           (gnus-message 4 "(No changes need to be saved)")
16379         (run-hooks 'gnus-save-newsrc-hook)
16380         (if gnus-slave
16381             (gnus-slave-save-newsrc)
16382           ;; Save .newsrc.
16383           (when gnus-save-newsrc-file
16384             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16385             (gnus-gnus-to-newsrc-format)
16386             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16387           ;; Save .newsrc.eld.
16388           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16389           (make-local-variable 'version-control)
16390           (setq version-control 'never)
16391           (setq buffer-file-name
16392                 (concat gnus-current-startup-file ".eld"))
16393           (gnus-add-current-to-buffer-list)
16394           (buffer-disable-undo (current-buffer))
16395           (erase-buffer)
16396           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16397           (gnus-gnus-to-quick-newsrc-format)
16398           (run-hooks 'gnus-save-quick-newsrc-hook)
16399           (save-buffer)
16400           (kill-buffer (current-buffer))
16401           (gnus-message
16402            5 "Saving %s.eld...done" gnus-current-startup-file))
16403         (gnus-dribble-delete-file)))))
16404
16405 (defun gnus-gnus-to-quick-newsrc-format ()
16406   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16407   (insert ";; Gnus startup file.\n")
16408   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16409   (insert ";; to read .newsrc.\n")
16410   (insert "(setq gnus-newsrc-file-version "
16411           (prin1-to-string gnus-version) ")\n")
16412   (let ((variables
16413          (if gnus-save-killed-list gnus-variable-list
16414            ;; Remove the `gnus-killed-list' from the list of variables
16415            ;; to be saved, if required.
16416            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16417         ;; Peel off the "dummy" group.
16418         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16419         variable)
16420     ;; Insert the variables into the file.
16421     (while variables
16422       (when (and (boundp (setq variable (pop variables)))
16423                  (symbol-value variable))
16424         (insert "(setq " (symbol-name variable) " '")
16425         (prin1 (symbol-value variable) (current-buffer))
16426         (insert ")\n")))))
16427
16428 (defun gnus-gnus-to-newsrc-format ()
16429   ;; Generate and save the .newsrc file.
16430   (save-excursion
16431     (set-buffer (create-file-buffer gnus-current-startup-file))
16432     (let ((newsrc (cdr gnus-newsrc-alist))
16433           (standard-output (current-buffer))
16434           info ranges range method)
16435       (setq buffer-file-name gnus-current-startup-file)
16436       (buffer-disable-undo (current-buffer))
16437       (erase-buffer)
16438       ;; Write options.
16439       (if gnus-newsrc-options (insert gnus-newsrc-options))
16440       ;; Write subscribed and unsubscribed.
16441       (while (setq info (pop newsrc))
16442         ;; Don't write foreign groups to .newsrc.
16443         (when (or (null (setq method (gnus-info-method info)))
16444                   (equal method "native")
16445                   (gnus-server-equal method gnus-select-method))
16446           (insert (gnus-info-group info)
16447                   (if (> (gnus-info-level info) gnus-level-subscribed)
16448                       "!" ":"))
16449           (when (setq ranges (gnus-info-read info))
16450             (insert " ")
16451             (if (not (listp (cdr ranges)))
16452                 (if (= (car ranges) (cdr ranges))
16453                     (princ (car ranges))
16454                   (princ (car ranges))
16455                   (insert "-")
16456                   (princ (cdr ranges)))
16457               (while (setq range (pop ranges))
16458                 (if (or (atom range) (= (car range) (cdr range)))
16459                     (princ (or (and (atom range) range) (car range)))
16460                   (princ (car range))
16461                   (insert "-")
16462                   (princ (cdr range)))
16463                 (if ranges (insert ",")))))
16464           (insert "\n")))
16465       (make-local-variable 'version-control)
16466       (setq version-control 'never)
16467       ;; It has been reported that sometime the modtime on the .newsrc
16468       ;; file seems to be off.  We really do want to overwrite it, so
16469       ;; we clear the modtime here before saving.  It's a bit odd,
16470       ;; though...
16471       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16472       ;; delete the silly thing entirely first.  but this fails to provide
16473       ;; such niceties as .newsrc~ creation.
16474       (if gnus-modtime-botch
16475           (delete-file gnus-startup-file)
16476         (clear-visited-file-modtime))
16477       (run-hooks 'gnus-save-standard-newsrc-hook)
16478       (save-buffer)
16479       (kill-buffer (current-buffer)))))
16480
16481 \f
16482 ;;;
16483 ;;; Slave functions.
16484 ;;;
16485
16486 (defun gnus-slave-save-newsrc ()
16487   (save-excursion
16488     (set-buffer gnus-dribble-buffer)
16489     (let ((slave-name
16490            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16491       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16492
16493 (defun gnus-master-read-slave-newsrc ()
16494   (let ((slave-files
16495          (directory-files
16496           (file-name-directory gnus-current-startup-file)
16497           t (concat
16498              "^" (regexp-quote
16499                   (concat
16500                    (file-name-nondirectory gnus-current-startup-file)
16501                    "-slave-")))
16502           t))
16503         file)
16504     (if (not slave-files)
16505         ()                              ; There are no slave files to read.
16506       (gnus-message 7 "Reading slave newsrcs...")
16507       (save-excursion
16508         (set-buffer (get-buffer-create " *gnus slave*"))
16509         (buffer-disable-undo (current-buffer))
16510         (setq slave-files
16511               (sort (mapcar (lambda (file)
16512                               (list (nth 5 (file-attributes file)) file))
16513                             slave-files)
16514                     (lambda (f1 f2)
16515                       (or (< (caar f1) (caar f2))
16516                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16517         (while slave-files
16518           (erase-buffer)
16519           (setq file (nth 1 (car slave-files)))
16520           (insert-file-contents file)
16521           (if (condition-case ()
16522                   (progn
16523                     (eval-buffer (current-buffer))
16524                     t)
16525                 (error
16526                  (gnus-error 3.2 "Possible error in %s" file)
16527                  nil))
16528               (or gnus-slave ; Slaves shouldn't delete these files.
16529                   (condition-case ()
16530                       (delete-file file)
16531                     (error nil))))
16532           (setq slave-files (cdr slave-files))))
16533       (gnus-message 7 "Reading slave newsrcs...done"))))
16534
16535 \f
16536 ;;;
16537 ;;; Group description.
16538 ;;;
16539
16540 (defun gnus-read-all-descriptions-files ()
16541   (let ((methods (cons gnus-select-method 
16542                        (nconc
16543                         (when gnus-message-archive-method
16544                           (list "archive"))
16545                         gnus-secondary-select-methods))))
16546     (while methods
16547       (gnus-read-descriptions-file (car methods))
16548       (setq methods (cdr methods)))
16549     t))
16550
16551 (defun gnus-read-descriptions-file (&optional method)
16552   (let ((method (or method gnus-select-method)))
16553     (when (stringp method)
16554       (setq method (gnus-server-to-method method)))
16555     ;; We create the hashtable whether we manage to read the desc file
16556     ;; to avoid trying to re-read after a failed read.
16557     (or gnus-description-hashtb
16558         (setq gnus-description-hashtb
16559               (gnus-make-hashtable (length gnus-active-hashtb))))
16560     ;; Mark this method's desc file as read.
16561     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16562                   gnus-description-hashtb)
16563
16564     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16565     (cond
16566      ((not (gnus-check-server method))
16567       (gnus-message 1 "Couldn't open server")
16568       nil)
16569      ((not (gnus-request-list-newsgroups method))
16570       (gnus-message 1 "Couldn't read newsgroups descriptions")
16571       nil)
16572      (t
16573       (let (group)
16574         (save-excursion
16575           (save-restriction
16576             (set-buffer nntp-server-buffer)
16577             (goto-char (point-min))
16578             (if (or (search-forward "\n.\n" nil t)
16579                     (goto-char (point-max)))
16580                 (progn
16581                   (beginning-of-line)
16582                   (narrow-to-region (point-min) (point))))
16583             (goto-char (point-min))
16584             (while (not (eobp))
16585               ;; If we get an error, we set group to 0, which is not a
16586               ;; symbol...
16587               (setq group
16588                     (condition-case ()
16589                         (let ((obarray gnus-description-hashtb))
16590                           ;; Group is set to a symbol interned in this
16591                           ;; hash table.
16592                           (read nntp-server-buffer))
16593                       (error 0)))
16594               (skip-chars-forward " \t")
16595               ;; ...  which leads to this line being effectively ignored.
16596               (and (symbolp group)
16597                    (set group (buffer-substring
16598                                (point) (progn (end-of-line) (point)))))
16599               (forward-line 1))))
16600         (gnus-message 5 "Reading descriptions file...done")
16601         t)))))
16602
16603 (defun gnus-group-get-description (group)
16604   "Get the description of a group by sending XGTITLE to the server."
16605   (when (gnus-request-group-description group)
16606     (save-excursion
16607       (set-buffer nntp-server-buffer)
16608       (goto-char (point-min))
16609       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16610         (match-string 1)))))
16611
16612 \f
16613 ;;;
16614 ;;; Buffering of read articles.
16615 ;;;
16616
16617 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16618 (defvar gnus-backlog-articles nil)
16619 (defvar gnus-backlog-hashtb nil)
16620
16621 (defun gnus-backlog-buffer ()
16622   "Return the backlog buffer."
16623   (or (get-buffer gnus-backlog-buffer)
16624       (save-excursion
16625         (set-buffer (get-buffer-create gnus-backlog-buffer))
16626         (buffer-disable-undo (current-buffer))
16627         (setq buffer-read-only t)
16628         (gnus-add-current-to-buffer-list)
16629         (get-buffer gnus-backlog-buffer))))
16630
16631 (defun gnus-backlog-setup ()
16632   "Initialize backlog variables."
16633   (unless gnus-backlog-hashtb
16634     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16635
16636 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
16637
16638 (defun gnus-backlog-shutdown ()
16639   "Clear all backlog variables and buffers."
16640   (when (get-buffer gnus-backlog-buffer)
16641     (kill-buffer gnus-backlog-buffer))
16642   (setq gnus-backlog-hashtb nil
16643         gnus-backlog-articles nil))
16644
16645 (defun gnus-backlog-enter-article (group number buffer)
16646   (gnus-backlog-setup)
16647   (let ((ident (intern (concat group ":" (int-to-string number))
16648                        gnus-backlog-hashtb))
16649         b)
16650     (if (memq ident gnus-backlog-articles)
16651         () ; It's already kept.
16652       ;; Remove the oldest article, if necessary.
16653       (and (numberp gnus-keep-backlog)
16654            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16655            (gnus-backlog-remove-oldest-article))
16656       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16657       ;; Insert the new article.
16658       (save-excursion
16659         (set-buffer (gnus-backlog-buffer))
16660         (let (buffer-read-only)
16661           (goto-char (point-max))
16662           (or (bolp) (insert "\n"))
16663           (setq b (point))
16664           (insert-buffer-substring buffer)
16665           ;; Tag the beginning of the article with the ident.
16666           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16667
16668 (defun gnus-backlog-remove-oldest-article ()
16669   (save-excursion
16670     (set-buffer (gnus-backlog-buffer))
16671     (goto-char (point-min))
16672     (if (zerop (buffer-size))
16673         () ; The buffer is empty.
16674       (let ((ident (get-text-property (point) 'gnus-backlog))
16675             buffer-read-only)
16676         ;; Remove the ident from the list of articles.
16677         (when ident
16678           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16679         ;; Delete the article itself.
16680         (delete-region
16681          (point) (next-single-property-change
16682                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16683
16684 (defun gnus-backlog-request-article (group number buffer)
16685   (when (numberp number)
16686     (gnus-backlog-setup)
16687     (let ((ident (intern (concat group ":" (int-to-string number))
16688                          gnus-backlog-hashtb))
16689           beg end)
16690       (when (memq ident gnus-backlog-articles)
16691         ;; It was in the backlog.
16692         (save-excursion
16693           (set-buffer (gnus-backlog-buffer))
16694           (if (not (setq beg (text-property-any
16695                               (point-min) (point-max) 'gnus-backlog
16696                               ident)))
16697               ;; It wasn't in the backlog after all.
16698               (ignore
16699                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16700             ;; Find the end (i. e., the beginning of the next article).
16701             (setq end
16702                   (next-single-property-change
16703                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16704         (let ((buffer-read-only nil))
16705           (erase-buffer)
16706           (insert-buffer-substring gnus-backlog-buffer beg end)
16707           t)))))
16708
16709 ;; Allow redefinition of Gnus functions.
16710
16711 (gnus-ems-redefine)
16712
16713 (provide 'gnus)
16714
16715 ;;; gnus.el ends here